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