1 /* keyid.c - key ID and fingerprint handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 * 2004, 2006, 2010 Free Software Foundation, Inc.
4 * Copyright (C) 2014 Werner Koch
6 * This file is part of GnuPG.
8 * GnuPG is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuPG is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
41 #define KEYID_STR_SIZE 19
43 #ifdef HAVE_UNSIGNED_TIME_T
44 # define IS_INVALID_TIME_T(a) ((a) == (time_t)(-1))
46 /* Error or 32 bit time_t and value after 2038-01-19. */
47 # define IS_INVALID_TIME_T(a) ((a) < 0)
51 /* Return a letter describing the public key algorithms. */
53 pubkey_letter( int algo )
57 case PUBKEY_ALGO_RSA: return 'R' ;
58 case PUBKEY_ALGO_RSA_E: return 'r' ;
59 case PUBKEY_ALGO_RSA_S: return 's' ;
60 case PUBKEY_ALGO_ELGAMAL_E: return 'g' ;
61 case PUBKEY_ALGO_ELGAMAL: return 'G' ;
62 case PUBKEY_ALGO_DSA: return 'D' ;
63 case PUBKEY_ALGO_ECDH: return 'e' ; /* ECC DH (encrypt only) */
64 case PUBKEY_ALGO_ECDSA: return 'E' ; /* ECC DSA (sign only) */
65 case PUBKEY_ALGO_EDDSA: return 'E' ; /* ECC EdDSA (sign only) */
70 /* Return a string describing the public key algorithm and the
71 keysize. For elliptic curves the functions prints the name of the
72 curve because the keysize is a property of the curve. The string
73 is copied to the supplied buffer up a length of BUFSIZE-1.
74 Examples for the output are:
76 "rsa2048" - RSA with 2048 bit
77 "elg1024" - Elgamal with 1024 bit
78 "ed25519" - ECC using the curve Ed25519.
79 "E_1.2.3.4" - ECC using the unsupported curve with OID "1.2.3.4".
80 "E_1.3.6.1.4.1.11591.2.12242973" ECC with a bogus OID.
81 "unknown_N" - Unknown OpenPGP algorithm N.
83 If the option --legacy-list-mode is active, the output use the
86 "2048R" - RSA with 2048 bit
87 "1024g" - Elgamal with 1024 bit
88 "256E" - ECDSA using a curve with 256 bit
90 The macro PUBKEY_STRING_SIZE may be used to allocate a buffer with
93 pubkey_string (PKT_public_key *pk, char *buffer, size_t bufsize)
95 const char *prefix = NULL;
97 if (opt.legacy_list_mode)
99 snprintf (buffer, bufsize, "%4u%c",
100 nbits_from_pk (pk), pubkey_letter (pk->pubkey_algo));
104 switch (pk->pubkey_algo)
106 case PUBKEY_ALGO_RSA:
107 case PUBKEY_ALGO_RSA_E:
108 case PUBKEY_ALGO_RSA_S: prefix = "rsa"; break;
109 case PUBKEY_ALGO_ELGAMAL_E: prefix = "elg"; break;
110 case PUBKEY_ALGO_DSA: prefix = "dsa"; break;
111 case PUBKEY_ALGO_ELGAMAL: prefix = "xxx"; break;
112 case PUBKEY_ALGO_ECDH:
113 case PUBKEY_ALGO_ECDSA:
114 case PUBKEY_ALGO_EDDSA: prefix = ""; break;
117 if (prefix && *prefix)
118 snprintf (buffer, bufsize, "%s%u", prefix, nbits_from_pk (pk));
121 char *curve = openpgp_oid_to_str (pk->pkey[0]);
122 const char *name = openpgp_oid_to_curve (curve);
124 if (*name && *name != '?')
125 snprintf (buffer, bufsize, "%s", name);
127 snprintf (buffer, bufsize, "E_%s", curve);
129 snprintf (buffer, bufsize, "E_error");
133 snprintf (buffer, bufsize, "unknown_%u", (unsigned int)pk->pubkey_algo);
139 /* Hash a public key. This function is useful for v4 fingerprints and
140 for v3 or v4 key signing. */
142 hash_public_key (gcry_md_hd_t md, PKT_public_key *pk)
145 unsigned int nn[PUBKEY_MAX_NPKEY];
146 byte *pp[PUBKEY_MAX_NPKEY];
150 int npkey = pubkey_get_npkey (pk->pubkey_algo);
152 /* FIXME: We can avoid the extra malloc by calling only the first
153 mpi_print here which computes the required length and calling the
154 real mpi_print only at the end. The speed advantage would only be
155 for ECC (opaque MPIs) or if we could implement an mpi_print
156 variant with a callback handler to do the hashing. */
157 if (npkey==0 && pk->pkey[0]
158 && gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
160 pp[0] = gcry_mpi_get_opaque (pk->pkey[0], &nbits);
166 for (i=0; i < npkey; i++ )
170 /* This case may only happen if the parsing of the MPI
171 failed but the key was anyway created. May happen
172 during "gpg KEYFILE". */
176 else if (gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_OPAQUE))
180 p = gcry_mpi_get_opaque (pk->pkey[i], &nbits);
181 pp[i] = xmalloc ((nbits+7)/8);
183 memcpy (pp[i], p, (nbits+7)/8);
191 if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0,
192 &nbytes, pk->pkey[i]))
194 pp[i] = xmalloc (nbytes);
195 if (gcry_mpi_print (GCRYMPI_FMT_PGP, pp[i], nbytes,
196 &nbytes, pk->pkey[i]))
204 gcry_md_putc ( md, 0x99 ); /* ctb */
205 /* What does it mean if n is greater than than 0xFFFF ? */
206 gcry_md_putc ( md, n >> 8 ); /* 2 byte length header */
207 gcry_md_putc ( md, n );
208 gcry_md_putc ( md, pk->version );
210 gcry_md_putc ( md, pk->timestamp >> 24 );
211 gcry_md_putc ( md, pk->timestamp >> 16 );
212 gcry_md_putc ( md, pk->timestamp >> 8 );
213 gcry_md_putc ( md, pk->timestamp );
215 gcry_md_putc ( md, pk->pubkey_algo );
217 if(npkey==0 && pk->pkey[0]
218 && gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
221 gcry_md_write (md, pp[0], nn[0]);
225 for(i=0; i < npkey; i++ )
228 gcry_md_write ( md, pp[i], nn[i] );
236 do_fingerprint_md( PKT_public_key *pk )
240 if (gcry_md_open (&md, DIGEST_ALGO_SHA1, 0))
242 hash_public_key(md,pk);
249 /* fixme: Check whether we can replace this function or if not
250 describe why we need it. */
252 v3_keyid (gcry_mpi_t a, u32 *ki)
257 if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &nbytes, a ))
259 /* fixme: allocate it on the stack */
260 buffer = xmalloc (nbytes);
261 if (gcry_mpi_print( GCRYMPI_FMT_USG, buffer, nbytes, NULL, a ))
263 if (nbytes < 8) /* oops */
267 p = buffer + nbytes - 8;
268 ki[0] = buf32_to_u32 (p);
270 ki[1] = buf32_to_u32 (p);
280 switch(opt.keyid_format)
303 static char keyid_str[KEYID_STR_SIZE];
305 switch (opt.keyid_format)
308 snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]);
313 snprintf (keyid_str, sizeof keyid_str, "%08lX%08lX",
314 (ulong)keyid[0], (ulong)keyid[1]);
316 snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]);
320 snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]);
325 snprintf (keyid_str, sizeof keyid_str, "0x%08lX%08lX",
326 (ulong)keyid[0],(ulong)keyid[1]);
328 snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]);
340 keystr_with_sub (u32 *main_kid, u32 *sub_kid)
342 static char buffer[KEYID_STR_SIZE+1+KEYID_STR_SIZE];
345 mem2str (buffer, keystr (main_kid), KEYID_STR_SIZE);
348 p = buffer + strlen (buffer);
350 mem2str (p, keystr (sub_kid), KEYID_STR_SIZE);
357 keystr_from_pk(PKT_public_key *pk)
359 keyid_from_pk(pk,NULL);
361 return keystr(pk->keyid);
366 keystr_from_pk_with_sub (PKT_public_key *main_pk, PKT_public_key *sub_pk)
368 keyid_from_pk (main_pk, NULL);
370 keyid_from_pk (sub_pk, NULL);
372 return keystr_with_sub (main_pk->keyid, sub_pk? sub_pk->keyid:NULL);
378 keystr_from_desc(KEYDB_SEARCH_DESC *desc)
382 case KEYDB_SEARCH_MODE_LONG_KID:
383 case KEYDB_SEARCH_MODE_SHORT_KID:
384 return keystr(desc->u.kid);
386 case KEYDB_SEARCH_MODE_FPR20:
390 keyid[0] = buf32_to_u32 (desc->u.fpr+12);
391 keyid[1] = buf32_to_u32 (desc->u.fpr+16);
392 return keystr(keyid);
395 case KEYDB_SEARCH_MODE_FPR16:
405 * Get the keyid from the public key and put it into keyid
406 * if this is not NULL. Return the 32 low bits of the keyid.
409 keyid_from_pk (PKT_public_key *pk, u32 *keyid)
417 if( pk->keyid[0] || pk->keyid[1] )
419 keyid[0] = pk->keyid[0];
420 keyid[1] = pk->keyid[1];
428 md = do_fingerprint_md(pk);
431 dp = gcry_md_read ( md, 0 );
432 keyid[0] = buf32_to_u32 (dp+12);
433 keyid[1] = buf32_to_u32 (dp+16);
436 pk->keyid[0] = keyid[0];
437 pk->keyid[1] = keyid[1];
440 pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
448 * Get the keyid from the fingerprint. This function is simple for most
449 * keys, but has to do a keylookup for old stayle keys.
452 keyid_from_fingerprint( const byte *fprint, size_t fprint_len, u32 *keyid )
459 if (fprint_len != 20)
461 /* This is special as we have to lookup the key first. */
465 memset (&pk, 0, sizeof pk);
466 rc = get_pubkey_byfprint (&pk, fprint, fprint_len);
469 log_error("Oops: keyid_from_fingerprint: no pubkey\n");
474 keyid_from_pk (&pk, keyid);
478 const byte *dp = fprint;
479 keyid[0] = buf32_to_u32 (dp+12);
480 keyid[1] = buf32_to_u32 (dp+16);
488 keyid_from_sig (PKT_signature *sig, u32 *keyid)
492 keyid[0] = sig->keyid[0];
493 keyid[1] = sig->keyid[1];
495 return sig->keyid[1];
500 namehash_from_uid (PKT_user_id *uid)
504 uid->namehash = xmalloc (20);
506 if (uid->attrib_data)
507 rmd160_hash_buffer (uid->namehash, uid->attrib_data, uid->attrib_len);
509 rmd160_hash_buffer (uid->namehash, uid->name, uid->len);
512 return uid->namehash;
517 * Return the number of bits used in PK.
520 nbits_from_pk (PKT_public_key *pk)
522 return pubkey_nbits (pk->pubkey_algo, pk->pkey);
527 mk_datestr (char *buffer, time_t atime)
531 if (IS_INVALID_TIME_T (atime))
532 strcpy (buffer, "????" "-??" "-??"); /* Mark this as invalid. */
535 tp = gmtime (&atime);
536 sprintf (buffer,"%04d-%02d-%02d",
537 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
544 * return a string with the creation date of the pk
545 * Note: this is alloced in a static buffer.
546 * Format is: yyyy-mm-dd
549 datestr_from_pk (PKT_public_key *pk)
551 static char buffer[11+5];
552 time_t atime = pk->timestamp;
554 return mk_datestr (buffer, atime);
559 datestr_from_sig (PKT_signature *sig )
561 static char buffer[11+5];
562 time_t atime = sig->timestamp;
564 return mk_datestr (buffer, atime);
569 expirestr_from_pk (PKT_public_key *pk)
571 static char buffer[11+5];
576 atime = pk->expiredate;
577 return mk_datestr (buffer, atime);
582 expirestr_from_sig (PKT_signature *sig)
584 static char buffer[11+5];
587 if (!sig->expiredate)
589 atime=sig->expiredate;
590 return mk_datestr (buffer, atime);
595 revokestr_from_pk( PKT_public_key *pk )
597 static char buffer[11+5];
600 if(!pk->revoked.date)
602 atime=pk->revoked.date;
603 return mk_datestr (buffer, atime);
608 usagestr_from_pk (PKT_public_key *pk, int fill)
610 static char buffer[10];
612 unsigned int use = pk->pubkey_usage;
614 if ( use & PUBKEY_USAGE_SIG )
617 if ( use & PUBKEY_USAGE_CERT )
620 if ( use & PUBKEY_USAGE_ENC )
623 if ( (use & PUBKEY_USAGE_AUTH) )
626 while (fill && i < 4)
635 colon_strtime (u32 t)
641 snprintf (buf, sizeof buf, "%lu", (ulong)t);
646 colon_datestr_from_pk (PKT_public_key *pk)
650 snprintf (buf, sizeof buf, "%lu", (ulong)pk->timestamp);
656 colon_datestr_from_sig (PKT_signature *sig)
660 snprintf (buf, sizeof buf, "%lu", (ulong)sig->timestamp);
665 colon_expirestr_from_sig (PKT_signature *sig)
669 if (!sig->expiredate)
672 snprintf (buf, sizeof buf,"%lu", (ulong)sig->expiredate);
678 * Return a byte array with the fingerprint for the given PK/SK
679 * The length of the array is returned in ret_len. Caller must free
680 * the array or provide an array of length MAX_FINGERPRINT_LEN.
683 fingerprint_from_pk (PKT_public_key *pk, byte *array, size_t *ret_len)
689 md = do_fingerprint_md(pk);
690 dp = gcry_md_read( md, 0 );
691 len = gcry_md_get_algo_dlen (gcry_md_get_algo (md));
692 assert( len <= MAX_FINGERPRINT_LEN );
694 array = xmalloc ( len );
695 memcpy (array, dp, len );
696 pk->keyid[0] = buf32_to_u32 (dp+12);
697 pk->keyid[1] = buf32_to_u32 (dp+16);
706 /* Return an allocated buffer with the fingerprint of PK formatted as
707 a plain hexstring. */
709 hexfingerprint (PKT_public_key *pk)
711 unsigned char fpr[MAX_FINGERPRINT_LEN];
715 fingerprint_from_pk (pk, fpr, &len);
716 result = xmalloc (2 * len + 1);
717 bin2hex (fpr, len, result);
723 /* Return the so called KEYGRIP which is the SHA-1 hash of the public
724 key parameters expressed as an canoncial encoded S-Exp. ARRAY must
725 be 20 bytes long. Returns 0 on sucess or an error code. */
727 keygrip_from_pk (PKT_public_key *pk, unsigned char *array)
733 log_debug ("get_keygrip for public key\n");
735 switch (pk->pubkey_algo)
738 err = gcry_sexp_build (&s_pkey, NULL,
739 "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
740 pk->pkey[0], pk->pkey[1],
741 pk->pkey[2], pk->pkey[3]);
746 err = gcry_sexp_build (&s_pkey, NULL,
747 "(public-key(elg(p%m)(g%m)(y%m)))",
748 pk->pkey[0], pk->pkey[1], pk->pkey[2]);
754 err = gcry_sexp_build (&s_pkey, NULL,
755 "(public-key(rsa(n%m)(e%m)))",
756 pk->pkey[0], pk->pkey[1]);
759 case PUBKEY_ALGO_EDDSA:
760 case PUBKEY_ALGO_ECDSA:
761 case PUBKEY_ALGO_ECDH:
763 char *curve = openpgp_oid_to_str (pk->pkey[0]);
765 err = gpg_error_from_syserror ();
768 err = gcry_sexp_build (&s_pkey, NULL,
769 pk->pubkey_algo == PUBKEY_ALGO_EDDSA ?
770 "(public-key(ecc(curve%s)(flags eddsa)(q%m)))"
771 : "(public-key(ecc(curve%s)(q%m)))",
779 err = gpg_error (GPG_ERR_PUBKEY_ALGO);
786 if (!gcry_pk_get_keygrip (s_pkey, array))
788 log_info ("error computing keygrip\n");
789 memset (array, 0, 20);
790 err = gpg_error (GPG_ERR_GENERAL);
795 log_printhex ("keygrip=", array, 20);
796 /* FIXME: Save the keygrip in PK. */
798 gcry_sexp_release (s_pkey);
804 /* Store an allocated buffer with the keygrip of PK encoded as a
805 hexstring at r_GRIP. Returns 0 on success. */
807 hexkeygrip_from_pk (PKT_public_key *pk, char **r_grip)
810 unsigned char grip[20];
813 err = keygrip_from_pk (pk, grip);
816 char * buf = xtrymalloc (20*2+1);
818 err = gpg_error_from_syserror ();
821 bin2hex (grip, 20, buf);