diff options
author | Werner Koch <wk@gnupg.org> | 2010-09-06 21:57:42 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2010-09-06 21:57:42 +0200 |
commit | 299ed4c9e26f16ce5365ed69dde5e109aa4e4f9e (patch) | |
tree | 76ec60edc1f48dff9ad7b1d968ebedab0692dc14 /g10/keyid.c | |
parent | Merge secret keys during import (diff) | |
download | gnupg2-299ed4c9e26f16ce5365ed69dde5e109aa4e4f9e.tar.xz gnupg2-299ed4c9e26f16ce5365ed69dde5e109aa4e4f9e.zip |
Removed more secret key related code.
It builds fine and passes some of the tests but there are quite some
features which don't work yet.
Diffstat (limited to 'g10/keyid.c')
-rw-r--r-- | g10/keyid.c | 452 |
1 files changed, 122 insertions, 330 deletions
diff --git a/g10/keyid.c b/g10/keyid.c index a6e8b3728..1d0972649 100644 --- a/g10/keyid.c +++ b/g10/keyid.c @@ -38,24 +38,27 @@ #define KEYID_STR_SIZE 19 +/* Return a letter describing the public key algorithms. */ int pubkey_letter( int algo ) { - switch( algo ) { - case PUBKEY_ALGO_RSA: return 'R' ; - case PUBKEY_ALGO_RSA_E: return 'r' ; - case PUBKEY_ALGO_RSA_S: return 's' ; - case PUBKEY_ALGO_ELGAMAL_E: return 'g'; - case PUBKEY_ALGO_ELGAMAL: return 'G' ; - case PUBKEY_ALGO_DSA: return 'D' ; - default: return '?'; + switch (algo) + { + case PUBKEY_ALGO_RSA: return 'R' ; + case PUBKEY_ALGO_RSA_E: return 'r' ; + case PUBKEY_ALGO_RSA_S: return 's' ; + case PUBKEY_ALGO_ELGAMAL_E: return 'g'; + case PUBKEY_ALGO_ELGAMAL: return 'G' ; + case PUBKEY_ALGO_DSA: return 'D' ; + default: return '?'; } } -/* This function is useful for v4 fingerprints and v3 or v4 key - signing. */ + +/* Hash a public key. This function is useful for v4 fingerprints and + for v3 or v4 key signing. */ void -hash_public_key( gcry_md_hd_t md, PKT_public_key *pk ) +hash_public_key (gcry_md_hd_t md, PKT_public_key *pk) { unsigned int n = 6; unsigned int nn[PUBKEY_MAX_NPKEY]; @@ -77,17 +80,19 @@ hash_public_key( gcry_md_hd_t md, PKT_public_key *pk ) n+=nn[0]; } else - for(i=0; i < npkey; i++ ) - { - if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, pk->pkey[i])) - BUG (); - pp[i] = xmalloc (nbytes); - if (gcry_mpi_print (GCRYMPI_FMT_PGP, pp[i], nbytes, - &nbytes, pk->pkey[i])) - BUG (); - nn[i] = nbytes; - n += nn[i]; - } + { + for(i=0; i < npkey; i++ ) + { + if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, pk->pkey[i])) + BUG (); + pp[i] = xmalloc (nbytes); + if (gcry_mpi_print (GCRYMPI_FMT_PGP, pp[i], nbytes, + &nbytes, pk->pkey[i])) + BUG (); + nn[i] = nbytes; + n += nn[i]; + } + } gcry_md_putc ( md, 0x99 ); /* ctb */ /* What does it mean if n is greater than than 0xFFFF ? */ @@ -125,6 +130,7 @@ hash_public_key( gcry_md_hd_t md, PKT_public_key *pk ) } } + static gcry_md_hd_t do_fingerprint_md( PKT_public_key *pk ) { @@ -138,27 +144,9 @@ do_fingerprint_md( PKT_public_key *pk ) return md; } -static gcry_md_hd_t -do_fingerprint_md_sk( PKT_secret_key *sk ) -{ - PKT_public_key pk; - int npkey = pubkey_get_npkey( sk->pubkey_algo ); /* npkey is correct! */ - int i; - - if(npkey==0) - return NULL; - - pk.pubkey_algo = sk->pubkey_algo; - pk.version = sk->version; - pk.timestamp = sk->timestamp; - pk.expiredate = sk->expiredate; - pk.pubkey_algo = sk->pubkey_algo; - for( i=0; i < npkey; i++ ) - pk.pkey[i] = sk->skey[i]; - return do_fingerprint_md( &pk ); -} - +/* fixme: Check whether we can replace this function or if not + describe why we need it. */ u32 v3_keyid (gcry_mpi_t a, u32 *ki) { @@ -280,24 +268,6 @@ keystr_from_pk_with_sub (PKT_public_key *main_pk, PKT_public_key *sub_pk) } -const char * -keystr_from_sk(PKT_secret_key *sk) -{ - keyid_from_sk (sk,NULL); - - return keystr(sk->keyid); -} - - -const char * -keystr_from_sk_with_sub (PKT_secret_key *main_sk, PKT_secret_key *sub_sk) -{ - keyid_from_sk (main_sk, NULL); - keyid_from_sk (sub_sk, NULL); - - return keystr_with_sub (main_sk->keyid, sub_sk->keyid); -} - const char * keystr_from_desc(KEYDB_SEARCH_DESC *desc) @@ -332,72 +302,18 @@ keystr_from_desc(KEYDB_SEARCH_DESC *desc) } } -/**************** - * Get the keyid from the secret key and put it into keyid - * if this is not NULL. Return the 32 low bits of the keyid. - */ -u32 -keyid_from_sk( PKT_secret_key *sk, u32 *keyid ) -{ - u32 lowbits; - u32 dummy_keyid[2]; - if( !keyid ) - keyid = dummy_keyid; - - if( sk->keyid[0] || sk->keyid[1] ) - { - keyid[0] = sk->keyid[0]; - keyid[1] = sk->keyid[1]; - lowbits = keyid[1]; - } - else if( sk->version < 4 ) - { - if( is_RSA(sk->pubkey_algo) ) - { - lowbits = (pubkey_get_npkey (sk->pubkey_algo) ? - v3_keyid( sk->skey[0], keyid ) : 0); /* Take n. */ - sk->keyid[0]=keyid[0]; - sk->keyid[1]=keyid[1]; - } - else - sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF; - } - else - { - const byte *dp; - gcry_md_hd_t md; - - md = do_fingerprint_md_sk(sk); - if(md) - { - dp = gcry_md_read (md, 0); - keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ; - keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ; - lowbits = keyid[1]; - gcry_md_close (md); - sk->keyid[0] = keyid[0]; - sk->keyid[1] = keyid[1]; - } - else - sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF; - } - - return lowbits; -} - - -/**************** +/* * Get the keyid from the public key and put it into keyid * if this is not NULL. Return the 32 low bits of the keyid. */ u32 -keyid_from_pk( PKT_public_key *pk, u32 *keyid ) +keyid_from_pk (PKT_public_key *pk, u32 *keyid) { u32 lowbits; u32 dummy_keyid[2]; - if( !keyid ) + if (!keyid) keyid = dummy_keyid; if( pk->keyid[0] || pk->keyid[1] ) @@ -442,61 +358,66 @@ keyid_from_pk( PKT_public_key *pk, u32 *keyid ) } -/**************** +/* * Get the keyid from the fingerprint. This function is simple for most * keys, but has to do a keylookup for old stayle keys. */ u32 keyid_from_fingerprint( const byte *fprint, size_t fprint_len, u32 *keyid ) { - u32 dummy_keyid[2]; - - if( !keyid ) - keyid = dummy_keyid; - - if( fprint_len != 20 ) { - /* This is special as we have to lookup the key first */ - PKT_public_key pk; - int rc; - - memset( &pk, 0, sizeof pk ); - rc = get_pubkey_byfprint( &pk, fprint, fprint_len ); - if( rc ) { - log_error("Oops: keyid_from_fingerprint: no pubkey\n"); - keyid[0] = 0; - keyid[1] = 0; - } - else - keyid_from_pk( &pk, keyid ); + u32 dummy_keyid[2]; + + if( !keyid ) + keyid = dummy_keyid; + + if (fprint_len != 20) + { + /* This is special as we have to lookup the key first. */ + PKT_public_key pk; + int rc; + + memset (&pk, 0, sizeof pk); + rc = get_pubkey_byfprint (&pk, fprint, fprint_len); + if( rc ) + { + log_error("Oops: keyid_from_fingerprint: no pubkey\n"); + keyid[0] = 0; + keyid[1] = 0; + } + else + keyid_from_pk (&pk, keyid); } - else { - const byte *dp = fprint; - keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ; - keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ; + else + { + const byte *dp = fprint; + keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ; + keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ; } - return keyid[1]; + return keyid[1]; } u32 -keyid_from_sig( PKT_signature *sig, u32 *keyid ) +keyid_from_sig (PKT_signature *sig, u32 *keyid) { - if( keyid ) { - keyid[0] = sig->keyid[0]; - keyid[1] = sig->keyid[1]; + if( keyid ) + { + keyid[0] = sig->keyid[0]; + keyid[1] = sig->keyid[1]; } - return sig->keyid[1]; + return sig->keyid[1]; } + byte * -namehash_from_uid(PKT_user_id *uid) +namehash_from_uid (PKT_user_id *uid) { if (!uid->namehash) { uid->namehash = xmalloc (20); - if(uid->attrib_data) + if (uid->attrib_data) rmd160_hash_buffer (uid->namehash, uid->attrib_data, uid->attrib_len); else rmd160_hash_buffer (uid->namehash, uid->name, uid->len); @@ -505,117 +426,95 @@ namehash_from_uid(PKT_user_id *uid) return uid->namehash; } -/**************** - * return the number of bits used in the pk - */ -unsigned -nbits_from_pk( PKT_public_key *pk ) -{ - return pubkey_nbits( pk->pubkey_algo, pk->pkey ); -} -/**************** - * return the number of bits used in the sk +/* + * Return the number of bits used in PK. */ -unsigned -nbits_from_sk( PKT_secret_key *sk ) +unsigned int +nbits_from_pk (PKT_public_key *pk) { - return pubkey_nbits( sk->pubkey_algo, sk->skey ); + return pubkey_nbits (pk->pubkey_algo, pk->pkey); } + static const char * mk_datestr (char *buffer, time_t atime) { - struct tm *tp; - - if ( atime < 0 ) /* 32 bit time_t and after 2038-01-19 */ - strcpy (buffer, "????" "-??" "-??"); /* mark this as invalid */ - else { - tp = gmtime (&atime); - sprintf (buffer,"%04d-%02d-%02d", - 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday ); + struct tm *tp; + + if ( atime < 0 ) /* 32 bit time_t and after 2038-01-19 */ + strcpy (buffer, "????" "-??" "-??"); /* mark this as invalid */ + else + { + tp = gmtime (&atime); + sprintf (buffer,"%04d-%02d-%02d", + 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday ); } - return buffer; + return buffer; } -/**************** + +/* * return a string with the creation date of the pk * Note: this is alloced in a static buffer. * Format is: yyyy-mm-dd */ const char * -datestr_from_pk( PKT_public_key *pk ) +datestr_from_pk (PKT_public_key *pk) { - static char buffer[11+5]; - time_t atime = pk->timestamp; - - return mk_datestr (buffer, atime); + static char buffer[11+5]; + time_t atime = pk->timestamp; + + return mk_datestr (buffer, atime); } -const char * -datestr_from_sk( PKT_secret_key *sk ) -{ - static char buffer[11+5]; - time_t atime = sk->timestamp; - - return mk_datestr (buffer, atime); -} const char * -datestr_from_sig( PKT_signature *sig ) +datestr_from_sig (PKT_signature *sig ) { - static char buffer[11+5]; - time_t atime = sig->timestamp; + static char buffer[11+5]; + time_t atime = sig->timestamp; - return mk_datestr (buffer, atime); + return mk_datestr (buffer, atime); } -const char * -expirestr_from_pk( PKT_public_key *pk ) -{ - static char buffer[11+5]; - time_t atime; - - if( !pk->expiredate ) - return _("never "); - atime = pk->expiredate; - return mk_datestr (buffer, atime); -} const char * -expirestr_from_sk( PKT_secret_key *sk ) +expirestr_from_pk (PKT_public_key *pk) { - static char buffer[11+5]; - time_t atime; + static char buffer[11+5]; + time_t atime; - if( !sk->expiredate ) - return _("never "); - atime = sk->expiredate; - return mk_datestr (buffer, atime); + if (!pk->expiredate) + return _("never "); + atime = pk->expiredate; + return mk_datestr (buffer, atime); } + const char * -expirestr_from_sig( PKT_signature *sig ) +expirestr_from_sig (PKT_signature *sig) { - static char buffer[11+5]; - time_t atime; - - if(!sig->expiredate) - return _("never "); - atime=sig->expiredate; - return mk_datestr (buffer, atime); + static char buffer[11+5]; + time_t atime; + + if (!sig->expiredate) + return _("never "); + atime=sig->expiredate; + return mk_datestr (buffer, atime); } + const char * revokestr_from_pk( PKT_public_key *pk ) { - static char buffer[11+5]; - time_t atime; + static char buffer[11+5]; + time_t atime; - if(!pk->revoked.date) - return _("never "); - atime=pk->revoked.date; - return mk_datestr (buffer, atime); + if(!pk->revoked.date) + return _("never "); + atime=pk->revoked.date; + return mk_datestr (buffer, atime); } @@ -666,14 +565,6 @@ colon_datestr_from_pk (PKT_public_key *pk) return buf; } -const char * -colon_datestr_from_sk (PKT_secret_key *sk) -{ - static char buf[20]; - - snprintf (buf, sizeof buf, "%lu", (ulong)sk->timestamp); - return buf; -} const char * colon_datestr_from_sig (PKT_signature *sig) @@ -697,14 +588,13 @@ colon_expirestr_from_sig (PKT_signature *sig) } -/**************** . +/* * Return a byte array with the fingerprint for the given PK/SK * The length of the array is returned in ret_len. Caller must free * the array or provide an array of length MAX_FINGERPRINT_LEN. */ - byte * -fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) +fingerprint_from_pk (PKT_public_key *pk, byte *array, size_t *ret_len) { byte *buf; const byte *dp; @@ -771,106 +661,8 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) return array; } -byte * -fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len ) -{ - byte *buf; - const char *dp; - size_t len, nbytes; - int i; - - if (sk->version < 4) - { - if ( is_RSA(sk->pubkey_algo) ) - { - /* RSA in version 3 packets is special. */ - gcry_md_hd_t md; - - if (gcry_md_open (&md, DIGEST_ALGO_MD5, 0)) - BUG (); - if (pubkey_get_npkey( sk->pubkey_algo ) > 1) - { - for (i=0; i < 2; i++) - { - if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, - &nbytes, sk->skey[i])) - BUG (); - /* fixme: Better allocate BUF on the stack */ - buf = xmalloc (nbytes); - if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, nbytes, - NULL, sk->skey[i])) - BUG (); - gcry_md_write (md, buf, nbytes); - xfree (buf); - } - } - gcry_md_final(md); - if (!array) - array = xmalloc (16); - len = 16; - memcpy (array, gcry_md_read (md, DIGEST_ALGO_MD5), 16); - gcry_md_close (md); - } - else - { - if (!array) - array = xmalloc (16); - len=16; - memset (array,0,16); - } - } - else - { - gcry_md_hd_t md; - - md = do_fingerprint_md_sk(sk); - if (md) - { - dp = gcry_md_read ( md, 0 ); - len = gcry_md_get_algo_dlen ( gcry_md_get_algo (md) ); - assert ( len <= MAX_FINGERPRINT_LEN ); - if (!array) - array = xmalloc( len ); - memcpy (array, dp, len); - gcry_md_close (md); - } - else - { - len = MAX_FINGERPRINT_LEN; - if (!array) - array = xmalloc (len); - memset (array, 0, len); - } - } - - *ret_len = len; - return array; -} -/* Create a serialno/fpr string from the serial number and the secret - key. Caller must free the returned string. There is no error - return. */ -char * -serialno_and_fpr_from_sk (const unsigned char *sn, size_t snlen, - PKT_secret_key *sk) -{ - unsigned char fpr[MAX_FINGERPRINT_LEN]; - size_t fprlen; - char *buffer, *p; - int i; - - fingerprint_from_sk (sk, fpr, &fprlen); - buffer = p = xmalloc (snlen*2 + 1 + fprlen*2 + 1); - for (i=0; i < snlen; i++, p+=2) - sprintf (p, "%02X", sn[i]); - *p++ = '/'; - for (i=0; i < fprlen; i++, p+=2) - sprintf (p, "%02X", fpr[i]); - *p = 0; - return buffer; -} - /* Return the so called KEYGRIP which is the SHA-1 hash of the public |