* keyserver.c (path_makes_direct): New. (keyserver_spawn): Used here
[gnupg.git] / g10 / keyid.c
blob86a204ff3efc6e6452a777978f3a756acbc37a85
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,
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>
30 #include "util.h"
31 #include "main.h"
32 #include "packet.h"
33 #include "options.h"
34 #include "mpi.h"
35 #include "keydb.h"
36 #include "i18n.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( MD_HANDLE md, PKT_public_key *pk )
57 unsigned n=6;
58 unsigned nb[PUBKEY_MAX_NPKEY];
59 unsigned nn[PUBKEY_MAX_NPKEY];
60 byte *pp[PUBKEY_MAX_NPKEY];
61 int i;
62 int npkey = pubkey_get_npkey( pk->pubkey_algo );
64 /* Two extra bytes for the expiration date in v3 */
65 if(pk->version<4)
66 n+=2;
68 if(npkey==0 && pk->pkey[0] && mpi_is_opaque(pk->pkey[0]))
70 pp[0]=mpi_get_opaque(pk->pkey[0],&nn[0]);
71 n+=nn[0];
73 else
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 );
78 n += 2 + nn[i];
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 */
84 md_putc( md, n );
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 );
92 if(pk->version<4)
94 u16 days=0;
95 if(pk->expiredate)
96 days=(u16)((pk->expiredate - pk->timestamp) / 86400L);
98 md_putc( md, days >> 8 );
99 md_putc( md, days );
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]);
106 else
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] );
112 xfree(pp[i]);
116 static MD_HANDLE
117 do_fingerprint_md( PKT_public_key *pk )
119 MD_HANDLE md;
121 md = md_open( DIGEST_ALGO_SHA1, 0);
122 hash_public_key(md,pk);
123 md_final( md );
125 return md;
128 static MD_HANDLE
129 do_fingerprint_md_sk( PKT_secret_key *sk )
131 PKT_public_key pk;
132 int npkey = pubkey_get_npkey( sk->pubkey_algo ); /* npkey is correct! */
133 int i;
135 if(npkey==0)
136 return NULL;
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 );
148 size_t
149 keystrlen(void)
151 switch(opt.keyid_format)
153 case KF_SHORT:
154 return 8;
156 case KF_LONG:
157 return 16;
159 case KF_0xSHORT:
160 return 10;
162 case KF_0xLONG:
163 return 18;
165 default:
166 BUG();
170 const char *
171 keystr(u32 *keyid)
173 static char keyid_str[19];
175 switch(opt.keyid_format)
177 case KF_SHORT:
178 sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
179 break;
181 case KF_LONG:
182 if(keyid[0])
183 sprintf(keyid_str,"%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
184 else
185 sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
186 break;
188 case KF_0xSHORT:
189 sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
190 break;
192 case KF_0xLONG:
193 if(keyid[0])
194 sprintf(keyid_str,"0x%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
195 else
196 sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
197 break;
199 default:
200 BUG();
203 return keyid_str;
206 const char *
207 keystr_from_pk(PKT_public_key *pk)
209 keyid_from_pk(pk,NULL);
211 return keystr(pk->keyid);
214 const char *
215 keystr_from_sk(PKT_secret_key *sk)
217 keyid_from_sk(sk,NULL);
219 return keystr(sk->keyid);
222 const char *
223 keystr_from_desc(KEYDB_SEARCH_DESC *desc)
225 switch(desc->mode)
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:
233 u32 keyid[2];
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:
248 return "?v3 fpr?";
250 default:
251 BUG();
255 /****************
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 )
262 u32 lowbits;
263 u32 dummy_keyid[2];
265 if( !keyid )
266 keyid = dummy_keyid;
268 if( sk->keyid[0] || sk->keyid[1] )
270 keyid[0] = sk->keyid[0];
271 keyid[1] = sk->keyid[1];
272 lowbits = 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];
283 else
284 sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
286 else
288 const byte *dp;
289 MD_HANDLE md;
290 md = do_fingerprint_md_sk(sk);
291 if(md)
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] ;
296 lowbits = keyid[1];
297 md_close(md);
298 sk->keyid[0] = keyid[0];
299 sk->keyid[1] = keyid[1];
301 else
302 sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
305 return lowbits;
309 /****************
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 )
316 u32 lowbits;
317 u32 dummy_keyid[2];
319 if( !keyid )
320 keyid = dummy_keyid;
322 if( pk->keyid[0] || pk->keyid[1] )
324 keyid[0] = pk->keyid[0];
325 keyid[1] = pk->keyid[1];
326 lowbits = 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];
337 else
338 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
340 else
342 const byte *dp;
343 MD_HANDLE md;
344 md = do_fingerprint_md(pk);
345 if(md)
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] ;
350 lowbits = keyid[1];
351 md_close(md);
352 pk->keyid[0] = keyid[0];
353 pk->keyid[1] = keyid[1];
355 else
356 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
359 return lowbits;
363 /****************
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 )
370 u32 dummy_keyid[2];
372 if( !keyid )
373 keyid = dummy_keyid;
375 if( fprint_len != 20 ) {
376 /* This is special as we have to lookup the key first */
377 PKT_public_key pk;
378 int rc;
380 memset( &pk, 0, sizeof pk );
381 rc = get_pubkey_byfprint( &pk, fprint, fprint_len );
382 if( rc ) {
383 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
384 keyid[0] = 0;
385 keyid[1] = 0;
387 else
388 keyid_from_pk( &pk, keyid );
390 else {
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] ;
396 return keyid[1];
401 keyid_from_sig( PKT_signature *sig, u32 *keyid )
403 if( keyid ) {
404 keyid[0] = sig->keyid[0];
405 keyid[1] = sig->keyid[1];
407 return sig->keyid[1];
410 byte *
411 namehash_from_uid(PKT_user_id *uid)
413 if(uid->namehash==NULL)
415 uid->namehash=xmalloc(20);
417 if(uid->attrib_data)
418 rmd160_hash_buffer(uid->namehash,uid->attrib_data,uid->attrib_len);
419 else
420 rmd160_hash_buffer(uid->namehash,uid->name,uid->len);
423 return uid->namehash;
426 /****************
427 * return the number of bits used in the pk
429 unsigned
430 nbits_from_pk( PKT_public_key *pk )
432 return pubkey_nbits( pk->pubkey_algo, pk->pkey );
435 /****************
436 * return the number of bits used in the sk
438 unsigned
439 nbits_from_sk( PKT_secret_key *sk )
441 return pubkey_nbits( sk->pubkey_algo, sk->skey );
444 static const char *
445 mk_datestr (char *buffer, time_t atime)
447 struct tm *tp;
449 if ( atime < 0 ) /* 32 bit time_t and after 2038-01-19 */
450 strcpy (buffer, "????" "-??" "-??"); /* mark this as invalid */
451 else {
452 tp = gmtime (&atime);
453 sprintf (buffer,"%04d-%02d-%02d",
454 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
456 return buffer;
459 /****************
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
464 const char *
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);
473 const char *
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);
482 const char *
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);
491 const char *
492 expirestr_from_pk( PKT_public_key *pk )
494 static char buffer[11+5];
495 time_t atime;
497 if( !pk->expiredate )
498 return _("never ");
499 atime = pk->expiredate;
500 return mk_datestr (buffer, atime);
503 const char *
504 expirestr_from_sk( PKT_secret_key *sk )
506 static char buffer[11+5];
507 time_t atime;
509 if( !sk->expiredate )
510 return _("never ");
511 atime = sk->expiredate;
512 return mk_datestr (buffer, atime);
515 const char *
516 expirestr_from_sig( PKT_signature *sig )
518 static char buffer[11+5];
519 time_t atime;
521 if(!sig->expiredate)
522 return _("never ");
523 atime=sig->expiredate;
524 return mk_datestr (buffer, atime);
527 const char *
528 revokestr_from_pk( PKT_public_key *pk )
530 static char buffer[11+5];
531 time_t atime;
533 if(!pk->revoked.date)
534 return _("never ");
535 atime=pk->revoked.date;
536 return mk_datestr (buffer, atime);
540 const char *
541 usagestr_from_pk( PKT_public_key *pk )
543 static char buffer[10];
544 int i = 0;
545 unsigned int use = pk->pubkey_usage;
547 if ( use & PUBKEY_USAGE_SIG )
548 buffer[i++] = 'S';
550 if ( use & PUBKEY_USAGE_CERT )
551 buffer[i++] = 'C';
553 if ( use & PUBKEY_USAGE_ENC )
554 buffer[i++] = 'E';
556 if ( (use & PUBKEY_USAGE_AUTH) )
557 buffer[i++] = 'A';
559 while (i < 4)
560 buffer[i++] = ' ';
562 buffer[i] = 0;
563 return buffer;
567 const char *
568 colon_strtime (u32 t)
570 if (!t)
571 return "";
572 if (opt.fixed_list_mode) {
573 static char buf[15];
574 sprintf (buf, "%lu", (ulong)t);
575 return buf;
577 return strtimestamp(t);
580 const char *
581 colon_datestr_from_pk (PKT_public_key *pk)
583 if (opt.fixed_list_mode) {
584 static char buf[15];
585 sprintf (buf, "%lu", (ulong)pk->timestamp);
586 return buf;
588 return datestr_from_pk (pk);
591 const char *
592 colon_datestr_from_sk (PKT_secret_key *sk)
594 if (opt.fixed_list_mode) {
595 static char buf[15];
596 sprintf (buf, "%lu", (ulong)sk->timestamp);
597 return buf;
599 return datestr_from_sk (sk);
602 const char *
603 colon_datestr_from_sig (PKT_signature *sig)
605 if (opt.fixed_list_mode) {
606 static char buf[15];
607 sprintf (buf, "%lu", (ulong)sig->timestamp);
608 return buf;
610 return datestr_from_sig (sig);
613 const char *
614 colon_expirestr_from_sig (PKT_signature *sig)
616 if(!sig->expiredate)
617 return "";
618 if (opt.fixed_list_mode) {
619 static char buf[15];
620 sprintf (buf, "%lu", (ulong)sig->expiredate);
621 return buf;
623 return expirestr_from_sig (sig);
627 /**************** .
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.
633 byte *
634 fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len )
636 byte *p, *buf;
637 const byte *dp;
638 size_t len;
639 unsigned int n;
641 if( pk->version < 4 )
643 if( is_RSA(pk->pubkey_algo) )
645 /* RSA in version 3 packets is special */
646 MD_HANDLE md;
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 );
652 xfree(buf);
653 p = buf = mpi_get_buffer( pk->pkey[1], &n, NULL );
654 md_write( md, p, n );
655 xfree(buf);
657 md_final(md);
658 if( !array )
659 array = xmalloc( 16 );
660 len = 16;
661 memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 );
662 md_close(md);
664 else
666 if(!array)
667 array=xmalloc(16);
668 len=16;
669 memset(array,0,16);
672 else {
673 MD_HANDLE md;
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 );
678 if( !array )
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] ;
683 md_close(md);
686 *ret_len = len;
687 return array;
690 byte *
691 fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len )
693 byte *p, *buf;
694 const char *dp;
695 size_t len;
696 unsigned n;
698 if( sk->version < 4 )
700 if( is_RSA(sk->pubkey_algo) )
702 /* RSA in version 3 packets is special */
703 MD_HANDLE md;
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 );
709 xfree(buf);
710 p = buf = mpi_get_buffer( sk->skey[1], &n, NULL );
711 md_write( md, p, n );
712 xfree(buf);
714 md_final(md);
715 if( !array )
716 array = xmalloc( 16 );
717 len = 16;
718 memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 );
719 md_close(md);
721 else
723 if(!array)
724 array=xmalloc(16);
725 len=16;
726 memset(array,0,16);
729 else {
730 MD_HANDLE md;
731 md = do_fingerprint_md_sk(sk);
732 if(md)
734 dp = md_read( md, 0 );
735 len = md_digest_length( md_get_algo( md ) );
736 assert( len <= MAX_FINGERPRINT_LEN );
737 if( !array )
738 array = xmalloc( len );
739 memcpy(array, dp, len );
740 md_close(md);
742 else
744 len=MAX_FINGERPRINT_LEN;
745 if(!array)
746 array=xmalloc(len);
747 memset(array,0,len);
751 *ret_len = len;
752 return array;