1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-23 10:29:58 +01:00
gnupg/g10/keyid.c

565 lines
14 KiB
C
Raw Normal View History

1997-11-24 22:24:04 +00:00
/* keyid.c - jeyid and fingerprint handling
* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
1997-11-24 22:24:04 +00:00
*
* This file is part of GnuPG.
1997-11-24 22:24:04 +00:00
*
* GnuPG is free software; you can redistribute it and/or modify
1997-11-24 22:24:04 +00:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
1997-11-24 22:24:04 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
1997-12-01 10:33:23 +00:00
#include <time.h>
1997-11-24 22:24:04 +00:00
#include <assert.h>
#include <gcrypt.h>
1997-11-24 22:24:04 +00:00
#include "util.h"
#include "main.h"
#include "packet.h"
#include "options.h"
#include "keydb.h"
#include "i18n.h"
1997-11-24 22:24:04 +00:00
1997-12-16 19:15:09 +00:00
int
pubkey_letter( int algo )
{
switch( algo ) {
case GCRY_PK_RSA: return 'R' ;
case GCRY_PK_RSA_E: return 'r' ;
case GCRY_PK_RSA_S: return 's' ;
case GCRY_PK_ELG_E: return 'g';
case GCRY_PK_ELG: return 'G' ;
case GCRY_PK_DSA: return 'D' ;
1997-12-16 19:15:09 +00:00
default: return '?';
}
}
1997-11-24 22:24:04 +00:00
1998-01-07 20:47:46 +00:00
static GCRY_MD_HD
1998-06-29 12:30:57 +00:00
do_fingerprint_md( PKT_public_key *pk )
1998-01-07 20:47:46 +00:00
{
GCRY_MD_HD md;
unsigned int n;
unsigned int nn[GNUPG_MAX_NPKEY];
byte *pp[GNUPG_MAX_NPKEY];
1998-06-13 06:59:14 +00:00
int i;
1998-06-29 12:30:57 +00:00
int npkey = pubkey_get_npkey( pk->pubkey_algo );
1998-06-13 06:59:14 +00:00
md = gcry_md_open( pk->version < 4 ? GCRY_MD_RMD160 : GCRY_MD_SHA1, 0);
if( !md )
BUG();
1998-06-29 12:30:57 +00:00
n = pk->version < 4 ? 8 : 6;
1998-06-13 06:59:14 +00:00
for(i=0; i < npkey; i++ ) {
int rc;
size_t nbytes;
rc = gcry_mpi_print( GCRYMPI_FMT_PGP, NULL, &nbytes, pk->pkey[i] );
assert( !rc );
/* fixme: we should try to allocate a buffer on the stack */
pp[i] = gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_PGP, pp[i], &nbytes, pk->pkey[i] );
assert( !rc );
nn[i] = nbytes;
n += nn[i];
1998-06-13 06:59:14 +00:00
}
1998-01-07 20:47:46 +00:00
gcry_md_putc( md, 0x99 ); /* ctb */
gcry_md_putc( md, n >> 8 ); /* 2 byte length header */
gcry_md_putc( md, n );
1998-06-29 12:30:57 +00:00
if( pk->version < 4 )
gcry_md_putc( md, 3 );
1998-06-13 06:59:14 +00:00
else
gcry_md_putc( md, 4 );
1998-06-13 06:59:14 +00:00
1998-06-29 12:30:57 +00:00
{ u32 a = pk->timestamp;
gcry_md_putc( md, a >> 24 );
gcry_md_putc( md, a >> 16 );
gcry_md_putc( md, a >> 8 );
gcry_md_putc( md, a );
1998-01-07 20:47:46 +00:00
}
1998-06-29 12:30:57 +00:00
if( pk->version < 4 ) {
1998-10-16 16:00:17 +00:00
u16 a;
if( pk->expiredate )
a = (u16)((pk->expiredate - pk->timestamp) / 86400L);
else
a = 0;
gcry_md_putc( md, a >> 8 );
gcry_md_putc( md, a );
1998-01-07 20:47:46 +00:00
}
gcry_md_putc( md, pk->pubkey_algo );
1998-06-13 06:59:14 +00:00
for(i=0; i < npkey; i++ ) {
gcry_md_write( md, pp[i], nn[i] );
gcry_free(pp[i]);
1998-03-09 21:44:06 +00:00
}
gcry_md_final( md );
1998-03-09 21:44:06 +00:00
return md;
}
1998-01-07 20:47:46 +00:00
static GCRY_MD_HD
1998-06-29 12:30:57 +00:00
do_fingerprint_md_sk( PKT_secret_key *sk )
1998-01-07 20:47:46 +00:00
{
1998-06-29 12:30:57 +00:00
PKT_public_key pk;
int npkey = pubkey_get_npkey( sk->pubkey_algo ); /* npkey is correct! */
1998-06-13 06:59:14 +00:00
int i;
1998-01-07 20:47:46 +00:00
1998-06-29 12:30:57 +00:00
pk.pubkey_algo = sk->pubkey_algo;
pk.version = sk->version;
pk.timestamp = sk->timestamp;
1998-10-16 16:00:17 +00:00
pk.expiredate = sk->expiredate;
1998-06-29 12:30:57 +00:00
pk.pubkey_algo = sk->pubkey_algo;
1998-06-13 06:59:14 +00:00
for( i=0; i < npkey; i++ )
1998-06-29 12:30:57 +00:00
pk.pkey[i] = sk->skey[i];
return do_fingerprint_md( &pk );
1998-03-09 21:44:06 +00:00
}
1997-11-24 22:24:04 +00:00
static void
v3_keyid( MPI a, u32 *ki )
{
int rc;
byte *buffer;
size_t nbytes;
rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, a );
assert( !rc );
/* fixme: allocate it on the stack */
buffer = gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, buffer, &nbytes, a );
assert( !rc );
if( nbytes < 8 ) { /* oops */
ki[0] = ki[1] = 0;
}
else {
memcpy( ki+0, buffer+nbytes-8, 4);
memcpy( ki+1, buffer+nbytes-4, 4);
}
gcry_free( buffer );
}
1997-11-24 22:24:04 +00:00
/****************
1998-06-29 12:30:57 +00:00
* Get the keyid from the secret key and put it into keyid
1997-11-24 22:24:04 +00:00
* if this is not NULL. Return the 32 low bits of the keyid.
*/
u32
1998-06-29 12:30:57 +00:00
keyid_from_sk( PKT_secret_key *sk, u32 *keyid )
1997-11-24 22:24:04 +00:00
{
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];
}
else if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) {
if( pubkey_get_npkey(sk->pubkey_algo) )
v3_keyid( sk->skey[0], keyid ); /* take n */
else
keyid[0] = keyid[1] = 0;
sk->keyid[0] = keyid[0];
sk->keyid[1] = keyid[1];
1997-11-24 22:24:04 +00:00
}
1998-06-13 06:59:14 +00:00
else {
1998-03-09 21:44:06 +00:00
const byte *dp;
GCRY_MD_HD md;
1998-06-29 12:30:57 +00:00
md = do_fingerprint_md_sk(sk);
dp = gcry_md_read( md, 0 );
1998-03-09 21:44:06 +00:00
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] ;
gcry_md_close(md);
sk->keyid[0] = keyid[0];
sk->keyid[1] = keyid[1];
1998-03-09 21:44:06 +00:00
}
1998-06-13 06:59:14 +00:00
return keyid[1];
1997-11-24 22:24:04 +00:00
}
/****************
1998-06-29 12:30:57 +00:00
* Get the keyid from the public key and put it into keyid
1997-11-24 22:24:04 +00:00
* if this is not NULL. Return the 32 low bits of the keyid.
*/
u32
1998-06-29 12:30:57 +00:00
keyid_from_pk( PKT_public_key *pk, u32 *keyid )
1997-11-24 22:24:04 +00:00
{
u32 dummy_keyid[2];
if( !keyid )
keyid = dummy_keyid;
1998-07-09 13:37:17 +00:00
if( pk->keyid[0] || pk->keyid[1] ) {
keyid[0] = pk->keyid[0];
keyid[1] = pk->keyid[1];
}
else if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) {
if( pubkey_get_npkey(pk->pubkey_algo) )
v3_keyid( pk->pkey[0], keyid ); /* from n */
else
keyid[0] = keyid[1] = 0;
1998-07-09 13:37:17 +00:00
pk->keyid[0] = keyid[0];
pk->keyid[1] = keyid[1];
1997-11-24 22:24:04 +00:00
}
1998-06-13 06:59:14 +00:00
else {
1998-03-09 21:44:06 +00:00
const byte *dp;
GCRY_MD_HD md;
1998-06-29 12:30:57 +00:00
md = do_fingerprint_md(pk);
dp = gcry_md_read( md, 0 );
1998-03-09 21:44:06 +00:00
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] ;
gcry_md_close(md);
1998-07-09 13:37:17 +00:00
pk->keyid[0] = keyid[0];
pk->keyid[1] = keyid[1];
1998-03-09 21:44:06 +00:00
}
1997-11-24 22:24:04 +00:00
return keyid[1];
1997-11-24 22:24:04 +00:00
}
1998-07-21 12:53:38 +00:00
/****************
* 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 );
}
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];
}
1997-12-01 10:33:23 +00:00
u32
keyid_from_sig( PKT_signature *sig, u32 *keyid )
{
if( keyid ) {
keyid[0] = sig->keyid[0];
keyid[1] = sig->keyid[1];
}
return sig->keyid[1];
}
/****************
1998-06-29 12:30:57 +00:00
* return the number of bits used in the pk
1997-12-01 10:33:23 +00:00
*/
unsigned
1998-06-29 12:30:57 +00:00
nbits_from_pk( PKT_public_key *pk )
1997-12-01 10:33:23 +00:00
{
1998-06-29 12:30:57 +00:00
return pubkey_nbits( pk->pubkey_algo, pk->pkey );
1997-12-01 10:33:23 +00:00
}
/****************
1998-06-29 12:30:57 +00:00
* return the number of bits used in the sk
1997-12-01 10:33:23 +00:00
*/
unsigned
1998-06-29 12:30:57 +00:00
nbits_from_sk( PKT_secret_key *sk )
1997-12-01 10:33:23 +00:00
{
1998-06-29 12:30:57 +00:00
return pubkey_nbits( sk->pubkey_algo, sk->skey );
1997-12-01 10:33:23 +00:00
}
/****************
1998-06-29 12:30:57 +00:00
* return a string with the creation date of the pk
1997-12-01 10:33:23 +00:00
* Note: this is alloced in a static buffer.
* Format is: yyyy-mm-dd
*/
const char *
1998-06-29 12:30:57 +00:00
datestr_from_pk( PKT_public_key *pk )
1997-12-01 10:33:23 +00:00
{
static char buffer[11+5];
struct tm *tp;
1998-06-29 12:30:57 +00:00
time_t atime = pk->timestamp;
1997-12-01 10:33:23 +00:00
tp = gmtime( &atime );
1998-01-02 20:40:10 +00:00
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
1997-12-01 10:33:23 +00:00
return buffer;
}
const char *
1998-06-29 12:30:57 +00:00
datestr_from_sk( PKT_secret_key *sk )
1997-12-01 10:33:23 +00:00
{
static char buffer[11+5];
struct tm *tp;
1998-06-29 12:30:57 +00:00
time_t atime = sk->timestamp;
1997-12-01 10:33:23 +00:00
tp = gmtime( &atime );
1998-01-02 20:40:10 +00:00
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
1997-12-01 10:33:23 +00:00
return buffer;
}
const char *
datestr_from_sig( PKT_signature *sig )
{
static char buffer[11+5];
struct tm *tp;
time_t atime = sig->timestamp;
tp = gmtime( &atime );
1998-01-02 20:40:10 +00:00
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
1997-12-01 10:33:23 +00:00
return buffer;
}
1997-12-09 12:46:23 +00:00
1998-07-29 19:35:05 +00:00
const char *
expirestr_from_pk( PKT_public_key *pk )
{
static char buffer[11+5];
struct tm *tp;
time_t atime;
1998-10-16 16:00:17 +00:00
if( !pk->expiredate )
return _("never ");
1998-11-20 17:42:18 +00:00
atime = pk->expiredate;
1998-07-29 19:35:05 +00:00
tp = gmtime( &atime );
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
return buffer;
}
const char *
expirestr_from_sk( PKT_secret_key *sk )
{
static char buffer[11+5];
struct tm *tp;
time_t atime;
1998-10-16 16:00:17 +00:00
if( !sk->expiredate )
return _("never ");
1998-10-16 16:00:17 +00:00
atime = sk->expiredate;
1998-07-29 19:35:05 +00:00
tp = gmtime( &atime );
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
return buffer;
}
1997-12-09 12:46:23 +00:00
/**************** .
1998-06-29 12:30:57 +00:00
* Return a byte array with the fingerprint for the given PK/SK
1997-12-09 12:46:23 +00:00
* The length of the array is returned in ret_len. Caller must free
1998-10-12 20:16:38 +00:00
* the array or provide an array of length MAX_FINGERPRINT_LEN.
1997-12-09 12:46:23 +00:00
*/
1998-01-07 20:47:46 +00:00
1997-12-09 12:46:23 +00:00
byte *
1998-07-14 17:10:28 +00:00
fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len )
1997-12-09 12:46:23 +00:00
{
byte *buf;
1998-01-07 20:47:46 +00:00
const char *dp;
1997-12-09 12:46:23 +00:00
size_t len;
1998-06-29 12:30:57 +00:00
if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) {
1998-06-13 06:59:14 +00:00
/* RSA in version 3 packets is special */
GCRY_MD_HD md;
1998-06-13 06:59:14 +00:00
md = gcry_md_open( GCRY_MD_MD5, 0);
if( !md )
BUG();
if( pubkey_get_npkey( pk->pubkey_algo ) > 1 ) {
int rc;
size_t nbytes;
rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, pk->pkey[0] );
assert( !rc );
/* fixme: allocate it on the stack */
buf = gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, pk->pkey[0] );
assert( !rc );
gcry_md_write( md, buf, nbytes );
gcry_free(buf);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, pk->pkey[1] );
assert( !rc );
/* fixme: allocate it on the stack */
buf = gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, pk->pkey[1] );
assert( !rc );
gcry_md_write( md, buf, nbytes );
gcry_free(buf);
}
gcry_md_final(md);
1998-07-14 17:10:28 +00:00
if( !array )
array = gcry_xmalloc( 16 );
1998-06-13 06:59:14 +00:00
len = 16;
memcpy(array, gcry_md_read(md, GCRY_MD_MD5), 16 );
gcry_md_close(md);
1997-12-09 12:46:23 +00:00
}
1998-06-13 06:59:14 +00:00
else {
GCRY_MD_HD md;
1998-06-29 12:30:57 +00:00
md = do_fingerprint_md(pk);
dp = gcry_md_read( md, 0 );
len = gcry_md_get_algo_dlen( gcry_md_get_algo( md ) );
1998-07-14 17:10:28 +00:00
assert( len <= MAX_FINGERPRINT_LEN );
if( !array )
array = gcry_xmalloc( len );
1998-06-13 06:59:14 +00:00
memcpy(array, dp, len );
gcry_md_close(md);
1998-03-09 21:44:06 +00:00
}
1998-06-13 06:59:14 +00:00
*ret_len = len;
return array;
}
/* Create a unified fingerprint, that is a printable fingerprint along
* wth some other information suitable to passto get_pubkye_byname.
* Pass NULL for buffer to let this function allocate the buffer.
* This function will truncate the buffer in a way that a valid C string
* is returnd (unless bufsize is 0)
* Returns: Supplied buffer or newly allocated buffer
*/
char *
unified_fingerprint_from_pk( PKT_public_key *pk,
char *buffer, size_t bufsize )
{
byte fpr[MAX_FINGERPRINT_LEN];
size_t fprlen;
int i;
fingerprint_from_pk( pk, fpr, &fprlen );
if ( !buffer ) {
bufsize = 1+fprlen*2+1+4+1+1;
buffer = gcry_xmalloc( bufsize );
}
if ( bufsize < 1+fprlen*2+1+4+1+1 ) {
/* Hmmm, that should be sufficiend also not very nice */
if ( bufsize )
*buffer = 0;
return buffer;
}
*buffer = ':';
for (i=0; i < fprlen; i++ )
sprintf( buffer+1+i*2, "%02X", fpr[i] );
sprintf( buffer+1+i*2, ":%d:", (pk->pubkey_algo & 0xff) );
return buffer;
}
1998-06-13 06:59:14 +00:00
byte *
1998-07-14 17:10:28 +00:00
fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len )
1998-06-13 06:59:14 +00:00
{
byte *buf;
1998-06-13 06:59:14 +00:00
const char *dp;
size_t len;
1998-06-29 12:30:57 +00:00
if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) {
1998-06-13 06:59:14 +00:00
/* RSA in version 3 packets is special */
GCRY_MD_HD md;
1997-12-09 12:46:23 +00:00
md = gcry_md_open( GCRY_MD_MD5, 0);
if( !md )
BUG();
if( pubkey_get_npkey( sk->pubkey_algo ) > 1 ) {
int rc;
size_t nbytes;
/* FIXME: Why is the hash sequence for secret keys different */
rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, sk->skey[1] );
assert( !rc );
/* fixme: allocate it on the stack */
buf = gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, sk->skey[1] );
assert( !rc );
gcry_md_write( md, buf, nbytes );
gcry_free(buf);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, sk->skey[0] );
assert( !rc );
/* fixme: allocate it on the stack */
buf = gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, sk->skey[0] );
assert( !rc );
gcry_md_write( md, buf, nbytes );
gcry_free(buf);
}
gcry_md_final(md);
1998-07-14 17:10:28 +00:00
if( !array )
array = gcry_xmalloc( 16 );
1997-12-09 12:46:23 +00:00
len = 16;
memcpy(array, gcry_md_read(md, GCRY_MD_MD5), 16 );
gcry_md_close(md);
1997-12-09 12:46:23 +00:00
}
else {
GCRY_MD_HD md;
1998-06-29 12:30:57 +00:00
md = do_fingerprint_md_sk(sk);
dp = gcry_md_read( md, 0 );
len = gcry_md_get_algo_dlen( gcry_md_get_algo( md ) );
1998-07-14 17:10:28 +00:00
assert( len <= MAX_FINGERPRINT_LEN );
if( !array )
array = gcry_xmalloc( len );
1998-06-13 06:59:14 +00:00
memcpy(array, dp, len );
gcry_md_close(md);
1997-12-09 12:46:23 +00:00
}
*ret_len = len;
return array;
}
char *
unified_fingerprint_from_sk( PKT_secret_key *sk,
char *buffer, size_t bufsize )
{
byte fpr[MAX_FINGERPRINT_LEN];
size_t fprlen;
int i;
fingerprint_from_sk( sk, fpr, &fprlen );
if ( !buffer ) {
bufsize = 1+fprlen*2+1+4+1+1;
buffer = gcry_xmalloc( bufsize );
}
if ( bufsize < 1+fprlen*2+1+4+1+1 ) {
/* Hmmm, that should be sufficiend also not very nice */
if ( bufsize )
*buffer = 0;
return buffer;
}
*buffer = ':';
for (i=0; i < fprlen; i++ )
sprintf( buffer+1+i*2, "%02X", fpr[i] );
sprintf( buffer+1+i*2, ":%d:", (sk->pubkey_algo & 0xff) );
return buffer;
}
1997-12-09 12:46:23 +00:00