1 /* gpgsplit.c - An OpenPGP packet splitting tool
2 * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
28 #include <sys/types.h>
29 #ifdef HAVE_DOSISH_SYSTEM
30 # include <fcntl.h> /* for setmode() */
37 #endif /* HAVE_BZIP2 */
38 #if defined(__riscos__) && defined(USE_ZLIBRISCOS)
39 # include "zlib-riscos.h"
42 #define INCLUDED_BY_MAIN_MODULE 1
44 #include "openpgpdefs.h"
46 static int opt_verbose;
47 static const char *opt_prefix = "";
48 static int opt_uncompress;
49 static int opt_secret_to_public;
50 static int opt_no_split;
52 static void g10_exit( int rc );
53 static void split_packets (const char *fname);
56 enum cmd_and_opt_values {
68 static ARGPARSE_OPTS opts[] = {
70 { 301, NULL, 0, "@Options:\n " },
72 { oVerbose, "verbose", 0, "verbose" },
73 { oPrefix, "prefix", 2, "|STRING|Prepend filenames with STRING" },
74 { oUncompress, "uncompress", 0, "uncompress a packet"},
75 { oSecretToPublic, "secret-to-public", 0, "convert secret keys to public keys"},
76 { oNoSplit, "no-split", 0, "write to stdout and don't actually split"},
81 my_strusage (int level)
86 case 11: p = "gpgsplit (@GNUPG@)";
88 case 13: p = VERSION; break;
89 case 17: p = PRINTABLE_OS_NAME; break;
90 case 19: p = "Please report bugs to <@EMAIL@>.\n"; break;
94 "Usage: gpgsplit [options] [files] (-h for help)";
97 "Syntax: gpgsplit [options] [files]\n"
98 "Split an OpenPGP message into packets\n";
109 main (int argc, char **argv)
113 #ifdef HAVE_DOSISH_SYSTEM
114 setmode( fileno(stdin), O_BINARY );
115 setmode( fileno(stdout), O_BINARY );
117 log_set_prefix ("gpgsplit", GPGRT_LOG_WITH_PREFIX);
118 set_strusage (my_strusage);
122 pargs.flags= 1; /* do not remove the args */
123 while (optfile_parse( NULL, NULL, NULL, &pargs, opts))
127 case oVerbose: opt_verbose = 1; break;
128 case oPrefix: opt_prefix = pargs.r.ret_str; break;
129 case oUncompress: opt_uncompress = 1; break;
130 case oSecretToPublic: opt_secret_to_public = 1; break;
131 case oNoSplit: opt_no_split = 1; break;
132 default : pargs.err = 2; break;
136 if (log_get_errorcount(0))
140 split_packets (NULL);
143 for ( ;argc; argc--, argv++)
144 split_packets (*argv);
155 rc = rc? rc : log_get_errorcount(0)? 2 : 0;
160 pkttype_to_string (int pkttype)
166 case PKT_PUBKEY_ENC : s = "pk_enc"; break;
167 case PKT_SIGNATURE : s = "sig"; break;
168 case PKT_SYMKEY_ENC : s = "sym_enc"; break;
169 case PKT_ONEPASS_SIG : s = "onepass_sig"; break;
170 case PKT_SECRET_KEY : s = "secret_key"; break;
171 case PKT_PUBLIC_KEY : s = "public_key"; break;
172 case PKT_SECRET_SUBKEY : s = "secret_subkey"; break;
173 case PKT_COMPRESSED :
174 s = opt_uncompress? "uncompressed":"compressed";
176 case PKT_ENCRYPTED : s = "encrypted"; break;
177 case PKT_MARKER : s = "marker"; break;
178 case PKT_PLAINTEXT : s = "plaintext"; break;
179 case PKT_RING_TRUST : s = "ring_trust"; break;
180 case PKT_USER_ID : s = "user_id"; break;
181 case PKT_PUBLIC_SUBKEY : s = "public_subkey"; break;
182 case PKT_OLD_COMMENT : s = "old_comment"; break;
183 case PKT_ATTRIBUTE : s = "attribute"; break;
184 case PKT_ENCRYPTED_MDC : s = "encrypted_mdc"; break;
185 case PKT_MDC : s = "mdc"; break;
186 case PKT_COMMENT : s = "comment"; break;
187 case PKT_GPG_CONTROL : s = "gpg_control"; break;
188 default: s = "unknown"; break;
195 * Create a new filename and a return a pointer to a statically
199 create_filename (int pkttype)
201 static unsigned int partno = 0;
205 name = xmalloc (strlen (opt_prefix) + 100 );
207 assert (pkttype < 1000 && pkttype >= 0 );
209 sprintf (name, "%s%06u-%03d" EXTSEP_S "%.40s",
210 opt_prefix, partno, pkttype, pkttype_to_string (pkttype));
215 read_u16 (FILE *fp, size_t *rn)
219 if ( (c = getc (fp)) == EOF )
222 if ( (c = getc (fp)) == EOF )
229 read_u32 (FILE *fp, unsigned long *rn)
233 if (read_u16 (fp, &tmp))
236 if (read_u16 (fp, &tmp))
243 write_old_header (FILE *fp, int pkttype, unsigned int len)
245 int ctb = (0x80 | ((pkttype & 15)<<2));
249 else if (len < 65536)
254 if ( putc ( ctb, fp) == EOF )
259 if (putc ((len>>24), fp) == EOF)
261 if (putc ((len>>16), fp) == EOF)
266 if (putc ((len>>8), fp) == EOF)
269 if (putc ((len&0xff), fp) == EOF)
275 write_new_header (FILE *fp, int pkttype, unsigned int len)
277 if ( putc ((0xc0 | (pkttype & 0x3f)), fp) == EOF )
282 if (putc (len, fp) == EOF)
288 if (putc ((len/256)+192, fp) == EOF)
290 if (putc ((len%256), fp) == EOF)
295 if (putc ( 0xff, fp) == EOF)
297 if (putc ( (len >> 24), fp) == EOF)
299 if (putc ( (len >> 16), fp) == EOF)
301 if (putc ( (len >> 8), fp) == EOF)
303 if (putc ( (len & 0xff), fp) == EOF)
309 /* Return the length of the public key given BUF of BUFLEN with a
312 public_key_length (const unsigned char *buf, size_t buflen)
314 const unsigned char *s;
317 /* byte version number (3 or 4)
319 [u16 valid days (version 3 only)]
324 if (buf[0] < 2 || buf[0] > 4)
325 return 0; /* wrong version number */
326 if (buflen < (buf[0] == 4? 6:8))
328 s = buf + (buf[0] == 4? 6:8);
329 buflen -= (buf[0] == 4? 6:8);
348 for (; nmpis; nmpis--)
350 unsigned int nbits, nbytes;
354 nbits = (s[0] << 8) | s[1];
356 nbytes = (nbits+7) / 8;
359 s += nbytes; buflen -= nbytes;
367 handle_zlib(int algo,FILE *fpin,FILE *fpout)
370 byte *inbuf, *outbuf;
371 unsigned int inbufsize, outbufsize;
372 int c,zinit_done, zrc, nread, count;
375 memset (&zs, 0, sizeof zs);
377 inbuf = xmalloc (inbufsize);
379 outbuf = xmalloc (outbufsize);
385 if (zs.avail_in < inbufsize)
389 zs.next_in = (Bytef *) inbuf;
390 count = inbufsize - n;
392 nread < count && (c=getc (fpin)) != EOF;
397 if (nread < count && algo == 1)
399 inbuf[n] = 0xFF; /* chew dummy byte */
404 zs.next_out = (Bytef *) outbuf;
405 zs.avail_out = outbufsize;
409 zrc = (algo == 1? inflateInit2 ( &zs, -13)
410 : inflateInit ( &zs ));
413 log_fatal ("zlib problem: %s\n", zs.msg? zs.msg :
414 zrc == Z_MEM_ERROR ? "out of core" :
415 zrc == Z_VERSION_ERROR ?
416 "invalid lib version" :
424 zrc = inflate (&zs, Z_SYNC_FLUSH);
426 zrc = inflate (&zs, Z_PARTIAL_FLUSH);
428 if (zrc == Z_STREAM_END)
430 else if (zrc != Z_OK && zrc != Z_BUF_ERROR)
433 log_fatal ("zlib inflate problem: %s\n", zs.msg );
435 log_fatal ("zlib inflate problem: rc=%d\n", zrc );
437 for (n=0; n < outbufsize - zs.avail_out; n++)
439 if (putc (outbuf[n], fpout) == EOF )
444 while (zrc != Z_STREAM_END && zrc != Z_BUF_ERROR);
448 fputs ("Left over bytes:", stderr);
449 for (i=0; i < zs.avail_in; i++)
450 fprintf (stderr, " %02X", zs.next_in[i]);
462 handle_bzip2(int algo,FILE *fpin,FILE *fpout)
465 byte *inbuf, *outbuf;
466 unsigned int inbufsize, outbufsize;
467 int c,zinit_done, zrc, nread, count;
470 memset (&bzs, 0, sizeof bzs);
472 inbuf = xmalloc (inbufsize);
474 outbuf = xmalloc (outbufsize);
480 if (bzs.avail_in < inbufsize)
485 count = inbufsize - n;
487 nread < count && (c=getc (fpin)) != EOF;
492 if (nread < count && algo == 1)
494 inbuf[n] = 0xFF; /* chew dummy byte */
499 bzs.next_out = outbuf;
500 bzs.avail_out = outbufsize;
504 zrc = BZ2_bzDecompressInit(&bzs,0,0);
506 log_fatal ("bz2lib problem: %d\n",zrc);
511 zrc = BZ2_bzDecompress(&bzs);
512 if (zrc == BZ_STREAM_END)
514 else if (zrc != BZ_OK && zrc != BZ_PARAM_ERROR)
515 log_fatal ("bz2lib inflate problem: %d\n", zrc );
516 for (n=0; n < outbufsize - bzs.avail_out; n++)
518 if (putc (outbuf[n], fpout) == EOF )
523 while (zrc != BZ_STREAM_END && zrc != BZ_PARAM_ERROR);
524 BZ2_bzDecompressEnd(&bzs);
528 #endif /* HAVE_BZIP2 */
530 /* hdr must point to a buffer large enough to hold all header bytes */
532 write_part (FILE *fpin, unsigned long pktlen,
533 int pkttype, int partial, unsigned char *hdr, size_t hdrlen)
538 const char *outname = create_filename (pkttype);
540 #if defined(__riscos__) && defined(USE_ZLIBRISCOS)
541 static int initialized = 0;
544 initialized = riscos_load_module("ZLib", zlib_path, 1);
551 log_info ("writing '%s'\n", outname);
552 fpout = fopen (outname, "wb");
555 log_error ("error creating '%s': %s\n", outname, strerror(errno));
556 /* stop right now, otherwise we would mess up the sequence
557 of the part numbers */
562 if (opt_secret_to_public
563 && (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY))
565 unsigned char *blob = xmalloc (pktlen);
568 pkttype = pkttype == PKT_SECRET_KEY? PKT_PUBLIC_KEY:PKT_PUBLIC_SUBKEY;
570 for (i=0; i < pktlen; i++)
577 len = public_key_length (blob, pktlen);
580 log_error ("error calculating public key length\n");
583 if ( (hdr[0] & 0x40) )
585 if (write_new_header (fpout, pkttype, len))
590 if (write_old_header (fpout, pkttype, len))
594 for (i=0; i < len; i++)
596 if ( putc (blob[i], fpout) == EOF )
606 for (p=hdr; hdrlen; p++, hdrlen--)
608 if ( putc (*p, fpout) == EOF )
623 assert( c >= 224 && c < 255 );
626 else if ((c = getc (fpin)) == EOF )
634 partial = 0; /* (last segment may follow) */
638 pktlen = (c - 192) * 256;
639 if ((c = getc (fpin)) == EOF)
647 if (read_u32 (fpin, &pktlen))
649 hdr[hdrlen++] = pktlen >> 24;
650 hdr[hdrlen++] = pktlen >> 16;
651 hdr[hdrlen++] = pktlen >> 8;
652 hdr[hdrlen++] = pktlen;
656 { /* next partial body length */
657 for (p=hdr; hdrlen; p++, hdrlen--)
659 if ( putc (*p, fpout) == EOF )
662 partlen = 1 << (c & 0x1f);
663 for (; partlen; partlen--)
665 if ((c = getc (fpin)) == EOF)
667 if ( putc (c, fpout) == EOF )
672 else if (partial == 2)
675 if ( read_u16 (fpin, &partlen) )
677 hdr[hdrlen++] = partlen >> 8;
678 hdr[hdrlen++] = partlen;
679 for (p=hdr; hdrlen; p++, hdrlen--)
681 if ( putc (*p, fpout) == EOF )
685 partial = 0; /* end of packet */
686 for (; partlen; partlen--)
691 if ( putc (c, fpout) == EOF )
696 { /* compressed: read to end */
702 if ((c = getc (fpin)) == EOF)
708 else if(c==1 || c==2)
710 if(handle_zlib(c,fpin,fpout))
713 #endif /* HAVE_ZIP */
717 if(handle_bzip2(c,fpin,fpout))
720 #endif /* HAVE_BZIP2 */
723 log_error("invalid compression algorithm (%d)\n",c);
729 while ( (c=getc (fpin)) != EOF )
731 if ( putc (c, fpout) == EOF )
740 for (p=hdr; hdrlen; p++, hdrlen--)
742 if ( putc (*p, fpout) == EOF )
746 /* standard packet or last segment of partial length encoded packet */
747 for (; pktlen; pktlen--)
752 if ( putc (c, fpout) == EOF )
757 if ( !opt_no_split && fclose (fpout) )
758 log_error ("error closing '%s': %s\n", outname, strerror (errno));
762 log_error ("error writing '%s': %s\n", outname, strerror (errno));
783 unsigned long pktlen = 0;
785 unsigned char header[20];
790 return 3; /* ready */
791 header[header_idx++] = ctb;
795 log_error("invalid CTB %02x\n", ctb );
800 pkttype = (ctb & 0x3f);
801 if( (c = getc (fp)) == EOF )
803 header[header_idx++] = c;
809 pktlen = (c - 192) * 256;
810 if( (c = getc (fp)) == EOF )
812 header[header_idx++] = c;
817 if (read_u32 (fp, &pktlen))
819 header[header_idx++] = pktlen >> 24;
820 header[header_idx++] = pktlen >> 16;
821 header[header_idx++] = pktlen >> 8;
822 header[header_idx++] = pktlen;
825 { /* partial body length */
834 pkttype = (ctb>>2)&0xf;
835 lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
838 pktlen = 0; /* don't know the value */
839 if( pkttype == PKT_COMPRESSED )
842 partial = 2; /* the old GnuPG partial length encoding */
846 for ( ; lenbytes; lenbytes-- )
849 if( (c = getc (fp)) == EOF )
851 header[header_idx++] = c;
858 return write_part (fp, pktlen, pkttype, partial, header, header_idx);
863 split_packets (const char *fname)
868 if (!fname || !strcmp (fname, "-"))
873 else if ( !(fp = fopen (fname,"rb")) )
875 log_error ("can't open '%s': %s\n", fname, strerror (errno));
879 while ( !(rc = do_split (fp)) )
882 ; /* error already handled */
883 else if ( ferror (fp) )
884 log_error ("error reading '%s': %s\n", fname, strerror (errno));
886 log_error ("premature EOF while reading '%s'\n", fname );