1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-02 22:38:02 +02:00
gnupg/g10/pubkey-enc.c

161 lines
4.5 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* pubkey-enc.c - public key encoded packet handling
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +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-02-24 19:50:46 +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"
#include "cipher.h"
1998-05-29 13:53:54 +02:00
#include "status.h"
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-02-13 21:58:50 +01:00
int rc = 0;
MPI plain_dek = NULL;
byte *frame = NULL;
unsigned n, nframe;
1997-11-18 15:06:00 +01:00
u16 csum, csum2;
1997-12-01 11:33:23 +01:00
PKT_secret_cert *skc = m_alloc_clear( sizeof *skc );
1997-11-18 15:06:00 +01:00
1998-05-29 13:53:54 +02:00
#ifndef HAVE_RSA_CIPHER
if( is_RSA(k->pubkey_algo) )
write_status(STATUS_RSA_OR_IDEA);
#endif
1997-11-24 23:24:04 +01:00
skc->pubkey_algo = k->pubkey_algo; /* we want a pubkey with this algo*/
if( (rc = get_seckey( skc, k->keyid )) )
1997-11-18 15:06:00 +01:00
goto leave;
1997-11-24 23:24:04 +01:00
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(k->pubkey_algo) ) {
1997-11-24 23:24:04 +01:00
if( DBG_CIPHER ) {
log_mpidump("Encr DEK a:", k->d.elg.a );
log_mpidump(" DEK b:", k->d.elg.b );
}
1998-04-08 21:49:02 +02:00
plain_dek = mpi_alloc_secure( mpi_get_nlimbs(skc->d.elg.p) );
elg_decrypt( plain_dek, k->d.elg.a, k->d.elg.b, &skc->d.elg );
1997-11-18 15:06:00 +01:00
}
1997-11-26 23:02:28 +01:00
#ifdef HAVE_RSA_CIPHER
1998-05-29 13:53:54 +02:00
else if( is_RSA(k->pubkey_algo) ) {
1997-11-24 23:24:04 +01:00
if( DBG_CIPHER )
log_mpidump("Encr DEK frame:", k->d.rsa.rsa_integer );
1998-04-08 21:49:02 +02:00
plain_dek = mpi_alloc_secure( mpi_get_nlimbs(skc->d.rsa.n) );
rsa_secret( plain_dek, k->d.rsa.rsa_integer, &skc->d.rsa );
1997-11-24 23:24:04 +01:00
}
1997-11-26 23:02:28 +01:00
#endif/*HAVE_RSA_CIPHER*/
1997-11-24 23:24:04 +01:00
else {
rc = G10ERR_PUBKEY_ALGO; /* unsupported algorithm */
1997-11-18 15:06:00 +01:00
goto leave;
1997-11-24 23:24:04 +01:00
}
1997-12-01 11:33:23 +01:00
free_secret_cert( skc ); skc = NULL;
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 ) {
1997-11-18 15:06:00 +01:00
log_error("old encoding of DEK is not supported\n");
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++];
switch( dek->algo ) {
1997-11-24 23:24:04 +01:00
case CIPHER_ALGO_IDEA:
1998-05-29 13:53:54 +02:00
write_status(STATUS_RSA_OR_IDEA);
1997-11-18 15:06:00 +01:00
rc = G10ERR_NI_CIPHER;
goto leave;
1998-04-30 16:06:01 +02:00
case CIPHER_ALGO_BLOWFISH160:
1998-02-13 21:58:50 +01:00
if( dek->keylen != 20 )
1997-11-18 15:06:00 +01:00
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
break;
1998-04-30 16:06:01 +02:00
case CIPHER_ALGO_BLOWFISH:
1998-06-09 17:14:06 +02:00
case CIPHER_ALGO_CAST5:
1998-02-13 21:58:50 +01:00
if( dek->keylen != 16 )
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
break;
1997-11-18 15:06:00 +01:00
default:
1998-02-13 21:58:50 +01:00
dek->algo = 0;
1997-11-18 15:06:00 +01:00
rc = G10ERR_CIPHER_ALGO;
goto leave;
}
/* 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 );
leave:
1998-02-13 21:58:50 +01:00
mpi_free(plain_dek);
m_free(frame);
1997-11-24 23:24:04 +01:00
if( skc )
1997-12-01 11:33:23 +01:00
free_secret_cert( skc );
1997-11-18 15:06:00 +01:00
return rc;
}