Coverage Report

Created: 2026-10-08 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/keyedit.c
Line
Count
Source
1
/* keyedit.c - Edit properties of a key
2
 * Copyright (C) 1998-2010 Free Software Foundation, Inc.
3
 * Copyright (C) 1998-2017 Werner Koch
4
 * Copyright (C) 2015, 2016, 2022-2023 g10 Code GmbH
5
 *
6
 * This file is part of GnuPG.
7
 *
8
 * GnuPG is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * GnuPG is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <errno.h>
27
#include <ctype.h>
28
#ifdef HAVE_LIBREADLINE
29
# define GNUPG_LIBREADLINE_H_INCLUDED
30
# include <readline/readline.h>
31
#endif
32
33
#include "gpg.h"
34
#include "options.h"
35
#include "packet.h"
36
#include "../common/status.h"
37
#include "../common/iobuf.h"
38
#include "keydb.h"
39
#include "photoid.h"
40
#include "../common/util.h"
41
#include "main.h"
42
#include "trustdb.h"
43
#include "filter.h"
44
#include "../common/ttyio.h"
45
#include "../common/status.h"
46
#include "../common/i18n.h"
47
#include "../common/mbox-util.h"
48
#include "keyserver-internal.h"
49
#include "call-agent.h"
50
#include "../common/host2net.h"
51
#include "tofu.h"
52
#include "key-check.h"
53
#include "key-clean.h"
54
#include "keyedit.h"
55
56
static void maybe_upload_key (ctrl_t ctrl, kbnode_t keyblock);
57
static void show_prefs (PKT_user_id * uid, PKT_signature * selfsig,
58
      int verbose);
59
static void show_names (ctrl_t ctrl, estream_t fp,
60
                        kbnode_t keyblock, PKT_public_key * pk,
61
      unsigned int flag, int with_prefs);
62
static void show_key_with_all_names (ctrl_t ctrl, estream_t fp,
63
                                     KBNODE keyblock, int only_marked,
64
             int with_revoker, int with_fpr,
65
             int with_subkeys, int with_prefs,
66
                                     int nowarn);
67
static void show_key_and_fingerprint (ctrl_t ctrl,
68
                                      kbnode_t keyblock, int with_subkeys);
69
static void show_key_and_grip (kbnode_t keyblock);
70
static void subkey_expire_warning (kbnode_t keyblock);
71
static int menu_adduid (ctrl_t ctrl, kbnode_t keyblock,
72
                        int photo, const char *photo_name, const char *uidstr);
73
static void menu_deluid (KBNODE pub_keyblock);
74
static int menu_delsig (ctrl_t ctrl, kbnode_t pub_keyblock);
75
static int menu_clean (ctrl_t ctrl, kbnode_t keyblock, unsigned int options);
76
static void menu_delkey (KBNODE pub_keyblock);
77
static int menu_addrevoker (ctrl_t ctrl, kbnode_t pub_keyblock, int sensitive);
78
static int menu_addadsk (ctrl_t ctrl, kbnode_t pub_keyblock,
79
                         const char *adskfpr);
80
static gpg_error_t menu_expire (ctrl_t ctrl, kbnode_t pub_keyblock,
81
                                int unattended, u32 newexpiration);
82
static int menu_changeusage (ctrl_t ctrl, kbnode_t keyblock);
83
static int menu_backsign (ctrl_t ctrl, kbnode_t pub_keyblock);
84
static int menu_set_primary_uid (ctrl_t ctrl, kbnode_t pub_keyblock);
85
static int menu_set_preferences (ctrl_t ctrl, kbnode_t pub_keyblock,
86
                                 int unattended);
87
static int menu_set_keyserver_url (ctrl_t ctrl,
88
                                   const char *url, kbnode_t pub_keyblock);
89
static int menu_set_notation (ctrl_t ctrl,
90
                              const char *string, kbnode_t pub_keyblock);
91
static int menu_select_uid (KBNODE keyblock, int idx);
92
static int menu_select_uid_namehash (KBNODE keyblock, const char *namehash);
93
static int menu_select_key (KBNODE keyblock, int idx, char *p);
94
static int count_uids (KBNODE keyblock);
95
static int count_uids_with_flag (KBNODE keyblock, unsigned flag);
96
static int count_keys_with_flag (KBNODE keyblock, unsigned flag);
97
static int count_selected_uids (KBNODE keyblock);
98
static int real_uids_left (KBNODE keyblock);
99
static int count_selected_keys (KBNODE keyblock);
100
static int menu_revsig (ctrl_t ctrl, kbnode_t keyblock);
101
static int menu_revuid (ctrl_t ctrl, kbnode_t keyblock);
102
static int core_revuid (ctrl_t ctrl, kbnode_t keyblock, KBNODE node,
103
                        const struct revocation_reason_info *reason,
104
                        int *modified);
105
static int menu_revkey (ctrl_t ctrl, kbnode_t pub_keyblock);
106
static int menu_revsubkey (ctrl_t ctrl, kbnode_t pub_keyblock);
107
#ifndef NO_TRUST_MODELS
108
static int enable_disable_key (ctrl_t ctrl, kbnode_t keyblock, int disable);
109
#endif /*!NO_TRUST_MODELS*/
110
static void menu_showphoto (ctrl_t ctrl, kbnode_t keyblock);
111
112
static int update_trust = 0;
113
114
0
#define CONTROL_D ('D' - 'A' + 1)
115
116
/* Flags values used by sign_uids().  */
117
0
#define SIGN_UIDS_LOCAL         1  /* Create non-exportable sig.    */
118
0
#define SIGN_UIDS_NONREVOCABLE  2  /* Create non-revocable sig.     */
119
0
#define SIGN_UIDS_TRUSTSIG      4  /* Create trust signature.       */
120
0
#define SIGN_UIDS_INTERACTIVE   8  /* Change the way of prompting.  */
121
0
#define SIGN_UIDS_QUICK        16  /* Called by a --quick command.  */
122
123
struct sign_attrib
124
{
125
  int non_exportable, non_revocable;
126
  struct revocation_reason_info *reason;
127
  byte trust_depth, trust_value;
128
  char *trust_regexp;
129
};
130
131
132
133
/* TODO: Fix duplicated code between here and the check-sigs/list-sigs
134
   code in keylist.c. */
135
static int
136
print_and_check_one_sig_colon (ctrl_t ctrl, kbnode_t keyblock, kbnode_t node,
137
             int *inv_sigs, int *no_key, int *oth_err,
138
             int *is_selfsig, int print_without_key)
139
0
{
140
0
  PKT_signature *sig = node->pkt->pkt.signature;
141
0
  int rc, sigrc;
142
143
  /* TODO: Make sure a cached sig record here still has the pk that
144
     issued it.  See also keylist.c:list_keyblock_print */
145
146
0
  rc = check_key_signature (ctrl, keyblock, node, is_selfsig);
147
0
  switch (gpg_err_code (rc))
148
0
    {
149
0
    case 0:
150
0
      node->flag &= ~(NODFLG_BADSIG | NODFLG_NOKEY | NODFLG_SIGERR);
151
0
      sigrc = '!';
152
0
      break;
153
0
    case GPG_ERR_BAD_SIGNATURE:
154
0
      node->flag = NODFLG_BADSIG;
155
0
      sigrc = '-';
156
0
      if (inv_sigs)
157
0
  ++ * inv_sigs;
158
0
      break;
159
0
    case GPG_ERR_NO_PUBKEY:
160
0
    case GPG_ERR_UNUSABLE_PUBKEY:
161
0
      node->flag = NODFLG_NOKEY;
162
0
      sigrc = '?';
163
0
      if (no_key)
164
0
  ++ * no_key;
165
0
      break;
166
0
    default:
167
0
      node->flag = NODFLG_SIGERR;
168
0
      sigrc = '%';
169
0
      if (oth_err)
170
0
  ++ * oth_err;
171
0
      break;
172
0
    }
173
174
0
  if (sigrc != '?' || print_without_key)
175
0
    {
176
0
      es_printf ("sig:%c::%d:%08lX%08lX:%lu:%lu:",
177
0
                 sigrc, sig->pubkey_algo, (ulong) sig->keyid[0],
178
0
                 (ulong) sig->keyid[1], (ulong) sig->timestamp,
179
0
                 (ulong) sig->expiredate);
180
181
0
      if (sig->trust_depth || sig->trust_value)
182
0
  es_printf ("%d %d", sig->trust_depth, sig->trust_value);
183
184
0
      es_printf (":");
185
186
0
      if (sig->trust_regexp)
187
0
  es_write_sanitized (es_stdout,
188
0
          sig->trust_regexp, strlen (sig->trust_regexp),
189
0
          ":", NULL);
190
191
0
      es_printf ("::%02x%c\n", sig->sig_class,
192
0
                 sig->flags.exportable ? 'x' : 'l');
193
194
0
      if (opt.show_subpackets)
195
0
  print_subpackets_colon (sig);
196
0
    }
197
198
0
  return (sigrc == '!');
199
0
}
200
201
202
/*
203
 * Print information about a signature (rc is its status), check it
204
 * and return true if the signature is okay.  NODE must be a signature
205
 * packet.  With EXTENDED set all possible signature list options will
206
 * always be printed.
207
 */
208
int
209
keyedit_print_one_sig (ctrl_t ctrl, estream_t fp,
210
                       int rc, kbnode_t keyblock, kbnode_t node,
211
           int *inv_sigs, int *no_key, int *oth_err,
212
           int is_selfsig, int print_without_key, int extended)
213
0
{
214
0
  PKT_signature *sig = node->pkt->pkt.signature;
215
0
  int sigrc;
216
0
  int is_rev = sig->sig_class == 0x30;
217
218
  /* TODO: Make sure a cached sig record here still has the pk that
219
     issued it.  See also keylist.c:list_keyblock_print */
220
221
0
  switch (gpg_err_code (rc))
222
0
    {
223
0
    case 0:
224
0
      node->flag &= ~(NODFLG_BADSIG | NODFLG_NOKEY | NODFLG_SIGERR);
225
0
      sigrc = '!';
226
0
      break;
227
0
    case GPG_ERR_BAD_SIGNATURE:
228
0
      node->flag = NODFLG_BADSIG;
229
0
      sigrc = '-';
230
0
      if (inv_sigs)
231
0
  ++ * inv_sigs;
232
0
      break;
233
0
    case GPG_ERR_NO_PUBKEY:
234
0
    case GPG_ERR_UNUSABLE_PUBKEY:
235
0
      node->flag = NODFLG_NOKEY;
236
0
      sigrc = '?';
237
0
      if (no_key)
238
0
  ++ * no_key;
239
0
      break;
240
0
    default:
241
0
      node->flag = NODFLG_SIGERR;
242
0
      sigrc = '%';
243
0
      if (oth_err)
244
0
  ++ * oth_err;
245
0
      break;
246
0
    }
247
0
  if (sigrc != '?' || print_without_key)
248
0
    {
249
0
      tty_fprintf (fp, "%s%c%c %c%c%c%c%c%c %s %s",
250
0
      is_rev ? "rev" : "sig", sigrc,
251
0
      (sig->sig_class - 0x10 > 0 &&
252
0
       sig->sig_class - 0x10 <
253
0
       4) ? '0' + sig->sig_class - 0x10 : ' ',
254
0
      sig->flags.exportable ? ' ' : 'L',
255
0
      sig->flags.revocable ? ' ' : 'R',
256
0
      sig->flags.policy_url ? 'P' : ' ',
257
0
      sig->flags.notation ? 'N' : ' ',
258
0
      sig->flags.expired ? 'X' : ' ',
259
0
      (sig->trust_depth > 9) ? 'T' : (sig->trust_depth >
260
0
              0) ? '0' +
261
0
      sig->trust_depth : ' ',
262
0
                  keystr (sig->keyid),
263
0
      datestr_from_sig (sig));
264
0
      if ((opt.list_options & LIST_SHOW_SIG_EXPIRE) || extended )
265
0
  tty_fprintf (fp, " %s", expirestr_from_sig (sig));
266
0
      tty_fprintf (fp, "  ");
267
0
      if (sigrc == '%')
268
0
  tty_fprintf (fp, "[%s] ", gpg_strerror (rc));
269
0
      else if (sigrc == '?')
270
0
  ;
271
0
      else if (is_selfsig)
272
0
  {
273
0
    tty_fprintf (fp, is_rev ? _("[revocation]") : _("[self-signature]"));
274
0
          if (extended && sig->flags.chosen_selfsig)
275
0
            tty_fprintf (fp, "*");
276
0
  }
277
0
      else
278
0
  {
279
0
    size_t n;
280
0
    char *p = get_user_id (ctrl, sig->keyid, &n, NULL);
281
0
    tty_print_utf8_string2 (fp, p, n,
282
0
          opt.screen_columns - keystrlen () - 26 -
283
0
          ((opt.
284
0
            list_options & LIST_SHOW_SIG_EXPIRE) ? 11
285
0
           : 0));
286
0
    xfree (p);
287
0
  }
288
0
      if (fp == log_get_stream ())
289
0
        log_printf ("\n");
290
0
      else
291
0
        tty_fprintf (fp, "\n");
292
293
0
      if (sig->flags.policy_url
294
0
          && ((opt.list_options & LIST_SHOW_POLICY_URLS) || extended))
295
0
  show_policy_url (sig, 3, (!fp? -1 : fp == log_get_stream ()? 1 : 0));
296
297
0
      if (sig->flags.notation
298
0
          && ((opt.list_options & LIST_SHOW_NOTATIONS) || extended))
299
0
  show_notation (sig, 3, (!fp? -1 : fp == log_get_stream ()? 1 : 0),
300
0
           ((opt.
301
0
       list_options & LIST_SHOW_STD_NOTATIONS) ? 1 : 0) +
302
0
           ((opt.
303
0
       list_options & LIST_SHOW_USER_NOTATIONS) ? 2 : 0) +
304
0
                       ((opt.
305
0
                         list_options & LIST_SHOW_HIDDEN_NOTATIONS) ? 4:0));
306
307
0
      if (sig->flags.pref_ks
308
0
          && ((opt.list_options & LIST_SHOW_KEYSERVER_URLS) || extended))
309
0
  show_keyserver_url (sig, 3, (!fp? -1 : fp == log_get_stream ()? 1 : 0));
310
311
0
      if (extended)
312
0
        {
313
0
          PKT_public_key *pk = keyblock->pkt->pkt.public_key;
314
0
          const unsigned char *s;
315
316
0
          s = parse_sig_subpkt (sig, 1, SIGSUBPKT_PRIMARY_UID, NULL);
317
0
          if (s && *s)
318
0
            tty_fprintf (fp, "             [primary]\n");
319
320
0
          s = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_EXPIRE, NULL);
321
0
          if (s && buf32_to_u32 (s))
322
0
            tty_fprintf (fp, "             [expires: %s]\n",
323
0
                         isotimestamp (pk->timestamp + buf32_to_u32 (s)));
324
0
        }
325
0
    }
326
327
0
  return (sigrc == '!');
328
0
}
329
330
331
static int
332
print_and_check_one_sig (ctrl_t ctrl, kbnode_t keyblock, kbnode_t node,
333
       int *inv_sigs, int *no_key, int *oth_err,
334
       int *is_selfsig, int print_without_key, int extended)
335
0
{
336
0
  int rc;
337
338
0
  rc = check_key_signature (ctrl, keyblock, node, is_selfsig);
339
0
  return keyedit_print_one_sig (ctrl, NULL, rc,
340
0
        keyblock, node, inv_sigs, no_key, oth_err,
341
0
        *is_selfsig, print_without_key, extended);
342
0
}
343
344
345
static int
346
sign_mk_attrib (PKT_signature * sig, void *opaque)
347
0
{
348
0
  struct sign_attrib *attrib = opaque;
349
0
  byte buf[8];
350
351
0
  if (attrib->non_exportable)
352
0
    {
353
0
      buf[0] = 0;   /* not exportable */
354
0
      build_sig_subpkt (sig, SIGSUBPKT_EXPORTABLE, buf, 1);
355
0
    }
356
357
0
  if (attrib->non_revocable)
358
0
    {
359
0
      buf[0] = 0;   /* not revocable */
360
0
      build_sig_subpkt (sig, SIGSUBPKT_REVOCABLE, buf, 1);
361
0
    }
362
363
0
  if (attrib->reason)
364
0
    revocation_reason_build_cb (sig, attrib->reason);
365
366
0
  if (attrib->trust_depth)
367
0
    {
368
      /* Not critical.  If someone doesn't understand trust sigs,
369
         this can still be a valid regular signature. */
370
0
      buf[0] = attrib->trust_depth;
371
0
      buf[1] = attrib->trust_value;
372
0
      build_sig_subpkt (sig, SIGSUBPKT_TRUST, buf, 2);
373
374
      /* Critical.  If someone doesn't understands regexps, this
375
         whole sig should be invalid.  Note the +1 for the length -
376
         regexps are null terminated. */
377
0
      if (attrib->trust_regexp)
378
0
  build_sig_subpkt (sig, SIGSUBPKT_FLAG_CRITICAL | SIGSUBPKT_REGEXP,
379
0
        attrib->trust_regexp,
380
0
        strlen (attrib->trust_regexp) + 1);
381
0
    }
382
383
0
  return 0;
384
0
}
385
386
387
/* Parse a trust signature specification string into the 3 return
388
 * args.  Returns 0 on success or an errorcode.  Format for the string
389
 * is
390
 *  ['T=']<depth>,<value>[,<domain>]
391
 * The optional prefix is just to allow c+p from the --check-sigs
392
 * output.  The domain is optional, <depth> must be a value in the
393
 * range 0 to 255, value may either be value in the same range or -
394
 * preferred - 'm' or 'f'.
395
 */
396
static gpg_error_t
397
parse_trustsig_string (const char *string,
398
                       byte *trust_value, byte *trust_depth, char **regexp)
399
0
{
400
0
  gpg_error_t err = 0;
401
0
  char **fields;
402
0
  int nfields;
403
0
  int along;
404
0
  char *endp;
405
406
0
  *trust_value = 0;
407
0
  *trust_depth = 0;
408
0
  *regexp = NULL;
409
410
0
  if (!string)
411
0
    return gpg_error (GPG_ERR_INV_ARG);
412
413
0
  if (*string == 'T' && string[1] == '=')
414
0
    string += 2;
415
416
0
  fields = strtokenize (string, ",");
417
0
  if (!fields)
418
0
      return gpg_error_from_syserror ();
419
420
0
  for (nfields=0; fields[nfields]; nfields++)
421
0
    ;
422
0
  if (nfields < 2 || nfields > 3)
423
0
    {
424
0
      err = gpg_error (GPG_ERR_SYNTAX);
425
0
      goto leave;
426
0
    }
427
0
  along = strtol (fields[0], &endp, 10);
428
0
  if (along < 0 || along > 255 || fields[0] == endp || *endp)
429
0
    {
430
0
      err = gpg_error (GPG_ERR_ERANGE);
431
0
      goto leave;
432
0
    }
433
0
  *trust_depth = along;
434
0
  if (!strcmp (fields[1], "m")|| !strcmp (fields[1], "marginal"))
435
0
    along = 60;
436
0
  else if (!strcmp (fields[1], "f")|| !strcmp (fields[1], "full"))
437
0
    along = 120;
438
0
  else
439
0
    {
440
0
      along = strtol (fields[1], &endp, 10);
441
0
      if (along < 0 || along > 255 || fields[1] == endp || *endp)
442
0
        {
443
0
          err = gpg_error (GPG_ERR_ERANGE);
444
0
          goto leave;
445
0
        }
446
0
    }
447
0
  *trust_value = along;
448
449
0
  if (nfields == 3)
450
0
    {
451
0
      if (!is_valid_domain_name (fields[2]))
452
0
        err = gpg_error (GPG_ERR_NO_NAME);
453
0
      else
454
0
        {
455
0
          *regexp = strconcat ("<[^>]+[@.]", fields[2], ">$", NULL);
456
0
          if (!*regexp)
457
0
            err = gpg_error_from_syserror ();
458
0
        }
459
0
    }
460
461
0
 leave:
462
0
  xfree (fields);
463
0
  if (err && *regexp)
464
0
    {
465
0
      xfree (*regexp);
466
0
      *regexp = NULL;
467
0
    }
468
0
  return err;
469
0
}
470
471
472
/* Interactive version of parse_trustsig_string.  */
473
static void
474
trustsig_prompt (byte * trust_value, byte * trust_depth, char **regexp)
475
0
{
476
0
  char *p;
477
478
0
  *trust_value = 0;
479
0
  *trust_depth = 0;
480
0
  *regexp = NULL;
481
482
  /* Same string as pkclist.c:do_edit_ownertrust */
483
0
  tty_printf (_
484
0
        ("Please decide how far you trust this user to correctly verify"
485
0
         " other users' keys\n(by looking at passports, checking"
486
0
         " fingerprints from different sources, etc.)\n"));
487
0
  tty_printf ("\n");
488
0
  tty_printf (_("  %d = I trust marginally\n"), 1);
489
0
  tty_printf (_("  %d = I trust fully\n"), 2);
490
0
  tty_printf ("\n");
491
492
0
  while (*trust_value == 0)
493
0
    {
494
0
      p = cpr_get ("trustsig_prompt.trust_value", _("Your selection? "));
495
0
      trim_spaces (p);
496
0
      cpr_kill_prompt ();
497
      /* 60 and 120 are as per RFC2440 */
498
0
      if (p[0] == '1' && !p[1])
499
0
  *trust_value = 60;
500
0
      else if (p[0] == '2' && !p[1])
501
0
  *trust_value = 120;
502
0
      xfree (p);
503
0
    }
504
505
0
  tty_printf ("\n");
506
507
0
  tty_printf (_("Please enter the depth of this trust signature.\n"
508
0
    "A depth greater than 1 allows the key you are"
509
0
                " signing to make\n"
510
0
    "trust signatures on your behalf.\n"));
511
0
  tty_printf ("\n");
512
513
0
  while (*trust_depth == 0)
514
0
    {
515
0
      p = cpr_get ("trustsig_prompt.trust_depth", _("Your selection? "));
516
0
      trim_spaces (p);
517
0
      cpr_kill_prompt ();
518
0
      *trust_depth = atoi (p);
519
0
      xfree (p);
520
0
    }
521
522
0
  tty_printf ("\n");
523
524
0
  tty_printf (_("Please enter a domain to restrict this signature, "
525
0
    "or enter for none.\n"));
526
527
0
  tty_printf ("\n");
528
529
0
  p = cpr_get ("trustsig_prompt.trust_regexp", _("Your selection? "));
530
0
  trim_spaces (p);
531
0
  cpr_kill_prompt ();
532
533
0
  if (strlen (p) > 0)
534
0
    {
535
0
      char *q = p;
536
0
      int regexplen = 100, ind;
537
538
0
      *regexp = xmalloc (regexplen);
539
540
      /* Now mangle the domain the user entered into a regexp.  To do
541
         this, \-escape everything that isn't alphanumeric, and attach
542
         "<[^>]+[@.]" to the front, and ">$" to the end. */
543
544
0
      strcpy (*regexp, "<[^>]+[@.]");
545
0
      ind = strlen (*regexp);
546
547
0
      while (*q)
548
0
  {
549
0
    if (!((*q >= 'A' && *q <= 'Z')
550
0
    || (*q >= 'a' && *q <= 'z') || (*q >= '0' && *q <= '9')))
551
0
      (*regexp)[ind++] = '\\';
552
553
0
    (*regexp)[ind++] = *q;
554
555
0
    if ((regexplen - ind) < 3)
556
0
      {
557
0
        regexplen += 100;
558
0
        *regexp = xrealloc (*regexp, regexplen);
559
0
      }
560
561
0
    q++;
562
0
  }
563
564
0
      (*regexp)[ind] = '\0';
565
0
      strcat (*regexp, ">$");
566
0
    }
567
568
0
  xfree (p);
569
0
  tty_printf ("\n");
570
0
}
571
572
573
/*
574
 * Loop over all LOCUSR and sign the uids after asking.  If no user id
575
 * is marked, all user ids will be signed; if some user_ids are marked
576
 * only those will be signed.  FLAGS are the SIGN_UIDS_* constants.
577
 * For example with SIGN_UIDS_QUICK the function won't ask the user
578
 * and use sensible defaults.  TRUSTSIGSTR is only used if also
579
 * SIGN_UIDS_TRUSTSIG is set.
580
 */
581
static int
582
sign_uids (ctrl_t ctrl, estream_t fp,
583
           kbnode_t keyblock, strlist_t locusr, unsigned int flags,
584
           const char *trustsigstr, int *ret_modified)
585
0
{
586
0
  int rc = 0;
587
0
  SK_LIST sk_list = NULL;
588
0
  SK_LIST sk_rover = NULL;
589
0
  PKT_public_key *pk = NULL;
590
0
  KBNODE node, uidnode;
591
0
  PKT_public_key *primary_pk = NULL;
592
0
  char *trust_regexp = NULL;
593
0
  int select_all = (!count_selected_uids (keyblock)
594
0
                    || (flags & SIGN_UIDS_INTERACTIVE));
595
596
597
  /* Build a list of all signators.
598
   *
599
   * We use the CERT flag to request the primary which must always
600
   * be one which is capable of signing keys.  I can't see a reason
601
   * why to sign keys using a subkey.  Implementation of USAGE_CERT
602
   * is just a hack in getkey.c and does not mean that a subkey
603
   * marked as certification capable will be used. */
604
0
  rc = build_sk_list (ctrl, locusr, &sk_list, PUBKEY_USAGE_CERT);
605
0
  if (rc)
606
0
    goto leave;
607
608
  /* Loop over all signators.  */
609
0
  for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next)
