2003-06-05 07:14:21 +00:00
|
|
|
|
/* keyid.c - key ID and fingerprint handling
|
2006-04-19 11:26:11 +00:00
|
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2003,
|
2010-04-20 17:57:50 +00:00
|
|
|
|
* 2004, 2006, 2010 Free Software Foundation, Inc.
|
2003-06-05 07:14:21 +00:00
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 07:14:21 +00:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
|
* 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
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-06-05 07:14:21 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <assert.h>
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
|
|
|
|
#include "gpg.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include "packet.h"
|
|
|
|
|
#include "options.h"
|
|
|
|
|
#include "keydb.h"
|
|
|
|
|
#include "i18n.h"
|
2008-12-11 17:44:52 +00:00
|
|
|
|
#include "rmd160.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
#define KEYID_STR_SIZE 19
|
|
|
|
|
|
2010-10-27 11:26:53 +00:00
|
|
|
|
#ifdef HAVE_UNSIGNED_TIME_T
|
|
|
|
|
# define IS_INVALID_TIME_T(a) ((a) == (time_t)(-1))
|
|
|
|
|
#else
|
|
|
|
|
/* Error or 32 bit time_t and value after 2038-01-19. */
|
|
|
|
|
# define IS_INVALID_TIME_T(a) ((a) < 0)
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
/* Return a letter describing the public key algorithms. */
|
2003-06-05 07:14:21 +00:00
|
|
|
|
int
|
|
|
|
|
pubkey_letter( int algo )
|
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
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' ;
|
2011-01-05 17:33:17 -08:00
|
|
|
|
case PUBKEY_ALGO_ECDSA: return 'E' ; // ECC DSA (sign only)
|
|
|
|
|
case PUBKEY_ALGO_ECDH: return 'e' ; // ECC DH (encrypt only)
|
2010-09-06 19:57:42 +00:00
|
|
|
|
default: return '?';
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
|
|
|
|
/* Hash a public key. This function is useful for v4 fingerprints and
|
|
|
|
|
for v3 or v4 key signing. */
|
2006-04-19 11:26:11 +00:00
|
|
|
|
void
|
2010-09-06 19:57:42 +00:00
|
|
|
|
hash_public_key (gcry_md_hd_t md, PKT_public_key *pk)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2006-04-19 11:26:11 +00:00
|
|
|
|
unsigned int n = 6;
|
|
|
|
|
unsigned int nn[PUBKEY_MAX_NPKEY];
|
|
|
|
|
byte *pp[PUBKEY_MAX_NPKEY];
|
|
|
|
|
int i;
|
2006-11-21 11:00:14 +00:00
|
|
|
|
unsigned int nbits;
|
|
|
|
|
size_t nbytes;
|
2006-04-19 11:26:11 +00:00
|
|
|
|
int npkey = pubkey_get_npkey (pk->pubkey_algo);
|
2011-01-05 17:33:17 -08:00
|
|
|
|
/* name OID, MPI of public point, [for ECDH only: KEK params] */
|
|
|
|
|
enum gcry_mpi_format ecc_pub_format[3] = {GCRYMPI_FMT_USG, GCRYMPI_FMT_PGP, GCRYMPI_FMT_USG};
|
2003-07-23 07:11:06 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
/* Two extra bytes for the expiration date in v3 */
|
|
|
|
|
if(pk->version<4)
|
|
|
|
|
n+=2;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
if (npkey==0 && pk->pkey[0]
|
|
|
|
|
&& gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
|
|
|
|
|
{
|
|
|
|
|
pp[0] = gcry_mpi_get_opaque (pk->pkey[0], &nbits);
|
|
|
|
|
nn[0] = (nbits+7)/8;
|
|
|
|
|
n+=nn[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
2010-09-06 19:57:42 +00:00
|
|
|
|
{
|
|
|
|
|
for(i=0; i < npkey; i++ )
|
|
|
|
|
{
|
2011-01-05 17:33:17 -08:00
|
|
|
|
const enum gcry_mpi_format fmt =
|
|
|
|
|
((pk->pubkey_algo==PUBKEY_ALGO_ECDSA || pk->pubkey_algo==PUBKEY_ALGO_ECDH) ? ecc_pub_format[i] : GCRYMPI_FMT_PGP);
|
|
|
|
|
|
|
|
|
|
if (gcry_mpi_print (fmt, NULL, 0, &nbytes, pk->pkey[i]))
|
2010-09-06 19:57:42 +00:00
|
|
|
|
BUG ();
|
|
|
|
|
pp[i] = xmalloc (nbytes);
|
2011-01-05 17:33:17 -08:00
|
|
|
|
if (gcry_mpi_print (fmt, pp[i], nbytes, &nbytes, pk->pkey[i]))
|
2010-09-06 19:57:42 +00:00
|
|
|
|
BUG ();
|
|
|
|
|
nn[i] = nbytes;
|
|
|
|
|
n += nn[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
|
|
|
|
gcry_md_putc ( md, 0x99 ); /* ctb */
|
|
|
|
|
/* What does it mean if n is greater than than 0xFFFF ? */
|
|
|
|
|
gcry_md_putc ( md, n >> 8 ); /* 2 byte length header */
|
|
|
|
|
gcry_md_putc ( md, n );
|
|
|
|
|
gcry_md_putc ( md, pk->version );
|
|
|
|
|
|
|
|
|
|
gcry_md_putc ( md, pk->timestamp >> 24 );
|
|
|
|
|
gcry_md_putc ( md, pk->timestamp >> 16 );
|
|
|
|
|
gcry_md_putc ( md, pk->timestamp >> 8 );
|
|
|
|
|
gcry_md_putc ( md, pk->timestamp );
|
|
|
|
|
|
|
|
|
|
if(pk->version<4)
|
|
|
|
|
{
|
|
|
|
|
u16 days=0;
|
|
|
|
|
if(pk->expiredate)
|
|
|
|
|
days=(u16)((pk->expiredate - pk->timestamp) / 86400L);
|
|
|
|
|
|
|
|
|
|
gcry_md_putc ( md, days >> 8 );
|
|
|
|
|
gcry_md_putc ( md, days );
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
gcry_md_putc ( md, pk->pubkey_algo );
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
if(npkey==0 && pk->pkey[0]
|
|
|
|
|
&& gcry_mpi_get_flag (pk->pkey[0], GCRYMPI_FLAG_OPAQUE))
|
|
|
|
|
{
|
|
|
|
|
gcry_md_write (md, pp[0], nn[0]);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
else
|
|
|
|
|
for(i=0; i < npkey; i++ )
|
|
|
|
|
{
|
|
|
|
|
gcry_md_write ( md, pp[i], nn[i] );
|
|
|
|
|
xfree(pp[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
static gcry_md_hd_t
|
|
|
|
|
do_fingerprint_md( PKT_public_key *pk )
|
|
|
|
|
{
|
|
|
|
|
gcry_md_hd_t md;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
if (gcry_md_open (&md, DIGEST_ALGO_SHA1, 0))
|
|
|
|
|
BUG ();
|
|
|
|
|
hash_public_key(md,pk);
|
|
|
|
|
gcry_md_final( md );
|
|
|
|
|
|
|
|
|
|
return md;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
/* fixme: Check whether we can replace this function or if not
|
|
|
|
|
describe why we need it. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
u32
|
|
|
|
|
v3_keyid (gcry_mpi_t a, u32 *ki)
|
|
|
|
|
{
|
2006-10-18 15:34:54 +00:00
|
|
|
|
byte *buffer, *p;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
size_t nbytes;
|
|
|
|
|
|
2003-07-28 08:59:18 +00:00
|
|
|
|
if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &nbytes, a ))
|
2003-06-18 19:56:13 +00:00
|
|
|
|
BUG ();
|
|
|
|
|
/* fixme: allocate it on the stack */
|
|
|
|
|
buffer = xmalloc (nbytes);
|
2003-07-28 08:59:18 +00:00
|
|
|
|
if (gcry_mpi_print( GCRYMPI_FMT_USG, buffer, nbytes, NULL, a ))
|
2003-06-18 19:56:13 +00:00
|
|
|
|
BUG ();
|
|
|
|
|
if (nbytes < 8) /* oops */
|
|
|
|
|
ki[0] = ki[1] = 0;
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-10-18 15:34:54 +00:00
|
|
|
|
p = buffer + nbytes - 8;
|
|
|
|
|
ki[0] = (p[0] << 24) | (p[1] <<16) | (p[2] << 8) | p[3];
|
|
|
|
|
p += 4;
|
|
|
|
|
ki[1] = (p[0] << 24) | (p[1] <<16) | (p[2] << 8) | p[3];
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
xfree (buffer);
|
|
|
|
|
return ki[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-19 11:26:11 +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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
const char *
|
2010-08-31 15:58:39 +00:00
|
|
|
|
keystr (u32 *keyid)
|
2006-04-19 11:26:11 +00:00
|
|
|
|
{
|
2010-08-31 15:58:39 +00:00
|
|
|
|
static char keyid_str[KEYID_STR_SIZE];
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
switch (opt.keyid_format)
|
2006-04-19 11:26:11 +00:00
|
|
|
|
{
|
|
|
|
|
case KF_SHORT:
|
2010-08-31 15:58:39 +00:00
|
|
|
|
snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]);
|
2006-04-19 11:26:11 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KF_LONG:
|
2010-08-31 15:58:39 +00:00
|
|
|
|
if (keyid[0])
|
|
|
|
|
snprintf (keyid_str, sizeof keyid_str, "%08lX%08lX",
|
|
|
|
|
(ulong)keyid[0], (ulong)keyid[1]);
|
2006-04-19 11:26:11 +00:00
|
|
|
|
else
|
2010-08-31 15:58:39 +00:00
|
|
|
|
snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]);
|
2006-04-19 11:26:11 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KF_0xSHORT:
|
2010-08-31 15:58:39 +00:00
|
|
|
|
snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]);
|
2006-04-19 11:26:11 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KF_0xLONG:
|
|
|
|
|
if(keyid[0])
|
2010-08-31 15:58:39 +00:00
|
|
|
|
snprintf (keyid_str, sizeof keyid_str, "0x%08lX%08lX",
|
|
|
|
|
(ulong)keyid[0],(ulong)keyid[1]);
|
2006-04-19 11:26:11 +00:00
|
|
|
|
else
|
2010-08-31 15:58:39 +00:00
|
|
|
|
snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]);
|
2006-04-19 11:26:11 +00:00
|
|
|
|
break;
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
default:
|
|
|
|
|
BUG();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return keyid_str;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
keystr_with_sub (u32 *main_kid, u32 *sub_kid)
|
|
|
|
|
{
|
|
|
|
|
static char buffer[KEYID_STR_SIZE+1+KEYID_STR_SIZE];
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
mem2str (buffer, keystr (main_kid), KEYID_STR_SIZE);
|
2010-10-01 20:33:53 +00:00
|
|
|
|
if (sub_kid)
|
|
|
|
|
{
|
|
|
|
|
p = buffer + strlen (buffer);
|
|
|
|
|
*p++ = '/';
|
|
|
|
|
mem2str (p, keystr (sub_kid), KEYID_STR_SIZE);
|
|
|
|
|
}
|
2010-08-31 15:58:39 +00:00
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
const char *
|
|
|
|
|
keystr_from_pk(PKT_public_key *pk)
|
|
|
|
|
{
|
|
|
|
|
keyid_from_pk(pk,NULL);
|
|
|
|
|
|
|
|
|
|
return keystr(pk->keyid);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
keystr_from_pk_with_sub (PKT_public_key *main_pk, PKT_public_key *sub_pk)
|
|
|
|
|
{
|
|
|
|
|
keyid_from_pk (main_pk, NULL);
|
2010-10-01 20:33:53 +00:00
|
|
|
|
if (sub_pk)
|
|
|
|
|
keyid_from_pk (sub_pk, NULL);
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
|
return keystr_with_sub (main_pk->keyid, sub_pk? sub_pk->keyid:NULL);
|
2010-08-31 15:58:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
const char *
|
|
|
|
|
keystr_from_desc(KEYDB_SEARCH_DESC *desc)
|
|
|
|
|
{
|
|
|
|
|
switch(desc->mode)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
/*
|
2003-06-05 07:14:21 +00:00
|
|
|
|
* 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
|
2010-09-06 19:57:42 +00:00
|
|
|
|
keyid_from_pk (PKT_public_key *pk, u32 *keyid)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2006-04-19 11:26:11 +00:00
|
|
|
|
u32 lowbits;
|
|
|
|
|
u32 dummy_keyid[2];
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
if (!keyid)
|
2006-04-19 11:26:11 +00:00
|
|
|
|
keyid = dummy_keyid;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
if( pk->keyid[0] || pk->keyid[1] )
|
|
|
|
|
{
|
|
|
|
|
keyid[0] = pk->keyid[0];
|
|
|
|
|
keyid[1] = pk->keyid[1];
|
|
|
|
|
lowbits = keyid[1];
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
else if( pk->version < 4 )
|
|
|
|
|
{
|
|
|
|
|
if( is_RSA(pk->pubkey_algo) )
|
|
|
|
|
{
|
|
|
|
|
lowbits = (pubkey_get_npkey (pk->pubkey_algo) ?
|
|
|
|
|
v3_keyid ( pk->pkey[0], keyid ) : 0); /* From n. */
|
|
|
|
|
pk->keyid[0] = keyid[0];
|
|
|
|
|
pk->keyid[1] = keyid[1];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const byte *dp;
|
|
|
|
|
gcry_md_hd_t md;
|
|
|
|
|
|
|
|
|
|
md = do_fingerprint_md(pk);
|
|
|
|
|
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);
|
|
|
|
|
pk->keyid[0] = keyid[0];
|
|
|
|
|
pk->keyid[1] = keyid[1];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
pk->keyid[0]=pk->keyid[1]=keyid[0]=keyid[1]=lowbits=0xFFFFFFFF;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
return lowbits;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
/*
|
2003-06-05 07:14:21 +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 )
|
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
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);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2010-09-06 19:57:42 +00:00
|
|
|
|
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] ;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
return keyid[1];
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u32
|
2010-09-06 19:57:42 +00:00
|
|
|
|
keyid_from_sig (PKT_signature *sig, u32 *keyid)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
if( keyid )
|
|
|
|
|
{
|
|
|
|
|
keyid[0] = sig->keyid[0];
|
|
|
|
|
keyid[1] = sig->keyid[1];
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2010-09-06 19:57:42 +00:00
|
|
|
|
return sig->keyid[1];
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
byte *
|
2010-09-06 19:57:42 +00:00
|
|
|
|
namehash_from_uid (PKT_user_id *uid)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2008-12-11 17:44:52 +00:00
|
|
|
|
if (!uid->namehash)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2008-12-11 17:44:52 +00:00
|
|
|
|
uid->namehash = xmalloc (20);
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
if (uid->attrib_data)
|
2008-12-11 17:44:52 +00:00
|
|
|
|
rmd160_hash_buffer (uid->namehash, uid->attrib_data, uid->attrib_len);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
else
|
2008-12-11 17:44:52 +00:00
|
|
|
|
rmd160_hash_buffer (uid->namehash, uid->name, uid->len);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
return uid->namehash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
/*
|
|
|
|
|
* Return the number of bits used in PK.
|
2003-06-05 07:14:21 +00:00
|
|
|
|
*/
|
2010-09-06 19:57:42 +00:00
|
|
|
|
unsigned int
|
|
|
|
|
nbits_from_pk (PKT_public_key *pk)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
return pubkey_nbits (pk->pubkey_algo, pk->pkey);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
static const char *
|
|
|
|
|
mk_datestr (char *buffer, time_t atime)
|
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
struct tm *tp;
|
|
|
|
|
|
2010-10-27 11:26:53 +00:00
|
|
|
|
if (IS_INVALID_TIME_T (atime))
|
|
|
|
|
strcpy (buffer, "????" "-??" "-??"); /* Mark this as invalid. */
|
2010-09-06 19:57:42 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tp = gmtime (&atime);
|
|
|
|
|
sprintf (buffer,"%04d-%02d-%02d",
|
|
|
|
|
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2010-09-06 19:57:42 +00:00
|
|
|
|
return buffer;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
|
|
|
|
/*
|
2003-06-05 07:14:21 +00:00
|
|
|
|
* 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 *
|
2010-09-06 19:57:42 +00:00
|
|
|
|
datestr_from_pk (PKT_public_key *pk)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
static char buffer[11+5];
|
|
|
|
|
time_t atime = pk->timestamp;
|
|
|
|
|
|
|
|
|
|
return mk_datestr (buffer, atime);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
2010-09-06 19:57:42 +00:00
|
|
|
|
datestr_from_sig (PKT_signature *sig )
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
static char buffer[11+5];
|
|
|
|
|
time_t atime = sig->timestamp;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
return mk_datestr (buffer, atime);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
2010-09-06 19:57:42 +00:00
|
|
|
|
expirestr_from_pk (PKT_public_key *pk)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
static char buffer[11+5];
|
|
|
|
|
time_t atime;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
if (!pk->expiredate)
|
|
|
|
|
return _("never ");
|
|
|
|
|
atime = pk->expiredate;
|
|
|
|
|
return mk_datestr (buffer, atime);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
const char *
|
2010-09-06 19:57:42 +00:00
|
|
|
|
expirestr_from_sig (PKT_signature *sig)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
static char buffer[11+5];
|
|
|
|
|
time_t atime;
|
|
|
|
|
|
|
|
|
|
if (!sig->expiredate)
|
|
|
|
|
return _("never ");
|
|
|
|
|
atime=sig->expiredate;
|
|
|
|
|
return mk_datestr (buffer, atime);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
const char *
|
|
|
|
|
revokestr_from_pk( PKT_public_key *pk )
|
|
|
|
|
{
|
2010-09-06 19:57:42 +00:00
|
|
|
|
static char buffer[11+5];
|
|
|
|
|
time_t atime;
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
if(!pk->revoked.date)
|
|
|
|
|
return _("never ");
|
|
|
|
|
atime=pk->revoked.date;
|
|
|
|
|
return mk_datestr (buffer, atime);
|
2006-04-19 11:26:11 +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 )
|
|
|
|
|
buffer[i++] = 'S';
|
|
|
|
|
|
|
|
|
|
if ( use & PUBKEY_USAGE_CERT )
|
|
|
|
|
buffer[i++] = 'C';
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
const char *
|
|
|
|
|
colon_strtime (u32 t)
|
|
|
|
|
{
|
2008-06-11 08:07:54 +00:00
|
|
|
|
static char buf[20];
|
|
|
|
|
|
|
|
|
|
if (!t)
|
|
|
|
|
return "";
|
|
|
|
|
snprintf (buf, sizeof buf, "%lu", (ulong)t);
|
|
|
|
|
return buf;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
colon_datestr_from_pk (PKT_public_key *pk)
|
|
|
|
|
{
|
2008-06-11 08:07:54 +00:00
|
|
|
|
static char buf[20];
|
|
|
|
|
|
|
|
|
|
snprintf (buf, sizeof buf, "%lu", (ulong)pk->timestamp);
|
|
|
|
|
return buf;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
colon_datestr_from_sig (PKT_signature *sig)
|
|
|
|
|
{
|
2008-06-11 08:07:54 +00:00
|
|
|
|
static char buf[20];
|
|
|
|
|
|
|
|
|
|
snprintf (buf, sizeof buf, "%lu", (ulong)sig->timestamp);
|
|
|
|
|
return buf;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
colon_expirestr_from_sig (PKT_signature *sig)
|
|
|
|
|
{
|
2008-06-11 08:07:54 +00:00
|
|
|
|
static char buf[20];
|
|
|
|
|
|
|
|
|
|
if (!sig->expiredate)
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
snprintf (buf, sizeof buf,"%lu", (ulong)sig->expiredate);
|
|
|
|
|
return buf;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-06 19:57:42 +00:00
|
|
|
|
/*
|
2003-06-05 07:14:21 +00:00
|
|
|
|
* 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 *
|
2010-09-06 19:57:42 +00:00
|
|
|
|
fingerprint_from_pk (PKT_public_key *pk, byte *array, size_t *ret_len)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2006-04-19 11:26:11 +00:00
|
|
|
|
byte *buf;
|
|
|
|
|
const byte *dp;
|
|
|
|
|
size_t len, nbytes;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if ( pk->version < 4 )
|
|
|
|
|
{
|
|
|
|
|
if ( is_RSA(pk->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 (pk->pubkey_algo) > 1 )
|
|
|
|
|
{
|
|
|
|
|
for (i=0; i < 2; i++)
|
|
|
|
|
{
|
|
|
|
|
if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0,
|
|
|
|
|
&nbytes, pk->pkey[i]))
|
|
|
|
|
BUG ();
|
|
|
|
|
/* fixme: Better allocate BUF on the stack */
|
|
|
|
|
buf = xmalloc (nbytes);
|
|
|
|
|
if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, nbytes,
|
|
|
|
|
NULL, pk->pkey[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);
|
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gcry_md_hd_t md;
|
|
|
|
|
|
|
|
|
|
md = do_fingerprint_md(pk);
|
|
|
|
|
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 );
|
|
|
|
|
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] ;
|
|
|
|
|
gcry_md_close( md);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
|
|
|
|
*ret_len = len;
|
|
|
|
|
return array;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-08-16 10:47:53 +00:00
|
|
|
|
|
|
|
|
|
|
2010-04-20 17:57:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the so called KEYGRIP which is the SHA-1 hash of the public
|
|
|
|
|
key parameters expressed as an canoncial encoded S-Exp. ARRAY must
|
|
|
|
|
be 20 bytes long. Returns 0 on sucess or an error code. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
keygrip_from_pk (PKT_public_key *pk, unsigned char *array)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
gcry_sexp_t s_pkey;
|
|
|
|
|
|
|
|
|
|
if (DBG_PACKET)
|
|
|
|
|
log_debug ("get_keygrip for public key\n");
|
|
|
|
|
|
|
|
|
|
switch (pk->pubkey_algo)
|
|
|
|
|
{
|
|
|
|
|
case GCRY_PK_DSA:
|
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
|
"(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
|
|
|
|
|
pk->pkey[0], pk->pkey[1],
|
|
|
|
|
pk->pkey[2], pk->pkey[3]);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GCRY_PK_ELG:
|
|
|
|
|
case GCRY_PK_ELG_E:
|
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
|
"(public-key(elg(p%m)(g%m)(y%m)))",
|
|
|
|
|
pk->pkey[0], pk->pkey[1], pk->pkey[2]);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GCRY_PK_RSA:
|
|
|
|
|
case GCRY_PK_RSA_S:
|
|
|
|
|
case GCRY_PK_RSA_E:
|
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
|
"(public-key(rsa(n%m)(e%m)))",
|
|
|
|
|
pk->pkey[0], pk->pkey[1]);
|
|
|
|
|
break;
|
|
|
|
|
|
2011-01-05 17:33:17 -08:00
|
|
|
|
case PUBKEY_ALGO_ECDSA:
|
|
|
|
|
case PUBKEY_ALGO_ECDH:
|
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
|
"(public-key(ecc(c%m)(q%m)))",
|
|
|
|
|
pk->pkey[0], pk->pkey[1]);
|
|
|
|
|
break;
|
|
|
|
|
/*
|
|
|
|
|
case PUBKEY_ALGO_ECDH:
|
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
|
"(public-key(ecdh(c%m)(q%m)(p%m)))",
|
|
|
|
|
pk->pkey[0], pk->pkey[1], pk->pkey[2]);
|
|
|
|
|
break;
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-20 17:57:50 +00:00
|
|
|
|
default:
|
|
|
|
|
err = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if (!gcry_pk_get_keygrip (s_pkey, array))
|
|
|
|
|
{
|
|
|
|
|
log_error ("error computing keygrip\n");
|
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (DBG_PACKET)
|
|
|
|
|
log_printhex ("keygrip=", array, 20);
|
|
|
|
|
/* FIXME: Save the keygrip in PK. */
|
|
|
|
|
}
|
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Store an allocated buffer with the keygrip of PK encoded as a
|
|
|
|
|
hexstring at r_GRIP. Returns 0 on success. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
hexkeygrip_from_pk (PKT_public_key *pk, char **r_grip)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
unsigned char grip[20];
|
|
|
|
|
|
|
|
|
|
*r_grip = NULL;
|
|
|
|
|
err = keygrip_from_pk (pk, grip);
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
char * buf = xtrymalloc (20*2+1);
|
|
|
|
|
if (!buf)
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bin2hex (grip, 20, buf);
|
|
|
|
|
*r_grip = buf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|