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 2 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, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
30
#include <sys/types.h>
31
#ifdef HAVE_DOSISH_SYSTEM
32
# 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;
91
"Please report bugs to <bug-gnupg@gnu.org>.\n";
95
"Usage: gpgsplit [options] [files] (-h for help)";
98
"Syntax: gpgsplit [options] [files]\n"
99
"Split an OpenPGP message into packets\n";
110
main (int argc, char **argv)
114
#ifdef HAVE_DOSISH_SYSTEM
115
setmode( fileno(stdin), O_BINARY );
116
setmode( fileno(stdout), O_BINARY );
118
log_set_prefix ("gpgsplit", JNLIB_LOG_WITH_PREFIX);
119
set_strusage (my_strusage);
123
pargs.flags= 1; /* do not remove the args */
124
while (optfile_parse( NULL, NULL, NULL, &pargs, opts))
128
case oVerbose: opt_verbose = 1; break;
129
case oPrefix: opt_prefix = pargs.r.ret_str; break;
130
case oUncompress: opt_uncompress = 1; break;
131
case oSecretToPublic: opt_secret_to_public = 1; break;
132
case oNoSplit: opt_no_split = 1; break;
133
default : pargs.err = 2; break;
137
if (log_get_errorcount(0))
141
split_packets (NULL);
144
for ( ;argc; argc--, argv++)
145
split_packets (*argv);
156
rc = rc? rc : log_get_errorcount(0)? 2 : 0;
161
pkttype_to_string (int pkttype)
167
case PKT_PUBKEY_ENC : s = "pk_enc"; break;
168
case PKT_SIGNATURE : s = "sig"; break;
169
case PKT_SYMKEY_ENC : s = "sym_enc"; break;
170
case PKT_ONEPASS_SIG : s = "onepass_sig"; break;
171
case PKT_SECRET_KEY : s = "secret_key"; break;
172
case PKT_PUBLIC_KEY : s = "public_key"; break;
173
case PKT_SECRET_SUBKEY : s = "secret_subkey"; break;
174
case PKT_COMPRESSED :
175
s = opt_uncompress? "uncompressed":"compressed";
177
case PKT_ENCRYPTED : s = "encrypted"; break;
178
case PKT_MARKER : s = "marker"; break;
179
case PKT_PLAINTEXT : s = "plaintext"; break;
180
case PKT_RING_TRUST : s = "ring_trust"; break;
181
case PKT_USER_ID : s = "user_id"; break;
182
case PKT_PUBLIC_SUBKEY : s = "public_subkey"; break;
183
case PKT_OLD_COMMENT : s = "old_comment"; break;
184
case PKT_ATTRIBUTE : s = "attribute"; break;
185
case PKT_ENCRYPTED_MDC : s = "encrypted_mdc"; break;
186
case PKT_MDC : s = "mdc"; break;
187
case PKT_COMMENT : s = "comment"; break;
188
case PKT_GPG_CONTROL : s = "gpg_control"; break;
189
default: s = "unknown"; break;
196
* Create a new filename and a return a pointer to a statically
200
create_filename (int pkttype)
202
static unsigned int partno = 0;
206
name = xmalloc (strlen (opt_prefix) + 100 );
208
assert (pkttype < 1000 && pkttype >= 0 );
210
sprintf (name, "%s%06u-%03d" EXTSEP_S "%.40s",
211
opt_prefix, partno, pkttype, pkttype_to_string (pkttype));
216
read_u16 (FILE *fp, size_t *rn)
220
if ( (c = getc (fp)) == EOF )
223
if ( (c = getc (fp)) == EOF )
230
read_u32 (FILE *fp, unsigned long *rn)
234
if (read_u16 (fp, &tmp))
237
if (read_u16 (fp, &tmp))
244
write_old_header (FILE *fp, int pkttype, unsigned int len)
246
int ctb = (0x80 | ((pkttype & 15)<<2));
250
else if (len < 65536)
255
if ( putc ( ctb, fp) == EOF )
260
if (putc ((len>>24), fp) == EOF)
262
if (putc ((len>>16), fp) == EOF)
267
if (putc ((len>>8), fp) == EOF)
270
if (putc ((len&0xff), fp) == EOF)
276
write_new_header (FILE *fp, int pkttype, unsigned int len)
278
if ( putc ((0xc0 | (pkttype & 0x3f)), fp) == EOF )
283
if (putc (len, fp) == EOF)
289
if (putc ((len/256)+192, fp) == EOF)
291
if (putc ((len%256), fp) == EOF)
296
if (putc ( 0xff, fp) == EOF)
298
if (putc ( (len >> 24), fp) == EOF)
300
if (putc ( (len >> 16), fp) == EOF)
302
if (putc ( (len >> 8), fp) == EOF)
304
if (putc ( (len & 0xff), fp) == EOF)
310
/* Return the length of the public key given BUF of BUFLEN with a
313
public_key_length (const unsigned char *buf, size_t buflen)
315
const unsigned char *s;
318
/* byte version number (3 or 4)
320
[u16 valid days (version 3 only)]
325
if (buf[0] < 2 || buf[0] > 4)
326
return 0; /* wrong version number */
327
if (buflen < (buf[0] == 4? 6:8))
329
s = buf + (buf[0] == 4? 6:8);
330
buflen -= (buf[0] == 4? 6:8);
349
for (; nmpis; nmpis--)
351
unsigned int nbits, nbytes;
355
nbits = (s[0] << 8) | s[1];
357
nbytes = (nbits+7) / 8;
360
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]);
461
handle_bzip2(int algo,FILE *fpin,FILE *fpout)
464
byte *inbuf, *outbuf;
465
unsigned int inbufsize, outbufsize;
466
int c,zinit_done, zrc, nread, count;
469
memset (&bzs, 0, sizeof bzs);
471
inbuf = xmalloc (inbufsize);
473
outbuf = xmalloc (outbufsize);
479
if (bzs.avail_in < inbufsize)
484
count = inbufsize - n;
486
nread < count && (c=getc (fpin)) != EOF;
491
if (nread < count && algo == 1)
493
inbuf[n] = 0xFF; /* chew dummy byte */
498
bzs.next_out = outbuf;
499
bzs.avail_out = outbufsize;
503
zrc = BZ2_bzDecompressInit(&bzs,0,0);
505
log_fatal ("bz2lib problem: %d\n",zrc);
510
zrc = BZ2_bzDecompress(&bzs);
511
if (zrc == BZ_STREAM_END)
513
else if (zrc != BZ_OK && zrc != BZ_PARAM_ERROR)
514
log_fatal ("bz2lib inflate problem: %d\n", zrc );
515
for (n=0; n < outbufsize - bzs.avail_out; n++)
517
if (putc (outbuf[n], fpout) == EOF )
522
while (zrc != BZ_STREAM_END && zrc != BZ_PARAM_ERROR);
523
BZ2_bzDecompressEnd(&bzs);
527
#endif /* HAVE_BZIP2 */
529
/* hdr must point to a buffer large enough to hold all header bytes */
531
write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
532
int pkttype, int partial, unsigned char *hdr, size_t hdrlen)
537
const char *outname = create_filename (pkttype);
539
#if defined(__riscos__) && defined(USE_ZLIBRISCOS)
540
static int initialized = 0;
543
initialized = riscos_load_module("ZLib", zlib_path, 1);
550
log_info ("writing `%s'\n", outname);
551
fpout = fopen (outname, "wb");
554
log_error ("error creating `%s': %s\n", outname, strerror(errno));
555
/* stop right now, otherwise we would mess up the sequence
556
of the part numbers */
561
if (opt_secret_to_public
562
&& (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY))
564
unsigned char *blob = xmalloc (pktlen);
567
pkttype = pkttype == PKT_SECRET_KEY? PKT_PUBLIC_KEY:PKT_PUBLIC_SUBKEY;
569
for (i=0; i < pktlen; i++)
576
len = public_key_length (blob, pktlen);
579
log_error ("error calcualting public key length\n");
582
if ( (hdr[0] & 0x40) )
584
if (write_new_header (fpout, pkttype, len))
589
if (write_old_header (fpout, pkttype, len))
593
for (i=0; i < len; i++)
595
if ( putc (blob[i], fpout) == EOF )
605
for (p=hdr; hdrlen; p++, hdrlen--)
607
if ( putc (*p, fpout) == EOF )
622
assert( c >= 224 && c < 255 );
625
else if ((c = getc (fpin)) == EOF )
633
partial = 0; /* (last segment may follow) */
637
pktlen = (c - 192) * 256;
638
if ((c = getc (fpin)) == EOF)
646
if (read_u32 (fpin, &pktlen))
648
hdr[hdrlen++] = pktlen >> 24;
649
hdr[hdrlen++] = pktlen >> 16;
650
hdr[hdrlen++] = pktlen >> 8;
651
hdr[hdrlen++] = pktlen;
655
{ /* next partial body length */
656
for (p=hdr; hdrlen; p++, hdrlen--)
658
if ( putc (*p, fpout) == EOF )
661
partlen = 1 << (c & 0x1f);
662
for (; partlen; partlen--)
664
if ((c = getc (fpin)) == EOF)
666
if ( putc (c, fpout) == EOF )
671
else if (partial == 2)
674
if ( read_u16 (fpin, &partlen) )
676
hdr[hdrlen++] = partlen >> 8;
677
hdr[hdrlen++] = partlen;
678
for (p=hdr; hdrlen; p++, hdrlen--)
680
if ( putc (*p, fpout) == EOF )
684
partial = 0; /* end of packet */
685
for (; partlen; partlen--)
690
if ( putc (c, fpout) == EOF )
695
{ /* compressed: read to end */
701
if ((c = getc (fpin)) == EOF)
706
if(handle_zlib(c,fpin,fpout))
712
if(handle_bzip2(c,fpin,fpout))
715
#endif /* HAVE_BZIP2 */
718
log_error("invalid compression algorithm (%d)\n",c);
724
while ( (c=getc (fpin)) != EOF )
726
if ( putc (c, fpout) == EOF )
735
for (p=hdr; hdrlen; p++, hdrlen--)
737
if ( putc (*p, fpout) == EOF )
741
/* standard packet or last segment of partial length encoded packet */
742
for (; pktlen; pktlen--)
747
if ( putc (c, fpout) == EOF )
752
if ( !opt_no_split && fclose (fpout) )
753
log_error ("error closing `%s': %s\n", outname, strerror (errno));
757
log_error ("error writing `%s': %s\n", outname, strerror (errno));
775
do_split (const char *fname, FILE *fp)
778
unsigned long pktlen = 0;
780
unsigned char header[20];
785
return 3; /* ready */
786
header[header_idx++] = ctb;
790
log_error("invalid CTB %02x\n", ctb );
795
pkttype = (ctb & 0x3f);
796
if( (c = getc (fp)) == EOF )
798
header[header_idx++] = c;
804
pktlen = (c - 192) * 256;
805
if( (c = getc (fp)) == EOF )
807
header[header_idx++] = c;
812
if (read_u32 (fp, &pktlen))
814
header[header_idx++] = pktlen >> 24;
815
header[header_idx++] = pktlen >> 16;
816
header[header_idx++] = pktlen >> 8;
817
header[header_idx++] = pktlen;
820
{ /* partial body length */
829
pkttype = (ctb>>2)&0xf;
830
lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
833
pktlen = 0; /* don't know the value */
834
if( pkttype == PKT_COMPRESSED )
837
partial = 2; /* the old GnuPG partial length encoding */
841
for ( ; lenbytes; lenbytes-- )
844
if( (c = getc (fp)) == EOF )
846
header[header_idx++] = c;
853
return write_part (fname, fp, pktlen, pkttype, partial,
859
split_packets (const char *fname)
864
if (!fname || !strcmp (fname, "-"))
869
else if ( !(fp = fopen (fname,"rb")) )
871
log_error ("can't open `%s': %s\n", fname, strerror (errno));
875
while ( !(rc = do_split (fname, fp)) )
878
; /* error already handled */
879
else if ( ferror (fp) )
880
log_error ("error reading `%s': %s\n", fname, strerror (errno));
882
log_error ("premature EOF while reading `%s'\n", fname );