610
0
    {
611
0
      u32 sk_keyid[2], pk_keyid[2];
612
0
      char *p;
613
0
      int class = 0, selfsig = 0;
614
0
      u32 duration = 0, timestamp = 0;
615
0
      byte trust_depth = 0, trust_value = 0;
616
617
0
      pk = sk_rover->pk;
618
0
      keyid_from_pk (pk, sk_keyid);
619
620
      /* Set mark A for all selected user ids.  */
621
0
      for (node = keyblock; node; node = node->next)
622
0
  {
623
0
    if (select_all || (node->flag & NODFLG_SELUID))
624
0
      node->flag |= NODFLG_MARK_A;
625
0
    else
626
0
      node->flag &= ~NODFLG_MARK_A;
627
0
  }
628
629
      /* Reset mark for uids which are already signed.  */
630
0
      uidnode = NULL;
631
0
      for (node = keyblock; node; node = node->next)
632
0
  {
633
0
    if (node->pkt->pkttype == PKT_PUBLIC_KEY)
634
0
      {
635
0
        primary_pk = node->pkt->pkt.public_key;
636
0
        keyid_from_pk (primary_pk, pk_keyid);
637
638
        /* Is this a self-sig? */
639
0
        if (pk_keyid[0] == sk_keyid[0] && pk_keyid[1] == sk_keyid[1])
640
0
                selfsig = 1;
641
0
      }
642
0
    else if (node->pkt->pkttype == PKT_USER_ID)
643
0
      {
644
0
        uidnode = (node->flag & NODFLG_MARK_A) ? node : NULL;
645
0
        if (uidnode)
646
0
    {
647
0
      int yesreally = 0;
648
0
      char *user;
649
650
0
                  user = utf8_to_native (uidnode->pkt->pkt.user_id->name,
651
0
                                         uidnode->pkt->pkt.user_id->len, 0);
652
653
0
                  if (opt.only_sign_text_ids
654
0
                      && uidnode->pkt->pkt.user_id->attribs)
655
0
                    {
656
0
                      tty_fprintf (fp, _("Skipping user ID \"%s\","
657
0
                                         " which is not a text ID.\n"),
658
0
                                   user);
659
0
                      uidnode->flag &= ~NODFLG_MARK_A;
660
0
                      uidnode = NULL;
661
0
                    }
662
0
      else if (uidnode->pkt->pkt.user_id->flags.revoked)
663
0
        {
664
0
          tty_fprintf (fp, _("User ID \"%s\" is revoked."), user);
665
666
0
          if (selfsig)
667
0
      tty_fprintf (fp, "\n");
668
0
          else if (opt.expert && !(flags & SIGN_UIDS_QUICK))
669
0
      {
670
0
        tty_fprintf (fp, "\n");
671
        /* No, so remove the mark and continue */
672
0
        if (!cpr_get_answer_is_yes ("sign_uid.revoke_okay",
673
0
                  _("Are you sure you "
674
0
              "still want to sign "
675
0
              "it? (y/N) ")))
676
0
          {
677
0
            uidnode->flag &= ~NODFLG_MARK_A;
678
0
            uidnode = NULL;
679
0
          }
680
0
        else if ((flags & SIGN_UIDS_INTERACTIVE))
681
0
          yesreally = 1;
682
0
      }
683
0
          else
684
0
      {
685
0
        uidnode->flag &= ~NODFLG_MARK_A;
686
0
        uidnode = NULL;
687
0
        tty_fprintf (fp, _("  Unable to sign.\n"));
688
0
      }
689
0
        }
690
0
      else if (uidnode->pkt->pkt.user_id->flags.expired)
691
0
        {
692
0
          tty_fprintf (fp, _("User ID \"%s\" is expired."), user);
693
694
0
          if (selfsig)
695
0
      tty_fprintf (fp, "\n");
696
0
          else if (opt.expert && !(flags & SIGN_UIDS_QUICK))
697
0
      {
698
0
        tty_fprintf (fp, "\n");
699
        /* No, so remove the mark and continue */
700
0
        if (!cpr_get_answer_is_yes ("sign_uid.expire_okay",
701
0
                  _("Are you sure you "
702
0
              "still want to sign "
703
0
              "it? (y/N) ")))
704
0
          {
705
0
            uidnode->flag &= ~NODFLG_MARK_A;
706
0
            uidnode = NULL;
707
0
          }
708
0
        else if ((flags & SIGN_UIDS_INTERACTIVE))
709
0
          yesreally = 1;
710
0
      }
711
0
          else
712
0
      {
713
0
        uidnode->flag &= ~NODFLG_MARK_A;
714
0
        uidnode = NULL;
715
0
        tty_fprintf (fp, _("  Unable to sign.\n"));
716
0
      }
717
0
        }
718
0
      else if (!uidnode->pkt->pkt.user_id->created && !selfsig)
719
0
        {
720
0
          tty_fprintf (fp, _("User ID \"%s\" is not self-signed."),
721
0
                                   user);
722
723
0
          if (opt.expert && !(flags & SIGN_UIDS_QUICK))
724
0
      {
725
0
        tty_fprintf (fp, "\n");
726
        /* No, so remove the mark and continue */
727
0
        if (!cpr_get_answer_is_yes ("sign_uid.nosig_okay",
728
0
                  _("Are you sure you "
729
0
              "still want to sign "
730
0
              "it? (y/N) ")))
731
0
          {
732
0
            uidnode->flag &= ~NODFLG_MARK_A;
733
0
            uidnode = NULL;
734
0
          }
735
0
        else if ((flags & SIGN_UIDS_INTERACTIVE))
736
0
          yesreally = 1;
737
0
      }
738
0
          else
739
0
      {
740
0
        uidnode->flag &= ~NODFLG_MARK_A;
741
0
        uidnode = NULL;
742
0
        tty_fprintf (fp, _("  Unable to sign.\n"));
743
0
      }
744
0
        }
745
746
0
      if (uidnode && (flags & SIGN_UIDS_INTERACTIVE)
747
0
                      && !yesreally && !(flags & SIGN_UIDS_QUICK))
748
0
        {
749
0
          tty_fprintf (fp,
750
0
                                   _("User ID \"%s\" is signable.  "), user);
751
0
          if (!cpr_get_answer_is_yes ("sign_uid.sign_okay",
752
0
              _("Sign it? (y/N) ")))
753
0
      {
754
0
        uidnode->flag &= ~NODFLG_MARK_A;
755
0
        uidnode = NULL;
756
0
      }
757
0
        }
758
759
0
      xfree (user);
760
0
    }
761
0
      }
762
0
    else if (uidnode && node->pkt->pkttype == PKT_SIGNATURE
763
0
       && (node->pkt->pkt.signature->sig_class & ~3) == 0x10)
764
0
      {
765
0
        if (sk_keyid[0] == node->pkt->pkt.signature->keyid[0]
766
0
      && sk_keyid[1] == node->pkt->pkt.signature->keyid[1])
767
0
    {
768
0
      char buf[50];
769
0
      char *user;
770
771
0
                  user = utf8_to_native (uidnode->pkt->pkt.user_id->name,
772
0
                                         uidnode->pkt->pkt.user_id->len, 0);
773
774
      /* It's a v3 self-sig.  Make it into a v4 self-sig? */
775
0
      if (node->pkt->pkt.signature->version < 4
776
0
                      && selfsig && !(flags & SIGN_UIDS_QUICK))
777
0
        {
778
0
          tty_fprintf (fp,
779
0
                                   _("The self-signature on \"%s\"\n"
780
0
                                     "is a PGP 2.x-style signature.\n"), user);
781
782
          /* Note that the regular PGP2 warning below
783
             still applies if there are no v4 sigs on
784
             this key at all. */
785
786
0
          if (opt.expert)
787
0
      if (cpr_get_answer_is_yes ("sign_uid.v4_promote_okay",
788
0
               _("Do you want to promote "
789
0
                 "it to an OpenPGP self-"
790
0
                 "signature? (y/N) ")))
791
0
        {
792
0
          node->flag |= NODFLG_DELSIG;
793
0
          xfree (user);
794
0
          continue;
795
0
        }
796
0
        }
797
798
      /* Is the current signature expired? */
799
0
      if (node->pkt->pkt.signature->flags.expired)
800
0
        {
801
0
          tty_fprintf (fp, _("Your current signature on \"%s\"\n"
802
0
                                         "has expired.\n"), user);
803
804
0
          if ((flags & SIGN_UIDS_QUICK) || cpr_get_answer_is_yes
805
0
        ("sign_uid.replace_expired_okay",
806
0
         _("Do you want to issue a "
807
0
           "new signature to replace "
808
0
           "the expired one? (y/N) ")))
809
0
      {
810
        /* Mark these for later deletion.  We
811
           don't want to delete them here, just in
812
           case the replacement signature doesn't
813
           happen for some reason.  We only delete
814
           these after the replacement is already
815
           in place. */
816
817
0
        node->flag |= NODFLG_DELSIG;
818
0
        xfree (user);
819
0
        continue;
820
0
      }
821
0
        }
822
823
0
      if (!node->pkt->pkt.signature->flags.exportable
824
0
                      && !(flags & SIGN_UIDS_LOCAL))
825
0
        {
826
          /* It's a local sig, and we want to make a
827
             exportable sig. */
828
0
          tty_fprintf (fp, _("Your current signature on \"%s\"\n"
829
0
                                         "is a local signature.\n"), user);
830
831
0
          if ((flags & SIGN_UIDS_QUICK) || cpr_get_answer_is_yes
832
0
        ("sign_uid.local_promote_okay",
833
0
         _("Do you want to promote "
834
0
           "it to a full exportable " "signature? (y/N) ")))
835
0
      {
836
        /* Mark these for later deletion.  We
837
           don't want to delete them here, just in
838
           case the replacement signature doesn't
839
           happen for some reason.  We only delete
840
           these after the replacement is already
841
           in place. */
842
843
0
        node->flag |= NODFLG_DELSIG;
844
0
        xfree (user);
845
0
        continue;
846
0
      }
847
0
        }
848
849
      /* Fixme: see whether there is a revocation in which
850
       * case we should allow signing it again. */
851
0
      if (!node->pkt->pkt.signature->flags.exportable
852
0
                      && (flags & SIGN_UIDS_LOCAL))
853
0
        tty_fprintf ( fp,
854
0
                       _("\"%s\" was already locally signed by key %s\n"),
855
0
                       user, keystr_from_pk (pk));
856
0
      else
857
0
        tty_fprintf (fp,
858
0
                                _("\"%s\" was already signed by key %s\n"),
859
0
        user, keystr_from_pk (pk));
860
861
0
                  if (node->pkt->pkt.signature->digest_algo
862
0
                      == DIGEST_ALGO_SHA1
863
0
                      && !opt.flags.allow_weak_key_signatures)
864
0
                    {
865
                      /* Allow updating a signature to a stronger
866
                       * digest algorithm without an extra option.  */
867
0
          xfree (user);
868
0
          continue;
869
0
                    }
870
0
      else if (opt.flags.force_sign_key
871
0
                      || (opt.expert && !(flags & SIGN_UIDS_QUICK)
872
0
                          && cpr_get_answer_is_yes ("sign_uid.dupe_okay",
873
0
                                                    _("Do you want to sign it "
874
0
                                                      "again anyway? (y/N) "))))
875
0
        {
876
          /* Don't delete the old sig here since this is
877
             an --expert thing. */
878
0
          xfree (user);
879
0
          continue;
880
0
        }
881
882
0
      snprintf (buf, sizeof buf, "%08lX%08lX",
883
0
          (ulong) pk->keyid[0], (ulong) pk->keyid[1]);
884
0
      write_status_text (STATUS_ALREADY_SIGNED, buf);
885
0
      uidnode->flag &= ~NODFLG_MARK_A; /* remove mark */
886
887
0
      xfree (user);
888
0
    }
889
0
      }
890
0
  }
891
892
      /* Check whether any uids are left for signing.  */
893
0
      if (!count_uids_with_flag (keyblock, NODFLG_MARK_A))
894
0
  {
895
0
    tty_fprintf (fp, _("Nothing to sign with key %s\n"),
896
0
          keystr_from_pk (pk));
897
0
    continue;
898
0
  }
899
900
      /* Ask whether we really should sign these user id(s). */
901
0
      tty_fprintf (fp, "\n");
902
0
      show_key_with_all_names (ctrl, fp, keyblock, 1, 0, 1, 0, 0, 0);
903
0
      tty_fprintf (fp, "\n");
904
905
0
      if (primary_pk->expiredate && !selfsig)
906
0
  {
907
          /* Static analyzer note: A claim that PRIMARY_PK might be
908
             NULL is not correct because it set from the public key
909
             packet which is always the first packet in a keyblock and
910
             parsed in the above loop over the keyblock.  In case the
911
             keyblock has no packets at all and thus the loop was not
912
             entered the above count_uids_with_flag would have
913
             detected this case.  */
914
915
0
    u32 now = make_timestamp ();
916
917
0
    if (primary_pk->expiredate <= now)
918
0
      {
919
0
        tty_fprintf (fp, _("This key has expired!"));
920
921
0
        if (opt.expert && !(flags & SIGN_UIDS_QUICK))
922
0
    {
923
0
      tty_fprintf (fp, "  ");
924
0
      if (!cpr_get_answer_is_yes ("sign_uid.expired_okay",
925
0
                _("Are you sure you still "
926
0
            "want to sign it? (y/N) ")))
927
0
        continue;
928
0
    }
929
0
        else
930
0
    {
931
0
      tty_fprintf (fp, _("  Unable to sign.\n"));
932
0
      continue;
933
0
    }
934
0
      }
935
0
    else
936
0
      {
937
0
        tty_fprintf (fp, _("This key is due to expire on %s.\n"),
938
0
                           expirestr_from_pk (primary_pk));
939
940
0
        if (opt.ask_cert_expire && !(flags & SIGN_UIDS_QUICK))
941
0
    {
942
0
      char *answer = cpr_get ("sign_uid.expire",
943
0
            _("Do you want your signature to "
944
0
              "expire at the same time? (Y/n) "));
945
0
      if (answer_is_yes_no_default (answer, 1))
946
0
        {
947
          /* This fixes the signature timestamp we're
948
             going to make as now.  This is so the
949
             expiration date is exactly correct, and not
950
             a few seconds off (due to the time it takes
951
             to answer the questions, enter the
952
             passphrase, etc). */
953
0
          timestamp = now;
954
0
          duration = primary_pk->expiredate - now;
955
0
        }
956
957
0
      cpr_kill_prompt ();
958
0
      xfree (answer);
959
0
    }
960
0
      }
961
0
  }
962
963
      /* Only ask for duration if we haven't already set it to match
964
         the expiration of the pk */
965
0
      if (!duration && !selfsig)
966
0
  {
967
0
    if (opt.ask_cert_expire && !(flags & SIGN_UIDS_QUICK))
968
0
      duration = ask_expire_interval (1, opt.def_cert_expire);
969
0
    else
970
0
      duration = parse_expire_string (opt.def_cert_expire);
971
0
  }
972
973
0
      if (selfsig)
974
0
  ;
975
0
      else
976
0
  {
977
0
    if (opt.batch || !opt.ask_cert_level || (flags & SIGN_UIDS_QUICK))
978
0
      class = 0x10 + opt.def_cert_level;
979
0
    else
980
0
      {
981
0
        char *answer;
982
983
0
        tty_fprintf (fp,
984
0
                           _("How carefully have you verified the key you are "
985
0
          "about to sign actually belongs\nto the person "
986
0
          "named above?  If you don't know what to "
987
0
          "answer, enter \"0\".\n"));
988
0
        tty_fprintf (fp, "\n");
989
0
        tty_fprintf (fp, _("   (0) I will not answer.%s\n"),
990
0
        opt.def_cert_level == 0 ? " (default)" : "");
991
0
        tty_fprintf (fp, _("   (1) I have not checked at all.%s\n"),
992
0
        opt.def_cert_level == 1 ? " (default)" : "");
993
0
        tty_fprintf (fp, _("   (2) I have done casual checking.%s\n"),
994
0
        opt.def_cert_level == 2 ? " (default)" : "");
995
0
        tty_fprintf (fp,
996
0
                           _("   (3) I have done very careful checking.%s\n"),
997
0
        opt.def_cert_level == 3 ? " (default)" : "");
998
0
        tty_fprintf (fp, "\n");
999
1000
0
        while (class == 0)
1001
0
    {
1002
0
      answer = cpr_get ("sign_uid.class",
1003
0
                                    _("Your selection? "
1004
0
                                      "(enter '?' for more information): "));
1005
0
      if (answer[0] == '\0')
1006
0
        class = 0x10 + opt.def_cert_level; /* Default */
1007
0
      else if (ascii_strcasecmp (answer, "0") == 0)
1008
0
        class = 0x10; /* Generic */
1009
0
      else if (ascii_strcasecmp (answer, "1") == 0)
1010
0
        class = 0x11; /* Persona */
1011
0
      else if (ascii_strcasecmp (answer, "2") == 0)
1012
0
        class = 0x12; /* Casual */
1013
0
      else if (ascii_strcasecmp (answer, "3") == 0)
1014
0
        class = 0x13; /* Positive */
1015
0
      else
1016
0
        tty_fprintf (fp, _("Invalid selection.\n"));
1017
1018
0
      xfree (answer);
1019
0
    }
1020
0
      }
1021
1022
0
    if ((flags & SIGN_UIDS_TRUSTSIG))
1023
0
            {
1024
0
              xfree (trust_regexp);
1025
0
              trust_regexp = NULL;
1026
0
              if ((flags & SIGN_UIDS_QUICK))
1027
0
                {
1028
0
                  rc = parse_trustsig_string (trustsigstr, &trust_value,
1029
0
                                              &trust_depth, &trust_regexp);
1030
0
                  if (rc)
1031
0
                    goto leave;
1032
0
                }
1033
0
              else
1034
0
                trustsig_prompt (&trust_value, &trust_depth, &trust_regexp);
1035
0
            }
1036
0
        }
1037
1038
0
      if (!(flags & SIGN_UIDS_QUICK))
1039
0
        {
1040
0
          p = get_user_id_native (ctrl, sk_keyid);
1041
0
          tty_fprintf (fp,
1042
0
                   _("Are you sure that you want to sign this key with your\n"
1043
0
                     "key \"%s\" (%s)\n"), p, keystr_from_pk (pk));
1044
0
          xfree (p);
1045
0
        }
1046
1047
0
      if (selfsig)
1048
0
  {
1049
0
    tty_fprintf (fp, "\n");
1050
0
    tty_fprintf (fp, _("This will be a self-signature.\n"));
1051
1052
0
    if ((flags & SIGN_UIDS_LOCAL))
1053
0
      {
1054
0
        tty_fprintf (fp, "\n");
1055
0
        tty_fprintf (fp, _("WARNING: the signature will not be marked "
1056
0
                                 "as non-exportable.\n"));
1057
0
      }
1058
1059
0
    if ((flags & SIGN_UIDS_NONREVOCABLE))
1060
0
      {
1061
0
        tty_fprintf (fp, "\n");
1062
0
        tty_fprintf (fp, _("WARNING: the signature will not be marked "
1063
0
                                 "as non-revocable.\n"));
1064
0
      }
1065
0
  }
1066
0
      else
1067
0
  {
1068
0
    if ((flags & SIGN_UIDS_LOCAL))
1069
0
      {
1070
0
        tty_fprintf (fp, "\n");
1071
0
        tty_fprintf (fp,
1072
0
                 _("The signature will be marked as non-exportable.\n"));
1073
0
      }
1074
1075
0
    if ((flags & SIGN_UIDS_NONREVOCABLE))
1076
0
      {
1077
0
        tty_fprintf (fp, "\n");
1078
0
        tty_fprintf (fp,
1079
0
                 _("The signature will be marked as non-revocable.\n"));
1080
0
      }
1081
1082
0
    switch (class)
1083
0
      {
1084
0
      case 0x11:
1085
0
        tty_fprintf (fp, "\n");
1086
0
        tty_fprintf (fp, _("I have not checked this key at all.\n"));
1087
0
        break;
1088
1089
0
      case 0x12:
1090
0
        tty_fprintf (fp, "\n");
1091
0
        tty_fprintf (fp, _("I have checked this key casually.\n"));
1092
0
        break;
1093
1094
0
      case 0x13:
1095
0
        tty_fprintf (fp, "\n");
1096
0
        tty_fprintf (fp, _("I have checked this key very carefully.\n"));
1097
0
        break;
1098
0
      }
1099
0
  }
1100
1101
0
      tty_fprintf (fp, "\n");
1102
1103
0
      if (opt.batch && opt.answer_yes)
1104
0
  ;
1105
0
      else if ((flags & SIGN_UIDS_QUICK))
1106
0
        ;
1107
0
      else if (!cpr_get_answer_is_yes ("sign_uid.okay",
1108
0
               _("Really sign? (y/N) ")))
1109
0
  continue;
1110
1111
      /* Now we can sign the user ids.  */
1112
0
    reloop:  /* (Must use this, because we are modifying the list.)  */
1113
0
      primary_pk = NULL;
1114
0
      for (node = keyblock; node; node = node->next)
1115
0
  {
1116
0
    if (node->pkt->pkttype == PKT_PUBLIC_KEY)
1117
0
      primary_pk = node->pkt->pkt.public_key;
1118
0
    else if (node->pkt->pkttype == PKT_USER_ID
1119
0
       && (node->flag & NODFLG_MARK_A))
1120
0
      {
1121
0
        PACKET *pkt;
1122
0
        PKT_signature *sig;
1123
0
        struct sign_attrib attrib;
1124
1125
0
        log_assert (primary_pk);
1126
0
        memset (&attrib, 0, sizeof attrib);
1127
0
        attrib.non_exportable = !!(flags & SIGN_UIDS_LOCAL);
1128
0
        attrib.non_revocable = !!(flags & SIGN_UIDS_NONREVOCABLE);
1129
0
        attrib.trust_depth = trust_depth;
1130
0
        attrib.trust_value = trust_value;
1131
0
        attrib.trust_regexp = trust_regexp;
1132
0
        node->flag &= ~NODFLG_MARK_A;
1133
1134
        /* We force creation of a v4 signature for local
1135
         * signatures, otherwise we would not generate the
1136
         * subpacket with v3 keys and the signature becomes
1137
         * exportable.  */
1138
1139
0
        if (selfsig)
1140
0
    rc = make_keysig_packet (ctrl, &sig, primary_pk,
1141
0
           node->pkt->pkt.user_id,
1142
0
           NULL,
1143
0
           pk,
1144
0
           0x13,
1145
0
                                         0, 0,
1146
0
           keygen_add_std_prefs, primary_pk,
1147
0
                                         NULL);
1148
0
        else
1149
0
    rc = make_keysig_packet (ctrl, &sig, primary_pk,
1150
0
           node->pkt->pkt.user_id,
1151
0
           NULL,
1152
0
           pk,
1153
0
           class,
1154
0
           timestamp, duration,
1155
0
           sign_mk_attrib, &attrib,
1156
0
                                         NULL);
1157
0
        if (rc)
1158
0
    {
1159
0
                  write_status_error ("keysig", rc);
1160
0
      log_error (_("signing failed: %s\n"), gpg_strerror (rc));
1161
0
      goto leave;
1162
0
    }
1163
1164
0
        *ret_modified = 1;  /* We changed the keyblock. */
1165
0
        update_trust = 1;
1166
1167
0
        pkt = xmalloc_clear (sizeof *pkt);
1168
0
        pkt->pkttype = PKT_SIGNATURE;
1169
0
        pkt->pkt.signature = sig;
1170
0
        insert_kbnode (node, new_kbnode (pkt), PKT_SIGNATURE);
1171
0
        goto reloop;
1172
0
      }
1173
0
  }
1174
1175
      /* Delete any sigs that got promoted */
1176
0
      for (node = keyblock; node; node = node->next)
1177
0
  if (node->flag & NODFLG_DELSIG)
1178
0
    delete_kbnode (node);
1179
0
    } /* End loop over signators.  */
1180
1181
0
 leave:
1182
0
  xfree (trust_regexp);
1183
0
  trust_regexp = NULL;
1184
0
  release_sk_list (sk_list);
1185
0
  return rc;
1186
0
}
1187
1188
1189
/*
1190
 * Change the passphrase of the primary and all secondary keys.  Note
1191
 * that it is common to use only one passphrase for the primary and
1192
 * all subkeys.  However, this is now (since GnuPG 2.1) all up to the
1193
 * gpg-agent.  Returns 0 on success or an error code.
1194
 */
1195
static gpg_error_t
1196
change_passphrase (ctrl_t ctrl, kbnode_t keyblock)
1197
0
{
1198
0
  gpg_error_t err;
1199
0
  kbnode_t node;
1200
0
  PKT_public_key *pk;
1201
0
  int any;
1202
0
  u32 keyid[2], subid[2];
1203
0
  char *hexgrip = NULL;
1204
0
  const char *hexgrip2;
1205
0
  char *cache_nonce = NULL;
1206
0
  char *passwd_nonce = NULL;
1207
1208
0
  node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
1209
0
  if (!node)
1210
0
    {
1211
0
      log_error ("Oops; public key missing!\n");
1212
0
      err = gpg_error (GPG_ERR_INTERNAL);
1213
0
      goto leave;
1214
0
    }
1215
0
  pk = node->pkt->pkt.public_key;
1216
0
  keyid_from_pk (pk, keyid);
1217
1218
  /* Check whether it is likely that we will be able to change the
1219
     passphrase for any subkey.  */
1220
0
  for (any = 0, node = keyblock; node; node = node->next)
1221
0
    {
1222
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
1223
0
    || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1224
0
  {
1225
0
          char *serialno;
1226
1227
0
          pk = node->pkt->pkt.public_key;
1228
0
          keyid_from_pk (pk, subid);
1229
1230
0
          xfree (hexgrip);
1231
0
          err = hexkeygrip_from_pk (pk, &hexgrip);
1232
0
          if (err)
1233
0
            goto leave;
1234
0
          err = agent_get_keyinfo (ctrl, hexgrip, &serialno, NULL);
1235
0
          if (!err && serialno)
1236
0
            ; /* Key on card.  */
1237
0
          else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1238
0
            ; /* Maybe stub key. */
1239
0
          else if (!err)
1240
0
            any = 1; /* Key is known.  */
1241
0
          else
1242
0
            log_error ("key %s: error getting keyinfo from agent: %s\n",
1243
0
                       keystr_with_sub (keyid, subid), gpg_strerror (err));
1244
0
          xfree (serialno);
1245
0
  }
1246
0
    }
1247
0
  err = 0;
1248
0
  if (!any)
1249
0
    {
1250
0
      tty_printf (_("Key has only stub or on-card key items - "
1251
0
        "no passphrase to change.\n"));
1252
0
      goto leave;
1253
0
    }
1254
1255
  /* Change the passphrase for all keys.  */
1256
0
  for (node = keyblock; node; node = node->next)
1257
0
    {
1258
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
1259
0
    || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1260
0
        {
1261
0
          char *desc, *p;
1262
1263
0
          pk = node->pkt->pkt.public_key;
1264
0
          keyid_from_pk (pk, subid);
1265
1266
0
          xfree (hexgrip);
1267
0
          err = hexkeygrip_from_pk (pk, &hexgrip);
1268
0
          if (err)
1269
0
            goto leave;
1270
0
          if ((p=strchr (hexgrip, ',')))
1271
0
            {
1272
0
              *p++ = 0;
1273
0
              hexgrip2 = p;
1274
0
            }
1275
0
          else
1276
0
            hexgrip2 = NULL;
1277
1278
          /* Note that when using --dry-run we don't change the
1279
           * passphrase but merely verify the current passphrase.  */
1280
0
          desc = gpg_format_keydesc (ctrl, pk, FORMAT_KEYDESC_NORMAL, 1);
1281
0
          err = agent_passwd (ctrl, hexgrip, desc, !!opt.dry_run,
1282
0
                              &cache_nonce, &passwd_nonce);
1283
0
          if (!err && hexgrip2)
1284
0
            err = agent_passwd (ctrl, hexgrip2, desc, !!opt.dry_run,
1285
0
                                &cache_nonce, &passwd_nonce);
1286
0
          xfree (desc);
1287
1288
0
          if (err)
1289
0
            log_log ((gpg_err_code (err) == GPG_ERR_CANCELED
1290
0
                      || gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
1291
0
                     ? GPGRT_LOGLVL_INFO : GPGRT_LOGLVL_ERROR,
1292
0
                     _("key %s: error changing passphrase: %s\n"),
1293
0
                       keystr_with_sub (keyid, subid),
1294
0
                       gpg_strerror (err));
1295
0
          if (gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
1296
0
            break;
1297
0
        }
1298
0
    }
1299
1300
0
 leave:
1301
0
  xfree (hexgrip);
1302
0
  xfree (cache_nonce);
1303
0
  xfree (passwd_nonce);
1304
0
  return err;
1305
0
}
1306
1307
1308

1309
/* Fix various problems in the keyblock.  Returns true if the keyblock
1310
   was changed.  Note that a pointer to the keyblock must be given and
1311
   the function may change it (i.e. replacing the first node).  */
1312
static int
1313
fix_keyblock (ctrl_t ctrl, kbnode_t *keyblockp)
1314
0
{
1315
0
  int changed = 0;
1316
1317
0
  if (collapse_uids (keyblockp))
1318
0
    changed++;
1319
0
  if (collapse_subkeys (keyblockp))
1320
0
    changed++;
1321
0
  if (key_check_all_keysigs (ctrl, 1, *keyblockp, 0, 1))
1322
0
    changed++;
1323
0
  reorder_keyblock (*keyblockp);
1324
  /* If we modified the keyblock, make sure the flags are right. */
1325
0
  if (changed)
1326
0
    merge_keys_and_selfsig (ctrl, *keyblockp);
1327
1328
0
  return changed;
1329
0
}
1330
1331
1332
/* Helper to parse the prefix of the sign command STR and set the
1333
 * respective bits in R_FLAGS.  Returns false on error.  */
1334
static int
1335
parse_sign_type (const char *str, unsigned int *r_flags)
1336
0
{
1337
0
  const char *p = str;
1338
1339
0
  while (*p)
1340
0
    {
1341
0
      if (ascii_strncasecmp (p, "l", 1) == 0)
1342
0
  {
1343
0
    *r_flags |= SIGN_UIDS_LOCAL;
1344
0
    p++;
1345
0
  }
1346
0
      else if (ascii_strncasecmp (p, "nr", 2) == 0)
1347
0
  {
1348
0
    *r_flags |= SIGN_UIDS_NONREVOCABLE;
1349
0
    p += 2;
1350
0
  }
1351
0
      else if (ascii_strncasecmp (p, "t", 1) == 0)
1352
0
  {
1353
0
    *r_flags |= SIGN_UIDS_TRUSTSIG;
1354
0
    p++;
1355
0
  }
1356
0
      else
1357
0
  return 0;
1358
0
    }
1359
1360
0
  return 1;
1361
0
}
1362
1363
1364

1365
/*
1366
 * Menu driven key editor.  If seckey_check is true, then a secret key
1367
 * that matches username will be looked for.  If it is false, not all
1368
 * commands will be available.
1369
 *
1370
 * Note: to keep track of certain selections we use node->mark MARKBIT_xxxx.
1371
 */
1372
1373
/* Need an SK for this command */
1374
0
#define KEYEDIT_NEED_SK 1
1375
/* Need an SUB KEY for this command */
1376
0
#define KEYEDIT_NEED_SUBSK 2
1377
/* Match the tail of the string */
1378
0
#define KEYEDIT_TAIL_MATCH 8
1379
1380
enum cmdids
1381
{
1382
  cmdNONE = 0,
1383
  cmdQUIT, cmdHELP, cmdFPR, cmdLIST, cmdSELUID, cmdCHECK, cmdSIGN,
1384
  cmdREVSIG, cmdREVKEY, cmdREVUID, cmdDELSIG, cmdPRIMARY, cmdDEBUG,
1385
  cmdSAVE, cmdADDUID, cmdADDPHOTO, cmdDELUID, cmdADDKEY, cmdDELKEY,
1386
  cmdADDREVOKER, cmdTOGGLE, cmdSELKEY, cmdPASSWD, cmdTRUST, cmdPREF,
1387
  cmdEXPIRE, cmdCHANGEUSAGE, cmdBACKSIGN, cmdADDADSK, cmdADDPUBKEY,
1388
#ifndef NO_TRUST_MODELS
1389
  cmdENABLEKEY, cmdDISABLEKEY,
1390
#endif /*!NO_TRUST_MODELS*/
1391
  cmdSHOWPREF,
1392
  cmdSETPREF, cmdPREFKS, cmdNOTATION, cmdINVCMD, cmdSHOWPHOTO, cmdUPDTRUST,
1393
  cmdCHKTRUST, cmdADDCARDKEY, cmdKEYTOCARD, cmdKEYTOTPM, cmdBKUPTOCARD,
1394
  cmdCLEAN, cmdMINIMIZE, cmdGRIP, cmdNOP
1395
};
1396
1397
static struct
1398
{
1399
  const char *name;
1400
  enum cmdids id;
1401
  int flags;
1402
  const char *desc;
1403
} cmds[] =
1404
{
1405
  { "quit", cmdQUIT, 0, N_("quit this menu")},
1406
  { "q", cmdQUIT, 0, NULL},
1407
  { "save", cmdSAVE, 0, N_("save and quit")},
1408
  { "help", cmdHELP, 0, N_("show this help")},
1409
  { "?", cmdHELP, 0, NULL},
1410
  { "fpr", cmdFPR, 0, N_("show key fingerprint")},
1411
  { "grip", cmdGRIP, 0, N_("show the keygrip")},
1412
  { "list", cmdLIST, 0, N_("list key and user IDs")},
1413
  { "l", cmdLIST, 0, NULL},
1414
  { "uid", cmdSELUID, 0, N_("select user ID N")},
1415
  { "key", cmdSELKEY, 0, N_("select subkey N")},
1416
  { "check", cmdCHECK, 0, N_("check signatures")},
1417
  { "c", cmdCHECK, 0, NULL},
1418
  { "change-usage", cmdCHANGEUSAGE, KEYEDIT_NEED_SK, NULL},
1419
  { "cross-certify", cmdBACKSIGN, KEYEDIT_NEED_SK, NULL},
1420
  { "backsign", cmdBACKSIGN,  KEYEDIT_NEED_SK, NULL},
1421
  { "sign", cmdSIGN,  KEYEDIT_TAIL_MATCH,
1422
    N_("sign selected user IDs [* see below for related commands]")},
1423
  { "s", cmdSIGN, 0, NULL},
1424
    /* "lsign" and friends will never match since "sign" comes first
1425
       and it is a tail match.  They are just here so they show up in
1426
       the help menu. */
1427
  { "lsign", cmdNOP, 0, N_("sign selected user IDs locally")},
1428
  { "tsign", cmdNOP, 0, N_("sign selected user IDs with a trust signature")},
1429
  { "nrsign", cmdNOP, 0,
1430
    N_("sign selected user IDs with a non-revocable signature")},
1431
  { "debug", cmdDEBUG, 0, NULL},
1432
  { "adduid", cmdADDUID,  KEYEDIT_NEED_SK, N_("add a user ID")},
1433
  { "addphoto", cmdADDPHOTO,  KEYEDIT_NEED_SK,
1434
    N_("add a photo ID")},
1435
  { "deluid", cmdDELUID, 0, N_("delete selected user IDs")},
1436
    /* delphoto is really deluid in disguise */
1437
  { "delphoto", cmdDELUID, 0, NULL},
1438
  { "addkey", cmdADDKEY,  KEYEDIT_NEED_SK, N_("add a subkey")},
1439
  { "addpubkey", cmdADDPUBKEY,  KEYEDIT_NEED_SK, NULL },
1440
#ifdef ENABLE_CARD_SUPPORT
1441
  { "addcardkey", cmdADDCARDKEY,  KEYEDIT_NEED_SK,
1442
    N_("add a key to a smartcard")},
1443
  { "keytocard", cmdKEYTOCARD, KEYEDIT_NEED_SK | KEYEDIT_NEED_SUBSK,
1444
    N_("move a key to a smartcard")},
1445
  { "keytotpm", cmdKEYTOTPM, KEYEDIT_NEED_SK | KEYEDIT_NEED_SUBSK,
1446
    N_("convert a key to TPM form using the local TPM")},
1447
  { "bkuptocard", cmdBKUPTOCARD, KEYEDIT_NEED_SK | KEYEDIT_NEED_SUBSK,
1448
    N_("move a backup key to a smartcard")},
1449
#endif /*ENABLE_CARD_SUPPORT */
1450
  { "delkey", cmdDELKEY, 0, N_("delete selected subkeys")},
1451
  { "addrevoker", cmdADDREVOKER,  KEYEDIT_NEED_SK,
1452
    N_("add a revocation key")},
1453
  { "addadsk", cmdADDADSK,  KEYEDIT_NEED_SK,
1454
    N_("add an additional decryption subkey")},
1455
  { "delsig", cmdDELSIG, 0,
1456
    N_("delete signatures from the selected user IDs")},
1457
  { "expire", cmdEXPIRE,  KEYEDIT_NEED_SK | KEYEDIT_NEED_SUBSK,
1458
    N_("change the expiration date for the key or selected subkeys")},
1459
  { "primary", cmdPRIMARY,  KEYEDIT_NEED_SK,
1460
    N_("flag the selected user ID as primary")},
1461
  { "toggle", cmdTOGGLE, KEYEDIT_NEED_SK, NULL},  /* Dummy command.  */
1462
  { "t", cmdTOGGLE, KEYEDIT_NEED_SK, NULL},
1463
  { "pref", cmdPREF, 0, N_("list preferences (expert)")},
1464
  { "showpref", cmdSHOWPREF, 0, N_("list preferences (verbose)")},
1465
  { "setpref", cmdSETPREF,  KEYEDIT_NEED_SK,
1466
    N_("set preference list for the selected user IDs")},
1467
  { "updpref", cmdSETPREF,  KEYEDIT_NEED_SK, NULL},
1468
  { "keyserver", cmdPREFKS,  KEYEDIT_NEED_SK,
1469
    N_("set the preferred keyserver URL for the selected user IDs")},
1470
  { "notation", cmdNOTATION,  KEYEDIT_NEED_SK,
1471
    N_("set a notation for the selected user IDs")},
1472
  { "passwd", cmdPASSWD,  KEYEDIT_NEED_SK | KEYEDIT_NEED_SUBSK,
1473
    N_("change the passphrase")},
1474
  { "password", cmdPASSWD,  KEYEDIT_NEED_SK | KEYEDIT_NEED_SUBSK, NULL},
1475
#ifndef NO_TRUST_MODELS
1476
  { "trust", cmdTRUST, 0, N_("change the ownertrust")},
1477
#endif /*!NO_TRUST_MODELS*/
1478
  { "revsig", cmdREVSIG, 0,
1479
    N_("revoke signatures on the selected user IDs")},
1480
  { "revuid", cmdREVUID,  KEYEDIT_NEED_SK,
1481
    N_("revoke selected user IDs")},
1482
  { "revphoto", cmdREVUID,  KEYEDIT_NEED_SK, NULL},
1483
  { "revkey", cmdREVKEY,  KEYEDIT_NEED_SK,
1484
    N_("revoke key or selected subkeys")},
1485
#ifndef NO_TRUST_MODELS
1486
  { "enable", cmdENABLEKEY, 0, N_("enable key")},
1487
  { "disable", cmdDISABLEKEY, 0, N_("disable key")},
1488
#endif /*!NO_TRUST_MODELS*/
1489
  { "showphoto", cmdSHOWPHOTO, 0, N_("show selected photo IDs")},
1490
  { "clean", cmdCLEAN, 0,
1491
    N_("compact unusable user IDs and remove unusable signatures from key")},
1492
  { "minimize", cmdMINIMIZE, 0,
1493
    N_("compact unusable user IDs and remove all signatures from key")},
1494
1495
  { NULL, cmdNONE, 0, NULL}
1496
};
1497
1498
1499

1500
#ifdef HAVE_LIBREADLINE
1501
1502
/*
1503
   These two functions are used by readline for command completion.
1504
 */
1505
1506
static char *
1507
command_generator (const char *text, int state)
1508
{
1509
  static int list_index, len;
1510
  const char *name;
1511
1512
  /* If this is a new word to complete, initialize now.  This includes
1513
     saving the length of TEXT for efficiency, and initializing the
1514
     index variable to 0. */
1515
  if (!state)
1516
    {
1517
      list_index = 0;
1518
      len = strlen (text);
1519
    }
1520
1521
  /* Return the next partial match */
1522
  while ((name = cmds[list_index].name))
1523
    {
1524
      /* Only complete commands that have help text */
1525
      if (cmds[list_index++].desc && strncmp (name, text, len) == 0)
1526
  return strdup (name);
1527
    }
1528
1529
  return NULL;
1530
}
1531
1532
static char **
1533
keyedit_completion (const char *text, int start, int end)
1534
{
1535
  /* If we are at the start of a line, we try and command-complete.
1536
     If not, just do nothing for now. */
1537
1538
  (void) end;
1539
1540
  if (start == 0)
1541
    return rl_completion_matches (text, command_generator);
1542
1543
  rl_attempted_completion_over = 1;
1544
1545
  return NULL;
1546
}
1547
#endif /* HAVE_LIBREADLINE */
1548
1549
1550

1551
/* Main function of the menu driven key editor.  */
1552
void
1553
keyedit_menu (ctrl_t ctrl, const char *username, strlist_t locusr,
1554
        strlist_t commands, int quiet, int seckey_check)
1555
0
{
1556
0
  enum cmdids cmd = 0;
1557
0
  gpg_error_t err = 0;
1558
0
  KBNODE keyblock = NULL;
1559
0
  KEYDB_HANDLE kdbhd = NULL;
1560
0
  int have_seckey = 0;
1561
0
  int have_anyseckey = 0;
1562
0
  char *answer = NULL;
1563
0
  int redisplay = 1;
1564
0
  int modified = 0;
1565
0
  int upload = 0;  /* Set if the key maybe be uploaded.  */
1566
0
  int sec_shadowing = 0;
1567
0
  int run_subkey_warnings = 0;
1568
0
  int have_commands = !!commands;
1569
0
  strlist_t delseckey_list = NULL;
1570
0
  int delseckey_list_warn = 0;
1571
1572
0
  if (opt.command_fd != -1)
1573
0
    ;
1574
0
  else if (opt.batch && !have_commands)
1575
0
    {
1576
0
      log_error (_("can't do this in batch mode\n"));
1577
0
      goto leave;
1578
0
    }
1579
1580
#ifdef HAVE_W32_SYSTEM
1581
  /* Due to Windows peculiarities we need to make sure that the
1582
     trustdb stale check is done before we open another file
1583
     (i.e. by searching for a key).  In theory we could make sure
1584
     that the files are closed after use but the open/close caches
1585
     inhibits that and flushing the cache right before the stale
1586
     check is not easy to implement.  Thus we take the easy way out
1587
     and run the stale check as early as possible.  Note, that for
1588
     non- W32 platforms it is run indirectly through a call to
1589
     get_validity ().  */
1590
  check_trustdb_stale (ctrl);
1591
#endif
1592
1593
  /* Get the public key */
1594
0
  err = get_pubkey_byname (ctrl, GET_PUBKEY_NO_AKL,
1595
0
                           NULL, NULL, username, &keyblock, &kdbhd, 1);
1596
0
  if (err)
1597
0
    {
1598
0
      log_error (_("key \"%s\" not found: %s\n"), username, gpg_strerror (err));
1599
0
      goto leave;
1600
0
    }
1601
1602
0
  if (fix_keyblock (ctrl, &keyblock))
1603
0
    modified++;
1604
1605
  /* See whether we have a matching secret key.  */
1606
0
  if (seckey_check)
1607
0
    {
1608
0
      have_anyseckey = !agent_probe_any_secret_key (ctrl, keyblock);
1609
0
      if (have_anyseckey
1610
0
          && agent_probe_secret_key (ctrl, keyblock->pkt->pkt.public_key))
1611
0
        {
1612
          /* The primary key is also available.   */
1613
0
          have_seckey = 1;
1614
0
        }
1615
1616
0
      if (have_seckey && !quiet)
1617
0
        tty_printf (_("Secret key is available.\n"));
1618
0
      else if (have_anyseckey && !quiet)
1619
0
        tty_printf (_("Secret subkeys are available.\n"));
1620
0
    }
1621
1622
  /* Main command loop.  */
1623
0
  for (;;)
1624
0
    {
1625
0
      int i, arg_number, photo;
1626
0
      const char *arg_string = "";
1627
0
      char *p;
1628
0
      PKT_public_key *pk = keyblock->pkt->pkt.public_key;
1629
1630
0
      tty_printf ("\n");
1631
1632
0
      if (redisplay && !quiet)
1633
0
  {
1634
          /* Show using flags: with_revoker, with_subkeys.  */
1635
0
    show_key_with_all_names (ctrl, NULL, keyblock, 0, 1, 0, 1, 0, 0);
1636
0
    tty_printf ("\n");
1637
0
    redisplay = 0;
1638
0
  }
1639
1640
0
      if (run_subkey_warnings)
1641
0
        {
1642
0
          run_subkey_warnings = 0;
1643
0
          if (!count_selected_keys (keyblock))
1644
0
            subkey_expire_warning (keyblock);
1645
0
          no_usable_encr_subkeys_warning (keyblock);
1646
0
        }
1647
1648
0
      if (delseckey_list_warn)
1649
0
        {
1650
0
          delseckey_list_warn = 0;
1651
0
          tty_printf
1652
0
            (_("Note: the local copy of the secret key"
1653
0
               " will only be deleted with \"save\".\n"));
1654
0
        }
1655
1656
0
      do
1657
0
  {
1658
0
    xfree (answer);
1659
0
    if (have_commands)
1660
0
      {
1661
0
        if (commands)
1662
0
    {
1663
0
      answer = xstrdup (commands->d);
1664
0
      commands = commands->next;
1665
0
    }
1666
0
        else if (opt.batch)
1667
0
    {
1668
0
      answer = xstrdup ("quit");
1669
0
    }
1670
0
        else
1671
0
    have_commands = 0;
1672
0
      }
1673
0
    if (!have_commands)
1674
0
      {
1675
#ifdef HAVE_LIBREADLINE
1676
        tty_enable_completion (keyedit_completion);
1677
#endif
1678
0
        answer = cpr_get_no_help ("keyedit.prompt", GPG_NAME "> ");
1679
0
        cpr_kill_prompt ();
1680
0
        tty_disable_completion ();
1681
0
      }
1682
0
    trim_spaces (answer);
1683
0
  }
1684
0
      while (*answer == '#');
1685
1686
0
      arg_number = 0; /* Here is the init which egcc complains about.  */
1687
0
      photo = 0;      /* Same here. */
1688
0
      if (!*answer)
1689
0
  cmd = cmdLIST;
1690
0
      else if (*answer == CONTROL_D)
1691
0
  cmd = cmdQUIT;
1692
0
      else if (digitp (answer))
1693
0
  {
1694
0
    cmd = cmdSELUID;
1695
0
    arg_number = atoi (answer);
1696
0
  }
1697
0
      else
1698
0
  {
1699
0
    if ((p = strchr (answer, ' ')))
1700
0
      {
1701
0
        *p++ = 0;
1702
0
        trim_spaces (answer);
1703
0
        trim_spaces (p);
1704
0
        arg_number = atoi (p);
1705
0
        arg_string = p;
1706
0
      }
1707
1708
0
    for (i = 0; cmds[i].name; i++)
1709
0
      {
1710
0
        if (cmds[i].flags & KEYEDIT_TAIL_MATCH)
1711
0
    {
1712
0
      size_t l = strlen (cmds[i].name);
1713
0
      size_t a = strlen (answer);
1714
0
      if (a >= l)
1715
0
        {
1716
0
          if (!ascii_strcasecmp (&answer[a - l], cmds[i].name))
1717
0
      {
1718
0
        answer[a - l] = '\0';
1719
0
        break;
1720
0
      }
1721
0
        }
1722
0
    }
1723
0
        else if (!ascii_strcasecmp (answer, cmds[i].name))
1724
0
    break;
1725
0
      }
1726
0
    if ((cmds[i].flags & (KEYEDIT_NEED_SK|KEYEDIT_NEED_SUBSK))
1727
0
              && !(((cmds[i].flags & KEYEDIT_NEED_SK) && have_seckey)
1728
0
                   || ((cmds[i].flags & KEYEDIT_NEED_SUBSK) && have_anyseckey)))
1729
0
      {
1730
0
        tty_printf (_("Need the secret key to do this.\n"));
1731
0
        cmd = cmdNOP;
1732
0
      }
1733
0
          else
1734
0
      cmd = cmds[i].id;
1735
0
  }
1736
1737
      /* Dispatch the command.  */
1738
0
      switch (cmd)
1739
0
  {
1740
0
  case cmdHELP:
1741
0
    for (i = 0; cmds[i].name; i++)
1742
0
      {
1743
0
              if ((cmds[i].flags & (KEYEDIT_NEED_SK|KEYEDIT_NEED_SUBSK))
1744
0
                  && !(((cmds[i].flags & KEYEDIT_NEED_SK) && have_seckey)
1745
0
                       ||((cmds[i].flags&KEYEDIT_NEED_SUBSK)&&have_anyseckey)))
1746
0
    ; /* Skip those item if we do not have the secret key.  */
1747
0
        else if (cmds[i].desc)
1748
0
    tty_printf ("%-11s %s\n", cmds[i].name, _(cmds[i].desc));
1749
0
      }
1750
1751
0
    tty_printf ("\n");
1752
0
    tty_printf
1753
0
            (_("* The 'sign' command may be prefixed with an 'l' for local "
1754
0
               "signatures (lsign),\n"
1755
0
               "  a 't' for trust signatures (tsign), an 'nr' for "
1756
0
               "non-revocable signatures\n"
1757
0
               "  (nrsign), or any combination thereof (ltsign, "
1758
0
               "tnrsign, etc.).\n"));
1759
0
    break;
1760
1761
0
  case cmdLIST:
1762
0
    redisplay = 1;
1763
0
    break;
1764
1765
0
  case cmdFPR:
1766
0
    show_key_and_fingerprint
1767
0
            (ctrl,
1768
0
             keyblock, (*arg_string == '*'
1769
0
                        && (!arg_string[1] || spacep (arg_string + 1))));
1770
0
    break;
1771
1772
0
  case cmdGRIP:
1773
0
    show_key_and_grip (keyblock);
1774
0
    break;
1775
1776
0
  case cmdSELUID:
1777
0
    if (strlen (arg_string) == NAMEHASH_LEN * 2)
1778
0
      redisplay = menu_select_uid_namehash (keyblock, arg_string);
1779
0
    else
1780
0
      {
1781
0
        if (*arg_string == '*'
1782
0
      && (!arg_string[1] || spacep (arg_string + 1)))
1783
0
    arg_number = -1; /* Select all. */
1784
0
        redisplay = menu_select_uid (keyblock, arg_number);
1785
0
      }
1786
0
    break;
1787
1788
0
  case cmdSELKEY:
1789
0
    {
1790
0
      if (*arg_string == '*'
1791
0
    && (!arg_string[1] || spacep (arg_string + 1)))
1792
0
        arg_number = -1; /* Select all. */
1793
0
      if (menu_select_key (keyblock, arg_number, p))
1794
0
        redisplay = 1;
1795
0
    }
1796
0
    break;
1797
1798
0
  case cmdCHECK:
1799
0
    if (key_check_all_keysigs (ctrl, -1, keyblock,
1800
0
             count_selected_uids (keyblock),
1801
0
             !strcmp (arg_string, "selfsig")))
1802
0
            modified = 1;
1803
0
    break;
1804
1805
0
  case cmdSIGN:
1806
0
    {
1807
0
            unsigned int myflags = 0;
1808
0
            int my_modified = 0;
1809
1810
0
      if (pk->flags.revoked)
1811
0
        {
1812
0
    tty_printf (_("Key is revoked."));
1813
1814
0
    if (opt.expert)
1815
0
      {
1816
0
        tty_printf ("  ");
1817
0
        if (!cpr_get_answer_is_yes
1818
0
                        ("keyedit.sign_revoked.okay",
1819
0
                         _("Are you sure you still want to sign it? (y/N) ")))
1820
0
          break;
1821
0
      }
1822
0
    else
1823
0
      {
1824
0
        tty_printf (_("  Unable to sign.\n"));
1825
0
        break;
1826
0
      }
1827
0
        }
1828
1829
0
      if (count_uids (keyblock) > 1 && !count_selected_uids (keyblock))
1830
0
              {
1831
0
                int result;
1832
0
                if (opt.only_sign_text_ids)
1833
0
                  result = cpr_get_answer_is_yes
1834
0
                    ("keyedit.sign_all.okay",
1835
0
                     _("Really sign all text user IDs? (y/N) "));
1836
0
                else
1837
0
                  result = cpr_get_answer_is_yes
1838
0
                    ("keyedit.sign_all.okay",
1839
0
                     _("Really sign all user IDs? (y/N) "));
1840
1841
0
                if (! result)
1842
0
                  {
1843
0
                    if (opt.interactive)
1844
0
                      myflags |= SIGN_UIDS_INTERACTIVE;
1845
0
                    else
1846
0
                      {
1847
0
                        tty_printf (_("Hint: Select the user IDs to sign\n"));
1848
0
                        have_commands = 0;
1849
0
                        break;
1850
0
                      }
1851
1852
0
                  }
1853
0
              }
1854
1855
      /* What sort of signing are we doing? */
1856
0
      if (!parse_sign_type (answer, &myflags))
1857
0
        {
1858
0
    tty_printf (_("Unknown signature type '%s'\n"), answer);
1859
0
    break;
1860
0
        }
1861
1862
0
      sign_uids (ctrl, NULL, keyblock, locusr, myflags,
1863
0
                       NULL, &my_modified);
1864
0
            if (my_modified)  /* sign_uids modified the keyblock      */
1865
0
              modified = 1;   /* thus set the general modified flag.  */
1866
0
            if (my_modified && !(myflags & SIGN_UIDS_LOCAL))
1867
0
              upload = 1;     /* exportable signature -> mark uploadable.  */
1868
0
    }
1869
0
    break;
1870
1871
0
  case cmdDEBUG:
1872
0
    dump_kbnode (keyblock);
1873
0
    break;
1874
1875
0
  case cmdTOGGLE:
1876
          /* The toggle command is a leftover from old gpg versions
1877
             where we worked with a secret and a public keyring.  It
1878
             is not necessary anymore but we keep this command for the
1879
             sake of scripts using it.  */
1880
0
    redisplay = 1;
1881
0
    break;
1882
1883
0
  case cmdADDPHOTO:
1884
0
    if (RFC2440)
1885
0
      {
1886
0
        tty_printf (_("This command is not allowed while in %s mode.\n"),
1887
0
        gnupg_compliance_option_string (opt.compliance));
1888
0
        break;
1889
0
      }
1890
0
    photo = 1;
1891
    /* fall through */
1892
0
  case cmdADDUID:
1893
0
    if (menu_adduid (ctrl, keyblock, photo, arg_string, NULL))
1894
0
      {
1895
0
        update_trust = 1;
1896
0
        redisplay = 1;
1897
0
        modified = 1;
1898
0
              upload = 1;
1899
0
        merge_keys_and_selfsig (ctrl, keyblock);
1900
0
      }
1901
0
    break;
1902
1903
0
  case cmdDELUID:
1904
0
    {
1905
0
      int n1;
1906
1907
0
      if (!(n1 = count_selected_uids (keyblock)))
1908
0
              {
1909
0
                tty_printf (_("You must select at least one user ID.\n"));
1910
0
                if (!opt.expert)
1911
0
                  tty_printf (_("(Use the '%s' command.)\n"), "uid");
1912
0
              }
1913
0
      else if (real_uids_left (keyblock) < 1)
1914
0
        tty_printf (_("You can't delete the last user ID!\n"));
1915
0
      else if (cpr_get_answer_is_yes
1916
0
                     ("keyedit.remove.uid.okay",
1917
0
                      n1 > 1 ? _("Really remove all selected user IDs? (y/N) ")
1918
0
                      :        _("Really remove this user ID? (y/N) ")))
1919
0
        {
1920
0
    menu_deluid (keyblock);
1921
0
    redisplay = 1;
1922
0
    modified = 1;
1923
                /* upload does not make sense here.  Eventually we may
1924
                 * decide to delete a key from the keyserver.*/
1925
0
        }
1926
0
    }
1927
0
    break;
1928
1929
0
  case cmdDELSIG:
1930
0
    {
1931
0
      int n1;
1932
1933
0
      if (!(n1 = count_selected_uids (keyblock)))
1934
0
              {
1935
0
                tty_printf (_("You must select at least one user ID.\n"));
1936
0
                if (!opt.expert)
1937
0
                  tty_printf (_("(Use the '%s' command.)\n"), "uid");
1938
0
              }
1939
0
      else if (menu_delsig (ctrl, keyblock))
1940
0
        {
1941
    /* No redisplay here, because it may scroll away some
1942
     * of the status output of this command.  */
1943
0
    modified = 1;
1944
0
        }
1945
0
    }
1946
0
    break;
1947
1948
0
  case cmdADDKEY:
1949
0
    if (!generate_subkeypair (ctrl, keyblock, NULL, NULL, NULL))
1950
0
      {
1951
0
        redisplay = 1;
1952
0
        modified = 1;
1953
0
              upload = 1;
1954
0
        merge_keys_and_selfsig (ctrl, keyblock);
1955
0
      }
1956
0
    break;
1957
1958
0
  case cmdADDPUBKEY:
1959
0
    {
1960
      /* Ask for a filename, check whether this is really a
1961
       * subkey key, parse that key, and add it as subkey to the
1962
       * current keyblock. */
1963
0
      kbnode_t node;
1964
0
      char *fname;
1965
0
      PACKET *pkt;
1966
0
      iobuf_t a;
1967
0
            struct parse_packet_ctx_s parsectx;
1968
1969
0
            if (!*arg_string)
1970
0
        {
1971
0
    tty_printf (_("Command expects a filename argument\n"));
1972
0
    break;
1973
0
        }
1974
1975
0
            if (*arg_string == DIRSEP_C)
1976
0
              fname = xstrdup (arg_string);
1977
0
            else if (*arg_string == '~')
1978
0
              fname = make_filename (arg_string, NULL);
1979
0
            else
1980
0
              fname = make_filename (gnupg_homedir (), arg_string, NULL);
1981
1982
      /* Open that file.  */
1983
0
      a = iobuf_open (fname);
1984
0
      if (a && is_secured_file (iobuf_get_fd (a)))
1985
0
        {
1986
0
    iobuf_close (a);
1987
0
    a = NULL;
1988
0
    gpg_err_set_errno (EPERM);
1989
0
        }
1990
0
            if (!a)
1991
0
              {
1992
0
                tty_printf (_("Can't open '%s': %s\n"),
1993
0
                            fname, strerror (errno));
1994
0
                xfree (fname);
1995
0
                break;
1996
0
              }
1997
1998
      /* Parse and check that file.  */
1999
0
      pkt = xmalloc (sizeof *pkt);
2000
0
      init_packet (pkt);
2001
0
            init_parse_packet (&parsectx, a);
2002
0
      err = parse_packet (&parsectx, pkt);
2003
0
      deinit_parse_packet (&parsectx);
2004
0
            iobuf_close (a);
2005
0
      iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char *) fname);
2006
0
      if (!err && pkt->pkttype != PKT_PUBLIC_KEY
2007
0
    && pkt->pkttype != PKT_PUBLIC_SUBKEY)
2008
0
        err = GPG_ERR_NO_PUBKEY;
2009
0
            if (err)
2010
0
              {
2011
0
                tty_printf (_("Error reading key from '%s': %s\n"),
2012
0
                            fname, gpg_strerror (err));
2013
0
                xfree (fname);
2014
0
                free_packet (pkt, NULL);
2015
0
                xfree (pkt);
2016
0
                break;
2017
0
              }
2018
2019
0
      xfree (fname);
2020
0
      node = new_kbnode (pkt);
2021
2022
            /* Always treat the packet as a public subkey and append.  */
2023
0
            pkt->pkttype = PKT_PUBLIC_SUBKEY;
2024
0
            err = append_subkey_to_keyblock
2025
0
              (ctrl, keyblock, pkt->pkt.public_key,
2026
0
               ask_key_flags (pkt->pkt.public_key->pubkey_algo, 1, 0));
2027
0
            if (!err || gpg_err_code (err) == GPG_ERR_EEXIST)
2028
0
              {
2029
0
                err = 0;
2030
0
                merge_keys_and_selfsig (ctrl, keyblock);
2031
0
                modified = 1;
2032
0
                redisplay = 1;
2033
0
              }
2034
0
            release_kbnode (node);
2035
0
          }
2036
0
    break;
2037
2038
#ifdef ENABLE_CARD_SUPPORT
2039
  case cmdADDCARDKEY:
2040
    if (!card_generate_subkey (ctrl, keyblock))
2041
      {
2042
        redisplay = 1;
2043
        modified = 1;
2044
              upload = 1;
2045
        merge_keys_and_selfsig (ctrl, keyblock);
2046
      }
2047
    break;
2048
2049
  case cmdKEYTOTPM:
2050
    /* FIXME need to store the key and not commit until later */
2051
    {
2052
      kbnode_t node = NULL;
2053
      switch (count_selected_keys (keyblock))
2054
        {
2055
        case 0:
2056
    if (cpr_get_answer_is_yes
2057
                    ("keyedit.keytocard.use_primary",
2058
                     /* TRANSLATORS: Please take care: This is about
2059
                        moving the key and not about removing it.  */
2060
                     _("Really move the primary key? (y/N) ")))
2061
      node = keyblock;
2062
    break;
2063
        case 1:
2064
    for (node = keyblock; node; node = node->next)
2065
      {
2066
        if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2067
      && node->flag & NODFLG_SELKEY)
2068
          break;
2069
      }
2070
    break;
2071
        default:
2072
    tty_printf (_("You must select exactly one key.\n"));
2073
    break;
2074
        }
2075
      if (node)
2076
        {
2077
    PKT_public_key *xxpk = node->pkt->pkt.public_key;
2078
    char *hexgrip;
2079
2080
    hexkeygrip_from_pk (xxpk, &hexgrip);
2081
    if (!agent_keytotpm (ctrl, hexgrip))
2082
      {
2083
        redisplay = 1;
2084
      }
2085
    xfree (hexgrip);
2086
        }
2087
    }
2088
    break;
2089
2090
  case cmdKEYTOCARD:
2091
    {
2092
      KBNODE node = NULL;
2093
      switch (count_selected_keys (keyblock))
2094
        {
2095
        case 0:
2096
    if (cpr_get_answer_is_yes
2097
                    ("keyedit.keytocard.use_primary",
2098
                     /* TRANSLATORS: Please take care: This is about
2099
                        moving the key and not about removing it.  */
2100
                     _("Really move the primary key? (y/N) ")))
2101
      node = keyblock;
2102
    break;
2103
        case 1:
2104
    for (node = keyblock; node; node = node->next)
2105
      {
2106
        if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2107
      && node->flag & NODFLG_SELKEY)
2108
          break;
2109
      }
2110
    break;
2111
        default:
2112
    tty_printf (_("You must select exactly one key.\n"));
2113
    break;
2114
        }
2115
      if (node)
2116
        {
2117
    PKT_public_key *xxpk = node->pkt->pkt.public_key;
2118
    if (card_store_subkey (node, xxpk ? xxpk->pubkey_usage : 0,
2119
                                       &delseckey_list, NULL))
2120
      {
2121
        redisplay = 1;
2122
        sec_shadowing = 1;
2123
                    delseckey_list_warn = 1;
2124
      }
2125
        }
2126
    }
2127
    break;
2128
2129
  case cmdBKUPTOCARD:
2130
    {
2131
      /* Ask for a filename, check whether this is really a
2132
         backup key as generated by the card generation, parse
2133
         that key and store it on card. */
2134
      kbnode_t node;
2135
      char *fname;
2136
      PACKET *pkt;
2137
      IOBUF a;
2138
            struct parse_packet_ctx_s parsectx;
2139
            int lastmode, used_use;
2140
2141
            if (!*arg_string)
2142
        {
2143
    tty_printf (_("Command expects a filename argument\n"));
2144
    break;
2145
        }
2146
2147
            if (*arg_string == DIRSEP_C)
2148
              fname = xstrdup (arg_string);
2149
            else if (*arg_string == '~')
2150
              fname = make_filename (arg_string, NULL);
2151
            else
2152
              fname = make_filename (gnupg_homedir (), arg_string, NULL);
2153
2154
      /* Open that file.  */
2155
      a = iobuf_open (fname);
2156
      if (a && is_secured_file (iobuf_get_fd (a)))
2157
        {
2158
    iobuf_close (a);
2159
    a = NULL;
2160
    gpg_err_set_errno (EPERM);
2161
        }
2162
            if (!a)
2163
              {
2164
                tty_printf (_("Can't open '%s': %s\n"),
2165
                            fname, strerror (errno));
2166
                xfree (fname);
2167
                break;
2168
              }
2169
2170
      /* Parse and check that file.  */
2171
      pkt = xmalloc (sizeof *pkt);
2172
      init_packet (pkt);
2173
            init_parse_packet (&parsectx, a);
2174
      err = parse_packet (&parsectx, pkt);
2175
      deinit_parse_packet (&parsectx);
2176
            iobuf_close (a);
2177
      iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char *) fname);
