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