1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-04 20:38:50 +01:00
gnupg/g10/keyid.c

515 lines
13 KiB
C
Raw Normal View History

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' ;
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
/* this is special code for V3 which uses ElGamal and
* calculates a fingerprint like V4, but with rmd160
1998-01-12 11:18:17 +01:00
* and a version byte of 3. Returns an md handle, caller must
* do md_close()
1998-01-07 21:47:46 +01:00
*/
1998-01-12 11:18:17 +01:00
static MD_HANDLE
1998-01-07 21:47:46 +01:00
v3_elg_fingerprint_md( PKT_public_cert *pkc )
{
1998-01-12 11:18:17 +01:00
MD_HANDLE md;
1998-01-07 21:47:46 +01:00
byte *buf1, *buf2, *buf3;
byte *p1, *p2, *p3;
unsigned n1, n2, n3;
1998-02-09 18:43:42 +01:00
unsigned nb1, nb2, nb3;
1998-01-07 21:47:46 +01:00
unsigned n;
1998-02-09 18:43:42 +01:00
nb1 = mpi_get_nbits(pkc->d.elg.p);
1998-01-07 21:47:46 +01:00
p1 = buf1 = mpi_get_buffer( pkc->d.elg.p, &n1, NULL );
1998-02-09 18:43:42 +01:00
nb2 = mpi_get_nbits(pkc->d.elg.g);
1998-01-07 21:47:46 +01:00
p2 = buf2 = mpi_get_buffer( pkc->d.elg.g, &n2, NULL );
1998-02-09 18:43:42 +01:00
nb3 = mpi_get_nbits(pkc->d.elg.y);
1998-01-07 21:47:46 +01:00
p3 = buf3 = mpi_get_buffer( pkc->d.elg.y, &n3, NULL );
/* calculate length of packet (1+4+2+1+2+n1+2+n2+2+n3) */
n = 14 + n1 + n2 + n3;
1998-01-12 11:18:17 +01:00
md = md_open( DIGEST_ALGO_RMD160, 0);
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 );
md_putc( md, 3 ); /* version */
1998-01-07 21:47:46 +01:00
{ u32 a = pkc->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
}
{ u16 a = pkc->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-01-12 11:18:17 +01:00
md_putc( md, pkc->pubkey_algo );
1998-02-09 18:43:42 +01:00
md_putc( md, nb1>>8); md_putc( md, nb1 ); md_write( md, p1, n1 );
md_putc( md, nb2>>8); md_putc( md, nb2 ); md_write( md, p2, n2 );
md_putc( md, nb3>>8); md_putc( md, nb3 ); md_write( md, p3, n3 );
1998-01-07 21:47:46 +01:00
m_free(buf1);
m_free(buf2);
m_free(buf3);
1998-01-12 11:18:17 +01:00
md_final( md );
1998-01-07 21:47:46 +01:00
return md;
}
1998-03-19 16:27:29 +01:00
static MD_HANDLE
elg_fingerprint_md( PKT_public_cert *pkc )
{
MD_HANDLE md;
byte *buf1, *buf3, *buf4 ;
byte *p1, *p3, *p4;
unsigned n1, n3, n4;
unsigned nb1, nb3, nb4;
unsigned n;
1998-04-02 12:30:03 +02:00
nb1 = mpi_get_nbits(pkc->d.elg.p);
p1 = buf1 = mpi_get_buffer( pkc->d.elg.p, &n1, NULL );
nb3 = mpi_get_nbits(pkc->d.elg.g);
p3 = buf3 = mpi_get_buffer( pkc->d.elg.g, &n3, NULL );
nb4 = mpi_get_nbits(pkc->d.elg.y);
p4 = buf4 = mpi_get_buffer( pkc->d.elg.y, &n4, NULL );
1998-03-19 16:27:29 +01:00
/* calculate length of packet */
n = 12 + n1 + n3 +n4 ;
md = md_open( DIGEST_ALGO_SHA1, 0);
md_putc( md, 0x99 ); /* ctb */
md_putc( md, n >> 8 ); /* 2 byte length header */
md_putc( md, n );
md_putc( md, 4 ); /* version */
{ u32 a = pkc->timestamp;
md_putc( md, a >> 24 );
md_putc( md, a >> 16 );
md_putc( md, a >> 8 );
md_putc( md, a );
}
md_putc( md, pkc->pubkey_algo );
md_putc( md, nb1>>8); md_putc( md, nb1 ); md_write( md, p1, n1 );
md_putc( md, nb3>>8); md_putc( md, nb3 ); md_write( md, p3, n3 );
md_putc( md, nb4>>8); md_putc( md, nb4 ); md_write( md, p4, n4 );
m_free(buf1);
m_free(buf3);
m_free(buf4);
md_final( md );
return md;
}
1998-03-09 22:44:06 +01:00
static MD_HANDLE
dsa_fingerprint_md( PKT_public_cert *pkc )
{
MD_HANDLE md;
byte *buf1, *buf2, *buf3, *buf4 ;
byte *p1, *p2, *p3, *p4;
unsigned n1, n2, n3, n4;
unsigned nb1, nb2, nb3, nb4;
unsigned n;
nb1 = mpi_get_nbits(pkc->d.dsa.p);
p1 = buf1 = mpi_get_buffer( pkc->d.dsa.p, &n1, NULL );
nb2 = mpi_get_nbits(pkc->d.dsa.q);
p2 = buf2 = mpi_get_buffer( pkc->d.dsa.q, &n2, NULL );
nb3 = mpi_get_nbits(pkc->d.dsa.g);
p3 = buf3 = mpi_get_buffer( pkc->d.dsa.g, &n3, NULL );
nb4 = mpi_get_nbits(pkc->d.dsa.y);
p4 = buf4 = mpi_get_buffer( pkc->d.dsa.y, &n4, NULL );
/* calculate length of packet */
n = 14 + n1 + n2 + n3 +n4 ;
md = md_open( DIGEST_ALGO_SHA1, 0);
md_putc( md, 0x99 ); /* ctb */
md_putc( md, n >> 8 ); /* 2 byte length header */
md_putc( md, n );
md_putc( md, 4 ); /* version */
{ u32 a = pkc->timestamp;
md_putc( md, a >> 24 );
md_putc( md, a >> 16 );
md_putc( md, a >> 8 );
md_putc( md, a );
}
md_putc( md, pkc->pubkey_algo );
md_putc( md, nb1>>8); md_putc( md, nb1 ); md_write( md, p1, n1 );
md_putc( md, nb2>>8); md_putc( md, nb2 ); md_write( md, p2, n2 );
md_putc( md, nb3>>8); md_putc( md, nb3 ); md_write( md, p3, n3 );
md_putc( md, nb4>>8); md_putc( md, nb4 ); md_write( md, p4, n4 );
m_free(buf1);
m_free(buf2);
m_free(buf3);
m_free(buf4);
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-03-19 16:27:29 +01:00
elg_fingerprint_md_skc( PKT_secret_cert *skc )
1998-01-07 21:47:46 +01:00
{
PKT_public_cert pkc;
pkc.pubkey_algo = skc->pubkey_algo;
1998-03-19 16:27:29 +01:00
pkc.version = skc->version;
1998-01-07 21:47:46 +01:00
pkc.timestamp = skc->timestamp;
pkc.valid_days = skc->valid_days;
pkc.pubkey_algo = skc->pubkey_algo;
pkc.d.elg.p = skc->d.elg.p;
pkc.d.elg.g = skc->d.elg.g;
pkc.d.elg.y = skc->d.elg.y;
1998-03-19 16:27:29 +01:00
if( pkc.version < 4 )
return v3_elg_fingerprint_md( &pkc );
else
return elg_fingerprint_md( &pkc );
1998-01-07 21:47:46 +01:00
}
1998-03-09 22:44:06 +01:00
static MD_HANDLE
dsa_fingerprint_md_skc( PKT_secret_cert *skc )
{
PKT_public_cert pkc;
pkc.pubkey_algo = skc->pubkey_algo;
pkc.timestamp = skc->timestamp;
pkc.pubkey_algo = skc->pubkey_algo;
pkc.d.dsa.p = skc->d.dsa.p;
pkc.d.dsa.q = skc->d.dsa.q;
pkc.d.dsa.g = skc->d.dsa.g;
pkc.d.dsa.y = skc->d.dsa.y;
return dsa_fingerprint_md( &pkc );
}
1997-11-24 23:24:04 +01:00
/****************
* Get the keyid from the secret key certificate and put it into keyid
* if this is not NULL. Return the 32 low bits of the keyid.
*/
u32
1997-12-01 11:33:23 +01:00
keyid_from_skc( PKT_secret_cert *skc, u32 *keyid )
1997-11-24 23:24:04 +01:00
{
u32 lowbits;
u32 dummy_keyid[2];
if( !keyid )
keyid = dummy_keyid;
if( skc->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
1998-01-07 21:47:46 +01:00
const byte *dp;
1998-01-12 11:18:17 +01:00
MD_HANDLE md;
1998-03-19 16:27:29 +01:00
md = elg_fingerprint_md_skc(skc);
if( skc->version < 4 )
dp = md_read( md, DIGEST_ALGO_RMD160 );
else
dp = md_read( md, DIGEST_ALGO_SHA1 );
1998-01-07 21:47:46 +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];
1998-01-12 11:18:17 +01:00
md_close(md);
1997-11-24 23:24:04 +01:00
}
1998-03-09 22:44:06 +01:00
else if( skc->pubkey_algo == PUBKEY_ALGO_DSA ) {
const byte *dp;
MD_HANDLE md;
md = dsa_fingerprint_md_skc(skc);
dp = md_read( md, DIGEST_ALGO_SHA1 );
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
else if( skc->pubkey_algo == PUBKEY_ALGO_RSA ) {
1998-04-08 21:49:02 +02:00
lowbits = mpi_get_keyid( skc->d.rsa.n, keyid );
1997-11-24 23:24:04 +01:00
}
1997-12-01 11:33:23 +01:00
else {
keyid[0] = keyid[1] = lowbits = 0;
}
1997-11-24 23:24:04 +01:00
return lowbits;
}
/****************
* Get the keyid from the public key certificate and put it into keyid
* if this is not NULL. Return the 32 low bits of the keyid.
*/
u32
1997-12-01 11:33:23 +01:00
keyid_from_pkc( PKT_public_cert *pkc, u32 *keyid )
1997-11-24 23:24:04 +01:00
{
u32 lowbits;
u32 dummy_keyid[2];
if( !keyid )
keyid = dummy_keyid;
if( pkc->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
1998-01-07 21:47:46 +01:00
const byte *dp;
1998-01-12 11:18:17 +01:00
MD_HANDLE md;
1998-03-19 16:27:29 +01:00
if( pkc->version < 4 ) {
md = v3_elg_fingerprint_md(pkc);
dp = md_read( md, DIGEST_ALGO_RMD160 );
}
else {
md = elg_fingerprint_md(pkc);
dp = md_read( md, DIGEST_ALGO_SHA1 );
}
1998-01-07 21:47:46 +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];
1998-01-12 11:18:17 +01:00
md_close(md);
1997-11-24 23:24:04 +01:00
}
1998-03-19 16:27:29 +01:00
else if( pkc->pubkey_algo == PUBKEY_ALGO_DSA ) {
1998-03-09 22:44:06 +01:00
const byte *dp;
MD_HANDLE md;
md = dsa_fingerprint_md(pkc);
dp = md_read( md, DIGEST_ALGO_SHA1 );
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
else if( pkc->pubkey_algo == PUBKEY_ALGO_RSA ) {
1998-04-08 21:49:02 +02:00
lowbits = mpi_get_keyid( pkc->d.rsa.n, keyid );
1997-11-24 23:24:04 +01:00
}
1997-12-01 11:33:23 +01:00
else {
keyid[0] = keyid[1] = lowbits = 0;
}
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];
}
/****************
* return the number of bits used in the pkc
*/
unsigned
nbits_from_pkc( PKT_public_cert *pkc )
{
if( pkc->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
return mpi_get_nbits( pkc->d.elg.p );
}
1998-03-09 22:44:06 +01:00
else if( pkc->pubkey_algo == PUBKEY_ALGO_DSA ) {
return mpi_get_nbits( pkc->d.dsa.p );
}
1997-12-01 11:33:23 +01:00
else if( pkc->pubkey_algo == PUBKEY_ALGO_RSA ) {
1998-04-08 21:49:02 +02:00
return mpi_get_nbits( pkc->d.rsa.n );
1997-12-01 11:33:23 +01:00
}
else
return 0;
}
/****************
* return the number of bits used in the skc
*/
unsigned
nbits_from_skc( PKT_secret_cert *skc )
{
if( skc->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
return mpi_get_nbits( skc->d.elg.p );
}
1998-03-09 22:44:06 +01:00
else if( skc->pubkey_algo == PUBKEY_ALGO_DSA ) {
return mpi_get_nbits( skc->d.dsa.p );
}
1997-12-01 11:33:23 +01:00
else if( skc->pubkey_algo == PUBKEY_ALGO_RSA ) {
1998-04-08 21:49:02 +02:00
return mpi_get_nbits( skc->d.rsa.n );
1997-12-01 11:33:23 +01:00
}
else
return 0;
}
/****************
* return a string with the creation date of the pkc
* Note: this is alloced in a static buffer.
* Format is: yyyy-mm-dd
*/
const char *
datestr_from_pkc( PKT_public_cert *pkc )
{
static char buffer[11+5];
struct tm *tp;
time_t atime = pkc->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;
}
const char *
datestr_from_skc( PKT_secret_cert *skc )
{
static char buffer[11+5];
struct tm *tp;
time_t atime = skc->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;
}
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
/**************** .
* Return a byte array with the fingerprint for the given PKC/SKC
* The length of the array is returned in ret_len. Caller must free
* the array.
*/
byte *
fingerprint_from_skc( PKT_secret_cert *skc, size_t *ret_len )
{
PKT_public_cert pkc;
byte *p;
pkc.pubkey_algo = skc->pubkey_algo;
1998-03-19 16:27:29 +01:00
pkc.version = skc->version;
1997-12-09 13:46:23 +01:00
if( pkc.pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
pkc.timestamp = skc->timestamp;
pkc.valid_days = skc->valid_days;
pkc.pubkey_algo = skc->pubkey_algo;
pkc.d.elg.p = skc->d.elg.p;
pkc.d.elg.g = skc->d.elg.g;
pkc.d.elg.y = skc->d.elg.y;
}
1998-03-09 22:44:06 +01:00
else if( pkc.pubkey_algo == PUBKEY_ALGO_DSA ) {
pkc.timestamp = skc->timestamp;
pkc.valid_days = skc->valid_days;
pkc.pubkey_algo = skc->pubkey_algo;
pkc.d.dsa.p = skc->d.dsa.p;
pkc.d.dsa.q = skc->d.dsa.q;
pkc.d.dsa.g = skc->d.dsa.g;
pkc.d.dsa.y = skc->d.dsa.y;
}
1997-12-09 13:46:23 +01:00
else if( pkc.pubkey_algo == PUBKEY_ALGO_RSA ) {
1998-04-08 21:49:02 +02:00
pkc.d.rsa.n = skc->d.rsa.n;
pkc.d.rsa.e = skc->d.rsa.e;
1997-12-09 13:46:23 +01:00
}
p = fingerprint_from_pkc( &pkc, ret_len );
memset(&pkc, 0, sizeof pkc); /* not really needed */
return p;
}
1998-01-07 21:47:46 +01:00
1997-12-09 13:46:23 +01:00
byte *
fingerprint_from_pkc( PKT_public_cert *pkc, size_t *ret_len )
{
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;
if( pkc->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
1998-01-12 11:18:17 +01:00
MD_HANDLE md;
1998-03-19 16:27:29 +01:00
if( pkc->version < 4 ) {
md = v3_elg_fingerprint_md(pkc);
dp = md_read( md, DIGEST_ALGO_RMD160 );
}
else {
md = elg_fingerprint_md(pkc);
dp = md_read( md, DIGEST_ALGO_SHA1 );
}
1997-12-09 13:46:23 +01:00
array = m_alloc( 20 );
len = 20;
memcpy(array, dp, 20 );
1998-01-12 11:18:17 +01:00
md_close(md);
1997-12-09 13:46:23 +01:00
}
1998-03-09 22:44:06 +01:00
else if( pkc->pubkey_algo == PUBKEY_ALGO_DSA ) {
MD_HANDLE md;
md = dsa_fingerprint_md(pkc);
dp = md_read( md, DIGEST_ALGO_SHA1 );
array = m_alloc( 20 );
len = 20;
memcpy(array, dp, 20 );
md_close(md);
}
1997-12-09 13:46:23 +01:00
else if( pkc->pubkey_algo == PUBKEY_ALGO_RSA ) {
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-04-08 21:49:02 +02:00
p = buf = mpi_get_buffer( pkc->d.rsa.n, &n, NULL );
1998-01-12 11:18:17 +01:00
md_write( md, p, n );
1997-12-09 13:46:23 +01:00
m_free(buf);
1998-04-08 21:49:02 +02:00
p = buf = mpi_get_buffer( pkc->d.rsa.e, &n, NULL );
1998-01-12 11:18:17 +01:00
md_write( md, p, n );
1997-12-09 13:46:23 +01:00
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 {
array = m_alloc(1);
len = 0; /* ooops */
}
*ret_len = len;
return array;
}