Debian Codegraph @

g10/armor.c

raw ◀ ▶
Source reconstructed at patch state 0 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /* armor.c - Armor filter
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007 Free Software Foundation, Inc.
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 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <ctype.h>
27
28 #include "gpg.h"
29 #include "../common/status.h"
30 #include "../common/iobuf.h"
31 #include "../common/util.h"
32 #include "filter.h"
33 #include "packet.h"
34 #include "options.h"
35 #include "main.h"
36 #include "../common/i18n.h"
37
38 #define MAX_LINELEN 20000
39
40 static const byte bintoasc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyz"
42 "0123456789+/";
43 static u32 asctobin[4][256]; /* runtime initialized */
44 static int is_initialized;
45
46
47 typedef enum {
48 fhdrHASArmor = 0,
49 fhdrNOArmor,
50 fhdrINIT,
51 fhdrINITCont,
52 fhdrINITSkip,
53 fhdrCHECKBegin,
54 fhdrWAITHeader,
55 fhdrWAITClearsig,
56 fhdrSKIPHeader,
57 fhdrCLEARSIG,
58 fhdrREADClearsig,
59 fhdrNullClearsig,
60 fhdrEMPTYClearsig,
61 fhdrCHECKClearsig,
62 fhdrCHECKClearsig2,
63 fhdrCHECKDashEscaped,
64 fhdrCHECKDashEscaped2,
65 fhdrCHECKDashEscaped3,
66 fhdrREADClearsigNext,
67 fhdrENDClearsig,
68 fhdrENDClearsigHelp,
69 fhdrTESTSpaces,
70 fhdrCLEARSIGSimple,
71 fhdrCLEARSIGSimpleNext,
72 fhdrTEXT,
73 fhdrTEXTSimple,
74 fhdrERROR,
75 fhdrERRORShow,
76 fhdrEOF
77 } fhdr_state_t;
78
79
80 /* if we encounter this armor string with this index, go
81 * into a mode which fakes packets and wait for the next armor */
82 #define BEGIN_SIGNATURE 2
83 #define BEGIN_SIGNED_MSG_IDX 3
84 static char *head_strings[] = {
85 "BEGIN PGP MESSAGE",
86 "BEGIN PGP PUBLIC KEY BLOCK",
87 "BEGIN PGP SIGNATURE",
88 "BEGIN PGP SIGNED MESSAGE",
89 "BEGIN PGP ARMORED FILE", /* gnupg extension */
90 "BEGIN PGP PRIVATE KEY BLOCK",
91 "BEGIN PGP SECRET KEY BLOCK", /* only used by pgp2 */
92 NULL
93 };
94 static char *tail_strings[] = {
95 "END PGP MESSAGE",
96 "END PGP PUBLIC KEY BLOCK",
97 "END PGP SIGNATURE",
98 "END dummy",
99 "END PGP ARMORED FILE",
100 "END PGP PRIVATE KEY BLOCK",
101 "END PGP SECRET KEY BLOCK",
102 NULL
103 };
104
105
106 static int armor_filter ( void *opaque, int control,
107 iobuf_t chain, byte *buf, size_t *ret_len);
108
109
110
111
112 /* Create a new context for armor filters. */
113 armor_filter_context_t *
114 new_armor_context (void)
115 {
116 armor_filter_context_t *afx;
117 gpg_error_t err;
118
119 afx = xcalloc (1, sizeof *afx);
120 if (afx)
121 {
122 err = gcry_md_open (&afx->crc_md, GCRY_MD_CRC24_RFC2440, 0);
123 if (err != 0)
124 {
125 log_error ("gcry_md_open failed for GCRY_MD_CRC24_RFC2440: %s",
126 gpg_strerror (err));
127 xfree (afx);
128 return NULL;
129 }
130
131 afx->refcount = 1;
132 }
133
134 return afx;
135 }
136
137 /* Release an armor filter context. Passing NULL is explicitly
138 allowed and a no-op. */
139 void
140 release_armor_context (armor_filter_context_t *afx)
141 {
142 if (!afx)
143 return;
144 log_assert (afx->refcount);
145 if ( --afx->refcount )
146 return;
147 gcry_md_close (afx->crc_md);
148 xfree (afx);
149 }
150
151 /* Push the armor filter onto the iobuf stream IOBUF. */
152 int
153 push_armor_filter (armor_filter_context_t *afx, iobuf_t iobuf)
154 {
155 int rc;
156
157 afx->refcount++;
158 rc = iobuf_push_filter (iobuf, armor_filter, afx);
159 if (rc)
160 afx->refcount--;
161 return rc;
162 }
163
164
165
166
167
168 static void
169 initialize(void)
170 {
171 u32 i;
172 const byte *s;
173
174 /* Build the helptable for radix64 to bin conversion. Value 0xffffffff is
175 used to detect invalid characters. */
176 memset (asctobin, 0xff, sizeof(asctobin));
177 for(s=bintoasc,i=0; *s; s++,i++ )
178 {
179 asctobin[0][*s] = i << (0 * 6);
180 asctobin[1][*s] = i << (1 * 6);
181 asctobin[2][*s] = i << (2 * 6);
182 asctobin[3][*s] = i << (3 * 6);
183 }
184
185 is_initialized=1;
186 }
187
188
189 static inline u32
190 get_afx_crc (armor_filter_context_t *afx)
191 {
192 const byte *crc_buf;
193 u32 crc;
194
195 crc_buf = gcry_md_read (afx->crc_md, GCRY_MD_CRC24_RFC2440);
196
197 crc = crc_buf[0];
198 crc <<= 8;
199 crc |= crc_buf[1];
200 crc <<= 8;
201 crc |= crc_buf[2];
202
203 return crc;
204 }
205
206
207 /*
208 * Check whether this is an armored file. See also
209 * parse-packet.c for details on this code.
210 *
211 * Note that the buffer BUF needs to be at least 2 bytes long. If in
212 * doubt that the second byte to 0.
213 *
214 * Returns: True if it seems to be armored
215 */
216 static int
217 is_armored (const byte *buf)
218 {
219 int ctb, pkttype;
220 int indeterminate_length_allowed;
221
222 ctb = *buf;
223 if( !(ctb & 0x80) )
224 /* The most significant bit of the CTB must be set. Since it is
225 cleared, this is not a binary OpenPGP message. Assume it is
226 armored. */
227 return 1;
228
229 pkttype = ctb & 0x40 ? (ctb & 0x3f) : ((ctb>>2)&0xf);
230 switch( pkttype ) {
231 case PKT_PUBKEY_ENC:
232 case PKT_SIGNATURE:
233 case PKT_SYMKEY_ENC:
234 case PKT_ONEPASS_SIG:
235 case PKT_SECRET_KEY:
236 case PKT_PUBLIC_KEY:
237 case PKT_SECRET_SUBKEY:
238 case PKT_MARKER:
239 case PKT_RING_TRUST:
240 case PKT_USER_ID:
241 case PKT_PUBLIC_SUBKEY:
242 case PKT_ATTRIBUTE:
243 case PKT_MDC:
244 indeterminate_length_allowed = 0;
245 break;
246
247 case PKT_COMPRESSED:
248 case PKT_ENCRYPTED:
249 case PKT_ENCRYPTED_MDC:
250 case PKT_ENCRYPTED_AEAD:
251 case PKT_PLAINTEXT:
252 case PKT_OLD_COMMENT:
253 case PKT_COMMENT:
254 case PKT_GPG_CONTROL:
255 indeterminate_length_allowed = 1;
256 break;
257
258 default:
259 /* Invalid packet type. */
260 return 1;
261 }
262
263 if (! indeterminate_length_allowed)
264 /* It is only legal to use an indeterminate length with a few
265 packet types. If a packet uses an indeterminate length, but
266 that is not allowed, then the data is not valid binary
267 OpenPGP data. */
268 {
269 int new_format;
270 int indeterminate_length;
271
272 new_format = !! (ctb & (1 << 6));
273 if (new_format)
274 indeterminate_length = (buf[1] >= 224 && buf[1] < 255);
275 else
276 indeterminate_length = (ctb & 3) == 3;
277
278 if (indeterminate_length)
279 return 1;
280 }
281
282 /* The first CTB seems legit. It is probably not armored
283 data. */
284 return 0;
285 }
286
287
288 /****************
289 * Try to check whether the iobuf is armored
290 * Returns true if this may be the case; the caller should use the
291 * filter to do further processing.
292 */
293 int
294 use_armor_filter( IOBUF a )
295 {
296 byte buf[2];
297 int n;
298
299 /* fixme: there might be a problem with iobuf_peek */
300 n = iobuf_peek (a, buf, 2);
301 if( n == -1 )
302 return 0; /* EOF, doesn't matter whether armored or not */
303 if( !n )
304 return 1; /* can't check it: try armored */
305 if (n != 2)
306 return 0; /* short buffer */
307 return is_armored(buf);
308 }
309
310
311
312
313 static void
314 invalid_armor(void)
315 {
316 write_status(STATUS_BADARMOR);
317 g10_exit(1); /* stop here */
318 }
319
320
321 /****************
322 * check whether the armor header is valid on a signed message.
323 * this is for security reasons: the header lines are not included in the
324 * hash and by using some creative formatting rules, Mallory could fake
325 * any text at the beginning of a document; assuming it is read with
326 * a simple viewer. We only allow the Hash Header.
327 */
328 static int
329 parse_hash_header( const char *line )
330 {
331 const char *s, *s2;
332 unsigned found = 0;
333
334 if( strlen(line) < 6 || strlen(line) > 60 )
335 return 0; /* too short or too long */
336 if( memcmp( line, "Hash:", 5 ) )
337 return 0; /* invalid header */
338
339 for(s=line+5;;s=s2) {
340 for(; *s && (*s==' ' || *s == '\t'); s++ )
341 ;
342 if( !*s )
343 break;
344 for(s2=s+1; *s2 && *s2!=' ' && *s2 != '\t' && *s2 != ','; s2++ )
345 ;
346 if( !strncmp( s, "RIPEMD160", s2-s ) )
347 found |= 1;
348 else if( !strncmp( s, "SHA1", s2-s ) )
349 found |= 2;
350 else if( !strncmp( s, "SHA224", s2-s ) )
351 found |= 8;
352 else if( !strncmp( s, "SHA256", s2-s ) )
353 found |= 16;
354 else if( !strncmp( s, "SHA384", s2-s ) )
355 found |= 32;
356 else if( !strncmp( s, "SHA512", s2-s ) )
357 found |= 64;
358 else
359 return 0;
360 for(; *s2 && (*s2==' ' || *s2 == '\t'); s2++ )
361 ;
362 if( *s2 && *s2 != ',' )
363 return 0;
364 if( *s2 )
365 s2++;
366 }
367 return found;
368 }
369
370 /* Returns true if this is a valid armor tag as per RFC-2440bis-21. */
371 static int
372 is_armor_tag(const char *line)
373 {
374 if(strncmp(line,"Version",7)==0
375 || strncmp(line,"Comment",7)==0
376 || strncmp(line,"MessageID",9)==0
377 || strncmp(line,"Hash",4)==0
378 || strncmp(line,"Charset",7)==0)
379 return 1;
380
381 return 0;
382 }
383
384 /****************
385 * Check whether this is a armor line. Returns: -1 if it is not a
386 * armor header, 42 if it is a generic header, or the index number of
387 * the armor header.
388 */
389 static int
390 is_armor_header( byte *line, unsigned len )
391 {
392 const char *s;
393 byte *save_p, *p;
394 int save_c;
395 int i;
396
397 if( len < 15 )
398 return -1; /* too short */
399 if( memcmp( line, "-----", 5 ) )
400 return -1; /* no */
401 p = strstr( line+5, "-----");
402 if( !p )
403 return -1;
404 save_p = p;
405 p += 5;
406
407 /* Some Windows environments seem to add whitespace to the end of
408 the line, so we strip it here. This becomes strict if
409 --rfc2440 is set since 2440 reads "The header lines, therefore,
410 MUST start at the beginning of a line, and MUST NOT have text
411 following them on the same line." It is unclear whether "text"
412 refers to all text or just non-whitespace text. 4880 clarified
413 this was only non-whitespace text. */
414
415 if(RFC2440)
416 {
417 if( *p == '\r' )
418 p++;
419 if( *p == '\n' )
420 p++;
421 }
422 else
423 while(*p==' ' || *p=='\r' || *p=='\n' || *p=='\t')
424 p++;
425
426 if( *p )
427 return -1; /* garbage after dashes */
428 save_c = *save_p; *save_p = 0;
429 p = line+5;
430 for(i=0; (s=head_strings[i]); i++ )
431 if( !strcmp(s, p) )
432 break;
433 *save_p = save_c;
434 if (!s)
435 {
436 if (!strncmp (p, "BEGIN ", 6))
437 return 42;
438 return -1; /* unknown armor line */
439 }
440
441 if( opt.verbose > 1 )
442 log_info(_("armor: %s\n"), head_strings[i]);
443 return i;
444 }
445
446
447
448 /****************
449 * Parse a header lines
450 * Return 0: Empty line (end of header lines)
451 * -1: invalid header line
452 * >0: Good header line
453 */
454 static int
455 parse_header_line( armor_filter_context_t *afx, byte *line, unsigned int len )
456 {
457 byte *p;
458 int hashes=0;
459 unsigned int len2;
460
461 len2 = length_sans_trailing_ws ( line, len );
462 if( !len2 ) {
463 afx->buffer_pos = len2; /* (it is not the fine way to do it here) */
464 return 0; /* WS only: same as empty line */
465 }
466
467 /*
468 This is fussy. The spec says that a header line is delimited
469 with a colon-space pair. This means that a line such as
470 "Comment: " (with nothing else) is actually legal as an empty
471 string comment. However, email and cut-and-paste being what it
472 is, that trailing space may go away. Therefore, we accept empty
473 headers delimited with only a colon. --rfc2440, as always,
474 makes this strict and enforces the colon-space pair. -dms
475 */
476
477 p = strchr( line, ':');
478 if (!p && afx->dearmor_state)
479 return 0; /* Special treatment in --dearmor mode. */
480 if( !p || (RFC2440 && p[1]!=' ')
481 || (!RFC2440 && p[1]!=' ' && p[1]!='\n' && p[1]!='\r'))
482 {
483 log_error (_("invalid armor header: "));
484 es_write_sanitized (log_get_stream (), line, len, NULL, NULL);
485 log_printf ("\n");
486 return -1;
487 }
488
489 /* Chop off the whitespace we detected before */
490 len=len2;
491 line[len2]='\0';
492
493 if( opt.verbose ) {
494 log_info(_("armor header: "));
495 es_write_sanitized (log_get_stream (), line, len, NULL, NULL);
496 log_printf ("\n");
497 }
498
499 if (afx->dearmor_mode)
500 ;
501 else if (afx->in_cleartext)
502 {
503 if( (hashes=parse_hash_header( line )) )
504 afx->hashes |= hashes;
505 else if( strlen(line) > 15 && !memcmp( line, "NotDashEscaped:", 15 ) )
506 afx->not_dash_escaped = 1;
507 else
508 {
509 log_error(_("invalid clearsig header\n"));
510 return -1;
511 }
512 }
513 else if(!is_armor_tag(line))
514 {
515 /* Section 6.2: "Unknown keys should be reported to the user,
516 but OpenPGP should continue to process the message." Note
517 that in a clearsigned message this applies to the signature
518 part (i.e. "BEGIN PGP SIGNATURE") and not the signed data
519 ("BEGIN PGP SIGNED MESSAGE"). The only key allowed in the
520 signed data section is "Hash". */
521
522 log_info(_("unknown armor header: "));
523 es_write_sanitized (log_get_stream (), line, len, NULL, NULL);
524 log_printf ("\n");
525 }
526
527 return 1;
528 }
529
530
531
532 /* figure out whether the data is armored or not */
533 static int
534 check_input( armor_filter_context_t *afx, IOBUF a )
535 {
536 int rc = 0;
537 int i;
538 byte *line;
539 unsigned len;
540 unsigned maxlen;
541 int hdr_line = -1;
542
543 /* read the first line to see whether this is armored data */
544 maxlen = MAX_LINELEN;
545 len = afx->buffer_len = iobuf_read_line( a, &afx->buffer,
546 &afx->buffer_size, &maxlen );
547 line = afx->buffer;
548 if( !maxlen ) {
549 /* line has been truncated: assume not armored */
550 afx->inp_checked = 1;
551 afx->inp_bypass = 1;
552 return 0;
553 }
554
555 if( !len ) {
556 return -1; /* eof */
557 }
558
559 /* (the line is always a C string but maybe longer) */
560 if( *line == '\n' || ( len && (*line == '\r' && line[1]=='\n') ) )
561 ;
562 else if (len >= 2 && !is_armored (line)) {
563 afx->inp_checked = 1;
564 afx->inp_bypass = 1;
565 return 0;
566 }
567
568 /* find the armor header */
569 while(len) {
570 i = is_armor_header( line, len );
571 if ( i == 42 ) {
572 if (afx->dearmor_mode) {
573 afx->dearmor_state = 1;
574 break;
575 }
576 }
577 else if (i >= 0
578 && !(afx->only_keyblocks && i != 1 && i != 5 && i != 6 )) {
579 hdr_line = i;
580 if( hdr_line == BEGIN_SIGNED_MSG_IDX ) {
581 if( afx->in_cleartext ) {
582 log_error(_("nested clear text signatures\n"));
583 rc = gpg_error (GPG_ERR_INV_ARMOR);
584 }
585 afx->in_cleartext = 1;
586 }
587 break;
588 }
589
590 /* read the next line (skip all truncated lines) */
591 do {
592 maxlen = MAX_LINELEN;
593 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
594 &afx->buffer_size, &maxlen );
595 line = afx->buffer;
596 len = afx->buffer_len;
597 } while( !maxlen );
598 }
599
600 /* Parse the header lines. */
601 while(len) {
602 /* Read the next line (skip all truncated lines). */
603 do {
604 maxlen = MAX_LINELEN;
605 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
606 &afx->buffer_size, &maxlen );
607 line = afx->buffer;
608 len = afx->buffer_len;
609 } while( !maxlen );
610
611 i = parse_header_line( afx, line, len );
612 if( i <= 0 ) {
613 if (i && RFC2440)
614 rc = GPG_ERR_INV_ARMOR;
615 break;
616 }
617 }
618
619
620 if( rc )
621 invalid_armor();
622 else if( afx->in_cleartext )
623 afx->faked = 1;
624 else {
625 afx->inp_checked = 1;
626 gcry_md_reset (afx->crc_md);
627 afx->idx = 0;
628 afx->radbuf[0] = 0;
629 }
630
631 return rc;
632 }
633
634 #define PARTIAL_CHUNK 512
635 #define PARTIAL_POW 9
636
637 /****************
638 * Fake a literal data packet and wait for the next armor line
639 * fixme: empty line handling and null length clear text signature are
640 * not implemented/checked.
641 */
642 static int
643 fake_packet( armor_filter_context_t *afx, IOBUF a,
644 size_t *retn, byte *buf, size_t size )
645 {
646 int rc = 0;
647 size_t len = 0;
648 int lastline = 0;
649 unsigned maxlen, n;
650 byte *p;
651 byte tempbuf[PARTIAL_CHUNK];
652 size_t tempbuf_len=0;
653 int this_truncated;
654
655 while( !rc && size-len>=(PARTIAL_CHUNK+1)) {
656 /* copy what we have in the line buffer */
657 if( afx->faked == 1 )
658 afx->faked++; /* skip the first (empty) line */
659 else
660 {
661 /* It's full, so write this partial chunk */
662 if(tempbuf_len==PARTIAL_CHUNK)
663 {
664 buf[len++]=0xE0+PARTIAL_POW;
665 memcpy(&buf[len],tempbuf,PARTIAL_CHUNK);
666 len+=PARTIAL_CHUNK;
667 tempbuf_len=0;
668 continue;
669 }
670
671 while( tempbuf_len < PARTIAL_CHUNK
672 && afx->buffer_pos < afx->buffer_len )
673 tempbuf[tempbuf_len++] = afx->buffer[afx->buffer_pos++];
674 if( tempbuf_len==PARTIAL_CHUNK )
675 continue;
676 }
677
678 /* read the next line */
679 maxlen = MAX_LINELEN;
680 afx->buffer_pos = 0;
681 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
682 &afx->buffer_size, &maxlen );
683 if( !afx->buffer_len ) {
684 rc = -1; /* eof (should not happen) */
685 continue;
686 }
687 if( !maxlen )
688 {
689 afx->truncated++;
690 this_truncated = 1;
691 }
692 else
693 this_truncated = 0;
694
695
696 p = afx->buffer;
697 n = afx->buffer_len;
698
699 /* Armor header or dash-escaped line? */
700 if(p[0]=='-')
701 {
702 /* 2440bis-10: When reversing dash-escaping, an
703 implementation MUST strip the string "- " if it occurs
704 at the beginning of a line, and SHOULD warn on "-" and
705 any character other than a space at the beginning of a
706 line. */
707
708 if(p[1]==' ' && !afx->not_dash_escaped)
709 {
710 /* It's a dash-escaped line, so skip over the
711 escape. */
712 afx->buffer_pos = 2;
713 }
714 else if(p[1]=='-' && p[2]=='-' && p[3]=='-' && p[4]=='-')
715 {
716 /* Five dashes in a row mean it's probably armor
717 header. */
718 int type = is_armor_header( p, n );
719 if (type == 42)
720 type = -1; /* Only OpenPGP armors are expected. */
721 if( afx->not_dash_escaped && type != BEGIN_SIGNATURE )
722 ; /* this is okay */
723 else
724 {
725 if( type != BEGIN_SIGNATURE )
726 {
727 log_info(_("unexpected armor: "));
728 es_write_sanitized (log_get_stream (), p, n,
729 NULL, NULL);
730 log_printf ("\n");
731 }
732
733 lastline = 1;
734 rc = -1;
735 }
736 }
737 else if(!afx->not_dash_escaped)
738 {
739 /* Bad dash-escaping. */
740 log_info (_("invalid dash escaped line: "));
741 es_write_sanitized (log_get_stream (), p, n, NULL, NULL);
742 log_printf ("\n");
743 }
744 }
745
746 /* Now handle the end-of-line canonicalization */
747 if( !afx->not_dash_escaped || this_truncated)
748 {
749 int crlf = n > 1 && p[n-2] == '\r' && p[n-1]=='\n';
750
751 afx->buffer_len=
752 trim_trailing_chars( &p[afx->buffer_pos], n-afx->buffer_pos,
753 " \t\r\n");
754 afx->buffer_len+=afx->buffer_pos;
755 /* the buffer is always allocated with enough space to append
756 * the removed [CR], LF and a Nul
757 * The reason for this complicated procedure is to keep at least
758 * the original type of lineending - handling of the removed
759 * trailing spaces seems to be impossible in our method
760 * of faking a packet; either we have to use a temporary file
761 * or calculate the hash here in this module and somehow find
762 * a way to send the hash down the processing line (well, a special
763 * faked packet could do the job).
764 *
765 * To make sure that a truncated line triggers a bad
766 * signature error we replace a removed LF by a FF or
767 * append a FF. Right, this is a hack but better than a
768 * global variable and way easier than to introduce a new
769 * control packet or insert a line like "[truncated]\n"
770 * into the filter output.
771 */
772 if( crlf )
773 afx->buffer[afx->buffer_len++] = '\r';
774 afx->buffer[afx->buffer_len++] = this_truncated? '\f':'\n';
775 afx->buffer[afx->buffer_len] = '\0';
776 }
777 }
778
779 if( lastline ) { /* write last (ending) length header */
780 if(tempbuf_len<192)
781 buf[len++]=tempbuf_len;
782 else
783 {
784 buf[len++]=((tempbuf_len-192)/256) + 192;
785 buf[len++]=(tempbuf_len-192) % 256;
786 }
787 memcpy(&buf[len],tempbuf,tempbuf_len);
788 len+=tempbuf_len;
789
790 rc = 0;
791 afx->faked = 0;
792 afx->in_cleartext = 0;
793 /* and now read the header lines */
794 afx->buffer_pos = 0;
795 for(;;) {
796 int i;
797
798 /* read the next line (skip all truncated lines) */
799 do {
800 maxlen = MAX_LINELEN;
801 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
802 &afx->buffer_size, &maxlen );
803 } while( !maxlen );
804 p = afx->buffer;
805 n = afx->buffer_len;
806 if( !n ) {
807 rc = -1;
808 break; /* eof */
809 }
810 i = parse_header_line( afx, p , n );
811 if( i <= 0 ) {
812 if( i )
813 invalid_armor();
814 break;
815 }
816 }
817 afx->inp_checked = 1;
818 gcry_md_reset (afx->crc_md);
819 afx->idx = 0;
820 afx->radbuf[0] = 0;
821 }
822
823 *retn = len;
824 return rc;
825 }
826
827
828 static int
829 invalid_crc(void)
830 {
831 if ( opt.ignore_crc_error )
832 return 0;
833 log_inc_errorcount();
834 return gpg_error (GPG_ERR_INV_ARMOR);
835 }
836
837
838 static int
839 radix64_read( armor_filter_context_t *afx, IOBUF a, size_t *retn,
840 byte *buf, size_t size )
841 {
842 byte val;
843 int c;
844 u32 binc;
845 int checkcrc=0;
846 int rc = 0;
847 size_t n = 0;
848 int idx, onlypad=0;
849 int skip_fast = 0;
850
851 idx = afx->idx;
852 val = afx->radbuf[0];
853 for( n=0; n < size; ) {
854
855 if( afx->buffer_pos < afx->buffer_len )
856 c = afx->buffer[afx->buffer_pos++];
857 else { /* read the next line */
858 unsigned maxlen = MAX_LINELEN;
859 afx->buffer_pos = 0;
860 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
861 &afx->buffer_size, &maxlen );
862 if( !maxlen )
863 afx->truncated++;
864 if( !afx->buffer_len )
865 break; /* eof */
866 continue;
867 }
868
869 again:
870 binc = asctobin[0][c];
871
872 if( binc != 0xffffffffUL )
873 {
874 if( idx == 0 && skip_fast == 0
875 && afx->buffer_pos + (16 - 1) < afx->buffer_len
876 && n + 12 < size)
877 {
878 /* Fast path for radix64 to binary conversion. */
879 u32 b0,b1,b2,b3;
880
881 /* Speculatively load 15 more input bytes. */
882 b0 = binc << (3 * 6);
883 b0 |= asctobin[2][afx->buffer[afx->buffer_pos + 0]];
884 b0 |= asctobin[1][afx->buffer[afx->buffer_pos + 1]];
885 b0 |= asctobin[0][afx->buffer[afx->buffer_pos + 2]];
886 b1 = asctobin[3][afx->buffer[afx->buffer_pos + 3]];
887 b1 |= asctobin[2][afx->buffer[afx->buffer_pos + 4]];
888 b1 |= asctobin[1][afx->buffer[afx->buffer_pos + 5]];
889 b1 |= asctobin[0][afx->buffer[afx->buffer_pos + 6]];
890 b2 = asctobin[3][afx->buffer[afx->buffer_pos + 7]];
891 b2 |= asctobin[2][afx->buffer[afx->buffer_pos + 8]];
892 b2 |= asctobin[1][afx->buffer[afx->buffer_pos + 9]];
893 b2 |= asctobin[0][afx->buffer[afx->buffer_pos + 10]];
894 b3 = asctobin[3][afx->buffer[afx->buffer_pos + 11]];
895 b3 |= asctobin[2][afx->buffer[afx->buffer_pos + 12]];
896 b3 |= asctobin[1][afx->buffer[afx->buffer_pos + 13]];
897 b3 |= asctobin[0][afx->buffer[afx->buffer_pos + 14]];
898
899 /* Check if any of the input bytes were invalid. */
900 if( (b0 | b1 | b2 | b3) != 0xffffffffUL )
901 {
902 /* All 16 bytes are valid. */
903 buf[n + 0] = b0 >> (2 * 8);
904 buf[n + 1] = b0 >> (1 * 8);
905 buf[n + 2] = b0 >> (0 * 8);
906 buf[n + 3] = b1 >> (2 * 8);
907 buf[n + 4] = b1 >> (1 * 8);
908 buf[n + 5] = b1 >> (0 * 8);
909 buf[n + 6] = b2 >> (2 * 8);
910 buf[n + 7] = b2 >> (1 * 8);
911 buf[n + 8] = b2 >> (0 * 8);
912 buf[n + 9] = b3 >> (2 * 8);
913 buf[n + 10] = b3 >> (1 * 8);
914 buf[n + 11] = b3 >> (0 * 8);
915 afx->buffer_pos += 16 - 1;
916 n += 12;
917 continue;
918 }
919 else if( b0 == 0xffffffffUL )
920 {
921 /* byte[1..3] have invalid character(s). Switch to slow
922 path. */
923 skip_fast = 1;
924 }
925 else if( b1 == 0xffffffffUL )
926 {
927 /* byte[4..7] have invalid character(s), first 4 bytes are
928 valid. */
929 buf[n + 0] = b0 >> (2 * 8);
930 buf[n + 1] = b0 >> (1 * 8);
931 buf[n + 2] = b0 >> (0 * 8);
932 afx->buffer_pos += 4 - 1;
933 n += 3;
934 skip_fast = 1;
935 continue;
936 }
937 else if( b2 == 0xffffffffUL )
938 {
939 /* byte[8..11] have invalid character(s), first 8 bytes are
940 valid. */
941 buf[n + 0] = b0 >> (2 * 8);
942 buf[n + 1] = b0 >> (1 * 8);
943 buf[n + 2] = b0 >> (0 * 8);
944 buf[n + 3] = b1 >> (2 * 8);
945 buf[n + 4] = b1 >> (1 * 8);
946 buf[n + 5] = b1 >> (0 * 8);
947 afx->buffer_pos += 8 - 1;
948 n += 6;
949 skip_fast = 1;
950 continue;
951 }
952 else /*if( b3 == 0xffffffffUL )*/
953 {
954 /* byte[12..15] have invalid character(s), first 12 bytes
955 are valid. */
956 buf[n + 0] = b0 >> (2 * 8);
957 buf[n + 1] = b0 >> (1 * 8);
958 buf[n + 2] = b0 >> (0 * 8);
959 buf[n + 3] = b1 >> (2 * 8);
960 buf[n + 4] = b1 >> (1 * 8);
961 buf[n + 5] = b1 >> (0 * 8);
962 buf[n + 6] = b2 >> (2 * 8);
963 buf[n + 7] = b2 >> (1 * 8);
964 buf[n + 8] = b2 >> (0 * 8);
965 afx->buffer_pos += 12 - 1;
966 n += 9;
967 skip_fast = 1;
968 continue;
969 }
970 }
971
972 switch(idx)
973 {
974 case 0: val = binc << 2; break;
975 case 1: val |= (binc>>4)&3; buf[n++]=val;val=(binc<<4)&0xf0;break;
976 case 2: val |= (binc>>2)&15; buf[n++]=val;val=(binc<<6)&0xc0;break;
977 case 3: val |= binc&0x3f; buf[n++] = val; break;
978 }
979 idx = (idx+1) % 4;
980
981 continue;
982 }
983
984 skip_fast = 0;
985
986 if( c == '\n' || c == ' ' || c == '\r' || c == '\t' )
987 continue;
988 else if( c == '=' ) { /* pad character: stop */
989 /* some mailers leave quoted-printable encoded characters
990 * so we try to workaround this */
991 if( afx->buffer_pos+2 < afx->buffer_len ) {
992 int cc1, cc2, cc3;
993 cc1 = afx->buffer[afx->buffer_pos];
994 cc2 = afx->buffer[afx->buffer_pos+1];
995 cc3 = afx->buffer[afx->buffer_pos+2];
996 if( isxdigit(cc1) && isxdigit(cc2)
997 && strchr( "=\n\r\t ", cc3 )) {
998 /* well it seems to be the case - adjust */
999 c = isdigit(cc1)? (cc1 - '0'): (ascii_toupper(cc1)-'A'+10);
1000 c <<= 4;
1001 c |= isdigit(cc2)? (cc2 - '0'): (ascii_toupper(cc2)-'A'+10);
1002 afx->buffer_pos += 2;
1003 afx->qp_detected = 1;
1004 goto again;
1005 }
1006 }
1007
1008 /* Occasionally a bug MTA will leave the = escaped as
1009 =3D. If the 4 characters following that are valid
1010 Radix64 characters and they are following by a new
1011 line, assume that this is the case and skip the
1012 3D. */
1013 if (afx->buffer_pos + 6 < afx->buffer_len
1014 && afx->buffer[afx->buffer_pos + 0] == '3'
1015 && afx->buffer[afx->buffer_pos + 1] == 'D'
1016 && asctobin[0][afx->buffer[afx->buffer_pos + 2]] != 0xffffffffUL
1017 && asctobin[0][afx->buffer[afx->buffer_pos + 3]] != 0xffffffffUL
1018 && asctobin[0][afx->buffer[afx->buffer_pos + 4]] != 0xffffffffUL
1019 && asctobin[0][afx->buffer[afx->buffer_pos + 5]] != 0xffffffffUL
1020 && afx->buffer[afx->buffer_pos + 6] == '\n')
1021 {
1022 afx->buffer_pos += 2;
1023 afx->qp_detected = 1;
1024 }
1025
1026 if (!n)
1027 onlypad = 1;
1028
1029 if( idx == 1 )
1030 buf[n++] = val;
1031 checkcrc++;
1032 break;
1033 }
1034 else if (c == '-'
1035 && afx->buffer_pos + 8 < afx->buffer_len
1036 && !strncmp (afx->buffer, "-----END ", 8)) {
1037 break; /* End in --dearmor mode or No CRC. */
1038 }
1039 else {
1040 log_error(_("invalid radix64 character %02X skipped\n"), c);
1041 continue;
1042 }
1043 }
1044
1045 afx->idx = idx;
1046 afx->radbuf[0] = val;
1047
1048 if( n )
1049 gcry_md_write (afx->crc_md, buf, n);
1050
1051 if( checkcrc ) {
1052 gcry_md_final (afx->crc_md);
1053 afx->any_data = 1;
1054 afx->inp_checked=0;
1055 afx->faked = 0;
1056 for(;;) { /* skip lf and pad characters */
1057 if( afx->buffer_pos < afx->buffer_len )
1058 c = afx->buffer[afx->buffer_pos++];
1059 else { /* read the next line */
1060 unsigned maxlen = MAX_LINELEN;
1061 afx->buffer_pos = 0;
1062 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
1063 &afx->buffer_size, &maxlen );
1064 if( !maxlen )
1065 afx->truncated++;
1066 if( !afx->buffer_len )
1067 break; /* eof */
1068 continue;
1069 }
1070 if( c == '\n' || c == ' ' || c == '\r'
1071 || c == '\t' || c == '=' )
1072 continue;
1073 break;
1074 }
1075 if( !afx->buffer_len )
1076 log_error(_("premature eof (no CRC)\n"));
1077 else {
1078 u32 mycrc = 0;
1079 idx = 0;
1080 do {
1081 if( (binc = asctobin[0][c]) == 0xffffffffUL )
1082 break;
1083 switch(idx) {
1084 case 0: val = binc << 2; break;
1085 case 1: val |= (binc>>4)&3; mycrc |= val << 16;val=(binc<<4)&0xf0;break;
1086 case 2: val |= (binc>>2)&15; mycrc |= val << 8;val=(binc<<6)&0xc0;break;
1087 case 3: val |= binc&0x3f; mycrc |= val; break;
1088 }
1089 for(;;) {
1090 if( afx->buffer_pos < afx->buffer_len )
1091 c = afx->buffer[afx->buffer_pos++];
1092 else { /* read the next line */
1093 unsigned maxlen = MAX_LINELEN;
1094 afx->buffer_pos = 0;
1095 afx->buffer_len = iobuf_read_line( a, &afx->buffer,
1096 &afx->buffer_size,
1097 &maxlen );
1098 if( !maxlen )
1099 afx->truncated++;
1100 if( !afx->buffer_len )
1101 break; /* eof */
1102 continue;
1103 }
1104 break;
1105 }
1106 if( !afx->buffer_len )
1107 break; /* eof */
1108 } while( ++idx < 4 );
1109 if( !afx->buffer_len ) {
1110 log_info(_("premature eof (in CRC)\n"));
1111 rc = invalid_crc();
1112 }
1113 else if( idx == 0 ) {
1114 /* No CRC at all is legal ("MAY") */
1115 rc=0;
1116 }
1117 else if( idx != 4 ) {
1118 log_info(_("malformed CRC\n"));
1119 rc = invalid_crc();
1120 }
1121 else if( mycrc != get_afx_crc (afx) ) {
1122 log_info (_("CRC error; %06lX - %06lX\n"),
1123 (ulong)get_afx_crc (afx), (ulong)mycrc);
1124 rc = invalid_crc();
1125 }
1126 else {
1127 rc = 0;
1128 /* FIXME: Here we should emit another control packet,
1129 * so that we know in mainproc that we are processing
1130 * a clearsign message */
1131 #if 0
1132 for(rc=0;!rc;) {
1133 rc = 0 /*check_trailer( &fhdr, c )*/;
1134 if( !rc ) {
1135 if( (c=iobuf_get(a)) == -1 )
1136 rc = 2;
1137 }
1138 }
1139 if( rc == -1 )
1140 rc = 0;
1141 else if( rc == 2 ) {
1142 log_error(_("premature eof (in trailer)\n"));
1143 rc = GPG_ERR_INV_ARMOR;
1144 }
1145 else {
1146 log_error(_("error in trailer line\n"));
1147 rc = GPG_ERR_INV_ARMOR;
1148 }
1149 #endif
1150 }
1151 }
1152 }
1153
1154 if( !n && !onlypad )
1155 rc = -1;
1156
1157 *retn = n;
1158 return rc;
1159 }
1160
1161 static void
1162 armor_output_buf_as_radix64 (armor_filter_context_t *afx, IOBUF a,
1163 byte *buf, size_t size)
1164 {
1165 byte radbuf[sizeof (afx->radbuf)];
1166 byte outbuf[64 + sizeof (afx->eol)];
1167 unsigned int eollen = strlen (afx->eol);
1168 u32 in, in2;
1169 int idx, idx2;
1170 int i;
1171
1172 idx = afx->idx;
1173 idx2 = afx->idx2;
1174 memcpy (radbuf, afx->radbuf, sizeof (afx->radbuf));
1175
1176 if (size && (idx || idx2))
1177 {
1178 /* preload eol to outbuf buffer */
1179 memcpy (outbuf + 4, afx->eol, sizeof (afx->eol));
1180
1181 for (; size && (idx || idx2); buf++, size--)
1182 {
1183 radbuf[idx++] = *buf;
1184 if (idx > 2)
1185 {
1186 idx = 0;
1187 in = (u32)radbuf[0] << (2 * 8);
1188 in |= (u32)radbuf[1] << (1 * 8);
1189 in |= (u32)radbuf[2] << (0 * 8);
1190 outbuf[0] = bintoasc[(in >> 18) & 077];
1191 outbuf[1] = bintoasc[(in >> 12) & 077];
1192 outbuf[2] = bintoasc[(in >> 6) & 077];
1193 outbuf[3] = bintoasc[(in >> 0) & 077];
1194 if (++idx2 >= (64/4))
1195 { /* pgp doesn't like 72 here */
1196 idx2=0;
1197 iobuf_write (a, outbuf, 4 + eollen);
1198 }
1199 else
1200 {
1201 iobuf_write (a, outbuf, 4);
1202 }
1203 }
1204 }
1205 }
1206
1207 if (size >= (64/4)*3)
1208 {
1209 /* preload eol to outbuf buffer */
1210 memcpy (outbuf + 64, afx->eol, sizeof(afx->eol));
1211
1212 do
1213 {
1214 /* idx and idx2 == 0 */
1215
1216 for (i = 0; i < (64/8); i++)
1217 {
1218 in = (u32)buf[0] << (2 * 8);
1219 in |= (u32)buf[1] << (1 * 8);
1220 in |= (u32)buf[2] << (0 * 8);
1221 in2 = (u32)buf[3] << (2 * 8);
1222 in2 |= (u32)buf[4] << (1 * 8);
1223 in2 |= (u32)buf[5] << (0 * 8);
1224 outbuf[i*8+0] = bintoasc[(in >> 18) & 077];
1225 outbuf[i*8+1] = bintoasc[(in >> 12) & 077];
1226 outbuf[i*8+2] = bintoasc[(in >> 6) & 077];
1227 outbuf[i*8+3] = bintoasc[(in >> 0) & 077];
1228 outbuf[i*8+4] = bintoasc[(in2 >> 18) & 077];
1229 outbuf[i*8+5] = bintoasc[(in2 >> 12) & 077];
1230 outbuf[i*8+6] = bintoasc[(in2 >> 6) & 077];
1231 outbuf[i*8+7] = bintoasc[(in2 >> 0) & 077];
1232 buf+=6;
1233 size-=6;
1234 }
1235
1236 /* pgp doesn't like 72 here */
1237 iobuf_write (a, outbuf, 64 + eollen);
1238 }
1239 while (size >= (64/4)*3);
1240
1241 /* restore eol for tail handling */
1242 if (size)
1243 memcpy (outbuf + 4, afx->eol, sizeof (afx->eol));
1244 }
1245
1246 for (; size; buf++, size--)
1247 {
1248 radbuf[idx++] = *buf;
1249 if (idx > 2)
1250 {
1251 idx = 0;
1252 in = (u32)radbuf[0] << (2 * 8);
1253 in |= (u32)radbuf[1] << (1 * 8);
1254 in |= (u32)radbuf[2] << (0 * 8);
1255 outbuf[0] = bintoasc[(in >> 18) & 077];
1256 outbuf[1] = bintoasc[(in >> 12) & 077];
1257 outbuf[2] = bintoasc[(in >> 6) & 077];
1258 outbuf[3] = bintoasc[(in >> 0) & 077];
1259 if (++idx2 >= (64/4))
1260 { /* pgp doesn't like 72 here */
1261 idx2=0;
1262 iobuf_write (a, outbuf, 4 + eollen);
1263 }
1264 else
1265 {
1266 iobuf_write (a, outbuf, 4);
1267 }
1268 }
1269 }
1270
1271 memcpy (afx->radbuf, radbuf, sizeof (afx->radbuf));
1272 afx->idx = idx;
1273 afx->idx2 = idx2;
1274 }
1275
1276 /****************
1277 * This filter is used to handle the armor stuff
1278 */
1279 static int
1280 armor_filter( void *opaque, int control,
1281 IOBUF a, byte *buf, size_t *ret_len)
1282 {
1283 size_t size = *ret_len;
1284 armor_filter_context_t *afx = opaque;
1285 int rc=0, c;
1286 byte radbuf[3];
1287 int idx, idx2;
1288 size_t n=0;
1289 u32 crc;
1290 #if 0
1291 static FILE *fp ;
1292
1293 if( !fp ) {
1294 fp = fopen("armor.out", "w");
1295 log_assert(fp);
1296 }
1297 #endif
1298
1299 if( DBG_FILTER )
1300 log_debug("armor-filter: control: %d\n", control );
1301 if( control == IOBUFCTRL_UNDERFLOW && afx->inp_bypass ) {
1302 n = 0;
1303 if( afx->buffer_len ) {
1304 /* Copy the data from AFX->BUFFER to BUF. */
1305 for(; n < size && afx->buffer_pos < afx->buffer_len;)
1306 buf[n++] = afx->buffer[afx->buffer_pos++];
1307 if( afx->buffer_pos >= afx->buffer_len )
1308 afx->buffer_len = 0;
1309 }
1310 /* If there is still space in BUF, read directly into it. */
1311 for(; n < size; n++ ) {
1312 if( (c=iobuf_get(a)) == -1 )
1313 break;
1314 buf[n] = c & 0xff;
1315 }
1316 if( !n )
1317 /* We didn't get any data. EOF. */
1318 rc = -1;
1319 *ret_len = n;
1320 }
1321 else if( control == IOBUFCTRL_UNDERFLOW ) {
1322 /* We need some space for the faked packet. The minmum
1323 * required size is the PARTIAL_CHUNK size plus a byte for the
1324 * length itself */
1325 if( size < PARTIAL_CHUNK+1 )
1326 BUG(); /* supplied buffer too short */
1327
1328 if( afx->faked )
1329 rc = fake_packet( afx, a, &n, buf, size );
1330 else if( !afx->inp_checked ) {
1331 rc = check_input( afx, a );
1332 if( afx->inp_bypass ) {
1333 for(n=0; n < size && afx->buffer_pos < afx->buffer_len; )
1334 buf[n++] = afx->buffer[afx->buffer_pos++];
1335 if( afx->buffer_pos >= afx->buffer_len )
1336 afx->buffer_len = 0;
1337 if( !n )
1338 rc = -1;
1339 }
1340 else if( afx->faked ) {
1341 unsigned int hashes = afx->hashes;
1342 const byte *sesmark;
1343 size_t sesmarklen;
1344
1345 sesmark = get_session_marker( &sesmarklen );
1346 if ( sesmarklen > 20 )
1347 BUG();
1348
1349 /* the buffer is at least 15+n*15 bytes long, so it
1350 * is easy to construct the packets */
1351
1352 hashes &= 1|2|8|16|32|64;
1353 if( !hashes ) {
1354 hashes |= 2; /* Default to SHA-1. */
1355 }
1356 n=0;
1357 /* First a gpg control packet... */
1358 buf[n++] = 0xff; /* new format, type 63, 1 length byte */
1359 n++; /* see below */
1360 memcpy(buf+n, sesmark, sesmarklen ); n+= sesmarklen;
1361 buf[n++] = CTRLPKT_CLEARSIGN_START;
1362 buf[n++] = afx->not_dash_escaped? 0:1; /* sigclass */
1363 if( hashes & 1 )
1364 buf[n++] = DIGEST_ALGO_RMD160;
1365 if( hashes & 2 )
1366 buf[n++] = DIGEST_ALGO_SHA1;
1367 if( hashes & 8 )
1368 buf[n++] = DIGEST_ALGO_SHA224;
1369 if( hashes & 16 )
1370 buf[n++] = DIGEST_ALGO_SHA256;
1371 if( hashes & 32 )
1372 buf[n++] = DIGEST_ALGO_SHA384;
1373 if( hashes & 64 )
1374 buf[n++] = DIGEST_ALGO_SHA512;
1375 buf[1] = n - 2;
1376
1377 /* ...followed by an invented plaintext packet.
1378 Amusingly enough, this packet is not compliant with
1379 2440 as the initial partial length is less than 512
1380 bytes. Of course, we'll accept it anyway ;) */
1381
1382 buf[n++] = 0xCB; /* new packet format, type 11 */
1383 buf[n++] = 0xE1; /* 2^1 == 2 bytes */
1384 buf[n++] = 't'; /* canonical text mode */
1385 buf[n++] = 0; /* namelength */
1386 buf[n++] = 0xE2; /* 2^2 == 4 more bytes */
1387 memset(buf+n, 0, 4); /* timestamp */
1388 n += 4;
1389 }
1390 else if( !rc )
1391 rc = radix64_read( afx, a, &n, buf, size );
1392 }
1393 else
1394 rc = radix64_read( afx, a, &n, buf, size );
1395 #if 0
1396 if( n )
1397 if( fwrite(buf, n, 1, fp ) != 1 )
1398 BUG();
1399 #endif
1400 *ret_len = n;
1401 }
1402 else if( control == IOBUFCTRL_FLUSH && !afx->cancel ) {
1403 if( !afx->status ) { /* write the header line */
1404 const char *s;
1405 strlist_t comment=opt.comments;
1406
1407 if( afx->what >= DIM(head_strings) )
1408 log_bug("afx->what=%d", afx->what);
1409 iobuf_writestr(a, "-----");
1410 iobuf_writestr(a, head_strings[afx->what] );
1411 iobuf_writestr(a, "-----" );
1412 iobuf_writestr(a,afx->eol);
1413 if (opt.emit_version)
1414 {
1415 iobuf_writestr (a, "Version: "GNUPG_NAME" v");
1416 for (s=VERSION; *s && *s != '.'; s++)
1417 iobuf_writebyte (a, *s);
1418 if (opt.emit_version > 1 && *s)
1419 {
1420 iobuf_writebyte (a, *s++);
1421 for (; *s && *s != '.'; s++)
1422 iobuf_writebyte (a, *s);
1423 if (opt.emit_version > 2)
1424 {
1425 for (; *s && *s != '-' && !spacep (s); s++)
1426 iobuf_writebyte (a, *s);
1427 if (opt.emit_version > 3)
1428 iobuf_writestr (a, " (" PRINTABLE_OS_NAME ")");
1429 }
1430 }
1431 iobuf_writestr(a,afx->eol);
1432 }
1433
1434 /* write the comment strings */
1435 for(;comment;comment=comment->next)
1436 {
1437 iobuf_writestr(a, "Comment: " );
1438 for( s=comment->d; *s; s++ )
1439 {
1440 if( *s == '\n' )
1441 iobuf_writestr(a, "\\n" );
1442 else if( *s == '\r' )
1443 iobuf_writestr(a, "\\r" );
1444 else if( *s == '\v' )
1445 iobuf_writestr(a, "\\v" );
1446 else
1447 iobuf_put(a, *s );
1448 }
1449
1450 iobuf_writestr(a,afx->eol);
1451 }
1452
1453 if ( afx->hdrlines ) {
1454 for ( s = afx->hdrlines; *s; s++ ) {
1455 #ifdef HAVE_DOSISH_SYSTEM
1456 if ( *s == '\n' )
1457 iobuf_put( a, '\r');
1458 #endif
1459 iobuf_put(a, *s );
1460 }
1461 }
1462
1463 iobuf_writestr(a,afx->eol);
1464 afx->status++;
1465 afx->idx = 0;
1466 afx->idx2 = 0;
1467 gcry_md_reset (afx->crc_md);
1468 }
1469
1470 if( size ) {
1471 gcry_md_write (afx->crc_md, buf, size);
1472 armor_output_buf_as_radix64 (afx, a, buf, size);
1473 }
1474 }
1475 else if( control == IOBUFCTRL_INIT )
1476 {
1477 if( !is_initialized )
1478 initialize();
1479
1480 /* Figure out what we're using for line endings if the caller
1481 didn't specify. */
1482 if(afx->eol[0]==0)
1483 {
1484 #ifdef HAVE_DOSISH_SYSTEM
1485 afx->eol[0]='\r';
1486 afx->eol[1]='\n';
1487 #else
1488 afx->eol[0]='\n';
1489 #endif
1490 }
1491 }
1492 else if( control == IOBUFCTRL_CANCEL ) {
1493 afx->cancel = 1;
1494 }
1495 else if( control == IOBUFCTRL_FREE ) {
1496 if( afx->cancel )
1497 ;
1498 else if( afx->status ) { /* pad, write cecksum, and bottom line */
1499 gcry_md_final (afx->crc_md);
1500 crc = get_afx_crc (afx);
1501 idx = afx->idx;
1502 idx2 = afx->idx2;
1503 if( idx ) {
1504 c = bintoasc[(afx->radbuf[0]>>2)&077];
1505 iobuf_put(a, c);
1506 if( idx == 1 ) {
1507 c = bintoasc[((afx->radbuf[0] << 4) & 060) & 077];
1508 iobuf_put(a, c);
1509 iobuf_put(a, '=');
1510 iobuf_put(a, '=');
1511 }
1512 else { /* 2 */
1513 c = bintoasc[(((afx->radbuf[0]<<4)&060)
1514 |((afx->radbuf[1]>>4)&017))&077];
1515 iobuf_put(a, c);
1516 c = bintoasc[((afx->radbuf[1] << 2) & 074) & 077];
1517 iobuf_put(a, c);
1518 iobuf_put(a, '=');
1519 }
1520 if( ++idx2 >= (64/4) )
1521 { /* pgp doesn't like 72 here */
1522 iobuf_writestr(a,afx->eol);
1523 idx2=0;
1524 }
1525 }
1526 /* may need a linefeed */
1527 if( idx2 )
1528 iobuf_writestr(a,afx->eol);
1529 /* write the CRC */
1530 iobuf_put(a, '=');
1531 radbuf[0] = crc >>16;
1532 radbuf[1] = crc >> 8;
1533 radbuf[2] = crc;
1534 c = bintoasc[(*radbuf >> 2) & 077];
1535 iobuf_put(a, c);
1536 c = bintoasc[(((*radbuf<<4)&060)|((radbuf[1] >> 4)&017))&077];
1537 iobuf_put(a, c);
1538 c = bintoasc[(((radbuf[1]<<2)&074)|((radbuf[2]>>6)&03))&077];
1539 iobuf_put(a, c);
1540 c = bintoasc[radbuf[2]&077];
1541 iobuf_put(a, c);
1542 iobuf_writestr(a,afx->eol);
1543 /* and the trailer */
1544 if( afx->what >= DIM(tail_strings) )
1545 log_bug("afx->what=%d", afx->what);
1546 iobuf_writestr(a, "-----");
1547 iobuf_writestr(a, tail_strings[afx->what] );
1548 iobuf_writestr(a, "-----" );
1549 iobuf_writestr(a,afx->eol);
1550 }
1551 else if( !afx->any_data && !afx->inp_bypass ) {
1552 log_error(_("no valid OpenPGP data found.\n"));
1553 afx->no_openpgp_data = 1;
1554 write_status_text( STATUS_NODATA, "1" );
1555 }
1556 /* Note that in a cleartext signature truncated lines in the
1557 * plaintext are detected and propagated to the signature
1558 * checking code by inserting a \f into the plaintext. We do
1559 * not use log_info here because some of the truncated lines
1560 * are harmless. */
1561 if( afx->truncated )
1562 log_info(_("invalid armor: line longer than %d characters\n"),
1563 MAX_LINELEN );
1564 /* issue an error to enforce dissemination of correct software */
1565 if( afx->qp_detected )
1566 log_error(_("quoted printable character in armor - "
1567 "probably a buggy MTA has been used\n") );
1568 xfree( afx->buffer );
1569 afx->buffer = NULL;
1570 release_armor_context (afx);
1571 }
1572 else if( control == IOBUFCTRL_DESC )
1573 mem2str (buf, "armor_filter", *ret_len);
1574 return rc;
1575 }
1576
1577
1578 /****************
1579 * create a radix64 encoded string.
1580 */
1581 char *
1582 make_radix64_string( const byte *data, size_t len )
1583 {
1584 char *buffer, *p;
1585
1586 buffer = p = xmalloc( (len+2)/3*4 + 1 );
1587 for( ; len >= 3 ; len -= 3, data += 3 ) {
1588 *p++ = bintoasc[(data[0] >> 2) & 077];
1589 *p++ = bintoasc[(((data[0] <<4)&060)|((data[1] >> 4)&017))&077];
1590 *p++ = bintoasc[(((data[1]<<2)&074)|((data[2]>>6)&03))&077];
1591 *p++ = bintoasc[data[2]&077];
1592 }
1593 if( len == 2 ) {
1594 *p++ = bintoasc[(data[0] >> 2) & 077];
1595 *p++ = bintoasc[(((data[0] <<4)&060)|((data[1] >> 4)&017))&077];
1596 *p++ = bintoasc[((data[1]<<2)&074)];
1597 }
1598 else if( len == 1 ) {
1599 *p++ = bintoasc[(data[0] >> 2) & 077];
1600 *p++ = bintoasc[(data[0] <<4)&060];
1601 }
1602 *p = 0;
1603 return buffer;
1604 }