1 /* keyid.c - key ID and fingerprint handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 * 2004 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,
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( MD_HANDLE md
, PKT_public_key
*pk
)
58 unsigned nb
[PUBKEY_MAX_NPKEY
];
59 unsigned nn
[PUBKEY_MAX_NPKEY
];
60 byte
*pp
[PUBKEY_MAX_NPKEY
];
62 int npkey
= pubkey_get_npkey( pk
->pubkey_algo
);
64 /* Two extra bytes for the expiration date in v3 */
68 if(npkey
==0 && pk
->pkey
[0] && mpi_is_opaque(pk
->pkey
[0]))
70 pp
[0]=mpi_get_opaque(pk
->pkey
[0],&nn
[0]);
74 for(i
=0; i
< npkey
; i
++ )
76 nb
[i
] = mpi_get_nbits(pk
->pkey
[i
]);
77 pp
[i
] = mpi_get_buffer( pk
->pkey
[i
], nn
+i
, NULL
);
81 md_putc( md
, 0x99 ); /* ctb */
82 /* What does it mean if n is greater than than 0xFFFF ? */
83 md_putc( md
, n
>> 8 ); /* 2 byte length header */
85 md_putc( md
, pk
->version
);
87 md_putc( md
, pk
->timestamp
>> 24 );
88 md_putc( md
, pk
->timestamp
>> 16 );
89 md_putc( md
, pk
->timestamp
>> 8 );
90 md_putc( md
, pk
->timestamp
);
96 days
=(u16
)((pk
->expiredate
- pk
->timestamp
) / 86400L);
98 md_putc( md
, days
>> 8 );
102 md_putc( md
, pk
->pubkey_algo
);
104 if(npkey
==0 && pk
->pkey
[0] && mpi_is_opaque(pk
->pkey
[0]))
105 md_write(md
,pp
[0],nn
[0]);
107 for(i
=0; i
< npkey
; i
++ )
109 md_putc( md
, nb
[i
]>>8);
110 md_putc( md
, nb
[i
] );
111 md_write( md
, pp
[i
], nn
[i
] );
117 do_fingerprint_md( PKT_public_key
*pk
)
121 md
= md_open( DIGEST_ALGO_SHA1
, 0);
122 hash_public_key(md
,pk
);
129 do_fingerprint_md_sk( PKT_secret_key
*sk
)
132 int npkey
= pubkey_get_npkey( sk
->pubkey_algo
); /* npkey is correct! */
138 pk
.pubkey_algo
= sk
->pubkey_algo
;
139 pk
.version
= sk
->version
;
140 pk
.timestamp
= sk
->timestamp
;
141 pk
.expiredate
= sk
->expiredate
;
142 pk
.pubkey_algo
= sk
->pubkey_algo
;
143 for( i
=0; i
< npkey
; i
++ )
144 pk
.pkey
[i
] = sk
->skey
[i
];
145 return do_fingerprint_md( &pk
);
151 switch(opt
.keyid_format
)
173 static char keyid_str
[19];
175 switch(opt
.keyid_format
)
178 sprintf(keyid_str
,"%08lX",(ulong
)keyid
[1]);
183 sprintf(keyid_str
,"%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
185 sprintf(keyid_str
,"%08lX",(ulong
)keyid
[1]);
189 sprintf(keyid_str
,"0x%08lX",(ulong
)keyid
[1]);
194 sprintf(keyid_str
,"0x%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
196 sprintf(keyid_str
,"0x%08lX",(ulong
)keyid
[1]);
207 keystr_from_pk(PKT_public_key
*pk
)
209 keyid_from_pk(pk
,NULL
);
211 return keystr(pk
->keyid
);
215 keystr_from_sk(PKT_secret_key
*sk
)
217 keyid_from_sk(sk
,NULL
);
219 return keystr(sk
->keyid
);
223 keystr_from_desc(KEYDB_SEARCH_DESC
*desc
)
227 case KEYDB_SEARCH_MODE_LONG_KID
:
228 case KEYDB_SEARCH_MODE_SHORT_KID
:
229 return keystr(desc
->u
.kid
);
231 case KEYDB_SEARCH_MODE_FPR20
:
235 keyid
[0] = (unsigned char)desc
->u
.fpr
[12] << 24
236 | (unsigned char)desc
->u
.fpr
[13] << 16
237 | (unsigned char)desc
->u
.fpr
[14] << 8
238 | (unsigned char)desc
->u
.fpr
[15] ;
239 keyid
[1] = (unsigned char)desc
->u
.fpr
[16] << 24
240 | (unsigned char)desc
->u
.fpr
[17] << 16
241 | (unsigned char)desc
->u
.fpr
[18] << 8
242 | (unsigned char)desc
->u
.fpr
[19] ;
244 return keystr(keyid
);
247 case KEYDB_SEARCH_MODE_FPR16
:
256 * Get the keyid from the secret key and put it into keyid
257 * if this is not NULL. Return the 32 low bits of the keyid.
260 keyid_from_sk( PKT_secret_key
*sk
, u32
*keyid
)
268 if( sk
->keyid
[0] || sk
->keyid
[1] )
270 keyid
[0] = sk
->keyid
[0];
271 keyid
[1] = sk
->keyid
[1];
274 else if( sk
->version
< 4 )
276 if( is_RSA(sk
->pubkey_algo
) )
278 lowbits
= pubkey_get_npkey(sk
->pubkey_algo
) ?
279 mpi_get_keyid( sk
->skey
[0], keyid
) : 0; /* take n */
280 sk
->keyid
[0]=keyid
[0];
281 sk
->keyid
[1]=keyid
[1];
284 sk
->keyid
[0]=sk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
290 md
= do_fingerprint_md_sk(sk
);
293 dp
= md_read( md
, 0 );
294 keyid
[0] = dp
[12] << 24 | dp
[13] << 16 | dp
[14] << 8 | dp
[15] ;
295 keyid
[1] = dp
[16] << 24 | dp
[17] << 16 | dp
[18] << 8 | dp
[19] ;
298 sk
->keyid
[0] = keyid
[0];
299 sk
->keyid
[1] = keyid
[1];
302 sk
->keyid
[0]=sk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
310 * Get the keyid from the public key and put it into keyid
311 * if this is not NULL. Return the 32 low bits of the keyid.
314 keyid_from_pk( PKT_public_key
*pk
, u32
*keyid
)
322 if( pk
->keyid
[0] || pk
->keyid
[1] )
324 keyid
[0] = pk
->keyid
[0];
325 keyid
[1] = pk
->keyid
[1];
328 else if( pk
->version
< 4 )
330 if( is_RSA(pk
->pubkey_algo
) )
332 lowbits
= pubkey_get_npkey(pk
->pubkey_algo
) ?
333 mpi_get_keyid( pk
->pkey
[0], keyid
) : 0 ; /* from n */
334 pk
->keyid
[0] = keyid
[0];
335 pk
->keyid
[1] = keyid
[1];
338 pk
->keyid
[0]=pk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
344 md
= do_fingerprint_md(pk
);
347 dp
= md_read( md
, 0 );
348 keyid
[0] = dp
[12] << 24 | dp
[13] << 16 | dp
[14] << 8 | dp
[15] ;
349 keyid
[1] = dp
[16] << 24 | dp
[17] << 16 | dp
[18] << 8 | dp
[19] ;
352 pk
->keyid
[0] = keyid
[0];
353 pk
->keyid
[1] = keyid
[1];
356 pk
->keyid
[0]=pk
->keyid
[1]=keyid
[0]=keyid
[1]=lowbits
=0xFFFFFFFF;
364 * Get the keyid from the fingerprint. This function is simple for most
365 * keys, but has to do a keylookup for old stayle keys.
368 keyid_from_fingerprint( const byte
*fprint
, size_t fprint_len
, u32
*keyid
)
375 if( fprint_len
!= 20 ) {
376 /* This is special as we have to lookup the key first */
380 memset( &pk
, 0, sizeof pk
);
381 rc
= get_pubkey_byfprint( &pk
, fprint
, fprint_len
);
383 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
388 keyid_from_pk( &pk
, keyid
);
391 const byte
*dp
= fprint
;
392 keyid
[0] = dp
[12] << 24 | dp
[13] << 16 | dp
[14] << 8 | dp
[15] ;
393 keyid
[1] = dp
[16] << 24 | dp
[17] << 16 | dp
[18] << 8 | dp
[19] ;
401 keyid_from_sig( PKT_signature
*sig
, u32
*keyid
)
404 keyid
[0] = sig
->keyid
[0];
405 keyid
[1] = sig
->keyid
[1];
407 return sig
->keyid
[1];
411 namehash_from_uid(PKT_user_id
*uid
)
413 if(uid
->namehash
==NULL
)
415 uid
->namehash
=xmalloc(20);
418 rmd160_hash_buffer(uid
->namehash
,uid
->attrib_data
,uid
->attrib_len
);
420 rmd160_hash_buffer(uid
->namehash
,uid
->name
,uid
->len
);
423 return uid
->namehash
;
427 * return the number of bits used in the pk
430 nbits_from_pk( PKT_public_key
*pk
)
432 return pubkey_nbits( pk
->pubkey_algo
, pk
->pkey
);
436 * return the number of bits used in the sk
439 nbits_from_sk( PKT_secret_key
*sk
)
441 return pubkey_nbits( sk
->pubkey_algo
, sk
->skey
);
445 mk_datestr (char *buffer
, time_t atime
)
449 if ( atime
< 0 ) /* 32 bit time_t and after 2038-01-19 */
450 strcpy (buffer
, "????" "-??" "-??"); /* mark this as invalid */
452 tp
= gmtime (&atime
);
453 sprintf (buffer
,"%04d-%02d-%02d",
454 1900+tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
);
460 * return a string with the creation date of the pk
461 * Note: this is alloced in a static buffer.
462 * Format is: yyyy-mm-dd
465 datestr_from_pk( PKT_public_key
*pk
)
467 static char buffer
[11+5];
468 time_t atime
= pk
->timestamp
;
470 return mk_datestr (buffer
, atime
);
474 datestr_from_sk( PKT_secret_key
*sk
)
476 static char buffer
[11+5];
477 time_t atime
= sk
->timestamp
;
479 return mk_datestr (buffer
, atime
);
483 datestr_from_sig( PKT_signature
*sig
)
485 static char buffer
[11+5];
486 time_t atime
= sig
->timestamp
;
488 return mk_datestr (buffer
, atime
);
492 expirestr_from_pk( PKT_public_key
*pk
)
494 static char buffer
[11+5];
497 if( !pk
->expiredate
)
499 atime
= pk
->expiredate
;
500 return mk_datestr (buffer
, atime
);
504 expirestr_from_sk( PKT_secret_key
*sk
)
506 static char buffer
[11+5];
509 if( !sk
->expiredate
)
511 atime
= sk
->expiredate
;
512 return mk_datestr (buffer
, atime
);
516 expirestr_from_sig( PKT_signature
*sig
)
518 static char buffer
[11+5];
523 atime
=sig
->expiredate
;
524 return mk_datestr (buffer
, atime
);
528 revokestr_from_pk( PKT_public_key
*pk
)
530 static char buffer
[11+5];
533 if(!pk
->revoked
.date
)
535 atime
=pk
->revoked
.date
;
536 return mk_datestr (buffer
, atime
);
541 usagestr_from_pk( PKT_public_key
*pk
)
543 static char buffer
[10];
545 unsigned int use
= pk
->pubkey_usage
;
547 if ( use
& PUBKEY_USAGE_SIG
)
550 if ( use
& PUBKEY_USAGE_CERT
)
553 if ( use
& PUBKEY_USAGE_ENC
)
556 if ( (use
& PUBKEY_USAGE_AUTH
) )
568 colon_strtime (u32 t
)
572 if (opt
.fixed_list_mode
) {
574 sprintf (buf
, "%lu", (ulong
)t
);
577 return strtimestamp(t
);
581 colon_datestr_from_pk (PKT_public_key
*pk
)
583 if (opt
.fixed_list_mode
) {
585 sprintf (buf
, "%lu", (ulong
)pk
->timestamp
);
588 return datestr_from_pk (pk
);
592 colon_datestr_from_sk (PKT_secret_key
*sk
)
594 if (opt
.fixed_list_mode
) {
596 sprintf (buf
, "%lu", (ulong
)sk
->timestamp
);
599 return datestr_from_sk (sk
);
603 colon_datestr_from_sig (PKT_signature
*sig
)
605 if (opt
.fixed_list_mode
) {
607 sprintf (buf
, "%lu", (ulong
)sig
->timestamp
);
610 return datestr_from_sig (sig
);
614 colon_expirestr_from_sig (PKT_signature
*sig
)
618 if (opt
.fixed_list_mode
) {
620 sprintf (buf
, "%lu", (ulong
)sig
->expiredate
);
623 return expirestr_from_sig (sig
);
628 * Return a byte array with the fingerprint for the given PK/SK
629 * The length of the array is returned in ret_len. Caller must free
630 * the array or provide an array of length MAX_FINGERPRINT_LEN.
634 fingerprint_from_pk( PKT_public_key
*pk
, byte
*array
, size_t *ret_len
)
641 if( pk
->version
< 4 )
643 if( is_RSA(pk
->pubkey_algo
) )
645 /* RSA in version 3 packets is special */
648 md
= md_open( DIGEST_ALGO_MD5
, 0);
649 if( pubkey_get_npkey( pk
->pubkey_algo
) > 1 ) {
650 p
= buf
= mpi_get_buffer( pk
->pkey
[0], &n
, NULL
);
651 md_write( md
, p
, n
);
653 p
= buf
= mpi_get_buffer( pk
->pkey
[1], &n
, NULL
);
654 md_write( md
, p
, n
);
659 array
= xmalloc( 16 );
661 memcpy(array
, md_read(md
, DIGEST_ALGO_MD5
), 16 );
674 md
= do_fingerprint_md(pk
);
675 dp
= md_read( md
, 0 );
676 len
= md_digest_length( md_get_algo( md
) );
677 assert( len
<= MAX_FINGERPRINT_LEN
);
679 array
= xmalloc( len
);
680 memcpy(array
, dp
, len
);
681 pk
->keyid
[0] = dp
[12] << 24 | dp
[13] << 16 | dp
[14] << 8 | dp
[15] ;
682 pk
->keyid
[1] = dp
[16] << 24 | dp
[17] << 16 | dp
[18] << 8 | dp
[19] ;
691 fingerprint_from_sk( PKT_secret_key
*sk
, byte
*array
, size_t *ret_len
)
698 if( sk
->version
< 4 )
700 if( is_RSA(sk
->pubkey_algo
) )
702 /* RSA in version 3 packets is special */
705 md
= md_open( DIGEST_ALGO_MD5
, 0);
706 if( pubkey_get_npkey( sk
->pubkey_algo
) > 1 ) {
707 p
= buf
= mpi_get_buffer( sk
->skey
[0], &n
, NULL
);
708 md_write( md
, p
, n
);
710 p
= buf
= mpi_get_buffer( sk
->skey
[1], &n
, NULL
);
711 md_write( md
, p
, n
);
716 array
= xmalloc( 16 );
718 memcpy(array
, md_read(md
, DIGEST_ALGO_MD5
), 16 );
731 md
= do_fingerprint_md_sk(sk
);
734 dp
= md_read( md
, 0 );
735 len
= md_digest_length( md_get_algo( md
) );
736 assert( len
<= MAX_FINGERPRINT_LEN
);
738 array
= xmalloc( len
);
739 memcpy(array
, dp
, len
);
744 len
=MAX_FINGERPRINT_LEN
;