1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-23 10:29:58 +01:00

* gpgsplit.c: New options --secret-to-public and --no-split.

GNUified the indentation style.
This commit is contained in:
Werner Koch 2002-10-23 08:58:36 +00:00
parent d964ea1f84
commit 17edfbb907
2 changed files with 629 additions and 396 deletions

View File

@ -1,3 +1,8 @@
2002-10-23 Werner Koch <wk@gnupg.org>
* gpgsplit.c: New options --secret-to-public and --no-split.
GNUified the indentation style.
2002-09-25 David Shaw <dshaw@jabberwocky.com> 2002-09-25 David Shaw <dshaw@jabberwocky.com>
* Makefile.am: Link bftest with EGDLIBS (i.e. NETLIBS) as EGD uses * Makefile.am: Link bftest with EGDLIBS (i.e. NETLIBS) as EGD uses

View File

@ -1,5 +1,5 @@
/* gpgsplit.c - An OpenPGP packet splitting tool /* gpgsplit.c - An OpenPGP packet splitting tool
* Copyright (C) 2001 Free Software Foundation, Inc. * Copyright (C) 2001, 2002 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -45,16 +45,23 @@
static int opt_verbose; static int opt_verbose;
static const char *opt_prefix = ""; static const char *opt_prefix = "";
static int opt_uncompress; static int opt_uncompress;
static int opt_secret_to_public;
static int opt_no_split;
static void g10_exit( int rc ); static void g10_exit( int rc );
static void split_packets (const char *fname); static void split_packets (const char *fname);
enum cmd_and_opt_values { aNull = 0, enum cmd_and_opt_values {
aNull = 0,
oVerbose = 'v', oVerbose = 'v',
oPrefix = 'p', oPrefix = 'p',
oUncompress = 500, oUncompress = 500,
aTest }; oSecretToPublic,
oNoSplit,
aTest
};
static ARGPARSE_OPTS opts[] = { static ARGPARSE_OPTS opts[] = {
@ -64,6 +71,8 @@ static ARGPARSE_OPTS opts[] = {
{ oVerbose, "verbose", 0, "verbose" }, { oVerbose, "verbose", 0, "verbose" },
{ oPrefix, "prefix", 2, "|STRING|Prepend filenames with STRING" }, { oPrefix, "prefix", 2, "|STRING|Prepend filenames with STRING" },
{ oUncompress, "uncompress", 0, "uncompress a packet"}, { oUncompress, "uncompress", 0, "uncompress a packet"},
{ oSecretToPublic, "secret-to-public", 0, "convert secret keys to public keys"},
{ oNoSplit, "no-split", 0, "write to stdout and don't actually split"},
{0} }; {0} };
@ -71,7 +80,8 @@ const char *
strusage( int level ) strusage( int level )
{ {
const char *p; const char *p;
switch( level ) { switch (level)
{
case 11: p = "gpgsplit (GnuPG)"; case 11: p = "gpgsplit (GnuPG)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
@ -113,11 +123,15 @@ main( int argc, char **argv )
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1; /* do not remove the args */ pargs.flags= 1; /* do not remove the args */
while( optfile_parse( NULL, NULL, NULL, &pargs, opts) ) { while (optfile_parse( NULL, NULL, NULL, &pargs, opts))
switch( pargs.r_opt ) { {
switch (pargs.r_opt)
{
case oVerbose: opt_verbose = 1; break; case oVerbose: opt_verbose = 1; break;
case oPrefix: opt_prefix = pargs.r.ret_str; break; case oPrefix: opt_prefix = pargs.r.ret_str; break;
case oUncompress: opt_uncompress = 1; break; case oUncompress: opt_uncompress = 1; break;
case oSecretToPublic: opt_secret_to_public = 1; break;
case oNoSplit: opt_no_split = 1; break;
default : pargs.err = 2; break; default : pargs.err = 2; break;
} }
} }
@ -127,7 +141,8 @@ main( int argc, char **argv )
if (!argc) if (!argc)
split_packets (NULL); split_packets (NULL);
else { else
{
for ( ;argc; argc--, argv++) for ( ;argc; argc--, argv++)
split_packets (*argv); split_packets (*argv);
} }
@ -148,7 +163,9 @@ static const char *
pkttype_to_string (int pkttype) pkttype_to_string (int pkttype)
{ {
const char *s; const char *s;
switch (pkttype) {
switch (pkttype)
{
case PKT_PUBKEY_ENC : s = "pk_enc"; break; case PKT_PUBKEY_ENC : s = "pk_enc"; break;
case PKT_SIGNATURE : s = "sig"; break; case PKT_SIGNATURE : s = "sig"; break;
case PKT_SYMKEY_ENC : s = "sym_enc"; break; case PKT_SYMKEY_ENC : s = "sym_enc"; break;
@ -225,8 +242,131 @@ read_u32 (FILE *fp, unsigned long *rn)
return 0; return 0;
} }
static int
write_old_header (FILE *fp, int pkttype, unsigned int len)
{
int ctb = (0x80 | ((pkttype & 15)<<2));
/* hdr must pint to a buffer large enough to hold all header bytes */ if (len < 256)
;
else if (len < 65536)
ctb |= 1;
else
ctb |= 2;
if ( putc ( ctb, fp) == EOF )
return -1;
if ( (ctb & 2) )
{
if (putc ((len>>24), fp) == EOF)
return -1;
if (putc ((len>>16), fp) == EOF)
return -1;
}
if ( (ctb & 3) )
{
if (putc ((len>>8), fp) == EOF)
return -1;
}
if (putc ((len&0xff), fp) == EOF)
return -1;
return 0;
}
static int
write_new_header (FILE *fp, int pkttype, unsigned int len)
{
if ( putc ((0xc0 | (pkttype & 0x3f)), fp) == EOF )
return -1;
if (len < 192)
{
if (putc (len, fp) == EOF)
return -1;
}
else if (len < 8384)
{
len -= 192;
if (putc ((len/256)+192, fp) == EOF)
return -1;
if (putc ((len%256), fp) == EOF)
return -1;
}
else
{
if (putc ( 0xff, fp) == EOF)
return -1;
if (putc ( (len >> 24), fp) == EOF)
return -1;
if (putc ( (len >> 16), fp) == EOF)
return -1;
if (putc ( (len >> 8), fp) == EOF)
return -1;
if (putc ( (len & 0xff), fp) == EOF)
return -1;
}
return 0;
}
/* Return the length of the public key given BUF of BUFLEN with a
secret key. */
static int
public_key_length (const unsigned char *buf, size_t buflen)
{
const unsigned char *s;
int nmpis;
/* byte version number (3 or 4)
u32 creation time
[u16 valid days (version 3 only)]
byte algorithm
n MPIs (n and e) */
if (!buflen)
return 0;
if (buf[0] < 2 || buf[0] > 4)
return 0; /* wrong version number */
if (buflen < (buf[0] == 4? 6:8))
return 0;
s = buf + (buf[0] == 4? 6:8);
buflen -= (buf[0] == 4? 6:8);
switch (s[-1])
{
case 1:
case 2:
case 3:
nmpis = 2;
break;
case 16:
case 20:
nmpis = 3;
break;
case 17:
nmpis = 4;
break;
default:
return 0;
}
for (; nmpis; nmpis--)
{
unsigned int nbits, nbytes;
if (buflen < 2)
return 0;
nbits = (s[0] << 8) | s[1];
s += 2; buflen -= 2;
nbytes = (nbits+7) / 8;
if (buflen < nbytes)
return 0;
s += nbytes; buflen -= nbytes;
}
return s - buf;
}
/* hdr must point to a buffer large enough to hold all header bytes */
static int static int
write_part ( const char *fname, FILE *fpin, unsigned long pktlen, write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
int pkttype, int partial, unsigned char *hdr, size_t hdrlen) int pkttype, int partial, unsigned char *hdr, size_t hdrlen)
@ -236,45 +376,98 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
unsigned char *p; unsigned char *p;
const char *outname = create_filename (pkttype); const char *outname = create_filename (pkttype);
/* fixme: should we check that this file does not yet exist? */ if (opt_no_split)
fpout = stdout;
else
{
if (opt_verbose) if (opt_verbose)
log_info ("writing `%s'\n", outname); log_info ("writing `%s'\n", outname);
fpout = fopen (outname, "wb"); fpout = fopen (outname, "wb");
if (!fpout) { if (!fpout)
{
log_error ("error creating `%s': %s\n", outname, strerror(errno)); log_error ("error creating `%s': %s\n", outname, strerror(errno));
/* stop right now, otherwise we would mess up the sequence of /* stop right now, otherwise we would mess up the sequence
* the part numbers */ of the part numbers */
g10_exit (1); g10_exit (1);
} }
}
if (!opt_uncompress) { if (opt_secret_to_public
for (p=hdr; hdrlen; p++, hdrlen--) { && (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY))
{
unsigned char *blob = m_alloc (pktlen);
int i, len;
pkttype = pkttype == PKT_SECRET_KEY? PKT_PUBLIC_KEY:PKT_PUBLIC_SUBKEY;
for (i=0; i < pktlen; i++)
{
c = getc (fpin);
if (c == EOF)
goto read_error;
blob[i] = c;
}
len = public_key_length (blob, pktlen);
if (!len)
{
log_error ("error calcualting public key length\n");
g10_exit (1);
}
if ( (hdr[0] & 0x40) )
{
if (write_new_header (fpout, pkttype, len))
goto write_error;
}
else
{
if (write_old_header (fpout, pkttype, len))
goto write_error;
}
for (i=0; i < len; i++)
{
if ( putc (blob[i], fpout) == EOF )
goto write_error;
}
goto ready;
}
if (!opt_uncompress)
{
for (p=hdr; hdrlen; p++, hdrlen--)
{
if ( putc (*p, fpout) == EOF ) if ( putc (*p, fpout) == EOF )
goto write_error; goto write_error;
} }
} }
first = 1; first = 1;
while (partial) { while (partial)
{
size_t partlen; size_t partlen;
if (partial == 1) { /* openpgp */ if (partial == 1)
if( first ) { { /* openpgp */
if (first )
{
c = pktlen; c = pktlen;
assert( c >= 224 && c < 255 ); assert( c >= 224 && c < 255 );
first = 0; first = 0;
} }
else if( (c = getc (fpin)) == EOF ) { else if ((c = getc (fpin)) == EOF )
goto read_error; goto read_error;
}
else else
hdr[hdrlen++] = c; hdr[hdrlen++] = c;
if( c < 192 ) { if (c < 192)
{
pktlen = c; pktlen = c;
partial = 0; /* (last segment may follow) */ partial = 0; /* (last segment may follow) */
} }
else if( c < 224 ) { else if (c < 224 )
{
pktlen = (c - 192) * 256; pktlen = (c - 192) * 256;
if ((c = getc (fpin)) == EOF) if ((c = getc (fpin)) == EOF)
goto read_error; goto read_error;
@ -282,7 +475,8 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
pktlen += c + 192; pktlen += c + 192;
partial = 0; partial = 0;
} }
else if( c == 255 ) { else if (c == 255)
{
if (read_u32 (fpin, &pktlen)) if (read_u32 (fpin, &pktlen))
goto read_error; goto read_error;
hdr[hdrlen++] = pktlen >> 24; hdr[hdrlen++] = pktlen >> 24;
@ -291,13 +485,16 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
hdr[hdrlen++] = pktlen; hdr[hdrlen++] = pktlen;
partial = 0; partial = 0;
} }
else { /* next partial body length */ else
for (p=hdr; hdrlen; p++, hdrlen--) { { /* next partial body length */
for (p=hdr; hdrlen; p++, hdrlen--)
{
if ( putc (*p, fpout) == EOF ) if ( putc (*p, fpout) == EOF )
goto write_error; goto write_error;
} }
partlen = 1 << (c & 0x1f); partlen = 1 << (c & 0x1f);
for (; partlen; partlen--) { for (; partlen; partlen--)
{
if ((c = getc (fpin)) == EOF) if ((c = getc (fpin)) == EOF)
goto read_error; goto read_error;
if ( putc (c, fpout) == EOF ) if ( putc (c, fpout) == EOF )
@ -305,19 +502,22 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
} }
} }
} }
else if (partial == 2) { /* old gnupg */ else if (partial == 2)
{ /* old gnupg */
assert (!pktlen); assert (!pktlen);
if ( read_u16 (fpin, &partlen) ) if ( read_u16 (fpin, &partlen) )
goto read_error; goto read_error;
hdr[hdrlen++] = partlen >> 8; hdr[hdrlen++] = partlen >> 8;
hdr[hdrlen++] = partlen; hdr[hdrlen++] = partlen;
for (p=hdr; hdrlen; p++, hdrlen--) { for (p=hdr; hdrlen; p++, hdrlen--)
{
if ( putc (*p, fpout) == EOF ) if ( putc (*p, fpout) == EOF )
goto write_error; goto write_error;
} }
if (!partlen) if (!partlen)
partial = 0; /* end of packet */ partial = 0; /* end of packet */
for (; partlen; partlen--) { for (; partlen; partlen--)
{
c = getc (fpin); c = getc (fpin);
if (c == EOF) if (c == EOF)
goto read_error; goto read_error;
@ -325,11 +525,13 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
goto write_error; goto write_error;
} }
} }
else { /* compressed: read to end */ else
{ /* compressed: read to end */
pktlen = 0; pktlen = 0;
partial = 0; partial = 0;
hdrlen = 0; hdrlen = 0;
if (opt_uncompress) { if (opt_uncompress)
{
z_stream zs; z_stream zs;
byte *inbuf, *outbuf; byte *inbuf, *outbuf;
unsigned int inbufsize, outbufsize; unsigned int inbufsize, outbufsize;
@ -348,19 +550,22 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
zs.avail_in = 0; zs.avail_in = 0;
zinit_done = 0; zinit_done = 0;
do { do
if (zs.avail_in < inbufsize) { {
if (zs.avail_in < inbufsize)
{
n = zs.avail_in; n = zs.avail_in;
if (!n) if (!n)
zs.next_in = (Bytef *) inbuf; zs.next_in = (Bytef *) inbuf;
count = inbufsize - n; count = inbufsize - n;
for (nread=0; for (nread=0;
nread < count && (c=getc (fpin)) != EOF; nread < count && (c=getc (fpin)) != EOF;
nread++) { nread++)
inbuf[n+nread] = c; inbuf[n+nread] = c;
}
n += nread; n += nread;
if (nread < count && algo == 1) { if (nread < count && algo == 1)
{
inbuf[n] = 0xFF; /* chew dummy byte */ inbuf[n] = 0xFF; /* chew dummy byte */
n++; n++;
} }
@ -369,10 +574,12 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
zs.next_out = (Bytef *) outbuf; zs.next_out = (Bytef *) outbuf;
zs.avail_out = outbufsize; zs.avail_out = outbufsize;
if (!zinit_done) { if (!zinit_done)
zrc = algo == 1? inflateInit2 ( &zs, -13) {
: inflateInit ( &zs ); zrc = (algo == 1? inflateInit2 ( &zs, -13)
if (zrc != Z_OK) { : inflateInit ( &zs ));
if (zrc != Z_OK)
{
log_fatal ("zlib problem: %s\n", zs.msg? zs.msg : log_fatal ("zlib problem: %s\n", zs.msg? zs.msg :
zrc == Z_MEM_ERROR ? "out of core" : zrc == Z_MEM_ERROR ? "out of core" :
zrc == Z_VERSION_ERROR ? zrc == Z_VERSION_ERROR ?
@ -381,7 +588,8 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
} }
zinit_done = 1; zinit_done = 1;
} }
else { else
{
#ifdef Z_SYNC_FLUSH #ifdef Z_SYNC_FLUSH
zrc = inflate (&zs, Z_SYNC_FLUSH); zrc = inflate (&zs, Z_SYNC_FLUSH);
#else #else
@ -389,22 +597,27 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
#endif #endif
if (zrc == Z_STREAM_END) if (zrc == Z_STREAM_END)
; /* eof */ ; /* eof */
else if (zrc != Z_OK && zrc != Z_BUF_ERROR) { else if (zrc != Z_OK && zrc != Z_BUF_ERROR)
{
if (zs.msg) if (zs.msg)
log_fatal ("zlib inflate problem: %s\n", zs.msg ); log_fatal ("zlib inflate problem: %s\n", zs.msg );
else else
log_fatal ("zlib inflate problem: rc=%d\n", zrc ); log_fatal ("zlib inflate problem: rc=%d\n", zrc );
} }
for (n=0; n < outbufsize - zs.avail_out; n++) { for (n=0; n < outbufsize - zs.avail_out; n++)
{
if (putc (outbuf[n], fpout) == EOF ) if (putc (outbuf[n], fpout) == EOF )
goto write_error; goto write_error;
} }
} }
} while (zrc != Z_STREAM_END && zrc != Z_BUF_ERROR); }
while (zrc != Z_STREAM_END && zrc != Z_BUF_ERROR);
inflateEnd (&zs); inflateEnd (&zs);
} }
else { else
while ( (c=getc (fpin)) != EOF ) { {
while ( (c=getc (fpin)) != EOF )
{
if ( putc (c, fpout) == EOF ) if ( putc (c, fpout) == EOF )
goto write_error; goto write_error;
} }
@ -412,15 +625,17 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
if (!feof (fpin)) if (!feof (fpin))
goto read_error; goto read_error;
} }
} }
for (p=hdr; hdrlen; p++, hdrlen--) { for (p=hdr; hdrlen; p++, hdrlen--)
{
if ( putc (*p, fpout) == EOF ) if ( putc (*p, fpout) == EOF )
goto write_error; goto write_error;
} }
/* standard packet or last segment of partial length encoded packet */ /* standard packet or last segment of partial length encoded packet */
for (; pktlen; pktlen--) { for (; pktlen; pktlen--)
{
c = getc (fpin); c = getc (fpin);
if (c == EOF) if (c == EOF)
goto read_error; goto read_error;
@ -428,17 +643,20 @@ write_part ( const char *fname, FILE *fpin, unsigned long pktlen,
goto write_error; goto write_error;
} }
ready:
if ( fclose (fpout) ) if ( !opt_no_split && fclose (fpout) )
log_error ("error closing `%s': %s\n", outname, strerror (errno)); log_error ("error closing `%s': %s\n", outname, strerror (errno));
return 0; return 0;
write_error: write_error:
log_error ("error writing `%s': %s\n", outname, strerror (errno)); log_error ("error writing `%s': %s\n", outname, strerror (errno));
if (!opt_no_split)
fclose (fpout); fclose (fpout);
return 2; return 2;
read_error: { read_error:
if (!opt_no_split)
{
int save = errno; int save = errno;
fclose (fpout); fclose (fpout);
errno = save; errno = save;
@ -462,11 +680,13 @@ do_split (const char *fname, FILE *fp)
return 3; /* ready */ return 3; /* ready */
header[header_idx++] = ctb; header[header_idx++] = ctb;
if( !(ctb & 0x80) ) { if (!(ctb & 0x80))
{
log_error("invalid CTB %02x\n", ctb ); log_error("invalid CTB %02x\n", ctb );
return 1; return 1;
} }
if ( (ctb & 0x40) ) { /* new CTB */ if ( (ctb & 0x40) )
{ /* new CTB */
pkttype = (ctb & 0x3f); pkttype = (ctb & 0x3f);
if( (c = getc (fp)) == EOF ) if( (c = getc (fp)) == EOF )
return -1; return -1;
@ -474,14 +694,16 @@ do_split (const char *fname, FILE *fp)
if ( c < 192 ) if ( c < 192 )
pktlen = c; pktlen = c;
else if( c < 224 ) { else if ( c < 224 )
{
pktlen = (c - 192) * 256; pktlen = (c - 192) * 256;
if( (c = getc (fp)) == EOF ) if( (c = getc (fp)) == EOF )
return -1; return -1;
header[header_idx++] = c; header[header_idx++] = c;
pktlen += c + 192; pktlen += c + 192;
} }
else if( c == 255 ) { else if ( c == 255 )
{
if (read_u32 (fp, &pktlen)) if (read_u32 (fp, &pktlen))
return -1; return -1;
header[header_idx++] = pktlen >> 24; header[header_idx++] = pktlen >> 24;
@ -489,25 +711,30 @@ do_split (const char *fname, FILE *fp)
header[header_idx++] = pktlen >> 8; header[header_idx++] = pktlen >> 8;
header[header_idx++] = pktlen; header[header_idx++] = pktlen;
} }
else { /* partial body length */ else
{ /* partial body length */
pktlen = c; pktlen = c;
partial = 1; partial = 1;
} }
} }
else { else
{
int lenbytes; int lenbytes;
pkttype = (ctb>>2)&0xf; pkttype = (ctb>>2)&0xf;
lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3)); lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
if( !lenbytes ) { if (!lenbytes )
{
pktlen = 0; /* don't know the value */ pktlen = 0; /* don't know the value */
if( pkttype == PKT_COMPRESSED ) if( pkttype == PKT_COMPRESSED )
partial = 3; partial = 3;
else else
partial = 2; /* the old GnuPG partial length encoding */ partial = 2; /* the old GnuPG partial length encoding */
} }
else { else
for( ; lenbytes; lenbytes-- ) { {
for ( ; lenbytes; lenbytes-- )
{
pktlen <<= 8; pktlen <<= 8;
if( (c = getc (fp)) == EOF ) if( (c = getc (fp)) == EOF )
return -1; return -1;
@ -529,11 +756,13 @@ split_packets (const char *fname)
FILE *fp; FILE *fp;
int rc; int rc;
if (!fname || !strcmp (fname, "-")) { if (!fname || !strcmp (fname, "-"))
{
fp = stdin; fp = stdin;
fname = "-"; fname = "-";
} }
else if ( !(fp = fopen (fname,"rb")) ) { else if ( !(fp = fopen (fname,"rb")) )
{
log_error ("can't open `%s': %s\n", fname, strerror (errno)); log_error ("can't open `%s': %s\n", fname, strerror (errno));
return; return;
} }
@ -550,4 +779,3 @@ split_packets (const char *fname)
if ( fp != stdin ) if ( fp != stdin )
fclose (fp); fclose (fp);
} }