~ubuntu-branches/ubuntu/hardy/gnupg/hardy-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/* revoke.c
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
 *               2004 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>

#include "options.h"
#include "packet.h"
#include "errors.h"
#include "keydb.h"
#include "memory.h"
#include "util.h"
#include "main.h"
#include "ttyio.h"
#include "status.h"
#include "i18n.h"


struct revocation_reason_info {
    int code;
    char *desc;
};


int
revocation_reason_build_cb( PKT_signature *sig, void *opaque )
{
    struct revocation_reason_info *reason = opaque;
    char *ud = NULL;
    byte *buffer;
    size_t buflen = 1;

    if(!reason)
      return 0;

    if( reason->desc ) {
	ud = native_to_utf8( reason->desc );
	buflen += strlen(ud);
    }
    buffer = xmalloc( buflen );
    *buffer = reason->code;
    if( ud ) {
	memcpy(buffer+1, ud, strlen(ud) );
	xfree( ud );
    }

    build_sig_subpkt( sig, SIGSUBPKT_REVOC_REASON, buffer, buflen );
    xfree( buffer );
    return 0;
}

/* Outputs a minimal pk (as defined by 2440) from a keyblock.  A
   minimal pk consists of the public key packet and a user ID.  We try
   and pick a user ID that has a uid signature, and include it if
   possible. */
static int
export_minimal_pk(IOBUF out,KBNODE keyblock,
		  PKT_signature *revsig,PKT_signature *revkey)
{
  KBNODE node;
  PACKET pkt;
  PKT_user_id *uid=NULL;
  PKT_signature *selfsig=NULL;
  u32 keyid[2];
  int rc;

  node=find_kbnode(keyblock,PKT_PUBLIC_KEY);
  if(!node)
    {
      log_error("key incomplete\n");
      return G10ERR_GENERAL;
    }

  keyid_from_pk(node->pkt->pkt.public_key,keyid);

  pkt=*node->pkt;
  rc=build_packet(out,&pkt);
  if(rc)
    {
      log_error(_("build_packet failed: %s\n"), g10_errstr(rc) );
      return rc;
    }

  init_packet(&pkt);
  pkt.pkttype=PKT_SIGNATURE;

  /* the revocation itself, if any.  2440 likes this to come first. */
  if(revsig)
    {
      pkt.pkt.signature=revsig;
      rc=build_packet(out,&pkt);
      if(rc)
	{
	  log_error(_("build_packet failed: %s\n"), g10_errstr(rc) );
	  return rc;
	}
    }

  /* If a revkey in a 1F sig is present, include it too */
  if(revkey)
    {
      pkt.pkt.signature=revkey;
      rc=build_packet(out,&pkt);
      if(rc)
	{
	  log_error(_("build_packet failed: %s\n"), g10_errstr(rc) );
	  return rc;
	}
    }

  while(!selfsig)
    {
      KBNODE signode;

      node=find_next_kbnode(node,PKT_USER_ID);
      if(!node)
	{
	  /* We're out of user IDs - none were self-signed. */
	  if(uid)
	    break;
	  else
	    {
	      log_error(_("key %s has no user IDs\n"),keystr(keyid));
	      return G10ERR_GENERAL;
	    }
	}

      if(node->pkt->pkt.user_id->attrib_data)
	continue;

      uid=node->pkt->pkt.user_id;
      signode=node;

      while((signode=find_next_kbnode(signode,PKT_SIGNATURE)))
	{
	  if(keyid[0]==signode->pkt->pkt.signature->keyid[0] &&
	     keyid[1]==signode->pkt->pkt.signature->keyid[1] &&
	     IS_UID_SIG(signode->pkt->pkt.signature))
	    {
	      selfsig=signode->pkt->pkt.signature;
	      break;
	    }
	}
    }

  pkt.pkttype=PKT_USER_ID;
  pkt.pkt.user_id=uid;

  rc=build_packet(out,&pkt);
  if(rc)
    {
      log_error(_("build_packet failed: %s\n"), g10_errstr(rc) );
      return rc;
    }

  if(selfsig)
    {
      pkt.pkttype=PKT_SIGNATURE;
      pkt.pkt.signature=selfsig;

      rc=build_packet(out,&pkt);
      if(rc)
	{
	  log_error(_("build_packet failed: %s\n"), g10_errstr(rc) );
	  return rc;
	}
    }

  return 0;
}