2178
      if (!err && pkt->pkttype != PKT_SECRET_KEY
2179
    && pkt->pkttype != PKT_SECRET_SUBKEY)
2180
        err = GPG_ERR_NO_SECKEY;
2181
            if (err)
2182
              {
2183
                tty_printf (_("Error reading backup key from '%s': %s\n"),
2184
                            fname, gpg_strerror (err));
2185
                xfree (fname);
2186
                free_packet (pkt, NULL);
2187
                xfree (pkt);
2188
                break;
2189
              }
2190
2191
      xfree (fname);
2192
      node = new_kbnode (pkt);
2193
2194
            err = agent_set_ephemeral_mode (ctrl, 1, &lastmode);
2195
            if (err)
2196
              log_error ("error switching to ephemeral mode: %s\n",
2197
                         gpg_strerror (err));
2198
            else
2199
              {
2200
                /* Transfer it to gpg-agent which handles secret keys.  */
2201
                err = transfer_secret_keys (ctrl, NULL, node, 1, 1, 0);
2202
                if (!err)
2203
                  {
2204
                    /* Treat the pkt as a public key.  */
2205
                    pkt->pkttype = PKT_PUBLIC_KEY;
2206
2207
                    /* Ask gpg-agent to store the secret key to card.  */
2208
                    if (card_store_subkey (node, 0, NULL, &used_use))
2209
                      {
2210
                        /* Success: the key is now on the card.  */
2211
                        redisplay = 1;
2212
                        sec_shadowing = 1;
2213
                        /* We now have to add the public key to the
2214
                         * keyblock.  This will always be a subkey.  */
2215
                        err = append_subkey_to_keyblock (ctrl, keyblock,
2216
                                                         pkt->pkt.public_key,
2217
                                                         used_use);
2218
                        if (!err || gpg_err_code (err) == GPG_ERR_EEXIST)
2219
                          {
2220
                            err = 0;
2221
                            merge_keys_and_selfsig (ctrl, keyblock);
2222
                            modified = 1;
2223
                          }
2224
                      }
2225
                  }
2226
                if (!lastmode && agent_set_ephemeral_mode (ctrl, 0, NULL))
2227
                  log_error ("error clearing the ephemeral mode\n");
2228
              }
2229
            release_kbnode (node);
2230
          }
2231
          break;
2232
2233
#endif /* ENABLE_CARD_SUPPORT */
2234
2235
0
  case cmdDELKEY:
2236
0
    {
2237
0
      int n1;
2238
2239
0
      if (!(n1 = count_selected_keys (keyblock)))
2240
0
              {
2241
0
                tty_printf (_("You must select at least one key.\n"));
2242
0
                if (!opt.expert)
2243
0
                  tty_printf (_("(Use the '%s' command.)\n"), "key");
2244
0
              }
2245
0
      else if (!cpr_get_answer_is_yes
2246
0
                     ("keyedit.remove.subkey.okay",
2247
0
                      n1 > 1 ? _("Do you really want to delete the "
2248
0
                                 "selected keys? (y/N) ")
2249
0
                      :  _("Do you really want to delete this key? (y/N) ")))
2250
0
        ;
2251
0
      else
2252
0
        {
2253
0
    menu_delkey (keyblock);
2254
0
    redisplay = 1;
2255
0
    modified = 1;
2256
                /* upload does not make sense. */
2257
0
        }
2258
0
    }
2259
0
    break;
2260
2261
0
  case cmdADDREVOKER:
2262
0
    {
2263
0
      int sensitive = 0;
2264
2265
0
      if (ascii_strcasecmp (arg_string, "sensitive") == 0)
2266
0
        sensitive = 1;
2267
0
      if (menu_addrevoker (ctrl, keyblock, sensitive))
2268
0
        {
2269
0
    redisplay = 1;
2270
0
    modified = 1;
2271
0
                upload = 1;
2272
0
    merge_keys_and_selfsig (ctrl, keyblock);
2273
0
        }
2274
0
    }
2275
0
    break;
2276
2277
0
  case cmdADDADSK:
2278
0
          if (menu_addadsk (ctrl, keyblock, NULL))
2279
0
            {
2280
0
              redisplay = 1;
2281
0
              modified = 1;
2282
0
              upload = 1;
2283
0
              merge_keys_and_selfsig (ctrl, keyblock);
2284
0
            }
2285
0
    break;
2286
2287
0
  case cmdREVUID:
2288
0
    {
2289
0
      int n1;
2290
2291
0
      if (!(n1 = count_selected_uids (keyblock)))
2292
0
              {
2293
0
                tty_printf (_("You must select at least one user ID.\n"));
2294
0
                if (!opt.expert)
2295
0
                  tty_printf (_("(Use the '%s' command.)\n"), "uid");
2296
0
              }
2297
0
      else if (cpr_get_answer_is_yes
2298
0
                     ("keyedit.revoke.uid.okay",
2299
0
                      n1 > 1 ? _("Really revoke all selected user IDs? (y/N) ")
2300
0
          :        _("Really revoke this user ID? (y/N) ")))
2301
0
        {
2302
0
    if (menu_revuid (ctrl, keyblock))
2303
0
      {
2304
0
        modified = 1;
2305
0
        redisplay = 1;
2306
0
                    upload = 1;
2307
0
      }
2308
0
        }
2309
0
    }
2310
0
    break;
2311
2312
0
  case cmdREVKEY:
2313
0
    {
2314
0
      int n1;
2315
2316
0
      if (!(n1 = count_selected_keys (keyblock)))
2317
0
        {
2318
0
    if (cpr_get_answer_is_yes ("keyedit.revoke.subkey.okay",
2319
0
             _("Do you really want to revoke"
2320
0
               " the entire key? (y/N) ")))
2321
0
      {
2322
0
        if (menu_revkey (ctrl, keyblock))
2323
0
                      {
2324
0
                        modified = 1;
2325
0
                        upload = 1;
2326
0
                      }
2327
2328
0
        redisplay = 1;
2329
0
      }
2330
0
        }
2331
0
      else if (cpr_get_answer_is_yes ("keyedit.revoke.subkey.okay",
2332
0
              n1 > 1 ?
2333
0
              _("Do you really want to revoke"
2334
0
                " the selected subkeys? (y/N) ")
2335
0
              : _("Do you really want to revoke"
2336
0
            " this subkey? (y/N) ")))
2337
0
        {
2338
0
    if (menu_revsubkey (ctrl, keyblock))
2339
0
                  {
2340
0
                    modified = 1;
2341
0
                    upload = 1;
2342
0
                  }
2343
2344
0
    redisplay = 1;
2345
0
        }
2346
2347
0
      if (modified)
2348
0
        merge_keys_and_selfsig (ctrl, keyblock);
2349
0
    }
2350
0
    break;
2351
2352
0
  case cmdEXPIRE:
2353
0
    if (gpg_err_code (menu_expire (ctrl, keyblock, 0, 0)) == GPG_ERR_TRUE)
2354
0
      {
2355
0
        merge_keys_and_selfsig (ctrl, keyblock);
2356
0
              run_subkey_warnings = 1;
2357
0
        modified = 1;
2358
0
              upload = 1;
2359
0
        redisplay = 1;
2360
0
      }
2361
0
    break;
2362
2363
0
  case cmdCHANGEUSAGE:
2364
0
    if (menu_changeusage (ctrl, keyblock))
2365
0
      {
2366
0
        merge_keys_and_selfsig (ctrl, keyblock);
2367
0
        modified = 1;
2368
0
              upload = 1;
2369
0
        redisplay = 1;
2370
0
      }
2371
0
    break;
2372
2373
0
  case cmdBACKSIGN:
2374
0
    if (menu_backsign (ctrl, keyblock))
2375
0
      {
2376
0
        modified = 1;
2377
0
              upload = 1;
2378
0
        redisplay = 1;
2379
0
      }
2380
0
    break;
2381
2382
0
  case cmdPRIMARY:
2383
0
    if (menu_set_primary_uid (ctrl, keyblock))
2384
0
      {
2385
0
        merge_keys_and_selfsig (ctrl, keyblock);
2386
0
        modified = 1;
2387
0
              upload = 1;
2388
0
        redisplay = 1;
2389
0
      }
2390
0
    break;
2391
2392
0
  case cmdPASSWD:
2393
0
    change_passphrase (ctrl, keyblock);
2394
0
    break;
2395
2396
0
#ifndef NO_TRUST_MODELS
2397
0
  case cmdTRUST:
2398
0
    if (opt.trust_model == TM_EXTERNAL)
2399
0
      {
2400
0
        tty_printf (_("Owner trust may not be set while "
2401
0
          "using a user provided trust database\n"));
2402
0
        break;
2403
0
      }
2404
2405
0
    show_key_with_all_names (ctrl, NULL, keyblock, 0, 0, 0, 1, 0, 0);
2406
0
    tty_printf ("\n");
2407
0
    if (edit_ownertrust (ctrl, find_kbnode (keyblock,
2408
0
              PKT_PUBLIC_KEY)->pkt->pkt.
2409
0
             public_key, 1))
2410
0
      {
2411
0
        redisplay = 1;
2412
        /* No real need to set update_trust here as
2413
           edit_ownertrust() calls revalidation_mark()
2414
           anyway. */
2415
0
        update_trust = 1;
2416
0
      }
2417
0
    break;
2418
0
#endif /*!NO_TRUST_MODELS*/
2419
2420
0
  case cmdPREF:
2421
0
    {
2422
0
      int count = count_selected_uids (keyblock);
2423
0
      log_assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
2424
0
      show_names (ctrl, NULL, keyblock, keyblock->pkt->pkt.public_key,
2425
0
      count ? NODFLG_SELUID : 0, 1);
2426
0
    }
2427
0
    break;
2428
2429
0
  case cmdSHOWPREF:
2430
0
    {
2431
0
      int count = count_selected_uids (keyblock);
2432
0
      log_assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
2433
0
      show_names (ctrl, NULL, keyblock, keyblock->pkt->pkt.public_key,
2434
0
      count ? NODFLG_SELUID : 0, 2);
2435
0
    }
2436
0
    break;
2437
2438
0
  case cmdSETPREF:
2439
0
    {
2440
0
      PKT_user_id *tempuid;
2441
2442
0
      keygen_set_std_prefs (!*arg_string ? "default" : arg_string, 0);
2443
2444
0
      tempuid = keygen_get_std_prefs ();
2445
0
      tty_printf (_("Set preference list to:\n"));
2446
0
      show_prefs (tempuid, NULL, 1);
2447
0
      free_user_id (tempuid);
2448
2449
0
      if (cpr_get_answer_is_yes
2450
0
                ("keyedit.setpref.okay",
2451
0
                 count_selected_uids (keyblock) ?
2452
0
                 _("Really update the preferences"
2453
0
                   " for the selected user IDs? (y/N) ")
2454
0
                 : _("Really update the preferences? (y/N) ")))
2455
0
        {
2456
0
    if (menu_set_preferences (ctrl, keyblock, 0))
2457
0
      {
2458
0
        merge_keys_and_selfsig (ctrl, keyblock);
2459
0
        modified = 1;
2460
0
                    upload = 1;
2461
0
        redisplay = 1;
2462
0
      }
2463
0
        }
2464
0
    }
2465
0
    break;
2466
2467
0
  case cmdPREFKS:
2468
0
    if (menu_set_keyserver_url (ctrl, *arg_string ? arg_string : NULL,
2469
0
              keyblock))
2470
0
      {
2471
0
        merge_keys_and_selfsig (ctrl, keyblock);
2472
0
        modified = 1;
2473
0
              upload = 1;
2474
0
        redisplay = 1;
2475
0
      }
2476
0
    break;
2477
2478
0
  case cmdNOTATION:
2479
0
    if (menu_set_notation (ctrl, *arg_string ? arg_string : NULL,
2480
0
         keyblock))
2481
0
      {
2482
0
        merge_keys_and_selfsig (ctrl, keyblock);
2483
0
        modified = 1;
2484
0
              upload = 1;
2485
0
        redisplay = 1;
2486
0
      }
2487
0
    break;
2488
2489
0
  case cmdNOP:
2490
0
    break;
2491
2492
0
  case cmdREVSIG:
2493
0
    if (menu_revsig (ctrl, keyblock))
2494
0
      {
2495
0
        redisplay = 1;
2496
0
        modified = 1;
2497
0
              upload = 1;
2498
0
      }
2499
0
    break;
2500
2501
0
#ifndef NO_TRUST_MODELS
2502
0
  case cmdENABLEKEY:
2503
0
  case cmdDISABLEKEY:
2504
0
    if (enable_disable_key (ctrl, keyblock, cmd == cmdDISABLEKEY))
2505
0
      {
2506
0
        redisplay = 1;
2507
0
        modified = 1;
2508
0
      }
2509
0
    break;
2510
0
#endif /*!NO_TRUST_MODELS*/
2511
2512
0
  case cmdSHOWPHOTO:
2513
0
    menu_showphoto (ctrl, keyblock);
2514
0
    break;
2515
2516
0
  case cmdCLEAN:
2517
0
    if (menu_clean (ctrl, keyblock, 0))
2518
0
      redisplay = modified = 1;
2519
0
    break;
2520
2521
0
  case cmdMINIMIZE:
2522
0
    if (menu_clean (ctrl, keyblock, EXPORT_MINIMAL))
2523
0
      redisplay = modified = 1;
2524
0
    break;
2525
2526
0
  case cmdQUIT:
2527
0
    if (have_commands)
2528
0
      goto leave;
2529
0
    if (!modified && !sec_shadowing)
2530
0
      goto leave;
2531
0
    if (!cpr_get_answer_is_yes ("keyedit.save.okay",
2532
0
              _("Save changes? (y/N) ")))
2533
0
      {
2534
0
        if (cpr_enabled ()
2535
0
      || cpr_get_answer_is_yes ("keyedit.cancel.okay",
2536
0
              _("Quit without saving? (y/N) ")))
2537
0
    goto leave;
2538
0
        break;
2539
0
      }
2540
    /* fall through */
2541
0
  case cmdSAVE:
2542
0
    if (modified)
2543
0
      {
2544
0
              err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
2545
0
              if (err)
2546
0
                {
2547
0
                  log_error (_("update failed: %s\n"), gpg_strerror (err));
2548
0
                  break;
2549
0
                }
2550
0
              if (upload)
2551
0
                {
2552
0
                  maybe_upload_key (ctrl, keyblock);
2553
0
                  upload = 0;
2554
0
                }
2555
0
      }
2556
2557
0
          if (delseckey_list)
2558
0
            {
2559
0
              strlist_t sl;
2560
0
              for (err = 0, sl = delseckey_list; sl; sl = sl->next)
2561
0
                {
2562
0
                  if (*sl->d)
2563
0
                    {
2564
0
                      err = agent_delete_key (ctrl, sl->d, NULL, 1/*force*/);
2565
0
                      if (err)
2566
0
                        break;
2567
0
                      *sl->d = 0;  /* Mark deleted.  */
2568
0
                    }
2569
0
                }
2570
0
              if (err)
2571
0
                {
2572
0
                  log_error (_("deleting copy of secret key failed: %s\n"),
2573
0
                             gpg_strerror (err));
2574
0
                  break; /* the "save".  */
2575
0
                }
2576
0
            }
2577
2578
0
    if (sec_shadowing)
2579
0
      {
2580
0
        err = agent_scd_learn (NULL, 1);
2581
0
        if (err)
2582
0
                {
2583
0
                  log_error (_("update failed: %s\n"), gpg_strerror (err));
2584
0
                  break;
2585
0
                }
2586
0
      }
2587
2588
0
    if (!modified && !sec_shadowing)
2589
0
      tty_printf (_("Key not changed so no update needed.\n"));
2590
2591
0
    if (update_trust)
2592
0
      {
2593
0
        revalidation_mark (ctrl);
2594
0
        update_trust = 0;
2595
0
      }
2596
0
    goto leave;
2597
2598
0
  case cmdINVCMD:
2599
0
  default:
2600
0
    tty_printf ("\n");
2601
0
    tty_printf (_("Invalid command  (try \"help\")\n"));
2602
0
    break;
2603
0
  }
2604
0
    } /* End of the main command loop.  */
2605
2606
0
 leave:
2607
0
  free_strlist (delseckey_list);
2608
0
  release_kbnode (keyblock);
2609
0
  keydb_release (kdbhd);
2610
0
  xfree (answer);
2611
0
}
2612
2613
2614
/* Helper to upload a key to an LDAP server if configured.  */
2615
static void
2616
maybe_upload_key (ctrl_t ctrl, kbnode_t keyblock)
2617
0
{
2618
0
  unsigned int saved_options;
2619
2620
0
  if (!opt.flags.auto_key_upload)
2621
0
    return;
2622
2623
0
  saved_options = opt.keyserver_options.options;
2624
0
  opt.keyserver_options.options |= KEYSERVER_LDAP_ONLY;
2625
0
  opt.keyserver_options.options |= KEYSERVER_WARN_ONLY;
2626
0
  keyserver_export_pubkey (ctrl, keyblock->pkt->pkt.public_key, 0);
2627
0
  opt.keyserver_options.options = saved_options;
2628
0
}
2629
2630
2631
/* Change the passphrase of the secret key identified by USERNAME.  */
2632
void
2633
keyedit_passwd (ctrl_t ctrl, const char *username)
2634
0
{
2635
0
  gpg_error_t err;
2636
0
  PKT_public_key *pk;
2637
0
  kbnode_t keyblock = NULL;
2638
2639
0
  pk = xtrycalloc (1, sizeof *pk);
2640
0
  if (!pk)
2641
0
    {
2642
0
      err = gpg_error_from_syserror ();
2643
0
      goto leave;
2644
0
    }
2645
0
  err = getkey_byname (ctrl, NULL, pk, username, 1, &keyblock);
2646
0
  if (err)
2647
0
    goto leave;
2648
2649
0
  err = change_passphrase (ctrl, keyblock);
2650
2651
0
leave:
2652
0
  release_kbnode (keyblock);
2653
0
  free_public_key (pk);
2654
0
  if (err)
2655
0
    {
2656
0
      log_info ("error changing the passphrase for '%s': %s\n",
2657
0
    username, gpg_strerror (err));
2658
0
      write_status_error ("keyedit.passwd", err);
2659
0
    }
2660
0
  else
2661
0
    write_status_text (STATUS_SUCCESS, "keyedit.passwd");
2662
0
}
2663
2664
2665
/* Helper for quick commands to find the keyblock for USERNAME.
2666
 * Returns on success the key database handle at R_KDBHD and the
2667
 * keyblock at R_KEYBLOCK.  */
2668
static gpg_error_t
2669
quick_find_keyblock (ctrl_t ctrl, const char *username, int want_secret,
2670
                     KEYDB_HANDLE *r_kdbhd, kbnode_t *r_keyblock)
2671
0
{
2672
0
  gpg_error_t err;
2673
0
  KEYDB_HANDLE kdbhd = NULL;
2674
0
  kbnode_t keyblock = NULL;
2675
0
  KEYDB_SEARCH_DESC desc;
2676
0
  kbnode_t node;
2677
2678
0
  *r_kdbhd = NULL;
2679
0
  *r_keyblock = NULL;
2680
2681
  /* Search the key; we don't want the whole getkey stuff here.  */
2682
0
  kdbhd = keydb_new (ctrl);
2683
0
  if (!kdbhd)
2684
0
    {
2685
      /* Note that keydb_new has already used log_error.  */
2686
0
      err = gpg_error_from_syserror ();
2687
0
      goto leave;
2688
0
    }
2689
0
  err = keydb_lock (kdbhd);
2690
0
  if (err)
2691
0
    goto leave;
2692
2693
0
  err = classify_user_id (username, &desc, 1);
2694
0
  if (!err)
2695
0
    err = keydb_search (kdbhd, &desc, 1, NULL);
2696
0
  if (!err)
2697
0
    {
2698
0
      err = keydb_get_keyblock (kdbhd, &keyblock);
2699
0
      if (err)
2700
0
        {
2701
0
          log_error (_("error reading keyblock: %s\n"), gpg_strerror (err));
2702
0
          goto leave;
2703
0
        }
2704
      /* Now with the keyblock retrieved, search again to detect an
2705
         ambiguous specification.  We need to save the found state so
2706
         that we can do an update later.  */
2707
0
      keydb_push_found_state (kdbhd);
2708
0
      err = keydb_search (kdbhd, &desc, 1, NULL);
2709
0
      if (!err)
2710
0
        err = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
2711
0
      else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
2712
0
        err = 0;
2713
0
      keydb_pop_found_state (kdbhd);
2714
2715
0
      if (!err && want_secret)
2716
0
        {
2717
          /* We require the secret primary key to set the primary UID.  */
2718
0
          node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
2719
0
          log_assert (node);
2720
0
          if (!agent_probe_secret_key (ctrl, node->pkt->pkt.public_key))
2721
0
            err = gpg_error (GPG_ERR_NO_SECKEY);
2722
0
        }
2723
0
    }
2724
0
  else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
2725
0
    err = gpg_error (GPG_ERR_NO_PUBKEY);
2726
2727
0
  if (err)
2728
0
    {
2729
0
      log_error (_("key \"%s\" not found: %s\n"),
2730
0
                 username, gpg_strerror (err));
2731
0
      goto leave;
2732
0
    }
2733
2734
0
  fix_keyblock (ctrl, &keyblock);
2735
0
  merge_keys_and_selfsig (ctrl, keyblock);
2736
2737
0
  *r_keyblock = keyblock;
2738
0
  keyblock = NULL;
2739
0
  *r_kdbhd = kdbhd;
2740
0
  kdbhd = NULL;
2741
2742
0
 leave:
2743
0
  release_kbnode (keyblock);
2744
0
  keydb_release (kdbhd);
2745
0
  return err;
2746
0
}
2747
2748
2749
/* Unattended adding of a new keyid.  USERNAME specifies the
2750
   key. NEWUID is the new user id to add to the key.  */
