summaryrefslogtreecommitdiffstats
path: root/g10/pubkey-enc.c
blob: 0fcd9c21c53fdf10c0262ce87cbccbf953e6f875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* pubkey-enc.c -  public key encoded packet handling
 *	Copyright (c) 1997 by Werner Koch (dd9jn)
 *
 * This file is part of G10.
 *
 * G10 is free software; you can redistribute it and/or modify
 * 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.
 *
 * G10 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
 * 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"


/****************
 * 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 )
{
    int rc = 0;
    MPI plain_dek  = NULL;
    byte *frame = NULL;
    unsigned n, nframe;
    u16 csum, csum2;
    PKT_secret_cert *skc = m_alloc_clear( sizeof *skc );

    skc->pubkey_algo = k->pubkey_algo;	 /* we want a pubkey with this algo*/
    if( (rc = get_seckey( skc, k->keyid )) )
	goto leave;

    if( k->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
	ELG_secret_key skey;

	if( DBG_CIPHER ) {
	    log_mpidump("Encr DEK a:", k->d.elg.a );
	    log_mpidump("     DEK b:", k->d.elg.b );
	}
	skey.p = skc->d.elg.p;
	skey.g = skc->d.elg.g;
	skey.y = skc->d.elg.y;
	skey.x = skc->d.elg.x;
	plain_dek = mpi_alloc_secure( mpi_get_nlimbs(skey.p) );
	elg_decrypt( plain_dek, k->d.elg.a, k->d.elg.b, &skey );
	memset( &skey, 0, sizeof skey );
    }
  #ifdef HAVE_RSA_CIPHER
    else if( k->pubkey_algo == PUBKEY_ALGO_RSA ) {
	RSA_secret_key skey;

	if( DBG_CIPHER )
	    log_mpidump("Encr DEK frame:", k->d.rsa.rsa_integer );

	skey.e = skc->d.rsa.rsa_e;
	skey.n = skc->d.rsa.rsa_n;
	skey.p = skc->d.rsa.rsa_p;
	skey.q = skc->d.rsa.rsa_q;
	skey.d = skc->d.rsa.rsa_d;
	skey.u = skc->d.rsa.rsa_u;
	plain_dek = mpi_alloc_secure( mpi_get_nlimbs(skey.n) );
	rsa_secret( plain_dek, k->d.rsa.rsa_integer, &skey );
	memset( &skey, 0, sizeof skey );
    }
  #endif/*HAVE_RSA_CIPHER*/
    else {
	rc = G10ERR_PUBKEY_ALGO; /* unsupported algorithm */
	goto leave;
    }
    free_secret_cert( skc ); skc = NULL;
    frame = mpi_get_buffer( plain_dek, &nframe, NULL );
    mpi_free( plain_dek ); plain_dek = NULL;

    /* Now get the DEK (data encryption key) from the frame
     *
     * 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)
     *
     * RND are non-zero randow bytes.
     * A   is the cipher algorithm
     * DEK is the encryption key (session key) with length k
     * CSUM
     */
    if( DBG_CIPHER )
	log_hexdump("DEK frame:", frame, nframe );
    for(n=0; n < nframe && !frame[n]; n++ ) /* skip leading zeroes */
	;
    if( n + 7 > nframe )
	{ rc = G10ERR_WRONG_SECKEY; goto leave; }
    if( frame[n] == 1 && frame[nframe-1] == 2 ) {
	log_error("old encoding of DEK is not supported\n");
	rc = G10ERR_CIPHER_ALGO;
	goto leave;
    }
    if( frame[n] != 2 )  /* somethink is wrong */
	{ rc = G10ERR_WRONG_SECKEY; goto leave; }
    for(n++; n < nframe && frame[n]; n++ ) /* skip the random bytes */
	;
    n++; /* and the zero byte */
    if( n + 4 > nframe )
	{ rc = G10ERR_WRONG_SECKEY; goto leave; }

    dek->keylen = nframe - (n+1) - 2;
    dek->algo = frame[n++];
    switch( dek->algo ) {
      case CIPHER_ALGO_IDEA:
	rc = G10ERR_NI_CIPHER;
	goto leave;
      case CIPHER_ALGO_BLOWFISH:
	if( dek->keylen != 20 )
	    { rc = G10ERR_WRONG_SECKEY; goto leave; }
	break;
      case CIPHER_ALGO_BLOWFISH128:
	if( dek->keylen != 16 )
	    { rc = G10ERR_WRONG_SECKEY; goto leave; }
	break;
      case CIPHER_ALGO_CAST:
	if( dek->keylen < 5 || dek->keylen > 16 )
	    { rc = G10ERR_WRONG_SECKEY; goto leave; }
	break;
      default:
	dek->algo = 0;
	rc = G10ERR_CIPHER_ALGO;
	goto leave;
    }
    /* copy the key to DEK and compare the checksum */
    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];
    if( csum != csum2 ) {
	rc = G10ERR_WRONG_SECKEY;
	goto leave;
    }
    if( DBG_CIPHER )
	log_hexdump("DEK is:", dek->key, dek->keylen );

  leave:
    mpi_free(plain_dek);
    m_free(frame);
    if( skc )
	free_secret_cert( skc );
    return rc;
}