/src/gnupg/g10/decrypt-data.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* decrypt-data.c - Decrypt an encrypted data packet |
2 | | * Copyright (C) 1998-2001, 2005-2006, 2009 Free Software Foundation, Inc. |
3 | | * Copyright (C) 1998-2001, 2005-2006, 2009, 2018 Werner Koch |
4 | | * |
5 | | * This file is part of GnuPG. |
6 | | * |
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 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
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. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
19 | | * SPDX-License-Identifier: GPL-3.0-or-later |
20 | | */ |
21 | | |
22 | | #include <config.h> |
23 | | #include <stdio.h> |
24 | | #include <stdlib.h> |
25 | | #include <string.h> |
26 | | |
27 | | #include "gpg.h" |
28 | | #include "../common/util.h" |
29 | | #include "packet.h" |
30 | | #include "options.h" |
31 | | #include "../common/i18n.h" |
32 | | #include "../common/status.h" |
33 | | #include "../common/compliance.h" |
34 | | |
35 | | |
36 | | static int aead_decode_filter (void *opaque, int control, iobuf_t a, |
37 | | byte *buf, size_t *ret_len); |
38 | | static int mdc_decode_filter ( void *opaque, int control, IOBUF a, |
39 | | byte *buf, size_t *ret_len); |
40 | | static int decode_filter ( void *opaque, int control, IOBUF a, |
41 | | byte *buf, size_t *ret_len); |
42 | | |
43 | | /* Our context object. */ |
44 | | struct decode_filter_context_s |
45 | | { |
46 | | /* Redcounter (max value is 2). We need it because we do not know |
47 | | * whether the iobuf or the outer control code frees this object |
48 | | * first. */ |
49 | | int refcount; |
50 | | |
51 | | /* The cipher handle. */ |
52 | | gcry_cipher_hd_t cipher_hd; |
53 | | |
54 | | /* The hash handle for use in MDC mode. */ |
55 | | gcry_md_hd_t mdc_hash; |
56 | | |
57 | | /* The start IV for AEAD encryption. */ |
58 | | byte startiv[16]; |
59 | | |
60 | | /* The holdback buffer and its used length. For AEAD we need 32+1 |
61 | | * bytes but we use 48 byte. For MDC we need 22 bytes; here |
62 | | * holdbacklen will either 0 or 22. */ |
63 | | char holdback[48]; |
64 | | unsigned int holdbacklen; |
65 | | |
66 | | /* Working on a partial length packet. */ |
67 | | unsigned int partial : 1; |
68 | | |
69 | | /* EOF indicator with these true values: |
70 | | * 1 = normal EOF |
71 | | * 2 = premature EOF (tag or hash incomplete) |
72 | | * 3 = premature EOF (general) */ |
73 | | unsigned int eof_seen : 2; |
74 | | |
75 | | /* The actually used cipher algo for AEAD. */ |
76 | | byte cipher_algo; |
77 | | |
78 | | /* The AEAD algo. */ |
79 | | byte aead_algo; |
80 | | |
81 | | /* The encoded chunk byte for AEAD. */ |
82 | | byte chunkbyte; |
83 | | |
84 | | /* The decoded CHUNKBYTE. */ |
85 | | uint64_t chunksize; |
86 | | |
87 | | /* The chunk index for AEAD. */ |
88 | | uint64_t chunkindex; |
89 | | |
90 | | /* The number of bytes in the current chunk. */ |
91 | | uint64_t chunklen; |
92 | | |
93 | | /* The total count of decrypted plaintext octets. */ |
94 | | uint64_t total; |
95 | | |
96 | | /* Remaining bytes in the packet according to the packet header. |
97 | | * Not used if PARTIAL is true. */ |
98 | | size_t length; |
99 | | }; |
100 | | typedef struct decode_filter_context_s *decode_filter_ctx_t; |
101 | | |
102 | | |
103 | | /* Helper to release the decode context. */ |
104 | | static void |
105 | | release_dfx_context (decode_filter_ctx_t dfx) |
106 | 0 | { |
107 | 0 | if (!dfx) |
108 | 0 | return; |
109 | | |
110 | 0 | log_assert (dfx->refcount); |
111 | 0 | if ( !--dfx->refcount ) |
112 | 0 | { |
113 | 0 | gcry_cipher_close (dfx->cipher_hd); |
114 | 0 | dfx->cipher_hd = NULL; |
115 | 0 | gcry_md_close (dfx->mdc_hash); |
116 | 0 | dfx->mdc_hash = NULL; |
117 | 0 | xfree (dfx); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | |
122 | | /* Set the nonce and the additional data for the current chunk. This |
123 | | * also reset the decryption machinery so that the handle can be |
124 | | * used for a new chunk. If FINAL is set the final AEAD chunk is |
125 | | * processed. */ |
126 | | static gpg_error_t |
127 | | aead_set_nonce_and_ad (decode_filter_ctx_t dfx, int final) |
128 | 0 | { |
129 | 0 | gpg_error_t err; |
130 | 0 | unsigned char ad[21]; |
131 | 0 | unsigned char nonce[16]; |
132 | 0 | int i; |
133 | |
|
134 | 0 | switch (dfx->aead_algo) |
135 | 0 | { |
136 | 0 | case AEAD_ALGO_OCB: |
137 | 0 | memcpy (nonce, dfx->startiv, 15); |
138 | 0 | i = 7; |
139 | 0 | break; |
140 | | |
141 | 0 | case AEAD_ALGO_EAX: |
142 | 0 | memcpy (nonce, dfx->startiv, 16); |
143 | 0 | i = 8; |
144 | 0 | break; |
145 | | |
146 | 0 | default: |
147 | 0 | BUG (); |
148 | 0 | } |
149 | 0 | nonce[i++] ^= dfx->chunkindex >> 56; |
150 | 0 | nonce[i++] ^= dfx->chunkindex >> 48; |
151 | 0 | nonce[i++] ^= dfx->chunkindex >> 40; |
152 | 0 | nonce[i++] ^= dfx->chunkindex >> 32; |
153 | 0 | nonce[i++] ^= dfx->chunkindex >> 24; |
154 | 0 | nonce[i++] ^= dfx->chunkindex >> 16; |
155 | 0 | nonce[i++] ^= dfx->chunkindex >> 8; |
156 | 0 | nonce[i++] ^= dfx->chunkindex; |
157 | |
|
158 | 0 | if (DBG_CRYPTO) |
159 | 0 | log_printhex (nonce, i, "nonce:"); |
160 | 0 | err = gcry_cipher_setiv (dfx->cipher_hd, nonce, i); |
161 | 0 | if (err) |
162 | 0 | return err; |
163 | | |
164 | 0 | ad[0] = (0xc0 | PKT_ENCRYPTED_AEAD); |
165 | 0 | ad[1] = 1; |
166 | 0 | ad[2] = dfx->cipher_algo; |
167 | 0 | ad[3] = dfx->aead_algo; |
168 | 0 | ad[4] = dfx->chunkbyte; |
169 | 0 | ad[5] = dfx->chunkindex >> 56; |
170 | 0 | ad[6] = dfx->chunkindex >> 48; |
171 | 0 | ad[7] = dfx->chunkindex >> 40; |
172 | 0 | ad[8] = dfx->chunkindex >> 32; |
173 | 0 | ad[9] = dfx->chunkindex >> 24; |
174 | 0 | ad[10]= dfx->chunkindex >> 16; |
175 | 0 | ad[11]= dfx->chunkindex >> 8; |
176 | 0 | ad[12]= dfx->chunkindex; |
177 | 0 | if (final) |
178 | 0 | { |
179 | 0 | ad[13] = dfx->total >> 56; |
180 | 0 | ad[14] = dfx->total >> 48; |
181 | 0 | ad[15] = dfx->total >> 40; |
182 | 0 | ad[16] = dfx->total >> 32; |
183 | 0 | ad[17] = dfx->total >> 24; |
184 | 0 | ad[18] = dfx->total >> 16; |
185 | 0 | ad[19] = dfx->total >> 8; |
186 | 0 | ad[20] = dfx->total; |
187 | 0 | } |
188 | 0 | if (DBG_CRYPTO) |
189 | 0 | log_printhex (ad, final? 21 : 13, "authdata:"); |
190 | 0 | return gcry_cipher_authenticate (dfx->cipher_hd, ad, final? 21 : 13); |
191 | 0 | } |
192 | | |
193 | | |
194 | | /* Helper to check the 16 byte tag in TAGBUF. The FINAL flag is only |
195 | | * for debug messages. */ |
196 | | static gpg_error_t |
197 | | aead_checktag (decode_filter_ctx_t dfx, int final, const void *tagbuf) |
198 | 0 | { |
199 | 0 | gpg_error_t err; |
200 | |
|
201 | 0 | if (DBG_FILTER) |
202 | 0 | log_printhex (tagbuf, 16, "tag:"); |
203 | 0 | err = gcry_cipher_checktag (dfx->cipher_hd, tagbuf, 16); |
204 | 0 | if (err) |
205 | 0 | { |
206 | 0 | log_error ("gcry_cipher_checktag%s failed: %s\n", |
207 | 0 | final? " (final)":"", gpg_strerror (err)); |
208 | 0 | return err; |
209 | 0 | } |
210 | 0 | if (DBG_FILTER) |
211 | 0 | log_debug ("%stag is valid\n", final?"final ":""); |
212 | 0 | return 0; |
213 | 0 | } |
214 | | |
215 | | |
216 | | /**************** |
217 | | * Decrypt the data, specified by ED with the key DEK. On return |
218 | | * COMPLIANCE_ERROR is set to true iff the decryption can claim that |
219 | | * it was compliant in the current mode; otherwise this flag is set to |
220 | | * false. |
221 | | */ |
222 | | int |
223 | | decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek, |
224 | | int *compliance_error) |
225 | 0 | { |
226 | 0 | decode_filter_ctx_t dfx; |
227 | 0 | enum gcry_cipher_modes ciphermode; |
228 | 0 | unsigned int startivlen; |
229 | 0 | byte *p; |
230 | 0 | int rc=0, c, i; |
231 | 0 | byte temp[32]; |
232 | 0 | unsigned int blocksize; |
233 | 0 | unsigned int nprefix; |
234 | |
|
235 | 0 | *compliance_error = 0; |
236 | |
|
237 | 0 | dfx = xtrycalloc (1, sizeof *dfx); |
238 | 0 | if (!dfx) |
239 | 0 | return gpg_error_from_syserror (); |
240 | 0 | dfx->refcount = 1; |
241 | |
|
242 | 0 | if ( opt.verbose && !dek->algo_info_printed ) |
243 | 0 | { |
244 | 0 | if (!openpgp_cipher_test_algo (dek->algo)) |
245 | 0 | log_info (_("%s encrypted data\n"), |
246 | 0 | openpgp_cipher_algo_mode_name (dek->algo, ed->aead_algo)); |
247 | 0 | else |
248 | 0 | log_info (_("encrypted with unknown algorithm %d\n"), dek->algo ); |
249 | 0 | dek->algo_info_printed = 1; |
250 | 0 | } |
251 | |
|
252 | 0 | if (ed->aead_algo) |
253 | 0 | { |
254 | 0 | rc = openpgp_aead_algo_info (ed->aead_algo, &ciphermode, &startivlen); |
255 | 0 | if (rc) |
256 | 0 | goto leave; |
257 | 0 | log_assert (startivlen <= sizeof dfx->startiv); |
258 | 0 | } |
259 | 0 | else |
260 | 0 | ciphermode = GCRY_CIPHER_MODE_CFB; |
261 | | |
262 | | /* Check compliance. */ |
263 | 0 | if (!gnupg_cipher_is_allowed (opt.compliance, 0, dek->algo, ciphermode)) |
264 | 0 | { |
265 | 0 | log_error (_("cipher algorithm '%s' may not be used in %s mode\n"), |
266 | 0 | openpgp_cipher_algo_mode_name (dek->algo,ed->aead_algo), |
267 | 0 | gnupg_compliance_option_string (opt.compliance)); |
268 | 0 | *compliance_error = 1; |
269 | 0 | if (opt.flags.require_compliance) |
270 | 0 | { |
271 | | /* We fail early in this case because it does not make sense |
272 | | * to first decrypt everything. */ |
273 | 0 | rc = gpg_error (GPG_ERR_CIPHER_ALGO); |
274 | 0 | goto leave; |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | 0 | write_status_printf (STATUS_DECRYPTION_INFO, "%d %d %d", |
279 | 0 | ed->mdc_method, dek->algo, ed->aead_algo); |
280 | |
|
281 | 0 | if (opt.show_session_key) |
282 | 0 | { |
283 | 0 | char numbuf[30]; |
284 | 0 | char *hexbuf; |
285 | |
|
286 | 0 | if (ed->aead_algo) |
287 | 0 | snprintf (numbuf, sizeof numbuf, "%d.%u:", dek->algo, ed->aead_algo); |
288 | 0 | else |
289 | 0 | snprintf (numbuf, sizeof numbuf, "%d:", dek->algo); |
290 | 0 | hexbuf = bin2hex (dek->key, dek->keylen, NULL); |
291 | 0 | if (!hexbuf) |
292 | 0 | { |
293 | 0 | rc = gpg_error_from_syserror (); |
294 | 0 | goto leave; |
295 | 0 | } |
296 | 0 | log_info ("session key: '%s%s'\n", numbuf, hexbuf); |
297 | 0 | write_status_strings (STATUS_SESSION_KEY, numbuf, hexbuf, NULL); |
298 | 0 | xfree (hexbuf); |
299 | 0 | } |
300 | | |
301 | 0 | rc = openpgp_cipher_test_algo (dek->algo); |
302 | 0 | if (rc) |
303 | 0 | goto leave; |
304 | 0 | blocksize = openpgp_cipher_get_algo_blklen (dek->algo); |
305 | 0 | if ( !blocksize || blocksize > 16 ) |
306 | 0 | log_fatal ("unsupported blocksize %u\n", blocksize ); |
307 | | |
308 | 0 | if (ed->aead_algo) |
309 | 0 | { |
310 | 0 | if (blocksize != 16) |
311 | 0 | { |
312 | 0 | rc = gpg_error (GPG_ERR_CIPHER_ALGO); |
313 | 0 | goto leave; |
314 | 0 | } |
315 | | |
316 | 0 | if (ed->chunkbyte > 56) |
317 | 0 | { |
318 | 0 | log_error ("invalid AEAD chunkbyte %u\n", ed->chunkbyte); |
319 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
320 | 0 | goto leave; |
321 | 0 | } |
322 | | |
323 | | /* Read the Start-IV. */ |
324 | 0 | if (ed->len) |
325 | 0 | { |
326 | 0 | for (i=0; i < startivlen && ed->len; i++, ed->len--) |
327 | 0 | { |
328 | 0 | if ((c=iobuf_get (ed->buf)) == -1) |
329 | 0 | break; |
330 | 0 | dfx->startiv[i] = c; |
331 | 0 | } |
332 | 0 | } |
333 | 0 | else |
334 | 0 | { |
335 | 0 | for (i=0; i < startivlen; i++ ) |
336 | 0 | if ( (c=iobuf_get (ed->buf)) == -1 ) |
337 | 0 | break; |
338 | 0 | else |
339 | 0 | dfx->startiv[i] = c; |
340 | 0 | } |
341 | 0 | if (i != startivlen) |
342 | 0 | { |
343 | 0 | log_error ("Start-IV in AEAD packet too short (%d/%u)\n", |
344 | 0 | i, startivlen); |
345 | 0 | rc = gpg_error (GPG_ERR_TOO_SHORT); |
346 | 0 | goto leave; |
347 | 0 | } |
348 | | |
349 | 0 | dfx->cipher_algo = ed->cipher_algo; |
350 | 0 | dfx->aead_algo = ed->aead_algo; |
351 | 0 | dfx->chunkbyte = ed->chunkbyte; |
352 | 0 | dfx->chunksize = (uint64_t)1 << (dfx->chunkbyte + 6); |
353 | |
|
354 | 0 | if (dek->algo != dfx->cipher_algo) |
355 | 0 | log_info ("Note: different cipher algorithms used (%s/%s)\n", |
356 | 0 | openpgp_cipher_algo_name (dek->algo), |
357 | 0 | openpgp_cipher_algo_name (dfx->cipher_algo)); |
358 | |
|
359 | 0 | rc = openpgp_cipher_open (&dfx->cipher_hd, |
360 | 0 | dfx->cipher_algo, |
361 | 0 | ciphermode, |
362 | 0 | GCRY_CIPHER_SECURE); |
363 | 0 | if (rc) |
364 | 0 | goto leave; /* Should never happen. */ |
365 | | |
366 | 0 | if (DBG_CRYPTO) |
367 | 0 | log_printhex (dek->key, dek->keylen, "thekey:"); |
368 | 0 | rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen); |
369 | 0 | if (gpg_err_code (rc) == GPG_ERR_WEAK_KEY) |
370 | 0 | { |
371 | 0 | log_info (_("WARNING: message was encrypted with" |
372 | 0 | " a weak key in the symmetric cipher.\n")); |
373 | 0 | rc = 0; |
374 | 0 | } |
375 | 0 | else if (rc) |
376 | 0 | { |
377 | 0 | log_error("key setup failed: %s\n", gpg_strerror (rc)); |
378 | 0 | goto leave; |
379 | 0 | } |
380 | | |
381 | 0 | if (!ed->buf) |
382 | 0 | { |
383 | 0 | log_error(_("problem handling encrypted packet\n")); |
384 | 0 | goto leave; |
385 | 0 | } |
386 | |
|
387 | 0 | } |
388 | 0 | else /* CFB encryption. */ |
389 | 0 | { |
390 | 0 | nprefix = blocksize; |
391 | 0 | if ( ed->len && ed->len < (nprefix+2) ) |
392 | 0 | { |
393 | | /* An invalid message. We can't check that during parsing |
394 | | * because we may not know the used cipher then. */ |
395 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
396 | 0 | goto leave; |
397 | 0 | } |
398 | | |
399 | 0 | if ( ed->mdc_method ) |
400 | 0 | { |
401 | 0 | if (gcry_md_open (&dfx->mdc_hash, ed->mdc_method, 0 )) |
402 | 0 | BUG (); |
403 | 0 | if ( DBG_HASHING ) |
404 | 0 | gcry_md_debug (dfx->mdc_hash, "checkmdc"); |
405 | 0 | } |
406 | | |
407 | 0 | rc = openpgp_cipher_open (&dfx->cipher_hd, dek->algo, |
408 | 0 | GCRY_CIPHER_MODE_CFB, |
409 | 0 | (GCRY_CIPHER_SECURE |
410 | 0 | | ((ed->mdc_method || dek->algo >= 100)? |
411 | 0 | 0 : GCRY_CIPHER_ENABLE_SYNC))); |
412 | 0 | if (rc) |
413 | 0 | { |
414 | | /* We should never get an error here cause we already checked |
415 | | * that the algorithm is available. */ |
416 | 0 | BUG(); |
417 | 0 | } |
418 | | |
419 | | |
420 | | /* log_hexdump( "thekey", dek->key, dek->keylen );*/ |
421 | 0 | rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen); |
422 | 0 | if ( gpg_err_code (rc) == GPG_ERR_WEAK_KEY ) |
423 | 0 | { |
424 | 0 | log_info (_("WARNING: message was encrypted with" |
425 | 0 | " a weak key in the symmetric cipher.\n")); |
426 | 0 | rc=0; |
427 | 0 | } |
428 | 0 | else if (rc) |
429 | 0 | { |
430 | 0 | log_error ("key setup failed: %s\n", gpg_strerror (rc) ); |
431 | 0 | goto leave; |
432 | 0 | } |
433 | | |
434 | 0 | if (!ed->buf) |
435 | 0 | { |
436 | 0 | log_error (_("problem handling encrypted packet\n")); |
437 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
438 | 0 | goto leave; |
439 | 0 | } |
440 | | |
441 | 0 | gcry_cipher_setiv (dfx->cipher_hd, NULL, 0); |
442 | |
|
443 | 0 | if ( ed->len ) |
444 | 0 | { |
445 | 0 | for (i=0; i < (nprefix+2) && ed->len; i++, ed->len-- ) |
446 | 0 | { |
447 | 0 | if ( (c=iobuf_get(ed->buf)) == -1 ) |
448 | 0 | break; |
449 | 0 | else |
450 | 0 | temp[i] = c; |
451 | 0 | } |
452 | 0 | } |
453 | 0 | else |
454 | 0 | { |
455 | 0 | for (i=0; i < (nprefix+2); i++ ) |
456 | 0 | if ( (c=iobuf_get(ed->buf)) == -1 ) |
457 | 0 | break; |
458 | 0 | else |
459 | 0 | temp[i] = c; |
460 | 0 | } |
461 | |
|
462 | 0 | gcry_cipher_decrypt (dfx->cipher_hd, temp, nprefix+2, NULL, 0); |
463 | 0 | gcry_cipher_sync (dfx->cipher_hd); |
464 | 0 | p = temp; |
465 | | /* log_hexdump( "prefix", temp, nprefix+2 ); */ |
466 | 0 | if (dek->symmetric |
467 | 0 | && (p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1]) ) |
468 | 0 | { |
469 | 0 | rc = gpg_error (GPG_ERR_BAD_KEY); |
470 | 0 | goto leave; |
471 | 0 | } |
472 | | |
473 | 0 | if ( dfx->mdc_hash ) |
474 | 0 | gcry_md_write (dfx->mdc_hash, temp, nprefix+2); |
475 | 0 | } |
476 | | |
477 | 0 | dfx->refcount++; |
478 | 0 | dfx->partial = !!ed->is_partial; |
479 | 0 | dfx->length = ed->len; |
480 | 0 | if (ed->aead_algo) |
481 | 0 | iobuf_push_filter ( ed->buf, aead_decode_filter, dfx ); |
482 | 0 | else if (ed->mdc_method) |
483 | 0 | iobuf_push_filter ( ed->buf, mdc_decode_filter, dfx ); |
484 | 0 | else |
485 | 0 | iobuf_push_filter ( ed->buf, decode_filter, dfx ); |
486 | |
|
487 | 0 | if (opt.unwrap_encryption) |
488 | 0 | { |
489 | 0 | char *filename = NULL; |
490 | 0 | estream_t fp; |
491 | |
|
492 | 0 | rc = get_output_file ("", 0, ed->buf, &filename, &fp); |
493 | 0 | if (! rc) |
494 | 0 | { |
495 | 0 | iobuf_t output = iobuf_esopen (fp, "w", 0, 0); |
496 | 0 | armor_filter_context_t *afx = NULL; |
497 | |
|
498 | 0 | es_setbuf (fp, NULL); |
499 | |
|
500 | 0 | if (opt.armor) |
501 | 0 | { |
502 | 0 | afx = new_armor_context (); |
503 | 0 | push_armor_filter (afx, output); |
504 | 0 | } |
505 | |
|
506 | 0 | iobuf_copy (output, ed->buf); |
507 | 0 | if ((rc = iobuf_error (ed->buf))) |
508 | 0 | log_error (_("error reading '%s': %s\n"), |
509 | 0 | filename, gpg_strerror (rc)); |
510 | 0 | else if ((rc = iobuf_error (output))) |
511 | 0 | log_error (_("error writing '%s': %s\n"), |
512 | 0 | filename, gpg_strerror (rc)); |
513 | |
|
514 | 0 | iobuf_close (output); |
515 | 0 | release_armor_context (afx); |
516 | 0 | } |
517 | 0 | xfree (filename); |
518 | 0 | } |
519 | 0 | else |
520 | 0 | proc_packets (ctrl, procctx, ed->buf ); |
521 | |
|
522 | 0 | ed->buf = NULL; |
523 | 0 | if (dfx->eof_seen > 1 ) |
524 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
525 | 0 | else if ( ed->mdc_method ) |
526 | 0 | { |
527 | | /* We used to let parse-packet.c handle the MDC packet but this |
528 | | turned out to be a problem with compressed packets: With old |
529 | | style packets there is no length information available and |
530 | | the decompressor uses an implicit end. However we can't know |
531 | | this implicit end beforehand (:-) and thus may feed the |
532 | | decompressor with more bytes than actually needed. It would |
533 | | be possible to unread the extra bytes but due to our weird |
534 | | iobuf system any unread is non reliable due to filters |
535 | | already popped off. The easy and sane solution is to care |
536 | | about the MDC packet only here and never pass it to the |
537 | | packet parser. Fortunatley the OpenPGP spec requires a |
538 | | strict format for the MDC packet so that we know that 22 |
539 | | bytes are appended. */ |
540 | 0 | int datalen = gcry_md_get_algo_dlen (ed->mdc_method); |
541 | |
|
542 | 0 | log_assert (dfx->cipher_hd); |
543 | 0 | log_assert (dfx->mdc_hash); |
544 | 0 | gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 22, NULL, 0); |
545 | 0 | gcry_md_write (dfx->mdc_hash, dfx->holdback, 2); |
546 | 0 | gcry_md_final (dfx->mdc_hash); |
547 | |
|
548 | 0 | if ( dfx->holdback[0] != '\xd3' |
549 | 0 | || dfx->holdback[1] != '\x14' |
550 | 0 | || datalen != 20 |
551 | 0 | || memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->holdback+2, datalen)) |
552 | 0 | rc = gpg_error (GPG_ERR_BAD_SIGNATURE); |
553 | | /* log_printhex("MDC message:", dfx->holdback, 22); */ |
554 | | /* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */ |
555 | 0 | } |
556 | | |
557 | 0 | leave: |
558 | 0 | release_dfx_context (dfx); |
559 | 0 | return rc; |
560 | 0 | } |
561 | | |
562 | | |
563 | | /* Fill BUFFER with up to NBYTES-OFFSET from STREAM utilizing |
564 | | * information from the context DFX. Returns the new offset which is |
565 | | * the number of bytes read plus the original offset. On EOF the |
566 | | * respective flag in DFX is set. */ |
567 | | static size_t |
568 | | fill_buffer (decode_filter_ctx_t dfx, iobuf_t stream, |
569 | | byte *buffer, size_t nbytes, size_t offset) |
570 | 0 | { |
571 | 0 | size_t nread = offset; |
572 | 0 | size_t curr; |
573 | 0 | int ret; |
574 | |
|
575 | 0 | if (dfx->partial) |
576 | 0 | { |
577 | 0 | while (nread < nbytes) |
578 | 0 | { |
579 | 0 | curr = nbytes - nread; |
580 | |
|
581 | 0 | ret = iobuf_read (stream, &buffer[nread], curr); |
582 | 0 | if (ret == -1) |
583 | 0 | { |
584 | 0 | dfx->eof_seen = 1; /* Normal EOF. */ |
585 | 0 | break; |
586 | 0 | } |
587 | | |
588 | 0 | nread += ret; |
589 | 0 | } |
590 | 0 | } |
591 | 0 | else |
592 | 0 | { |
593 | 0 | while (nread < nbytes && dfx->length) |
594 | 0 | { |
595 | 0 | curr = nbytes - nread; |
596 | 0 | if (curr > dfx->length) |
597 | 0 | curr = dfx->length; |
598 | |
|
599 | 0 | ret = iobuf_read (stream, &buffer[nread], curr); |
600 | 0 | if (ret == -1) |
601 | 0 | { |
602 | 0 | dfx->eof_seen = 3; /* Premature EOF. */ |
603 | 0 | break; |
604 | 0 | } |
605 | | |
606 | 0 | nread += ret; |
607 | 0 | dfx->length -= ret; |
608 | 0 | } |
609 | 0 | if (!dfx->length) |
610 | 0 | dfx->eof_seen = 1; /* Normal EOF. */ |
611 | 0 | } |
612 | |
|
613 | 0 | return nread; |
614 | 0 | } |
615 | | |
616 | | |
617 | | /* The core of the AEAD decryption. This is the underflow function of |
618 | | * the aead_decode_filter. */ |
619 | | static gpg_error_t |
620 | | aead_underflow (decode_filter_ctx_t dfx, iobuf_t a, byte *buf, size_t *ret_len) |
621 | 0 | { |
622 | 0 | const size_t size = *ret_len; /* The allocated size of BUF. */ |
623 | 0 | gpg_error_t err; |
624 | 0 | size_t totallen = 0; /* The number of bytes to return on success or EOF. */ |
625 | 0 | size_t off = 0; /* The offset into the buffer. */ |
626 | 0 | size_t len; /* The current number of bytes in BUF+OFF. */ |
627 | |
|
628 | 0 | log_assert (size > 48); /* Our code requires at least this size. */ |
629 | | |
630 | | /* Copy the rest from the last call of this function into BUF. */ |
631 | 0 | len = dfx->holdbacklen; |
632 | 0 | dfx->holdbacklen = 0; |
633 | 0 | memcpy (buf, dfx->holdback, len); |
634 | |
|
635 | 0 | if (DBG_FILTER) |
636 | 0 | log_debug ("aead_underflow: size=%zu len=%zu%s%s\n", size, len, |
637 | 0 | dfx->partial? " partial":"", dfx->eof_seen? " eof":""); |
638 | | |
639 | | /* Read and fill up BUF. We need to watch out for an EOF so that we |
640 | | * can detect the last chunk which is commonly shorter than the |
641 | | * chunksize. After the last data byte from the last chunk 32 more |
642 | | * bytes are expected for the last chunk's tag and the following |
643 | | * final chunk's tag. To detect the EOF we need to try reading at least |
644 | | * one further byte; however we try to read 16 extra bytes to avoid |
645 | | * single byte reads in some lower layers. The outcome is that we |
646 | | * have up to 48 extra extra octets which we will later put into the |
647 | | * holdback buffer for the next invocation (which handles the EOF |
648 | | * case). */ |
649 | 0 | len = fill_buffer (dfx, a, buf, size, len); |
650 | 0 | if (len < 32) |
651 | 0 | { |
652 | | /* Not enough data for the last two tags. */ |
653 | 0 | err = gpg_error (GPG_ERR_TRUNCATED); |
654 | 0 | goto leave; |
655 | 0 | } |
656 | 0 | if (dfx->eof_seen) |
657 | 0 | { |
658 | | /* If have seen an EOF we copy only the last two auth tags into |
659 | | * the holdback buffer. */ |
660 | 0 | dfx->holdbacklen = 32; |
661 | 0 | memcpy (dfx->holdback, buf+len-32, 32); |
662 | 0 | len -= 32; |
663 | 0 | } |
664 | 0 | else |
665 | 0 | { |
666 | | /* If have not seen an EOF we copy the entire extra 48 bytes |
667 | | * into the holdback buffer for processing at the next call of |
668 | | * this function. */ |
669 | 0 | dfx->holdbacklen = len > 48? 48 : len; |
670 | 0 | memcpy (dfx->holdback, buf+len-dfx->holdbacklen, dfx->holdbacklen); |
671 | 0 | len -= dfx->holdbacklen; |
672 | 0 | } |
673 | | /* log_printhex (dfx->holdback, dfx->holdbacklen, "holdback:"); */ |
674 | | |
675 | | /* Decrypt the buffer. This first requires a loop to handle the |
676 | | * case when a chunk ends within the buffer. */ |
677 | 0 | if (DBG_FILTER) |
678 | 0 | log_debug ("decrypt: chunklen=%"PRIu64" total=%"PRIu64" size=%zu len=%zu%s\n", |
679 | 0 | dfx->chunklen, dfx->total, size, len, |
680 | 0 | dfx->eof_seen? " eof":""); |
681 | |
|
682 | 0 | while (len && dfx->chunklen + len >= dfx->chunksize) |
683 | 0 | { |
684 | 0 | size_t n = dfx->chunksize - dfx->chunklen; |
685 | 0 | byte tagbuf[16]; |
686 | |
|
687 | 0 | if (DBG_FILTER) |
688 | 0 | log_debug ("chunksize will be reached: n=%zu\n", n); |
689 | |
|
690 | 0 | if (!dfx->chunklen) |
691 | 0 | { |
692 | | /* First data for this chunk - prepare. */ |
693 | 0 | err = aead_set_nonce_and_ad (dfx, 0); |
694 | 0 | if (err) |
695 | 0 | goto leave; |
696 | 0 | } |
697 | | |
698 | | /* log_printhex (buf, n, "ciph:"); */ |
699 | 0 | gcry_cipher_final (dfx->cipher_hd); |
700 | 0 | err = gcry_cipher_decrypt (dfx->cipher_hd, buf+off, n, NULL, 0); |
701 | 0 | if (err) |
702 | 0 | { |
703 | 0 | log_error ("gcry_cipher_decrypt failed (1): %s\n", |
704 | 0 | gpg_strerror (err)); |
705 | 0 | goto leave; |
706 | 0 | } |
707 | | /* log_printhex (buf, n, "plai:"); */ |
708 | 0 | totallen += n; |
709 | 0 | dfx->chunklen += n; |
710 | 0 | dfx->total += n; |
711 | 0 | off += n; |
712 | 0 | len -= n; |
713 | |
|
714 | 0 | if (DBG_FILTER) |
715 | 0 | log_debug ("ndecrypted: %zu (nchunk=%"PRIu64") bytes left: %zu at off=%zu\n", |
716 | 0 | totallen, dfx->chunklen, len, off); |
717 | | |
718 | | /* Check the tag. */ |
719 | 0 | if (len < 16) |
720 | 0 | { |
721 | | /* The tag is not entirely in the buffer. Read the rest of |
722 | | * the tag from the holdback buffer. Then shift the holdback |
723 | | * buffer and fill it up again. */ |
724 | 0 | memcpy (tagbuf, buf+off, len); |
725 | 0 | memcpy (tagbuf + len, dfx->holdback, 16 - len); |
726 | 0 | dfx->holdbacklen -= 16-len; |
727 | 0 | memmove (dfx->holdback, dfx->holdback + (16-len), dfx->holdbacklen); |
728 | |
|
729 | 0 | if (dfx->eof_seen) |
730 | 0 | { |
731 | | /* We should have the last chunk's tag in TAGBUF and the |
732 | | * final tag in HOLDBACKBUF. */ |
733 | 0 | if (len || dfx->holdbacklen != 16) |
734 | 0 | { |
735 | | /* Not enough data for the last two tags. */ |
736 | 0 | err = gpg_error (GPG_ERR_TRUNCATED); |
737 | 0 | goto leave; |
738 | 0 | } |
739 | 0 | } |
740 | 0 | else |
741 | 0 | { |
742 | 0 | len = 0; |
743 | 0 | dfx->holdbacklen = fill_buffer (dfx, a, dfx->holdback, 48, |
744 | 0 | dfx->holdbacklen); |
745 | 0 | if (dfx->holdbacklen < 32) |
746 | 0 | { |
747 | | /* Not enough data for the last two tags. */ |
748 | 0 | err = gpg_error (GPG_ERR_TRUNCATED); |
749 | 0 | goto leave; |
750 | 0 | } |
751 | 0 | } |
752 | 0 | } |
753 | 0 | else /* We already have the full tag. */ |
754 | 0 | { |
755 | 0 | memcpy (tagbuf, buf+off, 16); |
756 | | /* Remove that tag from the output. */ |
757 | 0 | memmove (buf + off, buf + off + 16, len - 16); |
758 | 0 | len -= 16; |
759 | 0 | } |
760 | 0 | err = aead_checktag (dfx, 0, tagbuf); |
761 | 0 | if (err) |
762 | 0 | goto leave; |
763 | 0 | dfx->chunklen = 0; |
764 | 0 | dfx->chunkindex++; |
765 | |
|
766 | 0 | continue; |
767 | 0 | } |
768 | | |
769 | | /* The bulk decryption of our buffer. */ |
770 | 0 | if (len) |
771 | 0 | { |
772 | 0 | if (!dfx->chunklen) |
773 | 0 | { |
774 | | /* First data for this chunk - prepare. */ |
775 | 0 | err = aead_set_nonce_and_ad (dfx, 0); |
776 | 0 | if (err) |
777 | 0 | goto leave; |
778 | 0 | } |
779 | | |
780 | 0 | if (dfx->eof_seen) |
781 | 0 | { |
782 | | /* This is the last block of the last chunk. Its length may |
783 | | * not be a multiple of the block length. */ |
784 | 0 | gcry_cipher_final (dfx->cipher_hd); |
785 | 0 | } |
786 | 0 | err = gcry_cipher_decrypt (dfx->cipher_hd, buf + off, len, NULL, 0); |
787 | 0 | if (err) |
788 | 0 | { |
789 | 0 | log_error ("gcry_cipher_decrypt failed (2): %s\n", |
790 | 0 | gpg_strerror (err)); |
791 | 0 | goto leave; |
792 | 0 | } |
793 | 0 | totallen += len; |
794 | 0 | dfx->chunklen += len; |
795 | 0 | dfx->total += len; |
796 | 0 | if (DBG_FILTER) |
797 | 0 | log_debug ("ndecrypted: %zu (nchunk=%"PRIu64")\n", |
798 | 0 | totallen, dfx->chunklen); |
799 | 0 | } |
800 | | |
801 | 0 | if (dfx->eof_seen) |
802 | 0 | { |
803 | |
|
804 | 0 | if (dfx->chunklen) |
805 | 0 | { |
806 | 0 | if (DBG_FILTER) |
807 | 0 | log_debug ("eof seen: holdback has the last and final tag\n"); |
808 | 0 | log_assert (dfx->holdbacklen >= 32); |
809 | 0 | err = aead_checktag (dfx, 0, dfx->holdback); |
810 | 0 | if (err) |
811 | 0 | goto leave; |
812 | 0 | dfx->chunklen = 0; |
813 | 0 | dfx->chunkindex++; |
814 | 0 | off = 16; |
815 | 0 | } |
816 | 0 | else |
817 | 0 | { |
818 | 0 | if (DBG_FILTER) |
819 | 0 | log_debug ("eof seen: holdback has the final tag\n"); |
820 | 0 | log_assert (dfx->holdbacklen >= 16); |
821 | 0 | off = 0; |
822 | 0 | } |
823 | | |
824 | | /* Check the final chunk. */ |
825 | 0 | err = aead_set_nonce_and_ad (dfx, 1); |
826 | 0 | if (err) |
827 | 0 | goto leave; |
828 | 0 | gcry_cipher_final (dfx->cipher_hd); |
829 | | /* Decrypt an empty string (using HOLDBACK as a dummy). */ |
830 | 0 | err = gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 0, NULL, 0); |
831 | 0 | if (err) |
832 | 0 | { |
833 | 0 | log_error ("gcry_cipher_decrypt failed (final): %s\n", |
834 | 0 | gpg_strerror (err)); |
835 | 0 | goto leave; |
836 | 0 | } |
837 | 0 | err = aead_checktag (dfx, 1, dfx->holdback+off); |
838 | 0 | if (err) |
839 | 0 | goto leave; |
840 | 0 | err = gpg_error (GPG_ERR_EOF); |
841 | 0 | } |
842 | | |
843 | 0 | leave: |
844 | 0 | if (DBG_FILTER) |
845 | 0 | log_debug ("aead_underflow: returning %zu (%s)\n", |
846 | 0 | totallen, gpg_strerror (err)); |
847 | | |
848 | | /* In case of an auth error we map the error code to the same as |
849 | | * used by the MDC decryption. */ |
850 | 0 | if (gpg_err_code (err) == GPG_ERR_CHECKSUM) |
851 | 0 | err = gpg_error (GPG_ERR_BAD_SIGNATURE); |
852 | | |
853 | | /* In case of an error we better wipe out the buffer than to convey |
854 | | * partly decrypted data. */ |
855 | 0 | if (err && gpg_err_code (err) != GPG_ERR_EOF) |
856 | 0 | memset (buf, 0, size); |
857 | |
|
858 | 0 | *ret_len = totallen; |
859 | |
|
860 | 0 | return err; |
861 | 0 | } |
862 | | |
863 | | |
864 | | /* The IOBUF filter used to decrypt AEAD encrypted data. */ |
865 | | static int |
866 | | aead_decode_filter (void *opaque, int control, IOBUF a, |
867 | | byte *buf, size_t *ret_len) |
868 | 0 | { |
869 | 0 | decode_filter_ctx_t dfx = opaque; |
870 | 0 | int rc = 0; |
871 | |
|
872 | 0 | if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen ) |
873 | 0 | { |
874 | 0 | *ret_len = 0; |
875 | 0 | rc = -1; |
876 | 0 | } |
877 | 0 | else if ( control == IOBUFCTRL_UNDERFLOW ) |
878 | 0 | { |
879 | 0 | log_assert (a); |
880 | | |
881 | 0 | rc = aead_underflow (dfx, a, buf, ret_len); |
882 | 0 | if (gpg_err_code (rc) == GPG_ERR_EOF) |
883 | 0 | rc = -1; /* We need to use the old convention in the filter. */ |
884 | |
|
885 | 0 | } |
886 | 0 | else if ( control == IOBUFCTRL_FREE ) |
887 | 0 | { |
888 | 0 | release_dfx_context (dfx); |
889 | 0 | } |
890 | 0 | else if ( control == IOBUFCTRL_DESC ) |
891 | 0 | { |
892 | 0 | mem2str (buf, "aead_decode_filter", *ret_len); |
893 | 0 | } |
894 | | |
895 | 0 | return rc; |
896 | 0 | } |
897 | | |
898 | | |
899 | | static int |
900 | | mdc_decode_filter (void *opaque, int control, IOBUF a, |
901 | | byte *buf, size_t *ret_len) |
902 | 0 | { |
903 | 0 | decode_filter_ctx_t dfx = opaque; |
904 | 0 | size_t n, size = *ret_len; |
905 | 0 | int rc = 0; |
906 | | |
907 | | /* Note: We need to distinguish between a partial and a fixed length |
908 | | packet. The first is the usual case as created by GPG. However |
909 | | for short messages the format degrades to a fixed length packet |
910 | | and other implementations might use fixed length as well. Only |
911 | | looking for the EOF on fixed data works only if the encrypted |
912 | | packet is not followed by other data. This used to be a long |
913 | | standing bug which was fixed on 2009-10-02. */ |
914 | |
|
915 | 0 | if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen ) |
916 | 0 | { |
917 | 0 | *ret_len = 0; |
918 | 0 | rc = -1; |
919 | 0 | } |
920 | 0 | else if( control == IOBUFCTRL_UNDERFLOW ) |
921 | 0 | { |
922 | 0 | log_assert (a); |
923 | 0 | log_assert (size > 44); /* Our code requires at least this size. */ |
924 | | |
925 | | /* Get at least 22 bytes and put it ahead in the buffer. */ |
926 | 0 | n = fill_buffer (dfx, a, buf, 44, 22); |
927 | 0 | if (n == 44) |
928 | 0 | { |
929 | | /* We have enough stuff - flush the holdback buffer. */ |
930 | 0 | if ( !dfx->holdbacklen ) /* First time. */ |
931 | 0 | { |
932 | 0 | memcpy (buf, buf+22, 22); |
933 | 0 | n = 22; |
934 | 0 | } |
935 | 0 | else |
936 | 0 | { |
937 | 0 | memcpy (buf, dfx->holdback, 22); |
938 | 0 | } |
939 | | |
940 | | /* Fill up the buffer. */ |
941 | 0 | n = fill_buffer (dfx, a, buf, size, n); |
942 | | |
943 | | /* Move the trailing 22 bytes back to the holdback buffer. We |
944 | | have at least 44 bytes thus a memmove is not needed. */ |
945 | 0 | n -= 22; |
946 | 0 | memcpy (dfx->holdback, buf+n, 22 ); |
947 | 0 | dfx->holdbacklen = 22; |
948 | 0 | } |
949 | 0 | else if ( !dfx->holdbacklen ) /* EOF seen but empty holdback. */ |
950 | 0 | { |
951 | | /* This is bad because it means an incomplete hash. */ |
952 | 0 | n -= 22; |
953 | 0 | memcpy (buf, buf+22, n ); |
954 | 0 | dfx->eof_seen = 2; /* EOF with incomplete hash. */ |
955 | 0 | } |
956 | 0 | else /* EOF seen (i.e. read less than 22 bytes). */ |
957 | 0 | { |
958 | 0 | memcpy (buf, dfx->holdback, 22 ); |
959 | 0 | n -= 22; |
960 | 0 | memcpy (dfx->holdback, buf+n, 22 ); |
961 | 0 | dfx->eof_seen = 1; /* Normal EOF. */ |
962 | 0 | } |
963 | |
|
964 | 0 | if ( n ) |
965 | 0 | { |
966 | 0 | if ( dfx->cipher_hd ) |
967 | 0 | gcry_cipher_decrypt (dfx->cipher_hd, buf, n, NULL, 0); |
968 | 0 | if ( dfx->mdc_hash ) |
969 | 0 | gcry_md_write (dfx->mdc_hash, buf, n); |
970 | 0 | } |
971 | 0 | else |
972 | 0 | { |
973 | 0 | log_assert ( dfx->eof_seen ); |
974 | 0 | rc = -1; /* Return EOF. */ |
975 | 0 | } |
976 | 0 | *ret_len = n; |
977 | 0 | } |
978 | 0 | else if ( control == IOBUFCTRL_FREE ) |
979 | 0 | { |
980 | 0 | release_dfx_context (dfx); |
981 | 0 | } |
982 | 0 | else if ( control == IOBUFCTRL_DESC ) |
983 | 0 | { |
984 | 0 | mem2str (buf, "mdc_decode_filter", *ret_len); |
985 | 0 | } |
986 | 0 | return rc; |
987 | 0 | } |
988 | | |
989 | | |
990 | | static int |
991 | | decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len) |
992 | 0 | { |
993 | 0 | decode_filter_ctx_t fc = opaque; |
994 | 0 | size_t size = *ret_len; |
995 | 0 | size_t n; |
996 | 0 | int rc = 0; |
997 | | |
998 | |
|
999 | 0 | if ( control == IOBUFCTRL_UNDERFLOW && fc->eof_seen ) |
1000 | 0 | { |
1001 | 0 | *ret_len = 0; |
1002 | 0 | rc = -1; |
1003 | 0 | } |
1004 | 0 | else if ( control == IOBUFCTRL_UNDERFLOW ) |
1005 | 0 | { |
1006 | 0 | log_assert (a); |
1007 | | |
1008 | 0 | n = fill_buffer (fc, a, buf, size, 0); |
1009 | 0 | if (n) |
1010 | 0 | { |
1011 | 0 | if (fc->cipher_hd) |
1012 | 0 | gcry_cipher_decrypt (fc->cipher_hd, buf, n, NULL, 0); |
1013 | 0 | } |
1014 | 0 | else |
1015 | 0 | { |
1016 | 0 | if (!fc->eof_seen) |
1017 | 0 | fc->eof_seen = 1; |
1018 | 0 | rc = -1; /* Return EOF. */ |
1019 | 0 | } |
1020 | 0 | *ret_len = n; |
1021 | 0 | } |
1022 | 0 | else if ( control == IOBUFCTRL_FREE ) |
1023 | 0 | { |
1024 | 0 | release_dfx_context (fc); |
1025 | 0 | } |
1026 | 0 | else if ( control == IOBUFCTRL_DESC ) |
1027 | 0 | { |
1028 | 0 | mem2str (buf, "decode_filter", *ret_len); |
1029 | 0 | } |
1030 | 0 | return rc; |
1031 | 0 | } |