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,
40 pubkey_letter( int 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' ;
53 /* This function is useful for v4 fingerprints and v3 or v4 key
56 hash_public_key( gcry_md_hd_t md
, PKT_public_key
*pk
)
59 unsigned int nn
[PUBKEY_MAX_NPKEY
];
60 byte
*pp
[PUBKEY_MAX_NPKEY
];
64 int npkey
= pubkey_get_npkey (pk
->pubkey_algo
);
66 /* Two extra bytes for the expiration date in v3 */
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
);
78 for(i
=0; i
< npkey
; i
++ )
80 if (gcry_mpi_print (GCRYMPI_FMT_PGP
, NULL
, 0, &nbytes
, pk
->pkey
[i
]))
82 pp
[i
] = xmalloc (nbytes
);
83 if (gcry_mpi_print (GCRYMPI_FMT_PGP
, pp
[i
], nbytes
,
84 &nbytes
, pk
->pkey
[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
);
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]);
119 for(i
=0; i
< npkey
; i
++ )
121 gcry_md_write ( md
, pp
[i
], nn
[i
] );
127 do_fingerprint_md( PKT_public_key
*pk
)
131 if (gcry_md_open (&md
, DIGEST_ALGO_SHA1
, 0))
133 hash_public_key(md
,pk
);
140 do_fingerprint_md_sk( PKT_secret_key
*sk
)
143 int npkey
= pubkey_get_npkey( sk
->pubkey_algo
); /* npkey is correct! */
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
)
166 if (gcry_mpi_print (GCRYMPI_FMT_USG
, NULL
, 0, &nbytes
, a
))
168 /* fixme: allocate it on the stack */
169 buffer
= xmalloc (nbytes
);
170 if (gcry_mpi_print( GCRYMPI_FMT_USG
, buffer
, nbytes
, NULL
, a
))
172 if (nbytes
< 8) /* oops */
176 p
= buffer
+ nbytes
- 8;
177 ki
[0] = (p
[0] << 24) | (p
[1] <<16) | (p
[2] << 8) | p
[3];
179 ki
[1] = (p
[0] << 24) | (p
[1] <<16) | (p
[2] << 8) | p
[3];
189 switch(opt
.keyid_format
)
211 static char keyid_str
[19];
213 switch(opt
.keyid_format
)
216 sprintf(keyid_str
,"%08lX",(ulong
)keyid
[1]);
221 sprintf(keyid_str
,"%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
223 sprintf(keyid_str
,"%08lX",(ulong
)keyid
[1]);
227 sprintf(keyid_str
,"0x%08lX",(ulong
)keyid
[1]);
232 sprintf(keyid_str
,"0x%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
234 sprintf(keyid_str
,"0x%08lX",(ulong
)keyid
[1]);
245 keystr_from_pk(PKT_public_key
*pk
)
247 keyid_from_pk(pk
,NULL
);
249 return keystr(pk
->keyid
);
253 keystr_from_sk(PKT_secret_key
*sk
)
255 keyid_from_sk(sk
,NULL
);
257 return keystr(sk
->keyid
);
261 keystr_from_desc(KEYDB_SEARCH_DESC
*desc
)
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
:
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
:
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
)
306 if( sk
->keyid
[0] || sk
->keyid
[1] )
308 keyid
[0] = sk
->keyid
[0];
309 keyid
[1] = sk
->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];
322 sk
->keyid
[0]=sk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
329 md
= do_fingerprint_md_sk(sk
);
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] ;
337 sk
->keyid
[0] = keyid
[0];
338 sk
->keyid
[1] = keyid
[1];
341 sk
->keyid
[0]=sk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
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
)
361 if( pk
->keyid
[0] || pk
->keyid
[1] )
363 keyid
[0] = pk
->keyid
[0];
364 keyid
[1] = pk
->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];
377 pk
->keyid
[0]=pk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
384 md
= do_fingerprint_md(pk
);
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] ;
392 pk
->keyid
[0] = keyid
[0];
393 pk
->keyid
[1] = keyid
[1];
396 pk
->keyid
[0]=pk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
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
)
415 if( fprint_len
!= 20 ) {
416 /* This is special as we have to lookup the key first */
420 memset( &pk
, 0, sizeof pk
);
421 rc
= get_pubkey_byfprint( &pk
, fprint
, fprint_len
);
423 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
428 keyid_from_pk( &pk
, keyid
);
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] ;
441 keyid_from_sig( PKT_signature
*sig
, u32
*keyid
)
444 keyid
[0] = sig
->keyid
[0];
445 keyid
[1] = sig
->keyid
[1];
447 return sig
->keyid
[1];
451 namehash_from_uid(PKT_user_id
*uid
)
453 if(uid
->namehash
==NULL
)
455 uid
->namehash
= xmalloc(20);
458 gcry_md_hash_buffer (GCRY_MD_RMD160
, uid
->namehash
,
459 uid
->attrib_data
, uid
->attrib_len
);
461 gcry_md_hash_buffer (GCRY_MD_RMD160
, uid
->namehash
,
462 uid
->name
, uid
->len
);
465 return uid
->namehash
;
469 * return the number of bits used in the pk
472 nbits_from_pk( PKT_public_key
*pk
)
474 return pubkey_nbits( pk
->pubkey_algo
, pk
->pkey
);
478 * return the number of bits used in the sk
481 nbits_from_sk( PKT_secret_key
*sk
)
483 return pubkey_nbits( sk
->pubkey_algo
, sk
->skey
);
487 mk_datestr (char *buffer
, time_t atime
)
491 if ( atime
< 0 ) /* 32 bit time_t and after 2038-01-19 */
492 strcpy (buffer
, "????" "-??" "-??"); /* mark this as invalid */
494 tp
= gmtime (&atime
);
495 sprintf (buffer
,"%04d-%02d-%02d",
496 1900+tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
);
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
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
);
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
);
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
);
534 expirestr_from_pk( PKT_public_key
*pk
)
536 static char buffer
[11+5];
539 if( !pk
->expiredate
)
541 atime
= pk
->expiredate
;
542 return mk_datestr (buffer
, atime
);
546 expirestr_from_sk( PKT_secret_key
*sk
)
548 static char buffer
[11+5];
551 if( !sk
->expiredate
)
553 atime
= sk
->expiredate
;
554 return mk_datestr (buffer
, atime
);
558 expirestr_from_sig( PKT_signature
*sig
)
560 static char buffer
[11+5];
565 atime
=sig
->expiredate
;
566 return mk_datestr (buffer
, atime
);
570 revokestr_from_pk( PKT_public_key
*pk
)
572 static char buffer
[11+5];
575 if(!pk
->revoked
.date
)
577 atime
=pk
->revoked
.date
;
578 return mk_datestr (buffer
, atime
);
583 usagestr_from_pk( PKT_public_key
*pk
)
585 static char buffer
[10];
587 unsigned int use
= pk
->pubkey_usage
;
589 if ( use
& PUBKEY_USAGE_SIG
)
592 if ( use
& PUBKEY_USAGE_CERT
)
595 if ( use
& PUBKEY_USAGE_ENC
)
598 if ( (use
& PUBKEY_USAGE_AUTH
) )
610 colon_strtime (u32 t
)
614 if (opt
.fixed_list_mode
) {
616 sprintf (buf
, "%lu", (ulong
)t
);
619 return strtimestamp(t
);
623 colon_datestr_from_pk (PKT_public_key
*pk
)
625 if (opt
.fixed_list_mode
) {
627 sprintf (buf
, "%lu", (ulong
)pk
->timestamp
);
630 return datestr_from_pk (pk
);
634 colon_datestr_from_sk (PKT_secret_key
*sk
)
636 if (opt
.fixed_list_mode
) {
638 sprintf (buf
, "%lu", (ulong
)sk
->timestamp
);
641 return datestr_from_sk (sk
);
645 colon_datestr_from_sig (PKT_signature
*sig
)
647 if (opt
.fixed_list_mode
) {
649 sprintf (buf
, "%lu", (ulong
)sig
->timestamp
);
652 return datestr_from_sig (sig
);
656 colon_expirestr_from_sig (PKT_signature
*sig
)
660 if (opt
.fixed_list_mode
) {
662 sprintf (buf
, "%lu", (ulong
)sig
->expiredate
);
665 return expirestr_from_sig (sig
);
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.
676 fingerprint_from_pk( PKT_public_key
*pk
, byte
*array
, size_t *ret_len
)
683 if ( pk
->version
< 4 )
685 if ( is_RSA(pk
->pubkey_algo
) )
687 /* RSA in version 3 packets is special. */
690 if (gcry_md_open (&md
, DIGEST_ALGO_MD5
, 0))
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
]))
699 /* fixme: Better allocate BUF on the stack */
700 buf
= xmalloc (nbytes
);
701 if (gcry_mpi_print (GCRYMPI_FMT_USG
, buf
, nbytes
,
704 gcry_md_write (md
, buf
, nbytes
);
710 array
= xmalloc (16);
712 memcpy (array
, gcry_md_read (md
, DIGEST_ALGO_MD5
), 16);
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
);
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] ;
744 fingerprint_from_sk( PKT_secret_key
*sk
, byte
*array
, size_t *ret_len
)
753 if ( is_RSA(sk
->pubkey_algo
) )
755 /* RSA in version 3 packets is special. */
758 if (gcry_md_open (&md
, DIGEST_ALGO_MD5
, 0))
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
]))
767 /* fixme: Better allocate BUF on the stack */
768 buf
= xmalloc (nbytes
);
769 if (gcry_mpi_print (GCRYMPI_FMT_USG
, buf
, nbytes
,
772 gcry_md_write (md
, buf
, nbytes
);
778 array
= xmalloc (16);
780 memcpy (array
, gcry_md_read (md
, DIGEST_ALGO_MD5
), 16);
786 array
= xmalloc (16);
795 md
= do_fingerprint_md_sk(sk
);
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
);
802 array
= xmalloc( len
);
803 memcpy (array
, dp
, len
);
808 len
= MAX_FINGERPRINT_LEN
;
810 array
= xmalloc (len
);
811 memset (array
, 0, len
);
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
824 serialno_and_fpr_from_sk (const unsigned char *sn
, size_t snlen
,
827 unsigned char fpr
[MAX_FINGERPRINT_LEN
];
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
]);
837 for (i
=0; i
< fprlen
; i
++, p
+=2)
838 sprintf (p
, "%02X", fpr
[i
]);