1997-11-18 15:06:00 +01:00
|
|
|
/* pubkey-enc.c - public key encoded packet handling
|
2002-04-29 16:42:34 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 15:06:00 +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-12-23 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 15:06:00 +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 <assert.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "mpi.h"
|
|
|
|
#include "keydb.h"
|
1998-08-05 18:51:59 +02:00
|
|
|
#include "trustdb.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "cipher.h"
|
1998-05-29 13:53:54 +02:00
|
|
|
#include "status.h"
|
1999-02-10 17:22:40 +01:00
|
|
|
#include "options.h"
|
2001-12-19 00:10:46 +01:00
|
|
|
#include "main.h"
|
1998-08-08 21:27:00 +02:00
|
|
|
#include "i18n.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-09-11 07:47:32 +02:00
|
|
|
static int get_it( PKT_pubkey_enc *k,
|
|
|
|
DEK *dek, PKT_secret_key *sk, u32 *keyid );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2001-08-10 16:04:32 +02:00
|
|
|
|
|
|
|
/* check that the given algo is mentioned in one of the valid user IDs */
|
|
|
|
static int
|
|
|
|
is_algo_in_prefs ( KBNODE keyblock, preftype_t type, int algo )
|
|
|
|
{
|
|
|
|
KBNODE k;
|
|
|
|
|
|
|
|
for (k=keyblock; k; k=k->next) {
|
|
|
|
if (k->pkt->pkttype == PKT_USER_ID) {
|
|
|
|
PKT_user_id *uid = k->pkt->pkt.user_id;
|
|
|
|
prefitem_t *prefs = uid->prefs;
|
|
|
|
|
|
|
|
if (uid->created && !uid->is_revoked && prefs ) {
|
|
|
|
for (; prefs->type; prefs++ )
|
|
|
|
if (prefs->type == type && prefs->value == algo)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/****************
|
|
|
|
* Get the session key from a pubkey enc paket and return
|
|
|
|
* it in DEK, which should have been allocated in secure memory.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
get_session_key( PKT_pubkey_enc *k, DEK *dek )
|
|
|
|
{
|
1998-09-11 07:47:32 +02:00
|
|
|
PKT_secret_key *sk = NULL;
|
|
|
|
int rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2001-12-15 19:27:13 +01:00
|
|
|
rc = check_pubkey_algo2 (k->pubkey_algo, PUBKEY_USAGE_ENC);
|
1998-06-11 09:16:50 +02:00
|
|
|
if( rc )
|
|
|
|
goto leave;
|
1998-05-29 13:53:54 +02:00
|
|
|
|
2000-08-01 20:06:27 +02:00
|
|
|
if( (k->keyid[0] || k->keyid[1]) && !opt.try_all_secrets ) {
|
1998-09-11 07:47:32 +02:00
|
|
|
sk = m_alloc_clear( sizeof *sk );
|
|
|
|
sk->pubkey_algo = k->pubkey_algo; /* we want a pubkey with this algo*/
|
|
|
|
if( !(rc = get_seckey( sk, k->keyid )) )
|
|
|
|
rc = get_it( k, dek, sk, k->keyid );
|
|
|
|
}
|
|
|
|
else { /* anonymous receiver: Try all available secret keys */
|
|
|
|
void *enum_context = NULL;
|
|
|
|
u32 keyid[2];
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if( sk )
|
|
|
|
free_secret_key( sk );
|
|
|
|
sk = m_alloc_clear( sizeof *sk );
|
|
|
|
rc=enum_secret_keys( &enum_context, sk, 1);
|
|
|
|
if( rc ) {
|
|
|
|
rc = G10ERR_NO_SECKEY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( sk->pubkey_algo != k->pubkey_algo )
|
|
|
|
continue;
|
|
|
|
keyid_from_sk( sk, keyid );
|
2001-12-18 13:16:07 +01:00
|
|
|
log_info(_("anonymous recipient; trying secret key %08lX ...\n"),
|
|
|
|
(ulong)keyid[1] );
|
1998-09-11 07:47:32 +02:00
|
|
|
rc = check_secret_key( sk, 1 ); /* ask only once */
|
|
|
|
if( !rc )
|
|
|
|
rc = get_it( k, dek, sk, keyid );
|
|
|
|
if( !rc ) {
|
1998-12-29 14:47:31 +01:00
|
|
|
log_info(_("okay, we are the anonymous recipient.\n") );
|
1998-09-11 07:47:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enum_secret_keys( &enum_context, NULL, 0 ); /* free context */
|
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
if( sk )
|
|
|
|
free_secret_key( sk );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2001-09-09 18:09:19 +02:00
|
|
|
get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
|
1998-09-11 07:47:32 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
MPI plain_dek = NULL;
|
|
|
|
byte *frame = NULL;
|
|
|
|
unsigned n, nframe;
|
|
|
|
u16 csum, csum2;
|
1997-11-24 23:24:04 +01:00
|
|
|
|
2001-09-09 18:09:19 +02:00
|
|
|
rc = pubkey_decrypt(sk->pubkey_algo, &plain_dek, enc->data, sk->skey );
|
1998-06-13 08:59:14 +02:00
|
|
|
if( rc )
|
1997-11-18 15:06:00 +01:00
|
|
|
goto leave;
|
1998-02-13 21:58:50 +01:00
|
|
|
frame = mpi_get_buffer( plain_dek, &nframe, NULL );
|
|
|
|
mpi_free( plain_dek ); plain_dek = NULL;
|
1997-11-24 23:24:04 +01:00
|
|
|
|
1998-02-13 21:58:50 +01:00
|
|
|
/* Now get the DEK (data encryption key) from the frame
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
|
|
|
* Old versions encode the DEK in in this format (msb is left):
|
|
|
|
*
|
|
|
|
* 0 1 DEK(16 bytes) CSUM(2 bytes) 0 RND(n bytes) 2
|
|
|
|
*
|
|
|
|
* Later versions encode the DEK like this:
|
|
|
|
*
|
|
|
|
* 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
|
|
|
|
*
|
1998-04-07 20:16:10 +02:00
|
|
|
* (mpi_get_buffer already removed the leading zero).
|
|
|
|
*
|
1997-11-18 15:06:00 +01:00
|
|
|
* RND are non-zero randow bytes.
|
1997-11-24 12:04:11 +01:00
|
|
|
* A is the cipher algorithm
|
1997-11-18 15:06:00 +01:00
|
|
|
* DEK is the encryption key (session key) with length k
|
|
|
|
* CSUM
|
|
|
|
*/
|
|
|
|
if( DBG_CIPHER )
|
1998-02-13 21:58:50 +01:00
|
|
|
log_hexdump("DEK frame:", frame, nframe );
|
1998-04-07 20:16:10 +02:00
|
|
|
n=0;
|
1998-02-13 21:58:50 +01:00
|
|
|
if( n + 7 > nframe )
|
1997-11-18 15:06:00 +01:00
|
|
|
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
|
1998-02-13 21:58:50 +01:00
|
|
|
if( frame[n] == 1 && frame[nframe-1] == 2 ) {
|
1998-11-10 13:59:59 +01:00
|
|
|
log_info(_("old encoding of the DEK is not supported\n"));
|
1997-11-18 15:06:00 +01:00
|
|
|
rc = G10ERR_CIPHER_ALGO;
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-02-13 21:58:50 +01:00
|
|
|
if( frame[n] != 2 ) /* somethink is wrong */
|
1997-11-18 15:06:00 +01:00
|
|
|
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
|
1998-02-13 21:58:50 +01:00
|
|
|
for(n++; n < nframe && frame[n]; n++ ) /* skip the random bytes */
|
|
|
|
;
|
|
|
|
n++; /* and the zero byte */
|
|
|
|
if( n + 4 > nframe )
|
1997-11-18 15:06:00 +01:00
|
|
|
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
|
1998-02-13 21:58:50 +01:00
|
|
|
|
|
|
|
dek->keylen = nframe - (n+1) - 2;
|
|
|
|
dek->algo = frame[n++];
|
1998-06-13 08:59:14 +02:00
|
|
|
if( dek->algo == CIPHER_ALGO_IDEA )
|
1998-05-29 13:53:54 +02:00
|
|
|
write_status(STATUS_RSA_OR_IDEA);
|
1998-06-13 08:59:14 +02:00
|
|
|
rc = check_cipher_algo( dek->algo );
|
|
|
|
if( rc ) {
|
2000-03-02 15:36:39 +01:00
|
|
|
if( !opt.quiet && rc == G10ERR_CIPHER_ALGO ) {
|
2001-11-08 17:24:04 +01:00
|
|
|
log_info(_("cipher algorithm %d%s is unknown or disabled\n"),
|
|
|
|
dek->algo, dek->algo == CIPHER_ALGO_IDEA? " (IDEA)":"");
|
2001-12-19 00:10:46 +01:00
|
|
|
if(dek->algo==CIPHER_ALGO_IDEA)
|
|
|
|
idea_cipher_warn(0);
|
2000-03-02 15:36:39 +01:00
|
|
|
}
|
1998-02-13 21:58:50 +01:00
|
|
|
dek->algo = 0;
|
1997-11-18 15:06:00 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
1998-06-13 08:59:14 +02:00
|
|
|
if( (dek->keylen*8) != cipher_get_keylen( dek->algo ) ) {
|
|
|
|
rc = G10ERR_WRONG_SECKEY;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/* copy the key to DEK and compare the checksum */
|
1998-02-13 21:58:50 +01:00
|
|
|
csum = frame[nframe-2] << 8;
|
|
|
|
csum |= frame[nframe-1];
|
|
|
|
memcpy( dek->key, frame+n, dek->keylen );
|
|
|
|
for( csum2=0, n=0; n < dek->keylen; n++ )
|
|
|
|
csum2 += dek->key[n];
|
1997-11-18 15:06:00 +01:00
|
|
|
if( csum != csum2 ) {
|
|
|
|
rc = G10ERR_WRONG_SECKEY;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( DBG_CIPHER )
|
|
|
|
log_hexdump("DEK is:", dek->key, dek->keylen );
|
2000-03-16 16:16:15 +01:00
|
|
|
/* check that the algo is in the preferences and whether it has expired */
|
1998-08-05 18:51:59 +02:00
|
|
|
{
|
2001-08-10 16:04:32 +02:00
|
|
|
PKT_public_key *pk = NULL;
|
|
|
|
KBNODE pkb = get_pubkeyblock (keyid);
|
|
|
|
|
|
|
|
if( !pkb ) {
|
|
|
|
rc = -1;
|
|
|
|
log_error("oops: public key not found for preference check\n");
|
|
|
|
}
|
2002-01-25 00:54:36 +01:00
|
|
|
else if( pkb->pkt->pkt.public_key->selfsigversion > 3
|
|
|
|
&& dek->algo != CIPHER_ALGO_3DES
|
2001-08-10 16:04:32 +02:00
|
|
|
&& !is_algo_in_prefs( pkb, PREFTYPE_SYM, dek->algo ) ) {
|
1999-02-10 17:22:40 +01:00
|
|
|
/* Don't print a note while we are not on verbose mode,
|
|
|
|
* the cipher is blowfish and the preferences have twofish
|
|
|
|
* listed */
|
|
|
|
if( opt.verbose || dek->algo != CIPHER_ALGO_BLOWFISH
|
2001-08-10 16:04:32 +02:00
|
|
|
|| !is_algo_in_prefs( pkb, PREFTYPE_SYM, CIPHER_ALGO_TWOFISH))
|
1999-02-10 17:22:40 +01:00
|
|
|
log_info(_(
|
|
|
|
"NOTE: cipher algorithm %d not found in preferences\n"),
|
1998-08-05 18:51:59 +02:00
|
|
|
dek->algo );
|
1999-02-10 17:22:40 +01:00
|
|
|
}
|
2000-03-16 16:16:15 +01:00
|
|
|
|
2001-08-10 16:04:32 +02:00
|
|
|
if (!rc) {
|
|
|
|
KBNODE k;
|
|
|
|
|
|
|
|
for (k=pkb; k; k = k->next) {
|
|
|
|
if (k->pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
|| k->pkt->pkttype == PKT_PUBLIC_SUBKEY){
|
|
|
|
u32 aki[2];
|
|
|
|
keyid_from_pk(k->pkt->pkt.public_key, aki);
|
2000-03-16 16:16:15 +01:00
|
|
|
|
2001-08-10 16:04:32 +02:00
|
|
|
if (aki[0]==keyid[0] && aki[1]==keyid[1]) {
|
|
|
|
pk = k->pkt->pkt.public_key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!pk)
|
|
|
|
BUG ();
|
|
|
|
if ( pk->expiredate && pk->expiredate <= make_timestamp() ) {
|
|
|
|
log_info(_("NOTE: secret key %08lX expired at %s\n"),
|
|
|
|
(ulong)keyid[1], asctimestamp( pk->expiredate) );
|
|
|
|
}
|
|
|
|
}
|
2000-03-16 16:16:15 +01:00
|
|
|
|
2002-05-31 11:23:24 +02:00
|
|
|
if ( pk->is_revoked ) {
|
|
|
|
log_info( _("NOTE: key has been revoked") );
|
|
|
|
putc( '\n', log_stream() );
|
|
|
|
show_revocation_reason( pk, 1 );
|
|
|
|
}
|
|
|
|
|
2001-08-10 16:04:32 +02:00
|
|
|
release_kbnode (pkb);
|
1998-08-05 18:51:59 +02:00
|
|
|
rc = 0;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2000-03-16 16:16:15 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
leave:
|
1998-02-13 21:58:50 +01:00
|
|
|
mpi_free(plain_dek);
|
|
|
|
m_free(frame);
|
1997-11-18 15:06:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-27 12:01:27 +02:00
|
|
|
/****************
|
|
|
|
* Get the session key from the given string.
|
|
|
|
* String is supposed to be formatted as this:
|
|
|
|
* <algo-id>:<even-number-of-hex-digits>
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
get_override_session_key( DEK *dek, const char *string )
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( !string )
|
|
|
|
return G10ERR_BAD_KEY;
|
|
|
|
dek->algo = atoi(string);
|
|
|
|
if ( dek->algo < 1 )
|
|
|
|
return G10ERR_BAD_KEY;
|
|
|
|
if ( !(s = strchr ( string, ':' )) )
|
|
|
|
return G10ERR_BAD_KEY;
|
|
|
|
s++;
|
|
|
|
for(i=0; i < DIM(dek->key) && *s; i++, s +=2 ) {
|
|
|
|
int c = hextobyte ( s );
|
|
|
|
if (c == -1)
|
|
|
|
return G10ERR_BAD_KEY;
|
|
|
|
dek->key[i] = c;
|
|
|
|
}
|
|
|
|
if ( *s )
|
|
|
|
return G10ERR_BAD_KEY;
|
|
|
|
dek->keylen = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|