2751
void
2752
keyedit_quick_adduid (ctrl_t ctrl, const char *username, const char *newuid)
2753
0
{
2754
0
  gpg_error_t err;
2755
0
  KEYDB_HANDLE kdbhd = NULL;
2756
0
  kbnode_t keyblock = NULL;
2757
0
  char *uidstring = NULL;
2758
2759
0
  uidstring = xstrdup (newuid);
2760
0
  trim_spaces (uidstring);
2761
0
  if (!*uidstring)
2762
0
    {
2763
0
      log_error ("%s\n", gpg_strerror (GPG_ERR_INV_USER_ID));
2764
0
      goto leave;
2765
0
    }
2766
2767
#ifdef HAVE_W32_SYSTEM
2768
  /* See keyedit_menu for why we need this.  */
2769
  check_trustdb_stale (ctrl);
2770
#endif
2771
2772
  /* Search the key; we don't want the whole getkey stuff here.  */
2773
0
  err = quick_find_keyblock (ctrl, username, 1, &kdbhd, &keyblock);
2774
0
  if (err)
2775
0
    goto leave;
2776
2777
0
  if (menu_adduid (ctrl, keyblock, 0, NULL, uidstring))
2778
0
    {
2779
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
2780
0
      if (err)
2781
0
        {
2782
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
2783
0
          goto leave;
2784
0
        }
2785
0
      maybe_upload_key (ctrl, keyblock);
2786
0
      if (update_trust)
2787
0
        revalidation_mark (ctrl);
2788
0
    }
2789
2790
0
 leave:
2791
0
  xfree (uidstring);
2792
0
  release_kbnode (keyblock);
2793
0
  keydb_release (kdbhd);
2794
0
}
2795
2796
2797
/* Helper to find the UID node for namehash.  On success, returns the UID node.
2798
   Otherwise, return NULL. */
2799
kbnode_t
2800
find_userid_by_namehash (kbnode_t keyblock, const char *namehash, int want_valid)
2801
0
{
2802
0
  byte hash[NAMEHASH_LEN];
2803
0
  kbnode_t node = NULL;
2804
2805
0
  if (!namehash)
2806
0
    goto leave;
2807
2808
0
  if (strlen (namehash) != NAMEHASH_LEN * 2)
2809
0
    goto leave;
2810
2811
0
  if (hex2bin (namehash, hash, NAMEHASH_LEN) < 0)
2812
0
    goto leave;
2813
2814
0
  for (node = keyblock; node; node = node->next)
2815
0
    {
2816
0
      if (node->pkt->pkttype == PKT_USER_ID
2817
0
          && (!want_valid || (!node->pkt->pkt.user_id->flags.revoked
2818
0
                              && !node->pkt->pkt.user_id->flags.expired)))
2819
0
  {
2820
0
    namehash_from_uid (node->pkt->pkt.user_id);
2821
0
    if (!memcmp (node->pkt->pkt.user_id->namehash, hash, NAMEHASH_LEN))
2822
0
      break;
2823
0
        }
2824
0
    }
2825
2826
0
 leave:
2827
0
  return node;
2828
0
}
2829
2830
2831
/* Helper to find the UID node for uid.  On success, returns the UID node.
2832
   Otherwise, return NULL. */
2833
kbnode_t
2834
find_userid (kbnode_t keyblock, const char *uid, int want_valid)
2835
0
{
2836
0
  kbnode_t node = NULL;
2837
0
  size_t uidlen;
2838
2839
0
  if (!keyblock || !uid)
2840
0
    goto leave;
2841
2842
  /* First try to find UID by namehash. */
2843
0
  node = find_userid_by_namehash (keyblock, uid, want_valid);
2844
0
  if (node)
2845
0
    goto leave;
2846
2847
0
  uidlen = strlen (uid);
2848
0
  for (node = keyblock; node; node = node->next)
2849
0
    {
2850
0
      if (node->pkt->pkttype == PKT_USER_ID
2851
0
          && (!want_valid || (!node->pkt->pkt.user_id->flags.revoked
2852
0
                              && !node->pkt->pkt.user_id->flags.expired))
2853
0
          && uidlen == node->pkt->pkt.user_id->len
2854
0
          && !memcmp (node->pkt->pkt.user_id->name, uid, uidlen))
2855
0
        break;
2856
0
    }
2857
2858
0
 leave:
2859
0
  return node;
2860
0
}
2861
2862
2863
/* Unattended revocation of a keyid.  USERNAME specifies the
2864
   key. UIDTOREV is the user id revoke from the key.  */
2865
void
2866
keyedit_quick_revuid (ctrl_t ctrl, const char *username, const char *uidtorev)
2867
0
{
2868
0
  gpg_error_t err;
2869
0
  KEYDB_HANDLE kdbhd = NULL;
2870
0
  kbnode_t keyblock = NULL;
2871
0
  kbnode_t node;
2872
0
  int modified = 0;
2873
0
  size_t valid_uids;
2874
2875
#ifdef HAVE_W32_SYSTEM
2876
  /* See keyedit_menu for why we need this.  */
2877
  check_trustdb_stale (ctrl);
2878
#endif
2879
2880
  /* Search the key; we don't want the whole getkey stuff here.  */
2881
0
  err = quick_find_keyblock (ctrl, username, 1, &kdbhd, &keyblock);
2882
0
  if (err)
2883
0
    goto leave;
2884
2885
  /* To make sure that we do not revoke the last valid UID, we first
2886
     count how many valid UIDs there are.  */
2887
0
  valid_uids = 0;
2888
0
  for (node = keyblock; node; node = node->next)
2889
0
    valid_uids += (node->pkt->pkttype == PKT_USER_ID
2890
0
                   && !node->pkt->pkt.user_id->flags.revoked
2891
0
                   && !node->pkt->pkt.user_id->flags.expired);
2892
2893
  /* Find the right UID. */
2894
0
  node = find_userid (keyblock, uidtorev, 0);
2895
0
  if (node)
2896
0
    {
2897
0
      struct revocation_reason_info *reason;
2898
2899
      /* Make sure that we do not revoke the last valid UID.  */
2900
0
      if (valid_uids == 1
2901
0
          && ! node->pkt->pkt.user_id->flags.revoked
2902
0
          && ! node->pkt->pkt.user_id->flags.expired)
2903
0
        {
2904
0
          log_error (_("cannot revoke the last valid user ID.\n"));
2905
0
          err = gpg_error (GPG_ERR_INV_USER_ID);
2906
0
          goto leave;
2907
0
        }
2908
2909
0
      reason = get_default_uid_revocation_reason ();
2910
0
      err = core_revuid (ctrl, keyblock, node, reason, &modified);
2911
0
      release_revocation_reason_info (reason);
2912
0
      if (err)
2913
0
        goto leave;
2914
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
2915
0
      if (err)
2916
0
        {
2917
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
2918
0
          goto leave;
2919
0
        }
2920
0
      maybe_upload_key (ctrl, keyblock);
2921
0
      revalidation_mark (ctrl);
2922
0
      goto leave;
2923
0
    }
2924
0
  err = gpg_error (GPG_ERR_NO_USER_ID);
2925
2926
2927
0
 leave:
2928
0
  if (err)
2929
0
    {
2930
0
      log_error (_("revoking the user ID failed: %s\n"), gpg_strerror (err));
2931
0
      write_status_error ("keyedit.revoke.uid", err);
2932
0
    }
2933
0
  release_kbnode (keyblock);
2934
0
  keydb_release (kdbhd);
2935
0
}
2936
2937
2938
/* Unattended setting of the primary uid.  USERNAME specifies the key.
2939
   PRIMARYUID is the user id which shall be primary.  */
2940
void
2941
keyedit_quick_set_primary (ctrl_t ctrl, const char *username,
2942
                           const char *primaryuid)
2943
0
{
2944
0
  gpg_error_t err;
2945
0
  KEYDB_HANDLE kdbhd = NULL;
2946
0
  kbnode_t keyblock = NULL;
2947
0
  kbnode_t primarynode;
2948
0
  kbnode_t node;
2949
2950
#ifdef HAVE_W32_SYSTEM
2951
  /* See keyedit_menu for why we need this.  */
2952
  check_trustdb_stale (ctrl);
2953
#endif
2954
2955
0
  err = quick_find_keyblock (ctrl, username, 1, &kdbhd, &keyblock);
2956
0
  if (err)
2957
0
    {
2958
0
      write_status_error ("keyedit.primary", err);
2959
0
      goto leave;
2960
0
    }
2961
2962
  /* Find the first matching UID that is valid */
2963
0
  primarynode = find_userid (keyblock, primaryuid, 1);
2964
2965
  /* and mark it. */
2966
0
  if (primarynode)
2967
0
    for (node = keyblock; node; node = node->next)
2968
0
      {
2969
0
        if (node == primarynode)
2970
0
          node->flag |= NODFLG_SELUID;
2971
0
        else
2972
0
          node->flag &= ~NODFLG_SELUID;
2973
0
      }
2974
2975
0
  if (!primarynode)
2976
0
    err = gpg_error (GPG_ERR_NO_USER_ID);
2977
0
  else if (menu_set_primary_uid (ctrl, keyblock))
2978
0
    {
2979
0
      merge_keys_and_selfsig (ctrl, keyblock);
2980
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
2981
0
      if (err)
2982
0
        {
2983
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
2984
0
          goto leave;
2985
0
        }
2986
0
      maybe_upload_key (ctrl, keyblock);
2987
0
      revalidation_mark (ctrl);
2988
0
    }
2989
0
  else
2990
0
    err = gpg_error (GPG_ERR_GENERAL);
2991
2992
0
  if (err)
2993
0
    {
2994
0
      log_error (_("setting the primary user ID failed: %s\n"),
2995
0
                gpg_strerror (err));
2996
0
      write_status_error ("keyedit.primary", err);
2997
0
    }
2998
2999
0
 leave:
3000
0
  release_kbnode (keyblock);
3001
0
  keydb_release (kdbhd);
3002
0
}
3003
3004
3005
/* Unattended updating of the preference tro the standard preferences.
3006
 * USERNAME specifies the key.  This is basically the same as
3007
 *      gpg --edit-key <<userif> updpref save
3008
 */
3009
void
3010
keyedit_quick_update_pref (ctrl_t ctrl, const char *username)
3011
0
{
3012
0
  gpg_error_t err;
3013
0
  KEYDB_HANDLE kdbhd = NULL;
3014
0
  kbnode_t keyblock = NULL;
3015
3016
#ifdef HAVE_W32_SYSTEM
3017
  /* See keyedit_menu for why we need this.  */
3018
  check_trustdb_stale (ctrl);
3019
#endif
3020
3021
0
  err = quick_find_keyblock (ctrl, username, 1, &kdbhd, &keyblock);
3022
0
  if (err)
3023
0
    goto leave;
3024
3025
0
  if (menu_set_preferences (ctrl, keyblock, 1))
3026
0
    {
3027
0
      merge_keys_and_selfsig (ctrl, keyblock);
3028
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
3029
0
      if (err)
3030
0
        {
3031
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
3032
0
          goto leave;
3033
0
        }
3034
0
      maybe_upload_key (ctrl, keyblock);
3035
0
    }
3036
3037
0
 leave:
3038
0
  if (err)
3039
0
    write_status_error ("keyedit.updpref", err);
3040
0
  release_kbnode (keyblock);
3041
0
  keydb_release (kdbhd);
3042
0
}
3043
3044
3045
/* Unattended updating of the ownertrust or disable/enable state of a key
3046
 * USERNAME specifies the key.  This is somewhat similar to
3047
 *      gpg --edit-key <userid> trust save
3048
 *      gpg --edit-key <userid> disable save
3049
 *
3050
 * VALUE is the new trust value which is one of:
3051
 *    "undefined"  - Ownertrust is set to undefined
3052
 *    "never"      - Ownertrust is set to never trust
3053
 *    "marginal"   - Ownertrust is set to marginal trust
3054
 *    "full"       - Ownertrust is set to full trust
3055
 *    "ultimate"   - Ownertrust is set to ultimate trust
3056
 *    "enable"     - The key is re-enabled.
3057
 *    "disable"    - The key is disabled.
3058
 * Trust settings do not change the ebable/disable state.
3059
 */
3060
void
3061
keyedit_quick_set_ownertrust (ctrl_t ctrl, const char *username,
3062
                              const char *value)
3063
0
{
3064
0
  gpg_error_t err;
3065
0
  KEYDB_HANDLE kdbhd = NULL;
3066
0
  kbnode_t keyblock = NULL;
3067
0
  PKT_public_key *pk;
3068
0
  unsigned int trust, newtrust;
3069
0
  int x;
3070
0
  int maybe_update_trust = 0;
3071
3072
#ifdef HAVE_W32_SYSTEM
3073
  /* See keyedit_menu for why we need this.  */
3074
  check_trustdb_stale (ctrl);
3075
#endif
3076
3077
  /* Search the key; we don't want the whole getkey stuff here.  Note
3078
   * that we are looking for the public key here.  */
3079
0
  err = quick_find_keyblock (ctrl, username, 0, &kdbhd, &keyblock);
3080
0
  if (err)
3081
0
    goto leave;
3082
0
  log_assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY
3083
0
              || keyblock->pkt->pkttype == PKT_SECRET_KEY);
3084
0
  pk = keyblock->pkt->pkt.public_key;
3085
3086
0
  trust = newtrust = get_ownertrust (ctrl, pk);
3087
3088
0
  if (!ascii_strcasecmp (value, "enable"))
3089
0
    newtrust &= ~TRUST_FLAG_DISABLED;
3090
0
  else if (!ascii_strcasecmp (value, "disable"))
3091
0
    newtrust |= TRUST_FLAG_DISABLED;
3092
0
  else if ((x = string_to_trust_value (value)) >= 0)
3093
0
    {
3094
0
      newtrust = x;
3095
0
      newtrust &= TRUST_MASK;
3096
0
      newtrust |= (trust & ~TRUST_MASK);
3097
0
      maybe_update_trust = 1;
3098
0
    }
3099
0
  else
3100
0
    {
3101
0
      err = gpg_error (GPG_ERR_INV_ARG);
3102
0
      goto leave;
3103
0
    }
3104
3105
0
  if (trust != newtrust)
3106
0
    {
3107
0
      update_ownertrust (ctrl, pk, newtrust);
3108
0
      if (maybe_update_trust)
3109
0
        revalidation_mark (ctrl);
3110
0
    }
3111
0
  else if (opt.verbose)
3112
0
    log_info (_("Key not changed so no update needed.\n"));
3113
3114
0
 leave:
3115
0
  if (err)
3116
0
    {
3117
0
      log_error (_("setting the ownertrust to '%s' failed: %s\n"),
3118
0
                 value, gpg_strerror (err));
3119
0
      write_status_error ("keyedit.setownertrust", err);
3120
0
    }
3121
0
  release_kbnode (keyblock);
3122
0
  keydb_release (kdbhd);
3123
0
}
3124
3125
3126
/* Find a keyblock by fingerprint because only this uniquely
3127
 * identifies a key and may thus be used to select a key for
3128
 * unattended subkey creation os key signing.  */
3129
static gpg_error_t
3130
find_by_primary_fpr (ctrl_t ctrl, const char *fpr,
3131
                     kbnode_t *r_keyblock, KEYDB_HANDLE *r_kdbhd)
3132
0
{
3133
0
  gpg_error_t err;
3134
0
  kbnode_t keyblock = NULL;
3135
0
  KEYDB_HANDLE kdbhd = NULL;
3136
0
  KEYDB_SEARCH_DESC desc;
3137
0
  byte fprbin[MAX_FINGERPRINT_LEN];
3138
0
  size_t fprlen;
3139
3140
0
  *r_keyblock = NULL;
3141
0
  *r_kdbhd = NULL;
3142
3143
0
  if (classify_user_id (fpr, &desc, 1)
3144
0
      || desc.mode != KEYDB_SEARCH_MODE_FPR)
3145
0
    {
3146
0
      log_error (_("\"%s\" is not a fingerprint\n"), fpr);
3147
0
      err = gpg_error (GPG_ERR_INV_NAME);
3148
0
      goto leave;
3149
0
    }
3150
0
  err = get_pubkey_byname (ctrl, GET_PUBKEY_NO_AKL,
3151
0
                           NULL, NULL, fpr, &keyblock, &kdbhd, 1);
3152
0
  if (err)
3153
0
    {
3154
0
      log_error (_("key \"%s\" not found: %s\n"), fpr, gpg_strerror (err));
3155
0
      goto leave;
3156
0
    }
3157
3158
  /* Check that the primary fingerprint has been given. */
3159
0
  fingerprint_from_pk (keyblock->pkt->pkt.public_key, fprbin, &fprlen);
3160
0
  if (desc.mode == KEYDB_SEARCH_MODE_FPR
3161
0
      && fprlen == desc.fprlen
3162
0
      && !memcmp (fprbin, desc.u.fpr, fprlen))
3163
0
    ;
3164
0
  else
3165
0
    {
3166
0
      log_error (_("\"%s\" is not the primary fingerprint\n"), fpr);
3167
0
      err = gpg_error (GPG_ERR_INV_NAME);
3168
0
      goto leave;
3169
0
    }
3170
3171
0
  *r_keyblock = keyblock;
3172
0
  keyblock = NULL;
3173
0
  *r_kdbhd = kdbhd;
3174
0
  kdbhd = NULL;
3175
0
  err = 0;
3176
3177
0
 leave:
3178
0
  release_kbnode (keyblock);
3179
0
  keydb_release (kdbhd);
3180
0
  return err;
3181
0
}
3182
3183
3184
/* Unattended key signing function.  If the key specified by FPR is
3185
   available and FPR is the primary fingerprint all user ids of the
3186
   key are signed using the default signing key.  If UIDS is an empty
3187
   list all usable UIDs are signed, if it is not empty, only those
3188
   user ids matching one of the entries of the list are signed.  With
3189
   LOCAL being true the signatures are marked as non-exportable.  If
3190
   TRUSTSIG is given a trust signature is created; see
3191
   parse_trustsig_string(). */
3192
void
3193
keyedit_quick_sign (ctrl_t ctrl, const char *fpr, strlist_t uids,
3194
                    strlist_t locusr, const char *trustsig, int local)
3195
0
{
3196
0
  gpg_error_t err = 0;
3197
0
  kbnode_t keyblock = NULL;
3198
0
  KEYDB_HANDLE kdbhd = NULL;
3199
0
  int modified = 0;
3200
0
  PKT_public_key *pk;
3201
0
  kbnode_t node;
3202
0
  strlist_t sl;
3203
0
  int any;
3204
3205
#ifdef HAVE_W32_SYSTEM
3206
  /* See keyedit_menu for why we need this.  */
3207
  check_trustdb_stale (ctrl);
3208
#endif
3209
3210
  /* Do an early check on an arg for an immediate error message.  */
3211
0
  if (trustsig)
3212
0
    {
3213
0
      byte trust_depth, trust_value;
3214
0
      char *trust_regexp;
3215
0
      err = parse_trustsig_string (trustsig, &trust_value,
3216
0
                                   &trust_depth, &trust_regexp);
3217
0
      xfree (trust_regexp);
3218
0
      (void)trust_depth;
3219
0
      (void)trust_value;
3220
0
      if (err)
3221
0
        goto leave;
3222
0
    }
3223
3224
  /* We require a fingerprint because only this uniquely identifies a
3225
     key and may thus be used to select a key for unattended key
3226
     signing.  */
3227
0
  if (find_by_primary_fpr (ctrl, fpr, &keyblock, &kdbhd))
3228
0
    goto leave;
3229
3230
0
  if (fix_keyblock (ctrl, &keyblock))
3231
0
    modified++;
3232
3233
  /* Give some info in verbose.  */
3234
0
  if (opt.verbose)
3235
0
    {
3236
0
      show_key_with_all_names (ctrl, es_stdout, keyblock, 0,
3237
0
                               1/*with_revoker*/, 1/*with_fingerprint*/,
3238
0
                               0, 0, 1);
3239
0
      es_fflush (es_stdout);
3240
0
    }
3241
3242
0
  pk = keyblock->pkt->pkt.public_key;
3243
0
  if (pk->flags.revoked)
3244
0
    {
3245
0
      if (!opt.verbose)
3246
0
        show_key_with_all_names (ctrl, es_stdout, keyblock, 0, 0, 0, 0, 0, 1);
3247
0
      log_error ("%s%s", _("Key is revoked."), _("  Unable to sign.\n"));
3248
0
      err = gpg_error (GPG_ERR_CERT_REVOKED);
3249
0
      goto leave;
3250
0
    }
3251
3252
  /* Set the flags according to the UIDS list.  Fixme: We may want to
3253
     use classify_user_id along with dedicated compare functions so
3254
     that we match the same way as in the key lookup. */
3255
0
  any = 0;
3256
0
  menu_select_uid (keyblock, 0);   /* Better clear the flags first. */
3257
0
  for (sl=uids; sl; sl = sl->next)
3258
0
    {
3259
0
      const char *name = sl->d;
3260
0
      int count = 0;
3261
3262
0
      sl->flags &= ~(1|2);  /* Clear flags used for error reporting.  */
3263
3264
0
      for (node = keyblock; node; node = node->next)
3265
0
        {
3266
0
          if (node->pkt->pkttype == PKT_USER_ID)
3267
0
            {
3268
0
              PKT_user_id *uid = node->pkt->pkt.user_id;
3269
3270
0
              if (uid->attrib_data)
3271
0
                ;
3272
0
              else if (*name == '='
3273
0
                       && strlen (name+1) == uid->len
3274
0
                       && !memcmp (uid->name, name + 1, uid->len))
3275
0
                { /* Exact match - we don't do a check for ambiguity
3276
                   * in this case.  */
3277
0
                  node->flag |= NODFLG_SELUID;
3278
0
                  if (any != -1)
3279
0
                    {
3280
0
                      sl->flags |= 1;  /* Report as found.  */
3281
0
                      any = 1;
3282
0
                    }
3283
0
                }
3284
0
              else if (ascii_memistr (uid->name, uid->len,
3285
0
                                      *name == '*'? name+1:name))
3286
0
                {
3287
0
                  node->flag |= NODFLG_SELUID;
3288
0
                  if (any != -1)
3289
0
                    {
3290
0
                      sl->flags |= 1;  /* Report as found.  */
3291
0
                      any = 1;
3292
0
                    }
3293
0
                  count++;
3294
0
                }
3295
0
            }
3296
0
        }
3297
3298
0
      if (count > 1)
3299
0
        {
3300
0
          any = -1;        /* Force failure at end.  */
3301
0
          sl->flags |= 2;  /* Report as ambiguous.  */
3302
0
        }
3303
0
    }
3304
3305
  /* Check whether all given user ids were found.  */
3306
0
  for (sl=uids; sl; sl = sl->next)
3307
0
    if (!(sl->flags & 1))
3308
0
      any = -1;  /* That user id was not found.  */
3309
3310
  /* Print an error if there was a problem with the user ids.  */
3311
0
  if (uids && any < 1)
3312
0
    {
3313
0
      if (!opt.verbose)
3314
0
        show_key_with_all_names (ctrl, es_stdout, keyblock, 0, 0, 0, 0, 0, 1);
3315
0
      es_fflush (es_stdout);
3316
0
      for (sl=uids; sl; sl = sl->next)
3317
0
        {
3318
0
          if ((sl->flags & 2))
3319
0
            log_info (_("Invalid user ID '%s': %s\n"),
3320
0
                      sl->d, gpg_strerror (GPG_ERR_AMBIGUOUS_NAME));
3321
0
          else if (!(sl->flags & 1))
3322
0
            log_info (_("Invalid user ID '%s': %s\n"),
3323
0
                      sl->d, gpg_strerror (GPG_ERR_NOT_FOUND));
3324
0
        }
3325
0
      log_error ("%s  %s", _("No matching user IDs."), _("Nothing to sign.\n"));
3326
0
      err = gpg_error (GPG_ERR_NO_USER_ID);
3327
0
      goto leave;
3328
0
    }
3329
3330
  /* Sign. */
3331
0
  err = sign_uids (ctrl, es_stdout, keyblock, locusr,
3332
0
                   (SIGN_UIDS_QUICK
3333
0
                    | (local? SIGN_UIDS_LOCAL : 0)
3334
0
                    | (trustsig? SIGN_UIDS_TRUSTSIG : 0)),
3335
0
                   trustsig, &modified);
3336
0
  es_fflush (es_stdout);
3337
0
  if (err)
3338
0
    goto leave;
3339
3340
0
  if (modified)
3341
0
    {
3342
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
3343
0
      if (err)
3344
0
        {
3345
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
3346
0
          goto leave;
3347
0
        }
3348
0
      if (!local) /* No need to upload new non-expotable sigs.  */
3349
0
        maybe_upload_key (ctrl, keyblock);
3350
0
    }
3351
0
  else
3352
0
    log_info (_("Key not changed so no update needed.\n"));
3353
3354
0
  if (update_trust)
3355
0
    revalidation_mark (ctrl);
3356
3357
0
 leave:
3358
0
  if (err)
3359
0
    {
3360
0
      log_error (_("creating key signature failed: %s\n"), gpg_strerror (err));
3361
0
      write_status_error ("keyedit.sign-key", err);
3362
0
    }
3363
0
  release_kbnode (keyblock);
3364
0
  keydb_release (kdbhd);
3365
0
}
3366
3367
3368
/* Unattended revocation of a key signatures.  USERNAME specifies the
3369
 * key; this should best be a fingerprint. SIGTOREV is the user-id of
3370
 * the key for which the key signature shall be removed.  Only
3371
 * non-self-signatures can be removed with this functions.  If
3372
 * AFFECTED_UIDS is not NULL only the key signatures on these user-ids
3373
 * are revoked. */
3374
void
3375
keyedit_quick_revsig (ctrl_t ctrl, const char *username, const char *sigtorev,
3376
                      strlist_t affected_uids)
3377
0
{
3378
0
  gpg_error_t err = 0;
3379
0
  int no_signing_key = 0;
3380
0
  KEYDB_HANDLE kdbhd = NULL;
3381
0
  kbnode_t keyblock = NULL;
3382
0
  PKT_public_key *primarypk;  /* Points into KEYBLOCK.  */
3383
0
  u32 *primarykid;
3384
0
  PKT_public_key *pksigtorev = NULL;
3385
0
  u32 *pksigtorevkid;
3386
0
  kbnode_t node, n;
3387
0
  int skip_remaining;
3388
0
  int consider_sig;
3389
0
  strlist_t sl;
3390
0
  struct sign_attrib attrib = { 0 };
3391
3392
#ifdef HAVE_W32_SYSTEM
3393
  /* See keyedit_menu for why we need this.  */
3394
  check_trustdb_stale (ctrl);
3395
#endif
3396
3397
  /* Search the key; we don't want the whole getkey stuff here.  Note
3398
   * that we are looking for the public key here.  */
3399
0
  err = quick_find_keyblock (ctrl, username, 0, &kdbhd, &keyblock);
3400
0
  if (err)
3401
0
    goto leave;
3402
0
  log_assert (keyblock && (keyblock->pkt->pkttype == PKT_PUBLIC_KEY
3403
0
              || keyblock->pkt->pkttype == PKT_SECRET_KEY));
3404
0
  primarypk = keyblock->pkt->pkt.public_key;
3405
0
  primarykid = pk_keyid (primarypk);
3406
3407
  /* Get the signing key we want to revoke.  This must be one of our
3408
   * signing keys.  We will compare only the keyid because we don't
3409
   * assume that we have duplicated keyids on our own secret keys.  If
3410
   * a there is a duplicated one we will notice this when creating the
3411
   * revocation.  */
3412
0
  pksigtorev = xtrycalloc (1, sizeof *pksigtorev);
3413
0
  if (!pksigtorev)
3414
0
    {
3415
0
      err = gpg_error_from_syserror ();
3416
0
      goto leave;
3417
0
    }
3418
0
  pksigtorev->req_usage = PUBKEY_USAGE_CERT;
3419
0
  err = getkey_byname (ctrl, NULL, pksigtorev, sigtorev, 1, NULL);
3420
0
  if (err)
3421
0
    {
3422
0
      no_signing_key = 1;
3423
0
      goto leave;
3424
0
    }
3425
0
  pksigtorevkid = pk_keyid (pksigtorev);
3426
3427
  /* Find the signatures we want to revoke and set a mark.  */
3428
0
  skip_remaining = consider_sig = 0;
3429
0
  for (node = keyblock; node; node = node->next)
3430
0
    {
3431
0
      node->flag &= ~NODFLG_MARK_A;
3432
0
      if (skip_remaining)
3433
0
        ;
3434
0
      else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
3435
0
        skip_remaining = 1;
3436
0
      else if (node->pkt->pkttype == PKT_USER_ID)
3437
0
  {
3438
0
          PKT_user_id *uid = node->pkt->pkt.user_id;
3439
3440
0
          consider_sig = !affected_uids;
3441
0
          for (sl = affected_uids; !consider_sig && sl; sl = sl->next)
3442
0
            {
3443
0
              const char *name = sl->d;
3444
3445
0
              if (uid->attrib_data)
3446
0
                ;
3447
0
              else if (*name == '='
3448
0
                       && strlen (name+1) == uid->len
3449
0
                       && !memcmp (uid->name, name + 1, uid->len))
3450
0
                { /* Exact match.  */
3451
0
                  consider_sig = 1;
3452
0
                }
3453
0
              else if (ascii_memistr (uid->name, uid->len,
3454
0
                                      *name == '*'? name+1:name))
3455
0
                { /* Case-insensitive substring match.  */
3456
0
                  consider_sig = 1;
3457
0
                }
3458
0
            }
3459
0
  }
3460
0
      else if (node->pkt->pkttype == PKT_SIGNATURE)
3461
0
  {
3462
          /* We need to sort the signatures so that we can figure out
3463
           * whether the key signature has been revoked or the
3464
           * revocation has been superseded by a new key
3465
           * signature.  */
3466
0
          PKT_signature *sig;
3467
0
          unsigned int sigcount = 0;
3468
0
          kbnode_t *sigarray;
3469
3470
          /* Allocate an array large enough for all signatures.  */
3471
0
          for (n=node; n && n->pkt->pkttype == PKT_SIGNATURE; n = n->next)
3472
0
            sigcount++;
3473
0
          sigarray = xtrycalloc (sigcount, sizeof *sigarray);
3474
0
          if (!sigarray)
3475
0
            {
3476
0
              err = gpg_error_from_syserror ();
3477
0
              goto leave;
3478
0
            }
3479
3480
          /* Now fill the array with signatures we are interested in.
3481
           * We also move NODE forward to the end.  */
3482
0
          sigcount = 0;
3483
0
          for (n=node; n && n->pkt->pkttype == PKT_SIGNATURE; node=n, n=n->next)
3484
0
            {
3485
0
              sig = n->pkt->pkt.signature;
3486
0
              if (!keyid_cmp (primarykid, sig->keyid))
3487
0
                continue;  /* Ignore self-signatures.  */
3488
3489
0
              if (keyid_cmp (pksigtorevkid, sig->keyid))
3490
0
                continue; /* Ignore non-matching signatures.  */
3491
3492
0
              n->flag &= ~NODFLG_MARK_B; /* Clear flag used by cm_signode. */
3493
0
              sigarray[sigcount++] = n;
3494
0
            }
3495
3496
0
          if (sigcount)
3497
0
            {
3498
0
              qsort (sigarray, sigcount, sizeof *sigarray, cmp_signodes);
3499
3500
              /* log_debug ("Sorted signatures:\n"); */
3501
              /* for (idx=0; idx < sigcount; idx++) */
3502
              /*   { */
3503
              /*     sig = sigarray[idx]->pkt->pkt.signature; */
3504
              /*     log_debug ("%s 0x%02x %s\n", keystr (sig->keyid), */
3505
              /*                sig->sig_class, datestr_from_sig (sig)); */
3506
              /*   } */
3507
0
              sig = sigarray[sigcount-1]->pkt->pkt.signature;
3508
0
              if ((consider_sig || !affected_uids) && IS_UID_REV (sig))
3509
0
                {
3510
0
                  if (!opt.quiet)
3511
0
                    log_info ("sig by %s already revoked at %s\n",
3512
0
                              keystr (sig->keyid), datestr_from_sig (sig));
3513
0
                }
3514
0
              else if ((consider_sig && IS_UID_SIG (sig))
3515
0
                       || (!affected_uids && IS_KEY_SIG (sig)))
3516
0
                node->flag |= NODFLG_MARK_A;  /* Select signature.  */
3517
0
            }
3518
3519
0
          xfree (sigarray);
3520
0
  }
3521
0
    }
3522
3523
  /* Check whether any signatures were done by the given key.  We do
3524
   * not return an error if none were found.  */
3525
0
  for (node = keyblock; node; node = node->next)
3526
0
    if ((node->flag & NODFLG_MARK_A))
3527
0
      break;
3528
0
  if (!node)
3529
0
    {
3530
0
      if (opt.verbose)
3531
0
        log_info (_("Not signed by you.\n"));
3532
0
      err = 0;
3533
0
      goto leave;
3534
0
    }
3535
3536
  /* Revoke all marked signatures.  */
3537
0
  attrib.reason = get_default_sig_revocation_reason ();
3538
3539
0
 reloop: /* (we must repeat because we are modifying the list) */
3540
0
  for (node = keyblock; node; node = node->next)
3541
0
    {
3542
0
      kbnode_t unode;
3543
0
      PKT_signature *sig;
3544
0
      PACKET *pkt;
3545
3546
0
      if (!(node->flag & NODFLG_MARK_A))
3547
0
  continue;
3548
0
      node->flag &= ~NODFLG_MARK_A;
3549
3550
0
      if (IS_KEY_SIG (node->pkt->pkt.signature))
3551
0
        unode = NULL;
3552
0
      else
3553
0
        {
3554
0
          unode = find_prev_kbnode (keyblock, node, PKT_USER_ID);
3555
0
          log_assert (unode);
3556
0
        }
3557
3558
0
      attrib.non_exportable = !node->pkt->pkt.signature->flags.exportable;
3559
3560
0
      err = make_keysig_packet (ctrl, &sig, primarypk,
3561
0
                                unode? unode->pkt->pkt.user_id : NULL,
3562
0
                                NULL, pksigtorev, 0x30, 0, 0,
3563
0
                                sign_mk_attrib, &attrib, NULL);
3564
0
      if (err)
3565
0
        {
3566
0
          log_error ("signing failed: %s\n", gpg_strerror (err));
3567
0
          goto leave;
3568
0
        }
3569
3570
0
      pkt = xmalloc_clear (sizeof *pkt);
3571
0
      pkt->pkttype = PKT_SIGNATURE;
3572
0
      pkt->pkt.signature = sig;
3573
0
      if (unode)
3574
0
        insert_kbnode (unode, new_kbnode (pkt), 0);
3575
0
      goto reloop;
3576
0
    }
3577
3578
0
  err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
3579
0
  if (err)
3580
0
    {
3581
0
      log_error (_("update failed: %s\n"), gpg_strerror (err));
3582
0
      goto leave;
3583
0
    }
3584
0
  maybe_upload_key (ctrl, keyblock);
3585
0
  revalidation_mark (ctrl);
3586
3587
0
 leave:
3588
0
  if (err)
3589
0
    {
3590
0
      log_error (_("revoking the key signature failed: %s\n"),
3591
0
                 gpg_strerror (err));
3592
0
      if (no_signing_key)
3593
0
        print_further_info ("error getting key used to make the key signature");
3594
0
      write_status_error ("keyedit.revoke.sig", err);
3595
0
    }
3596
0
  release_revocation_reason_info (attrib.reason);
3597
0
  free_public_key (pksigtorev);
3598
0
  release_kbnode (keyblock);
3599
0
  keydb_release (kdbhd);
3600
0
}
3601
3602
3603
/* Unattended subkey creation function.
3604
 *
3605
 */