/****************
 * Generate a revocation certificate for UNAME via a designated revoker
 */
int
gen_desig_revoke( const char *uname, STRLIST locusr )
{
    int rc = 0;
    armor_filter_context_t afx;
    PKT_public_key *pk = NULL;
    PKT_secret_key *sk = NULL;
    PKT_signature *sig = NULL;
    IOBUF out = NULL;
    struct revocation_reason_info *reason = NULL;
    KEYDB_HANDLE kdbhd;
    KEYDB_SEARCH_DESC desc;
    KBNODE keyblock=NULL,node;
    u32 keyid[2];
    int i,any=0;
    SK_LIST sk_list=NULL;

    if( opt.batch )
      {
	log_error(_("can't do this in batch mode\n"));
	return G10ERR_GENERAL;
      }

    memset( &afx, 0, sizeof afx);

    kdbhd = keydb_new (0);
    classify_user_id (uname, &desc);
    rc = desc.mode? keydb_search (kdbhd, &desc, 1) : G10ERR_INV_USER_ID;
    if (rc) {
	log_error (_("key \"%s\" not found: %s\n"),uname, g10_errstr (rc));
	goto leave;
    }

    rc = keydb_get_keyblock (kdbhd, &keyblock );
    if( rc ) {
	log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
	goto leave;
    }

    /* To parse the revkeys */
    merge_keys_and_selfsig(keyblock);

    /* get the key from the keyblock */
    node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
    if( !node ) 
      BUG ();

    pk=node->pkt->pkt.public_key;

    keyid_from_pk(pk,keyid);

    if(locusr)
      {
	rc=build_sk_list(locusr,&sk_list,0,PUBKEY_USAGE_CERT);
	if(rc)
	  goto leave;
      }

    /* Are we a designated revoker for this key? */

    if(!pk->revkey && pk->numrevkeys)
      BUG();

    for(i=0;i<pk->numrevkeys;i++)
      {
	SK_LIST list;

	if(sk)
	  free_secret_key(sk);

	if(sk_list)
	  {
	    for(list=sk_list;list;list=list->next)
	      {
		byte fpr[MAX_FINGERPRINT_LEN];
		size_t fprlen;

		fingerprint_from_sk(list->sk,fpr,&fprlen);

		/* Don't get involved with keys that don't have 160
		   bit fingerprints */
		if(fprlen!=20)
		  continue;

		if(memcmp(fpr,pk->revkey[i].fpr,20)==0)
		  break;
	      }

	    if(list)
	      sk=copy_secret_key(NULL,list->sk);
	    else
	      continue;
	  }
	else
	  {
	    sk=xmalloc_secure_clear(sizeof(*sk));
	    rc=get_seckey_byfprint(sk,pk->revkey[i].fpr,MAX_FINGERPRINT_LEN);
	  }

	/* We have the revocation key */
	if(!rc)
	  {
	    PKT_signature *revkey = NULL;

	    any = 1;

            print_pubkey_info (NULL, pk);
	    tty_printf ("\n");

	    tty_printf (_("To be revoked by:\n"));
            print_seckey_info (sk);

	    if(pk->revkey[i].class&0x40)
	      tty_printf(_("(This is a sensitive revocation key)\n"));
	    tty_printf("\n");

	    if( !cpr_get_answer_is_yes("gen_desig_revoke.okay",
         _("Create a designated revocation certificate for this key? (y/N) ")))
	      continue;

	    /* get the reason for the revocation (this is always v4) */
	    reason = ask_revocation_reason( 1, 0, 1 );
	    if( !reason )
	      continue;

	    rc = check_secret_key( sk, 0 );
	    if( rc )
	      continue;

	    if( !opt.armor )
	      tty_printf(_("ASCII armored output forced.\n"));

	    if( (rc = open_outfile( NULL, 0, &out )) )
	      goto leave;

	    afx.what = 1;
	    afx.hdrlines = "Comment: A designated revocation certificate"
	      " should follow\n";
	    iobuf_push_filter( out, armor_filter, &afx );

	    /* create it */
	    rc = make_keysig_packet( &sig, pk, NULL, NULL, sk, 0x20, 0,
				     0, 0, 0,
				     revocation_reason_build_cb, reason );
	    if( rc ) {
	      log_error(_("make_keysig_packet failed: %s\n"), g10_errstr(rc));
	      goto leave;
	    }

	    /* Spit out a minimal pk as well, since otherwise there is
               no way to know which key to attach this revocation to.
               Also include the direct key signature that contains
               this revocation key.  We're allowed to include
               sensitive revocation keys along with a revocation, as
               this may be the only time the recipient has seen it.
               Note that this means that if we have multiple different
               sensitive revocation keys in a given direct key
               signature, we're going to include them all here.  This
               is annoying, but the good outweighs the bad, since
               without including this a sensitive revoker can't really
               do their job.  People should not include multiple
               sensitive revocation keys in one signature: 2440 says
               "Note that it may be appropriate to isolate this
               subpacket within a separate signature so that it is not
               combined with other subpackets that need to be
               exported." -dms */

	    while(!revkey)
	      {
		KBNODE signode;

		signode=find_next_kbnode(node,PKT_SIGNATURE);
		if(!signode)
		  break;

		node=signode;

		if(keyid[0]==signode->pkt->pkt.signature->keyid[0] &&
		   keyid[1]==signode->pkt->pkt.signature->keyid[1] &&
		   IS_KEY_SIG(signode->pkt->pkt.signature))
		  {
		    int j;

		    for(j=0;j<signode->pkt->pkt.signature->numrevkeys;j++)
		      {
			if(pk->revkey[i].class==
			   signode->pkt->pkt.signature->revkey[j]->class &&
			   pk->revkey[i].algid==
			   signode->pkt->pkt.signature->revkey[j]->algid &&
			   memcmp(pk->revkey[i].fpr,
				  signode->pkt->pkt.signature->revkey[j]->fpr,
				  MAX_FINGERPRINT_LEN)==0)
			  {
			    revkey=signode->pkt->pkt.signature;
			    break;
			  }
		      }
		  }
	      }

	    if(!revkey)
	      BUG();

	    rc=export_minimal_pk(out,keyblock,sig,revkey);
	    if(rc)
	      goto leave;

	    /* and issue a usage notice */
	    tty_printf(_("Revocation certificate created.\n"));
	    break;
	  }
      }

    if(!any)
      log_error(_("no revocation keys found for \"%s\"\n"),uname);

  leave:
    if( pk )
	free_public_key( pk );
    if( sk )
	free_secret_key( sk );
    if( sig )
	free_seckey_enc( sig );

    release_sk_list(sk_list);

    if( rc )
	iobuf_cancel(out);
    else
	iobuf_close(out);
    release_revocation_reason_info( reason );
    return rc;
}


