2002-06-29 13:46:34 +00:00
|
|
|
/* keyid.c - key ID and fingerprint handling
|
2004-02-11 04:32:52 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2003,
|
|
|
|
* 2004 Free Software Foundation, Inc.
|
1997-11-24 22:24:04 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-24 22:24:04 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +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.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* 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
|
2005-05-31 08:39:18 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
1997-11-24 22:24:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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 "util.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "mpi.h"
|
1997-11-24 22:24:04 +00:00
|
|
|
#include "keydb.h"
|
2000-07-14 17:34:53 +00:00
|
|
|
#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 ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
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' ;
|
1997-12-16 19:15:09 +00:00
|
|
|
default: return '?';
|
|
|
|
}
|
|
|
|
}
|
1997-11-24 22:24:04 +00:00
|
|
|
|
* packet.h, build-packet.c (hash_public_key): Remove function ...
* keydb.h, keyid.c (hash_public_key, do_fingerprint_md): ... and make a
new one here that shares code with the fingerprint calculations. This
removes some duplicated functionality, and is also around 14% faster.
(Every bit helps).
* import.c (import_one): No longer need the Elgamal import warning.
* getkey.c (get_pubkey_fast): This one is sort of obscure. get_pubkey_fast
returns the primary key when requesting a subkey, so if a user has a key
signed by a subkey (we don't do this, but used to), AND that key is not
self-signed, AND the algorithm of the subkey in question is not present in
GnuPG, AND the algorithm of the primary key that owns the subkey in
question is present in GnuPG, then we will try and verify the subkey
signature using the primary key algorithm and hit a BUG(). The fix is to
not return a hit if the keyid is not the primary. All other users of
get_pubkey_fast already expect a primary only.
2003-12-11 01:07:42 +00:00
|
|
|
/* This function is useful for v4 fingerprints and v3 or v4 key
|
|
|
|
signing. */
|
|
|
|
void
|
|
|
|
hash_public_key( MD_HANDLE md, PKT_public_key *pk )
|
1998-01-07 20:47:46 +00:00
|
|
|
{
|
* packet.h, build-packet.c (hash_public_key): Remove function ...
* keydb.h, keyid.c (hash_public_key, do_fingerprint_md): ... and make a
new one here that shares code with the fingerprint calculations. This
removes some duplicated functionality, and is also around 14% faster.
(Every bit helps).
* import.c (import_one): No longer need the Elgamal import warning.
* getkey.c (get_pubkey_fast): This one is sort of obscure. get_pubkey_fast
returns the primary key when requesting a subkey, so if a user has a key
signed by a subkey (we don't do this, but used to), AND that key is not
self-signed, AND the algorithm of the subkey in question is not present in
GnuPG, AND the algorithm of the primary key that owns the subkey in
question is present in GnuPG, then we will try and verify the subkey
signature using the primary key algorithm and hit a BUG(). The fix is to
not return a hit if the keyid is not the primary. All other users of
get_pubkey_fast already expect a primary only.
2003-12-11 01:07:42 +00:00
|
|
|
unsigned n=6;
|
|
|
|
unsigned nb[PUBKEY_MAX_NPKEY];
|
|
|
|
unsigned nn[PUBKEY_MAX_NPKEY];
|
|
|
|
byte *pp[PUBKEY_MAX_NPKEY];
|
|
|
|
int i;
|
|
|
|
int npkey = pubkey_get_npkey( pk->pubkey_algo );
|
|
|
|
|
|
|
|
/* Two extra bytes for the expiration date in v3 */
|
|
|
|
if(pk->version<4)
|
|
|
|
n+=2;
|
|
|
|
|
|
|
|
if(npkey==0 && pk->pkey[0] && mpi_is_opaque(pk->pkey[0]))
|
|
|
|
{
|
|
|
|
pp[0]=mpi_get_opaque(pk->pkey[0],&nn[0]);
|
|
|
|
n+=nn[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for(i=0; i < npkey; i++ )
|
|
|
|
{
|
|
|
|
nb[i] = mpi_get_nbits(pk->pkey[i]);
|
|
|
|
pp[i] = mpi_get_buffer( pk->pkey[i], nn+i, NULL );
|
|
|
|
n += 2 + nn[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
md_putc( md, 0x99 ); /* ctb */
|
|
|
|
/* What does it mean if n is greater than than 0xFFFF ? */
|
|
|
|
md_putc( md, n >> 8 ); /* 2 byte length header */
|
|
|
|
md_putc( md, n );
|
|
|
|
md_putc( md, pk->version );
|
|
|
|
|
|
|
|
md_putc( md, pk->timestamp >> 24 );
|
|
|
|
md_putc( md, pk->timestamp >> 16 );
|
|
|
|
md_putc( md, pk->timestamp >> 8 );
|
|
|
|
md_putc( md, pk->timestamp );
|
|
|
|
|
|
|
|
if(pk->version<4)
|
|
|
|
{
|
|
|
|
u16 days=0;
|
|
|
|
if(pk->expiredate)
|
|
|
|
days=(u16)((pk->expiredate - pk->timestamp) / 86400L);
|
|
|
|
|
|
|
|
md_putc( md, days >> 8 );
|
|
|
|
md_putc( md, days );
|
|
|
|
}
|
1998-06-13 06:59:14 +00:00
|
|
|
|
* packet.h, build-packet.c (hash_public_key): Remove function ...
* keydb.h, keyid.c (hash_public_key, do_fingerprint_md): ... and make a
new one here that shares code with the fingerprint calculations. This
removes some duplicated functionality, and is also around 14% faster.
(Every bit helps).
* import.c (import_one): No longer need the Elgamal import warning.
* getkey.c (get_pubkey_fast): This one is sort of obscure. get_pubkey_fast
returns the primary key when requesting a subkey, so if a user has a key
signed by a subkey (we don't do this, but used to), AND that key is not
self-signed, AND the algorithm of the subkey in question is not present in
GnuPG, AND the algorithm of the primary key that owns the subkey in
question is present in GnuPG, then we will try and verify the subkey
signature using the primary key algorithm and hit a BUG(). The fix is to
not return a hit if the keyid is not the primary. All other users of
get_pubkey_fast already expect a primary only.
2003-12-11 01:07:42 +00:00
|
|
|
md_putc( md, pk->pubkey_algo );
|
2003-12-10 01:59:45 +00:00
|
|
|
|
* packet.h, build-packet.c (hash_public_key): Remove function ...
* keydb.h, keyid.c (hash_public_key, do_fingerprint_md): ... and make a
new one here that shares code with the fingerprint calculations. This
removes some duplicated functionality, and is also around 14% faster.
(Every bit helps).
* import.c (import_one): No longer need the Elgamal import warning.
* getkey.c (get_pubkey_fast): This one is sort of obscure. get_pubkey_fast
returns the primary key when requesting a subkey, so if a user has a key
signed by a subkey (we don't do this, but used to), AND that key is not
self-signed, AND the algorithm of the subkey in question is not present in
GnuPG, AND the algorithm of the primary key that owns the subkey in
question is present in GnuPG, then we will try and verify the subkey
signature using the primary key algorithm and hit a BUG(). The fix is to
not return a hit if the keyid is not the primary. All other users of
get_pubkey_fast already expect a primary only.
2003-12-11 01:07:42 +00:00
|
|
|
if(npkey==0 && pk->pkey[0] && mpi_is_opaque(pk->pkey[0]))
|
|
|
|
md_write(md,pp[0],nn[0]);
|
|
|
|
else
|
|
|
|
for(i=0; i < npkey; i++ )
|
2003-12-10 01:27:55 +00:00
|
|
|
{
|
* packet.h, build-packet.c (hash_public_key): Remove function ...
* keydb.h, keyid.c (hash_public_key, do_fingerprint_md): ... and make a
new one here that shares code with the fingerprint calculations. This
removes some duplicated functionality, and is also around 14% faster.
(Every bit helps).
* import.c (import_one): No longer need the Elgamal import warning.
* getkey.c (get_pubkey_fast): This one is sort of obscure. get_pubkey_fast
returns the primary key when requesting a subkey, so if a user has a key
signed by a subkey (we don't do this, but used to), AND that key is not
self-signed, AND the algorithm of the subkey in question is not present in
GnuPG, AND the algorithm of the primary key that owns the subkey in
question is present in GnuPG, then we will try and verify the subkey
signature using the primary key algorithm and hit a BUG(). The fix is to
not return a hit if the keyid is not the primary. All other users of
get_pubkey_fast already expect a primary only.
2003-12-11 01:07:42 +00:00
|
|
|
md_putc( md, nb[i]>>8);
|
|
|
|
md_putc( md, nb[i] );
|
|
|
|
md_write( md, pp[i], nn[i] );
|
2005-07-27 18:10:56 +00:00
|
|
|
xfree(pp[i]);
|
2003-12-10 01:27:55 +00:00
|
|
|
}
|
* packet.h, build-packet.c (hash_public_key): Remove function ...
* keydb.h, keyid.c (hash_public_key, do_fingerprint_md): ... and make a
new one here that shares code with the fingerprint calculations. This
removes some duplicated functionality, and is also around 14% faster.
(Every bit helps).
* import.c (import_one): No longer need the Elgamal import warning.
* getkey.c (get_pubkey_fast): This one is sort of obscure. get_pubkey_fast
returns the primary key when requesting a subkey, so if a user has a key
signed by a subkey (we don't do this, but used to), AND that key is not
self-signed, AND the algorithm of the subkey in question is not present in
GnuPG, AND the algorithm of the primary key that owns the subkey in
question is present in GnuPG, then we will try and verify the subkey
signature using the primary key algorithm and hit a BUG(). The fix is to
not return a hit if the keyid is not the primary. All other users of
get_pubkey_fast already expect a primary only.
2003-12-11 01:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static MD_HANDLE
|
|
|
|
do_fingerprint_md( PKT_public_key *pk )
|
|
|
|
{
|
|
|
|
MD_HANDLE md;
|
|
|
|
|
|
|
|
md = md_open( DIGEST_ALGO_SHA1, 0);
|
|
|
|
hash_public_key(md,pk);
|
|
|
|
md_final( md );
|
|
|
|
|
|
|
|
return md;
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
1998-01-07 20:47:46 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
static MD_HANDLE
|
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
|
|
|
|
2003-12-10 01:27:55 +00:00
|
|
|
if(npkey==0)
|
|
|
|
return NULL;
|
|
|
|
|
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++ )
|
2003-12-10 01:27:55 +00:00
|
|
|
pk.pkey[i] = sk->skey[i];
|
1998-06-29 12:30:57 +00:00
|
|
|
return do_fingerprint_md( &pk );
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
|
|
|
|
2004-03-03 05:47:51 +00:00
|
|
|
size_t
|
|
|
|
keystrlen(void)
|
|
|
|
{
|
|
|
|
switch(opt.keyid_format)
|
|
|
|
{
|
|
|
|
case KF_SHORT:
|
|
|
|
return 8;
|
|
|
|
|
|
|
|
case KF_LONG:
|
|
|
|
return 16;
|
|
|
|
|
|
|
|
case KF_0xSHORT:
|
|
|
|
return 10;
|
|
|
|
|
|
|
|
case KF_0xLONG:
|
|
|
|
return 18;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
keystr(u32 *keyid)
|
|
|
|
{
|
|
|
|
static char keyid_str[19];
|
|
|
|
|
|
|
|
switch(opt.keyid_format)
|
|
|
|
{
|
|
|
|
case KF_SHORT:
|
|
|
|
sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_LONG:
|
2004-08-23 19:20:17 +00:00
|
|
|
if(keyid[0])
|
|
|
|
sprintf(keyid_str,"%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
|
|
|
|
else
|
|
|
|
sprintf(keyid_str,"%08lX",(ulong)keyid[1]);
|
2004-03-03 05:47:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_0xSHORT:
|
|
|
|
sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KF_0xLONG:
|
2004-08-23 19:20:17 +00:00
|
|
|
if(keyid[0])
|
|
|
|
sprintf(keyid_str,"0x%08lX%08lX",(ulong)keyid[0],(ulong)keyid[1]);
|
|
|
|
else
|
|
|
|
sprintf(keyid_str,"0x%08lX",(ulong)keyid[1]);
|
2004-03-03 05:47:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyid_str;
|
|
|
|
}
|
|
|
|
|
2004-03-06 17:12:44 +00:00
|
|
|
const char *
|
|
|
|
keystr_from_pk(PKT_public_key *pk)
|
|
|
|
{
|
|
|
|
keyid_from_pk(pk,NULL);
|
|
|
|
|
|
|
|
return keystr(pk->keyid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
keystr_from_sk(PKT_secret_key *sk)
|
|
|
|
{
|
|
|
|
keyid_from_sk(sk,NULL);
|
|
|
|
|
|
|
|
return keystr(sk->keyid);
|
|
|
|
}
|
2004-03-03 05:47:51 +00:00
|
|
|
|
2004-05-21 17:32:30 +00:00
|
|
|
const char *
|
|
|
|
keystr_from_desc(KEYDB_SEARCH_DESC *desc)
|
|
|
|
{
|
2004-08-23 19:20:17 +00:00
|
|
|
switch(desc->mode)
|
2004-05-21 17:32:30 +00:00
|
|
|
{
|
2004-08-23 19:20:17 +00:00
|
|
|
case KEYDB_SEARCH_MODE_LONG_KID:
|
|
|
|
case KEYDB_SEARCH_MODE_SHORT_KID:
|
|
|
|
return keystr(desc->u.kid);
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR20:
|
|
|
|
{
|
|
|
|
u32 keyid[2];
|
|
|
|
|
|
|
|
keyid[0] = (unsigned char)desc->u.fpr[12] << 24
|
|
|
|
| (unsigned char)desc->u.fpr[13] << 16
|
|
|
|
| (unsigned char)desc->u.fpr[14] << 8
|
|
|
|
| (unsigned char)desc->u.fpr[15] ;
|
|
|
|
keyid[1] = (unsigned char)desc->u.fpr[16] << 24
|
|
|
|
| (unsigned char)desc->u.fpr[17] << 16
|
|
|
|
| (unsigned char)desc->u.fpr[18] << 8
|
|
|
|
| (unsigned char)desc->u.fpr[19] ;
|
|
|
|
|
|
|
|
return keystr(keyid);
|
|
|
|
}
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR16:
|
|
|
|
return "?v3 fpr?";
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
2004-05-21 17:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-03-06 04:08:06 +00:00
|
|
|
u32 lowbits;
|
|
|
|
u32 dummy_keyid[2];
|
1997-11-24 22:24:04 +00:00
|
|
|
|
2004-03-06 04:08:06 +00:00
|
|
|
if( !keyid )
|
|
|
|
keyid = dummy_keyid;
|
1997-11-24 22:24:04 +00:00
|
|
|
|
2004-03-06 04:08:06 +00:00
|
|
|
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) )
|
|
|
|
{
|
2003-12-10 01:52:33 +00:00
|
|
|
lowbits = pubkey_get_npkey(sk->pubkey_algo) ?
|
|
|
|
mpi_get_keyid( sk->skey[0], keyid ) : 0; /* take n */
|
2004-03-06 04:08:06 +00:00
|
|
|
sk->keyid[0]=keyid[0];
|
|
|
|
sk->keyid[1]=keyid[1];
|
|
|
|
}
|
|
|
|
else
|
2005-01-28 00:50:10 +00:00
|
|
|
sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
|
2004-03-06 04:08:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const byte *dp;
|
|
|
|
MD_HANDLE md;
|
|
|
|
md = do_fingerprint_md_sk(sk);
|
|
|
|
if(md)
|
|
|
|
{
|
|
|
|
dp = 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];
|
|
|
|
md_close(md);
|
|
|
|
sk->keyid[0] = keyid[0];
|
|
|
|
sk->keyid[1] = keyid[1];
|
|
|
|
}
|
|
|
|
else
|
2005-01-28 00:50:10 +00:00
|
|
|
sk->keyid[0]=sk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
1998-06-13 06:59:14 +00:00
|
|
|
|
2004-03-06 04:08:06 +00:00
|
|
|
return lowbits;
|
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
|
|
|
{
|
2004-03-06 17:12:44 +00:00
|
|
|
u32 lowbits;
|
|
|
|
u32 dummy_keyid[2];
|
1997-11-24 22:24:04 +00:00
|
|
|
|
2004-03-06 17:12:44 +00:00
|
|
|
if( !keyid )
|
|
|
|
keyid = dummy_keyid;
|
1997-11-24 22:24:04 +00:00
|
|
|
|
2004-03-06 17:12:44 +00:00
|
|
|
if( pk->keyid[0] || pk->keyid[1] )
|
|
|
|
{
|
|
|
|
keyid[0] = pk->keyid[0];
|
|
|
|
keyid[1] = pk->keyid[1];
|
|
|
|
lowbits = keyid[1];
|
1998-07-09 13:37:17 +00:00
|
|
|
}
|
2004-03-06 17:12:44 +00:00
|
|
|
else if( pk->version < 4 )
|
|
|
|
{
|
|
|
|
if( is_RSA(pk->pubkey_algo) )
|
|
|
|
{
|
|
|
|
lowbits = pubkey_get_npkey(pk->pubkey_algo) ?
|
|
|
|
mpi_get_keyid( pk->pkey[0], keyid ) : 0 ; /* from n */
|
|
|
|
pk->keyid[0] = keyid[0];
|
|
|
|
pk->keyid[1] = keyid[1];
|
|
|
|
}
|
|
|
|
else
|
2005-01-28 00:50:10 +00:00
|
|
|
pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
|
2004-03-06 17:12:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const byte *dp;
|
|
|
|
MD_HANDLE md;
|
|
|
|
md = do_fingerprint_md(pk);
|
|
|
|
if(md)
|
|
|
|
{
|
|
|
|
dp = 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];
|
|
|
|
md_close(md);
|
|
|
|
pk->keyid[0] = keyid[0];
|
|
|
|
pk->keyid[1] = keyid[1];
|
|
|
|
}
|
|
|
|
else
|
2005-01-28 00:50:10 +00:00
|
|
|
pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
1997-11-24 22:24:04 +00:00
|
|
|
|
2004-03-06 17:12:44 +00:00
|
|
|
return lowbits;
|
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];
|
|
|
|
}
|
|
|
|
|
2003-01-14 18:13:22 +00:00
|
|
|
byte *
|
|
|
|
namehash_from_uid(PKT_user_id *uid)
|
|
|
|
{
|
|
|
|
if(uid->namehash==NULL)
|
|
|
|
{
|
2005-07-27 18:10:56 +00:00
|
|
|
uid->namehash=xmalloc(20);
|
2003-01-14 18:13:22 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return uid->namehash;
|
|
|
|
}
|
|
|
|
|
1997-12-01 10:33:23 +00:00
|
|
|
/****************
|
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
|
|
|
}
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
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];
|
1998-06-29 12:30:57 +00:00
|
|
|
time_t atime = pk->timestamp;
|
1997-12-01 10:33:23 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
return mk_datestr (buffer, atime);
|
1997-12-01 10:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
1998-06-29 12:30:57 +00:00
|
|
|
time_t atime = sk->timestamp;
|
1997-12-01 10:33:23 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
return mk_datestr (buffer, atime);
|
1997-12-01 10:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
datestr_from_sig( PKT_signature *sig )
|
|
|
|
{
|
|
|
|
static char buffer[11+5];
|
|
|
|
time_t atime = sig->timestamp;
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
return mk_datestr (buffer, atime);
|
1997-12-01 10:33: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];
|
|
|
|
time_t atime;
|
|
|
|
|
1998-10-16 16:00:17 +00:00
|
|
|
if( !pk->expiredate )
|
2000-07-14 17:34:53 +00:00
|
|
|
return _("never ");
|
1998-11-20 17:42:18 +00:00
|
|
|
atime = pk->expiredate;
|
2002-06-29 13:46:34 +00:00
|
|
|
return mk_datestr (buffer, atime);
|
1998-07-29 19:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
expirestr_from_sk( PKT_secret_key *sk )
|
|
|
|
{
|
|
|
|
static char buffer[11+5];
|
|
|
|
time_t atime;
|
|
|
|
|
1998-10-16 16:00:17 +00:00
|
|
|
if( !sk->expiredate )
|
2000-07-14 17:34:53 +00:00
|
|
|
return _("never ");
|
1998-10-16 16:00:17 +00:00
|
|
|
atime = sk->expiredate;
|
2002-06-29 13:46:34 +00:00
|
|
|
return mk_datestr (buffer, atime);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
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);
|
2004-02-11 04:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
revokestr_from_pk( PKT_public_key *pk )
|
|
|
|
{
|
|
|
|
static char buffer[11+5];
|
|
|
|
time_t atime;
|
|
|
|
|
2004-12-30 03:26:57 +00:00
|
|
|
if(!pk->revoked.date)
|
2004-02-11 04:32:52 +00:00
|
|
|
return _("never ");
|
2004-12-30 03:26:57 +00:00
|
|
|
atime=pk->revoked.date;
|
2004-02-11 04:32:52 +00:00
|
|
|
return mk_datestr (buffer, atime);
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 13:32:31 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
usagestr_from_pk( PKT_public_key *pk )
|
|
|
|
{
|
|
|
|
static char buffer[10];
|
|
|
|
int i = 0;
|
|
|
|
unsigned int use = pk->pubkey_usage;
|
|
|
|
|
|
|
|
if ( use & PUBKEY_USAGE_SIG )
|
|
|
|
{
|
|
|
|
if (pk->is_primary)
|
2005-08-27 03:09:40 +00:00
|
|
|
use|=PUBKEY_USAGE_CERT;
|
2004-09-23 13:32:31 +00:00
|
|
|
buffer[i++] = 'S';
|
|
|
|
}
|
|
|
|
|
2005-08-27 03:09:40 +00:00
|
|
|
if ( use & PUBKEY_USAGE_CERT )
|
|
|
|
buffer[i++] = 'C';
|
|
|
|
|
2004-09-23 13:32:31 +00:00
|
|
|
if ( use & PUBKEY_USAGE_ENC )
|
|
|
|
buffer[i++] = 'E';
|
|
|
|
|
|
|
|
if ( (use & PUBKEY_USAGE_AUTH) )
|
|
|
|
buffer[i++] = 'A';
|
|
|
|
|
|
|
|
while (i < 4)
|
|
|
|
buffer[i++] = ' ';
|
|
|
|
|
|
|
|
buffer[i] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
const char *
|
|
|
|
colon_strtime (u32 t)
|
|
|
|
{
|
|
|
|
if (!t)
|
|
|
|
return "";
|
|
|
|
if (opt.fixed_list_mode) {
|
|
|
|
static char buf[15];
|
|
|
|
sprintf (buf, "%lu", (ulong)t);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return strtimestamp(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
colon_datestr_from_pk (PKT_public_key *pk)
|
|
|
|
{
|
|
|
|
if (opt.fixed_list_mode) {
|
|
|
|
static char buf[15];
|
|
|
|
sprintf (buf, "%lu", (ulong)pk->timestamp);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return datestr_from_pk (pk);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
colon_datestr_from_sk (PKT_secret_key *sk)
|
|
|
|
{
|
|
|
|
if (opt.fixed_list_mode) {
|
|
|
|
static char buf[15];
|
|
|
|
sprintf (buf, "%lu", (ulong)sk->timestamp);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return datestr_from_sk (sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
colon_datestr_from_sig (PKT_signature *sig)
|
|
|
|
{
|
|
|
|
if (opt.fixed_list_mode) {
|
|
|
|
static char buf[15];
|
|
|
|
sprintf (buf, "%lu", (ulong)sig->timestamp);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return datestr_from_sig (sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
colon_expirestr_from_sig (PKT_signature *sig)
|
|
|
|
{
|
|
|
|
if(!sig->expiredate)
|
|
|
|
return "";
|
|
|
|
if (opt.fixed_list_mode) {
|
|
|
|
static char buf[15];
|
|
|
|
sprintf (buf, "%lu", (ulong)sig->expiredate);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return expirestr_from_sig (sig);
|
1998-07-29 19:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
byte *p, *buf;
|
|
|
|
const byte *dp;
|
1997-12-09 12:46:23 +00:00
|
|
|
size_t len;
|
2002-06-29 13:46:34 +00:00
|
|
|
unsigned int n;
|
1997-12-09 12:46:23 +00:00
|
|
|
|
2003-12-10 01:52:33 +00:00
|
|
|
if( pk->version < 4 )
|
|
|
|
{
|
|
|
|
if( is_RSA(pk->pubkey_algo) )
|
|
|
|
{
|
|
|
|
/* RSA in version 3 packets is special */
|
|
|
|
MD_HANDLE md;
|
|
|
|
|
|
|
|
md = md_open( DIGEST_ALGO_MD5, 0);
|
|
|
|
if( pubkey_get_npkey( pk->pubkey_algo ) > 1 ) {
|
|
|
|
p = buf = mpi_get_buffer( pk->pkey[0], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
2005-07-27 18:10:56 +00:00
|
|
|
xfree(buf);
|
2003-12-10 01:52:33 +00:00
|
|
|
p = buf = mpi_get_buffer( pk->pkey[1], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
2005-07-27 18:10:56 +00:00
|
|
|
xfree(buf);
|
2003-12-10 01:52:33 +00:00
|
|
|
}
|
|
|
|
md_final(md);
|
|
|
|
if( !array )
|
2005-07-27 18:10:56 +00:00
|
|
|
array = xmalloc( 16 );
|
2003-12-10 01:52:33 +00:00
|
|
|
len = 16;
|
|
|
|
memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 );
|
|
|
|
md_close(md);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!array)
|
2005-07-27 18:10:56 +00:00
|
|
|
array=xmalloc(16);
|
2003-12-10 01:52:33 +00:00
|
|
|
len=16;
|
|
|
|
memset(array,0,16);
|
|
|
|
}
|
|
|
|
}
|
1998-06-13 06:59:14 +00:00
|
|
|
else {
|
2002-06-29 13:46:34 +00:00
|
|
|
MD_HANDLE md;
|
1998-06-29 12:30:57 +00:00
|
|
|
md = do_fingerprint_md(pk);
|
2002-06-29 13:46:34 +00:00
|
|
|
dp = md_read( md, 0 );
|
|
|
|
len = md_digest_length( md_get_algo( md ) );
|
1998-07-14 17:10:28 +00:00
|
|
|
assert( len <= MAX_FINGERPRINT_LEN );
|
|
|
|
if( !array )
|
2005-07-27 18:10:56 +00:00
|
|
|
array = xmalloc( len );
|
1998-06-13 06:59:14 +00:00
|
|
|
memcpy(array, dp, len );
|
2002-06-29 13:46:34 +00:00
|
|
|
pk->keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
|
|
|
|
pk->keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
|
|
|
|
md_close(md);
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
1998-06-13 06:59:14 +00:00
|
|
|
|
|
|
|
*ret_len = len;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
byte *p, *buf;
|
1998-06-13 06:59:14 +00:00
|
|
|
const char *dp;
|
|
|
|
size_t len;
|
2002-06-29 13:46:34 +00:00
|
|
|
unsigned n;
|
1998-06-13 06:59:14 +00:00
|
|
|
|
2003-12-10 01:52:33 +00:00
|
|
|
if( sk->version < 4 )
|
|
|
|
{
|
|
|
|
if( is_RSA(sk->pubkey_algo) )
|
|
|
|
{
|
|
|
|
/* RSA in version 3 packets is special */
|
|
|
|
MD_HANDLE md;
|
|
|
|
|
|
|
|
md = md_open( DIGEST_ALGO_MD5, 0);
|
|
|
|
if( pubkey_get_npkey( sk->pubkey_algo ) > 1 ) {
|
|
|
|
p = buf = mpi_get_buffer( sk->skey[0], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
2005-07-27 18:10:56 +00:00
|
|
|
xfree(buf);
|
2003-12-10 01:52:33 +00:00
|
|
|
p = buf = mpi_get_buffer( sk->skey[1], &n, NULL );
|
|
|
|
md_write( md, p, n );
|
2005-07-27 18:10:56 +00:00
|
|
|
xfree(buf);
|
2003-12-10 01:52:33 +00:00
|
|
|
}
|
|
|
|
md_final(md);
|
|
|
|
if( !array )
|
2005-07-27 18:10:56 +00:00
|
|
|
array = xmalloc( 16 );
|
2003-12-10 01:52:33 +00:00
|
|
|
len = 16;
|
|
|
|
memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 );
|
|
|
|
md_close(md);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!array)
|
2005-07-27 18:10:56 +00:00
|
|
|
array=xmalloc(16);
|
2003-12-10 01:52:33 +00:00
|
|
|
len=16;
|
|
|
|
memset(array,0,16);
|
|
|
|
}
|
|
|
|
}
|
1997-12-09 12:46:23 +00:00
|
|
|
else {
|
2002-06-29 13:46:34 +00:00
|
|
|
MD_HANDLE md;
|
1998-06-29 12:30:57 +00:00
|
|
|
md = do_fingerprint_md_sk(sk);
|
2003-12-10 01:27:55 +00:00
|
|
|
if(md)
|
|
|
|
{
|
|
|
|
dp = md_read( md, 0 );
|
|
|
|
len = md_digest_length( md_get_algo( md ) );
|
|
|
|
assert( len <= MAX_FINGERPRINT_LEN );
|
|
|
|
if( !array )
|
2005-07-27 18:10:56 +00:00
|
|
|
array = xmalloc( len );
|
2003-12-10 01:27:55 +00:00
|
|
|
memcpy(array, dp, len );
|
|
|
|
md_close(md);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len=MAX_FINGERPRINT_LEN;
|
|
|
|
if(!array)
|
2005-07-27 18:10:56 +00:00
|
|
|
array=xmalloc(len);
|
2003-12-10 01:27:55 +00:00
|
|
|
memset(array,0,len);
|
|
|
|
}
|
1997-12-09 12:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*ret_len = len;
|
|
|
|
return array;
|
|
|
|
}
|