3606
void
3607
keyedit_quick_addkey (ctrl_t ctrl, const char *fpr, const char *algostr,
3608
                      const char *usagestr, const char *expirestr)
3609
0
{
3610
0
  gpg_error_t err;
3611
0
  kbnode_t keyblock;
3612
0
  KEYDB_HANDLE kdbhd;
3613
0
  int modified = 0;
3614
0
  PKT_public_key *pk;
3615
3616
#ifdef HAVE_W32_SYSTEM
3617
  /* See keyedit_menu for why we need this.  */
3618
  check_trustdb_stale (ctrl);
3619
#endif
3620
3621
  /* We require a fingerprint because only this uniquely identifies a
3622
   * key and may thus be used to select a key for unattended subkey
3623
   * creation.  */
3624
0
  if ((err=find_by_primary_fpr (ctrl, fpr, &keyblock, &kdbhd)))
3625
0
    goto leave;
3626
3627
0
  if (fix_keyblock (ctrl, &keyblock))
3628
0
    modified++;
3629
3630
0
  pk = keyblock->pkt->pkt.public_key;
3631
0
  if (pk->flags.revoked)
3632
0
    {
3633
0
      if (!opt.verbose)
3634
0
        show_key_with_all_names (ctrl, es_stdout, keyblock, 0, 0, 0, 0, 0, 1);
3635
0
      log_error ("%s%s", _("Key is revoked."), "\n");
3636
0
      err = gpg_error (GPG_ERR_CERT_REVOKED);
3637
0
      goto leave;
3638
0
    }
3639
3640
  /* Create the subkey.  Note that the called function already prints
3641
   * an error message. */
3642
0
  if (!generate_subkeypair (ctrl, keyblock, algostr, usagestr, expirestr))
3643
0
    modified = 1;
3644
0
  es_fflush (es_stdout);
3645
3646
  /* Store.  */
3647
0
  if (modified)
3648
0
    {
3649
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
3650
0
      if (err)
3651
0
        {
3652
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
3653
0
          goto leave;
3654
0
        }
3655
0
      maybe_upload_key (ctrl, keyblock);
3656
0
    }
3657
0
  else
3658
0
    log_info (_("Key not changed so no update needed.\n"));
3659
3660
0
 leave:
3661
0
  if (err)
3662
0
    write_status_error ("keyedit.addkey", err);
3663
0
  release_kbnode (keyblock);
3664
0
  keydb_release (kdbhd);
3665
0
}
3666
3667
3668
/* Unattended ADSK setup function.
3669
 *
3670
 * FPR is the fingerprint of our key.  ADSKFPR is the fingerprint of
3671
 * another subkey which we want to add as ADSK to our key.
3672
 */
3673
void
3674
keyedit_quick_addadsk (ctrl_t ctrl, const char *fpr, const char *adskfpr)
3675
0
{
3676
0
  gpg_error_t err;
3677
0
  kbnode_t keyblock;
3678
0
  KEYDB_HANDLE kdbhd;
3679
0
  int modified = 0;
3680
0
  PKT_public_key *pk;
3681
3682
#ifdef HAVE_W32_SYSTEM
3683
  /* See keyedit_menu for why we need this.  */
3684
  check_trustdb_stale (ctrl);
3685
#endif
3686
3687
  /* We require a fingerprint because only this uniquely identifies a
3688
   * key and may thus be used to select a key for unattended adsk
3689
   * adding. */
3690
0
  if ((err = find_by_primary_fpr (ctrl, fpr, &keyblock, &kdbhd)))
3691
0
    goto leave;
3692
3693
0
  if (fix_keyblock (ctrl, &keyblock))
3694
0
    modified++;
3695
3696
0
  pk = keyblock->pkt->pkt.public_key;
3697
0
  if (pk->flags.revoked)
3698
0
    {
3699
0
      if (!opt.verbose)
3700
0
        show_key_with_all_names (ctrl, es_stdout, keyblock, 0, 0, 0, 0, 0, 1);
3701
0
      log_error ("%s%s", _("Key is revoked."), "\n");
3702
0
      err = gpg_error (GPG_ERR_CERT_REVOKED);
3703
0
      goto leave;
3704
0
    }
3705
3706
  /* Locate and add the ADSK.  Note that the called function already
3707
   * prints error messages. */
3708
0
  if (adskfpr && !ascii_strcasecmp (adskfpr, "default"))
3709
0
    {
3710
0
      err = append_all_default_adsks (ctrl, keyblock);
3711
0
      if (!err)
3712
0
        modified = 1;
3713
0
      else if (gpg_err_code (err) == GPG_ERR_FALSE)
3714
0
        err = 0;
3715
0
    }
3716
0
  else if (menu_addadsk (ctrl, keyblock, adskfpr))
3717
0
    modified = 1;
3718
0
  else
3719
0
    log_inc_errorcount ();  /* (We use log_info in menu_adsk) */
3720
3721
0
  es_fflush (es_stdout);
3722
3723
  /* Store.  */
3724
0
  if (modified)
3725
0
    {
3726
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
3727
0
      if (err)
3728
0
        {
3729
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
3730
0
          goto leave;
3731
0
        }
3732
0
      maybe_upload_key (ctrl, keyblock);
3733
0
    }
3734
3735
0
 leave:
3736
0
  if (err)
3737
0
    write_status_error ("keyedit.addadsk", err);
3738
0
  release_kbnode (keyblock);
3739
0
  keydb_release (kdbhd);
3740
0
}
3741
3742
3743
/* Unattended expiration setting function for the main key.  If
3744
 * SUBKEYFPRS is not NULL and SUBKEYSFPRS[0] is neither NULL, it is
3745
 * expected to be an array of fingerprints for subkeys to change. It
3746
 * may also be an array with only the item "*" to indicate that all
3747
 * keys shall be set to that expiration date.
3748
 */
3749
void
3750
keyedit_quick_set_expire (ctrl_t ctrl, const char *fpr, const char *expirestr,
3751
                          char **subkeyfprs)
3752
0
{
3753
0
  gpg_error_t err;
3754
0
  kbnode_t keyblock, node;
3755
0
  KEYDB_HANDLE kdbhd;
3756
0
  int modified = 0;
3757
0
  PKT_public_key *pk;
3758
0
  u32 expire;
3759
0
  int primary_only = 0;
3760
0
  int idx;
3761
3762
#ifdef HAVE_W32_SYSTEM
3763
  /* See keyedit_menu for why we need this.  */
3764
  check_trustdb_stale (ctrl);
3765
#endif
3766
3767
  /* We require a fingerprint because only this uniquely identifies a
3768
   * key and may thus be used to select a key for unattended
3769
   * expiration setting.  */
3770
0
  err = find_by_primary_fpr (ctrl, fpr, &keyblock, &kdbhd);
3771
0
  if (err)
3772
0
    goto leave;
3773
3774
0
  if (fix_keyblock (ctrl, &keyblock))
3775
0
    modified++;
3776
3777
0
  pk = keyblock->pkt->pkt.public_key;
3778
0
  if (pk->flags.revoked)
3779
0
    {
3780
0
      if (!opt.verbose)
3781
0
        show_key_with_all_names (ctrl, es_stdout, keyblock, 0, 0, 0, 0, 0, 1);
3782
0
      log_error ("%s%s", _("Key is revoked."), "\n");
3783
0
      err = gpg_error (GPG_ERR_CERT_REVOKED);
3784
0
      goto leave;
3785
0
    }
3786
3787
0
  expire = parse_expire_string (expirestr);
3788
0
  if (expire == (u32)-1 )
3789
0
    {
3790
0
      log_error (_("'%s' is not a valid expiration time\n"), expirestr);
3791
0
      err = gpg_error (GPG_ERR_INV_VALUE);
3792
0
      goto leave;
3793
0
    }
3794
0
  if (expire)
3795
0
    expire += make_timestamp ();
3796
3797
  /* Check whether a subkey's expiration time shall be changed or the
3798
   * expiration time of all keys.  */
3799
0
  if (!subkeyfprs || !subkeyfprs[0])
3800
0
    primary_only = 1;
3801
0
  else if ( !strcmp (subkeyfprs[0], "*") && !subkeyfprs[1])
3802
0
    {
3803
      /* Change all subkeys keys which have not been revoked and are
3804
       * not yet expired.  */
3805
0
      merge_keys_and_selfsig (ctrl, keyblock);
3806
0
      for (node = keyblock; node; node = node->next)
3807
0
        {
3808
0
          if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
3809
0
              && (pk = node->pkt->pkt.public_key)
3810
0
              && !pk->flags.revoked
3811
0
              && !pk->has_expired)
3812
0
            node->flag |= NODFLG_SELKEY;
3813
0
        }
3814
0
    }
3815
0
  else
3816
0
    {
3817
      /* Change specified subkeys.  */
3818
0
      KEYDB_SEARCH_DESC desc;
3819
0
      byte fprbin[MAX_FINGERPRINT_LEN];
3820
0
      size_t fprlen;
3821
3822
0
      err = 0;
3823
0
      merge_keys_and_selfsig (ctrl, keyblock);
3824
0
      for (idx=0; subkeyfprs[idx]; idx++)
3825
0
        {
3826
0
          int any = 0;
3827
3828
          /* Parse the fingerprint.  */
3829
0
          if (classify_user_id (subkeyfprs[idx], &desc, 1)
3830
0
              || desc.mode != KEYDB_SEARCH_MODE_FPR)
3831
0
            {
3832
0
              log_error (_("\"%s\" is not a proper fingerprint\n"),
3833
0
                         subkeyfprs[idx] );
3834
0
              if (!err)
3835
0
                err = gpg_error (GPG_ERR_INV_NAME);
3836
0
              continue;
3837
0
            }
3838
3839
          /* Set the flag for the matching non revoked subkey.  */
3840
0
          for (node = keyblock; node; node = node->next)
3841
0
            {
3842
0
              if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
3843
0
                  && (pk = node->pkt->pkt.public_key)
3844
0
                  && !pk->flags.revoked )
3845
0
                {
3846
0
                  fingerprint_from_pk (pk, fprbin, &fprlen);
3847
0
                  if (fprlen == desc.fprlen && !memcmp (fprbin, desc.u.fpr, fprlen))
3848
0
                    {
3849
0
                      node->flag |= NODFLG_SELKEY;
3850
0
                      any = 1;
3851
0
                    }
3852
0
                }
3853
0
            }
3854
0
          if (!any)
3855
0
            {
3856
0
              log_error (_("subkey \"%s\" not found\n"), subkeyfprs[idx]);
3857
0
              if (!err)
3858
0
                err = gpg_error (GPG_ERR_NOT_FOUND);
3859
0
            }
3860
0
        }
3861
3862
0
      if (err)
3863
0
        goto leave;
3864
0
    }
3865
3866
  /* Set the new expiration date.  */
3867
0
  err = menu_expire (ctrl, keyblock, primary_only? 1 : 2, expire);
3868
0
  if (gpg_err_code (err) == GPG_ERR_TRUE)
3869
0
    modified = 1;
3870
0
  else if (err)
3871
0
    goto leave;
3872
0
  es_fflush (es_stdout);
3873
3874
  /* Store.  */
3875
0
  if (modified)
3876
0
    {
3877
0
      err = keydb_update_keyblock (ctrl, kdbhd, keyblock);
3878
0
      if (err)
3879
0
        {
3880
0
          log_error (_("update failed: %s\n"), gpg_strerror (err));
3881
0
          goto leave;
3882
0
        }
3883
0
      maybe_upload_key (ctrl, keyblock);
3884
0
      if (update_trust)
3885
0
        revalidation_mark (ctrl);
3886
0
    }
3887
0
  else
3888
0
    log_info (_("Key not changed so no update needed.\n"));
3889
3890
0
 leave:
3891
0
  release_kbnode (keyblock);
3892
0
  keydb_release (kdbhd);
3893
0
  if (err)
3894
0
    write_status_error ("set_expire", err);
3895
0
}
3896
3897
3898

3899
static void
3900
tty_print_notations (int indent, PKT_signature * sig)
3901
0
{
3902
0
  int first = 1;
3903
0
  struct notation *notation, *nd;
3904
3905
0
  if (indent < 0)
3906
0
    {
3907
0
      first = 0;
3908
0
      indent = -indent;
3909
0
    }
3910
3911
0
  notation = sig_to_notation (sig);
3912
3913
0
  for (nd = notation; nd; nd = nd->next)
3914
0
    {
3915
0
      if (!first)
3916
0
  tty_printf ("%*s", indent, "");
3917
0
      else
3918
0
  first = 0;
3919
3920
0
      tty_print_utf8_string (nd->name, strlen (nd->name));
3921
0
      tty_printf ("=");
3922
0
      tty_print_utf8_string (nd->value, strlen (nd->value));
3923
0
      tty_printf ("\n");
3924
0
    }
3925
3926
0
  free_notation (notation);
3927
0
}
3928
3929
3930
/*
3931
 * Show preferences of a public keyblock.
3932
 */
3933
static void
3934
show_prefs (PKT_user_id * uid, PKT_signature * selfsig, int verbose)
3935
0
{
3936
0
  if (!uid)
3937
0
    return;
3938
3939
0
  if (verbose)
3940
0
    {
3941
0
      show_preferences (uid, 4, -1, 1);
3942
3943
0
      if (selfsig)
3944
0
  {
3945
0
    const byte *pref_ks;
3946
0
    size_t pref_ks_len;
3947
3948
0
    pref_ks = parse_sig_subpkt (selfsig, 1,
3949
0
                                      SIGSUBPKT_PREF_KS, &pref_ks_len);
3950
0
    if (pref_ks && pref_ks_len)
3951
0
      {
3952
0
        tty_printf ("     ");
3953
0
        tty_printf (_("Preferred keyserver: "));
3954
0
        tty_print_utf8_string (pref_ks, pref_ks_len);
3955
0
        tty_printf ("\n");
3956
0
      }
3957
3958
0
    if (selfsig->flags.notation)
3959
0
      {
3960
0
        tty_printf ("     ");
3961
0
        tty_printf (_("Notations: "));
3962
0
        tty_print_notations (5 + strlen (_("Notations: ")), selfsig);
3963
0
      }
3964
0
  }
3965
0
    }
3966
0
  else
3967
0
    {
3968
0
      show_preferences (uid, 4, -1, 0);
3969
0
    }
3970
0
}
3971
3972
3973
/* This is the version of show_key_with_all_names used when
3974
   opt.with_colons is used.  It prints all available data in a easy to
3975
   parse format and does not translate utf8 */
3976
static void
3977
show_key_with_all_names_colon (ctrl_t ctrl, estream_t fp, kbnode_t keyblock)
3978
0
{
3979
0
  KBNODE node;
3980
0
  int i, j, ulti_hack = 0;
3981
0
  byte pk_version = 0;
3982
0
  PKT_public_key *primary = NULL;
3983
0
  int have_seckey;
3984
3985
0
  if (!fp)
3986
0
    fp = es_stdout;
3987
3988
  /* the keys */
3989
0
  for (node = keyblock; node; node = node->next)
3990
0
    {
3991
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
3992
0
    || (node->pkt->pkttype == PKT_PUBLIC_SUBKEY))
3993
0
  {
3994
0
    PKT_public_key *pk = node->pkt->pkt.public_key;
3995
0
    u32 keyid[2];
3996
3997
0
    if (node->pkt->pkttype == PKT_PUBLIC_KEY)
3998
0
      {
3999
0
        pk_version = pk->version;
4000
0
        primary = pk;
4001
0
      }
4002
4003
0
    keyid_from_pk (pk, keyid);
4004
0
          have_seckey = agent_probe_secret_key (ctrl, pk);
4005
4006
0
          if (node->pkt->pkttype == PKT_PUBLIC_KEY)
4007
0
            es_fputs (have_seckey? "sec:" : "pub:", fp);
4008
0
          else
4009
0
            es_fputs (have_seckey? "ssb:" : "sub:", fp);
4010
4011
0
    if (!pk->flags.valid)
4012
0
      es_putc ('i', fp);
4013
0
    else if (pk->flags.revoked)
4014
0
      es_putc ('r', fp);
4015
0
    else if (pk->has_expired)
4016
0
      es_putc ('e', fp);
4017
0
    else if (!(opt.fast_list_mode || opt.no_expensive_trust_checks))
4018
0
      {
4019
0
        int trust = get_validity_info (ctrl, keyblock, pk, NULL);
4020
0
        if (trust == 'u')
4021
0
    ulti_hack = 1;
4022
0
        es_putc (trust, fp);
4023
0
      }
4024
4025
0
    es_fprintf (fp, ":%u:%d:%08lX%08lX:%lu:%lu::",
4026
0
                      nbits_from_pk (pk),
4027
0
                      pk->pubkey_algo,
4028
0
                      (ulong) keyid[0], (ulong) keyid[1],
4029
0
                      (ulong) pk->timestamp, (ulong) pk->expiredate);
4030
0
    if (node->pkt->pkttype == PKT_PUBLIC_KEY
4031
0
        && !(opt.fast_list_mode || opt.no_expensive_trust_checks))
4032
0
      es_putc (get_ownertrust_info (ctrl, pk, 0), fp);
4033
0
    es_putc (':', fp);
4034
0
    es_putc (':', fp);
4035
0
    es_putc (':', fp);
4036
    /* Print capabilities.  */
4037
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_ENC))
4038
0
      es_putc ('e', fp);
4039
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_SIG))
4040
0
      es_putc ('s', fp);
4041
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_CERT))
4042
0
      es_putc ('c', fp);
4043
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_AUTH))
4044
0
      es_putc ('a', fp);
4045
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_RENC))
4046
0
      es_putc ('r', fp);
4047
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_TIME))
4048
0
      es_putc ('t', fp);
4049
0
    if ((pk->pubkey_usage & PUBKEY_USAGE_GROUP))
4050
0
      es_putc ('g', fp);
4051
0
    es_putc ('\n', fp);
4052
4053
0
    print_fingerprint (ctrl, fp, pk, 0);
4054
0
    print_revokers (fp, 1, pk);
4055
0
  }
4056
0
    }
4057
4058
  /* the user ids */
4059
0
  i = 0;
4060
0
  for (node = keyblock; node; node = node->next)
4061
0
    {
4062
0
      if (node->pkt->pkttype == PKT_USER_ID)
4063
0
  {
4064
0
    PKT_user_id *uid = node->pkt->pkt.user_id;
4065
4066
0
    ++i;
4067
4068
0
    if (uid->attrib_data)
4069
0
      es_fputs ("uat:", fp);
4070
0
    else
4071
0
      es_fputs ("uid:", fp);
4072
4073
0
    if (uid->flags.revoked)
4074
0
      es_fputs ("r::::::::", fp);
4075
0
    else if (uid->flags.expired)
4076
0
      es_fputs ("e::::::::", fp);
4077
0
    else if (opt.fast_list_mode || opt.no_expensive_trust_checks)
4078
0
      es_fputs ("::::::::", fp);
4079
0
    else
4080
0
      {
4081
0
        int uid_validity;
4082
4083
0
        if (primary && !ulti_hack)
4084
0
    uid_validity = get_validity_info (ctrl, keyblock, primary, uid);
4085
0
        else
4086
0
    uid_validity = 'u';
4087
0
        es_fprintf (fp, "%c::::::::", uid_validity);
4088
0
      }
4089
4090
0
    if (uid->attrib_data)
4091
0
      es_fprintf (fp, "%u %lu", uid->numattribs, uid->attrib_len);
4092
0
    else
4093
0
      es_write_sanitized (fp, uid->name, uid->len, ":", NULL);
4094
4095
0
    es_putc (':', fp);
4096
    /* signature class */
4097
0
    es_putc (':', fp);
4098
    /* capabilities */
4099
0
    es_putc (':', fp);
4100
    /* preferences */
4101
0
    if (pk_version > 3 || uid->selfsigversion > 3)
4102
0
      {
4103
0
        const prefitem_t *prefs = uid->prefs;
4104
4105
0
        for (j = 0; prefs && prefs[j].type; j++)
4106
0
    {
4107
0
      if (j)
4108
0
        es_putc (' ', fp);
4109
0
      es_fprintf (fp,
4110
0
                              "%c%d", prefs[j].type == PREFTYPE_SYM ? 'S' :
4111
0
                              prefs[j].type == PREFTYPE_HASH ? 'H' :
4112
0
                              prefs[j].type == PREFTYPE_ZIP ? 'Z' : '?',
4113
0
                              prefs[j].value);
4114
0
    }
4115
0
        if (uid->flags.mdc)
4116
0
    es_fputs (",mdc", fp);
4117
0
        if (uid->flags.aead)
4118
0
    es_fputs (",aead", fp);
4119
0
        if (!uid->flags.ks_modify)
4120
0
    es_fputs (",no-ks-modify", fp);
4121
0
      }
4122
0
    es_putc (':', fp);
4123
    /* flags */
4124
0
    es_fprintf (fp, "%d,", i);
4125
0
    if (uid->flags.primary)
4126
0
      es_putc ('p', fp);
4127
0
    if (uid->flags.revoked)
4128
0
      es_putc ('r', fp);
4129
0
    if (uid->flags.expired)
4130
0
      es_putc ('e', fp);
4131
0
    if ((node->flag & NODFLG_SELUID))
4132
0
      es_putc ('s', fp);
4133
0
    if ((node->flag & NODFLG_MARK_A))
4134
0
      es_putc ('m', fp);
4135
0
    es_putc (':', fp);
4136
0
    if (opt.trust_model == TM_TOFU || opt.trust_model == TM_TOFU_PGP)
4137
0
      {
4138
#ifdef USE_TOFU
4139
        enum tofu_policy policy;
4140
        if (! tofu_get_policy (ctrl, primary, uid, &policy)
4141
      && policy != TOFU_POLICY_NONE)
4142
    es_fprintf (fp, "%s", tofu_policy_str (policy));
4143
#endif /*USE_TOFU*/
4144
0
      }
4145
0
    es_putc (':', fp);
4146
0
    es_putc ('\n', fp);
4147
0
  }
4148
0
    }
4149
0
}
4150
4151
4152
static void
4153
show_names (ctrl_t ctrl, estream_t fp,
4154
            kbnode_t keyblock, PKT_public_key * pk, unsigned int flag,
4155
      int with_prefs)
4156
0
{
4157
0
  KBNODE node;
4158
0
  int i = 0;
4159
4160
0
  for (node = keyblock; node; node = node->next)
4161
0
    {
4162
0
      if (node->pkt->pkttype == PKT_USER_ID && !is_deleted_kbnode (node))
4163
0
  {
4164
0
    PKT_user_id *uid = node->pkt->pkt.user_id;
4165
0
    ++i;
4166
0
    if (!flag || (flag && (node->flag & flag)))
4167
0
      {
4168
0
        if (!(flag & NODFLG_MARK_A) && pk)
4169
0
    tty_fprintf (fp, "%s ", uid_trust_string_fixed (ctrl, pk, uid));
4170
4171
0
        if (flag & NODFLG_MARK_A)
4172
0
    tty_fprintf (fp, "     ");
4173
0
        else if (node->flag & NODFLG_SELUID)
4174
0
    tty_fprintf (fp, "(%d)* ", i);
4175
0
        else if (uid->flags.primary)
4176
0
    tty_fprintf (fp, "(%d). ", i);
4177
0
        else
4178
0
    tty_fprintf (fp, "(%d)  ", i);
4179
0
        tty_print_utf8_string2 (fp, uid->name, uid->len, 0);
4180
0
        tty_fprintf (fp, "\n");
4181
0
        if (with_prefs && pk)
4182
0
    {
4183
0
      if (pk->version > 3 || uid->selfsigversion > 3)
4184
0
        {
4185
0
          PKT_signature *selfsig = NULL;
4186
0
          KBNODE signode;
4187
4188
0
          for (signode = node->next;
4189
0
         signode && signode->pkt->pkttype == PKT_SIGNATURE;
4190
0
         signode = signode->next)
4191
0
      {
4192
0
        if (signode->pkt->pkt.signature->
4193
0
            flags.chosen_selfsig)
4194
0
          {
4195
0
            selfsig = signode->pkt->pkt.signature;
4196
0
            break;
4197
0
          }
4198
0
      }
4199
4200
0
          show_prefs (uid, selfsig, with_prefs == 2);
4201
0
        }
4202
0
      else
4203
0
        tty_fprintf (fp, _("There are no preferences on a"
4204
0
                                       " PGP 2.x-style user ID.\n"));
4205
0
    }
4206
0
      }
4207
0
  }
4208
0
    }
4209
0
}
4210
4211
4212
/*
4213
 * Display the key a the user ids, if only_marked is true, do only so
4214
 * for user ids with mark A flag set and do not display the index
4215
 * number.  If FP is not NULL print to the given stream and not to the
4216
 * tty (ignored in with-colons mode).
4217
 */
4218
static void
4219
show_key_with_all_names (ctrl_t ctrl, estream_t fp,
4220
                         KBNODE keyblock, int only_marked, int with_revoker,
4221
       int with_fpr, int with_subkeys, int with_prefs,
4222
                         int nowarn)
4223
0
{
4224
0
  gpg_error_t err;
4225
0
  kbnode_t node;
4226
0
  int i;
4227
0
  int do_warn = 0;
4228
0
  int have_seckey = 0;
4229
0
  char *serialno = NULL;
4230
0
  PKT_public_key *primary = NULL;
4231
0
  char pkstrbuf[PUBKEY_STRING_SIZE];
4232
4233
0
  if (opt.with_colons)
4234
0
    {
4235
0
      show_key_with_all_names_colon (ctrl, fp, keyblock);
4236
0
      return;
4237
0
    }
4238
4239
  /* the keys */
4240
0
  for (node = keyblock; node; node = node->next)
4241
0
    {
4242
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
4243
0
    || (with_subkeys && node->pkt->pkttype == PKT_PUBLIC_SUBKEY
4244
0
        && !is_deleted_kbnode (node)))
4245
0
  {
4246
0
    PKT_public_key *pk = node->pkt->pkt.public_key;
4247
0
    const char *otrust = "err";
4248
0
    const char *trust = "err";
4249
4250
0
    if (node->pkt->pkttype == PKT_PUBLIC_KEY)
4251
0
      {
4252
        /* do it here, so that debug messages don't clutter the
4253
         * output */
4254
0
        static int did_warn = 0;
4255
4256
0
        trust = get_validity_string (ctrl, pk, NULL);
4257
0
        otrust = get_ownertrust_string (ctrl, pk, 0);
4258
4259
        /* Show a warning once */
4260
0
        if (!did_warn
4261
0
      && (get_validity (ctrl, keyblock, pk, NULL, NULL, 0)
4262
0
          & TRUST_FLAG_PENDING_CHECK))
4263
0
    {
4264
0
      did_warn = 1;
4265
0
      do_warn = 1;
4266
0
    }
4267
4268
0
        primary = pk;
4269
0
      }
4270
4271
0
    if (pk->flags.revoked)
4272
0
      {
4273
0
        char *user = get_user_id_string_native (ctrl, pk->revoked.keyid);
4274
0
              tty_fprintf (fp,
4275
0
                           _("The following key was revoked on"
4276
0
                            " %s by %s key %s\n"),
4277
0
        revokestr_from_pk (pk),
4278
0
                          openpgp_pk_algo_name (pk->revoked.algo), user);
4279
0
        xfree (user);
4280
0
      }
4281
4282
0
    if (with_revoker)
4283
0
      {
4284
0
        if (!pk->revkey && pk->numrevkeys)
4285
0
    BUG ();
4286
0
        else
4287
0
    for (i = 0; i < pk->numrevkeys; i++)
4288
0
      {
4289
0
        u32 r_keyid[2];
4290
0
        char *user;
4291
0
        const char *algo;
4292
4293
0
        algo = openpgp_pk_algo_name (pk->revkey[i].algid);
4294
0
        keyid_from_fingerprint (ctrl, pk->revkey[i].fpr,
4295
0
              pk->revkey[i].fprlen, r_keyid);
4296
4297
0
        user = get_user_id_string_native (ctrl, r_keyid);
4298
0
        tty_fprintf (fp,
4299
0
                                 _("This key may be revoked by %s key %s"),
4300
0
                                 algo, user);
4301
4302
0
        if (pk->revkey[i].class & 0x40)
4303
0
          {
4304
0
      tty_fprintf (fp, " ");
4305
0
      tty_fprintf (fp, _("(sensitive)"));
4306
0
          }
4307
4308
0
        tty_fprintf (fp, "\n");
4309
0
        xfree (user);
4310
0
      }
4311
0
      }
4312
4313
0
    keyid_from_pk (pk, NULL);
4314
4315
0
          xfree (serialno);
4316
0
          serialno = NULL;
4317
0
          {
4318
0
            char *hexgrip;
4319
4320
0
            err = hexkeygrip_from_pk (pk, &hexgrip);
4321
0
            if (err)
4322
0
              {
4323
0
                log_error ("error computing a keygrip: %s\n",
4324
0
                           gpg_strerror (err));
4325
0
                have_seckey = 0;
4326
0
              }
4327
0
            else
4328
0
              have_seckey = !agent_get_keyinfo (ctrl, hexgrip, &serialno, NULL);
4329
0
            xfree (hexgrip);
4330
0
          }
4331
4332
0
    tty_fprintf
4333
0
            (fp, "%s%c %s/%s",
4334
0
             node->pkt->pkttype == PKT_PUBLIC_KEY && have_seckey? "sec" :
4335
0
             node->pkt->pkttype == PKT_PUBLIC_KEY ?               "pub" :
4336
0
             have_seckey ?                                        "ssb" :
4337
0
                                                                  "sub",
4338
0
             (node->flag & NODFLG_SELKEY) ? '*' : ' ',
4339
0
             pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
4340
0
             keystr (pk->keyid));
4341
4342
0
          if (opt.legacy_list_mode)
4343
0
            tty_fprintf (fp, "  ");
4344
0
          else
4345
0
            tty_fprintf (fp, "\n     ");
4346
4347
0
          tty_fprintf (fp, _("created: %s"), datestr_from_pk (pk));
4348
0
    tty_fprintf (fp, "  ");
4349
0
    if (pk->flags.revoked)
4350
0
      tty_fprintf (fp, _("revoked: %s"), revokestr_from_pk (pk));
4351
0
    else if (pk->has_expired)
4352
0
      tty_fprintf (fp, _("expired: %s"), expirestr_from_pk (pk));
4353
0
    else
4354
0
      tty_fprintf (fp, _("expires: %s"), expirestr_from_pk (pk));
4355
0
    tty_fprintf (fp, "  ");
4356
0
    tty_fprintf (fp, _("usage: %s"), usagestr_from_pk (pk, 1));
4357
0
    tty_fprintf (fp, "\n");
4358
4359
0
          if (serialno)
4360
0
            {
4361
              /* The agent told us that a secret key is available and
4362
                 that it has been stored on a card.  */
4363
0
        tty_fprintf (fp, "%*s%s", opt.legacy_list_mode? 21:5, "",
4364
0
                           _("card-no: "));
4365
0
              if (strlen (serialno) == 32
4366
0
                  && !strncmp (serialno, "D27600012401", 12))
4367
0
                {
4368
                  /* This is an OpenPGP card.  Print the relevant part.  */
4369
                  /* Example: D2760001240101010001000003470000 */
4370
                  /*                          xxxxyyyyyyyy     */
4371
0
                  tty_fprintf (fp, "%.*s %.*s\n",
4372
0
                               4, serialno+16, 8, serialno+20);
4373
0
                }
4374
0
              else
4375
0
                tty_fprintf (fp, "%s\n", serialno);
4376
4377
0
            }
4378
0
    else if (pk->seckey_info
4379
0
              && pk->seckey_info->is_protected
4380
0
              && pk->seckey_info->s2k.mode == 1002)
4381
0
      {
4382
              /* FIXME: Check whether this code path is still used.  */
4383
0
        tty_fprintf (fp, "%*s%s", opt.legacy_list_mode? 21:5, "",
4384
0
                           _("card-no: "));
4385
0
        if (pk->seckey_info->ivlen == 16
4386
0
      && !memcmp (pk->seckey_info->iv,
4387
0
                              "\xD2\x76\x00\x01\x24\x01", 6))
4388
0
    {
4389
                  /* This is an OpenPGP card. */
4390
0
      for (i = 8; i < 14; i++)
4391
0
        {
4392
0
          if (i == 10)
4393
0
      tty_fprintf (fp, " ");
4394
0
          tty_fprintf (fp, "%02X", pk->seckey_info->iv[i]);
4395
0
        }
4396
0
    }
4397
0
        else
4398
0
    {
4399
                  /* Unknown card: Print all. */
4400
0
      for (i = 0; i < pk->seckey_info->ivlen; i++)
4401
0
        tty_fprintf (fp, "%02X", pk->seckey_info->iv[i]);
4402
0
    }
4403
0
        tty_fprintf (fp, "\n");
4404
0
      }
4405
4406
0
    if (node->pkt->pkttype == PKT_PUBLIC_KEY
4407
0
              || node->pkt->pkttype == PKT_SECRET_KEY)
4408
0
      {
4409
0
        if (opt.trust_model != TM_ALWAYS)
4410
0
    {
4411
0
      tty_fprintf (fp, "%*s",
4412
0
                               opt.legacy_list_mode?
4413
0
                               ((int) keystrlen () + 13):5, "");
4414
      /* Ownertrust is only meaningful for the PGP or
4415
         classic trust models, or PGP combined with TOFU */
4416
0
      if (opt.trust_model == TM_PGP
4417
0
          || opt.trust_model == TM_CLASSIC
4418
0
          || opt.trust_model == TM_TOFU_PGP)
4419
0
        {
4420
0
          int width = 14 - strlen (otrust);
4421
0
          if (width <= 0)
4422
0
      width = 1;
4423
0
          tty_fprintf (fp, _("trust: %s"), otrust);
4424
0
          tty_fprintf (fp, "%*s", width, "");
4425
0
        }
4426
4427
0
      tty_fprintf (fp, _("validity: %s"), trust);
4428
0
      tty_fprintf (fp, "\n");
4429
0
    }
4430
0
        if (node->pkt->pkttype == PKT_PUBLIC_KEY
4431
0
      && (get_ownertrust (ctrl, pk) & TRUST_FLAG_DISABLED))
4432
0
    {
4433
0
      tty_fprintf (fp, "*** ");
4434
0
      tty_fprintf (fp, _("This key has been disabled"));
4435
0
      tty_fprintf (fp, "\n");
4436
0
    }
4437
0
      }
4438
4439
0
    if ((node->pkt->pkttype == PKT_PUBLIC_KEY
4440
0
               || node->pkt->pkttype == PKT_SECRET_KEY) && with_fpr)
4441
0
      {
4442
0
              print_fingerprint (ctrl, fp, pk, 2);
4443
0
        tty_fprintf (fp, "\n");
4444
0
      }
4445
0
  }
4446
0
    }
4447
4448
0
  show_names (ctrl, fp,
4449
0
              keyblock, primary, only_marked ? NODFLG_MARK_A : 0, with_prefs);
4450
4451
0
  if (do_warn && !nowarn)
4452
0
    tty_fprintf (fp, _("Please note that the shown key validity"
4453
0
                       " is not necessarily correct\n"
4454
0
                       "unless you restart the program.\n"));
4455
4456
0
  xfree (serialno);
4457
0
}
4458
4459
4460
/* Display basic key information.  This function is suitable to show
4461
 * information on the key without any dependencies on the trustdb or
4462
 * any other internal GnuPG stuff.  KEYBLOCK may either be a public or
4463
 * a secret key.  This function may be called with KEYBLOCK containing
4464
 * secret keys and thus the printing of "pub" vs. "sec" does only
4465
 * depend on the packet type and not by checking with gpg-agent.  If
4466
 * PRINT_SEC is set "sec" is printed instead of "pub".  */
4467
void
4468
show_basic_key_info (ctrl_t ctrl, kbnode_t keyblock, int print_sec)
4469
0
{
4470
0
  KBNODE node;
4471
0
  int i;
4472
0
  char pkstrbuf[PUBKEY_STRING_SIZE];
4473
4474
  /* The primary key */
4475
0
  for (node = keyblock; node; node = node->next)
4476
0
    {
4477
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
4478
0
          || node->pkt->pkttype == PKT_SECRET_KEY)
4479
0
  {
4480
0
    PKT_public_key *pk = node->pkt->pkt.public_key;
4481
0
          const char *tag;
4482
4483
0
          if (node->pkt->pkttype == PKT_SECRET_KEY || print_sec)
4484
0
            tag = "sec";
4485
0
          else
4486
0
            tag = "pub";
4487
4488
    /* Note, we use the same format string as in other show
4489
       functions to make the translation job easier. */
4490
0
    tty_printf ("%s  %s/%s  ",
4491
0
                      tag,
4492
0
                      pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
4493
0
          keystr_from_pk (pk));
4494
0
    tty_printf (_("created: %s"), datestr_from_pk (pk));
4495
0
    tty_printf ("  ");
4496
0
    tty_printf (_("expires: %s"), expirestr_from_pk (pk));
4497
0
    tty_printf ("\n");
4498
0
    print_fingerprint (ctrl, NULL, pk, 3);
4499
0
    tty_printf ("\n");
4500
0
  }
4501
0
    }
4502
4503
  /* The user IDs. */
4504
0
  (void)i; /* Counting User IDs */
4505
0
  for (i = 0, node = keyblock; node; node = node->next)
