1 /* encr-data.c - process an encrypted data packet
2 * Copyright (C) 1998, 1999, 2000, 2001, 2005,
3 * 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
37 static int mdc_decode_filter( void *opaque
, int control
, IOBUF a
,
38 byte
*buf
, size_t *ret_len
);
39 static int decode_filter( void *opaque
, int control
, IOBUF a
,
40 byte
*buf
, size_t *ret_len
);
44 gcry_cipher_hd_t cipher_hd
;
45 gcry_md_hd_t mdc_hash
;
49 } decode_filter_ctx_t
;
53 * Decrypt the data, specified by ED with the key DEK.
56 decrypt_data( void *procctx
, PKT_encrypted
*ed
, DEK
*dek
)
58 decode_filter_ctx_t dfx
;
65 memset( &dfx
, 0, sizeof dfx
);
66 if( opt
.verbose
&& !dek
->algo_info_printed
) {
67 const char *s
= gcry_cipher_algo_name (dek
->algo
);
69 log_info(_("%s encrypted data\n"), s
);
71 log_info(_("encrypted with unknown algorithm %d\n"), dek
->algo
);
72 dek
->algo_info_printed
= 1;
74 rc
= openpgp_cipher_test_algo (dek
->algo
);
77 blocksize
= gcry_cipher_get_algo_blklen (dek
->algo
);
78 if( !blocksize
|| blocksize
> 16 )
79 log_fatal("unsupported blocksize %u\n", blocksize
);
81 if( ed
->len
&& ed
->len
< (nprefix
+2) )
84 if( ed
->mdc_method
) {
85 if (gcry_md_open (&dfx
.mdc_hash
, ed
->mdc_method
, 0 ))
88 gcry_md_start_debug (dfx
.mdc_hash
, "checkmdc");
91 rc
= gcry_cipher_open (&dfx
.cipher_hd
, dek
->algo
,
94 | ((ed
->mdc_method
|| dek
->algo
>= 100)?
95 0 : GCRY_CIPHER_ENABLE_SYNC
)));
98 /* We should never get an error here cause we already checked
99 * that the algorithm is available. */
104 /* log_hexdump( "thekey", dek->key, dek->keylen );*/
105 rc
= gcry_cipher_setkey (dfx
.cipher_hd
, dek
->key
, dek
->keylen
);
106 if ( gpg_err_code (rc
) == GPG_ERR_WEAK_KEY
)
108 log_info(_("WARNING: message was encrypted with"
109 " a weak key in the symmetric cipher.\n"));
114 log_error("key setup failed: %s\n", g10_errstr(rc
) );
119 log_error(_("problem handling encrypted packet\n"));
123 gcry_cipher_setiv (dfx
.cipher_hd
, NULL
, 0);
126 for(i
=0; i
< (nprefix
+2) && ed
->len
; i
++, ed
->len
-- ) {
127 if( (c
=iobuf_get(ed
->buf
)) == -1 )
134 for(i
=0; i
< (nprefix
+2); i
++ )
135 if( (c
=iobuf_get(ed
->buf
)) == -1 )
141 gcry_cipher_decrypt (dfx
.cipher_hd
, temp
, nprefix
+2, NULL
, 0);
142 gcry_cipher_sync (dfx
.cipher_hd
);
144 /* log_hexdump( "prefix", temp, nprefix+2 ); */
146 && (p
[nprefix
-2] != p
[nprefix
] || p
[nprefix
-1] != p
[nprefix
+1]) )
148 rc
= GPG_ERR_BAD_KEY
;
153 gcry_md_write (dfx
.mdc_hash
, temp
, nprefix
+2);
156 iobuf_push_filter( ed
->buf
, mdc_decode_filter
, &dfx
);
158 iobuf_push_filter( ed
->buf
, decode_filter
, &dfx
);
160 proc_packets( procctx
, ed
->buf
);
162 if( ed
->mdc_method
&& dfx
.eof_seen
== 2 )
163 rc
= gpg_error (GPG_ERR_INV_PACKET
);
164 else if( ed
->mdc_method
) { /* check the mdc */
165 int datalen
= gcry_md_get_algo_dlen (ed
->mdc_method
);
167 gcry_cipher_decrypt (dfx
.cipher_hd
, dfx
.defer
, 20, NULL
, 0);
168 gcry_md_final (dfx
.mdc_hash
);
170 || memcmp (gcry_md_read( dfx
.mdc_hash
, 0 ), dfx
.defer
, datalen
) )
171 rc
= gpg_error (GPG_ERR_BAD_SIGNATURE
);
172 /*log_hexdump("MDC calculated:", md_read( dfx.mdc_hash, 0), datalen);*/
173 /*log_hexdump("MDC message :", dfx.defer, 20);*/
178 gcry_cipher_close (dfx
.cipher_hd
);
179 gcry_md_close (dfx
.mdc_hash
);
185 /* I think we should merge this with cipher_filter */
187 mdc_decode_filter( void *opaque
, int control
, IOBUF a
,
188 byte
*buf
, size_t *ret_len
)
190 decode_filter_ctx_t
*dfx
= opaque
;
191 size_t n
, size
= *ret_len
;
195 if( control
== IOBUFCTRL_UNDERFLOW
&& dfx
->eof_seen
) {
199 else if( control
== IOBUFCTRL_UNDERFLOW
) {
203 /* get at least 20 bytes and put it somewhere ahead in the buffer */
204 for(n
=20; n
< 40 ; n
++ ) {
205 if( (c
= iobuf_get(a
)) == -1 )
210 /* we have enough stuff - flush the deferred stuff */
211 /* (we have asserted that the buffer is large enough) */
212 if( !dfx
->defer_filled
) { /* the first time */
213 memcpy(buf
, buf
+20, 20 );
217 memcpy(buf
, dfx
->defer
, 20 );
220 for(; n
< size
; n
++ ) {
221 if( (c
= iobuf_get(a
)) == -1 )
225 /* move the last 20 bytes back to the defer buffer */
226 /* (okay, we are wasting 20 bytes of supplied buffer) */
228 memcpy( dfx
->defer
, buf
+n
, 20 );
229 dfx
->defer_filled
= 1;
231 else if( !dfx
->defer_filled
) { /* eof seen buf empty defer */
232 /* this is bad because there is an incomplete hash */
234 memcpy(buf
, buf
+20, n
);
235 dfx
->eof_seen
= 2; /* eof with incomplete hash */
237 else { /* eof seen */
238 memcpy(buf
, dfx
->defer
, 20 );
240 memcpy( dfx
->defer
, buf
+n
, 20 );
241 dfx
->eof_seen
= 1; /* normal eof */
245 gcry_cipher_decrypt (dfx
->cipher_hd
, buf
, n
, NULL
, 0);
246 gcry_md_write (dfx
->mdc_hash
, buf
, n
);
249 assert( dfx
->eof_seen
);
254 else if( control
== IOBUFCTRL_DESC
) {
255 *(char**)buf
= "mdc_decode_filter";
261 decode_filter( void *opaque
, int control
, IOBUF a
, byte
*buf
, size_t *ret_len
)
263 decode_filter_ctx_t
*fc
= opaque
;
264 size_t n
, size
= *ret_len
;
267 if( control
== IOBUFCTRL_UNDERFLOW
) {
269 n
= iobuf_read( a
, buf
, size
);
272 gcry_cipher_decrypt (fc
->cipher_hd
, buf
, n
, NULL
, 0);
277 else if( control
== IOBUFCTRL_DESC
) {
278 *(char**)buf
= "decode_filter";