Debian Codegraph @

contrib/pgcrypto/pgp-decrypt.c

raw
1 /*
2 * pgp-decrypt.c
3 * OpenPGP decrypt.
4 *
5 * Copyright (c) 2005 Marko Kreen
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * contrib/pgcrypto/pgp-decrypt.c
30 */
31
32 #include "postgres.h"
33
34 #include "mbuf.h"
35 #include "pgp.h"
36 #include "px.h"
37
38 #define NO_CTX_SIZE 0
39 #define ALLOW_CTX_SIZE 1
40 #define NO_COMPR 0
41 #define ALLOW_COMPR 1
42 #define NO_MDC 0
43 #define NEED_MDC 1
44
45 #define PKT_NORMAL 1
46 #define PKT_STREAM 2
47 #define PKT_CONTEXT 3
48
49 #define MAX_CHUNK (16*1024*1024)
50
51 static int
52 parse_new_len(PullFilter *src, int *len_p)
53 {
54 uint8 b;
55 int len;
56 int pkttype = PKT_NORMAL;
57
58 GETBYTE(src, b);
59 if (b <= 191)
60 len = b;
61 else if (b >= 192 && b <= 223)
62 {
63 len = ((unsigned) (b) - 192) << 8;
64 GETBYTE(src, b);
65 len += 192 + b;
66 }
67 else if (b == 255)
68 {
69 GETBYTE(src, b);
70 len = b;
71 GETBYTE(src, b);
72 len = (len << 8) | b;
73 GETBYTE(src, b);
74 len = (len << 8) | b;
75 GETBYTE(src, b);
76 len = (len << 8) | b;
77 }
78 else
79 {
80 len = 1 << (b & 0x1F);
81 pkttype = PKT_STREAM;
82 }
83
84 if (len < 0 || len > MAX_CHUNK)
85 {
86 px_debug("parse_new_len: weird length");
87 return PXE_PGP_CORRUPT_DATA;
88 }
89
90 *len_p = len;
91 return pkttype;
92 }
93
94 static int
95 parse_old_len(PullFilter *src, int *len_p, int lentype)
96 {
97 uint8 b;
98 int len;
99
100 GETBYTE(src, b);
101 len = b;
102
103 if (lentype == 1)
104 {
105 GETBYTE(src, b);
106 len = (len << 8) | b;
107 }
108 else if (lentype == 2)
109 {
110 GETBYTE(src, b);
111 len = (len << 8) | b;
112 GETBYTE(src, b);
113 len = (len << 8) | b;
114 GETBYTE(src, b);
115 len = (len << 8) | b;
116 }
117
118 if (len < 0 || len > MAX_CHUNK)
119 {
120 px_debug("parse_old_len: weird length");
121 return PXE_PGP_CORRUPT_DATA;
122 }
123 *len_p = len;
124 return PKT_NORMAL;
125 }
126
127 /* returns pkttype or 0 on eof */
128 int
129 pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p, int allow_ctx)
130 {
131 int lentype;
132 int res;
133 uint8 *p;
134
135 /* EOF is normal here, thus we don't use GETBYTE */
136 res = pullf_read(src, 1, &p);
137 if (res < 0)
138 return res;
139 if (res == 0)
140 return 0;
141
142 if ((*p & 0x80) == 0)
143 {
144 px_debug("pgp_parse_pkt_hdr: not pkt hdr");
145 return PXE_PGP_CORRUPT_DATA;
146 }
147
148 if (*p & 0x40)
149 {
150 *tag = *p & 0x3f;
151 res = parse_new_len(src, len_p);
152 }
153 else
154 {
155 lentype = *p & 3;
156 *tag = (*p >> 2) & 0x0F;
157 if (lentype == 3)
158 res = allow_ctx ? PKT_CONTEXT : PXE_PGP_CORRUPT_DATA;
159 else
160 res = parse_old_len(src, len_p, lentype);
161 }
162 return res;
163 }
164
165 /*
166 * Packet reader
167 */
168 struct PktData
169 {
170 int type;
171 int len;
172 };
173
174 static int
175 pktreader_pull(void *priv, PullFilter *src, int len,
176 uint8 **data_p, uint8 *buf, int buflen)
177 {
178 int res;
179 struct PktData *pkt = priv;
180
181 /* PKT_CONTEXT means: whatever there is */
182 if (pkt->type == PKT_CONTEXT)
183 return pullf_read(src, len, data_p);
184
185 while (pkt->len == 0)
186 {
187 /* this was last chunk in stream */
188 if (pkt->type == PKT_NORMAL)
189 return 0;
190
191 /* next chunk in stream */
192 res = parse_new_len(src, &pkt->len);
193 if (res < 0)
194 return res;
195 pkt->type = res;
196 }
197
198 if (len > pkt->len)
199 len = pkt->len;
200
201 res = pullf_read(src, len, data_p);
202 if (res > 0)
203 pkt->len -= res;
204
205 return res;
206 }
207
208 static void
209 pktreader_free(void *priv)
210 {
211 struct PktData *pkt = priv;
212
213 px_memset(pkt, 0, sizeof(*pkt));
214 pfree(pkt);
215 }
216
217 static struct PullFilterOps pktreader_filter = {
218 NULL, pktreader_pull, pktreader_free
219 };
220
221 /* needs helper function to pass several parameters */
222 int
223 pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
224 int pkttype, PGP_Context *ctx)
225 {
226 int res;
227 struct PktData *pkt = palloc(sizeof(*pkt));
228
229 pkt->type = pkttype;
230 pkt->len = len;
231 res = pullf_create(pf_p, &pktreader_filter, pkt, src);
232 if (res < 0)
233 pfree(pkt);
234 return res;
235 }
236
237 /*
238 * Prefix check filter
239 * https://tools.ietf.org/html/rfc4880#section-5.7
240 * https://tools.ietf.org/html/rfc4880#section-5.13
241 */
242
243 static int
244 prefix_init(void **priv_p, void *arg, PullFilter *src)
245 {
246 PGP_Context *ctx = arg;
247 int len;
248 int res;
249 uint8 *buf;
250 uint8 tmpbuf[PGP_MAX_BLOCK + 2];
251
252 len = pgp_get_cipher_block_size(ctx->cipher_algo);
253 /* Make sure we have space for prefix */
254 if (len > PGP_MAX_BLOCK)
255 return PXE_BUG;
256
257 res = pullf_read_max(src, len + 2, &buf, tmpbuf);
258 if (res < 0)
259 return res;
260 if (res != len + 2)
261 {
262 px_debug("prefix_init: short read");
263 px_memset(tmpbuf, 0, sizeof(tmpbuf));
264 return PXE_PGP_CORRUPT_DATA;
265 }
266
267 if (buf[len - 2] != buf[len] || buf[len - 1] != buf[len + 1])
268 {
269 px_debug("prefix_init: corrupt prefix");
270 /* report error in pgp_decrypt() */
271 ctx->corrupt_prefix = 1;
272 }
273 px_memset(tmpbuf, 0, sizeof(tmpbuf));
274 return 0;
275 }
276
277 static struct PullFilterOps prefix_filter = {
278 prefix_init, NULL, NULL
279 };
280
281
282 /*
283 * Decrypt filter
284 */
285
286 static int
287 decrypt_init(void **priv_p, void *arg, PullFilter *src)
288 {
289 PGP_CFB *cfb = arg;
290
291 *priv_p = cfb;
292
293 /* we need to write somewhere, so ask for a buffer */
294 return 4096;
295 }
296
297 static int
298 decrypt_read(void *priv, PullFilter *src, int len,
299 uint8 **data_p, uint8 *buf, int buflen)
300 {
301 PGP_CFB *cfb = priv;
302 uint8 *tmp;
303 int res;
304
305 res = pullf_read(src, len, &tmp);
306 if (res > 0)
307 {
308 pgp_cfb_decrypt(cfb, tmp, res, buf);
309 *data_p = buf;
310 }
311 return res;
312 }
313
314 struct PullFilterOps pgp_decrypt_filter = {
315 decrypt_init, decrypt_read, NULL
316 };
317
318
319 /*
320 * MDC hasher filter
321 */
322
323 static int
324 mdc_init(void **priv_p, void *arg, PullFilter *src)
325 {
326 PGP_Context *ctx = arg;
327
328 *priv_p = ctx;
329 return pgp_load_digest(PGP_DIGEST_SHA1, &ctx->mdc_ctx);
330 }
331
332 static void
333 mdc_free(void *priv)
334 {
335 PGP_Context *ctx = priv;
336
337 if (ctx->use_mdcbuf_filter)
338 return;
339 px_md_free(ctx->mdc_ctx);
340 ctx->mdc_ctx = NULL;
341 }
342
343 static int
344 mdc_finish(PGP_Context *ctx, PullFilter *src, int len)
345 {
346 int res;
347 uint8 hash[20];
348 uint8 tmpbuf[20];
349 uint8 *data;
350
351 /* should not happen */
352 if (ctx->use_mdcbuf_filter)
353 return PXE_BUG;
354
355 /* It's SHA1 */
356 if (len != 20)
357 return PXE_PGP_CORRUPT_DATA;
358
359 /* mdc_read should not call px_md_update */
360 ctx->in_mdc_pkt = 1;
361
362 /* read data */
363 res = pullf_read_max(src, len, &data, tmpbuf);
364 if (res < 0)
365 return res;
366 if (res == 0)
367 {
368 px_debug("no mdc");
369 return PXE_PGP_CORRUPT_DATA;
370 }
371
372 /* is the packet sane? */
373 if (res != 20)
374 {
375 px_debug("mdc_finish: read failed, res=%d", res);
376 return PXE_PGP_CORRUPT_DATA;
377 }
378
379 /*
380 * ok, we got the hash, now check
381 */
382 px_md_finish(ctx->mdc_ctx, hash);
383 res = memcmp(hash, data, 20);
384 px_memset(hash, 0, 20);
385 px_memset(tmpbuf, 0, sizeof(tmpbuf));
386 if (res != 0)
387 {
388 px_debug("mdc_finish: mdc failed");
389 return PXE_PGP_CORRUPT_DATA;
390 }
391 ctx->mdc_checked = 1;
392 return 0;
393 }
394
395 static int
396 mdc_read(void *priv, PullFilter *src, int len,
397 uint8 **data_p, uint8 *buf, int buflen)
398 {
399 int res;
400 PGP_Context *ctx = priv;
401
402 /* skip this filter? */
403 if (ctx->use_mdcbuf_filter || ctx->in_mdc_pkt)
404 return pullf_read(src, len, data_p);
405
406 res = pullf_read(src, len, data_p);
407 if (res < 0)
408 return res;
409 if (res == 0)
410 {
411 px_debug("mdc_read: unexpected eof");
412 return PXE_PGP_CORRUPT_DATA;
413 }
414 px_md_update(ctx->mdc_ctx, *data_p, res);
415
416 return res;
417 }
418
419 static struct PullFilterOps mdc_filter = {
420 mdc_init, mdc_read, mdc_free
421 };
422
423
424 /*
425 * Combined Pkt reader and MDC hasher.
426 *
427 * For the case of SYMENCRYPTED_DATA_MDC packet, where
428 * the data part has 'context length', which means
429 * that data packet ends 22 bytes before end of parent
430 * packet, which is silly.
431 */
432 #define MDCBUF_LEN 8192
433 struct MDCBufData
434 {
435 PGP_Context *ctx;
436 int eof;
437 int buflen;
438 int avail;
439 uint8 *pos;
440 int mdc_avail;
441 uint8 mdc_buf[22];
442 uint8 buf[MDCBUF_LEN];
443 };
444
445 static int
446 mdcbuf_init(void **priv_p, void *arg, PullFilter *src)
447 {
448 PGP_Context *ctx = arg;
449 struct MDCBufData *st;
450
451 st = palloc0(sizeof(*st));
452 st->buflen = sizeof(st->buf);
453 st->ctx = ctx;
454 *priv_p = st;
455
456 /* take over the work of mdc_filter */
457 ctx->use_mdcbuf_filter = 1;
458
459 return 0;
460 }
461
462 static int
463 mdcbuf_finish(struct MDCBufData *st)
464 {
465 uint8 hash[20];
466 int res;
467
468 st->eof = 1;
469
470 if (st->mdc_buf[0] != 0xD3 || st->mdc_buf[1] != 0x14)
471 {
472 px_debug("mdcbuf_finish: bad MDC pkt hdr");
473 return PXE_PGP_CORRUPT_DATA;
474 }
475 px_md_update(st->ctx->mdc_ctx, st->mdc_buf, 2);
476 px_md_finish(st->ctx->mdc_ctx, hash);
477 res = memcmp(hash, st->mdc_buf + 2, 20);
478 px_memset(hash, 0, 20);
479 if (res)
480 {
481 px_debug("mdcbuf_finish: MDC does not match");
482 res = PXE_PGP_CORRUPT_DATA;
483 }
484 return res;
485 }
486
487 static void
488 mdcbuf_load_data(struct MDCBufData *st, uint8 *src, int len)
489 {
490 uint8 *dst = st->pos + st->avail;
491
492 memcpy(dst, src, len);
493 px_md_update(st->ctx->mdc_ctx, src, len);
494 st->avail += len;
495 }
496
497 static void
498 mdcbuf_load_mdc(struct MDCBufData *st, uint8 *src, int len)
499 {
500 memmove(st->mdc_buf + st->mdc_avail, src, len);
501 st->mdc_avail += len;
502 }
503
504 static int
505 mdcbuf_refill(struct MDCBufData *st, PullFilter *src)
506 {
507 uint8 *data;
508 int res;
509 int need;
510
511 /* put avail data in start */
512 if (st->avail > 0 && st->pos != st->buf)
513 memmove(st->buf, st->pos, st->avail);
514 st->pos = st->buf;
515
516 /* read new data */
517 need = st->buflen + 22 - st->avail - st->mdc_avail;
518 res = pullf_read(src, need, &data);
519 if (res < 0)
520 return res;
521 if (res == 0)
522 return mdcbuf_finish(st);
523
524 /* add to buffer */
525 if (res >= 22)
526 {
527 mdcbuf_load_data(st, st->mdc_buf, st->mdc_avail);
528 st->mdc_avail = 0;
529
530 mdcbuf_load_data(st, data, res - 22);
531 mdcbuf_load_mdc(st, data + res - 22, 22);
532 }
533 else
534 {
535 int canmove = st->mdc_avail + res - 22;
536
537 if (canmove > 0)
538 {
539 mdcbuf_load_data(st, st->mdc_buf, canmove);
540 st->mdc_avail -= canmove;
541 memmove(st->mdc_buf, st->mdc_buf + canmove, st->mdc_avail);
542 }
543 mdcbuf_load_mdc(st, data, res);
544 }
545 return 0;
546 }
547
548 static int
549 mdcbuf_read(void *priv, PullFilter *src, int len,
550 uint8 **data_p, uint8 *buf, int buflen)
551 {
552 struct MDCBufData *st = priv;
553 int res;
554
555 if (!st->eof && len > st->avail)
556 {
557 res = mdcbuf_refill(st, src);
558 if (res < 0)
559 return res;
560 }
561
562 if (len > st->avail)
563 len = st->avail;
564
565 *data_p = st->pos;
566 st->pos += len;
567 st->avail -= len;
568 return len;
569 }
570
571 static void
572 mdcbuf_free(void *priv)
573 {
574 struct MDCBufData *st = priv;
575
576 px_md_free(st->ctx->mdc_ctx);
577 st->ctx->mdc_ctx = NULL;
578 px_memset(st, 0, sizeof(*st));
579 pfree(st);
580 }
581
582 static struct PullFilterOps mdcbuf_filter = {
583 mdcbuf_init, mdcbuf_read, mdcbuf_free
584 };
585
586
587 /*
588 * Decrypt separate session key
589 */
590 static int
591 decrypt_key(PGP_Context *ctx, const uint8 *src, int len)
592 {
593 int res;
594 uint8 algo;
595 PGP_CFB *cfb;
596
597 res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
598 ctx->s2k.key, ctx->s2k.key_len, 0, NULL,
599 ctx->ignore_cipher_failure);
600 if (res < 0)
601 return res;
602
603 pgp_cfb_decrypt(cfb, src, 1, &algo);
604 src++;
605 len--;
606
607 pgp_cfb_decrypt(cfb, src, len, ctx->sess_key);
608 pgp_cfb_free(cfb);
609 ctx->sess_key_len = len;
610 ctx->cipher_algo = algo;
611
612 if (pgp_get_cipher_key_size(algo) != len)
613 {
614 px_debug("sesskey bad len: algo=%d, expected=%d, got=%d",
615 algo, pgp_get_cipher_key_size(algo), len);
616 return PXE_PGP_CORRUPT_DATA;
617 }
618 return 0;
619 }
620
621 /*
622 * Handle key packet
623 */
624 static int
625 parse_symenc_sesskey(PGP_Context *ctx, PullFilter *src)
626 {
627 uint8 *p;
628 int res;
629 uint8 tmpbuf[PGP_MAX_KEY + 2];
630 uint8 ver;
631
632 GETBYTE(src, ver);
633 GETBYTE(src, ctx->s2k_cipher_algo);
634 if (ver != 4)
635 {
636 px_debug("bad key pkt ver");
637 return PXE_PGP_CORRUPT_DATA;
638 }
639
640 /*
641 * read S2K info
642 */
643 res = pgp_s2k_read(src, &ctx->s2k);
644 if (res < 0)
645 return res;
646 ctx->s2k_mode = ctx->s2k.mode;
647 ctx->s2k_count = s2k_decode_count(ctx->s2k.iter);
648 ctx->s2k_digest_algo = ctx->s2k.digest_algo;
649
650 /*
651 * generate key from password
652 */
653 res = pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
654 ctx->sym_key, ctx->sym_key_len);
655 if (res < 0)
656 return res;
657
658 /*
659 * do we have separate session key?
660 */
661 res = pullf_read_max(src, PGP_MAX_KEY + 2, &p, tmpbuf);
662 if (res < 0)
663 return res;
664
665 if (res == 0)
666 {
667 /*
668 * no, s2k key is session key
669 */
670 memcpy(ctx->sess_key, ctx->s2k.key, ctx->s2k.key_len);
671 ctx->sess_key_len = ctx->s2k.key_len;
672 ctx->cipher_algo = ctx->s2k_cipher_algo;
673 res = 0;
674 ctx->use_sess_key = 0;
675 }
676 else
677 {
678 /*
679 * yes, decrypt it
680 */
681 if (res < 17 || res > PGP_MAX_KEY + 1)
682 {
683 px_debug("expect key, but bad data");
684 return PXE_PGP_CORRUPT_DATA;
685 }
686 ctx->use_sess_key = 1;
687 res = decrypt_key(ctx, p, res);
688 }
689
690 px_memset(tmpbuf, 0, sizeof(tmpbuf));
691 return res;
692 }
693
694 static int
695 copy_crlf(MBuf *dst, uint8 *data, int len, int *got_cr)
696 {
697 uint8 *data_end = data + len;
698 uint8 tmpbuf[1024];
699 uint8 *tmp_end = tmpbuf + sizeof(tmpbuf);
700 uint8 *p;
701 int res;
702
703 p = tmpbuf;
704 if (*got_cr)
705 {
706 if (*data != '\n')
707 *p++ = '\r';
708 *got_cr = 0;
709 }
710 while (data < data_end)
711 {
712 if (*data == '\r')
713 {
714 if (data + 1 < data_end)
715 {
716 if (*(data + 1) == '\n')
717 data++;
718 }
719 else
720 {
721 *got_cr = 1;
722 break;
723 }
724 }
725 *p++ = *data++;
726 if (p >= tmp_end)
727 {
728 res = mbuf_append(dst, tmpbuf, p - tmpbuf);
729 if (res < 0)
730 return res;
731 p = tmpbuf;
732 }
733 }
734 if (p - tmpbuf > 0)
735 {
736 res = mbuf_append(dst, tmpbuf, p - tmpbuf);
737 if (res < 0)
738 return res;
739 }
740 px_memset(tmpbuf, 0, sizeof(tmpbuf));
741 return 0;
742 }
743
744 static int
745 parse_literal_data(PGP_Context *ctx, MBuf *dst, PullFilter *pkt)
746 {
747 int type;
748 int name_len;
749 int res;
750 uint8 *buf;
751 uint8 tmpbuf[4];
752 int got_cr = 0;
753
754 GETBYTE(pkt, type);
755 GETBYTE(pkt, name_len);
756
757 /* skip name */
758 while (name_len > 0)
759 {
760 res = pullf_read(pkt, name_len, &buf);
761 if (res < 0)
762 return res;
763 if (res == 0)
764 break;
765 name_len -= res;
766 }
767 if (name_len > 0)
768 {
769 px_debug("parse_literal_data: unexpected eof");
770 return PXE_PGP_CORRUPT_DATA;
771 }
772
773 /* skip date */
774 res = pullf_read_max(pkt, 4, &buf, tmpbuf);
775 if (res != 4)
776 {
777 px_debug("parse_literal_data: unexpected eof");
778 return PXE_PGP_CORRUPT_DATA;
779 }
780 px_memset(tmpbuf, 0, 4);
781
782 /*
783 * If called from an SQL function that returns text, pgp_decrypt() rejects
784 * inputs not self-identifying as text.
785 */
786 if (ctx->text_mode)
787 if (type != 't' && type != 'u')
788 {
789 px_debug("parse_literal_data: data type=%c", type);
790 ctx->unexpected_binary = true;
791 }
792
793 ctx->unicode_mode = (type == 'u') ? 1 : 0;
794
795 /* read data */
796 while (1)
797 {
798 res = pullf_read(pkt, 32 * 1024, &buf);
799 if (res <= 0)
800 break;
801
802 if (ctx->text_mode && ctx->convert_crlf)
803 res = copy_crlf(dst, buf, res, &got_cr);
804 else
805 res = mbuf_append(dst, buf, res);
806 if (res < 0)
807 break;
808 }
809 if (res >= 0 && got_cr)
810 res = mbuf_append(dst, (const uint8 *) "\r", 1);
811 return res;
812 }
813
814 /* process_data_packets and parse_compressed_data call each other */
815 static int process_data_packets(PGP_Context *ctx, MBuf *dst,
816 PullFilter *src, int allow_compr, int need_mdc);
817
818 static int
819 parse_compressed_data(PGP_Context *ctx, MBuf *dst, PullFilter *pkt)
820 {
821 int res;
822 uint8 type;
823 PullFilter *pf_decompr;
824 uint8 *discard_buf;
825
826 GETBYTE(pkt, type);
827
828 ctx->compress_algo = type;
829 switch (type)
830 {
831 case PGP_COMPR_NONE:
832 res = process_data_packets(ctx, dst, pkt, NO_COMPR, NO_MDC);
833 break;
834
835 case PGP_COMPR_ZIP:
836 case PGP_COMPR_ZLIB:
837 res = pgp_decompress_filter(&pf_decompr, ctx, pkt);
838 if (res >= 0)
839 {
840 res = process_data_packets(ctx, dst, pf_decompr,
841 NO_COMPR, NO_MDC);
842 pullf_free(pf_decompr);
843 }
844 break;
845
846 case PGP_COMPR_BZIP2:
847 px_debug("parse_compressed_data: bzip2 unsupported");
848 /* report error in pgp_decrypt() */
849 ctx->unsupported_compr = 1;
850
851 /*
852 * Discard the compressed data, allowing it to first affect any
853 * MDC digest computation.
854 */
855 while (1)
856 {
857 res = pullf_read(pkt, 32 * 1024, &discard_buf);
858 if (res <= 0)
859 break;
860 }
861
862 break;
863
864 default:
865 px_debug("parse_compressed_data: unknown compr type");
866 res = PXE_PGP_CORRUPT_DATA;
867 }
868
869 return res;
870 }
871
872 static int
873 process_data_packets(PGP_Context *ctx, MBuf *dst, PullFilter *src,
874 int allow_compr, int need_mdc)
875 {
876 uint8 tag;
877 int len,
878 res;
879 int got_data = 0;
880 int got_mdc = 0;
881 PullFilter *pkt = NULL;
882
883 while (1)
884 {
885 res = pgp_parse_pkt_hdr(src, &tag, &len, ALLOW_CTX_SIZE);
886 if (res <= 0)
887 break;
888
889
890 /* mdc packet should be last */
891 if (got_mdc)
892 {
893 px_debug("process_data_packets: data after mdc");
894 res = PXE_PGP_CORRUPT_DATA;
895 break;
896 }
897
898 /*
899 * Context length inside SYMENCRYPTED_DATA_MDC packet needs special
900 * handling.
901 */
902 if (need_mdc && res == PKT_CONTEXT)
903 res = pullf_create(&pkt, &mdcbuf_filter, ctx, src);
904 else
905 res = pgp_create_pkt_reader(&pkt, src, len, res, ctx);
906 if (res < 0)
907 break;
908
909 switch (tag)
910 {
911 case PGP_PKT_LITERAL_DATA:
912 got_data = 1;
913 res = parse_literal_data(ctx, dst, pkt);
914 break;
915 case PGP_PKT_COMPRESSED_DATA:
916 if (allow_compr == 0)
917 {
918 px_debug("process_data_packets: unexpected compression");
919 res = PXE_PGP_CORRUPT_DATA;
920 }
921 else if (got_data)
922 {
923 /*
924 * compr data must be alone
925 */
926 px_debug("process_data_packets: only one cmpr pkt allowed");
927 res = PXE_PGP_CORRUPT_DATA;
928 }
929 else
930 {
931 got_data = 1;
932 res = parse_compressed_data(ctx, dst, pkt);
933 }
934 break;
935 case PGP_PKT_MDC:
936 if (need_mdc == NO_MDC)
937 {
938 px_debug("process_data_packets: unexpected MDC");
939 res = PXE_PGP_CORRUPT_DATA;
940 break;
941 }
942
943 res = mdc_finish(ctx, pkt, len);
944 if (res >= 0)
945 got_mdc = 1;
946 break;
947 default:
948 px_debug("process_data_packets: unexpected pkt tag=%d", tag);
949 res = PXE_PGP_CORRUPT_DATA;
950 }
951
952 pullf_free(pkt);
953 pkt = NULL;
954
955 if (res < 0)
956 break;
957 }
958
959 if (pkt)
960 pullf_free(pkt);
961
962 if (res < 0)
963 return res;
964
965 if (!got_data)
966 {
967 px_debug("process_data_packets: no data");
968 res = PXE_PGP_CORRUPT_DATA;
969 }
970 if (need_mdc && !got_mdc && !ctx->use_mdcbuf_filter)
971 {
972 px_debug("process_data_packets: got no mdc");
973 res = PXE_PGP_CORRUPT_DATA;
974 }
975 return res;
976 }
977
978 static int
979 parse_symenc_data(PGP_Context *ctx, PullFilter *pkt, MBuf *dst)
980 {
981 int res;
982 PGP_CFB *cfb = NULL;
983 PullFilter *pf_decrypt = NULL;
984 PullFilter *pf_prefix = NULL;
985
986 res = pgp_cfb_create(&cfb, ctx->cipher_algo,
987 ctx->sess_key, ctx->sess_key_len, 1, NULL,
988 ctx->ignore_cipher_failure);
989 if (res < 0)
990 goto out;
991
992 res = pullf_create(&pf_decrypt, &pgp_decrypt_filter, cfb, pkt);
993 if (res < 0)
994 goto out;
995
996 res = pullf_create(&pf_prefix, &prefix_filter, ctx, pf_decrypt);
997 if (res < 0)
998 goto out;
999
1000 res = process_data_packets(ctx, dst, pf_prefix, ALLOW_COMPR, NO_MDC);
1001
1002 out:
1003 if (pf_prefix)
1004 pullf_free(pf_prefix);
1005 if (pf_decrypt)
1006 pullf_free(pf_decrypt);
1007 if (cfb)
1008 pgp_cfb_free(cfb);
1009
1010 return res;
1011 }
1012
1013 static int
1014 parse_symenc_mdc_data(PGP_Context *ctx, PullFilter *pkt, MBuf *dst)
1015 {
1016 int res;
1017 PGP_CFB *cfb = NULL;
1018 PullFilter *pf_decrypt = NULL;
1019 PullFilter *pf_prefix = NULL;
1020 PullFilter *pf_mdc = NULL;
1021 uint8 ver;
1022
1023 GETBYTE(pkt, ver);
1024 if (ver != 1)
1025 {
1026 px_debug("parse_symenc_mdc_data: pkt ver != 1");
1027 return PXE_PGP_CORRUPT_DATA;
1028 }
1029
1030 res = pgp_cfb_create(&cfb, ctx->cipher_algo,
1031 ctx->sess_key, ctx->sess_key_len, 0, NULL,
1032 ctx->ignore_cipher_failure);
1033 if (res < 0)
1034 goto out;
1035
1036 res = pullf_create(&pf_decrypt, &pgp_decrypt_filter, cfb, pkt);
1037 if (res < 0)
1038 goto out;
1039
1040 res = pullf_create(&pf_mdc, &mdc_filter, ctx, pf_decrypt);
1041 if (res < 0)
1042 goto out;
1043
1044 res = pullf_create(&pf_prefix, &prefix_filter, ctx, pf_mdc);
1045 if (res < 0)
1046 goto out;
1047
1048 res = process_data_packets(ctx, dst, pf_prefix, ALLOW_COMPR, NEED_MDC);
1049
1050 out:
1051 if (pf_prefix)
1052 pullf_free(pf_prefix);
1053 if (pf_mdc)
1054 pullf_free(pf_mdc);
1055 if (pf_decrypt)
1056 pullf_free(pf_decrypt);
1057 if (cfb)
1058 pgp_cfb_free(cfb);
1059
1060 return res;
1061 }
1062
1063 /*
1064 * skip over packet contents
1065 */
1066 int
1067 pgp_skip_packet(PullFilter *pkt)
1068 {
1069 int res = 1;
1070 uint8 *tmp;
1071
1072 while (res > 0)
1073 res = pullf_read(pkt, 32 * 1024, &tmp);
1074 return res;
1075 }
1076
1077 /*
1078 * expect to be at packet end, any data is error
1079 */
1080 int
1081 pgp_expect_packet_end(PullFilter *pkt)
1082 {
1083 int res;
1084 uint8 *tmp;
1085
1086 res = pullf_read(pkt, 32 * 1024, &tmp);
1087 if (res > 0)
1088 {
1089 px_debug("pgp_expect_packet_end: got data");
1090 return PXE_PGP_CORRUPT_DATA;
1091 }
1092 return res;
1093 }
1094
1095 int
1096 pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst)
1097 {
1098 int res;
1099 PullFilter *src = NULL;
1100 PullFilter *pkt = NULL;
1101 uint8 tag;
1102 int len;
1103 int got_key = 0;
1104 int got_data = 0;
1105
1106 res = pullf_create_mbuf_reader(&src, msrc);
1107
1108 while (res >= 0)
1109 {
1110 res = pgp_parse_pkt_hdr(src, &tag, &len, NO_CTX_SIZE);
1111 if (res <= 0)
1112 break;
1113
1114 res = pgp_create_pkt_reader(&pkt, src, len, res, ctx);
1115 if (res < 0)
1116 break;
1117
1118 res = PXE_PGP_CORRUPT_DATA;
1119 switch (tag)
1120 {
1121 case PGP_PKT_MARKER:
1122 res = pgp_skip_packet(pkt);
1123 break;
1124 case PGP_PKT_PUBENCRYPTED_SESSKEY:
1125 /* fixme: skip those */
1126 res = pgp_parse_pubenc_sesskey(ctx, pkt);
1127 got_key = 1;
1128 break;
1129 case PGP_PKT_SYMENCRYPTED_SESSKEY:
1130 if (got_key)
1131
1132 /*
1133 * Theoretically, there could be several keys, both public
1134 * and symmetric, all of which encrypt same session key.
1135 * Decrypt should try with each one, before failing.
1136 */
1137 px_debug("pgp_decrypt: using first of several keys");
1138 else
1139 {
1140 got_key = 1;
1141 res = parse_symenc_sesskey(ctx, pkt);
1142 }
1143 break;
1144 case PGP_PKT_SYMENCRYPTED_DATA:
1145 if (!got_key)
1146 px_debug("pgp_decrypt: have data but no key");
1147 else if (got_data)
1148 px_debug("pgp_decrypt: got second data packet");
1149 else
1150 {
1151 got_data = 1;
1152 ctx->disable_mdc = 1;
1153 res = parse_symenc_data(ctx, pkt, mdst);
1154 }
1155 break;
1156 case PGP_PKT_SYMENCRYPTED_DATA_MDC:
1157 if (!got_key)
1158 px_debug("pgp_decrypt: have data but no key");
1159 else if (got_data)
1160 px_debug("pgp_decrypt: several data pkts not supported");
1161 else
1162 {
1163 got_data = 1;
1164 ctx->disable_mdc = 0;
1165 res = parse_symenc_mdc_data(ctx, pkt, mdst);
1166 }
1167 break;
1168 default:
1169 px_debug("pgp_decrypt: unknown tag: 0x%02x", tag);
1170 }
1171 pullf_free(pkt);
1172 pkt = NULL;
1173 }
1174
1175 if (pkt)
1176 pullf_free(pkt);
1177
1178 if (src)
1179 pullf_free(src);
1180
1181 if (res < 0)
1182 return res;
1183
1184 /*
1185 * Report a failure of the prefix_init() "quick check" now, rather than
1186 * upon detection, to hinder timing attacks. pgcrypto is not generally
1187 * secure against timing attacks, but this helps.
1188 */
1189 if (!got_data || ctx->corrupt_prefix)
1190 return PXE_PGP_CORRUPT_DATA;
1191
1192 /*
1193 * Code interpreting purportedly-decrypted data prior to this stage shall
1194 * report no error other than PXE_PGP_CORRUPT_DATA. (PXE_BUG is okay so
1195 * long as it remains unreachable.) This ensures that an attacker able to
1196 * choose a ciphertext and receive a corresponding decryption error
1197 * message cannot use that oracle to gather clues about the decryption
1198 * key. See "An Attack on CFB Mode Encryption As Used By OpenPGP" by
1199 * Serge Mister and Robert Zuccherato.
1200 *
1201 * A problematic value in the first octet of a Literal Data or Compressed
1202 * Data packet may indicate a simple user error, such as the need to call
1203 * pgp_sym_decrypt_bytea instead of pgp_sym_decrypt. Occasionally,
1204 * though, it is the first symptom of the encryption key not matching the
1205 * decryption key. When this was the only problem encountered, report a
1206 * specific error to guide the user; otherwise, we will have reported
1207 * PXE_PGP_CORRUPT_DATA before now. A key mismatch makes the other errors
1208 * into red herrings, and this avoids leaking clues to attackers.
1209 */
1210 if (ctx->unsupported_compr)
1211 return PXE_PGP_UNSUPPORTED_COMPR;
1212 if (ctx->unexpected_binary)
1213 return PXE_PGP_NOT_TEXT;
1214
1215 return res;
1216 }