Debian Codegraph @

contrib/pgcrypto/pgp-encrypt.c

raw
1 /*
2 * pgp-encrypt.c
3 * OpenPGP encrypt.
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-encrypt.c
30 */
31
32 #include "postgres.h"
33
34 #include <time.h>
35
36 #include "mbuf.h"
37 #include "pgp.h"
38 #include "px.h"
39
40 #define MDC_DIGEST_LEN 20
41 #define STREAM_ID 0xE0
42 #define STREAM_BLOCK_SHIFT 14
43
44 static uint8 *
45 render_newlen(uint8 *h, int len)
46 {
47 if (len <= 191)
48 {
49 *h++ = len & 255;
50 }
51 else if (len > 191 && len <= 8383)
52 {
53 *h++ = ((len - 192) >> 8) + 192;
54 *h++ = (len - 192) & 255;
55 }
56 else
57 {
58 *h++ = 255;
59 *h++ = (len >> 24) & 255;
60 *h++ = (len >> 16) & 255;
61 *h++ = (len >> 8) & 255;
62 *h++ = len & 255;
63 }
64 return h;
65 }
66
67 static int
68 write_tag_only(PushFilter *dst, int tag)
69 {
70 uint8 hdr = 0xC0 | tag;
71
72 return pushf_write(dst, &hdr, 1);
73 }
74
75 static int
76 write_normal_header(PushFilter *dst, int tag, int len)
77 {
78 uint8 hdr[8];
79 uint8 *h = hdr;
80
81 *h++ = 0xC0 | tag;
82 h = render_newlen(h, len);
83 return pushf_write(dst, hdr, h - hdr);
84 }
85
86
87 /*
88 * MAC writer
89 */
90
91 static int
92 mdc_init(PushFilter *dst, void *init_arg, void **priv_p)
93 {
94 int res;
95 PX_MD *md;
96
97 res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
98 if (res < 0)
99 return res;
100
101 *priv_p = md;
102 return 0;
103 }
104
105 static int
106 mdc_write(PushFilter *dst, void *priv, const uint8 *data, int len)
107 {
108 PX_MD *md = priv;
109
110 px_md_update(md, data, len);
111 return pushf_write(dst, data, len);
112 }
113
114 static int
115 mdc_flush(PushFilter *dst, void *priv)
116 {
117 int res;
118 uint8 pkt[2 + MDC_DIGEST_LEN];
119 PX_MD *md = priv;
120
121 /*
122 * create mdc pkt
123 */
124 pkt[0] = 0xD3;
125 pkt[1] = 0x14; /* MDC_DIGEST_LEN */
126 px_md_update(md, pkt, 2);
127 px_md_finish(md, pkt + 2);
128
129 res = pushf_write(dst, pkt, 2 + MDC_DIGEST_LEN);
130 px_memset(pkt, 0, 2 + MDC_DIGEST_LEN);
131 return res;
132 }
133
134 static void
135 mdc_free(void *priv)
136 {
137 PX_MD *md = priv;
138
139 px_md_free(md);
140 }
141
142 static const PushFilterOps mdc_filter = {
143 mdc_init, mdc_write, mdc_flush, mdc_free
144 };
145
146
147 /*
148 * Encrypted pkt writer
149 */
150 #define ENCBUF 8192
151 struct EncStat
152 {
153 PGP_CFB *ciph;
154 uint8 buf[ENCBUF];
155 };
156
157 static int
158 encrypt_init(PushFilter *next, void *init_arg, void **priv_p)
159 {
160 struct EncStat *st;
161 PGP_Context *ctx = init_arg;
162 PGP_CFB *ciph;
163 int resync = 1;
164 int res;
165
166 /* should we use newer packet format? */
167 if (ctx->disable_mdc == 0)
168 {
169 uint8 ver = 1;
170
171 resync = 0;
172 res = pushf_write(next, &ver, 1);
173 if (res < 0)
174 return res;
175 }
176 res = pgp_cfb_create(&ciph, ctx->cipher_algo,
177 ctx->sess_key, ctx->sess_key_len, resync, NULL,
178 0 /* never ignore cipher failures for encrypt */ );
179 if (res < 0)
180 return res;
181
182 st = palloc0(sizeof(*st));
183 st->ciph = ciph;
184
185 *priv_p = st;
186 return ENCBUF;
187 }
188
189 static int
190 encrypt_process(PushFilter *next, void *priv, const uint8 *data, int len)
191 {
192 int res;
193 struct EncStat *st = priv;
194 int avail = len;
195
196 while (avail > 0)
197 {
198 int tmplen = avail > ENCBUF ? ENCBUF : avail;
199
200 res = pgp_cfb_encrypt(st->ciph, data, tmplen, st->buf);
201 if (res < 0)
202 return res;
203
204 res = pushf_write(next, st->buf, tmplen);
205 if (res < 0)
206 return res;
207
208 data += tmplen;
209 avail -= tmplen;
210 }
211 return 0;
212 }
213
214 static void
215 encrypt_free(void *priv)
216 {
217 struct EncStat *st = priv;
218
219 if (st->ciph)
220 pgp_cfb_free(st->ciph);
221 px_memset(st, 0, sizeof(*st));
222 pfree(st);
223 }
224
225 static const PushFilterOps encrypt_filter = {
226 encrypt_init, encrypt_process, NULL, encrypt_free
227 };
228
229 /*
230 * Write Streamable pkts
231 */
232
233 struct PktStreamStat
234 {
235 int final_done;
236 int pkt_block;
237 };
238
239 static int
240 pkt_stream_init(PushFilter *next, void *init_arg, void **priv_p)
241 {
242 struct PktStreamStat *st;
243
244 st = palloc(sizeof(*st));
245 st->final_done = 0;
246 st->pkt_block = 1 << STREAM_BLOCK_SHIFT;
247 *priv_p = st;
248
249 return st->pkt_block;
250 }
251
252 static int
253 pkt_stream_process(PushFilter *next, void *priv, const uint8 *data, int len)
254 {
255 int res;
256 uint8 hdr[8];
257 uint8 *h = hdr;
258 struct PktStreamStat *st = priv;
259
260 if (st->final_done)
261 return PXE_BUG;
262
263 if (len == st->pkt_block)
264 *h++ = STREAM_ID | STREAM_BLOCK_SHIFT;
265 else
266 {
267 h = render_newlen(h, len);
268 st->final_done = 1;
269 }
270
271 res = pushf_write(next, hdr, h - hdr);
272 if (res < 0)
273 return res;
274
275 return pushf_write(next, data, len);
276 }
277
278 static int
279 pkt_stream_flush(PushFilter *next, void *priv)
280 {
281 int res;
282 uint8 hdr[8];
283 uint8 *h = hdr;
284 struct PktStreamStat *st = priv;
285
286 /* stream MUST end with normal packet. */
287 if (!st->final_done)
288 {
289 h = render_newlen(h, 0);
290 res = pushf_write(next, hdr, h - hdr);
291 if (res < 0)
292 return res;
293 st->final_done = 1;
294 }
295 return 0;
296 }
297
298 static void
299 pkt_stream_free(void *priv)
300 {
301 struct PktStreamStat *st = priv;
302
303 px_memset(st, 0, sizeof(*st));
304 pfree(st);
305 }
306
307 static const PushFilterOps pkt_stream_filter = {
308 pkt_stream_init, pkt_stream_process, pkt_stream_flush, pkt_stream_free
309 };
310
311 int
312 pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p)
313 {
314 int res;
315
316 res = write_tag_only(dst, tag);
317 if (res < 0)
318 return res;
319
320 return pushf_create(res_p, &pkt_stream_filter, NULL, dst);
321 }
322
323 /*
324 * Text conversion filter
325 */
326
327 static int
328 crlf_process(PushFilter *dst, void *priv, const uint8 *data, int len)
329 {
330 const uint8 *data_end = data + len;
331 const uint8 *p2,
332 *p1 = data;
333 int line_len;
334 static const uint8 crlf[] = {'\r', '\n'};
335 int res = 0;
336
337 while (p1 < data_end)
338 {
339 p2 = memchr(p1, '\n', data_end - p1);
340 if (p2 == NULL)
341 p2 = data_end;
342
343 line_len = p2 - p1;
344
345 /* write data */
346 res = 0;
347 if (line_len > 0)
348 {
349 res = pushf_write(dst, p1, line_len);
350 if (res < 0)
351 break;
352 p1 += line_len;
353 }
354
355 /* write crlf */
356 while (p1 < data_end && *p1 == '\n')
357 {
358 res = pushf_write(dst, crlf, 2);
359 if (res < 0)
360 break;
361 p1++;
362 }
363 }
364 return res;
365 }
366
367 static const PushFilterOps crlf_filter = {
368 NULL, crlf_process, NULL, NULL
369 };
370
371 /*
372 * Initialize literal data packet
373 */
374 static int
375 init_litdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
376 {
377 int res;
378 int hdrlen;
379 uint8 hdr[6];
380 uint32 t;
381 PushFilter *pkt;
382 int type;
383
384 /*
385 * Create header
386 */
387
388 if (ctx->text_mode)
389 type = ctx->unicode_mode ? 'u' : 't';
390 else
391 type = 'b';
392
393 /*
394 * Store the creation time into packet. The goal is to have as few known
395 * bytes as possible.
396 */
397 t = (uint32) time(NULL);
398
399 hdr[0] = type;
400 hdr[1] = 0;
401 hdr[2] = (t >> 24) & 255;
402 hdr[3] = (t >> 16) & 255;
403 hdr[4] = (t >> 8) & 255;
404 hdr[5] = t & 255;
405 hdrlen = 6;
406
407 res = write_tag_only(dst, PGP_PKT_LITERAL_DATA);
408 if (res < 0)
409 return res;
410
411 res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
412 if (res < 0)
413 return res;
414
415 res = pushf_write(pkt, hdr, hdrlen);
416 if (res < 0)
417 {
418 pushf_free(pkt);
419 return res;
420 }
421
422 *pf_res = pkt;
423 return 0;
424 }
425
426 /*
427 * Initialize compression filter
428 */
429 static int
430 init_compress(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
431 {
432 int res;
433 uint8 type = ctx->compress_algo;
434 PushFilter *pkt;
435
436 res = write_tag_only(dst, PGP_PKT_COMPRESSED_DATA);
437 if (res < 0)
438 return res;
439
440 res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
441 if (res < 0)
442 return res;
443
444 res = pushf_write(pkt, &type, 1);
445 if (res >= 0)
446 res = pgp_compress_filter(pf_res, ctx, pkt);
447
448 if (res < 0)
449 pushf_free(pkt);
450
451 return res;
452 }
453
454 /*
455 * Initialize encdata packet
456 */
457 static int
458 init_encdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
459 {
460 int res;
461 int tag;
462
463 if (ctx->disable_mdc)
464 tag = PGP_PKT_SYMENCRYPTED_DATA;
465 else
466 tag = PGP_PKT_SYMENCRYPTED_DATA_MDC;
467
468 res = write_tag_only(dst, tag);
469 if (res < 0)
470 return res;
471
472 return pushf_create(pf_res, &pkt_stream_filter, ctx, dst);
473 }
474
475 /*
476 * write prefix
477 */
478 static int
479 write_prefix(PGP_Context *ctx, PushFilter *dst)
480 {
481 uint8 prefix[PGP_MAX_BLOCK + 2];
482 int res,
483 bs;
484
485 bs = pgp_get_cipher_block_size(ctx->cipher_algo);
486 if (!pg_strong_random(prefix, bs))
487 return PXE_NO_RANDOM;
488
489 prefix[bs + 0] = prefix[bs - 2];
490 prefix[bs + 1] = prefix[bs - 1];
491
492 res = pushf_write(dst, prefix, bs + 2);
493 px_memset(prefix, 0, bs + 2);
494 return res < 0 ? res : 0;
495 }
496
497 /*
498 * write symmetrically encrypted session key packet
499 */
500
501 static int
502 symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)
503 {
504 int res;
505 PGP_CFB *cfb;
506 uint8 algo = ctx->cipher_algo;
507
508 res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
509 ctx->s2k.key, ctx->s2k.key_len, 0, NULL,
510 0 /* never ignore cipher failures for encrypt */ );
511 if (res < 0)
512 return res;
513
514 pgp_cfb_encrypt(cfb, &algo, 1, dst);
515 pgp_cfb_encrypt(cfb, ctx->sess_key, ctx->sess_key_len, dst + 1);
516
517 pgp_cfb_free(cfb);
518 return ctx->sess_key_len + 1;
519 }
520
521 /* 5.3: Symmetric-Key Encrypted Session-Key */
522 static int
523 write_symenc_sesskey(PGP_Context *ctx, PushFilter *dst)
524 {
525 uint8 pkt[256];
526 int pktlen;
527 int res;
528 uint8 *p = pkt;
529
530 *p++ = 4; /* 5.3 - version number */
531 *p++ = ctx->s2k_cipher_algo;
532
533 *p++ = ctx->s2k.mode;
534 *p++ = ctx->s2k.digest_algo;
535 if (ctx->s2k.mode > 0)
536 {
537 memcpy(p, ctx->s2k.salt, 8);
538 p += 8;
539 }
540 if (ctx->s2k.mode == 3)
541 *p++ = ctx->s2k.iter;
542
543 if (ctx->use_sess_key)
544 {
545 res = symencrypt_sesskey(ctx, p);
546 if (res < 0)
547 return res;
548 p += res;
549 }
550
551 pktlen = p - pkt;
552 res = write_normal_header(dst, PGP_PKT_SYMENCRYPTED_SESSKEY, pktlen);
553 if (res >= 0)
554 res = pushf_write(dst, pkt, pktlen);
555
556 px_memset(pkt, 0, pktlen);
557 return res;
558 }
559
560 /*
561 * key setup
562 */
563 static int
564 init_s2k_key(PGP_Context *ctx)
565 {
566 int res;
567
568 if (ctx->s2k_cipher_algo < 0)
569 ctx->s2k_cipher_algo = ctx->cipher_algo;
570
571 res = pgp_s2k_fill(&ctx->s2k, ctx->s2k_mode, ctx->s2k_digest_algo, ctx->s2k_count);
572 if (res < 0)
573 return res;
574
575 return pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
576 ctx->sym_key, ctx->sym_key_len);
577 }
578
579 static int
580 init_sess_key(PGP_Context *ctx)
581 {
582 if (ctx->use_sess_key || ctx->pub_key)
583 {
584 ctx->sess_key_len = pgp_get_cipher_key_size(ctx->cipher_algo);
585 if (!pg_strong_random(ctx->sess_key, ctx->sess_key_len))
586 return PXE_NO_RANDOM;
587 }
588 else
589 {
590 ctx->sess_key_len = ctx->s2k.key_len;
591 memcpy(ctx->sess_key, ctx->s2k.key, ctx->s2k.key_len);
592 }
593
594 return 0;
595 }
596
597 /*
598 * combine
599 */
600 int
601 pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
602 {
603 int res;
604 int len;
605 uint8 *buf;
606 PushFilter *pf,
607 *pf_tmp;
608
609 /*
610 * do we have any key
611 */
612 if (!ctx->sym_key && !ctx->pub_key)
613 return PXE_ARGUMENT_ERROR;
614
615 /* MBuf writer */
616 res = pushf_create_mbuf_writer(&pf, dst);
617 if (res < 0)
618 goto out;
619
620 /*
621 * initialize sym_key
622 */
623 if (ctx->sym_key)
624 {
625 res = init_s2k_key(ctx);
626 if (res < 0)
627 goto out;
628 }
629
630 res = init_sess_key(ctx);
631 if (res < 0)
632 goto out;
633
634 /*
635 * write keypkt
636 */
637 if (ctx->pub_key)
638 res = pgp_write_pubenc_sesskey(ctx, pf);
639 else
640 res = write_symenc_sesskey(ctx, pf);
641 if (res < 0)
642 goto out;
643
644 /* encrypted data pkt */
645 res = init_encdata_packet(&pf_tmp, ctx, pf);
646 if (res < 0)
647 goto out;
648 pf = pf_tmp;
649
650 /* encrypter */
651 res = pushf_create(&pf_tmp, &encrypt_filter, ctx, pf);
652 if (res < 0)
653 goto out;
654 pf = pf_tmp;
655
656 /* hasher */
657 if (ctx->disable_mdc == 0)
658 {
659 res = pushf_create(&pf_tmp, &mdc_filter, ctx, pf);
660 if (res < 0)
661 goto out;
662 pf = pf_tmp;
663 }
664
665 /* prefix */
666 res = write_prefix(ctx, pf);
667 if (res < 0)
668 goto out;
669
670 /* compressor */
671 if (ctx->compress_algo > 0 && ctx->compress_level > 0)
672 {
673 res = init_compress(&pf_tmp, ctx, pf);
674 if (res < 0)
675 goto out;
676 pf = pf_tmp;
677 }
678
679 /* data streamer */
680 res = init_litdata_packet(&pf_tmp, ctx, pf);
681 if (res < 0)
682 goto out;
683 pf = pf_tmp;
684
685
686 /* text conversion? */
687 if (ctx->text_mode && ctx->convert_crlf)
688 {
689 res = pushf_create(&pf_tmp, &crlf_filter, ctx, pf);
690 if (res < 0)
691 goto out;
692 pf = pf_tmp;
693 }
694
695 /*
696 * chain complete
697 */
698
699 len = mbuf_grab(src, mbuf_avail(src), &buf);
700 res = pushf_write(pf, buf, len);
701 if (res >= 0)
702 res = pushf_flush(pf);
703 out:
704 pushf_free_all(pf);
705 return res;
706 }