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