/****************
 * Generate a revocation certificate for UNAME
 */
int
gen_revoke( const char *uname )
{
    int rc = 0;
    armor_filter_context_t afx;
    PACKET pkt;
    PKT_secret_key *sk; /* used as pointer into a kbnode */
    PKT_public_key *pk = NULL;
    PKT_signature *sig = NULL;
    u32 sk_keyid[2];
    IOBUF out = NULL;
    KBNODE keyblock = NULL, pub_keyblock = NULL;
    KBNODE node;
    KEYDB_HANDLE kdbhd;
    struct revocation_reason_info *reason = NULL;
    KEYDB_SEARCH_DESC desc;

    if( opt.batch )
      {
	log_error(_("can't do this in batch mode\n"));
	return G10ERR_GENERAL;
      }

    memset( &afx, 0, sizeof afx);
    init_packet( &pkt );

    /* search the userid: 
     * We don't want the whole getkey stuff here but the entire keyblock
     */
    kdbhd = keydb_new (1);
    classify_user_id (uname, &desc);
    rc = desc.mode? keydb_search (kdbhd, &desc, 1) : G10ERR_INV_USER_ID;
    if (rc)
      {
	log_error (_("secret key \"%s\" not found: %s\n"),
                   uname, g10_errstr (rc));
	goto leave;
      }

    rc = keydb_get_keyblock (kdbhd, &keyblock );
    if( rc ) {
	log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
	goto leave;
    }

    /* get the keyid from the keyblock */
    node = find_kbnode( keyblock, PKT_SECRET_KEY );
    if( !node ) 
	BUG ();

    /* fixme: should make a function out of this stuff,
     * it's used all over the source */
    sk = node->pkt->pkt.secret_key;
    keyid_from_sk( sk, sk_keyid );
    print_seckey_info (sk);

    pk = xmalloc_clear( sizeof *pk );

    /* FIXME: We should get the public key direct from the secret one */

    pub_keyblock=get_pubkeyblock(sk_keyid);
    if(!pub_keyblock)
      {
	log_error(_("no corresponding public key: %s\n"), g10_errstr(rc) );
	goto leave;
      }

    node=find_kbnode(pub_keyblock,PKT_PUBLIC_KEY);
    if(!node)
      BUG();

    pk=node->pkt->pkt.public_key;

    if( cmp_public_secret_key( pk, sk ) ) {
	log_error(_("public key does not match secret key!\n") );
	rc = G10ERR_GENERAL;
	goto leave;
    }

    tty_printf("\n");
    if( !cpr_get_answer_is_yes("gen_revoke.okay",
		  _("Create a revocation certificate for this key? (y/N) ")) )
      {
	rc = 0;
	goto leave;
      }

    if(sk->version>=4 || opt.force_v4_certs) {
      /* get the reason for the revocation */
      reason = ask_revocation_reason( 1, 0, 1 );
      if( !reason ) { /* user decided to cancel */
	rc = 0;
	goto leave;
      }
    }

    switch( is_secret_key_protected( sk ) ) {
      case -1:
	log_error(_("unknown protection algorithm\n"));
	rc = G10ERR_PUBKEY_ALGO;
	break;
      case -3:
	tty_printf (_("Secret parts of primary key are not available.\n"));
        rc = G10ERR_NO_SECKEY;
        break;
      case 0:
	tty_printf(_("NOTE: This key is not protected!\n"));
	break;
      default:
        rc = check_secret_key( sk, 0 );
	break;
    }
    if( rc )
	goto leave;


    if( !opt.armor )
	tty_printf(_("ASCII armored output forced.\n"));

    if( (rc = open_outfile( NULL, 0, &out )) )
	goto leave;

    afx.what = 1;
    afx.hdrlines = "Comment: A revocation certificate should follow\n";
    iobuf_push_filter( out, armor_filter, &afx );

    /* create it */
    rc = make_keysig_packet( &sig, pk, NULL, NULL, sk, 0x20, 0,
			     opt.force_v4_certs?4:0, 0, 0,
			     revocation_reason_build_cb, reason );
    if( rc ) {
	log_error(_("make_keysig_packet failed: %s\n"), g10_errstr(rc));
	goto leave;
    }

    if(PGP2 || PGP6 || PGP7 || PGP8)
      {
	/* Use a minimal pk for PGPx mode, since PGP can't import bare
	   revocation certificates. */
	rc=export_minimal_pk(out,pub_keyblock,sig,NULL);
	if(rc)
	  goto leave;
      }
    else
      {
	init_packet( &pkt );
	pkt.pkttype = PKT_SIGNATURE;
	pkt.pkt.signature = sig;

	rc = build_packet( out, &pkt );
	if( rc ) {
	  log_error(_("build_packet failed: %s\n"), g10_errstr(rc) );
	  goto leave;
	}
      }

    /* and issue a usage notice */
    tty_printf(_("Revocation certificate created.\n\n"
"Please move it to a medium which you can hide away; if Mallory gets\n"
"access to this certificate he can use it to make your key unusable.\n"
"It is smart to print this certificate and store it away, just in case\n"
"your media become unreadable.  But have some caution:  The print system of\n"
"your machine might store the data and make it available to others!\n"));

  leave:
    if( sig )
	free_seckey_enc( sig );
    release_kbnode( keyblock );
    release_kbnode( pub_keyblock );
    keydb_release (kdbhd);
    if( rc )
	iobuf_cancel(out);
    else
	iobuf_close(out);
    release_revocation_reason_info( reason );
    return rc;
}



