* gpg.texi (GPG Configuration Options): Make http_proxy option
[gnupg.git] / g10 / keyid.c
blob0de396f16b91b13c015cb6545668da791308ee54
1 /* keyid.c - key ID and fingerprint handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 * 2004, 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <assert.h>
31 #include "gpg.h"
32 #include "util.h"
33 #include "main.h"
34 #include "packet.h"
35 #include "options.h"
36 #include "keydb.h"
37 #include "i18n.h"
39 int
40 pubkey_letter( int algo )
42 switch( algo ) {
43 case PUBKEY_ALGO_RSA: return 'R' ;
44 case PUBKEY_ALGO_RSA_E: return 'r' ;
45 case PUBKEY_ALGO_RSA_S: return 's' ;
46 case PUBKEY_ALGO_ELGAMAL_E: return 'g';
47 case PUBKEY_ALGO_ELGAMAL: return 'G' ;
48 case PUBKEY_ALGO_DSA: return 'D' ;
49 default: return '?';
53 /* This function is useful for v4 fingerprints and v3 or v4 key
54 signing. */
55 void
56 hash_public_key( gcry_md_hd_t md, PKT_public_key *pk )
58 unsigned int n = 6;
59 unsigned int nn[PUBKEY_MAX_NPKEY];
60 byte *pp[PUBKEY_MAX_NPKEY];
61 int i;
62 unsigned int nbits;
63 size_t nbytes;
64 int npkey = pubkey_get_npkey (pk->pubkey_algo);
66 /* Two extra bytes for the expiration date in v3 */
67 if(pk->version<4)
68 n+=2;
70 if (npkey==0 && pk->pkey[0]
71 && gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
73 pp[0] = gcry_mpi_get_opaque (pk->pkey[0], &nbits);
74 nn[0] = (nbits+7)/8;
75 n+=nn[0];
77 else
78 for(i=0; i < npkey; i++ )
80 if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, pk->pkey[i]))
81 BUG ();
82 pp[i] = xmalloc (nbytes);
83 if (gcry_mpi_print (GCRYMPI_FMT_PGP, pp[i], nbytes,
84 &nbytes, pk->pkey[i]))
85 BUG ();
86 nn[i] = nbytes;
87 n += nn[i];
90 gcry_md_putc ( md, 0x99 ); /* ctb */
91 /* What does it mean if n is greater than than 0xFFFF ? */
92 gcry_md_putc ( md, n >> 8 ); /* 2 byte length header */
93 gcry_md_putc ( md, n );
94 gcry_md_putc ( md, pk->version );
96 gcry_md_putc ( md, pk->timestamp >> 24 );
97 gcry_md_putc ( md, pk->timestamp >> 16 );
98 gcry_md_putc ( md, pk->timestamp >> 8 );
99 gcry_md_putc ( md, pk->timestamp );
101 if(pk->version<4)
103 u16 days=0;
104 if(pk->expiredate)
105 days=(u16)((pk->expiredate - pk->timestamp) / 86400L);
107 gcry_md_putc ( md, days >> 8 );
108 gcry_md_putc ( md, days );
111 gcry_md_putc ( md, pk->pubkey_algo );
113 if(npkey==0 && pk->pkey[0]
114 && gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
116 gcry_md_write (md, pp[0], nn[0]);
118 else
119 for(i=0; i < npkey; i++ )
121 gcry_md_write ( md, pp[i], nn[i] );
122 xfree(pp[i]);
126 static gcry_md_hd_t
127 do_fingerprint_md( PKT_public_key *pk )
129 gcry_md_hd_t md;
131 if (gcry_md_open (&md, DIGEST_ALGO_SHA1, 0))
132 BUG ();
133 hash_public_key(md,pk);
134 gcry_md_final( md );
136 return md;
139 static gcry_md_hd_t
140 do_fingerprint_md_sk( PKT_secret_key *sk )
142 PKT_public_key pk;
143 int npkey = pubkey_get_npkey( sk->pubkey_algo ); /* npkey is correct! */
144 int i;
146 if(npkey==0)
147 return NULL;
149 pk.pubkey_algo = sk->pubkey_algo;
150 pk.version = sk->version;
151 pk.timestamp = sk->timestamp;
152 pk.expiredate = sk->expiredate;
153 pk.pubkey_algo = sk->pubkey_algo;
154 for( i=0; i < npkey; i++ )
155 pk.pkey[i] = sk->skey[i];
156 return do_fingerprint_md( &pk );
161 v3_keyid (gcry_mpi_t a, u32 *ki)
163 byte *buffer, *p;
164 size_t nbytes;
166 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &nbytes, a ))
167 BUG ();
168 /* fixme: allocate it on the stack */
169 buffer = xmalloc (nbytes);
170 if (gcry_mpi_print( GCRYMPI_FMT_USG, buffer, nbytes, NULL, a ))
171 BUG ();
172 if (nbytes < 8) /* oops */
173 ki[0] = ki[1] = 0;
174 else
176 p = buffer + nbytes - 8;
177 ki[0] = (p[0] << 24) | (p[1] <<16) | (p[2] << 8) | p[3];
178 p += 4;
179 ki[1] = (p[0] << 24) | (p[1] <<16) | (p[2] << 8) | p[3];
181 xfree (buffer);
182 return ki[1];
186 size_t
187 keystrlen(void)
189 switch(opt.keyid_format)
191 case KF_SHORT:
192 return 8;
194 case KF_LONG:
195 return 16;
197 case KF_0xSHORT:
198 return 10;
200 case KF_0xLONG:
201 return 18;
203 default:
204 BUG();
208 const char *
209 keystr(u32 *keyid)
211 static char keyid_str[19];
213 switch(opt.keyid_format)
215 case KF_SHORT:
216 sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
217 break;
219 case KF_LONG:
220 if(keyid[0])
221 sprintf(keyid_str,"%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
222 else
223 sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
224 break;
226 case KF_0xSHORT:
227 sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
228 break;
230 case KF_0xLONG:
231 if(keyid[0])
232 sprintf(keyid_str,"0x%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
233 else
234 sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
235 break;
237 default:
238 BUG();
241 return keyid_str;
244 const char *
245 keystr_from_pk(PKT_public_key *pk)
247 keyid_from_pk(pk,NULL);
249 return keystr(pk->keyid);
252 const char *
253 keystr_from_sk(PKT_secret_key *sk)
255 keyid_from_sk(sk,NULL);
257 return keystr(sk->keyid);
260 const char *
261 keystr_from_desc(KEYDB_SEARCH_DESC *desc)
263 switch(desc->mode)
265 case KEYDB_SEARCH_MODE_LONG_KID:
266 case KEYDB_SEARCH_MODE_SHORT_KID:
267 return keystr(desc->u.kid);
269 case KEYDB_SEARCH_MODE_FPR20:
271 u32 keyid[2];
273 keyid[0] = ((unsigned char)desc->u.fpr[12] << 24
274 | (unsigned char)desc->u.fpr[13] << 16
275 | (unsigned char)desc->u.fpr[14] << 8
276 | (unsigned char)desc->u.fpr[15]);
277 keyid[1] = ((unsigned char)desc->u.fpr[16] << 24
278 | (unsigned char)desc->u.fpr[17] << 16
279 | (unsigned char)desc->u.fpr[18] << 8
280 | (unsigned char)desc->u.fpr[19]);
282 return keystr(keyid);
285 case KEYDB_SEARCH_MODE_FPR16:
286 return "?v3 fpr?";
288 default:
289 BUG();
293 /****************
294 * Get the keyid from the secret key and put it into keyid
295 * if this is not NULL. Return the 32 low bits of the keyid.
298 keyid_from_sk( PKT_secret_key *sk, u32 *keyid )
300 u32 lowbits;
301 u32 dummy_keyid[2];
303 if( !keyid )
304 keyid = dummy_keyid;
306 if( sk->keyid[0] || sk->keyid[1] )
308 keyid[0] = sk->keyid[0];
309 keyid[1] = sk->keyid[1];
310 lowbits = keyid[1];
312 else if( sk->version < 4 )
314 if( is_RSA(sk->pubkey_algo) )
316 lowbits = (pubkey_get_npkey (sk->pubkey_algo) ?
317 v3_keyid( sk->skey[0], keyid ) : 0); /* Take n. */
318 sk->keyid[0]=keyid[0];
319 sk->keyid[1]=keyid[1];
321 else
322 sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
324 else
326 const byte *dp;
327 gcry_md_hd_t md;
329 md = do_fingerprint_md_sk(sk);
330 if(md)
332 dp = gcry_md_read (md, 0);
333 keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
334 keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
335 lowbits = keyid[1];
336 gcry_md_close (md);
337 sk->keyid[0] = keyid[0];
338 sk->keyid[1] = keyid[1];
340 else
341 sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
344 return lowbits;
348 /****************
349 * Get the keyid from the public key and put it into keyid
350 * if this is not NULL. Return the 32 low bits of the keyid.
353 keyid_from_pk( PKT_public_key *pk, u32 *keyid )
355 u32 lowbits;
356 u32 dummy_keyid[2];
358 if( !keyid )
359 keyid = dummy_keyid;
361 if( pk->keyid[0] || pk->keyid[1] )
363 keyid[0] = pk->keyid[0];
364 keyid[1] = pk->keyid[1];
365 lowbits = keyid[1];
367 else if( pk->version < 4 )
369 if( is_RSA(pk->pubkey_algo) )
371 lowbits = (pubkey_get_npkey (pk->pubkey_algo) ?
372 v3_keyid ( pk->pkey[0], keyid ) : 0); /* From n. */
373 pk->keyid[0] = keyid[0];
374 pk->keyid[1] = keyid[1];
376 else
377 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
379 else
381 const byte *dp;
382 gcry_md_hd_t md;
384 md = do_fingerprint_md(pk);
385 if(md)
387 dp = gcry_md_read ( md, 0 );
388 keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
389 keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
390 lowbits = keyid[1];
391 gcry_md_close (md);
392 pk->keyid[0] = keyid[0];
393 pk->keyid[1] = keyid[1];
395 else
396 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
399 return lowbits;
403 /****************
404 * Get the keyid from the fingerprint. This function is simple for most
405 * keys, but has to do a keylookup for old stayle keys.
408 keyid_from_fingerprint( const byte *fprint, size_t fprint_len, u32 *keyid )
410 u32 dummy_keyid[2];
412 if( !keyid )
413 keyid = dummy_keyid;
415 if( fprint_len != 20 ) {
416 /* This is special as we have to lookup the key first */
417 PKT_public_key pk;
418 int rc;
420 memset( &pk, 0, sizeof pk );
421 rc = get_pubkey_byfprint( &pk, fprint, fprint_len );
422 if( rc ) {
423 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
424 keyid[0] = 0;
425 keyid[1] = 0;
427 else
428 keyid_from_pk( &pk, keyid );
430 else {
431 const byte *dp = fprint;
432 keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
433 keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
436 return keyid[1];
441 keyid_from_sig( PKT_signature *sig, u32 *keyid )
443 if( keyid ) {
444 keyid[0] = sig->keyid[0];
445 keyid[1] = sig->keyid[1];
447 return sig->keyid[1];
450 byte *
451 namehash_from_uid(PKT_user_id *uid)
453 if(uid->namehash==NULL)
455 uid->namehash = xmalloc(20);
457 if(uid->attrib_data)
458 gcry_md_hash_buffer (GCRY_MD_RMD160, uid->namehash,
459 uid->attrib_data, uid->attrib_len);
460 else
461 gcry_md_hash_buffer (GCRY_MD_RMD160, uid->namehash,
462 uid->name, uid->len);
465 return uid->namehash;
468 /****************
469 * return the number of bits used in the pk
471 unsigned
472 nbits_from_pk( PKT_public_key *pk )
474 return pubkey_nbits( pk->pubkey_algo, pk->pkey );
477 /****************
478 * return the number of bits used in the sk
480 unsigned
481 nbits_from_sk( PKT_secret_key *sk )
483 return pubkey_nbits( sk->pubkey_algo, sk->skey );
486 static const char *
487 mk_datestr (char *buffer, time_t atime)
489 struct tm *tp;
491 if ( atime < 0 ) /* 32 bit time_t and after 2038-01-19 */
492 strcpy (buffer, "????" "-??" "-??"); /* mark this as invalid */
493 else {
494 tp = gmtime (&atime);
495 sprintf (buffer,"%04d-%02d-%02d",
496 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
498 return buffer;
501 /****************
502 * return a string with the creation date of the pk
503 * Note: this is alloced in a static buffer.
504 * Format is: yyyy-mm-dd
506 const char *
507 datestr_from_pk( PKT_public_key *pk )
509 static char buffer[11+5];
510 time_t atime = pk->timestamp;
512 return mk_datestr (buffer, atime);
515 const char *
516 datestr_from_sk( PKT_secret_key *sk )
518 static char buffer[11+5];
519 time_t atime = sk->timestamp;
521 return mk_datestr (buffer, atime);
524 const char *
525 datestr_from_sig( PKT_signature *sig )
527 static char buffer[11+5];
528 time_t atime = sig->timestamp;
530 return mk_datestr (buffer, atime);
533 const char *
534 expirestr_from_pk( PKT_public_key *pk )
536 static char buffer[11+5];
537 time_t atime;
539 if( !pk->expiredate )
540 return _("never ");
541 atime = pk->expiredate;
542 return mk_datestr (buffer, atime);
545 const char *
546 expirestr_from_sk( PKT_secret_key *sk )
548 static char buffer[11+5];
549 time_t atime;
551 if( !sk->expiredate )
552 return _("never ");
553 atime = sk->expiredate;
554 return mk_datestr (buffer, atime);
557 const char *
558 expirestr_from_sig( PKT_signature *sig )
560 static char buffer[11+5];
561 time_t atime;
563 if(!sig->expiredate)
564 return _("never ");
565 atime=sig->expiredate;
566 return mk_datestr (buffer, atime);
569 const char *
570 revokestr_from_pk( PKT_public_key *pk )
572 static char buffer[11+5];
573 time_t atime;
575 if(!pk->revoked.date)
576 return _("never ");
577 atime=pk->revoked.date;
578 return mk_datestr (buffer, atime);
582 const char *
583 usagestr_from_pk( PKT_public_key *pk )
585 static char buffer[10];
586 int i = 0;
587 unsigned int use = pk->pubkey_usage;
589 if ( use & PUBKEY_USAGE_SIG )
590 buffer[i++] = 'S';
592 if ( use & PUBKEY_USAGE_CERT )
593 buffer[i++] = 'C';
595 if ( use & PUBKEY_USAGE_ENC )
596 buffer[i++] = 'E';
598 if ( (use & PUBKEY_USAGE_AUTH) )
599 buffer[i++] = 'A';
601 while (i < 4)
602 buffer[i++] = ' ';
604 buffer[i] = 0;
605 return buffer;
609 const char *
610 colon_strtime (u32 t)
612 if (!t)
613 return "";
614 if (opt.fixed_list_mode) {
615 static char buf[15];
616 sprintf (buf, "%lu", (ulong)t);
617 return buf;
619 return strtimestamp(t);
622 const char *
623 colon_datestr_from_pk (PKT_public_key *pk)
625 if (opt.fixed_list_mode) {
626 static char buf[15];
627 sprintf (buf, "%lu", (ulong)pk->timestamp);
628 return buf;
630 return datestr_from_pk (pk);
633 const char *
634 colon_datestr_from_sk (PKT_secret_key *sk)
636 if (opt.fixed_list_mode) {
637 static char buf[15];
638 sprintf (buf, "%lu", (ulong)sk->timestamp);
639 return buf;
641 return datestr_from_sk (sk);
644 const char *
645 colon_datestr_from_sig (PKT_signature *sig)
647 if (opt.fixed_list_mode) {
648 static char buf[15];
649 sprintf (buf, "%lu", (ulong)sig->timestamp);
650 return buf;
652 return datestr_from_sig (sig);
655 const char *
656 colon_expirestr_from_sig (PKT_signature *sig)
658 if(!sig->expiredate)
659 return "";
660 if (opt.fixed_list_mode) {
661 static char buf[15];
662 sprintf (buf, "%lu", (ulong)sig->expiredate);
663 return buf;
665 return expirestr_from_sig (sig);
669 /**************** .
670 * Return a byte array with the fingerprint for the given PK/SK
671 * The length of the array is returned in ret_len. Caller must free
672 * the array or provide an array of length MAX_FINGERPRINT_LEN.
675 byte *
676 fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len )
678 byte *buf;
679 const byte *dp;
680 size_t len, nbytes;
681 int i;
683 if ( pk->version < 4 )
685 if ( is_RSA(pk->pubkey_algo) )
687 /* RSA in version 3 packets is special. */
688 gcry_md_hd_t md;
690 if (gcry_md_open (&md, DIGEST_ALGO_MD5, 0))
691 BUG ();
692 if ( pubkey_get_npkey (pk->pubkey_algo) > 1 )
694 for (i=0; i < 2; i++)
696 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0,
697 &nbytes, pk->pkey[i]))
698 BUG ();
699 /* fixme: Better allocate BUF on the stack */
700 buf = xmalloc (nbytes);
701 if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, nbytes,
702 NULL, pk->pkey[i]))
703 BUG ();
704 gcry_md_write (md, buf, nbytes);
705 xfree (buf);
708 gcry_md_final (md);
709 if (!array)
710 array = xmalloc (16);
711 len = 16;
712 memcpy (array, gcry_md_read (md, DIGEST_ALGO_MD5), 16);
713 gcry_md_close(md);
715 else
717 if (!array)
718 array = xmalloc(16);
719 len = 16;
720 memset (array,0,16);
723 else
725 gcry_md_hd_t md;
727 md = do_fingerprint_md(pk);
728 dp = gcry_md_read( md, 0 );
729 len = gcry_md_get_algo_dlen (gcry_md_get_algo (md));
730 assert( len <= MAX_FINGERPRINT_LEN );
731 if (!array)
732 array = xmalloc ( len );
733 memcpy (array, dp, len );
734 pk->keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
735 pk->keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
736 gcry_md_close( md);
739 *ret_len = len;
740 return array;
743 byte *
744 fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len )
746 byte *buf;
747 const char *dp;
748 size_t len, nbytes;
749 int i;
751 if (sk->version < 4)
753 if ( is_RSA(sk->pubkey_algo) )
755 /* RSA in version 3 packets is special. */
756 gcry_md_hd_t md;
758 if (gcry_md_open (&md, DIGEST_ALGO_MD5, 0))
759 BUG ();
760 if (pubkey_get_npkey( sk->pubkey_algo ) > 1)
762 for (i=0; i < 2; i++)
764 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0,
765 &nbytes, sk->skey[i]))
766 BUG ();
767 /* fixme: Better allocate BUF on the stack */
768 buf = xmalloc (nbytes);
769 if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, nbytes,
770 NULL, sk->skey[i]))
771 BUG ();
772 gcry_md_write (md, buf, nbytes);
773 xfree (buf);
776 gcry_md_final(md);
777 if (!array)
778 array = xmalloc (16);
779 len = 16;
780 memcpy (array, gcry_md_read (md, DIGEST_ALGO_MD5), 16);
781 gcry_md_close (md);
783 else
785 if (!array)
786 array = xmalloc (16);
787 len=16;
788 memset (array,0,16);
791 else
793 gcry_md_hd_t md;
795 md = do_fingerprint_md_sk(sk);
796 if (md)
798 dp = gcry_md_read ( md, 0 );
799 len = gcry_md_get_algo_dlen ( gcry_md_get_algo (md) );
800 assert ( len <= MAX_FINGERPRINT_LEN );
801 if (!array)
802 array = xmalloc( len );
803 memcpy (array, dp, len);
804 gcry_md_close (md);
806 else
808 len = MAX_FINGERPRINT_LEN;
809 if (!array)
810 array = xmalloc (len);
811 memset (array, 0, len);
815 *ret_len = len;
816 return array;
820 /* Create a serialno/fpr string from the serial number and the secret
821 key. Caller must free the returned string. There is no error
822 return. */
823 char *
824 serialno_and_fpr_from_sk (const unsigned char *sn, size_t snlen,
825 PKT_secret_key *sk)
827 unsigned char fpr[MAX_FINGERPRINT_LEN];
828 size_t fprlen;
829 char *buffer, *p;
830 int i;
832 fingerprint_from_sk (sk, fpr, &fprlen);
833 buffer = p = xmalloc (snlen*2 + 1 + fprlen*2 + 1);
834 for (i=0; i < snlen; i++, p+=2)
835 sprintf (p, "%02X", sn[i]);
836 *p++ = '/';
837 for (i=0; i < fprlen; i++, p+=2)
838 sprintf (p, "%02X", fpr[i]);
839 *p = 0;
840 return buffer;