1 |
twoaday |
133 |
/* wptGPGMEWrapper.cpp - Object oriented wrapper for GPGME |
2 |
twoaday |
304 |
* Copyright (C) 2006, 2007 Timo Schulz |
3 |
twoaday |
133 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <string.h> |
28 |
|
|
|
29 |
|
|
#include "gpgme.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptGPGME.h" |
32 |
twoaday |
195 |
#include "wptW32API.h" |
33 |
twoaday |
133 |
|
34 |
|
|
|
35 |
|
|
/* Constructor to build gpgme context. */ |
36 |
|
|
GPGME::GPGME () |
37 |
|
|
{ |
38 |
|
|
gpgme_new (&ctx); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/* Destructor to release gpgme context. */ |
42 |
|
|
GPGME::~GPGME () |
43 |
|
|
{ |
44 |
|
|
if (ctx != NULL) |
45 |
|
|
gpgme_release (ctx); |
46 |
|
|
} |
47 |
|
|
|
48 |
twoaday |
167 |
/* Enable or disable the feature to add the |
49 |
|
|
WinPT version to armored output. */ |
50 |
twoaday |
133 |
void |
51 |
|
|
GPGME::setChangeVersion (bool val) |
52 |
|
|
{ |
53 |
|
|
chg_ver = val; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/* Enable or disable armor. */ |
57 |
|
|
void |
58 |
|
|
GPGME::setArmor (bool val) |
59 |
|
|
{ |
60 |
|
|
gpgme_set_armor (ctx, val? 1 : 0); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
bool |
64 |
|
|
GPGME::getArmor (void) |
65 |
|
|
{ |
66 |
|
|
return gpgme_get_armor (ctx)? true: false; |
67 |
|
|
} |
68 |
|
|
|
69 |
twoaday |
260 |
|
70 |
twoaday |
262 |
/* Import keys from a memory buffer. |
71 |
|
|
Return value: 0 on success. */ |
72 |
|
|
gpgme_error_t |
73 |
|
|
GPGME::importFromBuffer (const char *indata) |
74 |
|
|
{ |
75 |
|
|
gpgme_data_t keydata = NULL; |
76 |
|
|
gpgme_error_t err; |
77 |
|
|
|
78 |
|
|
err = gpgme_data_new_from_mem (&keydata, indata, strlen (indata), 1); |
79 |
|
|
if (err) |
80 |
|
|
return err; |
81 |
|
|
|
82 |
|
|
op_begin (); |
83 |
|
|
err = gpgme_op_import (ctx, keydata); |
84 |
|
|
op_end (); |
85 |
|
|
|
86 |
|
|
gpgme_data_release (keydata); |
87 |
|
|
return err; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
twoaday |
260 |
/* Import the keys from the clipboard. |
92 |
|
|
Return value: 0 on success. */ |
93 |
|
|
gpgme_error_t |
94 |
|
|
GPGME::importFromClipboard (void) |
95 |
|
|
{ |
96 |
|
|
gpgme_data_t keydata = NULL; |
97 |
|
|
gpgme_error_t err; |
98 |
|
|
|
99 |
|
|
err = gpg_data_new_from_clipboard (&keydata, 0); |
100 |
|
|
if (err) |
101 |
|
|
return err; |
102 |
|
|
|
103 |
|
|
op_begin (); |
104 |
|
|
err = gpgme_op_import (ctx, keydata); |
105 |
|
|
op_end (); |
106 |
|
|
|
107 |
|
|
gpgme_data_release (keydata); |
108 |
|
|
return err; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
|
112 |
|
|
/* Import the keys from the file @file. |
113 |
|
|
Return value: 0 on success. */ |
114 |
|
|
gpgme_error_t |
115 |
|
|
GPGME::importFromFile (const char *file) |
116 |
|
|
{ |
117 |
|
|
gpgme_data_t keydata; |
118 |
|
|
gpgme_error_t err; |
119 |
|
|
|
120 |
|
|
err = gpgme_data_new_from_file (&keydata, file, 1); |
121 |
|
|
if (err) |
122 |
|
|
return err; |
123 |
|
|
|
124 |
|
|
op_begin (); |
125 |
|
|
err = gpgme_op_import (ctx, keydata); |
126 |
|
|
op_end (); |
127 |
|
|
|
128 |
|
|
gpgme_data_release (keydata); |
129 |
|
|
return err; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
/* Return the import result for the last GPG operation. */ |
134 |
|
|
gpgme_import_result_t |
135 |
|
|
GPGME::importGetResult (void) |
136 |
|
|
{ |
137 |
|
|
return gpgme_op_import_result (ctx); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
|
141 |
twoaday |
303 |
/* Export a single key or all keys which matches the pattern @patt |
142 |
|
|
to file @outfile. |
143 |
twoaday |
133 |
Return value: 0 on success. */ |
144 |
|
|
gpgme_error_t |
145 |
|
|
GPGME::exportToFile (const char *patt, const char *outfile) |
146 |
|
|
{ |
147 |
|
|
gpgme_data_t dat; |
148 |
|
|
gpgme_error_t err; |
149 |
|
|
|
150 |
|
|
err = gpgme_data_new (&dat); |
151 |
|
|
if (err) |
152 |
|
|
return err; |
153 |
twoaday |
167 |
op_begin (); |
154 |
twoaday |
133 |
err = gpgme_op_export (ctx, patt, 0, dat); |
155 |
twoaday |
167 |
op_end (); |
156 |
twoaday |
303 |
if (err) |
157 |
twoaday |
133 |
gpgme_data_release (dat); |
158 |
twoaday |
303 |
else { |
159 |
|
|
err = gpg_data_release_and_set_file (dat, outfile); |
160 |
|
|
if (err) |
161 |
|
|
gpgme_data_release (dat); |
162 |
twoaday |
133 |
} |
163 |
twoaday |
303 |
|
164 |
|
|
return err; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/* Export multiple keys using @pattern. */ |
168 |
|
|
gpgme_error_t |
169 |
|
|
GPGME::exportToFile (const char *pattern[], const char *outfile) |
170 |
|
|
{ |
171 |
|
|
gpgme_error_t err; |
172 |
|
|
gpgme_data_t dat; |
173 |
|
|
|
174 |
|
|
err = gpgme_data_new (&dat); |
175 |
twoaday |
247 |
if (err) |
176 |
twoaday |
303 |
return err; |
177 |
|
|
|
178 |
|
|
op_begin (); |
179 |
|
|
err = gpgme_op_export_ext (ctx, pattern, 0, dat); |
180 |
|
|
op_end (); |
181 |
|
|
if (err) |
182 |
twoaday |
247 |
gpgme_data_release (dat); |
183 |
twoaday |
303 |
else { |
184 |
|
|
err = gpg_data_release_and_set_file (dat, outfile); |
185 |
|
|
if (err) |
186 |
|
|
gpgme_data_release (dat); |
187 |
|
|
} |
188 |
|
|
|
189 |
twoaday |
133 |
return err; |
190 |
|
|
} |
191 |
|
|
/* Export key pattern @patt to the clipboard. |
192 |
|
|
Return value: 0 on success. */ |
193 |
|
|
gpgme_error_t |
194 |
|
|
GPGME::exportToClipboard (const char *patt) |
195 |
|
|
{ |
196 |
|
|
gpgme_data_t dat; |
197 |
|
|
gpgme_error_t err; |
198 |
|
|
|
199 |
|
|
err = gpgme_data_new (&dat); |
200 |
|
|
if (err) |
201 |
|
|
return err; |
202 |
twoaday |
167 |
op_begin (); |
203 |
twoaday |
133 |
err = gpgme_op_export (ctx, patt, 0, dat); |
204 |
twoaday |
167 |
op_end (); |
205 |
twoaday |
303 |
if (err) |
206 |
twoaday |
133 |
gpgme_data_release (dat); |
207 |
twoaday |
303 |
else |
208 |
twoaday |
328 |
gpg_data_release_to_clipboard (dat, chg_ver? 1 : 0); |
209 |
twoaday |
303 |
return err; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
gpgme_error_t |
213 |
|
|
GPGME::exportToClipboard (const char *pattern[]) |
214 |
|
|
{ |
215 |
|
|
gpgme_data_t dat; |
216 |
|
|
gpgme_error_t err; |
217 |
|
|
|
218 |
|
|
err = gpgme_data_new (&dat); |
219 |
|
|
if (err) |
220 |
twoaday |
133 |
return err; |
221 |
twoaday |
303 |
op_begin (); |
222 |
|
|
err = gpgme_op_export_ext (ctx, pattern, 0, dat); |
223 |
|
|
op_end (); |
224 |
|
|
if (err) |
225 |
|
|
gpgme_data_release (dat); |
226 |
|
|
else |
227 |
twoaday |
328 |
gpg_data_release_to_clipboard (dat, chg_ver? 1: 0); |
228 |
twoaday |
133 |
return err; |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
/* Export key pattern @patt to the buffer @outdata. |
232 |
|
|
Return value: 0 on success. */ |
233 |
|
|
gpgme_error_t |
234 |
|
|
GPGME::exportToBuffer (const char *patt, char **outdata) |
235 |
|
|
{ |
236 |
|
|
gpgme_error_t err; |
237 |
|
|
gpgme_data_t dat; |
238 |
|
|
size_t n; |
239 |
|
|
char *p; |
240 |
|
|
|
241 |
|
|
err = gpgme_data_new (&dat); |
242 |
|
|
if (err) |
243 |
|
|
return err; |
244 |
twoaday |
167 |
op_begin (); |
245 |
twoaday |
133 |
err = gpgme_op_export (ctx, patt, 0, dat); |
246 |
twoaday |
167 |
op_end (); |
247 |
twoaday |
133 |
if (err) { |
248 |
|
|
gpgme_data_release (dat); |
249 |
|
|
return err; |
250 |
|
|
} |
251 |
|
|
p = gpgme_data_release_and_get_mem (dat, &n); |
252 |
|
|
|
253 |
twoaday |
270 |
/* we use this detour because p were allocated inside |
254 |
|
|
the gpgme DLL and cannot be freed with 'free' because |
255 |
|
|
the DLL might be linked against debug libs but the application |
256 |
|
|
run ins release mode. */ |
257 |
twoaday |
133 |
*outdata = new char[n+1]; |
258 |
|
|
memcpy (*outdata, p, n); |
259 |
|
|
(*outdata)[n] = 0; |
260 |
|
|
gpgme_free (p); |
261 |
|
|
|
262 |
|
|
return err; |
263 |
|
|
} |