1997-11-24 23:24:04 +01:00
|
|
|
/* keyid.c - jeyid and fingerprint handling
|
1998-02-24 19:50:46 +01:00
|
|
|
* Copyright (C) 1998 Free Software Foundation, Inc.
|
1997-11-24 23:24:04 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* This file is part of GNUPG.
|
1997-11-24 23:24:04 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* GNUPG is free software; you can redistribute it and/or modify
|
1997-11-24 23:24:04 +01: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.
|
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* GNUPG is distributed in the hope that it will be useful,
|
1997-11-24 23:24:04 +01: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 11:33:23 +01:00
|
|
|
#include <time.h>
|
1997-11-24 23:24:04 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "mpi.h"
|
|
|
|
#include "keydb.h"
|
|
|
|
|
|
|
|
|
1997-12-16 20:15:09 +01:00
|
|
|
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' ;
|
1998-05-04 20:49:26 +02:00
|
|
|
case PUBKEY_ALGO_ELGAMAL_E:
|
1997-12-16 20:15:09 +01:00
|
|
|
case PUBKEY_ALGO_ELGAMAL: return 'G' ;
|
|
|
|
case PUBKEY_ALGO_DSA: return 'D' ;
|
|
|
|
default: return '?';
|
|
|
|
}
|
|
|
|
}
|
1997-11-24 23:24:04 +01:00
|
|
|
|
1998-01-07 21:47:46 +01:00
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
static MD_HANDLE
|
1998-06-29 14:30:57 +02:00
|
|
|
do_fingerprint_md( PKT_public_key *pk )
|
1998-01-07 21:47:46 +01:00
|
|
|
{
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE md;
|
1998-01-07 21:47:46 +01:00
|
|
|
unsigned n;
|
1998-06-13 08:59:14 +02:00
|
|
|
unsigned nb[PUBKEY_MAX_NPKEY];
|
|
|
|
unsigned nn[PUBKEY_MAX_NPKEY];
|
|
|
|
byte *pp[PUBKEY_MAX_NPKEY];
|
|
|
|
int i;
|
1998-06-29 14:30:57 +02:00
|
|
|
int npkey = pubkey_get_npkey( pk->pubkey_algo );
|
1998-06-13 08:59:14 +02:00
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
md = md_open( pk->version < 4 ? DIGEST_ALGO_RMD160 : DIGEST_ALGO_SHA1, 0);
|
|
|
|
n = pk->version < 4 ? 8 : 6;
|
1998-06-13 08:59:14 +02:00
|
|
|
for(i=0; i < npkey; i++ ) {
|
1998-06-29 14:30:57 +02:00
|
|
|
nb[i] = mpi_get_nbits(pk->pkey[i]);
|
|
|
|
pp[i] = mpi_get_buffer( pk->pkey[i], nn+i, NULL );
|
1998-06-13 08:59:14 +02:00
|
|
|
n += 2 + nn[i];
|
|
|
|
}
|
1998-01-07 21:47:46 +01:00
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
md_putc( md, 0x99 ); /* ctb */
|
|
|
|
md_putc( md, n >> 8 ); /* 2 byte length header */
|
|
|
|
md_putc( md, n );
|
1998-06-29 14:30:57 +02:00
|
|
|
if( pk->version < 4 )
|
1998-06-13 08:59:14 +02:00
|
|
|
md_putc( md, 3 );
|
|
|
|
else
|
|
|
|
md_putc( md, 4 );
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
{ u32 a = pk->timestamp;
|
1998-01-12 11:18:17 +01:00
|
|
|
md_putc( md, a >> 24 );
|
|
|
|
md_putc( md, a >> 16 );
|
|
|
|
md_putc( md, a >> 8 );
|
|
|
|
md_putc( md, a );
|
1998-01-07 21:47:46 +01:00
|
|
|
}
|
1998-06-29 14:30:57 +02:00
|
|
|
if( pk->version < 4 ) {
|
|
|
|
u16 a = pk->valid_days;
|
1998-01-12 11:18:17 +01:00
|
|
|
md_putc( md, a >> 8 );
|
|
|
|
md_putc( md, a );
|
1998-01-07 21:47:46 +01:00
|
|
|
}
|
1998-06-29 14:30:57 +02:00
|
|
|
md_putc( md, pk->pubkey_algo );
|
1998-06-13 08:59:14 +02:00
|
|
|
for(i=0; i < npkey; i++ ) {
|
|
|
|
md_putc( md, nb[i]>>8);
|
|
|
|
md_putc( md, nb[i] );
|
|
|
|
md_write( md, pp[i], nn[i] );
|
|
|
|
m_free(pp[i]);
|
1998-03-09 22:44:06 +01:00
|
|
|
}
|
|
|
|
md_final( md );
|
|
|
|
|
|
|
|
return md;
|
|
|
|
}
|
1998-01-07 21:47:46 +01:00
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
static MD_HANDLE
|
1998-06-29 14:30:57 +02:00
|
|
|
do_fingerprint_md_sk( PKT_secret_key *sk )
|
1998-01-07 21:47:46 +01:00
|
|
|
{
|
1998-06-29 14:30:57 +02:00
|
|
|
PKT_public_key pk;
|
|
|
|
int npkey = pubkey_get_npkey( sk->pubkey_algo ); /* npkey is correct! */
|
1998-06-13 08:59:14 +02:00
|
|
|
int i;
|
1998-01-07 21:47:46 +01:00
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
pk.pubkey_algo = sk->pubkey_algo;
|
|
|
|
pk.version = sk->version;
|
|
|
|
pk.timestamp = sk->timestamp;
|
|
|
|
pk.valid_days = sk->valid_days;
|
|
|
|
pk.pubkey_algo = sk->pubkey_algo;
|
1998-06-13 08:59:14 +02:00
|
|
|
for( i=0; i < npkey; i++ )
|
1998-06-29 14:30:57 +02:00
|
|
|
pk.pkey[i] = sk->skey[i];
|
|
|
|
return do_fingerprint_md( &pk );
|
1998-03-09 22:44:06 +01:00
|
|
|
}
|
|
|
|
|
1997-11-24 23:24:04 +01:00
|
|
|
|
|
|
|
/****************
|
1998-06-29 14:30:57 +02:00
|
|
|
* Get the keyid from the secret key and put it into keyid
|
1997-11-24 23:24:04 +01:00
|
|
|
* if this is not NULL. Return the 32 low bits of the keyid.
|
|
|
|
*/
|
|
|
|
u32
|
1998-06-29 14:30:57 +02:00
|
|
|
keyid_from_sk( PKT_secret_key *sk, u32 *keyid )
|
1997-11-24 23:24:04 +01:00
|
|
|
{
|
|
|
|
u32 lowbits;
|
|
|
|
u32 dummy_keyid[2];
|
|
|
|
|
|
|
|
if( !keyid )
|
|
|
|
keyid = dummy_keyid;
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) {
|
1998-07-02 21:31:46 +02:00
|
|
|
lowbits = pubkey_get_npkey(sk->pubkey_algo) ?
|
|
|
|
mpi_get_keyid( sk->skey[0], keyid ) : 0; /* take n */
|
1997-11-24 23:24:04 +01:00
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
else {
|
1998-03-09 22:44:06 +01:00
|
|
|
const byte *dp;
|
|
|
|
MD_HANDLE md;
|
1998-06-29 14:30:57 +02:00
|
|
|
md = do_fingerprint_md_sk(sk);
|
1998-06-13 08:59:14 +02:00
|
|
|
dp = md_read( md, 0 );
|
1998-03-09 22:44:06 +01: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] ;
|
|
|
|
lowbits = keyid[1];
|
|
|
|
md_close(md);
|
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
|
1997-11-24 23:24:04 +01:00
|
|
|
return lowbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
1998-06-29 14:30:57 +02:00
|
|
|
* Get the keyid from the public key and put it into keyid
|
1997-11-24 23:24:04 +01:00
|
|
|
* if this is not NULL. Return the 32 low bits of the keyid.
|
|
|
|
*/
|
|
|
|
u32
|
1998-06-29 14:30:57 +02:00
|
|
|
keyid_from_pk( PKT_public_key *pk, u32 *keyid )
|
1997-11-24 23:24:04 +01:00
|
|
|
{
|
|
|
|
u32 lowbits;
|
|
|
|
u32 dummy_keyid[2];
|
|
|
|
|
|
|
|
if( !keyid )
|
|
|
|
keyid = dummy_keyid;
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) {
|
1998-07-02 21:31:46 +02:00
|
|
|
lowbits = pubkey_get_npkey(pk->pubkey_algo) ?
|
|
|
|
mpi_get_keyid( pk->pkey[0], keyid ) : 0 ; /* from n */
|
1997-11-24 23:24:04 +01:00
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
else {
|
1998-03-09 22:44:06 +01:00
|
|
|
const byte *dp;
|
|
|
|
MD_HANDLE md;
|
1998-06-29 14:30:57 +02:00
|
|
|
md = do_fingerprint_md(pk);
|
1998-06-13 08:59:14 +02:00
|
|
|
dp = md_read( md, 0 );
|
1998-03-09 22:44:06 +01: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] ;
|
|
|
|
lowbits = keyid[1];
|
|
|
|
md_close(md);
|
|
|
|
}
|
1997-11-24 23:24:04 +01:00
|
|
|
|
|
|
|
return lowbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-01 11:33:23 +01: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 14:30:57 +02:00
|
|
|
* return the number of bits used in the pk
|
1997-12-01 11:33:23 +01:00
|
|
|
*/
|
|
|
|
unsigned
|
1998-06-29 14:30:57 +02:00
|
|
|
nbits_from_pk( PKT_public_key *pk )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-06-29 14:30:57 +02:00
|
|
|
return pubkey_nbits( pk->pubkey_algo, pk->pkey );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
1998-06-29 14:30:57 +02:00
|
|
|
* return the number of bits used in the sk
|
1997-12-01 11:33:23 +01:00
|
|
|
*/
|
|
|
|
unsigned
|
1998-06-29 14:30:57 +02:00
|
|
|
nbits_from_sk( PKT_secret_key *sk )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-06-29 14:30:57 +02:00
|
|
|
return pubkey_nbits( sk->pubkey_algo, sk->skey );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
1998-06-29 14:30:57 +02:00
|
|
|
* return a string with the creation date of the pk
|
1997-12-01 11:33:23 +01:00
|
|
|
* Note: this is alloced in a static buffer.
|
|
|
|
* Format is: yyyy-mm-dd
|
|
|
|
*/
|
|
|
|
const char *
|
1998-06-29 14:30:57 +02:00
|
|
|
datestr_from_pk( PKT_public_key *pk )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
|
|
|
static char buffer[11+5];
|
|
|
|
struct tm *tp;
|
1998-06-29 14:30:57 +02:00
|
|
|
time_t atime = pk->timestamp;
|
1997-12-01 11:33:23 +01:00
|
|
|
|
|
|
|
tp = gmtime( &atime );
|
1998-01-02 21:40:10 +01:00
|
|
|
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
|
1997-12-01 11:33:23 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
1998-06-29 14:30:57 +02:00
|
|
|
datestr_from_sk( PKT_secret_key *sk )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
|
|
|
static char buffer[11+5];
|
|
|
|
struct tm *tp;
|
1998-06-29 14:30:57 +02:00
|
|
|
time_t atime = sk->timestamp;
|
1997-12-01 11:33:23 +01:00
|
|
|
|
|
|
|
tp = gmtime( &atime );
|
1998-01-02 21:40:10 +01:00
|
|
|
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
|
1997-12-01 11:33:23 +01: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 21:40:10 +01:00
|
|
|
sprintf(buffer,"%04d-%02d-%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
|
1997-12-01 11:33:23 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
1997-12-09 13:46:23 +01:00
|
|
|
|
|
|
|
/**************** .
|
1998-06-29 14:30:57 +02:00
|
|
|
* Return a byte array with the fingerprint for the given PK/SK
|
1997-12-09 13:46:23 +01:00
|
|
|
* The length of the array is returned in ret_len. Caller must free
|
|
|
|
* the array.
|
|
|
|
*/
|
1998-01-07 21:47:46 +01:00
|
|
|
|
1997-12-09 13:46:23 +01:00
|
|
|
byte *
|
1998-06-29 14:30:57 +02:00
|
|
|
fingerprint_from_pk( PKT_public_key *pk, size_t *ret_len )
|
1997-12-09 13:46:23 +01:00
|
|
|
{
|
|
|
|
byte *p, *buf, *array;
|
1998-01-07 21:47:46 +01:00
|
|
|
const char *dp;
|
1997-12-09 13:46:23 +01:00
|
|
|
size_t len;
|
|
|
|
unsigned n;
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) {
|
1998-06-13 08:59:14 +02:00
|
|
|
/* RSA in version 3 packets is special */
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE md;
|
1998-06-13 08:59:14 +02:00
|
|
|
|
|
|
|
md = md_open( DIGEST_ALGO_MD5, 0);
|
1998-07-02 21:31:46 +02:00
|
|
|
if( pubkey_get_npkey( pk->pubkey_algo ) > 1 ) {
|
|
|
|
p = buf = mpi_get_buffer( pk->pkey[0], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
|
|
|
m_free(buf);
|
|
|
|
p = buf = mpi_get_buffer( pk->pkey[1], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
|
|
|
m_free(buf);
|
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
md_final(md);
|
|
|
|
array = m_alloc( 16 );
|
|
|
|
len = 16;
|
|
|
|
memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 );
|
1998-01-12 11:18:17 +01:00
|
|
|
md_close(md);
|
1997-12-09 13:46:23 +01:00
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
else {
|
1998-03-09 22:44:06 +01:00
|
|
|
MD_HANDLE md;
|
1998-06-29 14:30:57 +02:00
|
|
|
md = do_fingerprint_md(pk);
|
1998-06-13 08:59:14 +02:00
|
|
|
dp = md_read( md, 0 );
|
|
|
|
len = md_digest_length( md_get_algo( md ) );
|
|
|
|
array = m_alloc( len );
|
|
|
|
memcpy(array, dp, len );
|
1998-03-09 22:44:06 +01:00
|
|
|
md_close(md);
|
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
|
|
|
|
*ret_len = len;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte *
|
1998-06-29 14:30:57 +02:00
|
|
|
fingerprint_from_sk( PKT_secret_key *sk, size_t *ret_len )
|
1998-06-13 08:59:14 +02:00
|
|
|
{
|
|
|
|
byte *p, *buf, *array;
|
|
|
|
const char *dp;
|
|
|
|
size_t len;
|
|
|
|
unsigned n;
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) {
|
1998-06-13 08:59:14 +02:00
|
|
|
/* RSA in version 3 packets is special */
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE md;
|
1997-12-09 13:46:23 +01:00
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
md = md_open( DIGEST_ALGO_MD5, 0);
|
1998-07-02 21:31:46 +02:00
|
|
|
if( pubkey_get_npkey( sk->pubkey_algo ) > 1 ) {
|
|
|
|
p = buf = mpi_get_buffer( sk->skey[1], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
|
|
|
m_free(buf);
|
|
|
|
p = buf = mpi_get_buffer( sk->skey[0], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
|
|
|
m_free(buf);
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
md_final(md);
|
1997-12-09 13:46:23 +01:00
|
|
|
array = m_alloc( 16 );
|
|
|
|
len = 16;
|
1998-01-12 11:18:17 +01:00
|
|
|
memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 );
|
|
|
|
md_close(md);
|
1997-12-09 13:46:23 +01:00
|
|
|
}
|
|
|
|
else {
|
1998-06-13 08:59:14 +02:00
|
|
|
MD_HANDLE md;
|
1998-06-29 14:30:57 +02:00
|
|
|
md = do_fingerprint_md_sk(sk);
|
1998-06-13 08:59:14 +02:00
|
|
|
dp = md_read( md, 0 );
|
|
|
|
len = md_digest_length( md_get_algo( md ) );
|
|
|
|
array = m_alloc( len );
|
|
|
|
memcpy(array, dp, len );
|
|
|
|
md_close(md);
|
1997-12-09 13:46:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*ret_len = len;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|