1 |
werner |
36 |
/* wptKeyEdit.cpp - GPG key edit abstraction |
2 |
twoaday |
170 |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
werner |
36 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
twoaday |
225 |
|
26 |
|
|
#include <windows.h> |
27 |
twoaday |
205 |
#include <time.h> |
28 |
werner |
36 |
|
29 |
|
|
#include "gpgme.h" |
30 |
|
|
#include "wptCommonCtl.h" |
31 |
|
|
#include "wptContext.h" |
32 |
|
|
#include "wptKeyEdit.h" |
33 |
|
|
#include "wptTypes.h" |
34 |
|
|
#include "wptW32API.h" |
35 |
|
|
#include "wptGPG.h" |
36 |
|
|
#include "wptErrors.h" |
37 |
twoaday |
205 |
#include "wptUTF8.h" |
38 |
werner |
36 |
|
39 |
|
|
|
40 |
|
|
/* Parse the colon status information of @line and store |
41 |
|
|
the information in @rev. |
42 |
|
|
Return value: 0 on success. */ |
43 |
|
|
static gpgme_error_t |
44 |
|
|
rev_key_colon_handler (gpg_desig_rev_t *rev, char *line) |
45 |
|
|
{ |
46 |
|
|
char *p, *pend; |
47 |
|
|
gpg_desig_rev_t r, t; |
48 |
|
|
int field = 0; |
49 |
|
|
|
50 |
|
|
if (!line || strlen (line) < 3) |
51 |
|
|
return gpg_error (GPG_ERR_EOF); |
52 |
|
|
if (strncmp (line, "rvk", 3)) |
53 |
|
|
return 0; /* skip this line. */ |
54 |
|
|
|
55 |
|
|
log_debug ("rev_key: line=%s\r\n", line); |
56 |
|
|
|
57 |
|
|
r = (gpg_desig_rev_t)calloc (1, sizeof *r); |
58 |
|
|
if (!r) |
59 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
60 |
|
|
if (!*rev) |
61 |
|
|
*rev = r; |
62 |
|
|
else { |
63 |
|
|
for (t=*rev; t->next; t=t->next) |
64 |
|
|
; |
65 |
|
|
t->next = r; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
p = strdup (line); |
69 |
|
|
if (!p) |
70 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
71 |
|
|
|
72 |
|
|
for (;;) { |
73 |
|
|
field++; |
74 |
|
|
pend = strsep (&p, ":"); |
75 |
|
|
if (pend == NULL) |
76 |
|
|
break; |
77 |
|
|
switch (field) { |
78 |
twoaday |
234 |
case 4: |
79 |
|
|
r->pubkey_algo = (gpgme_pubkey_algo_t)atol (pend); |
80 |
|
|
break; |
81 |
|
|
|
82 |
|
|
case 10: |
83 |
|
|
strncpy (r->fpr, pend, 40); |
84 |
|
|
r->fpr[40] = 0; |
85 |
|
|
break; |
86 |
werner |
36 |
} |
87 |
|
|
} |
88 |
twoaday |
320 |
free (p); |
89 |
werner |
36 |
return 0; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* Parse the colon data output of edit key from @line and |
94 |
|
|
store the information in the @inf context. |
95 |
|
|
Return value: 0 on success. */ |
96 |
|
|
static gpgme_error_t |
97 |
|
|
uid_inf_colon_handler (gpg_uid_info_t *inf, char *line) |
98 |
|
|
{ |
99 |
|
|
gpg_uid_info_t i, t; |
100 |
|
|
char *p, *pend; |
101 |
twoaday |
205 |
char *name; |
102 |
werner |
36 |
int field = 0, len = 0; |
103 |
|
|
|
104 |
|
|
if (!line || strlen (line) < 3 || strncmp (line, "uid", 3)) |
105 |
|
|
return gpg_error (GPG_ERR_EOF); |
106 |
|
|
|
107 |
|
|
i = (gpg_uid_info_t)calloc (1, sizeof *i); |
108 |
|
|
if (!i) |
109 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
110 |
|
|
if (!*inf) |
111 |
|
|
*inf = i; |
112 |
|
|
else { |
113 |
|
|
for (t=*inf; t->next; t=t->next) |
114 |
|
|
; |
115 |
|
|
t->next = i; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
p = strdup (line); |
119 |
|
|
if (!p) |
120 |
|
|
return gpg_error (GPG_ERR_ENOMEM);; |
121 |
|
|
for (;;) { |
122 |
|
|
field++; |
123 |
|
|
pend = strsep (&p, ":"); |
124 |
|
|
if (pend == NULL) |
125 |
|
|
break; |
126 |
|
|
|
127 |
|
|
switch (field) { |
128 |
|
|
case 2: /* trust info */ |
129 |
|
|
break; |
130 |
|
|
|
131 |
|
|
case 10: /* user ID */ |
132 |
twoaday |
205 |
name = (char *)calloc (1, strlen (pend)+1); |
133 |
|
|
if (!name) |
134 |
werner |
36 |
return gpg_error (GPG_ERR_ENOMEM);; |
135 |
twoaday |
205 |
gpg_decode_c_string (pend, &name, strlen (pend)+ 1); |
136 |
|
|
i->name = utf8_to_native (name); |
137 |
|
|
safe_free (name); |
138 |
werner |
36 |
if (strchr (pend, '<') != NULL && strchr (pend, '>') != NULL) { |
139 |
|
|
int pos = strchr (i->name, '<')- i->name + 1; |
140 |
|
|
int end = strchr (i->name, '>') - i->name; |
141 |
|
|
i->email = (char*) calloc (1, end-pos+2); |
142 |
|
|
if (!i->email) |
143 |
|
|
return gpg_error (GPG_ERR_ENOMEM);; |
144 |
|
|
memcpy (i->email, i->name+pos, (end-pos)); |
145 |
|
|
} |
146 |
|
|
break; |
147 |
|
|
|
148 |
|
|
case 13: /* preferences */ |
149 |
|
|
if (strstr (pend, "mdc")) { |
150 |
twoaday |
234 |
const char *s = "no-ks-modify"; |
151 |
werner |
36 |
len = strlen (pend) - 4; /* ,mdc */ |
152 |
twoaday |
234 |
if (strstr (pend, s)) { |
153 |
werner |
36 |
i->flags.no_ks_modify = 1; |
154 |
twoaday |
234 |
len -= strlen (s)+1; /* ',' + s */ |
155 |
werner |
36 |
} |
156 |
|
|
i->prefs = (char*)calloc (1, len+1); |
157 |
|
|
if (!i->prefs) |
158 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
159 |
|
|
memcpy (i->prefs, pend, len); |
160 |
|
|
i->prefs[len] = '\0'; |
161 |
|
|
i->flags.mdc = 1; |
162 |
|
|
} |
163 |
|
|
else { |
164 |
|
|
i->prefs = strdup (pend); |
165 |
|
|
if (!i->prefs) |
166 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
167 |
|
|
i->flags.mdc = 0; |
168 |
|
|
} |
169 |
|
|
break; |
170 |
|
|
|
171 |
|
|
case 14: /* index/flags */ |
172 |
twoaday |
234 |
i->index = atoi (pend); |
173 |
werner |
36 |
if (strchr (pend, 'r')) |
174 |
|
|
i->flags.revoked = 1; |
175 |
|
|
if (strchr (pend, 'p')) |
176 |
|
|
i->flags.primary = 1; |
177 |
|
|
break; |
178 |
|
|
} |
179 |
|
|
} |
180 |
twoaday |
320 |
free (p); |
181 |
werner |
36 |
return 0; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
|
185 |
|
|
/* Release the context in @inf. */ |
186 |
|
|
void |
187 |
|
|
gpg_uid_info_release (gpg_uid_info_t list) |
188 |
|
|
{ |
189 |
|
|
gpg_uid_info_t i; |
190 |
|
|
|
191 |
|
|
while (list) { |
192 |
|
|
i = list->next; |
193 |
|
|
if (list->name) { |
194 |
|
|
if (list->name) |
195 |
|
|
free (list->name); |
196 |
|
|
list->name = NULL; |
197 |
|
|
} |
198 |
|
|
if (list->prefs) { |
199 |
|
|
if (list->prefs) |
200 |
|
|
free (list->prefs); |
201 |
|
|
list->prefs = NULL; |
202 |
|
|
} |
203 |
|
|
free (list); |
204 |
|
|
list = i; |
205 |
|
|
} |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/* Release the context in @rev. */ |
210 |
|
|
void |
211 |
|
|
gpg_desig_rev_release (gpg_desig_rev_t rev) |
212 |
|
|
{ |
213 |
|
|
gpg_desig_rev_t r; |
214 |
|
|
|
215 |
|
|
while (rev) { |
216 |
|
|
r = rev->next; |
217 |
|
|
free (rev); |
218 |
|
|
rev = r; |
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
static gpgme_error_t |
224 |
|
|
list2_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
225 |
|
|
{ |
226 |
|
|
DWORD n; |
227 |
|
|
const char *s; |
228 |
|
|
|
229 |
|
|
if (!strcmp (key, "keyedit.prompt")) { |
230 |
|
|
s = "quit\n"; |
231 |
|
|
WriteFile ((HANDLE)fd, s, strlen (s), &n, NULL); |
232 |
|
|
} |
233 |
|
|
return 0; |
234 |
|
|
} |
235 |
|
|
|
236 |
twoaday |
170 |
|
237 |
werner |
36 |
/* Dummy handler to get the colon data and then quit. */ |
238 |
|
|
static gpgme_error_t |
239 |
|
|
list_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
240 |
|
|
{ |
241 |
twoaday |
320 |
static int step = 0; |
242 |
|
|
const char *s = ""; |
243 |
werner |
36 |
DWORD n; |
244 |
|
|
|
245 |
|
|
if (!strcmp (key, "keyedit.prompt") && step == 0) { |
246 |
|
|
step = 1; |
247 |
|
|
s = "list\n"; |
248 |
|
|
WriteFile ((HANDLE)fd, s, strlen (s), &n, NULL); |
249 |
|
|
} |
250 |
|
|
else if (!strcmp (key, "keyedit.prompt") && step == 1) { |
251 |
|
|
step = 0; |
252 |
|
|
s = "quit\n"; |
253 |
|
|
WriteFile ((HANDLE)fd, s, strlen (s), &n, NULL); |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
return 0; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
/* Return all designated revokers for this key. If no revoker |
261 |
|
|
was set, @r_rev is NULL. |
262 |
|
|
Return value: 0 on success. */ |
263 |
|
|
gpgme_error_t |
264 |
|
|
GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev) |
265 |
|
|
{ |
266 |
twoaday |
129 |
gpgme_data_t out = NULL; |
267 |
werner |
36 |
gpg_desig_rev_t rev = NULL; |
268 |
|
|
gpgme_error_t err; |
269 |
|
|
char buf[256]; |
270 |
|
|
|
271 |
|
|
if (!this->key) |
272 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
273 |
|
|
|
274 |
|
|
err = gpgme_data_new (&out); |
275 |
|
|
if (err) |
276 |
|
|
goto leave; |
277 |
|
|
err = gpgme_op_edit (ctx, key, list2_handler, NULL, out); |
278 |
|
|
if (err) |
279 |
|
|
goto leave; |
280 |
|
|
|
281 |
|
|
gpgme_data_rewind (out); |
282 |
twoaday |
304 |
while (gpg_data_readline (out, buf, DIM (buf)-1) > 0) |
283 |
werner |
36 |
rev_key_colon_handler (&rev, buf); |
284 |
|
|
*r_rev = rev; |
285 |
|
|
|
286 |
|
|
leave: |
287 |
|
|
if (out) |
288 |
|
|
gpgme_data_release (out); |
289 |
|
|
if (err) { |
290 |
|
|
gpg_desig_rev_release (rev); |
291 |
|
|
*r_rev = NULL; |
292 |
|
|
} |
293 |
|
|
return err; |
294 |
|
|
} |
295 |
|
|
|
296 |
twoaday |
170 |
|
297 |
werner |
36 |
/* Retrieve all user ID information of the key set via setKey |
298 |
|
|
in @r_inf. The result also contains the user ID number which |
299 |
|
|
is needed to securely delete the user-ID. */ |
300 |
|
|
gpgme_error_t |
301 |
|
|
GpgKeyEdit::getUseridInfo (gpg_uid_info_t *r_uinf) |
302 |
|
|
{ |
303 |
|
|
gpgme_data_t out = NULL; |
304 |
|
|
gpgme_error_t err; |
305 |
|
|
gpg_uid_info_t inf = NULL; |
306 |
|
|
char buf[256]; |
307 |
|
|
|
308 |
|
|
if (!this->key) |
309 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
310 |
|
|
|
311 |
|
|
err = gpgme_data_new (&out); |
312 |
|
|
if (err) |
313 |
|
|
goto leave; |
314 |
|
|
err = gpgme_op_edit (ctx, key, list_handler, NULL, out); |
315 |
|
|
if (err) |
316 |
|
|
goto leave; |
317 |
|
|
|
318 |
|
|
gpgme_data_rewind (out); |
319 |
twoaday |
304 |
while (gpg_data_readline (out, buf, DIM (buf) -1) > 0) |
320 |
werner |
36 |
uid_inf_colon_handler (&inf, buf); |
321 |
|
|
|
322 |
|
|
*r_uinf = inf; |
323 |
|
|
|
324 |
|
|
leave: |
325 |
|
|
if (out) |
326 |
|
|
gpgme_data_release (out); |
327 |
|
|
if (err) { |
328 |
|
|
gpg_uid_info_release (inf); |
329 |
|
|
*r_uinf = NULL; |
330 |
|
|
} |
331 |
|
|
return err; |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
|
335 |
twoaday |
170 |
/* Clear object. */ |
336 |
|
|
void |
337 |
|
|
GpgKeyEdit::clear (void) |
338 |
werner |
36 |
{ |
339 |
twoaday |
320 |
pass = NULL; |
340 |
werner |
36 |
name = NULL; |
341 |
|
|
cmt = NULL; |
342 |
|
|
email = NULL; |
343 |
twoaday |
170 |
type = 0; |
344 |
werner |
36 |
cnt = 0; |
345 |
|
|
cmd_sent = 0; |
346 |
|
|
resval = 0; |
347 |
twoaday |
170 |
uid_index = sig_index = key_index = -1; |
348 |
twoaday |
176 |
key_has_passwd = true; |
349 |
twoaday |
170 |
} |
350 |
|
|
|
351 |
|
|
|
352 |
twoaday |
278 |
GpgKeyEdit::GpgKeyEdit (void) |
353 |
|
|
{ |
354 |
|
|
clear (); |
355 |
|
|
key = NULL; |
356 |
|
|
gpgme_new (&ctx); /*FIXME */ |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
|
360 |
twoaday |
170 |
/* Construct an object with the given key in @key. */ |
361 |
|
|
GpgKeyEdit::GpgKeyEdit (gpgme_key_t _key) |
362 |
|
|
{ |
363 |
|
|
clear (); |
364 |
|
|
this->key = _key; |
365 |
werner |
36 |
gpgme_new (&ctx); /* FIXME */ |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
/* Construct an object and fetch the key with the keyid @keyid. */ |
369 |
twoaday |
69 |
GpgKeyEdit::GpgKeyEdit (const char *_keyid) |
370 |
werner |
36 |
{ |
371 |
twoaday |
170 |
clear (); |
372 |
twoaday |
320 |
get_pubkey (_keyid, &this->key); /*FIXME: check return code*/ |
373 |
|
|
gpgme_new (&ctx); /* FIXME */ |
374 |
werner |
36 |
} |
375 |
|
|
|
376 |
|
|
/* Delete the given object. */ |
377 |
|
|
GpgKeyEdit::~GpgKeyEdit (void) |
378 |
|
|
{ |
379 |
|
|
free_if_alloc (name); |
380 |
|
|
free_if_alloc (cmt); |
381 |
|
|
free_if_alloc (email); |
382 |
|
|
gpgme_release (ctx); |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
|
386 |
|
|
/* Reset the state of the object. */ |
387 |
|
|
void |
388 |
|
|
GpgKeyEdit::reset (void) |
389 |
|
|
{ |
390 |
|
|
cmd_sent = 0; |
391 |
twoaday |
129 |
cnt = 0; |
392 |
werner |
36 |
} |
393 |
|
|
|
394 |
|
|
|
395 |
|
|
/* Return true if type has a non-zero value. */ |
396 |
|
|
bool |
397 |
|
|
GpgKeyEdit::isValid (void) |
398 |
|
|
{ |
399 |
|
|
return type != 0; |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
|
403 |
|
|
/* Return the GPGME key. */ |
404 |
|
|
gpgme_key_t |
405 |
|
|
GpgKeyEdit::getKey (void) |
406 |
|
|
{ |
407 |
|
|
return key; |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
|
411 |
|
|
/* Set the GPGME callback to @cb. The hook value can be |
412 |
|
|
given in @cb_value. */ |
413 |
|
|
void |
414 |
|
|
GpgKeyEdit::setCallback (gpgme_progress_cb_t cb, void *cb_value) |
415 |
|
|
{ |
416 |
|
|
gpgme_set_progress_cb (ctx, cb, cb_value); |
417 |
|
|
} |
418 |
|
|
|
419 |
twoaday |
175 |
|
420 |
|
|
/* Clear the stored passphrase. */ |
421 |
|
|
void |
422 |
|
|
GpgKeyEdit::clearPassphrase (void) |
423 |
|
|
{ |
424 |
|
|
if (pass) |
425 |
|
|
pass = NULL; |
426 |
|
|
} |
427 |
|
|
|
428 |
|
|
|
429 |
twoaday |
176 |
|
430 |
|
|
/* Inidicate that a key is protected by a passphrase or not. */ |
431 |
|
|
void |
432 |
|
|
GpgKeyEdit::setNoPassphrase (bool val) |
433 |
|
|
{ |
434 |
|
|
key_has_passwd = !val; |
435 |
|
|
} |
436 |
|
|
|
437 |
werner |
36 |
/* Set the passphrase to @pass. */ |
438 |
|
|
void |
439 |
twoaday |
69 |
GpgKeyEdit::setPassphrase (const char *_pass) |
440 |
werner |
36 |
{ |
441 |
twoaday |
69 |
this->pass = _pass; |
442 |
werner |
36 |
} |
443 |
|
|
|
444 |
twoaday |
260 |
|
445 |
|
|
/* Kludge to allow to handle admin and user PINs. */ |
446 |
|
|
void |
447 |
|
|
GpgKeyEdit::setPassphrase2 (const char *_pass2) |
448 |
|
|
{ |
449 |
|
|
this->new_pass = _pass2; |
450 |
|
|
} |
451 |
|
|
|
452 |
werner |
36 |
/* Set the current key to @key. */ |
453 |
|
|
void |
454 |
twoaday |
69 |
GpgKeyEdit::setKey (gpgme_key_t _key) |
455 |
werner |
36 |
{ |
456 |
twoaday |
69 |
this->key = _key; |
457 |
werner |
36 |
} |
458 |
|
|
|
459 |
|
|
/* Set the keyid of the destination key to @keyid. */ |
460 |
|
|
void |
461 |
twoaday |
69 |
GpgKeyEdit::setKeyID (const char *_keyid) |
462 |
werner |
36 |
{ |
463 |
twoaday |
69 |
if (!_keyid) |
464 |
werner |
36 |
return; |
465 |
twoaday |
320 |
get_pubkey (_keyid, &this->key); /* FIXME: check return code */ |
466 |
werner |
36 |
} |
467 |
|
|
|
468 |
|
|
|
469 |
|
|
/* Set the local user for the operation to @locusr. */ |
470 |
|
|
void |
471 |
|
|
GpgKeyEdit::setLocalUser (gpgme_key_t locusr) |
472 |
|
|
{ |
473 |
|
|
gpgme_signers_add (ctx, locusr); |
474 |
|
|
} |
475 |
|
|
|
476 |
|
|
/* Set the result of the operation to @val. */ |
477 |
|
|
void |
478 |
|
|
GpgKeyEdit::setResult (int val) |
479 |
|
|
{ |
480 |
|
|
resval |= val; |
481 |
|
|
} |
482 |
|
|
|
483 |
|
|
|
484 |
|
|
/* Return the result of the operation. */ |
485 |
|
|
int |
486 |
|
|
GpgKeyEdit::getResult(void) |
487 |
|
|
{ |
488 |
|
|
return resval; |
489 |
|
|
} |
490 |
|
|
|
491 |
|
|
|
492 |
twoaday |
176 |
/* Return the amount of days the key is valid. */ |
493 |
|
|
int |
494 |
|
|
GpgKeyEdit::getValidDays (void) |
495 |
|
|
{ |
496 |
|
|
return valid; |
497 |
|
|
} |
498 |
werner |
36 |
|
499 |
twoaday |
176 |
|
500 |
werner |
36 |
int |
501 |
|
|
GpgKeyEdit::getType (void) |
502 |
|
|
{ |
503 |
|
|
return type; |
504 |
|
|
} |
505 |
|
|
|
506 |
twoaday |
200 |
|
507 |
|
|
/* Add the notation data from @notation to the user ID |
508 |
|
|
with the index @_uid_idx. |
509 |
|
|
Return value: 0 on success. */ |
510 |
|
|
gpgme_error_t |
511 |
|
|
GpgKeyEdit::addNotation (int _uid_idx, const char *_notation) |
512 |
|
|
{ |
513 |
|
|
if (!key) |
514 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
515 |
|
|
if (key_has_passwd && !this->pass) |
516 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
517 |
|
|
|
518 |
|
|
type = GPG_EDITKEY_NOTATION; |
519 |
|
|
this->uid_index = _uid_idx; |
520 |
|
|
this->notation = (char*)_notation; |
521 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
522 |
|
|
} |
523 |
|
|
|
524 |
|
|
|
525 |
werner |
36 |
/* Sign the key stored in the object with the |
526 |
|
|
signing mode @mode and the signature class @sig_class. |
527 |
|
|
Return value: 0 on success. */ |
528 |
|
|
gpgme_error_t |
529 |
twoaday |
69 |
GpgKeyEdit::signKey (int mode, int _sig_class, const char *_exp_date) |
530 |
werner |
36 |
{ |
531 |
twoaday |
176 |
if (!this->key) |
532 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
533 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
534 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
535 |
werner |
36 |
|
536 |
|
|
type = mode; |
537 |
twoaday |
69 |
this->exp_date = _exp_date; |
538 |
|
|
this->sig_class = _sig_class; |
539 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
540 |
|
|
} |
541 |
|
|
|
542 |
twoaday |
170 |
|
543 |
|
|
/* Sign a single user-id with the index @_uid_index. |
544 |
|
|
All other parameters are equal to signKey(). |
545 |
|
|
Return value: 0 on success. */ |
546 |
|
|
gpgme_error_t |
547 |
|
|
GpgKeyEdit::signUserid (int _uid_idx, int mode, int _sig_class, |
548 |
|
|
const char *_exp_date) |
549 |
|
|
{ |
550 |
twoaday |
176 |
if (!this->key) |
551 |
twoaday |
170 |
return gpg_error (GPG_ERR_INV_OBJ); |
552 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
553 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
554 |
|
|
|
555 |
twoaday |
170 |
this->uid_index = _uid_idx; |
556 |
|
|
type = mode; |
557 |
|
|
this->exp_date = _exp_date; |
558 |
|
|
this->sig_class = _sig_class; |
559 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
|
563 |
werner |
36 |
/* Set the ownertrust of the key stored in the object |
564 |
|
|
to the trust value @trust. |
565 |
|
|
Return value: 0 on success. */ |
566 |
|
|
gpgme_error_t |
567 |
|
|
GpgKeyEdit::setTrust (gpgme_validity_t trust) |
568 |
|
|
{ |
569 |
|
|
if (!this->key) |
570 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
571 |
|
|
|
572 |
|
|
type = GPG_EDITKEY_TRUST; |
573 |
|
|
this->trust_id = (int)trust; |
574 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
575 |
|
|
} |
576 |
|
|
|
577 |
|
|
/* Add a user ID to the given key with the @name as the |
578 |
|
|
name, @cmt as the comment (or NULL) and @email as the email. |
579 |
|
|
Return value: 0 on success. */ |
580 |
|
|
gpgme_error_t |
581 |
twoaday |
77 |
GpgKeyEdit::addUserid (const char *_name, const char *_cmt, const char *_email) |
582 |
werner |
36 |
{ |
583 |
twoaday |
176 |
if (!this->key) |
584 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
585 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
586 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
587 |
werner |
36 |
|
588 |
|
|
type = GPG_EDITKEY_ADDUID; |
589 |
|
|
free_if_alloc (this->name); |
590 |
twoaday |
77 |
this->name = m_strdup (_name); |
591 |
werner |
36 |
free_if_alloc (this->cmt); |
592 |
|
|
this->cmt = NULL; |
593 |
twoaday |
129 |
if (_cmt != NULL) |
594 |
twoaday |
77 |
this->cmt = m_strdup (_cmt); |
595 |
werner |
36 |
free_if_alloc (this->email); |
596 |
twoaday |
77 |
this->email = m_strdup (_email); |
597 |
werner |
36 |
if (!this->email || !this->name) |
598 |
|
|
BUG (NULL); |
599 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
/* Delete the user-ID with the index @uid_index of the given key. |
603 |
|
|
Return value: 0 on success. */ |
604 |
|
|
gpgme_error_t |
605 |
twoaday |
69 |
GpgKeyEdit::delUserid (int _uid_index) |
606 |
werner |
36 |
{ |
607 |
|
|
if (!this->key) |
608 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
609 |
|
|
|
610 |
|
|
type = GPG_EDITKEY_DELUID; |
611 |
twoaday |
69 |
this->uid_index = _uid_index; |
612 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
613 |
|
|
} |
614 |
|
|
|
615 |
|
|
/* Delete the subkey with the index @key_index. |
616 |
|
|
Return value: 0 on success. */ |
617 |
|
|
gpgme_error_t |
618 |
twoaday |
69 |
GpgKeyEdit::delKey (int _key_index) |
619 |
werner |
36 |
{ |
620 |
|
|
if (!this->key) |
621 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
622 |
|
|
|
623 |
|
|
type = GPG_EDITKEY_DELKEY; |
624 |
twoaday |
69 |
this->key_index = _key_index; |
625 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
626 |
|
|
} |
627 |
|
|
|
628 |
twoaday |
260 |
|
629 |
|
|
/* Add a new key on a smart card. |
630 |
|
|
The type is given in @type. */ |
631 |
|
|
gpgme_error_t |
632 |
|
|
GpgKeyEdit::addCardKey (int _type) |
633 |
|
|
{ |
634 |
|
|
if (!this->key) |
635 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
636 |
|
|
if (!this->pass) |
637 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
638 |
|
|
if (_type < 1 || _type > 3) |
639 |
|
|
return gpg_error (GPG_ERR_INV_VALUE); |
640 |
|
|
|
641 |
|
|
type = GPG_EDITKEY_ADDCARDKEY; |
642 |
|
|
this->key_index = _type; |
643 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
644 |
|
|
} |
645 |
|
|
|
646 |
|
|
|
647 |
werner |
36 |
/* Add a new subkey to the given key. |
648 |
|
|
The new key will have @pubkey_algo as the algorithm |
649 |
|
|
and a size of @pubkey_size bits. If valid > 0, the |
650 |
|
|
key expires in @valid days. |
651 |
|
|
Return value: 0 on success. */ |
652 |
|
|
gpgme_error_t |
653 |
twoaday |
77 |
GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo, |
654 |
|
|
unsigned int _pubkey_size, long _valid) |
655 |
werner |
36 |
{ |
656 |
twoaday |
176 |
if (!this->key) |
657 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
658 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
659 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
660 |
werner |
36 |
|
661 |
|
|
type = GPG_EDITKEY_ADDKEY; |
662 |
twoaday |
77 |
this->pubkey_algo = _pubkey_algo; |
663 |
|
|
this->pubkey_size = _pubkey_size; |
664 |
|
|
this->valid = _valid; |
665 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
666 |
|
|
} |
667 |
|
|
|
668 |
|
|
/* Change the passphrase of the given key to @new_pass. |
669 |
|
|
If allow_empty != 0, it is allowed to provide an empty passphrase. |
670 |
|
|
Return value: 0 on success. */ |
671 |
|
|
gpgme_error_t |
672 |
twoaday |
69 |
GpgKeyEdit::changePassphrase (const char *_new_pass, int allow_empty) |
673 |
werner |
36 |
{ |
674 |
twoaday |
176 |
if (!this->key) |
675 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
676 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
677 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
678 |
werner |
36 |
|
679 |
|
|
type = GPG_EDITKEY_PASSWD; |
680 |
twoaday |
69 |
this->new_pass = _new_pass; |
681 |
werner |
36 |
this->flags = allow_empty? 1 : 0; |
682 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
683 |
|
|
} |
684 |
|
|
|
685 |
|
|
/* Set the primary user-ID of the given key to user-ID with |
686 |
|
|
the index @uid_index. |
687 |
|
|
Return value: 0 on success. */ |
688 |
|
|
gpgme_error_t |
689 |
twoaday |
69 |
GpgKeyEdit::setPrimaryUserid (int _uid_index) |
690 |
werner |
36 |
{ |
691 |
twoaday |
176 |
if (!this->key) |
692 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
693 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
694 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
695 |
werner |
36 |
|
696 |
|
|
type = GPG_EDITKEY_PRIMARY; |
697 |
twoaday |
69 |
this->uid_index = _uid_index; |
698 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
699 |
|
|
} |
700 |
|
|
|
701 |
|
|
/* Set the expire date of the subkey with the index @key_index. |
702 |
|
|
@exp_timestamp is used to calculate the days the key is valid. |
703 |
|
|
if @exp_days is true, exp_timestamp is already converted to days. |
704 |
|
|
Return value: 0 on success. */ |
705 |
|
|
gpgme_error_t |
706 |
twoaday |
129 |
GpgKeyEdit::setKeyExpireDate (int _key_index, long exp_timestamp, |
707 |
|
|
bool exp_days) |
708 |
werner |
36 |
{ |
709 |
twoaday |
176 |
if (!this->key) |
710 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
711 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
712 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
713 |
werner |
36 |
if (!exp_days && exp_timestamp > 0 && exp_timestamp < time (NULL)) |
714 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
715 |
|
|
|
716 |
|
|
type = GPG_EDITKEY_EXPIRE; |
717 |
|
|
if (!exp_days && exp_timestamp > 0) { |
718 |
|
|
valid = exp_timestamp - time (NULL); |
719 |
|
|
valid /= 86400; |
720 |
|
|
} |
721 |
|
|
else |
722 |
|
|
valid = exp_timestamp; |
723 |
twoaday |
69 |
this->key_index = _key_index; |
724 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
725 |
|
|
} |
726 |
|
|
|
727 |
|
|
/* Revoke the userid given by the index @uid_index. |
728 |
|
|
Return value: 0 on success. */ |
729 |
|
|
gpgme_error_t |
730 |
twoaday |
69 |
GpgKeyEdit::revokeUserid (int _uid_index) |
731 |
werner |
36 |
{ |
732 |
twoaday |
176 |
if (!this->key) |
733 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
734 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
735 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
736 |
werner |
36 |
|
737 |
|
|
type = GPG_EDITKEY_REVUID; |
738 |
twoaday |
69 |
this->uid_index = _uid_index; |
739 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
740 |
|
|
} |
741 |
|
|
|
742 |
|
|
|
743 |
|
|
/* Revoke a signature on the user-ID with the index @uid_index |
744 |
|
|
and the signature index @sig_index. |
745 |
|
|
Return value: 0 on success. */ |
746 |
|
|
gpgme_error_t |
747 |
twoaday |
69 |
GpgKeyEdit::revokeSignature (int _uid_index, int _sig_index) |
748 |
werner |
36 |
{ |
749 |
twoaday |
176 |
if (!this->key) |
750 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
751 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
752 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
753 |
werner |
36 |
|
754 |
|
|
type = GPG_EDITKEY_REVSIG; |
755 |
twoaday |
69 |
this->uid_index = _uid_index; |
756 |
|
|
this->sig_index = _sig_index; |
757 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
758 |
|
|
} |
759 |
|
|
|
760 |
|
|
|
761 |
|
|
/* Revoke the subkey with the index @key_index. Optionally |
762 |
|
|
a reason can be provided in @reason with a text to describe |
763 |
|
|
more details in @cmt. |
764 |
|
|
Return value: 0 on success. */ |
765 |
|
|
gpgme_error_t |
766 |
twoaday |
69 |
GpgKeyEdit::revokeSubkey (int _key_index, int _reason, const char *_cmt) |
767 |
werner |
36 |
{ |
768 |
twoaday |
176 |
if (!this->key) |
769 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
770 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
771 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
772 |
werner |
36 |
|
773 |
|
|
type = GPG_EDITKEY_REVKEY; |
774 |
twoaday |
69 |
this->key_index = _key_index; |
775 |
|
|
this->reason = _reason; |
776 |
werner |
36 |
free_if_alloc (this->cmt); |
777 |
|
|
this->cmt = NULL; |
778 |
twoaday |
69 |
if (_cmt) |
779 |
|
|
this->cmt = m_strdup (_cmt); |
780 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
781 |
|
|
} |
782 |
|
|
|
783 |
|
|
|
784 |
|
|
/* Add a designated revoker to the key. @uid stores |
785 |
|
|
the user-ID of the key who is allowed to be a |
786 |
|
|
designated revoker. |
787 |
|
|
Return value: 0 on success. */ |
788 |
|
|
gpgme_error_t |
789 |
|
|
GpgKeyEdit::addDesignatedRevoker (const char *uid) |
790 |
|
|
{ |
791 |
twoaday |
176 |
if (!this->key) |
792 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
793 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
794 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
795 |
werner |
36 |
|
796 |
|
|
type = GPG_EDITKEY_ADDREV; |
797 |
|
|
free_if_alloc (this->name); |
798 |
|
|
this->name = m_strdup (uid); |
799 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
800 |
|
|
} |
801 |
|
|
|
802 |
|
|
/* Add a photo-ID to the key. The JPG image is given |
803 |
|
|
in the file with the name @jpg_file. |
804 |
|
|
Return value: 0 on success. */ |
805 |
|
|
gpgme_error_t |
806 |
|
|
GpgKeyEdit::addPhotoid (const char *jpg_file) |
807 |
|
|
{ |
808 |
twoaday |
176 |
if (!this->key) |
809 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
810 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
811 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
812 |
werner |
36 |
|
813 |
|
|
type = GPG_EDITKEY_ADDPHOTO; |
814 |
|
|
this->url = jpg_file; |
815 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
816 |
|
|
} |
817 |
|
|
|
818 |
|
|
/* Enable the given key. */ |
819 |
|
|
gpgme_error_t |
820 |
|
|
GpgKeyEdit::enable (void) |
821 |
|
|
{ |
822 |
|
|
if (!this->key) |
823 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
824 |
|
|
type = GPG_EDITKEY_ENABLE; |
825 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
826 |
|
|
} |
827 |
|
|
|
828 |
|
|
/* Disable the given key. */ |
829 |
|
|
gpgme_error_t |
830 |
|
|
GpgKeyEdit::disable (void) |
831 |
|
|
{ |
832 |
|
|
if (!this->key) |
833 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
834 |
|
|
type = GPG_EDITKEY_DISABLE; |
835 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
836 |
|
|
} |
837 |
|
|
|
838 |
|
|
|
839 |
twoaday |
207 |
/* Remove unusable parts and all signatures from a key. */ |
840 |
|
|
gpgme_error_t |
841 |
|
|
GpgKeyEdit::minimizeKey (void) |
842 |
|
|
{ |
843 |
|
|
if (!this->key) |
844 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
845 |
|
|
type = GPG_EDITKEY_MINIMIZE; |
846 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
847 |
|
|
} |
848 |
|
|
|
849 |
|
|
|
850 |
twoaday |
119 |
/* Remove unusable parts from a key. */ |
851 |
|
|
gpgme_error_t |
852 |
|
|
GpgKeyEdit::cleanKey (void) |
853 |
|
|
{ |
854 |
|
|
if (!this->key) |
855 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
856 |
|
|
type = GPG_EDITKEY_CLEAN; |
857 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
858 |
|
|
} |
859 |
|
|
|
860 |
|
|
|
861 |
werner |
36 |
/* Update the user-ID preferences of the user-ID with the |
862 |
|
|
index @uid_index to the prefs given in @new_prefs. |
863 |
|
|
Return value: 0 on success. */ |
864 |
|
|
gpgme_error_t |
865 |
twoaday |
234 |
GpgKeyEdit::setUseridPreferences (int _uid_index, const char *_new_prefs) |
866 |
werner |
36 |
{ |
867 |
twoaday |
176 |
if (!this->key) |
868 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
869 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
870 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
871 |
twoaday |
234 |
type = GPG_EDITKEY_SETPREF; |
872 |
|
|
this->uid_index = _uid_index; |
873 |
|
|
this->new_prefs = _new_prefs; |
874 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
875 |
werner |
36 |
} |
876 |
|
|
|
877 |
|
|
|
878 |
|
|
/* Delete a signature from the user-ID with the index @uid_index. |
879 |
|
|
The index of the signature is given in @sig_index. |
880 |
|
|
Return value: 0 on success. */ |
881 |
|
|
gpgme_error_t |
882 |
twoaday |
69 |
GpgKeyEdit::delUseridSignature (int _uid_index, int _sig_index) |
883 |
werner |
36 |
{ |
884 |
|
|
if (!this->key) |
885 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
886 |
|
|
type = GPG_EDITKEY_DELSIG; |
887 |
twoaday |
69 |
this->uid_index = _uid_index; |
888 |
|
|
this->sig_index = _sig_index; |
889 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
890 |
|
|
} |
891 |
|
|
|
892 |
|
|
/* Set the preferred keyserver for the given key to @url. |
893 |
twoaday |
181 |
If @_uid_index is -1, set the keyserver for all user-ids. |
894 |
werner |
36 |
Return value: 0 on success. */ |
895 |
|
|
gpgme_error_t |
896 |
twoaday |
69 |
GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url) |
897 |
werner |
36 |
{ |
898 |
twoaday |
176 |
if (!this->key) |
899 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
900 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
901 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
902 |
|
|
if (!_url) |
903 |
werner |
36 |
return gpg_error (GPG_ERR_INV_ARG); |
904 |
twoaday |
176 |
|
905 |
werner |
36 |
type = GPG_EDITKEY_KEYSERV; |
906 |
twoaday |
69 |
this->url = _url; |
907 |
|
|
this->uid_index = _uid_index; |
908 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
909 |
|
|
} |
910 |
twoaday |
170 |
|
911 |
|
|
|
912 |
|
|
/* Return the saved user-id index. */ |
913 |
|
|
int |
914 |
|
|
GpgKeyEdit::getUseridIndex (void) |
915 |
|
|
{ |
916 |
|
|
return uid_index; |
917 |
|
|
} |
918 |
|
|
|
919 |
|
|
|
920 |
|
|
/* Return the saved key index. */ |
921 |
|
|
int |
922 |
|
|
GpgKeyEdit::getKeyIndex (void) |
923 |
|
|
{ |
924 |
|
|
return key_index; |
925 |
|
|
} |
926 |
|
|
|
927 |
|
|
|
928 |
|
|
/* Return the saved sig index. */ |
929 |
|
|
int |
930 |
|
|
GpgKeyEdit::getSigIndex (void) |
931 |
|
|
{ |
932 |
|
|
return sig_index; |
933 |
|
|
} |