Fix for extended length Le in decipher
[gnupg.git] / g10 / keyid.c
blob3ba7d425666655da3e2bd3d2e4c44d819e8498f3
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <time.h>
27 #include <assert.h>
29 #include "gpg.h"
30 #include "util.h"
31 #include "main.h"
32 #include "packet.h"
33 #include "options.h"
34 #include "keydb.h"
35 #include "i18n.h"
36 #include "rmd160.h"
38 int
39 pubkey_letter( int algo )
41 switch( algo ) {
42 case PUBKEY_ALGO_RSA: return 'R' ;
43 case PUBKEY_ALGO_RSA_E: return 'r' ;
44 case PUBKEY_ALGO_RSA_S: return 's' ;
45 case PUBKEY_ALGO_ELGAMAL_E: return 'g';
46 case PUBKEY_ALGO_ELGAMAL: return 'G' ;
47 case PUBKEY_ALGO_DSA: return 'D' ;
48 default: return '?';
52 /* This function is useful for v4 fingerprints and v3 or v4 key
53 signing. */
54 void
55 hash_public_key( gcry_md_hd_t md, PKT_public_key *pk )
57 unsigned int n = 6;
58 unsigned int nn[PUBKEY_MAX_NPKEY];
59 byte *pp[PUBKEY_MAX_NPKEY];
60 int i;
61 unsigned int nbits;
62 size_t nbytes;
63 int npkey = pubkey_get_npkey (pk->pubkey_algo);
65 /* Two extra bytes for the expiration date in v3 */
66 if(pk->version<4)
67 n+=2;
69 if (npkey==0 && pk->pkey[0]
70 && gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
72 pp[0] = gcry_mpi_get_opaque (pk->pkey[0], &nbits);
73 nn[0] = (nbits+7)/8;
74 n+=nn[0];
76 else
77 for(i=0; i < npkey; i++ )
79 if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, pk->pkey[i]))
80 BUG ();
81 pp[i] = xmalloc (nbytes);
82 if (gcry_mpi_print (GCRYMPI_FMT_PGP, pp[i], nbytes,
83 &nbytes, pk->pkey[i]))
84 BUG ();
85 nn[i] = nbytes;
86 n += nn[i];
89 gcry_md_putc ( md, 0x99 ); /* ctb */
90 /* What does it mean if n is greater than than 0xFFFF ? */
91 gcry_md_putc ( md, n >> 8 ); /* 2 byte length header */
92 gcry_md_putc ( md, n );
93 gcry_md_putc ( md, pk->version );
95 gcry_md_putc ( md, pk->timestamp >> 24 );
96 gcry_md_putc ( md, pk->timestamp >> 16 );
97 gcry_md_putc ( md, pk->timestamp >> 8 );
98 gcry_md_putc ( md, pk->timestamp );
100 if(pk->version<4)
102 u16 days=0;
103 if(pk->expiredate)
104 days=(u16)((pk->expiredate - pk->timestamp) / 86400L);
106 gcry_md_putc ( md, days >> 8 );
107 gcry_md_putc ( md, days );
110 gcry_md_putc ( md, pk->pubkey_algo );
112 if(npkey==0 && pk->pkey[0]
113 && gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
115 gcry_md_write (md, pp[0], nn[0]);
117 else
118 for(i=0; i < npkey; i++ )
120 gcry_md_write ( md, pp[i], nn[i] );
121 xfree(pp[i]);
125 static gcry_md_hd_t
126 do_fingerprint_md( PKT_public_key *pk )
128 gcry_md_hd_t md;
130 if (gcry_md_open (&md, DIGEST_ALGO_SHA1, 0))
131 BUG ();
132 hash_public_key(md,pk);
133 gcry_md_final( md );
135 return md;
138 static gcry_md_hd_t
139 do_fingerprint_md_sk( PKT_secret_key *sk )
141 PKT_public_key pk;
142 int npkey = pubkey_get_npkey( sk->pubkey_algo ); /* npkey is correct! */
143 int i;
145 if(npkey==0)
146 return NULL;
148 pk.pubkey_algo = sk->pubkey_algo;
149 pk.version = sk->version;
150 pk.timestamp = sk->timestamp;
151 pk.expiredate = sk->expiredate;
152 pk.pubkey_algo = sk->pubkey_algo;
153 for( i=0; i < npkey; i++ )
154 pk.pkey[i] = sk->skey[i];
155 return do_fingerprint_md( &pk );
160 v3_keyid (gcry_mpi_t a, u32 *ki)
162 byte *buffer, *p;
163 size_t nbytes;
165 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &nbytes, a ))
166 BUG ();
167 /* fixme: allocate it on the stack */
168 buffer = xmalloc (nbytes);
169 if (gcry_mpi_print( GCRYMPI_FMT_USG, buffer, nbytes, NULL, a ))
170 BUG ();
171 if (nbytes < 8) /* oops */
172 ki[0] = ki[1] = 0;
173 else
175 p = buffer + nbytes - 8;
176 ki[0] = (p[0] << 24) | (p[1] <<16) | (p[2] << 8) | p[3];
177 p += 4;
178 ki[1] = (p[0] << 24) | (p[1] <<16) | (p[2] << 8) | p[3];
180 xfree (buffer);
181 return ki[1];
185 size_t
186 keystrlen(void)
188 switch(opt.keyid_format)
190 case KF_SHORT:
191 return 8;
193 case KF_LONG:
194 return 16;
196 case KF_0xSHORT:
197 return 10;
199 case KF_0xLONG:
200 return 18;
202 default:
203 BUG();
207 const char *
208 keystr(u32 *keyid)
210 static char keyid_str[19];
212 switch(opt.keyid_format)
214 case KF_SHORT:
215 sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
216 break;
218 case KF_LONG:
219 if(keyid[0])
220 sprintf(keyid_str,"%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
221 else
222 sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
223 break;
225 case KF_0xSHORT:
226 sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
227 break;
229 case KF_0xLONG:
230 if(keyid[0])
231 sprintf(keyid_str,"0x%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
232 else
233 sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
234 break;
236 default:
237 BUG();
240 return keyid_str;
243 const char *
244 keystr_from_pk(PKT_public_key *pk)
246 keyid_from_pk(pk,NULL);
248 return keystr(pk->keyid);
251 const char *
252 keystr_from_sk(PKT_secret_key *sk)
254 keyid_from_sk(sk,NULL);
256 return keystr(sk->keyid);
259 const char *
260 keystr_from_desc(KEYDB_SEARCH_DESC *desc)
262 switch(desc->mode)
264 case KEYDB_SEARCH_MODE_LONG_KID:
265 case KEYDB_SEARCH_MODE_SHORT_KID:
266 return keystr(desc->u.kid);
268 case KEYDB_SEARCH_MODE_FPR20:
270 u32 keyid[2];
272 keyid[0] = ((unsigned char)desc->u.fpr[12] << 24
273 | (unsigned char)desc->u.fpr[13] << 16
274 | (unsigned char)desc->u.fpr[14] << 8
275 | (unsigned char)desc->u.fpr[15]);
276 keyid[1] = ((unsigned char)desc->u.fpr[16] << 24
277 | (unsigned char)desc->u.fpr[17] << 16
278 | (unsigned char)desc->u.fpr[18] << 8
279 | (unsigned char)desc->u.fpr[19]);
281 return keystr(keyid);
284 case KEYDB_SEARCH_MODE_FPR16:
285 return "?v3 fpr?";
287 default:
288 BUG();
292 /****************
293 * Get the keyid from the secret key and put it into keyid
294 * if this is not NULL. Return the 32 low bits of the keyid.
297 keyid_from_sk( PKT_secret_key *sk, u32 *keyid )
299 u32 lowbits;
300 u32 dummy_keyid[2];
302 if( !keyid )
303 keyid = dummy_keyid;
305 if( sk->keyid[0] || sk->keyid[1] )
307 keyid[0] = sk->keyid[0];
308 keyid[1] = sk->keyid[1];
309 lowbits = keyid[1];
311 else if( sk->version < 4 )
313 if( is_RSA(sk->pubkey_algo) )
315 lowbits = (pubkey_get_npkey (sk->pubkey_algo) ?
316 v3_keyid( sk->skey[0], keyid ) : 0); /* Take n. */
317 sk->keyid[0]=keyid[0];
318 sk->keyid[1]=keyid[1];
320 else
321 sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
323 else
325 const byte *dp;
326 gcry_md_hd_t md;
328 md = do_fingerprint_md_sk(sk);
329 if(md)
331 dp = gcry_md_read (md, 0);
332 keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
333 keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
334 lowbits = keyid[1];
335 gcry_md_close (md);
336 sk->keyid[0] = keyid[0];
337 sk->keyid[1] = keyid[1];
339 else
340 sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
343 return lowbits;
347 /****************
348 * Get the keyid from the public key and put it into keyid
349 * if this is not NULL. Return the 32 low bits of the keyid.
352 keyid_from_pk( PKT_public_key *pk, u32 *keyid )
354 u32 lowbits;
355 u32 dummy_keyid[2];
357 if( !keyid )
358 keyid = dummy_keyid;
360 if( pk->keyid[0] || pk->keyid[1] )
362 keyid[0] = pk->keyid[0];
363 keyid[1] = pk->keyid[1];
364 lowbits = keyid[1];
366 else if( pk->version < 4 )
368 if( is_RSA(pk->pubkey_algo) )
370 lowbits = (pubkey_get_npkey (pk->pubkey_algo) ?
371 v3_keyid ( pk->pkey[0], keyid ) : 0); /* From n. */
372 pk->keyid[0] = keyid[0];
373 pk->keyid[1] = keyid[1];
375 else
376 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
378 else
380 const byte *dp;
381 gcry_md_hd_t md;
383 md = do_fingerprint_md(pk);
384 if(md)
386 dp = gcry_md_read ( md, 0 );
387 keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
388 keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
389 lowbits = keyid[1];
390 gcry_md_close (md);
391 pk->keyid[0] = keyid[0];
392 pk->keyid[1] = keyid[1];
394 else
395 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
398 return lowbits;
402 /****************
403 * Get the keyid from the fingerprint. This function is simple for most
404 * keys, but has to do a keylookup for old stayle keys.
407 keyid_from_fingerprint( const byte *fprint, size_t fprint_len, u32 *keyid )
409 u32 dummy_keyid[2];
411 if( !keyid )
412 keyid = dummy_keyid;
414 if( fprint_len != 20 ) {
415 /* This is special as we have to lookup the key first */
416 PKT_public_key pk;
417 int rc;
419 memset( &pk, 0, sizeof pk );
420 rc = get_pubkey_byfprint( &pk, fprint, fprint_len );
421 if( rc ) {
422 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
423 keyid[0] = 0;
424 keyid[1] = 0;
426 else
427 keyid_from_pk( &pk, keyid );
429 else {
430 const byte *dp = fprint;
431 keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
432 keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
435 return keyid[1];
440 keyid_from_sig( PKT_signature *sig, u32 *keyid )
442 if( keyid ) {
443 keyid[0] = sig->keyid[0];
444 keyid[1] = sig->keyid[1];
446 return sig->keyid[1];
449 byte *
450 namehash_from_uid(PKT_user_id *uid)
452 if (!uid->namehash)
454 uid->namehash = xmalloc (20);
456 if(uid->attrib_data)
457 rmd160_hash_buffer (uid->namehash, uid->attrib_data, uid->attrib_len);
458 else
459 rmd160_hash_buffer (uid->namehash, uid->name, uid->len);
462 return uid->namehash;
465 /****************
466 * return the number of bits used in the pk
468 unsigned
469 nbits_from_pk( PKT_public_key *pk )
471 return pubkey_nbits( pk->pubkey_algo, pk->pkey );
474 /****************
475 * return the number of bits used in the sk
477 unsigned
478 nbits_from_sk( PKT_secret_key *sk )
480 return pubkey_nbits( sk->pubkey_algo, sk->skey );
483 static const char *
484 mk_datestr (char *buffer, time_t atime)
486 struct tm *tp;
488 if ( atime < 0 ) /* 32 bit time_t and after 2038-01-19 */
489 strcpy (buffer, "????" "-??" "-??"); /* mark this as invalid */
490 else {
491 tp = gmtime (&atime);
492 sprintf (buffer,"%04d-%02d-%02d",
493 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
495 return buffer;
498 /****************
499 * return a string with the creation date of the pk
500 * Note: this is alloced in a static buffer.
501 * Format is: yyyy-mm-dd
503 const char *
504 datestr_from_pk( PKT_public_key *pk )
506 static char buffer[11+5];
507 time_t atime = pk->timestamp;
509 return mk_datestr (buffer, atime);
512 const char *
513 datestr_from_sk( PKT_secret_key *sk )
515 static char buffer[11+5];
516 time_t atime = sk->timestamp;
518 return mk_datestr (buffer, atime);
521 const char *
522 datestr_from_sig( PKT_signature *sig )
524 static char buffer[11+5];
525 time_t atime = sig->timestamp;
527 return mk_datestr (buffer, atime);
530 const char *
531 expirestr_from_pk( PKT_public_key *pk )
533 static char buffer[11+5];
534 time_t atime;
536 if( !pk->expiredate )
537 return _("never ");
538 atime = pk->expiredate;
539 return mk_datestr (buffer, atime);
542 const char *
543 expirestr_from_sk( PKT_secret_key *sk )
545 static char buffer[11+5];
546 time_t atime;
548 if( !sk->expiredate )
549 return _("never ");
550 atime = sk->expiredate;
551 return mk_datestr (buffer, atime);
554 const char *
555 expirestr_from_sig( PKT_signature *sig )
557 static char buffer[11+5];
558 time_t atime;
560 if(!sig->expiredate)
561 return _("never ");
562 atime=sig->expiredate;
563 return mk_datestr (buffer, atime);
566 const char *
567 revokestr_from_pk( PKT_public_key *pk )
569 static char buffer[11+5];
570 time_t atime;
572 if(!pk->revoked.date)
573 return _("never ");
574 atime=pk->revoked.date;
575 return mk_datestr (buffer, atime);
579 const char *
580 usagestr_from_pk( PKT_public_key *pk )
582 static char buffer[10];
583 int i = 0;
584 unsigned int use = pk->pubkey_usage;
586 if ( use & PUBKEY_USAGE_SIG )
587 buffer[i++] = 'S';
589 if ( use & PUBKEY_USAGE_CERT )
590 buffer[i++] = 'C';
592 if ( use & PUBKEY_USAGE_ENC )
593 buffer[i++] = 'E';
595 if ( (use & PUBKEY_USAGE_AUTH) )
596 buffer[i++] = 'A';
598 while (i < 4)
599 buffer[i++] = ' ';
601 buffer[i] = 0;
602 return buffer;
606 const char *
607 colon_strtime (u32 t)
609 static char buf[20];
611 if (!t)
612 return "";
613 snprintf (buf, sizeof buf, "%lu", (ulong)t);
614 return buf;
617 const char *
618 colon_datestr_from_pk (PKT_public_key *pk)
620 static char buf[20];
622 snprintf (buf, sizeof buf, "%lu", (ulong)pk->timestamp);
623 return buf;
626 const char *
627 colon_datestr_from_sk (PKT_secret_key *sk)
629 static char buf[20];
631 snprintf (buf, sizeof buf, "%lu", (ulong)sk->timestamp);
632 return buf;
635 const char *
636 colon_datestr_from_sig (PKT_signature *sig)
638 static char buf[20];
640 snprintf (buf, sizeof buf, "%lu", (ulong)sig->timestamp);
641 return buf;
644 const char *
645 colon_expirestr_from_sig (PKT_signature *sig)
647 static char buf[20];
649 if (!sig->expiredate)
650 return "";
652 snprintf (buf, sizeof buf,"%lu", (ulong)sig->expiredate);
653 return buf;
657 /**************** .
658 * Return a byte array with the fingerprint for the given PK/SK
659 * The length of the array is returned in ret_len. Caller must free
660 * the array or provide an array of length MAX_FINGERPRINT_LEN.
663 byte *
664 fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len )
666 byte *buf;
667 const byte *dp;
668 size_t len, nbytes;
669 int i;
671 if ( pk->version < 4 )
673 if ( is_RSA(pk->pubkey_algo) )
675 /* RSA in version 3 packets is special. */
676 gcry_md_hd_t md;
678 if (gcry_md_open (&md, DIGEST_ALGO_MD5, 0))
679 BUG ();
680 if ( pubkey_get_npkey (pk->pubkey_algo) > 1 )
682 for (i=0; i < 2; i++)
684 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0,
685 &nbytes, pk->pkey[i]))
686 BUG ();
687 /* fixme: Better allocate BUF on the stack */
688 buf = xmalloc (nbytes);
689 if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, nbytes,
690 NULL, pk->pkey[i]))
691 BUG ();
692 gcry_md_write (md, buf, nbytes);
693 xfree (buf);
696 gcry_md_final (md);
697 if (!array)
698 array = xmalloc (16);
699 len = 16;
700 memcpy (array, gcry_md_read (md, DIGEST_ALGO_MD5), 16);
701 gcry_md_close(md);
703 else
705 if (!array)
706 array = xmalloc(16);
707 len = 16;
708 memset (array,0,16);
711 else
713 gcry_md_hd_t md;
715 md = do_fingerprint_md(pk);
716 dp = gcry_md_read( md, 0 );
717 len = gcry_md_get_algo_dlen (gcry_md_get_algo (md));
718 assert( len <= MAX_FINGERPRINT_LEN );
719 if (!array)
720 array = xmalloc ( len );
721 memcpy (array, dp, len );
722 pk->keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
723 pk->keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
724 gcry_md_close( md);
727 *ret_len = len;
728 return array;
731 byte *
732 fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len )
734 byte *buf;
735 const char *dp;
736 size_t len, nbytes;
737 int i;
739 if (sk->version < 4)
741 if ( is_RSA(sk->pubkey_algo) )
743 /* RSA in version 3 packets is special. */
744 gcry_md_hd_t md;
746 if (gcry_md_open (&md, DIGEST_ALGO_MD5, 0))
747 BUG ();
748 if (pubkey_get_npkey( sk->pubkey_algo ) > 1)
750 for (i=0; i < 2; i++)
752 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0,
753 &nbytes, sk->skey[i]))
754 BUG ();
755 /* fixme: Better allocate BUF on the stack */
756 buf = xmalloc (nbytes);
757 if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, nbytes,
758 NULL, sk->skey[i]))
759 BUG ();
760 gcry_md_write (md, buf, nbytes);
761 xfree (buf);
764 gcry_md_final(md);
765 if (!array)
766 array = xmalloc (16);
767 len = 16;
768 memcpy (array, gcry_md_read (md, DIGEST_ALGO_MD5), 16);
769 gcry_md_close (md);
771 else
773 if (!array)
774 array = xmalloc (16);
775 len=16;
776 memset (array,0,16);
779 else
781 gcry_md_hd_t md;
783 md = do_fingerprint_md_sk(sk);
784 if (md)
786 dp = gcry_md_read ( md, 0 );
787 len = gcry_md_get_algo_dlen ( gcry_md_get_algo (md) );
788 assert ( len <= MAX_FINGERPRINT_LEN );
789 if (!array)
790 array = xmalloc( len );
791 memcpy (array, dp, len);
792 gcry_md_close (md);
794 else
796 len = MAX_FINGERPRINT_LEN;
797 if (!array)
798 array = xmalloc (len);
799 memset (array, 0, len);
803 *ret_len = len;
804 return array;
808 /* Create a serialno/fpr string from the serial number and the secret
809 key. Caller must free the returned string. There is no error
810 return. */
811 char *
812 serialno_and_fpr_from_sk (const unsigned char *sn, size_t snlen,
813 PKT_secret_key *sk)
815 unsigned char fpr[MAX_FINGERPRINT_LEN];
816 size_t fprlen;
817 char *buffer, *p;
818 int i;
820 fingerprint_from_sk (sk, fpr, &fprlen);
821 buffer = p = xmalloc (snlen*2 + 1 + fprlen*2 + 1);
822 for (i=0; i < snlen; i++, p+=2)
823 sprintf (p, "%02X", sn[i]);
824 *p++ = '/';
825 for (i=0; i < fprlen; i++, p+=2)
826 sprintf (p, "%02X", fpr[i]);
827 *p = 0;
828 return buffer;