4506
0
    {
4507
0
      if (node->pkt->pkttype == PKT_USER_ID)
4508
0
  {
4509
0
    PKT_user_id *uid = node->pkt->pkt.user_id;
4510
0
    ++i;
4511
4512
0
    tty_printf ("     ");
4513
0
    if (uid->flags.revoked)
4514
0
      tty_printf ("[%s] ", _("revoked"));
4515
0
    else if (uid->flags.expired)
4516
0
      tty_printf ("[%s] ", _("expired"));
4517
0
    tty_print_utf8_string (uid->name, uid->len);
4518
0
    tty_printf ("\n");
4519
0
  }
4520
0
    }
4521
0
}
4522
4523
4524
static void
4525
show_key_and_fingerprint (ctrl_t ctrl, kbnode_t keyblock, int with_subkeys)
4526
0
{
4527
0
  kbnode_t node;
4528
0
  PKT_public_key *pk = NULL;
4529
0
  char pkstrbuf[PUBKEY_STRING_SIZE];
4530
4531
0
  for (node = keyblock; node; node = node->next)
4532
0
    {
4533
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
4534
0
  {
4535
0
    pk = node->pkt->pkt.public_key;
4536
0
    tty_printf ("pub   %s/%s %s ",
4537
0
                      pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
4538
0
                      keystr_from_pk(pk),
4539
0
                      datestr_from_pk (pk));
4540
0
  }
4541
0
      else if (node->pkt->pkttype == PKT_USER_ID)
4542
0
  {
4543
0
    PKT_user_id *uid = node->pkt->pkt.user_id;
4544
0
    tty_print_utf8_string (uid->name, uid->len);
4545
0
    break;
4546
0
  }
4547
0
    }
4548
0
  tty_printf ("\n");
4549
0
  if (pk)
4550
0
    print_fingerprint (ctrl, NULL, pk, 2);
4551
0
  if (with_subkeys)
4552
0
    {
4553
0
      for (node = keyblock; node; node = node->next)
4554
0
        {
4555
0
          if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
4556
0
            {
4557
0
              pk = node->pkt->pkt.public_key;
4558
0
              tty_printf ("sub   %s/%s %s [%s]\n",
4559
0
                          pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
4560
0
                          keystr_from_pk(pk),
4561
0
                          datestr_from_pk (pk),
4562
0
                          usagestr_from_pk (pk, 0));
4563
4564
0
              print_fingerprint (ctrl, NULL, pk, 4);
4565
0
            }
4566
0
        }
4567
0
    }
4568
0
}
4569
4570
4571
/* Show a listing of the primary and its subkeys along with their
4572
   keygrips.  */
4573
static void
4574
show_key_and_grip (kbnode_t keyblock)
4575
0
{
4576
0
  kbnode_t node;
4577
0
  PKT_public_key *pk = NULL;
4578
0
  char pkstrbuf[PUBKEY_STRING_SIZE];
4579
0
  char *hexgrip;
4580
4581
0
  for (node = keyblock; node; node = node->next)
4582
0
    {
4583
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
4584
0
          || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
4585
0
        {
4586
0
          pk = node->pkt->pkt.public_key;
4587
0
          tty_printf ("%s   %s/%s %s [%s]\n",
4588
0
                      node->pkt->pkttype == PKT_PUBLIC_KEY? "pub":"sub",
4589
0
                      pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
4590
0
                      keystr_from_pk(pk),
4591
0
                      datestr_from_pk (pk),
4592
0
                      usagestr_from_pk (pk, 0));
4593
4594
0
          if (!hexkeygrip_from_pk (pk, &hexgrip))
4595
0
            {
4596
0
              tty_printf ("      Keygrip: %s\n", hexgrip);
4597
0
              xfree (hexgrip);
4598
0
            }
4599
0
        }
4600
0
    }
4601
0
}
4602
4603
4604
/* Show a warning if no uids on the key have the primary uid flag
4605
   set. */
4606
static void
4607
no_primary_warning (KBNODE keyblock)
4608
0
{
4609
0
  KBNODE node;
4610
0
  int have_primary = 0, uid_count = 0;
4611
4612
  /* TODO: if we ever start behaving differently with a primary or
4613
     non-primary attribute ID, we will need to check for attributes
4614
     here as well. */
4615
4616
0
  for (node = keyblock; node; node = node->next)
4617
0
    {
4618
0
      if (node->pkt->pkttype == PKT_USER_ID
4619
0
    && node->pkt->pkt.user_id->attrib_data == NULL)
4620
0
  {
4621
0
    uid_count++;
4622
4623
0
    if (node->pkt->pkt.user_id->flags.primary == 2)
4624
0
      {
4625
0
        have_primary = 1;
4626
0
        break;
4627
0
      }
4628
0
  }
4629
0
    }
4630
4631
0
  if (uid_count > 1 && !have_primary)
4632
0
    log_info (_
4633
0
        ("WARNING: no user ID has been marked as primary.  This command"
4634
0
         " may\n              cause a different user ID to become"
4635
0
         " the assumed primary.\n"));
4636
0
}
4637
4638
4639
/* Print a warning if the latest encryption subkey expires soon.  This
4640
   function is called after the expire data of the primary key has
4641
   been changed.  */
4642
static void
4643
subkey_expire_warning (kbnode_t keyblock)
4644
0
{
4645
0
  u32 curtime = make_timestamp ();
4646
0
  kbnode_t node;
4647
0
  PKT_public_key *pk;
4648
  /* u32 mainexpire = 0; */
4649
0
  u32 subexpire = 0;
4650
0
  u32 latest_date = 0;
4651
4652
0
  for (node = keyblock; node; node = node->next)
4653
0
    {
4654
      /* if (node->pkt->pkttype == PKT_PUBLIC_KEY) */
4655
      /*   { */
4656
      /*     pk = node->pkt->pkt.public_key; */
4657
      /*     mainexpire = pk->expiredate; */
4658
      /*   } */
4659
4660
0
      if (node->pkt->pkttype != PKT_PUBLIC_SUBKEY)
4661
0
        continue;
4662
0
      pk = node->pkt->pkt.public_key;
4663
4664
0
      if (!pk->flags.valid)
4665
0
        continue;
4666
0
      if (pk->flags.revoked)
4667
0
        continue;
4668
0
      if (pk->timestamp > curtime)
4669
0
        continue; /* Ignore future keys.  */
4670
0
      if (!(pk->pubkey_usage & PUBKEY_USAGE_ENC))
4671
0
        continue; /* Not an encryption key.  */
4672
4673
0
      if (pk->timestamp > latest_date || (!pk->timestamp && !latest_date))
4674
0
        {
4675
0
          latest_date = pk->timestamp;
4676
0
          subexpire = pk->expiredate;
4677
0
        }
4678
0
    }
4679
4680
0
  if (!subexpire)
4681
0
    return;  /* No valid subkey with an expiration time.  */
4682
4683
0
  if (curtime + (10*86400) > subexpire)
4684
0
    {
4685
0
      log_info (_("WARNING: Your encryption subkey expires soon.\n"));
4686
0
      log_info (_("You may want to change its expiration date too.\n"));
4687
0
    }
4688
0
}
4689
4690
4691
/* Print a warning if all encryption (sub|primary)keys are expired.
4692
 * The warning is not printed if there is no encryption
4693
 * (sub|primary)key at all.  This function is called after the expire
4694
 * data of the primary key has been changed.  */
4695
void
4696
no_usable_encr_subkeys_warning (kbnode_t keyblock)
4697
0
{
4698
0
  kbnode_t node;
4699
0
  PKT_public_key *pk;
4700
0
  int any_encr_key = 0;
4701
4702
0
  for (node = keyblock; node; node = node->next)
4703
0
    {
4704
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY
4705
0
          || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
4706
0
        {
4707
0
          pk = node->pkt->pkt.public_key;
4708
0
          if ((pk->pubkey_usage & PUBKEY_USAGE_ENC))
4709
0
            {
4710
0
              any_encr_key = 1;
4711
0
              if (pk->flags.valid && !pk->has_expired && !pk->flags.revoked
4712
0
                  && !pk->flags.disabled)
4713
0
                {
4714
0
                  return; /* Key is usable for encryption  */
4715
0
                }
4716
0
            }
4717
0
        }
4718
0
    }
4719
4720
0
  if (any_encr_key && !opt.quiet)
4721
0
    log_info (_("WARNING: No valid encryption subkey left over.\n"));
4722
0
}
4723
4724
4725
/*
4726
 * Ask for a new user id, add the self-signature, and update the
4727
 * keyblock.  If UIDSTRING is not NULL the user ID is generated
4728
 * unattended using that string.  UIDSTRING is expected to be utf-8
4729
 * encoded and white space trimmed.  Returns true if there is a new
4730
 * user id.
4731
 */
4732
static int
4733
menu_adduid (ctrl_t ctrl, kbnode_t pub_keyblock,
4734
             int photo, const char *photo_name, const char *uidstring)
4735
0
{
4736
0
  PKT_user_id *uid;
4737
0
  PKT_public_key *pk = NULL;
4738
0
  PKT_signature *sig = NULL;
4739
0
  PACKET *pkt;
4740
0
  KBNODE node;
4741
0
  KBNODE pub_where = NULL;
4742
0
  gpg_error_t err;
4743
4744
0
  if (photo && uidstring)
4745
0
    return 0;  /* Not allowed.  */
4746
4747
0
  for (node = pub_keyblock; node; pub_where = node, node = node->next)
4748
0
    {
4749
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
4750
0
  pk = node->pkt->pkt.public_key;
4751
0
      else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
4752
0
  break;
4753
0
    }
4754
0
  if (!node) /* No subkey.  */
4755
0
    pub_where = NULL;
4756
0
  log_assert (pk);
4757
4758
0
  if (photo)
4759
0
    {
4760
0
      int hasattrib = 0;
4761
4762
0
      for (node = pub_keyblock; node; node = node->next)
4763
0
  if (node->pkt->pkttype == PKT_USER_ID &&
4764
0
      node->pkt->pkt.user_id->attrib_data != NULL)
4765
0
    {
4766
0
      hasattrib = 1;
4767
0
      break;
4768
0
    }
4769
4770
      /* It is legal but bad for compatibility to add a photo ID to a
4771
         v3 key as it means that PGP2 will not be able to use that key
4772
         anymore.  Also, PGP may not expect a photo on a v3 key.
4773
         Don't bother to ask this if the key already has a photo - any
4774
         damage has already been done at that point. -dms */
4775
0
      if (pk->version == 3 && !hasattrib)
4776
0
  {
4777
0
    if (opt.expert)
4778
0
      {
4779
0
        tty_printf (_("WARNING: This is a PGP2-style key.  "
4780
0
          "Adding a photo ID may cause some versions\n"
4781
0
          "         of PGP to reject this key.\n"));
4782
4783
0
        if (!cpr_get_answer_is_yes ("keyedit.v3_photo.okay",
4784
0
            _("Are you sure you still want "
4785
0
              "to add it? (y/N) ")))
4786
0
    return 0;
4787
0
      }
4788
0
    else
4789
0
      {
4790
0
        tty_printf (_("You may not add a photo ID to "
4791
0
          "a PGP2-style key.\n"));
4792
0
        return 0;
4793
0
      }
4794
0
  }
4795
4796
0
      uid = generate_photo_id (ctrl, pk, photo_name);
4797
0
    }
4798
0
  else
4799
0
    uid = generate_user_id (pub_keyblock, uidstring);
4800
0
  if (!uid)
4801
0
    {
4802
0
      if (uidstring)
4803
0
        {
4804
0
          write_status_error ("adduid", gpg_error (304));
4805
0
          log_error ("%s\n", _("Such a user ID already exists on this key!"));
4806
0
        }
4807
0
      return 0;
4808
0
    }
4809
4810
0
  err = make_keysig_packet (ctrl, &sig, pk, uid, NULL, pk, 0x13, 0, 0,
4811
0
                            keygen_add_std_prefs, pk, NULL);
4812
0
  if (err)
4813
0
    {
4814
0
      write_status_error ("keysig", err);
4815
0
      log_error ("signing failed: %s\n", gpg_strerror (err));
4816
0
      free_user_id (uid);
4817
0
      return 0;
4818
0
    }
4819
4820
  /* Insert/append to public keyblock */
4821
0
  pkt = xmalloc_clear (sizeof *pkt);
4822
0
  pkt->pkttype = PKT_USER_ID;
4823
0
  pkt->pkt.user_id = uid;
4824
0
  node = new_kbnode (pkt);
4825
0
  if (pub_where)
4826
0
    insert_kbnode (pub_where, node, 0);
4827
0
  else
4828
0
    add_kbnode (pub_keyblock, node);
4829
0
  pkt = xmalloc_clear (sizeof *pkt);
4830
0
  pkt->pkttype = PKT_SIGNATURE;
4831
0
  pkt->pkt.signature = sig;
4832
0
  if (pub_where)
4833
0
    insert_kbnode (node, new_kbnode (pkt), 0);
4834
0
  else
4835
0
    add_kbnode (pub_keyblock, new_kbnode (pkt));
4836
0
  return 1;
4837
0
}
4838
4839
4840
/*
4841
 * Remove all selected userids from the keyring
4842
 */
4843
static void
4844
menu_deluid (KBNODE pub_keyblock)
4845
0
{
4846
0
  KBNODE node;
4847
0
  int selected = 0;
4848
4849
0
  for (node = pub_keyblock; node; node = node->next)
4850
0
    {
4851
0
      if (node->pkt->pkttype == PKT_USER_ID)
4852
0
  {
4853
0
    selected = node->flag & NODFLG_SELUID;
4854
0
    if (selected)
4855
0
      {
4856
        /* Only cause a trust update if we delete a
4857
           non-revoked user id */
4858
0
        if (!node->pkt->pkt.user_id->flags.revoked)
4859
0
    update_trust = 1;
4860
0
        delete_kbnode (node);
4861
0
      }
4862
0
  }
4863
0
      else if (selected && node->pkt->pkttype == PKT_SIGNATURE)
4864
0
  delete_kbnode (node);
4865
0
      else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
4866
0
  selected = 0;
4867
0
    }
4868
0
  commit_kbnode (&pub_keyblock);
4869
0
}
4870
4871
4872
static int
4873
menu_delsig (ctrl_t ctrl, kbnode_t pub_keyblock)
4874
0
{
4875
0
  KBNODE node;
4876
0
  PKT_user_id *uid = NULL;
4877
0
  int changed = 0;
4878
4879
0
  for (node = pub_keyblock; node; node = node->next)
4880
0
    {
4881
0
      if (node->pkt->pkttype == PKT_USER_ID)
4882
0
  {
4883
0
    uid = (node->flag & NODFLG_SELUID) ? node->pkt->pkt.user_id : NULL;
4884
0
  }
4885
0
      else if (uid && node->pkt->pkttype == PKT_SIGNATURE)
4886
0
  {
4887
0
    int okay, valid, selfsig, inv_sig, no_key, other_err;
4888
4889
0
    tty_printf ("uid  ");
4890
0
    tty_print_utf8_string (uid->name, uid->len);
4891
0
    tty_printf ("\n");
4892
4893
0
    okay = inv_sig = no_key = other_err = 0;
4894
0
    if (opt.with_colons)
4895
0
      valid = print_and_check_one_sig_colon (ctrl, pub_keyblock, node,
4896
0
               &inv_sig, &no_key,
4897
0
               &other_err, &selfsig, 1);
4898
0
    else
4899
0
      valid = print_and_check_one_sig (ctrl, pub_keyblock, node,
4900
0
               &inv_sig, &no_key, &other_err,
4901
0
               &selfsig, 1, 0);
4902
4903
0
    if (valid)
4904
0
      {
4905
0
        okay = cpr_get_answer_yes_no_quit
4906
0
                ("keyedit.delsig.valid",
4907
0
                 _("Delete this good signature? (y/N/q)"));
4908
4909
        /* Only update trust if we delete a good signature.
4910
           The other two cases do not affect trust. */
4911
0
        if (okay)
4912
0
    update_trust = 1;
4913
0
      }
4914
0
    else if (inv_sig || other_err)
4915
0
      okay = cpr_get_answer_yes_no_quit
4916
0
              ("keyedit.delsig.invalid",
4917
0
               _("Delete this invalid signature? (y/N/q)"));
4918
0
    else if (no_key)
4919
0
      okay = cpr_get_answer_yes_no_quit
4920
0
              ("keyedit.delsig.unknown",
4921
0
               _("Delete this unknown signature? (y/N/q)"));
4922
4923
0
    if (okay == -1)
4924
0
      break;
4925
0
    if (okay && selfsig
4926
0
        && !cpr_get_answer_is_yes
4927
0
              ("keyedit.delsig.selfsig",
4928
0
               _("Really delete this self-signature? (y/N)")))
4929
0
      okay = 0;
4930
0
    if (okay)
4931
0
      {
4932
0
        delete_kbnode (node);
4933
0
        changed++;
4934
0
      }
4935
4936
0
  }
4937
0
      else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
4938
0
  uid = NULL;
4939
0
    }
4940
4941
0
  if (changed)
4942
0
    {
4943
0
      commit_kbnode (&pub_keyblock);
4944
0
      tty_printf (ngettext("Deleted %d signature.\n",
4945
0
                           "Deleted %d signatures.\n", changed), changed);
4946
0
    }
4947
0
  else
4948
0
    tty_printf (_("Nothing deleted.\n"));
4949
4950
0
  return changed;
4951
0
}
4952
4953
4954
/* Note: OPTIONS are from the EXPORT_* set. */
4955
static int
4956
menu_clean (ctrl_t ctrl, kbnode_t keyblock, unsigned int options)
4957
0
{
4958
0
  KBNODE uidnode;
4959
0
  int modified = 0;
4960
0
  int select_all = !count_selected_uids (keyblock);
4961
4962
0
  for (uidnode = keyblock->next;
4963
0
       uidnode && uidnode->pkt->pkttype != PKT_PUBLIC_SUBKEY;
4964
0
       uidnode = uidnode->next)
4965
0
    {
4966
0
      if (uidnode->pkt->pkttype == PKT_USER_ID
4967
0
    && (uidnode->flag & NODFLG_SELUID || select_all))
4968
0
  {
4969
0
    int uids = 0, sigs = 0;
4970
0
    char *user = utf8_to_native (uidnode->pkt->pkt.user_id->name,
4971
0
               uidnode->pkt->pkt.user_id->len,
4972
0
               0);
4973
4974
0
    clean_one_uid (ctrl, keyblock, uidnode, opt.verbose, options,
4975
0
                         &uids, &sigs);
4976
0
    if (uids)
4977
0
      {
4978
0
        const char *reason;
4979
4980
0
        if (uidnode->pkt->pkt.user_id->flags.revoked)
4981
0
    reason = _("revoked");
4982
0
        else if (uidnode->pkt->pkt.user_id->flags.expired)
4983
0
    reason = _("expired");
4984
0
        else
4985
0
    reason = _("invalid");
4986
4987
0
        tty_printf (_("User ID \"%s\" compacted: %s\n"), user, reason);
4988
4989
0
        modified = 1;
4990
0
      }
4991
0
    else if (sigs)
4992
0
      {
4993
0
        tty_printf (ngettext("User ID \"%s\": %d signature removed\n",
4994
0
                                   "User ID \"%s\": %d signatures removed\n",
4995
0
                                   sigs), user, sigs);
4996
0
        modified = 1;
4997
0
      }
4998
0
    else
4999
0
      {
5000
0
        tty_printf ((options & EXPORT_MINIMAL)?
5001
0
        _("User ID \"%s\": already minimized\n") :
5002
0
        _("User ID \"%s\": already clean\n"), user);
5003
0
      }
5004
5005
0
    xfree (user);
5006
0
  }
5007
0
    }
5008
5009
0
  return modified;
5010
0
}
5011
5012
5013
/*
5014
 * Remove some of the secondary keys
5015
 */
5016
static void
5017
menu_delkey (KBNODE pub_keyblock)
5018
0
{
5019
0
  KBNODE node;
5020
0
  int selected = 0;
5021
5022
0
  for (node = pub_keyblock; node; node = node->next)
5023
0
    {
5024
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5025
0
  {
5026
0
    selected = node->flag & NODFLG_SELKEY;
5027
0
    if (selected)
5028
0
            delete_kbnode (node);
5029
0
  }
5030
0
      else if (selected && node->pkt->pkttype == PKT_SIGNATURE)
5031
0
  delete_kbnode (node);
5032
0
      else
5033
0
  selected = 0;
5034
0
    }
5035
0
  commit_kbnode (&pub_keyblock);
5036
5037
  /* No need to set update_trust here since signing keys are no
5038
     longer used to certify other keys, so there is no change in
5039
     trust when revoking/removing them.   */
5040
0
}
5041
5042
5043
/*
5044
 * Ask for a new revoker, create the self-signature and put it into
5045
 * the keyblock.  Returns true if there is a new revoker.
5046
 */
5047
static int
5048
menu_addrevoker (ctrl_t ctrl, kbnode_t pub_keyblock, int sensitive)
5049
0
{
5050
0
  PKT_public_key *pk = NULL;
5051
0
  PKT_public_key *revoker_pk = NULL;
5052
0
  PKT_signature *sig = NULL;
5053
0
  PACKET *pkt;
5054
0
  struct revocation_key revkey;
5055
0
  size_t fprlen;
5056
0
  int rc;
5057
5058
0
  log_assert (pub_keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
5059
5060
0
  pk = pub_keyblock->pkt->pkt.public_key;
5061
5062
0
  if (pk->numrevkeys == 0 && pk->version == 3)
5063
0
    {
5064
      /* It is legal but bad for compatibility to add a revoker to a
5065
         v3 key as it means that PGP2 will not be able to use that key
5066
         anymore.  Also, PGP may not expect a revoker on a v3 key.
5067
         Don't bother to ask this if the key already has a revoker -
5068
         any damage has already been done at that point. -dms */
5069
0
      if (opt.expert)
5070
0
  {
5071
0
    tty_printf (_("WARNING: This is a PGP 2.x-style key.  "
5072
0
      "Adding a designated revoker may cause\n"
5073
0
      "         some versions of PGP to reject this key.\n"));
5074
5075
0
    if (!cpr_get_answer_is_yes ("keyedit.v3_revoker.okay",
5076
0
              _("Are you sure you still want "
5077
0
          "to add it? (y/N) ")))
5078
0
      return 0;
5079
0
  }
5080
0
      else
5081
0
  {
5082
0
    tty_printf (_("You may not add a designated revoker to "
5083
0
      "a PGP 2.x-style key.\n"));
5084
0
    return 0;
5085
0
  }
5086
0
    }
5087
5088
0
  for (;;)
5089
0
    {
5090
0
      char *answer;
5091
5092
0
      free_public_key (revoker_pk);
5093
0
      revoker_pk = xmalloc_clear (sizeof (*revoker_pk));
5094
5095
0
      tty_printf ("\n");
5096
5097
0
      answer = cpr_get_utf8
5098
0
        ("keyedit.add_revoker",
5099
0
         _("Enter the user ID of the designated revoker: "));
5100
0
      if (answer[0] == '\0' || answer[0] == CONTROL_D)
5101
0
  {
5102
0
    xfree (answer);
5103
0
    goto fail;
5104
0
  }
5105
5106
      /* Note that I'm requesting CERT here, which usually implies
5107
         primary keys only, but some casual testing shows that PGP and
5108
         GnuPG both can handle a designated revocation from a subkey. */
5109
0
      revoker_pk->req_usage = PUBKEY_USAGE_CERT;
5110
0
      rc = get_pubkey_byname (ctrl, GET_PUBKEY_NO_AKL,
5111
0
                              NULL, revoker_pk, answer, NULL, NULL, 1);
5112
0
      if (rc)
5113
0
  {
5114
0
    log_error (_("key \"%s\" not found: %s\n"), answer,
5115
0
         gpg_strerror (rc));
5116
0
    xfree (answer);
5117
0
    continue;
5118
0
  }
5119
5120
0
      xfree (answer);
5121
5122
0
      fingerprint_from_pk (revoker_pk, revkey.fpr, &fprlen);
5123
0
      if (fprlen != 20 && fprlen != 32)
5124
0
  {
5125
0
    log_error (_("cannot appoint a PGP 2.x style key as a "
5126
0
           "designated revoker\n"));
5127
0
    continue;
5128
0
  }
5129
5130
0
      revkey.fprlen = fprlen;
5131
0
      revkey.class = 0x80;
5132
0
      if (sensitive)
5133
0
  revkey.class |= 0x40;
5134
0
      revkey.algid = revoker_pk->pubkey_algo;
5135
5136
0
      if (cmp_public_keys (revoker_pk, pk) == 0)
5137
0
  {
5138
    /* This actually causes no harm (after all, a key that
5139
       designates itself as a revoker is the same as a
5140
       regular key), but it's easy enough to check. */
5141
0
    log_error (_("you cannot appoint a key as its own "
5142
0
           "designated revoker\n"));
5143
5144
0
    continue;
5145
0
  }
5146
5147
0
      keyid_from_pk (pk, NULL);
5148
5149
      /* Does this revkey already exist? */
5150
0
      if (!pk->revkey && pk->numrevkeys)
5151
0
  BUG ();
5152
0
      else
5153
0
  {
5154
0
    int i;
5155
5156
0
    for (i = 0; i < pk->numrevkeys; i++)
5157
0
      {
5158
0
        if (memcmp (&pk->revkey[i], &revkey,
5159
0
        sizeof (struct revocation_key)) == 0)
5160
0
    {
5161
0
      char buf[50];
5162
5163
0
      log_error (_("this key has already been designated "
5164
0
             "as a revoker\n"));
5165
5166
0
                  format_keyid (pk_keyid (pk), KF_LONG, buf, sizeof (buf));
5167
0
      write_status_text (STATUS_ALREADY_SIGNED, buf);
5168
5169
0
      break;
5170
0
    }
5171
0
      }
5172
5173
0
    if (i < pk->numrevkeys)
5174
0
      continue;
5175
0
  }
5176
5177
0
      print_key_info (ctrl, NULL, 0, revoker_pk, 0);
5178
0
      print_fingerprint (ctrl, NULL, revoker_pk, 2);
5179
0
      tty_printf ("\n");
5180
5181
0
      tty_printf (_("WARNING: appointing a key as a designated revoker "
5182
0
        "cannot be undone!\n"));
5183
5184
0
      tty_printf ("\n");
5185
5186
0
      if (!cpr_get_answer_is_yes ("keyedit.add_revoker.okay",
5187
0
          _("Are you sure you want to appoint this "
5188
0
            "key as a designated revoker? (y/N) ")))
5189
0
  continue;
5190
5191
0
      free_public_key (revoker_pk);
5192
0
      revoker_pk = NULL;
5193
0
      break;
5194
0
    }
5195
5196
0
  rc = make_keysig_packet (ctrl, &sig, pk, NULL, NULL, pk, 0x1F, 0, 0,
5197
0
         keygen_add_revkey, &revkey, NULL);
5198
0
  if (rc)
5199
0
    {
5200
0
      write_status_error ("keysig", rc);
5201
0
      log_error ("signing failed: %s\n", gpg_strerror (rc));
5202
0
      goto fail;
5203
0
    }
5204
5205
  /* Insert into public keyblock.  */
5206
0
  pkt = xmalloc_clear (sizeof *pkt);
5207
0
  pkt->pkttype = PKT_SIGNATURE;
5208
0
  pkt->pkt.signature = sig;
5209
0
  insert_kbnode (pub_keyblock, new_kbnode (pkt), PKT_SIGNATURE);
5210
5211
0
  return 1;
5212
5213
0
fail:
5214
0
  if (sig)
5215
0
    free_seckey_enc (sig);
5216
0
  free_public_key (revoker_pk);
5217
5218
0
  return 0;
5219
0
}
5220
5221
5222
/* Core function to add an ADSK to the KEYBLOCK.  Returns 0 on success
5223
 * or an error code.  If SIGTIMESTAMP is not 0 it is used for the key
5224
 * binding signature creation time; if not given the current time is
5225
 * used.  CACHE_NONCE can be used to avoid a second Pinetry pop-up for
5226
 * appending the ADSK. */
5227
gpg_error_t
5228
append_adsk_to_key (ctrl_t ctrl, kbnode_t keyblock, PKT_public_key *adsk,
5229
                    u32 sigtimestamp, const char *cache_nonce)
5230
0
{
5231
0
  gpg_error_t err;
5232
0
  PKT_public_key *main_pk;  /* The primary key.  */
5233
0
  PKT_signature *sig = NULL;
5234
0
  kbnode_t adsknode = NULL;
5235
0
  PACKET *pkt;  /* (temp. use; will be put into a kbnode_t)  */
5236
5237
  /* First get a copy.  */
5238
0
  adsk = copy_public_key_basics (NULL, adsk);
5239
5240
  /* Check compliance.  */
5241
0
  if (!gnupg_pk_is_compliant (opt.compliance, adsk->pubkey_algo, 0,
5242
0
                              adsk->pkey, nbits_from_pk (adsk), NULL))
5243
0
    {
5244
0
      char pkhex[MAX_FINGERPRINT_LEN*2+1];
5245
5246
0
      hexfingerprint (adsk, pkhex, sizeof pkhex);
5247
0
      log_error (_("WARNING: key %s is not suitable for encryption"
5248
0
                   " in %s mode\n"),
5249
0
                 pkhex, gnupg_compliance_option_string (opt.compliance));
5250
0
      err = gpg_error (GPG_ERR_FORBIDDEN);
5251
0
      goto leave;
5252
0
    }
5253
5254
  /* Get the primary key.  */
5255
0
  log_assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
5256
0
  main_pk = keyblock->pkt->pkt.public_key;
5257
5258
  /* Prepare and append the adsk.  */
5259
0
  keyid_from_pk (main_pk, adsk->main_keyid); /* Fixup main keyid.  */
5260
0
  log_assert ((adsk->pubkey_usage & PUBKEY_USAGE_XENC_MASK));
5261
0
  adsk->pubkey_usage = PUBKEY_USAGE_RENC;    /* 'e' or 'r' -> 'r'  */
5262
0
  pkt = xtrycalloc (1, sizeof *pkt);
5263
0
  if (!pkt)
5264
0
    {
5265
0
      err = gpg_error_from_syserror ();
5266
0
      goto leave;
5267
0
    }
5268
0
  pkt->pkttype = PKT_PUBLIC_SUBKEY;    /* Make sure it is a subkey.  */
5269
0
  pkt->pkt.public_key = adsk;
5270
0
  adsknode = new_kbnode (pkt);
5271
5272
  /* Make the signature.  */
5273
0
  err = make_keysig_packet (ctrl, &sig, main_pk, NULL, adsk, main_pk, 0x18,
5274
0
                            sigtimestamp, 0,
5275
0
                            keygen_add_key_flags_and_expire, adsk, cache_nonce);
5276
0
  adsk = NULL; /* (owned by adsknode - avoid double free.)  */
5277
0
  if (err)
5278
0
    {
5279
0
      write_status_error ("keysig", err);
5280
0
      log_error ("creating key binding failed: %s\n", gpg_strerror (err));
5281
0
      goto leave;
5282
0
    }
5283
5284
  /* Append the subkey packet and the binding signature.  */
5285
0
  add_kbnode (keyblock, adsknode);
5286
0
  adsknode = NULL;
5287
0
  pkt = xtrycalloc (1, sizeof *pkt);
5288
0
  if (!pkt)
5289
0
    {
5290
0
      err = gpg_error_from_syserror ();
5291
0
      goto leave;
5292
0
    }
5293
0
  pkt->pkttype = PKT_SIGNATURE;
5294
0
  pkt->pkt.signature = sig;
5295
0
  add_kbnode (keyblock, new_kbnode (pkt));
5296
5297
0
 leave:
5298
0
  release_kbnode (adsknode);
5299
0
  free_public_key (adsk);  /* Release our copy.  */
5300
0
  return err;
5301
0
}
5302
5303
5304
/*
5305
 * Ask for a new additional decryption subkey and add it to the key
5306
 * block.  Returns true if the keyblock was changed and false
5307
 * otherwise.  If ADSKFPR is not NULL, this function has been called
5308
 * by quick_addadsk and gives the fingerprint of the to be added key.
5309
 */
5310
static int
5311
menu_addadsk (ctrl_t ctrl, kbnode_t pub_keyblock, const char *adskfpr)
5312
0
{
5313
0
  PKT_public_key *pk;
5314
0
  PKT_public_key *adsk_pk = NULL;
5315
0
  kbnode_t adsk_keyblock = NULL;
5316
0
  char *answer = NULL;
5317
0
  gpg_error_t err;
5318
0
  KEYDB_SEARCH_DESC desc;
5319
0
  byte fpr[MAX_FINGERPRINT_LEN];
5320
0
  size_t fprlen;
5321
0
  kbnode_t node;
5322
0
  u32 sigtimestamp = make_timestamp ();
5323
5324
0
  log_assert (pub_keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
5325
5326
0
  for (;;)
5327
0
    {
5328
0
      xfree (answer);
5329
0
      if (adskfpr)
5330
0
        answer = xstrdup (adskfpr);
5331
0
      else
5332
0
        {
5333
0
          answer = cpr_get_utf8
5334
0
            ("keyedit.addadsk",
5335
0
             _("Enter the fingerprint of the additional decryption subkey: "));
5336
0
          if (answer[0] == '\0' || answer[0] == CONTROL_D)
5337
0
            {
5338
0
              err = gpg_error (GPG_ERR_CANCELED);
5339
0
              goto leave;
5340
0
            }
5341
0
        }
5342
0
      if (classify_user_id (answer, &desc, 1)
5343
0
          || desc.mode != KEYDB_SEARCH_MODE_FPR)
5344
0
        {
5345
0
          log_info (_("\"%s\" is not a fingerprint\n"), answer);
5346
0
          err = gpg_error (GPG_ERR_INV_USER_ID);
5347
0
          if (adskfpr)
5348
0
            goto leave;
5349
0
          continue;
5350
0
        }
5351
5352
      /* Force searching for that exact fingerprint and for any key
5353
       * which has a key with that fingerprint.  */
5354
0
      if (!strchr (answer, '!'))
5355
0
        {
5356
0
          char *tmpstr = xstrconcat (answer, "!", NULL);
5357
0
          xfree (answer);
5358
0
          answer = tmpstr;
5359
0
        }
5360
5361
0
      free_public_key (adsk_pk);
5362
0
      adsk_pk = xcalloc (1, sizeof *adsk_pk);
5363
0
      adsk_pk->req_usage = PUBKEY_USAGE_ENC;
5364
0
      release_kbnode (adsk_keyblock);
5365
0
      adsk_keyblock = NULL;
5366
0
      err = get_pubkey_byname (ctrl, GET_PUBKEY_NO_AKL,
5367
0
                               NULL, adsk_pk, answer, &adsk_keyblock, NULL, 1);
5368
0
      if (err)
5369
0
        {
5370
0
          write_status_error ("add_adsk", err);
5371
0
          log_info (_("key \"%s\" not found: %s\n"), answer,
5372
0
                    gpg_strerror (err));
5373
0
          if ((!opt.batch || adskfpr) && !opt.quiet
5374
0
              && gpg_err_code (err) == GPG_ERR_UNUSABLE_PUBKEY)
5375
0
            log_info (_("Did you specify the fingerprint of a subkey?\n"));
5376
0
          if (adskfpr)
5377
0
            goto leave;
5378
0
    continue;
5379
0
  }
5380
5381
0
      for (node = adsk_keyblock; node; node = node->next)
5382
0
        {
5383
0
          if (node->pkt->pkttype == PKT_PUBLIC_KEY
5384
0
              || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5385
0
            {
5386
0
              pk = node->pkt->pkt.public_key;
5387
0
              fingerprint_from_pk (pk, fpr, &fprlen);
5388
0
              if (fprlen == desc.fprlen
5389
0
                  && !memcmp (fpr, desc.u.fpr, fprlen)
5390
0
                  && (pk->pubkey_usage & PUBKEY_USAGE_ENC))
5391
0
                break;
5392
0
            }
5393
0
        }
5394
0
      if (!node)
5395
0
        {
5396
0
          write_status_error ("add_adsk", err);
5397
0
          err = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
5398
0
          log_info (_("key \"%s\" not found: %s\n"), answer,
5399
0
                    gpg_strerror (err));
5400
0
          if ((!opt.batch || adskfpr) && !opt.quiet)
5401
0
            log_info (_("Did you specify the fingerprint of a subkey?\n"));
5402
0
          if (adskfpr)
5403
0
            goto leave;
5404
0
    continue;
5405
0
        }
5406
5407
      /* Check that the selected subkey is not yet on our keyblock.  */
5408
0
      err = has_key_with_fingerprint (pub_keyblock, desc.u.fpr, desc.fprlen);
5409
0
      if (err)
5410
0
        {
5411
0
          log_info (_("key \"%s\" is already on this keyblock\n"), answer);
5412
0
          if (adskfpr)
5413
0
            goto leave;
5414
0
          continue;
5415
0
        }
5416
5417
0
      break;
5418
0
    }
5419
5420
  /* Append the subkey.  */
5421
0
  log_assert (node->pkt->pkttype == PKT_PUBLIC_KEY
5422
0
              || node->pkt->pkttype == PKT_PUBLIC_SUBKEY);
5423
0
  err = append_adsk_to_key (ctrl, pub_keyblock, node->pkt->pkt.public_key,
5424
0
                            sigtimestamp, NULL);
5425
5426
5427
0
 leave:
5428
0
  xfree (answer);
5429
0
  free_public_key (adsk_pk);
5430
0
  release_kbnode (adsk_keyblock);
5431
0
  if (!err)
5432
0
    return 1; /* The keyblock was modified.  */
5433
0
  else
5434
0
    return 0; /* Not modified.  */
5435
5436
0
}
5437
5438
5439
/* With FORCE_MAINKEY cleared this function handles the interactive
5440
 * menu option "expire".  With UNATTENDED set to 1 this function only
5441
 * sets the expiration date of the primary key to NEWEXPIRATION and
5442
 * avoid all interactivity; with a value of 2 only the flagged subkeys
5443
 * are set to NEWEXPIRATION.  Returns 0 if nothing was done,
5444
 * GPG_ERR_TRUE if the key was modified, or any other error code. */
5445
static gpg_error_t
5446
menu_expire (ctrl_t ctrl, kbnode_t pub_keyblock,
5447
             int unattended, u32 newexpiration)
5448
0
{
5449
0
  int signumber, rc;
5450
0
  u32 expiredate;
5451
0
  int only_mainkey;  /* Set if only the mainkey is to be updated.  */
5452
0
  PKT_public_key *main_pk, *sub_pk;
5453
0
  PKT_user_id *uid;
5454
0
  kbnode_t node;
5455
0
  u32 keyid[2];
5456
5457
0
  (void)signumber;
5458
0
  if (unattended)
5459
0
    {
5460
0
      only_mainkey = (unattended == 1);
5461
0
      expiredate = newexpiration;
5462
0
    }
5463
0
  else
5464
0
    {
5465
0
      int n1;
5466
5467
0
      only_mainkey = 0;
5468
0
      n1 = count_selected_keys (pub_keyblock);
5469
0
      if (n1 > 1)
5470
0
        {
5471
0
          if (!cpr_get_answer_is_yes
5472
0
              ("keyedit.expire_multiple_subkeys.okay",
5473
0
               _("Are you sure you want to change the"
5474
0
                 " expiration time for multiple subkeys? (y/N) ")))
5475
0
            return gpg_error (GPG_ERR_CANCELED);;
5476
0
        }
5477
0
      else if (n1)
5478
0
        tty_printf (_("Changing expiration time for a subkey.\n"));
5479
0
      else
5480
0
        {
5481
0
          tty_printf (_("Changing expiration time for the primary key.\n"));
5482
0
          only_mainkey = 1;
5483
0
          no_primary_warning (pub_keyblock);
5484
0
        }
5485
5486
0
      expiredate = ask_expiredate ();
5487
0
    }
5488
5489
5490
  /* Now we can actually change the self-signature(s) */
5491
0
  main_pk = sub_pk = NULL;
5492
0
  uid = NULL;
5493
0
  signumber = 0;
5494
0
  for (node = pub_keyblock; node; node = node->next)
5495
0
    {
5496
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
5497
0
  {
5498
0
    main_pk = node->pkt->pkt.public_key;
5499
0
    keyid_from_pk (main_pk, keyid);
5500
0
    main_pk->expiredate = expiredate;
5501
0
  }
5502
0
      else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5503
0
  {
5504
0
          if ((node->flag & NODFLG_SELKEY) && unattended != 1)
5505
0
            {
5506
              /* The flag is set and we do not want to set the
5507
               * expiration date only for the main key.  */
5508
0
              sub_pk = node->pkt->pkt.public_key;
5509
0
              sub_pk->expiredate = expiredate;
5510
0
            }
5511
0
          else
5512
0
            sub_pk = NULL;
5513
0
  }
5514
0
      else if (node->pkt->pkttype == PKT_USER_ID)
5515
0
  uid = node->pkt->pkt.user_id;
5516
0
      else if (main_pk && node->pkt->pkttype == PKT_SIGNATURE
5517
0
         && (only_mainkey || sub_pk))
5518
0
  {
5519
0
    PKT_signature *sig = node->pkt->pkt.signature;
5520
5521
0
    if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1]
5522
0
        && ((only_mainkey && uid
5523
0
       && uid->created && (sig->sig_class & ~3) == 0x10)
5524
0
      || (!only_mainkey && sig->sig_class == 0x18))
5525
0
        && sig->flags.chosen_selfsig)
5526
0
      {
5527
        /* This is a self-signature which is to be replaced.  */
5528
0
        PKT_signature *newsig;
5529
0
        PACKET *newpkt;
5530
5531
0
        signumber++;
5532
5533
0
        if ((only_mainkey && main_pk->version < 4)
5534
0
      || (!only_mainkey && sub_pk->version < 4))
5535
0
    {
5536
0
      log_info
5537
0
                    (_("You can't change the expiration date of a v3 key\n"));
5538
0
      return gpg_error (GPG_ERR_LEGACY_KEY);
5539
0
    }
5540
5541
0
        if (only_mainkey)
5542
0
    rc = update_keysig_packet (ctrl,
5543
0
                                           &newsig, sig, main_pk, uid, NULL,
5544
0
             main_pk, keygen_add_key_expire,
5545
0
             main_pk);
5546
0
        else
5547
0
    rc =
5548
0
      update_keysig_packet (ctrl,
5549
0
                                        &newsig, sig, main_pk, NULL, sub_pk,
5550
0
          main_pk, keygen_add_key_expire, sub_pk);
5551
0
        if (rc)
5552
0
    {
5553
0
      log_error ("make_keysig_packet failed: %s\n",
5554
0
           gpg_strerror (rc));
5555
0
                  if (gpg_err_code (rc) == GPG_ERR_TRUE)
5556
0
                    rc = GPG_ERR_GENERAL;
5557
0
      return rc;
5558
0
    }
5559
5560
        /* Replace the packet.  */
5561
0
        newpkt = xmalloc_clear (sizeof *newpkt);
5562
0
        newpkt->pkttype = PKT_SIGNATURE;
5563
0
        newpkt->pkt.signature = newsig;
5564
0
        free_packet (node->pkt, NULL);
5565
0
        xfree (node->pkt);
5566
0
        node->pkt = newpkt;
5567
0
        sub_pk = NULL;
5568
0
      }
5569
0
  }
5570
0
    }
5571
5572
0
  update_trust = 1;
5573
0
  return gpg_error (GPG_ERR_TRUE);
5574
0
}
5575
5576
5577
/* Change the capability of a selected key.  This command should only
5578
 * be used to rectify badly created keys and as such is not suggested
5579
 * for general use.  */
5580
static int
5581
menu_changeusage (ctrl_t ctrl, kbnode_t keyblock)
5582
0
{
5583
0
  int n1, rc;
5584
0
  int mainkey = 0;
5585
0
  PKT_public_key *main_pk, *sub_pk;
5586
0
  PKT_user_id *uid;
5587
0
  kbnode_t node;
5588
0
  u32 keyid[2];
5589
5590
0
  n1 = count_selected_keys (keyblock);
5591
0
  if (n1 > 1)
5592
0
    {
5593
0
      tty_printf (_("You must select exactly one key.\n"));
5594
0
      return 0;
5595
0
    }
5596
0
  else if (n1)
5597
0
    tty_printf (_("Changing usage of a subkey.\n"));
5598
0
  else
5599
0
    {
5600
0
      tty_printf (_("Changing usage of the primary key.\n"));
5601
0
      mainkey = 1;
5602
0
    }
5603
5604
  /* Now we can actually change the self-signature(s) */
5605
0
  main_pk = sub_pk = NULL;
5606
0
  uid = NULL;
5607
0
  for (node = keyblock; node; node = node->next)
5608
0
    {
5609
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
5610
0
  {
5611
0
    main_pk = node->pkt->pkt.public_key;
5612
0
    keyid_from_pk (main_pk, keyid);
5613
0
  }
5614
0
      else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5615
0
  {
5616
0
          if (node->flag & NODFLG_SELKEY)
5617
0
            sub_pk = node->pkt->pkt.public_key;
5618
0
          else
5619
0
            sub_pk = NULL;
5620
0
  }
5621
0
      else if (node->pkt->pkttype == PKT_USER_ID)
5622
0
  uid = node->pkt->pkt.user_id;
5623
0
      else if (main_pk && node->pkt->pkttype == PKT_SIGNATURE
5624
0
         && (mainkey || sub_pk))
5625
0
  {
5626
0
    PKT_signature *sig = node->pkt->pkt.signature;
5627
0
    if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1]
5628
0
        && ((mainkey && uid
5629
0
       && uid->created && (sig->sig_class & ~3) == 0x10)
5630
0
      || (!mainkey && sig->sig_class == 0x18))
5631
0
        && sig->flags.chosen_selfsig)
5632
0
      {
5633
        /* This is the self-signature which is to be replaced.  */
5634
0
        PKT_signature *newsig;
5635
0
        PACKET *newpkt;
5636
5637
0
        if ((mainkey && main_pk->version < 4)
5638
0
      || (!mainkey && sub_pk->version < 4))
5639
0
    {
5640
                  /* Note: This won't happen because we don't support
5641
                   * v3 keys anymore.  */
5642
0
      log_info ("You can't change the capabilities of a v3 key\n");
5643
0
      return 0;
5644
0
    }
5645
5646
0
              if (mainkey)
5647
0
                main_pk->pubkey_usage = ask_key_flags (main_pk->pubkey_algo, 0,
5648
0
                                                       main_pk->pubkey_usage);
5649
0
              else
5650
0
                sub_pk->pubkey_usage  = ask_key_flags (sub_pk->pubkey_algo, 1,
5651
0
                                                       sub_pk->pubkey_usage);
5652
5653
0
        if (mainkey)
5654
0
    rc = update_keysig_packet (ctrl,
5655
0
                                           &newsig, sig, main_pk, uid, NULL,
5656
0
             main_pk, keygen_add_key_flags,
5657
0
             main_pk);
5658
0
        else
5659
0
    rc =
5660
0
      update_keysig_packet (ctrl,
5661
0
                                        &newsig, sig, main_pk, NULL, sub_pk,
5662
0
          main_pk, keygen_add_key_flags, sub_pk);
5663
0
        if (rc)
5664
0
    {
5665
0
      log_error ("make_keysig_packet failed: %s\n",
5666
0
                             gpg_strerror (rc));
5667
0
      return 0;
5668
0
    }
5669
5670
        /* Replace the packet.  */
5671
0
        newpkt = xmalloc_clear (sizeof *newpkt);
5672
0
        newpkt->pkttype = PKT_SIGNATURE;
5673
0
        newpkt->pkt.signature = newsig;
5674
0
        free_packet (node->pkt, NULL);
5675
0
        xfree (node->pkt);
5676
0
        node->pkt = newpkt;
5677
0
        sub_pk = NULL;
5678
0
              break;
5679
0
      }
5680
0
  }
5681
0
    }
