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/>.
39 pubkey_letter( int 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' ;
52 /* This function is useful for v4 fingerprints and v3 or v4 key
55 hash_public_key( gcry_md_hd_t md
, PKT_public_key
*pk
)
58 unsigned int nn
[PUBKEY_MAX_NPKEY
];
59 byte
*pp
[PUBKEY_MAX_NPKEY
];
63 int npkey
= pubkey_get_npkey (pk
->pubkey_algo
);
65 /* Two extra bytes for the expiration date in v3 */
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
);
77 for(i
=0; i
< npkey
; i
++ )
79 if (gcry_mpi_print (GCRYMPI_FMT_PGP
, NULL
, 0, &nbytes
, pk
->pkey
[i
]))
81 pp
[i
] = xmalloc (nbytes
);
82 if (gcry_mpi_print (GCRYMPI_FMT_PGP
, pp
[i
], nbytes
,
83 &nbytes
, pk
->pkey
[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
);
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]);
118 for(i
=0; i
< npkey
; i
++ )
120 gcry_md_write ( md
, pp
[i
], nn
[i
] );
126 do_fingerprint_md( PKT_public_key
*pk
)
130 if (gcry_md_open (&md
, DIGEST_ALGO_SHA1
, 0))
132 hash_public_key(md
,pk
);
139 do_fingerprint_md_sk( PKT_secret_key
*sk
)
142 int npkey
= pubkey_get_npkey( sk
->pubkey_algo
); /* npkey is correct! */
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
)
165 if (gcry_mpi_print (GCRYMPI_FMT_USG
, NULL
, 0, &nbytes
, a
))
167 /* fixme: allocate it on the stack */
168 buffer
= xmalloc (nbytes
);
169 if (gcry_mpi_print( GCRYMPI_FMT_USG
, buffer
, nbytes
, NULL
, a
))
171 if (nbytes
< 8) /* oops */
175 p
= buffer
+ nbytes
- 8;
176 ki
[0] = (p
[0] << 24) | (p
[1] <<16) | (p
[2] << 8) | p
[3];
178 ki
[1] = (p
[0] << 24) | (p
[1] <<16) | (p
[2] << 8) | p
[3];
188 switch(opt
.keyid_format
)
210 static char keyid_str
[19];
212 switch(opt
.keyid_format
)
215 sprintf(keyid_str
,"%08lX",(ulong
)keyid
[1]);
220 sprintf(keyid_str
,"%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
222 sprintf(keyid_str
,"%08lX",(ulong
)keyid
[1]);
226 sprintf(keyid_str
,"0x%08lX",(ulong
)keyid
[1]);
231 sprintf(keyid_str
,"0x%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
233 sprintf(keyid_str
,"0x%08lX",(ulong
)keyid
[1]);
244 keystr_from_pk(PKT_public_key
*pk
)
246 keyid_from_pk(pk
,NULL
);
248 return keystr(pk
->keyid
);
252 keystr_from_sk(PKT_secret_key
*sk
)
254 keyid_from_sk(sk
,NULL
);
256 return keystr(sk
->keyid
);
260 keystr_from_desc(KEYDB_SEARCH_DESC
*desc
)
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
:
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
:
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
)
305 if( sk
->keyid
[0] || sk
->keyid
[1] )
307 keyid
[0] = sk
->keyid
[0];
308 keyid
[1] = sk
->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];
321 sk
->keyid
[0]=sk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
328 md
= do_fingerprint_md_sk(sk
);
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] ;
336 sk
->keyid
[0] = keyid
[0];
337 sk
->keyid
[1] = keyid
[1];
340 sk
->keyid
[0]=sk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
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
)
360 if( pk
->keyid
[0] || pk
->keyid
[1] )
362 keyid
[0] = pk
->keyid
[0];
363 keyid
[1] = pk
->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];
376 pk
->keyid
[0]=pk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
383 md
= do_fingerprint_md(pk
);
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] ;
391 pk
->keyid
[0] = keyid
[0];
392 pk
->keyid
[1] = keyid
[1];
395 pk
->keyid
[0]=pk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
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
)
414 if( fprint_len
!= 20 ) {
415 /* This is special as we have to lookup the key first */
419 memset( &pk
, 0, sizeof pk
);
420 rc
= get_pubkey_byfprint( &pk
, fprint
, fprint_len
);
422 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
427 keyid_from_pk( &pk
, keyid
);
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] ;
440 keyid_from_sig( PKT_signature
*sig
, u32
*keyid
)
443 keyid
[0] = sig
->keyid
[0];
444 keyid
[1] = sig
->keyid
[1];
446 return sig
->keyid
[1];
450 namehash_from_uid(PKT_user_id
*uid
)
454 uid
->namehash
= xmalloc (20);
457 rmd160_hash_buffer (uid
->namehash
, uid
->attrib_data
, uid
->attrib_len
);
459 rmd160_hash_buffer (uid
->namehash
, uid
->name
, uid
->len
);
462 return uid
->namehash
;
466 * return the number of bits used in the pk
469 nbits_from_pk( PKT_public_key
*pk
)
471 return pubkey_nbits( pk
->pubkey_algo
, pk
->pkey
);
475 * return the number of bits used in the sk
478 nbits_from_sk( PKT_secret_key
*sk
)
480 return pubkey_nbits( sk
->pubkey_algo
, sk
->skey
);
484 mk_datestr (char *buffer
, time_t atime
)
488 if ( atime
< 0 ) /* 32 bit time_t and after 2038-01-19 */
489 strcpy (buffer
, "????" "-??" "-??"); /* mark this as invalid */
491 tp
= gmtime (&atime
);
492 sprintf (buffer
,"%04d-%02d-%02d",
493 1900+tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
);
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
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
);
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
);
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
);
531 expirestr_from_pk( PKT_public_key
*pk
)
533 static char buffer
[11+5];
536 if( !pk
->expiredate
)
538 atime
= pk
->expiredate
;
539 return mk_datestr (buffer
, atime
);
543 expirestr_from_sk( PKT_secret_key
*sk
)
545 static char buffer
[11+5];
548 if( !sk
->expiredate
)
550 atime
= sk
->expiredate
;
551 return mk_datestr (buffer
, atime
);
555 expirestr_from_sig( PKT_signature
*sig
)
557 static char buffer
[11+5];
562 atime
=sig
->expiredate
;
563 return mk_datestr (buffer
, atime
);
567 revokestr_from_pk( PKT_public_key
*pk
)
569 static char buffer
[11+5];
572 if(!pk
->revoked
.date
)
574 atime
=pk
->revoked
.date
;
575 return mk_datestr (buffer
, atime
);
580 usagestr_from_pk( PKT_public_key
*pk
)
582 static char buffer
[10];
584 unsigned int use
= pk
->pubkey_usage
;
586 if ( use
& PUBKEY_USAGE_SIG
)
589 if ( use
& PUBKEY_USAGE_CERT
)
592 if ( use
& PUBKEY_USAGE_ENC
)
595 if ( (use
& PUBKEY_USAGE_AUTH
) )
607 colon_strtime (u32 t
)
613 snprintf (buf
, sizeof buf
, "%lu", (ulong
)t
);
618 colon_datestr_from_pk (PKT_public_key
*pk
)
622 snprintf (buf
, sizeof buf
, "%lu", (ulong
)pk
->timestamp
);
627 colon_datestr_from_sk (PKT_secret_key
*sk
)
631 snprintf (buf
, sizeof buf
, "%lu", (ulong
)sk
->timestamp
);
636 colon_datestr_from_sig (PKT_signature
*sig
)
640 snprintf (buf
, sizeof buf
, "%lu", (ulong
)sig
->timestamp
);
645 colon_expirestr_from_sig (PKT_signature
*sig
)
649 if (!sig
->expiredate
)
652 snprintf (buf
, sizeof buf
,"%lu", (ulong
)sig
->expiredate
);
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.
664 fingerprint_from_pk( PKT_public_key
*pk
, byte
*array
, size_t *ret_len
)
671 if ( pk
->version
< 4 )
673 if ( is_RSA(pk
->pubkey_algo
) )
675 /* RSA in version 3 packets is special. */
678 if (gcry_md_open (&md
, DIGEST_ALGO_MD5
, 0))
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
]))
687 /* fixme: Better allocate BUF on the stack */
688 buf
= xmalloc (nbytes
);
689 if (gcry_mpi_print (GCRYMPI_FMT_USG
, buf
, nbytes
,
692 gcry_md_write (md
, buf
, nbytes
);
698 array
= xmalloc (16);
700 memcpy (array
, gcry_md_read (md
, DIGEST_ALGO_MD5
), 16);
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
);
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] ;
732 fingerprint_from_sk( PKT_secret_key
*sk
, byte
*array
, size_t *ret_len
)
741 if ( is_RSA(sk
->pubkey_algo
) )
743 /* RSA in version 3 packets is special. */
746 if (gcry_md_open (&md
, DIGEST_ALGO_MD5
, 0))
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
]))
755 /* fixme: Better allocate BUF on the stack */
756 buf
= xmalloc (nbytes
);
757 if (gcry_mpi_print (GCRYMPI_FMT_USG
, buf
, nbytes
,
760 gcry_md_write (md
, buf
, nbytes
);
766 array
= xmalloc (16);
768 memcpy (array
, gcry_md_read (md
, DIGEST_ALGO_MD5
), 16);
774 array
= xmalloc (16);
783 md
= do_fingerprint_md_sk(sk
);
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
);
790 array
= xmalloc( len
);
791 memcpy (array
, dp
, len
);
796 len
= MAX_FINGERPRINT_LEN
;
798 array
= xmalloc (len
);
799 memset (array
, 0, len
);
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
812 serialno_and_fpr_from_sk (const unsigned char *sn
, size_t snlen
,
815 unsigned char fpr
[MAX_FINGERPRINT_LEN
];
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
]);
825 for (i
=0; i
< fprlen
; i
++, p
+=2)
826 sprintf (p
, "%02X", fpr
[i
]);