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