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