5682
5683
0
  return 1;
5684
0
}
5685
5686
5687
static int
5688
menu_backsign (ctrl_t ctrl, kbnode_t pub_keyblock)
5689
0
{
5690
0
  int rc, modified = 0;
5691
0
  PKT_public_key *main_pk;
5692
0
  KBNODE node;
5693
0
  u32 timestamp;
5694
5695
0
  log_assert (pub_keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
5696
5697
0
  merge_keys_and_selfsig (ctrl, pub_keyblock);
5698
0
  main_pk = pub_keyblock->pkt->pkt.public_key;
5699
0
  keyid_from_pk (main_pk, NULL);
5700
5701
  /* We use the same timestamp for all backsigs so that we don't
5702
     reveal information about the used machine.  */
5703
0
  timestamp = make_timestamp ();
5704
5705
0
  for (node = pub_keyblock; node; node = node->next)
5706
0
    {
5707
0
      PKT_public_key *sub_pk = NULL;
5708
0
      KBNODE node2, sig_pk = NULL /*,sig_sk = NULL*/;
5709
      /* char *passphrase; */
5710
5711
      /* Find a signing subkey with no backsig */
5712
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5713
0
  {
5714
0
    if (node->pkt->pkt.public_key->pubkey_usage & PUBKEY_USAGE_SIG)
5715
0
      {
5716
0
        if (node->pkt->pkt.public_key->flags.backsig)
5717
0
    tty_printf (_
5718
0
          ("signing subkey %s is already cross-certified\n"),
5719
0
          keystr_from_pk (node->pkt->pkt.public_key));
5720
0
        else
5721
0
    sub_pk = node->pkt->pkt.public_key;
5722
0
      }
5723
0
    else
5724
0
      tty_printf (_("subkey %s does not sign and so does"
5725
0
        " not need to be cross-certified\n"),
5726
0
      keystr_from_pk (node->pkt->pkt.public_key));
5727
0
  }
5728
5729
0
      if (!sub_pk)
5730
0
  continue;
5731
5732
      /* Find the selected selfsig on this subkey */
5733
0
      for (node2 = node->next;
5734
0
     node2 && node2->pkt->pkttype == PKT_SIGNATURE; node2 = node2->next)
5735
0
  if (node2->pkt->pkt.signature->version >= 4
5736
0
      && node2->pkt->pkt.signature->flags.chosen_selfsig)
5737
0
    {
5738
0
      sig_pk = node2;
5739
0
      break;
5740
0
    }
5741
5742
0
      if (!sig_pk)
5743
0
  continue;
5744
5745
      /* Find the secret subkey that matches the public subkey */
5746
0
      log_debug ("FIXME: Check whether a secret subkey is available.\n");
5747
      /* if (!sub_sk) */
5748
      /*   { */
5749
      /*     tty_printf (_("no secret subkey for public subkey %s - ignoring\n"), */
5750
      /*          keystr_from_pk (sub_pk)); */
5751
      /*     continue; */
5752
      /*   } */
5753
5754
5755
      /* Now we can get to work.  */
5756
5757
0
      rc = make_backsig (ctrl,
5758
0
                         sig_pk->pkt->pkt.signature, main_pk, sub_pk, sub_pk,
5759
0
       timestamp, NULL);
5760
0
      if (!rc)
5761
0
  {
5762
0
    PKT_signature *newsig;
5763
0
    PACKET *newpkt;
5764
5765
0
    rc = update_keysig_packet (ctrl,
5766
0
                                     &newsig, sig_pk->pkt->pkt.signature,
5767
0
                                     main_pk, NULL, sub_pk, main_pk,
5768
0
                                     NULL, NULL);
5769
0
    if (!rc)
5770
0
      {
5771
        /* Put the new sig into place on the pubkey */
5772
0
        newpkt = xmalloc_clear (sizeof (*newpkt));
5773
0
        newpkt->pkttype = PKT_SIGNATURE;
5774
0
        newpkt->pkt.signature = newsig;
5775
0
        free_packet (sig_pk->pkt, NULL);
5776
0
        xfree (sig_pk->pkt);
5777
0
        sig_pk->pkt = newpkt;
5778
5779
0
        modified = 1;
5780
0
      }
5781
0
    else
5782
0
      {
5783
0
        log_error ("update_keysig_packet failed: %s\n",
5784
0
       gpg_strerror (rc));
5785
0
        break;
5786
0
      }
5787
0
  }
5788
0
      else
5789
0
  {
5790
0
    log_error ("make_backsig failed: %s\n", gpg_strerror (rc));
5791
0
    break;
5792
0
  }
5793
0
    }
5794
5795
0
  return modified;
5796
0
}
5797
5798
5799
static int
5800
change_primary_uid_cb (PKT_signature * sig, void *opaque)
5801
0
{
5802
0
  byte buf[1];
5803
5804
  /* first clear all primary uid flags so that we are sure none are
5805
   * lingering around */
5806
0
  delete_sig_subpkt (sig->hashed, SIGSUBPKT_PRIMARY_UID);
5807
0
  delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PRIMARY_UID);
5808
5809
  /* if opaque is set,we want to set the primary id */
5810
0
  if (opaque)
5811
0
    {
5812
0
      buf[0] = 1;
5813
0
      build_sig_subpkt (sig, SIGSUBPKT_PRIMARY_UID, buf, 1);
5814
0
    }
5815
5816
0
  return 0;
5817
0
}
5818
5819
5820
/*
5821
 * Set the primary uid flag for the selected UID.  We will also reset
5822
 * all other primary uid flags.  For this to work we have to update
5823
 * all the signature timestamps.  If we would do this with the current
5824
 * time, we lose quite a lot of information, so we use a kludge to
5825
 * do this: Just increment the timestamp by one second which is
5826
 * sufficient to updated a signature during import.
5827
 */
5828
static int
5829
menu_set_primary_uid (ctrl_t ctrl, kbnode_t pub_keyblock)
5830
0
{
5831
0
  PKT_public_key *main_pk;
5832
0
  PKT_user_id *uid;
5833
0
  KBNODE node;
5834
0
  u32 keyid[2];
5835
0
  int selected;
5836
0
  int attribute = 0;
5837
0
  int modified = 0;
5838
5839
0
  if (count_selected_uids (pub_keyblock) != 1)
5840
0
    {
5841
0
      tty_printf (_("Please select exactly one user ID.\n"));
5842
0
      return 0;
5843
0
    }
5844
5845
0
  main_pk = NULL;
5846
0
  uid = NULL;
5847
0
  selected = 0;
5848
5849
  /* Is our selected uid an attribute packet? */
5850
0
  for (node = pub_keyblock; node; node = node->next)
5851
0
    if (node->pkt->pkttype == PKT_USER_ID && node->flag & NODFLG_SELUID)
5852
0
      attribute = (node->pkt->pkt.user_id->attrib_data != NULL);
5853
5854
0
  for (node = pub_keyblock; node; node = node->next)
5855
0
    {
5856
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5857
0
  break; /* No more user ids expected - ready.  */
5858
5859
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
5860
0
  {
5861
0
    main_pk = node->pkt->pkt.public_key;
5862
0
    keyid_from_pk (main_pk, keyid);
5863
0
  }
5864
0
      else if (node->pkt->pkttype == PKT_USER_ID)
5865
0
  {
5866
0
    uid = node->pkt->pkt.user_id;
5867
0
    selected = node->flag & NODFLG_SELUID;
5868
0
  }
5869
0
      else if (main_pk && uid && node->pkt->pkttype == PKT_SIGNATURE)
5870
0
  {
5871
0
    PKT_signature *sig = node->pkt->pkt.signature;
5872
0
    if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1]
5873
0
        && (uid && (sig->sig_class & ~3) == 0x10)
5874
0
        && attribute == (uid->attrib_data != NULL)
5875
0
        && sig->flags.chosen_selfsig)
5876
0
      {
5877
0
        if (sig->version < 4)
5878
0
    {
5879
0
      char *user =
5880
0
        utf8_to_native (uid->name, strlen (uid->name), 0);
5881
5882
0
      log_info (_("skipping v3 self-signature on user ID \"%s\"\n"),
5883
0
                            user);
5884
0
      xfree (user);
5885
0
    }
5886
0
        else
5887
0
    {
5888
      /* This is a selfsignature which is to be replaced.
5889
         We can just ignore v3 signatures because they are
5890
         not able to carry the primary ID flag.  We also
5891
         ignore self-sigs on user IDs that are not of the
5892
         same type that we are making primary.  That is, if
5893
         we are making a user ID primary, we alter user IDs.
5894
         If we are making an attribute packet primary, we
5895
         alter attribute packets. */
5896
5897
      /* FIXME: We must make sure that we only have one
5898
         self-signature per user ID here (not counting
5899
         revocations) */
5900
0
      PKT_signature *newsig;
5901
0
      PACKET *newpkt;
5902
0
      const byte *p;
5903
0
      int action;
5904
5905
      /* See whether this signature has the primary UID flag.  */
5906
0
      p = parse_sig_subpkt (sig, 1,
5907
0
          SIGSUBPKT_PRIMARY_UID, NULL);
5908
0
      if (!p)
5909
0
        p = parse_sig_subpkt (sig, 0,
5910
0
            SIGSUBPKT_PRIMARY_UID, NULL);
5911
0
      if (p && *p) /* yes */
5912
0
        action = selected ? 0 : -1;
5913
0
      else    /* no */
5914
0
        action = selected ? 1 : 0;
5915
5916
0
      if (action)
5917
0
        {
5918
0
          int rc = update_keysig_packet (ctrl, &newsig, sig,
5919
0
                 main_pk, uid, NULL,
5920
0
                 main_pk,
5921
0
                 change_primary_uid_cb,
5922
0
                 action > 0 ? "x" : NULL);
5923
0
          if (rc)
5924
0
      {
5925
0
        log_error ("update_keysig_packet failed: %s\n",
5926
0
             gpg_strerror (rc));
5927
0
        return 0;
5928
0
      }
5929
          /* replace the packet */
5930
0
          newpkt = xmalloc_clear (sizeof *newpkt);
5931
0
          newpkt->pkttype = PKT_SIGNATURE;
5932
0
          newpkt->pkt.signature = newsig;
5933
0
          free_packet (node->pkt, NULL);
5934
0
          xfree (node->pkt);
5935
0
          node->pkt = newpkt;
5936
0
          modified = 1;
5937
0
        }
5938
0
    }
5939
0
      }
5940
0
  }
5941
0
    }
5942
5943
0
  return modified;
5944
0
}
5945
5946
5947
/*
5948
 * Set preferences to new values for the selected user IDs.
5949
 * --quick-update-pred calls this with UNATTENDED set.
5950
 */
5951
static int
5952
menu_set_preferences (ctrl_t ctrl, kbnode_t pub_keyblock, int unattended)
5953
0
{
5954
0
  PKT_public_key *main_pk;
5955
0
  PKT_user_id *uid;
5956
0
  KBNODE node;
5957
0
  u32 keyid[2];
5958
0
  int selected, select_all;
5959
0
  int modified = 0;
5960
5961
0
  if (!unattended)
5962
0
    no_primary_warning (pub_keyblock);
5963
5964
0
  select_all = unattended? 1 : !count_selected_uids (pub_keyblock);
5965
5966
  /* Now we can actually change the self signature(s) */
5967
0
  main_pk = NULL;
5968
0
  uid = NULL;
5969
0
  selected = 0;
5970
0
  for (node = pub_keyblock; node; node = node->next)
5971
0
    {
5972
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
5973
0
  break; /* No more user-ids expected - ready.  */
5974
5975
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
5976
0
  {
5977
0
    main_pk = node->pkt->pkt.public_key;
5978
0
    keyid_from_pk (main_pk, keyid);
5979
0
  }
5980
0
      else if (node->pkt->pkttype == PKT_USER_ID)
5981
0
  {
5982
0
    uid = node->pkt->pkt.user_id;
5983
0
    selected = select_all || (node->flag & NODFLG_SELUID);
5984
0
  }
5985
0
      else if (main_pk && uid && selected
5986
0
         && node->pkt->pkttype == PKT_SIGNATURE)
5987
0
  {
5988
0
    PKT_signature *sig = node->pkt->pkt.signature;
5989
0
    if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1]
5990
0
        && (uid && (sig->sig_class & ~3) == 0x10)
5991
0
        && sig->flags.chosen_selfsig)
5992
0
      {
5993
0
        if (sig->version < 4)
5994
0
    {
5995
0
      char *user =
5996
0
        utf8_to_native (uid->name, strlen (uid->name), 0);
5997
5998
0
      log_info (_("skipping v3 self-signature on user ID \"%s\"\n"),
5999
0
          user);
6000
0
      xfree (user);
6001
0
    }
6002
0
        else
6003
0
    {
6004
      /* This is a selfsignature which is to be replaced
6005
       * We have to ignore v3 signatures because they are
6006
       * not able to carry the preferences.  */
6007
0
      PKT_signature *newsig;
6008
0
      PACKET *newpkt;
6009
0
      int rc;
6010
6011
0
      rc = update_keysig_packet (ctrl, &newsig, sig,
6012
0
               main_pk, uid, NULL, main_pk,
6013
0
                                             keygen_upd_std_prefs, NULL);
6014
0
      if (rc)
6015
0
        {
6016
0
          log_error ("update_keysig_packet failed: %s\n",
6017
0
         gpg_strerror (rc));
6018
0
          return 0;
6019
0
        }
6020
      /* replace the packet */
6021
0
      newpkt = xmalloc_clear (sizeof *newpkt);
6022
0
      newpkt->pkttype = PKT_SIGNATURE;
6023
0
      newpkt->pkt.signature = newsig;
6024
0
      free_packet (node->pkt, NULL);
6025
0
      xfree (node->pkt);
6026
0
      node->pkt = newpkt;
6027
0
      modified = 1;
6028
0
    }
6029
0
      }
6030
0
  }
6031
0
    }
6032
6033
0
  return modified;
6034
0
}
6035
6036
6037
static int
6038
menu_set_keyserver_url (ctrl_t ctrl, const char *url, kbnode_t pub_keyblock)
6039
0
{
6040
0
  PKT_public_key *main_pk;
6041
0
  PKT_user_id *uid;
6042
0
  KBNODE node;
6043
0
  u32 keyid[2];
6044
0
  int selected, select_all;
6045
0
  int modified = 0;
6046
0
  char *answer, *uri;
6047
6048
0
  no_primary_warning (pub_keyblock);
6049
6050
0
  if (url)
6051
0
    answer = xstrdup (url);
6052
0
  else
6053
0
    {
6054
0
      answer = cpr_get_utf8 ("keyedit.add_keyserver",
6055
0
           _("Enter your preferred keyserver URL: "));
6056
0
      if (answer[0] == '\0' || answer[0] == CONTROL_D)
6057
0
  {
6058
0
    xfree (answer);
6059
0
    return 0;
6060
0
  }
6061
0
    }
6062
6063
0
  if (!ascii_strcasecmp (answer, "none"))
6064
0
    {
6065
0
      xfree (answer);
6066
0
      uri = NULL;
6067
0
    }
6068
0
  else
6069
0
    {
6070
0
      struct keyserver_spec *keyserver = NULL;
6071
      /* Sanity check the format */
6072
0
      keyserver = parse_keyserver_uri (answer, 1);
6073
0
      xfree (answer);
6074
0
      if (!keyserver)
6075
0
  {
6076
0
    log_info (_("could not parse keyserver URL\n"));
6077
0
    return 0;
6078
0
  }
6079
0
      uri = xstrdup (keyserver->uri);
6080
0
      free_keyserver_spec (keyserver);
6081
0
    }
6082
6083
0
  select_all = !count_selected_uids (pub_keyblock);
6084
6085
  /* Now we can actually change the self signature(s) */
6086
0
  main_pk = NULL;
6087
0
  uid = NULL;
6088
0
  selected = 0;
6089
0
  for (node = pub_keyblock; node; node = node->next)
6090
0
    {
6091
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
6092
0
  break; /* ready */
6093
6094
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
6095
0
  {
6096
0
    main_pk = node->pkt->pkt.public_key;
6097
0
    keyid_from_pk (main_pk, keyid);
6098
0
  }
6099
0
      else if (node->pkt->pkttype == PKT_USER_ID)
6100
0
  {
6101
0
    uid = node->pkt->pkt.user_id;
6102
0
    selected = select_all || (node->flag & NODFLG_SELUID);
6103
0
  }
6104
0
      else if (main_pk && uid && selected
6105
0
         && node->pkt->pkttype == PKT_SIGNATURE)
6106
0
  {
6107
0
    PKT_signature *sig = node->pkt->pkt.signature;
6108
0
    if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1]
6109
0
        && (uid && (sig->sig_class & ~3) == 0x10)
6110
0
        && sig->flags.chosen_selfsig)
6111
0
      {
6112
0
        char *user = utf8_to_native (uid->name, strlen (uid->name), 0);
6113
0
        if (sig->version < 4)
6114
0
    log_info (_("skipping v3 self-signature on user ID \"%s\"\n"),
6115
0
        user);
6116
0
        else
6117
0
    {
6118
      /* This is a selfsignature which is to be replaced
6119
       * We have to ignore v3 signatures because they are
6120
       * not able to carry the subpacket. */
6121
0
      PKT_signature *newsig;
6122
0
      PACKET *newpkt;
6123
0
      int rc;
6124
0
      const byte *p;
6125
0
      size_t plen;
6126
6127
0
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, &plen);
6128
0
      if (p && plen)
6129
0
        {
6130
0
          tty_printf ("Current preferred keyserver for user"
6131
0
          " ID \"%s\": ", user);
6132
0
          tty_print_utf8_string (p, plen);
6133
0
          tty_printf ("\n");
6134
0
          if (!cpr_get_answer_is_yes
6135
0
                          ("keyedit.confirm_keyserver",
6136
0
                           uri
6137
0
                           ? _("Are you sure you want to replace it? (y/N) ")
6138
0
                           : _("Are you sure you want to delete it? (y/N) ")))
6139
0
            {
6140
0
        xfree (user);
6141
0
        continue;
6142
0
            }
6143
0
        }
6144
0
      else if (uri == NULL)
6145
0
        {
6146
          /* There is no current keyserver URL, so there
6147
             is no point in trying to un-set it. */
6148
0
                      xfree (user);
6149
0
          continue;
6150
0
        }
6151
6152
0
      rc = update_keysig_packet (ctrl, &newsig, sig,
6153
0
               main_pk, uid, NULL,
6154
0
               main_pk,
6155
0
               keygen_add_keyserver_url, uri);
6156
0
      if (rc)
6157
0
        {
6158
0
          log_error ("update_keysig_packet failed: %s\n",
6159
0
         gpg_strerror (rc));
6160
0
          xfree (uri);
6161
0
                xfree (user);
6162
0
          return 0;
6163
0
        }
6164
      /* replace the packet */
6165
0
      newpkt = xmalloc_clear (sizeof *newpkt);
6166
0
      newpkt->pkttype = PKT_SIGNATURE;
6167
0
      newpkt->pkt.signature = newsig;
6168
0
      free_packet (node->pkt, NULL);
6169
0
      xfree (node->pkt);
6170
0
      node->pkt = newpkt;
6171
0
      modified = 1;
6172
0
    }
6173
6174
0
        xfree (user);
6175
0
      }
6176
0
  }
6177
0
    }
6178
6179
0
  xfree (uri);
6180
0
  return modified;
6181
0
}
6182
6183
6184
static int
6185
menu_set_notation (ctrl_t ctrl, const char *string, KBNODE pub_keyblock)
6186
0
{
6187
0
  PKT_public_key *main_pk;
6188
0
  PKT_user_id *uid;
6189
0
  KBNODE node;
6190
0
  u32 keyid[2];
6191
0
  int selected, select_all;
6192
0
  int modified = 0;
6193
0
  char *answer;
6194
0
  struct notation *notation;
6195
6196
0
  no_primary_warning (pub_keyblock);
6197
6198
0
  if (string)
6199
0
    answer = xstrdup (string);
6200
0
  else
6201
0
    {
6202
0
      answer = cpr_get_utf8 ("keyedit.add_notation",
6203
0
           _("Enter the notation: "));
6204
0
      if (answer[0] == '\0' || answer[0] == CONTROL_D)
6205
0
  {
6206
0
    xfree (answer);
6207
0
    return 0;
6208
0
  }
6209
0
    }
6210
6211
0
  if (!ascii_strcasecmp (answer, "none")
6212
0
      || !ascii_strcasecmp (answer, "-"))
6213
0
    notation = NULL; /* Delete them all.  */
6214
0
  else
6215
0
    {
6216
0
      notation = string_to_notation (answer, 0);
6217
0
      if (!notation)
6218
0
  {
6219
0
    xfree (answer);
6220
0
    return 0;
6221
0
  }
6222
0
    }
6223
6224
0
  xfree (answer);
6225
6226
0
  select_all = !count_selected_uids (pub_keyblock);
6227
6228
  /* Now we can actually change the self signature(s) */
6229
0
  main_pk = NULL;
6230
0
  uid = NULL;
6231
0
  selected = 0;
6232
0
  for (node = pub_keyblock; node; node = node->next)
6233
0
    {
6234
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
6235
0
  break; /* ready */
6236
6237
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
6238
0
  {
6239
0
    main_pk = node->pkt->pkt.public_key;
6240
0
    keyid_from_pk (main_pk, keyid);
6241
0
  }
6242
0
      else if (node->pkt->pkttype == PKT_USER_ID)
6243
0
  {
6244
0
    uid = node->pkt->pkt.user_id;
6245
0
    selected = select_all || (node->flag & NODFLG_SELUID);
6246
0
  }
6247
0
      else if (main_pk && uid && selected
6248
0
         && node->pkt->pkttype == PKT_SIGNATURE)
6249
0
  {
6250
0
    PKT_signature *sig = node->pkt->pkt.signature;
6251
0
    if (keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1]
6252
0
        && (uid && (sig->sig_class & ~3) == 0x10)
6253
0
        && sig->flags.chosen_selfsig)
6254
0
      {
6255
0
        char *user = utf8_to_native (uid->name, strlen (uid->name), 0);
6256
0
        if (sig->version < 4)
6257
0
    log_info (_("skipping v3 self-signature on user ID \"%s\"\n"),
6258
0
        user);
6259
0
        else
6260
0
    {
6261
0
      PKT_signature *newsig;
6262
0
      PACKET *newpkt;
6263
0
      int rc, skip = 0, addonly = 1;
6264
6265
0
      if (sig->flags.notation)
6266
0
        {
6267
0
          tty_printf ("Current notations for user ID \"%s\":\n",
6268
0
          user);
6269
0
          tty_print_notations (-9, sig);
6270
0
        }
6271
0
      else
6272
0
        {
6273
0
          tty_printf ("No notations on user ID \"%s\"\n", user);
6274
0
          if (notation == NULL)
6275
0
      {
6276
        /* There are no current notations, so there
6277
           is no point in trying to un-set them. */
6278
0
        continue;
6279
0
      }
6280
0
        }
6281
6282
0
      if (notation)
6283
0
        {
6284
0
          struct notation *n;
6285
0
          int deleting = 0;
6286
6287
0
          notation->next = sig_to_notation (sig);
6288
6289
0
          for (n = notation->next; n; n = n->next)
6290
0
      if (strcmp (n->name, notation->name) == 0)
6291
0
        {
6292
0
          if (notation->value)
6293
0
            {
6294
0
        if (strcmp (n->value, notation->value) == 0)
6295
0
          {
6296
0
            if (notation->flags.ignore)
6297
0
              {
6298
          /* Value match with a delete
6299
             flag. */
6300
0
          n->flags.ignore = 1;
6301
0
          deleting = 1;
6302
0
              }
6303
0
            else
6304
0
              {
6305
          /* Adding the same notation
6306
             twice, so don't add it at
6307
             all. */
6308
0
          skip = 1;
6309
0
          tty_printf ("Skipping notation:"
6310
0
                " %s=%s\n",
6311
0
                notation->name,
6312
0
                notation->value);
6313
0
          break;
6314
0
              }
6315
0
          }
6316
0
            }
6317
0
          else
6318
0
            {
6319
        /* No value, so it means delete. */
6320
0
        n->flags.ignore = 1;
6321
0
        deleting = 1;
6322
0
            }
6323
6324
0
          if (n->flags.ignore)
6325
0
            {
6326
0
        tty_printf ("Removing notation: %s=%s\n",
6327
0
              n->name, n->value);
6328
0
        addonly = 0;
6329
0
            }
6330
0
        }
6331
6332
0
          if (!notation->flags.ignore && !skip)
6333
0
      tty_printf ("Adding notation: %s=%s\n",
6334
0
            notation->name, notation->value);
6335
6336
          /* We tried to delete, but had no matches.  */
6337
0
          if (notation->flags.ignore && !deleting)
6338
0
      continue;
6339
0
        }
6340
0
      else
6341
0
        {
6342
0
          tty_printf ("Removing all notations\n");
6343
0
          addonly = 0;
6344
0
        }
6345
6346
0
      if (skip
6347
0
          || (!addonly
6348
0
        &&
6349
0
        !cpr_get_answer_is_yes ("keyedit.confirm_notation",
6350
0
              _("Proceed? (y/N) "))))
6351
0
        continue;
6352
6353
0
      rc = update_keysig_packet (ctrl, &newsig, sig,
6354
0
               main_pk, uid, NULL,
6355
0
               main_pk,
6356
0
               keygen_add_notations, notation);
6357
0
      if (rc)
6358
0
        {
6359
0
          log_error ("update_keysig_packet failed: %s\n",
6360
0
         gpg_strerror (rc));
6361
0
          free_notation (notation);
6362
0
          xfree (user);
6363
0
          return 0;
6364
0
        }
6365
6366
      /* replace the packet */
6367
0
      newpkt = xmalloc_clear (sizeof *newpkt);
6368
0
      newpkt->pkttype = PKT_SIGNATURE;
6369
0
      newpkt->pkt.signature = newsig;
6370
0
      free_packet (node->pkt, NULL);
6371
0
      xfree (node->pkt);
6372
0
      node->pkt = newpkt;
6373
0
      modified = 1;
6374
6375
0
      if (notation)
6376
0
        {
6377
          /* Snip off the notation list from the sig */
6378
0
          free_notation (notation->next);
6379
0
          notation->next = NULL;
6380
0
        }
6381
6382
0
      xfree (user);
6383
0
    }
6384
0
      }
6385
0
  }
6386
0
    }
6387
6388
0
  free_notation (notation);