struct revocation_reason_info *
ask_revocation_reason( int key_rev, int cert_rev, int hint )
{
    int code=-1;
    char *description = NULL;
    struct revocation_reason_info *reason;
    const char *text_0 = _("No reason specified");
    const char *text_1 = _("Key has been compromised");
    const char *text_2 = _("Key is superseded");
    const char *text_3 = _("Key is no longer used");
    const char *text_4 = _("User ID is no longer valid");
    const char *code_text = NULL;

    do {
        code=-1;
	xfree(description);
	description = NULL;

	tty_printf(_("Please select the reason for the revocation:\n"));
	tty_printf(    "  0 = %s\n", text_0 );
	if( key_rev )
	    tty_printf("  1 = %s\n", text_1 );
	if( key_rev )
	    tty_printf("  2 = %s\n", text_2 );
	if( key_rev )
	    tty_printf("  3 = %s\n", text_3 );
	if( cert_rev )
	    tty_printf("  4 = %s\n", text_4 );
	tty_printf(    "  Q = %s\n", _("Cancel") );
	if( hint )
	    tty_printf(_("(Probably you want to select %d here)\n"), hint );

	while(code==-1) {
	    int n;
	    char *answer = cpr_get("ask_revocation_reason.code",
						_("Your decision? "));
	    trim_spaces( answer );
	    cpr_kill_prompt();
	    if( *answer == 'q' || *answer == 'Q')
	      return NULL; /* cancel */
	    if( hint && !*answer )
		n = hint;
	    else if(!digitp( answer ) )
 	        n = -1;
	    else
		n = atoi(answer);
	    xfree(answer);
	    if( n == 0 ) {
	        code = 0x00; /* no particular reason */
		code_text = text_0;
	    }
	    else if( key_rev && n == 1 ) {
		code = 0x02; /* key has been compromised */
		code_text = text_1;
	    }
	    else if( key_rev && n == 2 ) {
		code = 0x01; /* key is superseded */
		code_text = text_2;
	    }
	    else if( key_rev && n == 3 ) {
		code = 0x03; /* key is no longer used */
		code_text = text_3;
	    }
	    else if( cert_rev && n == 4 ) {
		code = 0x20; /* uid is no longer valid */
		code_text = text_4;
	    }
	    else
		tty_printf(_("Invalid selection.\n"));
	}

	tty_printf(_("Enter an optional description; "
		     "end it with an empty line:\n") );
	for(;;) {
	    char *answer = cpr_get("ask_revocation_reason.text", "> " );
	    trim_trailing_ws( answer, strlen(answer) );
	    cpr_kill_prompt();
	    if( !*answer ) {
		xfree(answer);
		break;
	    }

	    {
		char *p = make_printable_string( answer, strlen(answer), 0 );
		xfree(answer);
		answer = p;
	    }

	    if( !description )
		description = xstrdup(answer);
	    else {
		char *p = xmalloc( strlen(description) + strlen(answer) + 2 );
		strcpy(stpcpy(stpcpy( p, description),"\n"),answer);
		xfree(description);
		description = p;
	    }
	    xfree(answer);
	}

	tty_printf(_("Reason for revocation: %s\n"), code_text );
	if( !description )
	    tty_printf(_("(No description given)\n") );
	else
	    tty_printf("%s\n", description );

    } while( !cpr_get_answer_is_yes("ask_revocation_reason.okay",
					    _("Is this okay? (y/N) "))  );

    reason = xmalloc( sizeof *reason );
    reason->code = code;
    reason->desc = description;
    return reason;
}

void
release_revocation_reason_info( struct revocation_reason_info *reason )
{
    if( reason ) {
	xfree( reason->desc );
	xfree( reason );
    }
}