1 |
/* wptKeyEditCB.cpp - Key edit callback handling |
2 |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
* 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 <stdio.h> |
27 |
#include <string.h> |
28 |
#include <stdlib.h> |
29 |
#include <assert.h> |
30 |
#include <windows.h> |
31 |
|
32 |
#include "gpgme.h" |
33 |
#include "wptCommonCtl.h" |
34 |
#include "wptContext.h" |
35 |
#include "wptKeyEdit.h" |
36 |
#include "wptErrors.h" |
37 |
|
38 |
/* Possible errors for the edit key operation. */ |
39 |
enum editkey_error_t { |
40 |
EDITKEY_ERR_ALREADY_SIGNED = 1, |
41 |
EDITKEY_ERR_BAD_PASSPHRASE = 2 |
42 |
}; |
43 |
|
44 |
typedef gpgme_status_code_t status_code_t; |
45 |
|
46 |
|
47 |
/* 'notation' command handler. */ |
48 |
static const char* |
49 |
cmd_notation_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
50 |
{ |
51 |
static char buf[32]; |
52 |
|
53 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
54 |
int uid = ctx->getUseridIndex (); |
55 |
ctx->cmd_sent = 1; |
56 |
if (uid != -1) { |
57 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
58 |
return buf; |
59 |
} |
60 |
} |
61 |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 0) { |
62 |
ctx->cnt = 1; |
63 |
return "notation"; |
64 |
} |
65 |
if (!strcmp (key, "keyedit.add_notation")) |
66 |
return ctx->notation; |
67 |
if (!strcmp (key, "passphrase.enter")) |
68 |
return ctx->pass; |
69 |
|
70 |
return NULL; |
71 |
} |
72 |
|
73 |
|
74 |
/* 'keyserver' command handler. */ |
75 |
static const char* |
76 |
cmd_keyserv_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
77 |
{ |
78 |
static char buf[32]; |
79 |
|
80 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
81 |
int uid = ctx->getUseridIndex (); |
82 |
ctx->cmd_sent = 1; |
83 |
if (uid != -1) { |
84 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
85 |
return buf; |
86 |
} |
87 |
} |
88 |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 0) { |
89 |
ctx->cnt = 1; |
90 |
return "keyserver"; |
91 |
} |
92 |
if (!strcmp (key, "keyedit.add_keyserver")) |
93 |
return ctx->url; |
94 |
if (!strcmp (key, "keyedit.confirm_keyserver")) |
95 |
return "Y"; |
96 |
if (!strcmp (key, "passphrase.enter")) |
97 |
return ctx->pass; |
98 |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 1) { |
99 |
ctx->reset (); |
100 |
return "save"; |
101 |
} |
102 |
|
103 |
return NULL; |
104 |
} |
105 |
|
106 |
|
107 |
/* 'sign' command handler. */ |
108 |
static const char* |
109 |
cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
110 |
{ |
111 |
static char buf[32]; |
112 |
|
113 |
if (ctx->getUseridIndex () != -1 && |
114 |
ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
115 |
ctx->cnt++; |
116 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
117 |
return buf; |
118 |
} |
119 |
|
120 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
121 |
ctx->cmd_sent = 1; |
122 |
switch (ctx->getType ()) { |
123 |
case GPG_EDITKEY_SIGN: return "sign"; |
124 |
case GPG_EDITKEY_TSIGN: return "tsign"; |
125 |
case GPG_EDITKEY_LSIGN: return "lsign"; |
126 |
case GPG_EDITKEY_NRSIGN: return "nrsign"; |
127 |
case GPG_EDITKEY_NRLSIGN: return "nrlsign"; |
128 |
} |
129 |
} |
130 |
if (!strcmp (key, "sign_uid.class")) { |
131 |
sprintf (buf, "%d", ctx->sig_class); |
132 |
return buf; |
133 |
} |
134 |
if (!strcmp (key, "sign_uid.expire")) |
135 |
return "Y"; /* the sig expires when the key expires */ |
136 |
if (!strcmp (key, "siggen.valid")) |
137 |
return ctx->exp_date? ctx->exp_date : "0"; |
138 |
if (!strcmp (key, "trustsig_prompt.trust_value")) { |
139 |
sprintf (buf, "%d", ctx->trust_id); |
140 |
return buf; |
141 |
} |
142 |
if (!strcmp (key, "trustsig_prompt.trust_depth")) |
143 |
return ""; /* fixme */ |
144 |
if (!strcmp (key, "trustsig_prompt.trust_regexp")) |
145 |
return ""; /* fixme */ |
146 |
if (!strcmp (key, "sign_uid.local_promote_okay" ) ) |
147 |
return "Y"; |
148 |
if (!strcmp (key, "sign_uid.okay" ) ) |
149 |
return "Y"; |
150 |
if (!strcmp (key, "keyedit.sign_all.okay")) |
151 |
return "Y"; |
152 |
if (!strcmp ( key, "passphrase.enter")) |
153 |
return ctx->pass; |
154 |
if (!strcmp (key, "keyedit.prompt")) { |
155 |
ctx->reset (); |
156 |
return "save"; |
157 |
} |
158 |
|
159 |
return NULL; |
160 |
} |
161 |
|
162 |
/* 'trust' command handler. */ |
163 |
static const char* |
164 |
cmd_trust_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
165 |
{ |
166 |
static char buf[4]; |
167 |
|
168 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
169 |
ctx->cmd_sent = 1; |
170 |
return "trust"; |
171 |
} |
172 |
if (!strcmp (key, "edit_ownertrust.set_ultimate.okay")) |
173 |
return "Y"; |
174 |
if (!strcmp (key, "edit_ownertrust.value" )) { |
175 |
sprintf (buf, "%d", ctx->trust_id); |
176 |
return buf; |
177 |
} |
178 |
if (!strcmp (key, "keyedit.prompt")) { |
179 |
ctx->reset (); |
180 |
return "save"; |
181 |
} |
182 |
|
183 |
return NULL; |
184 |
} |
185 |
|
186 |
|
187 |
/* 'adduid' command handler. */ |
188 |
static const char* |
189 |
cmd_adduid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
190 |
{ |
191 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
192 |
ctx->cmd_sent = 1; |
193 |
return "adduid"; |
194 |
} |
195 |
if (!strcmp (key, "keygen.name")) |
196 |
return ctx->name; |
197 |
if (!strcmp (key, "keygen.email")) |
198 |
return ctx->email; |
199 |
if (!strcmp (key, "keygen.comment")) |
200 |
return ctx->cmt? ctx->cmt : ""; |
201 |
if (!strcmp (key, "passphrase.enter")) |
202 |
return ctx->pass; |
203 |
if( !strcmp (key, "keyedit.prompt")) { |
204 |
ctx->reset (); |
205 |
return "save"; |
206 |
} |
207 |
|
208 |
return NULL; |
209 |
} |
210 |
|
211 |
|
212 |
static const char* |
213 |
cmd_deluid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
214 |
const char *key) |
215 |
{ |
216 |
static char buf[64]; |
217 |
|
218 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
219 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
220 |
ctx->cnt = 1; |
221 |
return buf; |
222 |
} |
223 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
224 |
ctx->cnt = 2; |
225 |
return "deluid"; |
226 |
} |
227 |
if (ctx->cnt == 2 && !strcmp (key, "keyedit.remove.uid.okay")) { |
228 |
ctx->cnt = 3; |
229 |
return "Y"; |
230 |
} |
231 |
if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt" )) { |
232 |
ctx->reset (); |
233 |
return "save"; |
234 |
} |
235 |
|
236 |
return NULL; |
237 |
} |
238 |
|
239 |
|
240 |
static const char* |
241 |
cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
242 |
{ |
243 |
static char buf[64]; |
244 |
static int sig_cnt = 0; |
245 |
|
246 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
247 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
248 |
ctx->cnt = 1; |
249 |
return buf; |
250 |
} |
251 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
252 |
ctx->cnt = 2; |
253 |
return "delsig"; |
254 |
} |
255 |
if (!strcmp (key, "keyedit.delsig.unknown") || |
256 |
!strcmp (key, "keyedit.delsig.valid")) { |
257 |
if (++sig_cnt == ctx->getSigIndex ()) |
258 |
return "Y"; |
259 |
else |
260 |
return "N"; |
261 |
} |
262 |
if (!strcmp (key, "keyedit.delsig.selfsig")) |
263 |
return "Y"; |
264 |
if (ctx->cnt == 2 && !strcmp (key, "keyedit.prompt")) { |
265 |
sig_cnt = 0; |
266 |
ctx->reset (); |
267 |
return "save"; |
268 |
} |
269 |
return NULL; |
270 |
} |
271 |
|
272 |
|
273 |
/* 'delkey' command handler. */ |
274 |
static const char* |
275 |
cmd_delkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
276 |
const char *key) |
277 |
{ |
278 |
static char buf[64]; |
279 |
|
280 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
281 |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
282 |
ctx->cnt = 1; |
283 |
return buf; |
284 |
} |
285 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
286 |
ctx->cnt = 2; |
287 |
return "delkey"; |
288 |
} |
289 |
if (ctx->cnt == 2 && !strcmp (key, "keyedit.remove.subkey.okay")) { |
290 |
ctx->cnt = 3; |
291 |
return "Y"; |
292 |
} |
293 |
if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt")) { |
294 |
ctx->reset (); |
295 |
return "save"; |
296 |
} |
297 |
|
298 |
return NULL; |
299 |
} |
300 |
|
301 |
|
302 |
/* 'addcardkey' command handler. */ |
303 |
static const char* |
304 |
cmd_addcardkey_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
305 |
{ |
306 |
static char buf[32]; |
307 |
|
308 |
/* XXX: actually we mix up things here, it would be better to |
309 |
implement this in CardEditCB but because the edit-key |
310 |
interface is used, we have to implement it here. */ |
311 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
312 |
ctx->cmd_sent = 1; |
313 |
return "addcardkey"; |
314 |
} |
315 |
if (!strcmp (key, "cardedit.genkeys.subkeytype")) { |
316 |
sprintf (buf, "%d", ctx->getKeyIndex ()); |
317 |
return buf; |
318 |
} |
319 |
if (!strcmp (key, "cardedit.genkeys.replace_key")) |
320 |
return "Y"; /* better issue an extra warning? */ |
321 |
if (!strcmp (key, "passphrase.adminpin.ask")) |
322 |
return ctx->pass; |
323 |
if (!strcmp (key, "passphrase.pin.ask")) |
324 |
return ctx->new_pass; |
325 |
if (!strcmp (key, "keygen.valid")) { |
326 |
sprintf (buf, "%d", ctx->getValidDays ()); |
327 |
return buf; |
328 |
} |
329 |
if (!strcmp (key, "keyedit.prompt")) { |
330 |
ctx->reset (); |
331 |
return "save"; |
332 |
} |
333 |
return NULL; |
334 |
} |
335 |
|
336 |
|
337 |
/* 'addkey' command handler. */ |
338 |
static const char* |
339 |
cmd_addkey_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
340 |
{ |
341 |
static char buf[64]; |
342 |
|
343 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
344 |
ctx->cmd_sent = 1; |
345 |
return "addkey"; |
346 |
} |
347 |
|
348 |
if (!strcmp (key, "passphrase.enter")) |
349 |
return ctx->pass; |
350 |
if (!strcmp (key, "keygen.algo")) { |
351 |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_algo); |
352 |
return buf; |
353 |
} |
354 |
if (!strcmp (key, "keygen.size")) { |
355 |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_size); |
356 |
return buf; |
357 |
} |
358 |
if (!strcmp (key, "keygen.valid")) { |
359 |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->getValidDays ()); |
360 |
return buf; |
361 |
} |
362 |
if (!strcmp (key, "keyedit.prompt")) { |
363 |
ctx->reset (); |
364 |
return "save"; |
365 |
} |
366 |
return NULL; |
367 |
} |
368 |
|
369 |
|
370 |
/* 'passwd' command handler. */ |
371 |
static const char* |
372 |
cmd_passwd_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
373 |
{ |
374 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
375 |
ctx->cmd_sent = 1; |
376 |
return "passwd"; |
377 |
} |
378 |
if (ctx->cnt == 0 && !strcmp (key, "passphrase.enter")) { |
379 |
ctx->cnt = 1; |
380 |
/* it is possible that there is no old passphrase. */ |
381 |
if (!ctx->pass && ctx->flags) |
382 |
return ""; |
383 |
return ctx->pass; |
384 |
} |
385 |
if (!strcmp (key, "passphrase.enter" )) |
386 |
return ctx->new_pass; |
387 |
if (!strcmp (key, "change_passwd.empty.okay" )) |
388 |
return ctx->flags? "Y" : "N"; |
389 |
if (!strcmp (key, "keyedit.prompt")) { |
390 |
ctx->reset (); |
391 |
return "save"; |
392 |
} |
393 |
|
394 |
return NULL; |
395 |
} |
396 |
|
397 |
|
398 |
static const char* |
399 |
cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
400 |
{ |
401 |
static char buf[128] = {0}; |
402 |
|
403 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
404 |
_snprintf (buf, sizeof (buf)-1, "uid %d", ctx->getUseridIndex ()); |
405 |
ctx->cmd_sent = 1; |
406 |
ctx->cnt = 1; |
407 |
return buf; |
408 |
} |
409 |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 1) { |
410 |
_snprintf (buf, sizeof buf-1, "setpref %s", ctx->new_prefs); |
411 |
ctx->cnt = 2; |
412 |
return buf; |
413 |
} |
414 |
if (!strcmp (key, "keyedit.setpref.okay")) |
415 |
return "Y"; |
416 |
if (!strcmp (key, "passphrase.enter")) |
417 |
return ctx->pass; |
418 |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 2) { |
419 |
ctx->reset (); |
420 |
return "save"; |
421 |
} |
422 |
return buf; |
423 |
} |
424 |
|
425 |
|
426 |
/* 'primary' command handler. */ |
427 |
static const char* |
428 |
cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
429 |
const char *key) |
430 |
{ |
431 |
static char buf[64]; |
432 |
|
433 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
434 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
435 |
ctx->cnt = 1; |
436 |
return buf; |
437 |
} |
438 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt" )) { |
439 |
ctx->cnt = 2; |
440 |
return "primary"; |
441 |
} |
442 |
if (ctx->cnt == 2 && !strcmp (key, "passphrase.enter")) { |
443 |
ctx->cnt = 3; |
444 |
return ctx->pass; |
445 |
} |
446 |
if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt")) { |
447 |
ctx->reset (); |
448 |
return "save"; |
449 |
} |
450 |
return NULL; |
451 |
} |
452 |
|
453 |
|
454 |
/* 'expire' command handler. */ |
455 |
static const char* |
456 |
cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
457 |
const char *key) |
458 |
{ |
459 |
static char buf[64]; |
460 |
|
461 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
462 |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
463 |
ctx->cnt = 1; |
464 |
return buf; |
465 |
} |
466 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
467 |
ctx->cnt = 2; |
468 |
return "expire"; |
469 |
} |
470 |
if (ctx->cnt == 2 && !strcmp (key, "keygen.valid" )) { |
471 |
ctx->cnt = 3; |
472 |
sprintf (buf, "%d", ctx->getValidDays ()); |
473 |
return buf; |
474 |
} |
475 |
if (ctx->cnt == 3 && !strcmp (key, "passphrase.enter")) { |
476 |
ctx->cnt = 4; |
477 |
return ctx->pass; |
478 |
} |
479 |
if (ctx->cnt == 4 && !strcmp (key, "keyedit.prompt")) { |
480 |
ctx->reset (); |
481 |
return "save"; |
482 |
} |
483 |
|
484 |
return NULL; |
485 |
} |
486 |
|
487 |
|
488 |
/* 'revuid' command handler. */ |
489 |
const char* |
490 |
cmd_revuid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
491 |
const char *key) |
492 |
{ |
493 |
static char buf[32]; |
494 |
|
495 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
496 |
ctx->cnt = 1; |
497 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
498 |
return buf; |
499 |
} |
500 |
else if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
501 |
ctx->cnt = 2; |
502 |
return "revuid"; |
503 |
} |
504 |
else if (ctx->cnt == 2 && !strcmp (key, "keyedit.revoke.uid.okay")) { |
505 |
ctx->cnt = 3; |
506 |
return "Y"; |
507 |
} |
508 |
else if (ctx->cnt == 3 && !strcmp (key, "ask_revocation_reason.code")) { |
509 |
ctx->cnt = 4; |
510 |
return "4"; |
511 |
} |
512 |
else if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.text")) { |
513 |
ctx->cnt = 5; |
514 |
return ""; |
515 |
} |
516 |
else if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.okay")) { |
517 |
ctx->cnt = 6; |
518 |
return "Y"; |
519 |
} |
520 |
else if (ctx->cnt == 6 && !strcmp (key, "passphrase.enter")) { |
521 |
ctx->cnt = 7; |
522 |
return ctx->pass; |
523 |
} |
524 |
else if (ctx->cnt == 7 && !strcmp (key, "keyedit.prompt")) { |
525 |
ctx->reset (); |
526 |
return "save"; |
527 |
} |
528 |
return NULL; |
529 |
} |
530 |
|
531 |
|
532 |
const char* |
533 |
cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
534 |
const char *key) |
535 |
{ |
536 |
static char buf[64]; |
537 |
|
538 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt" )) { |
539 |
sprintf( buf, "uid %d", ctx->getUseridIndex ()); |
540 |
ctx->cnt = 1; |
541 |
return buf; |
542 |
} |
543 |
if (ctx->cnt == 1 && !strcmp ( key, "keyedit.prompt" ) ) { |
544 |
ctx->cnt = 2; |
545 |
return "revsig"; |
546 |
} |
547 |
if (ctx->cnt == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) { |
548 |
ctx->cnt = 3; |
549 |
return "Y"; |
550 |
} |
551 |
if (ctx->cnt == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) { |
552 |
ctx->cnt = 4; |
553 |
return "Y"; |
554 |
} |
555 |
if (ctx->cnt == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) { |
556 |
ctx->cnt = 5; |
557 |
return "0"; |
558 |
} |
559 |
if (ctx->cnt == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) { |
560 |
ctx->cnt = 6; |
561 |
return ""; |
562 |
} |
563 |
if (ctx->cnt == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) { |
564 |
ctx->cnt = 7; |
565 |
return "Y"; |
566 |
} |
567 |
if (ctx->cnt == 7 && !strcmp (key, "passphrase.enter" ) ) { |
568 |
ctx->cnt = 8; |
569 |
return ctx->pass; |
570 |
} |
571 |
if (ctx->cnt == 8 && !strcmp ( key, "keyedit.prompt" ) ) { |
572 |
ctx->reset (); |
573 |
return "save"; |
574 |
} |
575 |
|
576 |
return NULL; |
577 |
} |
578 |
|
579 |
|
580 |
/* 'revoke' command handler. */ |
581 |
static const char * |
582 |
cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
583 |
const char *key) |
584 |
{ |
585 |
static char buf[64]; |
586 |
|
587 |
if (ctx->cnt == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
588 |
sprintf( buf, "key %d", ctx->getKeyIndex ()); |
589 |
ctx->cnt = 1; |
590 |
return buf; |
591 |
} |
592 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
593 |
ctx->cnt = 2; |
594 |
return "revkey"; |
595 |
} |
596 |
if (ctx->cnt == 2 && !strcmp (key, "keyedit.revoke.subkey.okay")) { |
597 |
ctx->cnt = 3; |
598 |
return "Y"; |
599 |
} |
600 |
if (ctx->cnt == 3 && !strcmp ( key, "ask_revocation_reason.code")) { |
601 |
sprintf( buf, "%d", ctx->reason); |
602 |
ctx->cnt = 4; |
603 |
return buf; |
604 |
} |
605 |
if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.text")) { |
606 |
ctx->cnt = 5; |
607 |
return ""; |
608 |
} |
609 |
if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.okay")) { |
610 |
ctx->cnt = 6; |
611 |
return "Y"; |
612 |
} |
613 |
if (ctx->cnt == 6 && !strcmp (key, "passphrase.enter")) { |
614 |
ctx->cnt = 7; |
615 |
return ctx->pass; |
616 |
} |
617 |
if (ctx->cnt == 7 && !strcmp ( key, "keyedit.prompt")) { |
618 |
ctx->reset (); |
619 |
return "save"; |
620 |
} |
621 |
return NULL; |
622 |
} |
623 |
|
624 |
|
625 |
/* 'addrevoker' command handler. */ |
626 |
static const char * |
627 |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
628 |
const char *key) |
629 |
{ |
630 |
|
631 |
/* If the isuser already signed the key, send an empty |
632 |
string and jump to quit. */ |
633 |
if (ctx->getResult () & EDITKEY_ERR_ALREADY_SIGNED |
634 |
&& ctx->cnt != -1 && ctx->cnt != 4) { |
635 |
ctx->cnt = -1; |
636 |
return ""; |
637 |
} |
638 |
if (ctx->cnt == -1) { |
639 |
ctx->cnt = 4; |
640 |
return ""; /* empty value to abort. */ |
641 |
} |
642 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
643 |
ctx->cnt = 1; |
644 |
return "addrevoker"; |
645 |
} |
646 |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.add_revoker")) { |
647 |
ctx->cnt = 2; |
648 |
return ctx->name; |
649 |
} |
650 |
if (ctx->cnt == 2 && !strcmp (key, "keyedit.add_revoker.okay")) { |
651 |
ctx->cnt = 3; |
652 |
return "Y"; |
653 |
} |
654 |
if (ctx->cnt == 3 && !strcmp (key, "passphrase.enter")) { |
655 |
ctx->cnt = 4; |
656 |
return ctx->pass; |
657 |
} |
658 |
if (ctx->cnt == 4 && !strcmp (key, "keyedit.prompt")) { |
659 |
ctx->reset (); |
660 |
return "save"; |
661 |
} |
662 |
|
663 |
return NULL; |
664 |
} |
665 |
|
666 |
|
667 |
/* 'addphoto' command handler. */ |
668 |
static const char* |
669 |
cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
670 |
{ |
671 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
672 |
ctx->cmd_sent = 1; |
673 |
return "addphoto"; |
674 |
} |
675 |
if (!strcmp (key, "photoid.jpeg.add")) |
676 |
return ctx->url; |
677 |
if (!strcmp (key, "photoid.jpeg.size")) |
678 |
return "Y"; |
679 |
if (!strcmp (key, "passphrase.enter")) |
680 |
return ctx->pass; |
681 |
if (!strcmp (key, "keyedit.prompt")) { |
682 |
ctx->reset (); |
683 |
return "save"; |
684 |
} |
685 |
return NULL; |
686 |
} |
687 |
|
688 |
|
689 |
static const char* |
690 |
cmd_minimize_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
691 |
{ |
692 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
693 |
ctx->cmd_sent = 1; |
694 |
return "minimize"; |
695 |
} |
696 |
if (!strcmp (key, "keyedit.prompt")) { |
697 |
ctx->reset (); |
698 |
return "save"; |
699 |
} |
700 |
|
701 |
return NULL; |
702 |
} |
703 |
|
704 |
/* 'clean' command handler. */ |
705 |
static const char* |
706 |
cmd_clean_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
707 |
{ |
708 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
709 |
ctx->cmd_sent = 1; |
710 |
return "clean"; |
711 |
} |
712 |
if (!strcmp (key, "keyedit.prompt")) { |
713 |
ctx->reset (); |
714 |
return "save"; |
715 |
} |
716 |
return NULL; |
717 |
} |
718 |
|
719 |
|
720 |
/* 'enable' and 'disable' command handler. */ |
721 |
static const char * |
722 |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
723 |
const char * key, int mode) |
724 |
{ |
725 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
726 |
ctx->cmd_sent = 1; |
727 |
return mode? "disable": "enable"; |
728 |
} |
729 |
if (!strcmp (key, "keyedit.prompt")) { |
730 |
ctx->reset (); |
731 |
return "save"; |
732 |
} |
733 |
return NULL; |
734 |
} |
735 |
|
736 |
|
737 |
/* edit key dispatch handler. */ |
738 |
static gpgme_error_t |
739 |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
740 |
const char *key, int fd) |
741 |
{ |
742 |
const char *out = NULL; |
743 |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
744 |
HANDLE hd = (HANDLE)fd; |
745 |
DWORD n; |
746 |
|
747 |
if (!ke) |
748 |
return gpg_error (GPG_ERR_INV_ARG); |
749 |
|
750 |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
751 |
switch (code) { |
752 |
case GPGME_STATUS_ALREADY_SIGNED: |
753 |
ke->setResult (EDITKEY_ERR_ALREADY_SIGNED); |
754 |
break; |
755 |
|
756 |
case GPGME_STATUS_BAD_PASSPHRASE: |
757 |
log_debug ("editkey_command_handler: bad passphrase\n"); |
758 |
ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE); |
759 |
break; |
760 |
|
761 |
default: |
762 |
break; |
763 |
} |
764 |
|
765 |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) { |
766 |
/* If the entered passphrase is bad, we supply empty |
767 |
passphrase to abort and send 'quit' as soon as possible. */ |
768 |
if (!strcmp (key, "passphrase.enter")) |
769 |
WriteFile (hd, "\n", 1, &n, NULL); |
770 |
if (!strcmp (key, "keyedit.prompt")) |
771 |
WriteFile (hd, "quit\n", 5, &n, NULL); |
772 |
ke->reset (); |
773 |
return 0; |
774 |
} |
775 |
|
776 |
switch (ke->getType ()) { |
777 |
case GPG_EDITKEY_LSIGN: |
778 |
case GPG_EDITKEY_SIGN: |
779 |
case GPG_EDITKEY_NRSIGN: |
780 |
case GPG_EDITKEY_TSIGN: |
781 |
case GPG_EDITKEY_NRLSIGN: |
782 |
out = cmd_sign_handler (ke, code, key); |
783 |
break; |
784 |
|
785 |
case GPG_EDITKEY_TRUST: |
786 |
out = cmd_trust_handler (ke, code, key); |
787 |
break; |
788 |
|
789 |
case GPG_EDITKEY_ADDUID: |
790 |
out = cmd_adduid_handler (ke, code, key); |
791 |
break; |
792 |
|
793 |
case GPG_EDITKEY_DELUID: |
794 |
out = cmd_deluid_handler (ke, code, key); |
795 |
break; |
796 |
|
797 |
case GPG_EDITKEY_DELSIG: |
798 |
out = cmd_delsig_handler (ke, code, key); |
799 |
break; |
800 |
|
801 |
case GPG_EDITKEY_DELKEY: |
802 |
out = cmd_delkey_handler(ke, code, key); |
803 |
break; |
804 |
|
805 |
case GPG_EDITKEY_ADDKEY: |
806 |
out = cmd_addkey_handler (ke, code, key); |
807 |
break; |
808 |
|
809 |
case GPG_EDITKEY_ADDCARDKEY: |
810 |
out = cmd_addcardkey_handler (ke, code, key); |
811 |
break; |
812 |
|
813 |
case GPG_EDITKEY_PASSWD: |
814 |
out = cmd_passwd_handler (ke, code, key); |
815 |
break; |
816 |
|
817 |
case GPG_EDITKEY_PRIMARY: |
818 |
out = cmd_primary_handler (ke, code, key); |
819 |
break; |
820 |
|
821 |
case GPG_EDITKEY_EXPIRE: |
822 |
out = cmd_expire_handler (ke, code, key); |
823 |
break; |
824 |
|
825 |
case GPG_EDITKEY_REVSIG: |
826 |
out = cmd_revsig_handler (ke, code, key); |
827 |
break; |
828 |
|
829 |
case GPG_EDITKEY_REVKEY: |
830 |
out = cmd_revkey_handler (ke, code, key); |
831 |
break; |
832 |
|
833 |
case GPG_EDITKEY_REVUID: |
834 |
out = cmd_revuid_handler (ke, code, key); |
835 |
break; |
836 |
|
837 |
case GPG_EDITKEY_ADDREV: |
838 |
out = cmd_addrev_handler (ke, code, key); |
839 |
break; |
840 |
|
841 |
case GPG_EDITKEY_ADDPHOTO: |
842 |
out = cmd_addphoto_handler (ke, code, key); |
843 |
break; |
844 |
|
845 |
case GPG_EDITKEY_NOTATION: |
846 |
out = cmd_notation_handler (ke, code, key); |
847 |
break; |
848 |
|
849 |
case GPG_EDITKEY_MINIMIZE: |
850 |
out = cmd_minimize_handler (ke, code, key); |
851 |
break; |
852 |
|
853 |
case GPG_EDITKEY_CLEAN: |
854 |
out = cmd_clean_handler (ke, code, key); |
855 |
break; |
856 |
|
857 |
case GPG_EDITKEY_ENABLE: |
858 |
case GPG_EDITKEY_DISABLE: |
859 |
n = ke->getType () == GPG_EDITKEY_DISABLE? 1: 0; |
860 |
out = cmd_enable_disable_handler (ke, code, key, n); |
861 |
break; |
862 |
|
863 |
case GPG_EDITKEY_SETPREF: |
864 |
out = cmd_setpref_handler (ke, code, key); |
865 |
break; |
866 |
|
867 |
case GPG_EDITKEY_KEYSERV: |
868 |
out = cmd_keyserv_handler (ke, code, key); |
869 |
break; |
870 |
} |
871 |
|
872 |
if (out != NULL) { |
873 |
WriteFile (hd, out, strlen (out), &n, NULL); |
874 |
WriteFile (hd, "\n", 1, &n, NULL); |
875 |
} |
876 |
return 0; |
877 |
} |
878 |
|
879 |
|
880 |
/* Check if a GPG status code occured which marks the |
881 |
current operation as failed. |
882 |
Return value: gpg error constant. */ |
883 |
static gpgme_error_t |
884 |
map_result (GpgKeyEdit *ke) |
885 |
{ |
886 |
/* XXX Sometimes ALREADY_SIGNED indicates an failure. */ |
887 |
if (!ke->getResult ()) |
888 |
return gpg_error (GPG_ERR_NO_ERROR); |
889 |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) |
890 |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
891 |
return 0; |
892 |
} |
893 |
|
894 |
|
895 |
/* Wrapper around the gpgme edit interface. |
896 |
@ctx context to use. |
897 |
@key key on which the operation should be performed. |
898 |
@ek key edit context. |
899 |
Return value: 0 on success. */ |
900 |
gpgme_error_t |
901 |
gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek) |
902 |
{ |
903 |
gpgme_error_t err; |
904 |
gpgme_data_t out; |
905 |
|
906 |
err = gpgme_data_new (&out); |
907 |
if (err) |
908 |
return err; |
909 |
err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out); |
910 |
if (!err) |
911 |
err = map_result (ek); |
912 |
|
913 |
gpgme_data_release (out); |
914 |
return err; |
915 |
} |