6389
0
  return modified;
6390
0
}
6391
6392
6393
/*
6394
 * Select one user id or remove all selection if IDX is 0 or select
6395
 * all if IDX is -1.  Returns: True if the selection changed.
6396
 */
6397
static int
6398
menu_select_uid (KBNODE keyblock, int idx)
6399
0
{
6400
0
  KBNODE node;
6401
0
  int i;
6402
6403
0
  if (idx == -1)   /* Select all. */
6404
0
    {
6405
0
      for (node = keyblock; node; node = node->next)
6406
0
  if (node->pkt->pkttype == PKT_USER_ID)
6407
0
    node->flag |= NODFLG_SELUID;
6408
0
      return 1;
6409
0
    }
6410
0
  else if (idx)     /* Toggle.  */
6411
0
    {
6412
0
      for (i = 0, node = keyblock; node; node = node->next)
6413
0
  {
6414
0
    if (node->pkt->pkttype == PKT_USER_ID)
6415
0
      if (++i == idx)
6416
0
        break;
6417
0
  }
6418
0
      if (!node)
6419
0
  {
6420
0
    tty_printf (_("No user ID with index %d\n"), idx);
6421
0
    return 0;
6422
0
  }
6423
6424
0
      for (i = 0, node = keyblock; node; node = node->next)
6425
0
  {
6426
0
    if (node->pkt->pkttype == PKT_USER_ID)
6427
0
      {
6428
0
        if (++i == idx)
6429
0
    {
6430
0
      if ((node->flag & NODFLG_SELUID))
6431
0
        node->flag &= ~NODFLG_SELUID;
6432
0
      else
6433
0
        node->flag |= NODFLG_SELUID;
6434
0
    }
6435
0
      }
6436
0
  }
6437
0
    }
6438
0
  else        /* Unselect all */
6439
0
    {
6440
0
      for (node = keyblock; node; node = node->next)
6441
0
  if (node->pkt->pkttype == PKT_USER_ID)
6442
0
    node->flag &= ~NODFLG_SELUID;
6443
0
    }
6444
6445
0
  return 1;
6446
0
}
6447
6448
6449
/* Search in the keyblock for a uid that matches namehash */
6450
static int
6451
menu_select_uid_namehash (KBNODE keyblock, const char *namehash)
6452
0
{
6453
0
  byte hash[NAMEHASH_LEN];
6454
0
  KBNODE node;
6455
0
  int i;
6456
6457
0
  log_assert (strlen (namehash) == NAMEHASH_LEN * 2);
6458
6459
0
  for (i = 0; i < NAMEHASH_LEN; i++)
6460
0
    hash[i] = hextobyte (&namehash[i * 2]);
6461
6462
0
  for (node = keyblock->next; node; node = node->next)
6463
0
    {
6464
0
      if (node->pkt->pkttype == PKT_USER_ID)
6465
0
  {
6466
0
    namehash_from_uid (node->pkt->pkt.user_id);
6467
0
    if (memcmp (node->pkt->pkt.user_id->namehash, hash, NAMEHASH_LEN) ==
6468
0
        0)
6469
0
      {
6470
0
        if (node->flag & NODFLG_SELUID)
6471
0
    node->flag &= ~NODFLG_SELUID;
6472
0
        else
6473
0
    node->flag |= NODFLG_SELUID;
6474
6475
0
        break;
6476
0
      }
6477
0
  }
6478
0
    }
6479
6480
0
  if (!node)
6481
0
    {
6482
0
      tty_printf (_("No user ID with hash %s\n"), namehash);
6483
0
      return 0;
6484
0
    }
6485
6486
0
  return 1;
6487
0
}
6488
6489
6490
/*
6491
 * Select secondary keys
6492
 * Returns: True if the selection changed.
6493
 */
6494
static int
6495
menu_select_key (KBNODE keyblock, int idx, char *p)
6496
0
{
6497
0
  KBNODE node;
6498
0
  int i, j;
6499
0
  int is_hex_digits;
6500
6501
0
  is_hex_digits = p && strlen (p) >= 8;
6502
0
  if (is_hex_digits)
6503
0
    {
6504
      /* Skip initial spaces.  */
6505
0
      while (spacep (p))
6506
0
        p ++;
6507
      /* If the id starts with 0x accept and ignore it.  */
6508
0
      if (p[0] == '0' && p[1] == 'x')
6509
0
        p += 2;
6510
6511
0
      for (i = 0, j = 0; p[i]; i ++)
6512
0
        if (hexdigitp (&p[i]))
6513
0
          {
6514
0
            p[j] = toupper (p[i]);
6515
0
            j ++;
6516
0
          }
6517
0
        else if (spacep (&p[i]))
6518
          /* Skip spaces.  */
6519
0
          {
6520
0
          }
6521
0
        else
6522
0
          {
6523
0
            is_hex_digits = 0;
6524
0
            break;
6525
0
          }
6526
0
      if (is_hex_digits)
6527
        /* In case we skipped some spaces, add a new NUL terminator.  */
6528
0
        {
6529
0
          p[j] = 0;
6530
          /* If we skipped some spaces, make sure that we still have
6531
             at least 8 characters.  */
6532
0
          is_hex_digits = (/* Short keyid.  */
6533
0
                           strlen (p) == 8
6534
                           /* Long keyid.  */
6535
0
                           || strlen (p) == 16
6536
                           /* Fingerprints are (currently) 32 or 40
6537
                              characters.  */
6538
0
                           || strlen (p) >= 32);
6539
0
        }
6540
0
    }
6541
6542
0
  if (is_hex_digits)
6543
0
    {
6544
0
      int found_one = 0;
6545
0
      for (node = keyblock; node; node = node->next)
6546
0
  if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
6547
0
      || node->pkt->pkttype == PKT_SECRET_SUBKEY)
6548
0
          {
6549
0
            int match = 0;
6550
0
            if (strlen (p) == 8 || strlen (p) == 16)
6551
0
              {
6552
0
                u32 kid[2];
6553
0
                char kid_str[17];
6554
0
                keyid_from_pk (node->pkt->pkt.public_key, kid);
6555
0
                format_keyid (kid, strlen (p) == 8 ? KF_SHORT : KF_LONG,
6556
0
                              kid_str, sizeof (kid_str));
6557
6558
0
                if (strcmp (p, kid_str) == 0)
6559
0
                  match = 1;
6560
0
              }
6561
0
            else
6562
0
              {
6563
0
                char fp[2*MAX_FINGERPRINT_LEN + 1];
6564
0
                hexfingerprint (node->pkt->pkt.public_key, fp, sizeof (fp));
6565
0
                if (strcmp (fp, p) == 0)
6566
0
                  match = 1;
6567
0
              }
6568
6569
0
            if (match)
6570
0
              {
6571
0
                if ((node->flag & NODFLG_SELKEY))
6572
0
                  node->flag &= ~NODFLG_SELKEY;
6573
0
                else
6574
0
                  node->flag |= NODFLG_SELKEY;
6575
6576
0
                found_one = 1;
6577
0
              }
6578
0
          }
6579
6580
0
      if (found_one)
6581
0
        return 1;
6582
6583
0
      tty_printf (_("No subkey with key ID '%s'.\n"), p);
6584
0
      return 0;
6585
0
    }
6586
6587
0
  if (idx == -1)   /* Select all.  */
6588
0
    {
6589
0
      for (node = keyblock; node; node = node->next)
6590
0
  if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
6591
0
      || node->pkt->pkttype == PKT_SECRET_SUBKEY)
6592
0
    node->flag |= NODFLG_SELKEY;
6593
0
    }
6594
0
  else if (idx)     /* Toggle selection.  */
6595
0
    {
6596
0
      for (i = 0, node = keyblock; node; node = node->next)
6597
0
  {
6598
0
    if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
6599
0
        || node->pkt->pkttype == PKT_SECRET_SUBKEY)
6600
0
      if (++i == idx)
6601
0
        break;
6602
0
  }
6603
0
      if (!node)
6604
0
  {
6605
0
    tty_printf (_("No subkey with index %d\n"), idx);
6606
0
    return 0;
6607
0
  }
6608
6609
0
      for (i = 0, node = keyblock; node; node = node->next)
6610
0
  {
6611
0
    if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
6612
0
        || node->pkt->pkttype == PKT_SECRET_SUBKEY)
6613
0
      if (++i == idx)
6614
0
        {
6615
0
    if ((node->flag & NODFLG_SELKEY))
6616
0
      node->flag &= ~NODFLG_SELKEY;
6617
0
    else
6618
0
      node->flag |= NODFLG_SELKEY;
6619
0
        }
6620
0
  }
6621
0
    }
6622
0
  else        /* Unselect all. */
6623
0
    {
6624
0
      for (node = keyblock; node; node = node->next)
6625
0
  if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
6626
0
      || node->pkt->pkttype == PKT_SECRET_SUBKEY)
6627
0
    node->flag &= ~NODFLG_SELKEY;
6628
0
    }
6629
6630
0
  return 1;
6631
0
}
6632
6633
6634
static int
6635
count_uids_with_flag (KBNODE keyblock, unsigned flag)
6636
0
{
6637
0
  KBNODE node;
6638
0
  int i = 0;
6639
6640
0
  for (node = keyblock; node; node = node->next)
6641
0
    if (node->pkt->pkttype == PKT_USER_ID && (node->flag & flag))
6642
0
      i++;
6643
0
  return i;
6644
0
}
6645
6646
6647
static int
6648
count_keys_with_flag (KBNODE keyblock, unsigned flag)
6649
0
{
6650
0
  KBNODE node;
6651
0
  int i = 0;
6652
6653
0
  for (node = keyblock; node; node = node->next)
6654
0
    if ((node->pkt->pkttype == PKT_PUBLIC_SUBKEY
6655
0
   || node->pkt->pkttype == PKT_SECRET_SUBKEY) && (node->flag & flag))
6656
0
      i++;
6657
0
  return i;
6658
0
}
6659
6660
6661
static int
6662
count_uids (KBNODE keyblock)
6663
0
{
6664
0
  KBNODE node;
6665
0
  int i = 0;
6666
6667
0
  for (node = keyblock; node; node = node->next)
6668
0
    if (node->pkt->pkttype == PKT_USER_ID)
6669
0
      i++;
6670
0
  return i;
6671
0
}
6672
6673
6674
/*
6675
 * Returns true if there is at least one selected user id
6676
 */
6677
static int
6678
count_selected_uids (KBNODE keyblock)
6679
0
{
6680
0
  return count_uids_with_flag (keyblock, NODFLG_SELUID);
6681
0
}
6682
6683
6684
static int
6685
count_selected_keys (KBNODE keyblock)
6686
0
{
6687
0
  return count_keys_with_flag (keyblock, NODFLG_SELKEY);
6688
0
}
6689
6690
6691
/* Returns how many real (i.e. not attribute) uids are unmarked.  */
6692
static int
6693
real_uids_left (KBNODE keyblock)
6694
0
{
6695
0
  KBNODE node;
6696
0
  int real = 0;
6697
6698
0
  for (node = keyblock; node; node = node->next)
6699
0
    if (node->pkt->pkttype == PKT_USER_ID && !(node->flag & NODFLG_SELUID) &&
6700
0
  !node->pkt->pkt.user_id->attrib_data)
6701
0
      real++;
6702
6703
0
  return real;
6704
0
}
6705
6706
6707
/*
6708
 * Ask whether the signature should be revoked.  If the user commits this,
6709
 * flag bit MARK_A is set on the signature and the user ID.
6710
 */
6711
static void
6712
ask_revoke_sig (ctrl_t ctrl, kbnode_t keyblock, kbnode_t node)
6713
0
{
6714
0
  int doit = 0;
6715
0
  PKT_user_id *uid;
6716
0
  PKT_signature *sig = node->pkt->pkt.signature;
6717
0
  KBNODE unode = find_prev_kbnode (keyblock, node, PKT_USER_ID);
6718
6719
0
  if (!unode)
6720
0
    {
6721
0
      log_error ("Oops: no user ID for signature\n");
6722
0
      return;
6723
0
    }
6724
6725
0
  uid = unode->pkt->pkt.user_id;
6726
6727
0
  if (opt.with_colons)
6728
0
    {
6729
0
      if (uid->attrib_data)
6730
0
  printf ("uat:::::::::%u %lu", uid->numattribs, uid->attrib_len);
6731
0
      else
6732
0
  {
6733
0
    es_printf ("uid:::::::::");
6734
0
    es_write_sanitized (es_stdout, uid->name, uid->len, ":", NULL);
6735
0
  }
6736
6737
0
      es_printf ("\n");
6738
6739
0
      print_and_check_one_sig_colon (ctrl, keyblock, node,
6740
0
                                     NULL, NULL, NULL, NULL, 1);
6741
0
    }
6742
0
  else
6743
0
    {
6744
0
      char *p = utf8_to_native (unode->pkt->pkt.user_id->name,
6745
0
        unode->pkt->pkt.user_id->len, 0);
6746
0
      tty_printf (_("user ID: \"%s\"\n"), p);
6747
0
      xfree (p);
6748
6749
0
      tty_printf (_("signed by your key %s on %s%s%s\n"),
6750
0
      keystr (sig->keyid), datestr_from_sig (sig),
6751
0
      sig->flags.exportable ? "" : _(" (non-exportable)"), "");
6752
0
    }
6753
0
  if (sig->flags.expired)
6754
0
    {
6755
0
      tty_printf (_("This signature expired on %s.\n"),
6756
0
      expirestr_from_sig (sig));
6757
      /* Use a different question so we can have different help text */
6758
0
      doit = cpr_get_answer_is_yes
6759
0
        ("ask_revoke_sig.expired",
6760
0
         _("Are you sure you still want to revoke it? (y/N) "));
6761
0
    }
6762
0
  else
6763
0
    doit = cpr_get_answer_is_yes
6764
0
      ("ask_revoke_sig.one",
6765
0
       _("Create a revocation certificate for this signature? (y/N) "));
6766
6767
0
  if (doit)
6768
0
    {
6769
0
      node->flag |= NODFLG_MARK_A;
6770
0
      unode->flag |= NODFLG_MARK_A;
6771
0
    }
6772
0
}
6773
6774
6775
/*
6776
 * Display all user ids of the current public key together with signatures
6777
 * done by one of our keys.  Then walk over all this sigs and ask the user
6778
 * whether he wants to revoke this signature.
6779
 * Return: True when the keyblock has changed.
6780
 */
6781
static int
6782
menu_revsig (ctrl_t ctrl, kbnode_t keyblock)
6783
0
{
6784
0
  PKT_signature *sig;
6785
0
  PKT_public_key *primary_pk;
6786
0
  KBNODE node;
6787
0
  int changed = 0;
6788
0
  int rc, any, skip = 1, all = !count_selected_uids (keyblock);
6789
0
  struct revocation_reason_info *reason = NULL;
6790
6791
0
  log_assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
6792
6793
  /* First check whether we have any signatures at all.  */
6794
0
  any = 0;
6795
0
  for (node = keyblock; node; node = node->next)
6796
0
    {
6797
0
      node->flag &= ~(NODFLG_SELSIG | NODFLG_MARK_A);
6798
0
      if (node->pkt->pkttype == PKT_USER_ID)
6799
0
  {
6800
0
    if (node->flag & NODFLG_SELUID || all)
6801
0
      skip = 0;
6802
0
    else
6803
0
      skip = 1;
6804
0
  }
6805
0
      else if (!skip && node->pkt->pkttype == PKT_SIGNATURE
6806
0
         && ((sig = node->pkt->pkt.signature),
6807
0
       have_secret_key_with_kid (ctrl, sig->keyid)))
6808
0
  {
6809
0
    if ((sig->sig_class & ~3) == 0x10)
6810
0
      {
6811
0
        any = 1;
6812
0
        break;
6813
0
      }
6814
0
  }
6815
0
    }
6816
6817
0
  if (!any)
6818
0
    {
6819
0
      tty_printf (_("Not signed by you.\n"));
6820
0
      return 0;
6821
0
    }
6822
6823
6824
  /* FIXME: detect duplicates here  */
6825
0
  tty_printf (_("You have signed these user IDs on key %s:\n"),
6826
0
        keystr_from_pk (keyblock->pkt->pkt.public_key));
6827
0
  for (node = keyblock; node; node = node->next)
6828
0
    {
6829
0
      node->flag &= ~(NODFLG_SELSIG | NODFLG_MARK_A);
6830
0
      if (node->pkt->pkttype == PKT_USER_ID)
6831
0
  {
6832
0
    if (node->flag & NODFLG_SELUID || all)
6833
0
      {
6834
0
        PKT_user_id *uid = node->pkt->pkt.user_id;
6835
        /* Hmmm: Should we show only UIDs with a signature? */
6836
0
        tty_printf ("     ");
6837
0
        tty_print_utf8_string (uid->name, uid->len);
6838
0
        tty_printf ("\n");
6839
0
        skip = 0;
6840
0
      }
6841
0
    else
6842
0
      skip = 1;
6843
0
  }
6844
0
      else if (!skip && node->pkt->pkttype == PKT_SIGNATURE
6845
0
         && ((sig = node->pkt->pkt.signature),
6846
0
       have_secret_key_with_kid (ctrl, sig->keyid)))
6847
0
  {
6848
0
    if ((sig->sig_class & ~3) == 0x10)
6849
0
      {
6850
0
        tty_printf ("   ");
6851
0
        tty_printf (_("signed by your key %s on %s%s%s\n"),
6852
0
        keystr (sig->keyid), datestr_from_sig (sig),
6853
0
        sig->flags.exportable ? "" : _(" (non-exportable)"),
6854
0
        sig->flags.revocable ? "" : _(" (non-revocable)"));
6855
0
        if (sig->flags.revocable)
6856
0
    node->flag |= NODFLG_SELSIG;
6857
0
      }
6858
0
    else if (sig->sig_class == 0x30)
6859
0
      {
6860
0
        tty_printf ("   ");
6861
0
        tty_printf (_("revoked by your key %s on %s\n"),
6862
0
        keystr (sig->keyid), datestr_from_sig (sig));
6863
0
      }
6864
0
  }
6865
0
    }
6866
6867
0
  tty_printf ("\n");
6868
6869
  /* ask */
6870
0
  for (node = keyblock; node; node = node->next)
6871
0
    {
6872
0
      if (!(node->flag & NODFLG_SELSIG))
6873
0
  continue;
6874
0
      ask_revoke_sig (ctrl, keyblock, node);
6875
0
    }
6876
6877
  /* present selected */
6878
0
  any = 0;
6879
0
  for (node = keyblock; node; node = node->next)
6880
0
    {
6881
0
      if (!(node->flag & NODFLG_MARK_A))
6882
0
  continue;
6883
0
      if (!any)
6884
0
  {
6885
0
    any = 1;
6886
0
    tty_printf (_("You are about to revoke these signatures:\n"));
6887
0
  }
6888
0
      if (node->pkt->pkttype == PKT_USER_ID)
6889
0
  {
6890
0
    PKT_user_id *uid = node->pkt->pkt.user_id;
6891
0
    tty_printf ("     ");
6892
0
    tty_print_utf8_string (uid->name, uid->len);
6893
0
    tty_printf ("\n");
6894
0
  }
6895
0
      else if (node->pkt->pkttype == PKT_SIGNATURE)
6896
0
  {
6897
0
    sig = node->pkt->pkt.signature;
6898
0
    tty_printf ("   ");
6899
0
    tty_printf (_("signed by your key %s on %s%s%s\n"),
6900
0
          keystr (sig->keyid), datestr_from_sig (sig), "",
6901
0
          sig->flags.exportable ? "" : _(" (non-exportable)"));
6902
0
  }
6903
0
    }
6904
0
  if (!any)
6905
0
    return 0;     /* none selected */
6906
6907
0
  if (!cpr_get_answer_is_yes
6908
0
      ("ask_revoke_sig.okay",
6909
0
       _("Really create the revocation certificates? (y/N) ")))
6910
0
    return 0;     /* forget it */
6911
6912
0
  reason = ask_revocation_reason (0, 1, 0);
6913
0
  if (!reason)
6914
0
    {       /* user decided to cancel */
6915
0
      return 0;
6916
0
    }
6917
6918
  /* now we can sign the user ids */
6919
0
reloop:     /* (must use this, because we are modifying the list) */
6920
0
  primary_pk = keyblock->pkt->pkt.public_key;
6921
0
  for (node = keyblock; node; node = node->next)
6922
0
    {
6923
0
      KBNODE unode;
6924
0
      PACKET *pkt;
6925
0
      struct sign_attrib attrib;
6926
0
      PKT_public_key *signerkey;
6927
6928
0
      if (!(node->flag & NODFLG_MARK_A)
6929
0
    || node->pkt->pkttype != PKT_SIGNATURE)
6930
0
  continue;
6931
0
      unode = find_prev_kbnode (keyblock, node, PKT_USER_ID);
6932
0
      log_assert (unode); /* we already checked this */
6933
6934
0
      memset (&attrib, 0, sizeof attrib);
6935
0
      attrib.reason = reason;
6936
0
      attrib.non_exportable = !node->pkt->pkt.signature->flags.exportable;
6937
6938
0
      node->flag &= ~NODFLG_MARK_A;
6939
0
      signerkey = xmalloc_secure_clear (sizeof *signerkey);
6940
0
      if (get_seckey (ctrl, signerkey, node->pkt->pkt.signature->keyid))
6941
0
  {
6942
0
    log_info (_("no secret key\n"));
6943
0
          free_public_key (signerkey);
6944
0
    continue;
6945
0
  }
6946
0
      rc = make_keysig_packet (ctrl, &sig, primary_pk,
6947
0
             unode->pkt->pkt.user_id,
6948
0
             NULL, signerkey, 0x30, 0, 0,
6949
0
                               sign_mk_attrib, &attrib, NULL);
6950
0
      free_public_key (signerkey);
6951
0
      if (rc)
6952
0
  {
6953
0
          write_status_error ("keysig", rc);
6954
0
    log_error (_("signing failed: %s\n"), gpg_strerror (rc));
6955
0
    release_revocation_reason_info (reason);
6956
0
    return changed;
6957
0
  }
6958
0
      changed = 1;    /* we changed the keyblock */
6959
0
      update_trust = 1;
6960
      /* Are we revoking our own uid? */
6961
0
      if (primary_pk->keyid[0] == sig->keyid[0] &&
6962
0
    primary_pk->keyid[1] == sig->keyid[1])
6963
0
  unode->pkt->pkt.user_id->flags.revoked = 1;
6964
0
      pkt = xmalloc_clear (sizeof *pkt);
6965
0
      pkt->pkttype = PKT_SIGNATURE;
6966
0
      pkt->pkt.signature = sig;
6967
0
      insert_kbnode (unode, new_kbnode (pkt), 0);
6968
0
      goto reloop;
6969
0
    }
6970
6971
0
  release_revocation_reason_info (reason);
6972
0
  return changed;
6973
0
}
6974
6975
6976
/* return 0 if revocation of NODE (which must be a User ID) was
6977
   successful, non-zero if there was an error.  *modified will be set
6978
   to 1 if a change was made. */
6979
static int
6980
core_revuid (ctrl_t ctrl, kbnode_t keyblock, KBNODE node,
6981
             const struct revocation_reason_info *reason, int *modified)
6982
0
{
6983
0
  PKT_public_key *pk = keyblock->pkt->pkt.public_key;
6984
0
  gpg_error_t rc;
6985
6986
0
  if (node->pkt->pkttype != PKT_USER_ID)
6987
0
    {
6988
0
      rc = gpg_error (GPG_ERR_NO_USER_ID);
6989
0
      write_status_error ("keysig", rc);
6990
0
      log_error (_("tried to revoke a non-user ID: %s\n"), gpg_strerror (rc));
6991
0
      return 1;
6992
0
    }
6993
0
  else
6994
0
    {
6995
0
      PKT_user_id *uid = node->pkt->pkt.user_id;
6996
6997
0
      if (uid->flags.revoked)
6998
0
        {
6999
0
          char *user = utf8_to_native (uid->name, uid->len, 0);
7000
0
          log_info (_("user ID \"%s\" is already revoked\n"), user);
7001
0
          xfree (user);
7002
0
        }
7003
0
      else
7004
0
        {
7005
0
          PACKET *pkt;
7006
0
          PKT_signature *sig;
7007
0
          struct sign_attrib attrib;
7008
0
          u32 timestamp = make_timestamp ();
7009
7010
0
          if (uid->created >= timestamp)
7011
0
            {
7012
              /* Okay, this is a problem.  The user ID selfsig was
7013
                 created in the future, so we need to warn the user and
7014
                 set our revocation timestamp one second after that so
7015
                 everything comes out clean. */
7016
7017
0
              log_info (_("WARNING: a user ID signature is dated %d"
7018
0
                          " seconds in the future\n"),
7019
0
                        uid->created - timestamp);
7020
7021
0
              timestamp = uid->created + 1;
7022
0
            }
7023
7024
0
          memset (&attrib, 0, sizeof attrib);
7025
          /* should not need to cast away const here; but
7026
             revocation_reason_build_cb needs to take a non-const
7027
             void* in order to meet the function signature for the
7028
             mksubpkt argument to make_keysig_packet */
7029
0
          attrib.reason = (struct revocation_reason_info *)reason;
7030
7031
0
          rc = make_keysig_packet (ctrl, &sig, pk, uid, NULL, pk, 0x30,
7032
0
                                   timestamp, 0,
7033
0
                                   sign_mk_attrib, &attrib, NULL);
7034
0
          if (rc)
7035
0
            {
7036
0
              write_status_error ("keysig", rc);
7037
0
              log_error (_("signing failed: %s\n"), gpg_strerror (rc));
7038
0
              return 1;
7039
0
            }
7040
0
          else
7041
0
            {
7042
0
              pkt = xmalloc_clear (sizeof *pkt);
7043
0
              pkt->pkttype = PKT_SIGNATURE;
7044
0
              pkt->pkt.signature = sig;
7045
0
              insert_kbnode (node, new_kbnode (pkt), 0);
7046
7047
0
#ifndef NO_TRUST_MODELS
7048
              /* If the trustdb has an entry for this key+uid then the
7049
                 trustdb needs an update. */
7050
0
              if (!update_trust
7051
0
                  && ((get_validity (ctrl, keyblock, pk, uid, NULL, 0)
7052
0
                       & TRUST_MASK)
7053
0
                      >= TRUST_UNDEFINED))
7054
0
                update_trust = 1;
7055
0
#endif /*!NO_TRUST_MODELS*/
7056
7057
0
              node->pkt->pkt.user_id->flags.revoked = 1;
7058
0
              if (modified)
7059
0
                *modified = 1;
7060
0
            }
7061
0
        }
7062
0
      return 0;
7063
0
    }
7064
0
}
7065
7066
/* Revoke a user ID (i.e. revoke a user ID selfsig).  Return true if
7067
   keyblock changed.  */
7068
static int
7069
menu_revuid (ctrl_t ctrl, kbnode_t pub_keyblock)
7070
0
{
7071
0
  PKT_public_key *pk = pub_keyblock->pkt->pkt.public_key;
7072
0
  KBNODE node;
7073
0
  int changed = 0;
7074
0
  int rc;
7075
0
  struct revocation_reason_info *reason = NULL;
7076
0
  size_t valid_uids;
7077
7078
  /* Note that this is correct as per the RFCs, but nevertheless
7079
     somewhat meaningless in the real world.  1991 did define the 0x30
7080
     sig class, but PGP 2.x did not actually implement it, so it would
7081
     probably be safe to use v4 revocations everywhere. -ds */
7082
7083
0
  for (node = pub_keyblock; node; node = node->next)
7084
0
    if (pk->version > 3 || (node->pkt->pkttype == PKT_USER_ID &&
7085
0
          node->pkt->pkt.user_id->selfsigversion > 3))
7086
0
      {
7087
0
  if ((reason = ask_revocation_reason (0, 1, 4)))
7088
0
    break;
7089
0
  else
7090
0
    goto leave;
7091
0
      }
7092
7093
  /* Too make sure that we do not revoke the last valid UID, we first
7094
     count how many valid UIDs there are.  */
7095
0
  valid_uids = 0;
7096
0
  for (node = pub_keyblock; node; node = node->next)
7097
0
    valid_uids +=
7098
0
      node->pkt->pkttype == PKT_USER_ID
7099
0
      && ! node->pkt->pkt.user_id->flags.revoked
7100
0
      && ! node->pkt->pkt.user_id->flags.expired;
7101
7102
0
 reloop: /* (better this way because we are modifying the keyring) */
7103
0
  for (node = pub_keyblock; node; node = node->next)
7104
0
    if (node->pkt->pkttype == PKT_USER_ID && (node->flag & NODFLG_SELUID))
7105
0
      {
7106
0
        int modified = 0;
7107
7108
        /* Make sure that we do not revoke the last valid UID.  */
7109
0
        if (valid_uids == 1
7110
0
            && ! node->pkt->pkt.user_id->flags.revoked
7111
0
            && ! node->pkt->pkt.user_id->flags.expired)
7112
0
          {
7113
0
            log_error (_("Cannot revoke the last valid user ID.\n"));
7114
0
            goto leave;
7115
0
          }
7116
7117
0
        rc = core_revuid (ctrl, pub_keyblock, node, reason, &modified);
7118
0
        if (rc)
7119
0
          goto leave;
7120
0
        if (modified)
7121
0
          {
7122
0
      node->flag &= ~NODFLG_SELUID;
7123
0
            changed = 1;
7124
0
            goto reloop;
7125
0
          }
7126
0
      }
7127
7128
0
  if (changed)
7129
0
    commit_kbnode (&pub_keyblock);
7130
7131
0
leave:
7132
0
  release_revocation_reason_info (reason);
7133
0
  return changed;
7134
0
}
7135
7136
7137
/*
7138
 * Revoke the whole key.
7139
 */
7140
static int
7141
menu_revkey (ctrl_t ctrl, kbnode_t pub_keyblock)
7142
0
{
7143
0
  PKT_public_key *pk = pub_keyblock->pkt->pkt.public_key;
7144
0
  int rc, changed = 0;
7145
0
  struct revocation_reason_info *reason;
7146
0
  PACKET *pkt;
7147
0
  PKT_signature *sig;
7148
7149
0
  if (pk->flags.revoked)
7150
0
    {
7151
0
      tty_printf (_("Key %s is already revoked.\n"), keystr_from_pk (pk));
7152
0
      return 0;
7153
0
    }
7154
7155
0
  reason = ask_revocation_reason (1, 0, 0);
7156
  /* user decided to cancel */
7157
0
  if (!reason)
7158
0
    return 0;
7159
7160
0
  rc = make_keysig_packet (ctrl, &sig, pk, NULL, NULL, pk,
7161
0
         0x20, 0, 0,
7162
0
         revocation_reason_build_cb, reason, NULL);
7163
0
  if (rc)
7164
0
    {
7165
0
      write_status_error ("keysig", rc);
7166
0
      log_error (_("signing failed: %s\n"), gpg_strerror (rc));
7167
0
      goto scram;
7168
0
    }
7169
7170
0
  changed = 1;      /* we changed the keyblock */
7171
7172
0
  pkt = xmalloc_clear (sizeof *pkt);
7173
0
  pkt->pkttype = PKT_SIGNATURE;
7174
0
  pkt->pkt.signature = sig;
7175
0
  insert_kbnode (pub_keyblock, new_kbnode (pkt), 0);
7176
0
  commit_kbnode (&pub_keyblock);
7177
7178
0
  update_trust = 1;
7179
7180
0
 scram:
7181
0
  release_revocation_reason_info (reason);
7182
0
  return changed;
7183
0
}
7184
7185
7186
static int
7187
menu_revsubkey (ctrl_t ctrl, kbnode_t pub_keyblock)
7188
0
{
7189
0
  PKT_public_key *mainpk;
7190
0
  KBNODE node;
7191
0
  int changed = 0;
7192
0
  int rc;
7193
0
  struct revocation_reason_info *reason = NULL;
7194
7195
0
  reason = ask_revocation_reason (1, 0, 0);
7196
0
  if (!reason)
7197
0
      return 0; /* User decided to cancel.  */
7198
7199
0
 reloop: /* (better this way because we are modifying the keyring) */
7200
0
  mainpk = pub_keyblock->pkt->pkt.public_key;
7201
0
  for (node = pub_keyblock; node; node = node->next)
7202
0
    {
7203
0
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
7204
0
    && (node->flag & NODFLG_SELKEY))
7205
0
  {
7206
0
    PACKET *pkt;
7207
0
    PKT_signature *sig;
7208
0
    PKT_public_key *subpk = node->pkt->pkt.public_key;
7209
0
    struct sign_attrib attrib;
7210
7211
0
    if (subpk->flags.revoked)
7212
0
      {
7213
0
        tty_printf (_("Subkey %s is already revoked.\n"),
7214
0
        keystr_from_pk (subpk));
7215
0
        continue;
7216
0
      }
7217
7218
0
    memset (&attrib, 0, sizeof attrib);
7219
0
    attrib.reason = reason;
7220
7221
0
    node->flag &= ~NODFLG_SELKEY;
7222
0
    rc = make_keysig_packet (ctrl, &sig, mainpk, NULL, subpk, mainpk,
7223
0
           0x28, 0, 0, sign_mk_attrib, &attrib,
7224
0
                                   NULL);
7225
0
    if (rc)
7226
0
      {
7227
0
              write_status_error ("keysig", rc);
7228
0
        log_error (_("signing failed: %s\n"), gpg_strerror (rc));
7229
0
        release_revocation_reason_info (reason);
7230
0
        return changed;
7231
0
      }
7232
0
    changed = 1;    /* we changed the keyblock */
7233
7234
0
    pkt = xmalloc_clear (sizeof *pkt);
7235
0
    pkt->pkttype = PKT_SIGNATURE;
7236
0
    pkt->pkt.signature = sig;
7237
0
    insert_kbnode (node, new_kbnode (pkt), 0);
7238
0
    goto reloop;
7239
0
  }
7240
0
    }
7241
0
  commit_kbnode (&pub_keyblock);
7242
7243
  /* No need to set update_trust here since signing keys no longer
7244
     are used to certify other keys, so there is no change in trust
7245
     when revoking/removing them */
7246
7247
0
  release_revocation_reason_info (reason);
7248
0
  return changed;
7249
0
}
7250
7251
7252
/* Note that update_ownertrust is going to mark the trustdb dirty when
7253
   enabling or disabling a key.  This is arguably sub-optimal as
7254
   disabled keys are still counted in the web of trust, but perhaps
7255
   not worth adding extra complexity to change. -ds */
7256
#ifndef NO_TRUST_MODELS
7257
static int
7258
enable_disable_key (ctrl_t ctrl, kbnode_t keyblock, int disable)
7259
0
{
7260
0
  PKT_public_key *pk =
7261
0
    find_kbnode (keyblock, PKT_PUBLIC_KEY)->pkt->pkt.public_key;
7262
0
  unsigned int trust, newtrust;
7263
7264
0
  trust = newtrust = get_ownertrust (ctrl, pk);
7265
0
  newtrust &= ~TRUST_FLAG_DISABLED;
7266
0
  if (disable)
7267
0
    newtrust |= TRUST_FLAG_DISABLED;
7268
0
  if (trust == newtrust)
7269
0
    return 0;     /* already in that state */
7270
0
  update_ownertrust (ctrl, pk, newtrust);
7271
0
  return 0;
7272
0
}
7273
#endif /*!NO_TRUST_MODELS*/
7274
7275
7276
static void
7277
menu_showphoto (ctrl_t ctrl, kbnode_t keyblock)
7278
0
{
7279
0
  KBNODE node;
7280
0
  int select_all = !count_selected_uids (keyblock);
7281
0
  int count = 0;
7282
0
  PKT_public_key *pk = NULL;
7283
7284
  /* Look for the public key first.  We have to be really, really,
7285
     explicit as to which photo this is, and what key it is a UID on
7286
     since people may want to sign it. */
7287
7288
0
  for (node = keyblock; node; node = node->next)
7289
0
    {
7290
0
      if (node->pkt->pkttype == PKT_PUBLIC_KEY)
7291
0
  pk = node->pkt->pkt.public_key;
7292
0
      else if (node->pkt->pkttype == PKT_USER_ID)
7293
0
  {
7294
0
    PKT_user_id *uid = node->pkt->pkt.user_id;
7295
0
    count++;
7296
7297
0
    if ((select_all || (node->flag & NODFLG_SELUID)) &&
7298
0
        uid->attribs != NULL)
7299
0
      {
7300
0
        int i;
7301
7302
0
        for (i = 0; i < uid->numattribs; i++)
7303
0
    {
7304
0
      byte type;
7305
0
      u32 size;
7306
7307
0
      if (uid->attribs[i].type == ATTRIB_IMAGE &&
7308
0
          parse_image_header (&uid->attribs[i], &type, &size))
7309
0
        {
7310
0
          tty_printf (_("Displaying %s photo ID of size %ld for "
7311
0
            "key %s (uid %d)\n"),
7312
0
          image_type_to_string (type, 1),
7313
0
          (ulong) size, keystr_from_pk (pk), count);
7314
0
          show_photos (ctrl, &uid->attribs[i], 1, pk, uid);
7315
0
        }
7316
0
    }
7317
0
      }
7318
0
  }
7319
0
    }
7320
0
}