1997-11-18 14:06:00 +00:00
|
|
|
/* parse-packet.c - read packets
|
2001-03-08 14:33:24 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 14:06:00 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 14:06:00 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "packet.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "mpi.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "options.h"
|
1998-06-25 10:19:08 +00:00
|
|
|
#include "main.h"
|
1998-11-10 12:59:59 +00:00
|
|
|
#include "i18n.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-04-14 17:51:16 +00:00
|
|
|
static int mpi_print_mode = 0;
|
|
|
|
static int list_mode = 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2001-08-30 16:39:23 +00:00
|
|
|
static int parse( IOBUF inp, PACKET *pkt, int onlykeypkts,
|
2000-11-11 17:17:52 +00:00
|
|
|
off_t *retpos, int *skip, IOBUF out, int do_skip
|
1998-12-23 12:41:40 +00:00
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
,const char *dbg_w, const char *dbg_f, int dbg_l
|
|
|
|
#endif
|
|
|
|
);
|
1998-02-11 03:25:44 +00:00
|
|
|
static int copy_packet( IOBUF inp, IOBUF out, int pkttype,
|
|
|
|
unsigned long pktlen );
|
1997-11-18 14:06:00 +00:00
|
|
|
static void skip_packet( IOBUF inp, int pkttype, unsigned long pktlen );
|
|
|
|
static void skip_rest( IOBUF inp, unsigned long pktlen );
|
1999-05-08 17:35:16 +00:00
|
|
|
static void *read_rest( IOBUF inp, size_t pktlen );
|
1998-05-03 15:42:08 +00:00
|
|
|
static int parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet );
|
|
|
|
static int parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen,
|
1997-11-18 14:06:00 +00:00
|
|
|
PACKET *packet );
|
|
|
|
static int parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PKT_signature *sig );
|
1997-12-02 19:36:53 +00:00
|
|
|
static int parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PKT_onepass_sig *ops );
|
1998-06-29 12:30:57 +00:00
|
|
|
static int parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
|
1997-11-18 14:06:00 +00:00
|
|
|
byte *hdr, int hdrlen, PACKET *packet );
|
|
|
|
static int parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet );
|
1999-11-12 19:32:00 +00:00
|
|
|
static int parse_photo_id( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet );
|
1998-02-11 03:25:44 +00:00
|
|
|
static int parse_comment( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet );
|
1999-07-08 14:24:35 +00:00
|
|
|
static void parse_trust( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet );
|
1997-11-18 14:06:00 +00:00
|
|
|
static int parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
|
1998-07-02 19:31:46 +00:00
|
|
|
PACKET *packet, int new_ctb);
|
1997-11-18 14:06:00 +00:00
|
|
|
static int parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
|
1998-07-02 19:31:46 +00:00
|
|
|
PACKET *packet, int new_ctb );
|
1997-11-18 14:06:00 +00:00
|
|
|
static int parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
|
1998-07-02 19:31:46 +00:00
|
|
|
PACKET *packet, int new_ctb);
|
2000-06-05 10:27:46 +00:00
|
|
|
static int parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet, int new_ctb);
|
2000-10-13 15:03:48 +00:00
|
|
|
static int parse_gpg_control( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *packet );
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
static unsigned short
|
|
|
|
read_16(IOBUF inp)
|
|
|
|
{
|
|
|
|
unsigned short a;
|
|
|
|
a = iobuf_get_noeof(inp) << 8;
|
|
|
|
a |= iobuf_get_noeof(inp);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
read_32(IOBUF inp)
|
|
|
|
{
|
|
|
|
unsigned long a;
|
|
|
|
a = iobuf_get_noeof(inp) << 24;
|
|
|
|
a |= iobuf_get_noeof(inp) << 16;
|
|
|
|
a |= iobuf_get_noeof(inp) << 8;
|
|
|
|
a |= iobuf_get_noeof(inp);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
1998-03-09 21:44:06 +00:00
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
int
|
|
|
|
set_packet_list_mode( int mode )
|
|
|
|
{
|
|
|
|
int old = list_mode;
|
|
|
|
list_mode = mode;
|
|
|
|
mpi_print_mode = DBG_MPI;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
1998-07-02 19:31:46 +00:00
|
|
|
static void
|
|
|
|
unknown_pubkey_warning( int algo )
|
|
|
|
{
|
|
|
|
static byte unknown_pubkey_algos[256];
|
|
|
|
|
|
|
|
algo &= 0xff;
|
|
|
|
if( !unknown_pubkey_algos[algo] ) {
|
|
|
|
if( opt.verbose )
|
1998-11-10 12:59:59 +00:00
|
|
|
log_info(_("can't handle public key algorithm %d\n"), algo );
|
1998-07-02 19:31:46 +00:00
|
|
|
unknown_pubkey_algos[algo] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
/****************
|
|
|
|
* Parse a Packet and return it in packet
|
|
|
|
* Returns: 0 := valid packet in pkt
|
|
|
|
* -1 := no more packets
|
|
|
|
* >0 := error
|
|
|
|
* Note: The function may return an error and a partly valid packet;
|
|
|
|
* caller must free this packet.
|
|
|
|
*/
|
1998-12-23 12:41:40 +00:00
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
int
|
|
|
|
dbg_parse_packet( IOBUF inp, PACKET *pkt, const char *dbg_f, int dbg_l )
|
|
|
|
{
|
|
|
|
int skip, rc;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rc = parse( inp, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l );
|
|
|
|
} while( skip );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
1997-11-18 14:06:00 +00:00
|
|
|
int
|
|
|
|
parse_packet( IOBUF inp, PACKET *pkt )
|
1997-12-12 12:03:58 +00:00
|
|
|
{
|
|
|
|
int skip, rc;
|
|
|
|
|
|
|
|
do {
|
1998-02-11 03:25:44 +00:00
|
|
|
rc = parse( inp, pkt, 0, NULL, &skip, NULL, 0 );
|
1997-12-12 12:03:58 +00:00
|
|
|
} while( skip );
|
|
|
|
return rc;
|
|
|
|
}
|
1998-12-23 12:41:40 +00:00
|
|
|
#endif
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
|
|
/****************
|
2001-08-30 16:39:23 +00:00
|
|
|
* Like parse packet, but only return secret or public (sub)key packets.
|
1997-12-12 12:03:58 +00:00
|
|
|
*/
|
1998-12-23 12:41:40 +00:00
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
int
|
2001-09-06 17:10:00 +00:00
|
|
|
dbg_search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid,
|
1998-12-23 12:41:40 +00:00
|
|
|
const char *dbg_f, int dbg_l )
|
|
|
|
{
|
|
|
|
int skip, rc;
|
|
|
|
|
|
|
|
do {
|
2001-09-06 17:10:00 +00:00
|
|
|
rc = parse( inp, pkt, with_uid?2:1, retpos, &skip, NULL, 0, "search", dbg_f, dbg_l );
|
1998-12-23 12:41:40 +00:00
|
|
|
} while( skip );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
1997-12-12 12:03:58 +00:00
|
|
|
int
|
2001-09-06 17:10:00 +00:00
|
|
|
search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid )
|
1997-12-12 12:03:58 +00:00
|
|
|
{
|
|
|
|
int skip, rc;
|
|
|
|
|
|
|
|
do {
|
2001-09-06 17:10:00 +00:00
|
|
|
rc = parse( inp, pkt, with_uid?2:1, retpos, &skip, NULL, 0 );
|
1997-12-12 12:03:58 +00:00
|
|
|
} while( skip );
|
|
|
|
return rc;
|
|
|
|
}
|
1998-12-23 12:41:40 +00:00
|
|
|
#endif
|
1997-12-12 12:03:58 +00:00
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
/****************
|
|
|
|
* Copy all packets from INP to OUT, thereby removing unused spaces.
|
|
|
|
*/
|
1998-12-23 12:41:40 +00:00
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
int
|
|
|
|
dbg_copy_all_packets( IOBUF inp, IOBUF out,
|
|
|
|
const char *dbg_f, int dbg_l )
|
|
|
|
{
|
|
|
|
PACKET pkt;
|
|
|
|
int skip, rc=0;
|
|
|
|
do {
|
|
|
|
init_packet(&pkt);
|
|
|
|
} while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0, "copy", dbg_f, dbg_l )));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
1998-02-11 03:25:44 +00:00
|
|
|
int
|
|
|
|
copy_all_packets( IOBUF inp, IOBUF out )
|
|
|
|
{
|
|
|
|
PACKET pkt;
|
|
|
|
int skip, rc=0;
|
|
|
|
do {
|
|
|
|
init_packet(&pkt);
|
|
|
|
} while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0 )));
|
|
|
|
return rc;
|
|
|
|
}
|
1998-12-23 12:41:40 +00:00
|
|
|
#endif
|
1998-02-11 03:25:44 +00:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* Copy some packets from INP to OUT, thereby removing unused spaces.
|
1998-04-14 17:51:16 +00:00
|
|
|
* Stop at offset STOPoff (i.e. don't copy packets at this or later offsets)
|
1998-02-11 03:25:44 +00:00
|
|
|
*/
|
1998-12-23 12:41:40 +00:00
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
int
|
2000-11-11 17:17:52 +00:00
|
|
|
dbg_copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff,
|
1998-12-23 12:41:40 +00:00
|
|
|
const char *dbg_f, int dbg_l )
|
|
|
|
{
|
|
|
|
PACKET pkt;
|
|
|
|
int skip, rc=0;
|
|
|
|
do {
|
|
|
|
if( iobuf_tell(inp) >= stopoff )
|
|
|
|
return 0;
|
|
|
|
init_packet(&pkt);
|
|
|
|
} while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0,
|
|
|
|
"some", dbg_f, dbg_l )) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
1998-02-11 03:25:44 +00:00
|
|
|
int
|
2000-11-11 17:17:52 +00:00
|
|
|
copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff )
|
1998-02-11 03:25:44 +00:00
|
|
|
{
|
|
|
|
PACKET pkt;
|
|
|
|
int skip, rc=0;
|
|
|
|
do {
|
|
|
|
if( iobuf_tell(inp) >= stopoff )
|
|
|
|
return 0;
|
|
|
|
init_packet(&pkt);
|
|
|
|
} while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0 )) );
|
|
|
|
return rc;
|
|
|
|
}
|
1998-12-23 12:41:40 +00:00
|
|
|
#endif
|
1998-02-11 03:25:44 +00:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* Skip over N packets
|
|
|
|
*/
|
1998-12-23 12:41:40 +00:00
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
int
|
|
|
|
dbg_skip_some_packets( IOBUF inp, unsigned n,
|
|
|
|
const char *dbg_f, int dbg_l )
|
|
|
|
{
|
|
|
|
int skip, rc=0;
|
|
|
|
PACKET pkt;
|
|
|
|
|
|
|
|
for( ;n && !rc; n--) {
|
|
|
|
init_packet(&pkt);
|
|
|
|
rc = parse( inp, &pkt, 0, NULL, &skip, NULL, 1, "skip", dbg_f, dbg_l );
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
1998-02-11 03:25:44 +00:00
|
|
|
int
|
|
|
|
skip_some_packets( IOBUF inp, unsigned n )
|
|
|
|
{
|
|
|
|
int skip, rc=0;
|
|
|
|
PACKET pkt;
|
|
|
|
|
|
|
|
for( ;n && !rc; n--) {
|
|
|
|
init_packet(&pkt);
|
|
|
|
rc = parse( inp, &pkt, 0, NULL, &skip, NULL, 1 );
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
1998-12-23 12:41:40 +00:00
|
|
|
#endif
|
1998-06-29 12:30:57 +00:00
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
/****************
|
2001-08-30 16:39:23 +00:00
|
|
|
* Parse packet. Set the variable skip points to 1 if the packet
|
|
|
|
* should be skipped; this is the case if either ONLYKEYPKTS is set
|
|
|
|
* and the parsed packet isn't one or the
|
1997-12-12 12:03:58 +00:00
|
|
|
* packet-type is 0, indicating deleted stuff.
|
1998-02-11 03:25:44 +00:00
|
|
|
* if OUT is not NULL, a special copymode is used.
|
1997-12-12 12:03:58 +00:00
|
|
|
*/
|
|
|
|
static int
|
2001-08-30 16:39:23 +00:00
|
|
|
parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
|
1998-12-23 12:41:40 +00:00
|
|
|
int *skip, IOBUF out, int do_skip
|
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
,const char *dbg_w, const char *dbg_f, int dbg_l
|
|
|
|
#endif
|
|
|
|
)
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
1998-07-06 10:23:57 +00:00
|
|
|
int rc=0, c, ctb, pkttype, lenbytes;
|
1997-11-18 14:06:00 +00:00
|
|
|
unsigned long pktlen;
|
1998-05-13 17:53:36 +00:00
|
|
|
byte hdr[8];
|
1997-11-18 14:06:00 +00:00
|
|
|
int hdrlen;
|
1998-07-02 19:31:46 +00:00
|
|
|
int new_ctb = 0;
|
2001-09-06 17:10:00 +00:00
|
|
|
int with_uid = (onlykeypkts == 2);
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
*skip = 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
assert( !pkt->pkt.generic );
|
1997-12-12 12:03:58 +00:00
|
|
|
if( retpos )
|
|
|
|
*retpos = iobuf_tell(inp);
|
1998-12-23 12:41:40 +00:00
|
|
|
|
1998-07-06 10:23:57 +00:00
|
|
|
if( (ctb = iobuf_get(inp)) == -1 ) {
|
|
|
|
rc = -1;
|
|
|
|
goto leave;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
hdrlen=0;
|
|
|
|
hdr[hdrlen++] = ctb;
|
|
|
|
if( !(ctb & 0x80) ) {
|
2001-09-06 17:10:00 +00:00
|
|
|
log_error("%s: invalid packet (ctb=%02x)\n", iobuf_where(inp), ctb );
|
1998-07-06 10:23:57 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
pktlen = 0;
|
1998-07-02 19:31:46 +00:00
|
|
|
new_ctb = !!(ctb & 0x40);
|
|
|
|
if( new_ctb ) {
|
1998-01-16 21:15:24 +00:00
|
|
|
pkttype = ctb & 0x3f;
|
|
|
|
if( (c = iobuf_get(inp)) == -1 ) {
|
|
|
|
log_error("%s: 1st length byte missing\n", iobuf_where(inp) );
|
1998-07-06 10:23:57 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
1998-01-16 21:15:24 +00:00
|
|
|
}
|
|
|
|
hdr[hdrlen++] = c;
|
|
|
|
if( c < 192 )
|
|
|
|
pktlen = c;
|
|
|
|
else if( c < 224 ) {
|
|
|
|
pktlen = (c - 192) * 256;
|
|
|
|
if( (c = iobuf_get(inp)) == -1 ) {
|
|
|
|
log_error("%s: 2nd length byte missing\n", iobuf_where(inp) );
|
1998-07-06 10:23:57 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
1998-01-16 21:15:24 +00:00
|
|
|
}
|
|
|
|
hdr[hdrlen++] = c;
|
|
|
|
pktlen += c + 192;
|
|
|
|
}
|
1998-07-02 19:31:46 +00:00
|
|
|
else if( c == 255 ) {
|
1998-05-13 17:53:36 +00:00
|
|
|
pktlen = (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 24;
|
|
|
|
pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 16;
|
|
|
|
pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 8;
|
|
|
|
if( (c = iobuf_get(inp)) == -1 ) {
|
|
|
|
log_error("%s: 4 byte length invalid\n", iobuf_where(inp) );
|
1998-07-06 10:23:57 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
1998-05-13 17:53:36 +00:00
|
|
|
}
|
|
|
|
pktlen |= (hdr[hdrlen++] = c );
|
|
|
|
}
|
1998-01-16 21:15:24 +00:00
|
|
|
else { /* partial body length */
|
1998-07-02 19:31:46 +00:00
|
|
|
iobuf_set_partial_block_mode(inp, c & 0xff);
|
1998-01-16 21:15:24 +00:00
|
|
|
pktlen = 0;/* to indicate partial length */
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 21:15:24 +00:00
|
|
|
pkttype = (ctb>>2)&0xf;
|
|
|
|
lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
|
|
|
|
if( !lenbytes ) {
|
|
|
|
pktlen = 0; /* don't know the value */
|
|
|
|
if( pkttype != PKT_COMPRESSED )
|
|
|
|
iobuf_set_block_mode(inp, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( ; lenbytes; lenbytes-- ) {
|
|
|
|
pktlen <<= 8;
|
|
|
|
pktlen |= hdr[hdrlen++] = iobuf_get_noeof(inp);
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-18 09:40:31 +00:00
|
|
|
if (pktlen == 0xffffffff) {
|
|
|
|
/* with a some probability this is caused by a problem in the
|
|
|
|
* the uncompressing layer - in some error cases it just loops
|
|
|
|
* and spits out 0xff bytes. */
|
|
|
|
log_error ("%s: garbled packet detected\n", iobuf_where(inp) );
|
|
|
|
g10_exit (2);
|
|
|
|
}
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
if( out && pkttype ) {
|
|
|
|
if( iobuf_write( out, hdr, hdrlen ) == -1 )
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
else
|
|
|
|
rc = copy_packet(inp, out, pkttype, pktlen );
|
1998-07-06 10:23:57 +00:00
|
|
|
goto leave;
|
1998-02-11 03:25:44 +00:00
|
|
|
}
|
|
|
|
|
2001-09-06 17:10:00 +00:00
|
|
|
if (with_uid && pkttype == PKT_USER_ID)
|
|
|
|
;
|
|
|
|
else if( do_skip
|
2001-08-30 16:39:23 +00:00
|
|
|
|| !pkttype
|
|
|
|
|| (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
|
|
|
|
&& pkttype != PKT_PUBLIC_KEY
|
|
|
|
&& pkttype != PKT_SECRET_SUBKEY
|
2001-09-06 17:10:00 +00:00
|
|
|
&& pkttype != PKT_SECRET_KEY ) ) {
|
1998-11-20 17:42:18 +00:00
|
|
|
skip_rest(inp, pktlen);
|
1997-12-12 12:03:58 +00:00
|
|
|
*skip = 1;
|
1998-07-06 10:23:57 +00:00
|
|
|
rc = 0;
|
|
|
|
goto leave;
|
1997-12-12 12:03:58 +00:00
|
|
|
}
|
|
|
|
|
1998-12-23 12:41:40 +00:00
|
|
|
if( DBG_PACKET ) {
|
|
|
|
#ifdef DEBUG_PARSE_PACKET
|
|
|
|
log_debug("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
|
|
|
|
iobuf_id(inp), pkttype, pktlen, new_ctb?" (new_ctb)":"",
|
|
|
|
dbg_w, dbg_f, dbg_l );
|
|
|
|
#else
|
1998-01-16 21:15:24 +00:00
|
|
|
log_debug("parse_packet(iob=%d): type=%d length=%lu%s\n",
|
1998-07-02 19:31:46 +00:00
|
|
|
iobuf_id(inp), pkttype, pktlen, new_ctb?" (new_ctb)":"" );
|
1998-12-23 12:41:40 +00:00
|
|
|
#endif
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
pkt->pkttype = pkttype;
|
1998-01-16 21:15:24 +00:00
|
|
|
rc = G10ERR_UNKNOWN_PACKET; /* default error */
|
1997-11-18 14:06:00 +00:00
|
|
|
switch( pkttype ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
case PKT_PUBLIC_KEY:
|
|
|
|
case PKT_PUBLIC_SUBKEY:
|
|
|
|
pkt->pkt.public_key = m_alloc_clear(sizeof *pkt->pkt.public_key );
|
|
|
|
rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
1998-06-29 12:30:57 +00:00
|
|
|
case PKT_SECRET_KEY:
|
|
|
|
case PKT_SECRET_SUBKEY:
|
|
|
|
pkt->pkt.secret_key = m_alloc_clear(sizeof *pkt->pkt.secret_key );
|
|
|
|
rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
1998-05-03 15:42:08 +00:00
|
|
|
case PKT_SYMKEY_ENC:
|
|
|
|
rc = parse_symkeyenc( inp, pkttype, pktlen, pkt );
|
|
|
|
break;
|
1997-11-18 14:06:00 +00:00
|
|
|
case PKT_PUBKEY_ENC:
|
1998-05-03 15:42:08 +00:00
|
|
|
rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt );
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
|
|
|
case PKT_SIGNATURE:
|
|
|
|
pkt->pkt.signature = m_alloc_clear(sizeof *pkt->pkt.signature );
|
|
|
|
rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature );
|
|
|
|
break;
|
1997-12-02 19:36:53 +00:00
|
|
|
case PKT_ONEPASS_SIG:
|
|
|
|
pkt->pkt.onepass_sig = m_alloc_clear(sizeof *pkt->pkt.onepass_sig );
|
|
|
|
rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig );
|
|
|
|
break;
|
1997-11-18 14:06:00 +00:00
|
|
|
case PKT_USER_ID:
|
|
|
|
rc = parse_user_id(inp, pkttype, pktlen, pkt );
|
|
|
|
break;
|
1999-11-12 19:32:00 +00:00
|
|
|
case PKT_PHOTO_ID:
|
2001-07-26 09:37:47 +00:00
|
|
|
pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
|
1999-11-12 19:32:00 +00:00
|
|
|
rc = parse_photo_id(inp, pkttype, pktlen, pkt);
|
|
|
|
break;
|
1998-05-03 19:35:33 +00:00
|
|
|
case PKT_OLD_COMMENT:
|
1997-11-18 14:06:00 +00:00
|
|
|
case PKT_COMMENT:
|
1998-02-11 03:25:44 +00:00
|
|
|
rc = parse_comment(inp, pkttype, pktlen, pkt);
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
|
|
|
case PKT_RING_TRUST:
|
1999-07-08 14:24:35 +00:00
|
|
|
parse_trust(inp, pkttype, pktlen, pkt);
|
|
|
|
rc = 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
|
|
|
case PKT_PLAINTEXT:
|
1998-07-02 19:31:46 +00:00
|
|
|
rc = parse_plaintext(inp, pkttype, pktlen, pkt, new_ctb );
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
1997-12-01 10:33:23 +00:00
|
|
|
case PKT_COMPRESSED:
|
1998-07-02 19:31:46 +00:00
|
|
|
rc = parse_compressed(inp, pkttype, pktlen, pkt, new_ctb );
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
1997-12-01 10:33:23 +00:00
|
|
|
case PKT_ENCRYPTED:
|
1999-05-17 20:03:24 +00:00
|
|
|
case PKT_ENCRYPTED_MDC:
|
1998-07-02 19:31:46 +00:00
|
|
|
rc = parse_encrypted(inp, pkttype, pktlen, pkt, new_ctb );
|
1997-11-18 14:06:00 +00:00
|
|
|
break;
|
2000-06-05 10:27:46 +00:00
|
|
|
case PKT_MDC:
|
|
|
|
rc = parse_mdc(inp, pkttype, pktlen, pkt, new_ctb );
|
|
|
|
break;
|
2000-10-13 15:03:48 +00:00
|
|
|
case PKT_GPG_CONTROL:
|
|
|
|
rc = parse_gpg_control(inp, pkttype, pktlen, pkt );
|
|
|
|
break;
|
1997-11-18 14:06:00 +00:00
|
|
|
default:
|
|
|
|
skip_packet(inp, pkttype, pktlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-07-06 10:23:57 +00:00
|
|
|
leave:
|
1998-07-29 19:35:05 +00:00
|
|
|
if( !rc && iobuf_error(inp) )
|
1998-07-06 10:23:57 +00:00
|
|
|
rc = G10ERR_INV_KEYRING;
|
1997-11-18 14:06:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1998-01-16 21:15:24 +00:00
|
|
|
static void
|
|
|
|
dump_hex_line( int c, int *i )
|
|
|
|
{
|
|
|
|
if( *i && !(*i%8) ) {
|
|
|
|
if( *i && !(*i%24) )
|
|
|
|
printf("\n%4d:", *i );
|
|
|
|
else
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
if( c == -1 )
|
|
|
|
printf(" EOF" );
|
|
|
|
else
|
|
|
|
printf(" %02x", c );
|
|
|
|
++*i;
|
|
|
|
}
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
static int
|
|
|
|
copy_packet( IOBUF inp, IOBUF out, int pkttype, unsigned long pktlen )
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
if( iobuf_in_block_mode(inp) ) {
|
|
|
|
while( (n = iobuf_read( inp, buf, 100 )) != -1 )
|
|
|
|
if( iobuf_write(out, buf, n ) )
|
|
|
|
return G10ERR_WRITE_FILE; /* write error */
|
|
|
|
}
|
|
|
|
else if( !pktlen && pkttype == PKT_COMPRESSED ) {
|
1998-12-23 12:41:40 +00:00
|
|
|
log_debug("copy_packet: compressed!\n");
|
1998-02-11 03:25:44 +00:00
|
|
|
/* compressed packet, copy till EOF */
|
|
|
|
while( (n = iobuf_read( inp, buf, 100 )) != -1 )
|
|
|
|
if( iobuf_write(out, buf, n ) )
|
|
|
|
return G10ERR_WRITE_FILE; /* write error */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( ; pktlen; pktlen -= n ) {
|
|
|
|
n = pktlen > 100 ? 100 : pktlen;
|
|
|
|
n = iobuf_read( inp, buf, n );
|
|
|
|
if( n == -1 )
|
|
|
|
return G10ERR_READ_FILE;
|
|
|
|
if( iobuf_write(out, buf, n ) )
|
|
|
|
return G10ERR_WRITE_FILE; /* write error */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
static void
|
|
|
|
skip_packet( IOBUF inp, int pkttype, unsigned long pktlen )
|
|
|
|
{
|
1998-01-16 21:15:24 +00:00
|
|
|
if( list_mode ) {
|
1998-11-18 19:59:06 +00:00
|
|
|
if( pkttype == PKT_MARKER )
|
|
|
|
fputs(":marker packet:\n", stdout );
|
|
|
|
else
|
|
|
|
printf(":unknown packet: type %2d, length %lu\n", pkttype, pktlen);
|
1998-01-16 21:15:24 +00:00
|
|
|
if( pkttype ) {
|
|
|
|
int c, i=0 ;
|
1998-11-18 19:59:06 +00:00
|
|
|
if( pkttype != PKT_MARKER )
|
|
|
|
fputs("dump:", stdout );
|
1998-01-16 21:15:24 +00:00
|
|
|
if( iobuf_in_block_mode(inp) ) {
|
|
|
|
while( (c=iobuf_get(inp)) != -1 )
|
|
|
|
dump_hex_line(c, &i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( ; pktlen; pktlen-- )
|
|
|
|
dump_hex_line(iobuf_get(inp), &i);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
skip_rest(inp,pktlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
skip_rest( IOBUF inp, unsigned long pktlen )
|
|
|
|
{
|
|
|
|
if( iobuf_in_block_mode(inp) ) {
|
|
|
|
while( iobuf_get(inp) != -1 )
|
|
|
|
;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( ; pktlen; pktlen-- )
|
1998-07-29 19:35:05 +00:00
|
|
|
if( iobuf_get(inp) == -1 )
|
|
|
|
break;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-02 19:31:46 +00:00
|
|
|
|
1998-08-05 16:51:59 +00:00
|
|
|
static void *
|
1999-05-08 17:35:16 +00:00
|
|
|
read_rest( IOBUF inp, size_t pktlen )
|
1998-08-05 16:51:59 +00:00
|
|
|
{
|
|
|
|
byte *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( iobuf_in_block_mode(inp) ) {
|
|
|
|
log_error("read_rest: can't store stream data\n");
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p = m_alloc( pktlen );
|
|
|
|
for(i=0; pktlen; pktlen--, i++ )
|
|
|
|
p[i] = iobuf_get(inp);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
static int
|
1998-05-03 15:42:08 +00:00
|
|
|
parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
|
|
|
|
{
|
|
|
|
PKT_symkey_enc *k;
|
2001-03-12 20:21:50 +00:00
|
|
|
int rc = 0;
|
1998-05-03 15:42:08 +00:00
|
|
|
int i, version, s2kmode, cipher_algo, hash_algo, seskeylen, minlen;
|
|
|
|
|
|
|
|
if( pktlen < 4 ) {
|
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-05-03 15:42:08 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
version = iobuf_get_noeof(inp); pktlen--;
|
1998-05-04 18:49:26 +00:00
|
|
|
if( version != 4 ) {
|
1998-05-03 15:42:08 +00:00
|
|
|
log_error("packet(%d) with unknown version %d\n", pkttype, version);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-05-03 15:42:08 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( pktlen > 200 ) { /* (we encode the seskeylen in a byte) */
|
|
|
|
log_error("packet(%d) too large\n", pkttype);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-05-03 15:42:08 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
cipher_algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
s2kmode = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
hash_algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
switch( s2kmode ) {
|
|
|
|
case 0: /* simple s2k */
|
|
|
|
minlen = 0;
|
|
|
|
break;
|
|
|
|
case 1: /* salted s2k */
|
|
|
|
minlen = 8;
|
|
|
|
break;
|
1998-08-05 16:51:59 +00:00
|
|
|
case 3: /* iterated+salted s2k */
|
|
|
|
minlen = 9;
|
1998-05-03 15:42:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_error("unknown S2K %d\n", s2kmode );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( minlen > pktlen ) {
|
|
|
|
log_error("packet with S2K %d too short\n", s2kmode );
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-05-03 15:42:08 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
seskeylen = pktlen - minlen;
|
|
|
|
k = packet->pkt.symkey_enc = m_alloc_clear( sizeof *packet->pkt.symkey_enc
|
|
|
|
+ seskeylen - 1 );
|
|
|
|
k->version = version;
|
|
|
|
k->cipher_algo = cipher_algo;
|
|
|
|
k->s2k.mode = s2kmode;
|
|
|
|
k->s2k.hash_algo = hash_algo;
|
1998-08-05 16:51:59 +00:00
|
|
|
if( s2kmode == 1 || s2kmode == 3 ) {
|
1998-05-03 15:42:08 +00:00
|
|
|
for(i=0; i < 8 && pktlen; i++, pktlen-- )
|
|
|
|
k->s2k.salt[i] = iobuf_get_noeof(inp);
|
|
|
|
}
|
1998-08-05 16:51:59 +00:00
|
|
|
if( s2kmode == 3 ) {
|
|
|
|
k->s2k.count = iobuf_get(inp); pktlen--;
|
1998-05-03 15:42:08 +00:00
|
|
|
}
|
|
|
|
k->seskeylen = seskeylen;
|
|
|
|
for(i=0; i < seskeylen && pktlen; i++, pktlen-- )
|
|
|
|
k->seskey[i] = iobuf_get_noeof(inp);
|
|
|
|
assert( !pktlen );
|
|
|
|
|
|
|
|
if( list_mode ) {
|
|
|
|
printf(":symkey enc packet: version %d, cipher %d, s2k %d, hash %d\n",
|
|
|
|
version, cipher_algo, s2kmode, hash_algo);
|
1998-08-05 16:51:59 +00:00
|
|
|
if( s2kmode == 1 || s2kmode == 3 ) {
|
1998-05-03 15:42:08 +00:00
|
|
|
printf("\tsalt ");
|
|
|
|
for(i=0; i < 8; i++ )
|
|
|
|
printf("%02x", k->s2k.salt[i]);
|
1998-08-05 16:51:59 +00:00
|
|
|
if( s2kmode == 3 )
|
1998-05-03 15:42:08 +00:00
|
|
|
printf(", count %lu\n", (ulong)k->s2k.count );
|
1998-05-04 18:49:26 +00:00
|
|
|
printf("\n");
|
1998-05-03 15:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
skip_rest(inp, pktlen);
|
2001-03-12 20:21:50 +00:00
|
|
|
return rc;
|
1998-05-03 15:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
2001-03-12 20:21:50 +00:00
|
|
|
unsigned int n;
|
|
|
|
int rc = 0;
|
1998-06-13 06:59:14 +00:00
|
|
|
int i, ndata;
|
1997-11-18 14:06:00 +00:00
|
|
|
PKT_pubkey_enc *k;
|
|
|
|
|
1999-06-15 12:31:07 +00:00
|
|
|
k = packet->pkt.pubkey_enc = m_alloc_clear(sizeof *packet->pkt.pubkey_enc);
|
1997-11-18 14:06:00 +00:00
|
|
|
if( pktlen < 12 ) {
|
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
1998-03-19 15:27:29 +00:00
|
|
|
k->version = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( k->version != 2 && k->version != 3 ) {
|
|
|
|
log_error("packet(%d) with unknown version %d\n", pkttype, k->version);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
k->keyid[0] = read_32(inp); pktlen -= 4;
|
|
|
|
k->keyid[1] = read_32(inp); pktlen -= 4;
|
|
|
|
k->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
|
1998-09-11 05:47:32 +00:00
|
|
|
k->throw_keyid = 0; /* only used as flag for build_packet */
|
1997-11-18 14:06:00 +00:00
|
|
|
if( list_mode )
|
1998-05-04 18:49:26 +00:00
|
|
|
printf(":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
|
|
|
|
k->version, k->pubkey_algo, (ulong)k->keyid[0], (ulong)k->keyid[1]);
|
1998-06-13 06:59:14 +00:00
|
|
|
|
|
|
|
ndata = pubkey_get_nenc(k->pubkey_algo);
|
1998-07-02 19:31:46 +00:00
|
|
|
if( !ndata ) {
|
|
|
|
if( list_mode )
|
|
|
|
printf("\tunsupported algorithm %d\n", k->pubkey_algo );
|
|
|
|
unknown_pubkey_warning( k->pubkey_algo );
|
|
|
|
k->data[0] = NULL; /* no need to store the encrypted data */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( i=0; i < ndata; i++ ) {
|
|
|
|
n = pktlen;
|
|
|
|
k->data[i] = mpi_read(inp, &n, 0); pktlen -=n;
|
|
|
|
if( list_mode ) {
|
|
|
|
printf("\tdata: ");
|
|
|
|
mpi_print(stdout, k->data[i], mpi_print_mode );
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (!k->data[i])
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
skip_rest(inp, pktlen);
|
2001-03-12 20:21:50 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-18 15:21:22 +00:00
|
|
|
static void
|
|
|
|
dump_sig_subpkt( int hashed, int type, int critical,
|
1998-11-18 19:59:06 +00:00
|
|
|
const byte *buffer, size_t buflen, size_t length )
|
1998-10-18 15:21:22 +00:00
|
|
|
{
|
|
|
|
const char *p=NULL;
|
1998-11-18 19:59:06 +00:00
|
|
|
int i;
|
1998-10-18 15:21:22 +00:00
|
|
|
|
2000-09-14 15:45:03 +00:00
|
|
|
/* The CERT has warning out with explains how to use GNUPG to
|
|
|
|
* detect the ARRs - we print our old message here when it is a faked
|
|
|
|
* ARR and add an additional notice */
|
|
|
|
if ( type == SIGSUBPKT_ARR && !hashed ) {
|
|
|
|
printf("\tsubpkt %d len %u (additional recipient request)\n"
|
|
|
|
"WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
|
|
|
|
"encrypt to this key and thereby reveal the plaintext to "
|
|
|
|
"the owner of this ARR key. Detailed info follows:\n",
|
|
|
|
type, (unsigned)length );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-18 15:21:22 +00:00
|
|
|
printf("\t%s%ssubpkt %d len %u (", /*)*/
|
|
|
|
critical ? "critical ":"",
|
|
|
|
hashed ? "hashed ":"", type, (unsigned)length );
|
|
|
|
buffer++;
|
|
|
|
length--;
|
|
|
|
if( length > buflen ) {
|
|
|
|
printf("too short: buffer is only %u)\n", (unsigned)buflen );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch( type ) {
|
|
|
|
case SIGSUBPKT_SIG_CREATED:
|
|
|
|
if( length >= 4 )
|
|
|
|
printf("sig created %s", strtimestamp( buffer_to_u32(buffer) ) );
|
|
|
|
break;
|
|
|
|
case SIGSUBPKT_SIG_EXPIRE:
|
|
|
|
if( length >= 4 )
|
1998-11-20 17:42:18 +00:00
|
|
|
printf("sig expires after %s",
|
|
|
|
strtimevalue( buffer_to_u32(buffer) ) );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_EXPORTABLE:
|
1998-11-03 19:38:58 +00:00
|
|
|
if( length )
|
|
|
|
printf("%sexportable", *buffer? "":"not ");
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_TRUST:
|
|
|
|
p = "trust signature";
|
|
|
|
break;
|
|
|
|
case SIGSUBPKT_REGEXP:
|
|
|
|
p = "regular expression";
|
|
|
|
break;
|
|
|
|
case SIGSUBPKT_REVOCABLE:
|
2001-09-18 09:40:31 +00:00
|
|
|
if( length )
|
|
|
|
printf("%srevocable", *buffer? "":"not ");
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_KEY_EXPIRE:
|
|
|
|
if( length >= 4 )
|
1998-11-20 17:42:18 +00:00
|
|
|
printf("key expires after %s",
|
|
|
|
strtimevalue( buffer_to_u32(buffer) ) );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_PREF_SYM:
|
1998-11-18 19:59:06 +00:00
|
|
|
fputs("pref-sym-algos:", stdout );
|
|
|
|
for( i=0; i < length; i++ )
|
|
|
|
printf(" %d", buffer[i] );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_REV_KEY:
|
1999-08-04 08:45:27 +00:00
|
|
|
fputs("revocation key: ", stdout );
|
|
|
|
if( length < 22 )
|
|
|
|
p = "[too short]";
|
|
|
|
else {
|
|
|
|
printf("c=%02x a=%d f=", buffer[0], buffer[1] );
|
|
|
|
for( i=2; i < length; i++ )
|
|
|
|
printf("%02X", buffer[i] );
|
|
|
|
}
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_ISSUER:
|
|
|
|
if( length >= 8 )
|
|
|
|
printf("issuer key ID %08lX%08lX",
|
|
|
|
(ulong)buffer_to_u32(buffer),
|
|
|
|
(ulong)buffer_to_u32(buffer+4) );
|
|
|
|
break;
|
|
|
|
case SIGSUBPKT_NOTATION:
|
1999-05-25 17:56:15 +00:00
|
|
|
{
|
|
|
|
fputs("notation: ", stdout );
|
|
|
|
if( length < 8 )
|
|
|
|
p = "[too short]";
|
|
|
|
else if( !(*buffer & 0x80) )
|
|
|
|
p = "[not human readable]";
|
|
|
|
else {
|
|
|
|
const byte *s = buffer;
|
|
|
|
size_t n1, n2;
|
|
|
|
|
|
|
|
n1 = (s[4] << 8) | s[5];
|
|
|
|
n2 = (s[6] << 8) | s[7];
|
|
|
|
s += 8;
|
|
|
|
if( 8+n1+n2 != length )
|
|
|
|
p = "[error]";
|
|
|
|
else {
|
1999-05-26 12:41:46 +00:00
|
|
|
print_string( stdout, s, n1, ')' );
|
1999-05-25 17:56:15 +00:00
|
|
|
putc( '=', stdout );
|
1999-05-26 12:41:46 +00:00
|
|
|
print_string( stdout, s+n1, n2, ')' );
|
1999-05-25 17:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_PREF_HASH:
|
1998-11-18 19:59:06 +00:00
|
|
|
fputs("pref-hash-algos:", stdout );
|
|
|
|
for( i=0; i < length; i++ )
|
|
|
|
printf(" %d", buffer[i] );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_PREF_COMPR:
|
1998-11-18 19:59:06 +00:00
|
|
|
fputs("pref-zip-algos:", stdout );
|
|
|
|
for( i=0; i < length; i++ )
|
|
|
|
printf(" %d", buffer[i] );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_KS_FLAGS:
|
2001-09-18 09:40:31 +00:00
|
|
|
fputs("key server preferences:",stdout);
|
|
|
|
for(i=0;i<length;i++)
|
|
|
|
printf(" %02X", buffer[i]);
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_PREF_KS:
|
|
|
|
p = "preferred key server";
|
|
|
|
break;
|
|
|
|
case SIGSUBPKT_PRIMARY_UID:
|
1999-09-01 13:40:07 +00:00
|
|
|
p = "primary user ID";
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_POLICY:
|
1999-05-26 12:41:46 +00:00
|
|
|
fputs("policy: ", stdout );
|
|
|
|
print_string( stdout, buffer, length, ')' );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_KEY_FLAGS:
|
2000-09-14 15:45:03 +00:00
|
|
|
fputs ( "key flags:", stdout );
|
|
|
|
for( i=0; i < length; i++ )
|
|
|
|
printf(" %02X", buffer[i] );
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
case SIGSUBPKT_SIGNERS_UID:
|
1999-09-01 13:40:07 +00:00
|
|
|
p = "signer's user ID";
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
2000-02-17 12:41:58 +00:00
|
|
|
case SIGSUBPKT_REVOC_REASON:
|
2000-08-25 14:00:15 +00:00
|
|
|
if( length ) {
|
2000-02-17 12:41:58 +00:00
|
|
|
printf("revocation reason 0x%02x (", *buffer );
|
|
|
|
print_string( stdout, buffer+1, length-1, ')' );
|
|
|
|
p = ")";
|
|
|
|
}
|
|
|
|
break;
|
2000-08-25 14:00:15 +00:00
|
|
|
case SIGSUBPKT_ARR:
|
|
|
|
fputs("Big Brother's key (ignored): ", stdout );
|
|
|
|
if( length < 22 )
|
|
|
|
p = "[too short]";
|
|
|
|
else {
|
|
|
|
printf("c=%02x a=%d f=", buffer[0], buffer[1] );
|
|
|
|
for( i=2; i < length; i++ )
|
|
|
|
printf("%02X", buffer[i] );
|
|
|
|
}
|
|
|
|
break;
|
2001-08-30 16:39:23 +00:00
|
|
|
case SIGSUBPKT_FEATURES:
|
|
|
|
fputs ( "features:", stdout );
|
|
|
|
for( i=0; i < length; i++ )
|
2001-09-06 17:10:00 +00:00
|
|
|
printf(" %02x", buffer[i] );
|
2001-08-30 16:39:23 +00:00
|
|
|
break;
|
2001-03-05 13:59:16 +00:00
|
|
|
case SIGSUBPKT_PRIV_VERIFY_CACHE:
|
2001-09-25 15:20:59 +00:00
|
|
|
p = "obsolete verification cache";
|
1998-10-18 15:21:22 +00:00
|
|
|
break;
|
|
|
|
default: p = "?"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s)\n", p? p: "");
|
|
|
|
}
|
|
|
|
|
1998-12-14 20:22:42 +00:00
|
|
|
/****************
|
|
|
|
* Returns: >= 0 offset into buffer
|
|
|
|
* -1 unknown type
|
|
|
|
* -2 unsupported type
|
|
|
|
* -3 subpacket too short
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_one_sig_subpkt( const byte *buffer, size_t n, int type )
|
|
|
|
{
|
|
|
|
switch( type ) {
|
|
|
|
case SIGSUBPKT_SIG_CREATED:
|
|
|
|
case SIGSUBPKT_SIG_EXPIRE:
|
|
|
|
case SIGSUBPKT_KEY_EXPIRE:
|
|
|
|
if( n < 4 )
|
|
|
|
break;
|
|
|
|
return 0;
|
2001-02-08 20:59:58 +00:00
|
|
|
case SIGSUBPKT_KEY_FLAGS:
|
|
|
|
return 0;
|
1998-12-14 20:22:42 +00:00
|
|
|
case SIGSUBPKT_EXPORTABLE:
|
|
|
|
if( !n )
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
case SIGSUBPKT_ISSUER:/* issuer key ID */
|
|
|
|
if( n < 8 )
|
|
|
|
break;
|
|
|
|
return 0;
|
1999-05-25 17:56:15 +00:00
|
|
|
case SIGSUBPKT_NOTATION:
|
|
|
|
if( n < 8 ) /* minimum length needed */
|
|
|
|
break;
|
|
|
|
return 0;
|
2000-02-17 12:41:58 +00:00
|
|
|
case SIGSUBPKT_REVOC_REASON:
|
|
|
|
if( !n )
|
|
|
|
break;
|
|
|
|
return 0;
|
1998-12-14 20:22:42 +00:00
|
|
|
case SIGSUBPKT_PREF_SYM:
|
|
|
|
case SIGSUBPKT_PREF_HASH:
|
|
|
|
case SIGSUBPKT_PREF_COMPR:
|
1999-05-26 12:41:46 +00:00
|
|
|
case SIGSUBPKT_POLICY:
|
2001-08-30 16:39:23 +00:00
|
|
|
case SIGSUBPKT_FEATURES:
|
1998-12-14 20:22:42 +00:00
|
|
|
return 0;
|
2001-02-08 20:59:58 +00:00
|
|
|
case SIGSUBPKT_PRIMARY_UID:
|
|
|
|
if ( n != 1 )
|
|
|
|
break;
|
|
|
|
return 0;
|
2001-03-05 13:59:16 +00:00
|
|
|
case SIGSUBPKT_PRIV_VERIFY_CACHE:
|
2001-09-25 15:20:59 +00:00
|
|
|
/* We used this in gpg 1.0.5 and 1.0.6 to cache signature
|
|
|
|
* verification results - it is no longer used.
|
|
|
|
* "GPG" 0x00 <mode> <stat>
|
2001-03-05 13:59:16 +00:00
|
|
|
* where mode == 1: valid data, stat == 0: invalid signature
|
|
|
|
* stat == 1: valid signature
|
|
|
|
* (because we use private data, we check our marker) */
|
|
|
|
if( n < 6 )
|
1998-12-14 20:22:42 +00:00
|
|
|
break;
|
2001-03-05 13:59:16 +00:00
|
|
|
if( buffer[0] != 'G' || buffer[1] != 'P'
|
|
|
|
|| buffer[2] != 'G' || buffer[3] )
|
1998-12-14 20:22:42 +00:00
|
|
|
return -2;
|
2001-03-05 13:59:16 +00:00
|
|
|
return 4;
|
1998-12-14 20:22:42 +00:00
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
1999-05-26 12:41:46 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
can_handle_critical( const byte *buffer, size_t n, int type )
|
|
|
|
{
|
|
|
|
switch( type ) {
|
|
|
|
case SIGSUBPKT_NOTATION:
|
|
|
|
if( n >= 8 && (*buffer & 0x80) )
|
|
|
|
return 1; /* human readable is handled */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIGSUBPKT_SIG_CREATED:
|
|
|
|
case SIGSUBPKT_SIG_EXPIRE:
|
|
|
|
case SIGSUBPKT_KEY_EXPIRE:
|
|
|
|
case SIGSUBPKT_EXPORTABLE:
|
|
|
|
case SIGSUBPKT_ISSUER:/* issuer key ID */
|
|
|
|
case SIGSUBPKT_PREF_SYM:
|
|
|
|
case SIGSUBPKT_PREF_HASH:
|
|
|
|
case SIGSUBPKT_PREF_COMPR:
|
2001-02-08 20:59:58 +00:00
|
|
|
case SIGSUBPKT_KEY_FLAGS:
|
|
|
|
case SIGSUBPKT_PRIMARY_UID:
|
2001-08-30 16:39:23 +00:00
|
|
|
case SIGSUBPKT_FEATURES:
|
1999-05-26 12:41:46 +00:00
|
|
|
return 1;
|
|
|
|
|
2000-02-17 12:41:58 +00:00
|
|
|
case SIGSUBPKT_POLICY: /* Is it enough to show the policy? */
|
1999-05-26 12:41:46 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-13 17:53:36 +00:00
|
|
|
const byte *
|
2001-08-09 09:34:25 +00:00
|
|
|
enum_sig_subpkt( const subpktarea_t *pktbuf, sigsubpkttype_t reqtype,
|
1999-05-26 12:41:46 +00:00
|
|
|
size_t *ret_n, int *start )
|
1998-03-09 21:44:06 +00:00
|
|
|
{
|
2001-08-09 09:34:25 +00:00
|
|
|
const byte *buffer;
|
1998-05-13 17:53:36 +00:00
|
|
|
int buflen;
|
1998-03-09 21:44:06 +00:00
|
|
|
int type;
|
|
|
|
int critical;
|
1998-12-14 20:22:42 +00:00
|
|
|
int offset;
|
1998-03-09 21:44:06 +00:00
|
|
|
size_t n;
|
1999-05-26 12:41:46 +00:00
|
|
|
int seq = 0;
|
|
|
|
int reqseq = start? *start: 0;
|
1998-03-09 21:44:06 +00:00
|
|
|
|
2001-08-09 09:34:25 +00:00
|
|
|
if( !pktbuf || reqseq == -1 ) {
|
2000-07-10 14:06:27 +00:00
|
|
|
/* return some value different from NULL to indicate that
|
2001-08-09 09:34:25 +00:00
|
|
|
* there is no critical bit we do not understand. The caller
|
2000-07-10 14:06:27 +00:00
|
|
|
* will never use the value. Yes I know, it is an ugly hack */
|
2001-08-09 09:34:25 +00:00
|
|
|
return reqtype == SIGSUBPKT_TEST_CRITICAL? (const byte*)&pktbuf : NULL;
|
2000-07-10 14:06:27 +00:00
|
|
|
}
|
2001-08-09 09:34:25 +00:00
|
|
|
buffer = pktbuf->data;
|
|
|
|
buflen = pktbuf->len;
|
1998-12-14 20:22:42 +00:00
|
|
|
while( buflen ) {
|
1998-03-09 21:44:06 +00:00
|
|
|
n = *buffer++; buflen--;
|
2001-08-09 09:34:25 +00:00
|
|
|
if( n == 255 ) { /* 4 byte length header */
|
1998-05-13 17:53:36 +00:00
|
|
|
if( buflen < 4 )
|
|
|
|
goto too_short;
|
|
|
|
n = (buffer[0] << 24) | (buffer[1] << 16)
|
2001-08-09 09:34:25 +00:00
|
|
|
| (buffer[2] << 8) | buffer[3];
|
1998-05-13 17:53:36 +00:00
|
|
|
buffer += 4;
|
|
|
|
buflen -= 4;
|
|
|
|
}
|
2001-08-09 09:34:25 +00:00
|
|
|
else if( n >= 192 ) { /* 2 byte special encoded length header */
|
1998-03-09 21:44:06 +00:00
|
|
|
if( buflen < 2 )
|
|
|
|
goto too_short;
|
|
|
|
n = (( n - 192 ) << 8) + *buffer + 192;
|
1999-05-17 20:03:24 +00:00
|
|
|
buffer++;
|
1998-03-09 21:44:06 +00:00
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
if( buflen < n )
|
|
|
|
goto too_short;
|
|
|
|
type = *buffer;
|
|
|
|
if( type & 0x80 ) {
|
|
|
|
type &= 0x7f;
|
|
|
|
critical = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
critical = 0;
|
1999-05-26 12:41:46 +00:00
|
|
|
if( !(++seq > reqseq) )
|
|
|
|
;
|
|
|
|
else if( reqtype == SIGSUBPKT_TEST_CRITICAL ) {
|
1998-12-14 20:22:42 +00:00
|
|
|
if( critical ) {
|
|
|
|
if( n-1 > buflen+1 )
|
|
|
|
goto too_short;
|
1999-05-26 12:41:46 +00:00
|
|
|
if( !can_handle_critical(buffer+1, n-1, type ) ) {
|
1998-12-14 20:22:42 +00:00
|
|
|
log_info(_("subpacket of type %d has critical bit set\n"),
|
|
|
|
type);
|
1999-05-26 12:41:46 +00:00
|
|
|
if( start )
|
|
|
|
*start = seq;
|
1998-12-14 20:22:42 +00:00
|
|
|
return NULL; /* this is an error */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( reqtype < 0 ) /* list packets */
|
1998-10-18 15:21:22 +00:00
|
|
|
dump_sig_subpkt( reqtype == SIGSUBPKT_LIST_HASHED,
|
|
|
|
type, critical, buffer, buflen, n );
|
1998-12-14 20:22:42 +00:00
|
|
|
else if( type == reqtype ) { /* found */
|
|
|
|
buffer++;
|
|
|
|
n--;
|
|
|
|
if( n > buflen )
|
|
|
|
goto too_short;
|
|
|
|
if( ret_n )
|
|
|
|
*ret_n = n;
|
|
|
|
offset = parse_one_sig_subpkt(buffer, n, type );
|
|
|
|
switch( offset ) {
|
|
|
|
case -3:
|
|
|
|
log_error("subpacket of type %d too short\n", type);
|
|
|
|
return NULL;
|
|
|
|
case -2:
|
|
|
|
return NULL;
|
|
|
|
case -1:
|
|
|
|
BUG(); /* not yet needed */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1999-05-26 12:41:46 +00:00
|
|
|
if( start )
|
|
|
|
*start = seq;
|
1998-12-14 20:22:42 +00:00
|
|
|
return buffer+offset;
|
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
buffer += n; buflen -=n;
|
|
|
|
}
|
1998-12-14 20:22:42 +00:00
|
|
|
if( reqtype == SIGSUBPKT_TEST_CRITICAL )
|
1999-01-09 17:59:58 +00:00
|
|
|
return buffer; /* as value true to indicate that there is no */
|
1998-12-14 20:22:42 +00:00
|
|
|
/* critical bit we don't understand */
|
1999-05-26 12:41:46 +00:00
|
|
|
if( start )
|
|
|
|
*start = -1;
|
1998-12-14 20:22:42 +00:00
|
|
|
return NULL; /* end of packets; not found */
|
1998-03-09 21:44:06 +00:00
|
|
|
|
|
|
|
too_short:
|
|
|
|
log_error("buffer shorter than subpacket\n");
|
1999-05-26 12:41:46 +00:00
|
|
|
if( start )
|
|
|
|
*start = -1;
|
1998-03-09 21:44:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-26 12:41:46 +00:00
|
|
|
const byte *
|
2001-08-09 09:34:25 +00:00
|
|
|
parse_sig_subpkt (const subpktarea_t *buffer, sigsubpkttype_t reqtype,
|
|
|
|
size_t *ret_n)
|
1999-05-26 12:41:46 +00:00
|
|
|
{
|
|
|
|
return enum_sig_subpkt( buffer, reqtype, ret_n, NULL );
|
|
|
|
}
|
|
|
|
|
1998-08-05 16:51:59 +00:00
|
|
|
const byte *
|
2001-08-09 09:34:25 +00:00
|
|
|
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype,
|
|
|
|
size_t *ret_n )
|
1998-08-05 16:51:59 +00:00
|
|
|
{
|
|
|
|
const byte *p;
|
|
|
|
|
2001-08-09 09:34:25 +00:00
|
|
|
p = parse_sig_subpkt (sig->hashed, reqtype, ret_n );
|
1998-08-05 16:51:59 +00:00
|
|
|
if( !p )
|
2001-08-09 09:34:25 +00:00
|
|
|
p = parse_sig_subpkt (sig->unhashed, reqtype, ret_n );
|
1998-08-05 16:51:59 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
static int
|
|
|
|
parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PKT_signature *sig )
|
|
|
|
{
|
1998-03-09 21:44:06 +00:00
|
|
|
int md5_len=0;
|
1997-11-18 14:06:00 +00:00
|
|
|
unsigned n;
|
1998-03-09 21:44:06 +00:00
|
|
|
int is_v4=0;
|
|
|
|
int rc=0;
|
1998-06-13 06:59:14 +00:00
|
|
|
int i, ndata;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
if( pktlen < 16 ) {
|
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
sig->version = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( sig->version == 4 )
|
|
|
|
is_v4=1;
|
|
|
|
else if( sig->version != 2 && sig->version != 3 ) {
|
|
|
|
log_error("packet(%d) with unknown version %d\n", pkttype, sig->version);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
|
|
|
|
if( !is_v4 ) {
|
|
|
|
md5_len = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
sig->sig_class = iobuf_get_noeof(inp); pktlen--;
|
1998-03-09 21:44:06 +00:00
|
|
|
if( !is_v4 ) {
|
|
|
|
sig->timestamp = read_32(inp); pktlen -= 4;
|
|
|
|
sig->keyid[0] = read_32(inp); pktlen -= 4;
|
|
|
|
sig->keyid[1] = read_32(inp); pktlen -= 4;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
sig->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
|
1998-03-09 21:44:06 +00:00
|
|
|
sig->digest_algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( is_v4 ) { /* read subpackets */
|
|
|
|
n = read_16(inp); pktlen -= 2; /* length of hashed data */
|
|
|
|
if( n > 10000 ) {
|
|
|
|
log_error("signature packet: hashed data too long\n");
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( n ) {
|
2001-08-09 09:34:25 +00:00
|
|
|
sig->hashed = m_alloc (sizeof (*sig->hashed) + n - 1 );
|
|
|
|
sig->hashed->size = n;
|
|
|
|
sig->hashed->len = n;
|
|
|
|
if( iobuf_read (inp, sig->hashed->data, n ) != n ) {
|
|
|
|
log_error ("premature eof while reading "
|
|
|
|
"hashed signature data\n");
|
1998-03-09 21:44:06 +00:00
|
|
|
rc = -1;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
pktlen -= n;
|
|
|
|
}
|
|
|
|
n = read_16(inp); pktlen -= 2; /* length of unhashed data */
|
|
|
|
if( n > 10000 ) {
|
|
|
|
log_error("signature packet: unhashed data too long\n");
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( n ) {
|
2001-08-09 15:49:28 +00:00
|
|
|
/* we add 8 extra bytes so that we have space for the signature
|
2001-08-09 09:34:25 +00:00
|
|
|
* status cache. Well we are wastin this if there is a cache
|
|
|
|
* packet already, but in the other case it avoids an realloc */
|
|
|
|
sig->unhashed = m_alloc (sizeof(*sig->unhashed) + n + 8 - 1 );
|
|
|
|
sig->unhashed->size = n + 8;
|
|
|
|
sig->unhashed->len = n;
|
|
|
|
if( iobuf_read(inp, sig->unhashed->data, n ) != n ) {
|
|
|
|
log_error("premature eof while reading "
|
|
|
|
"unhashed signature data\n");
|
1998-03-09 21:44:06 +00:00
|
|
|
rc = -1;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
pktlen -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pktlen < 5 ) { /* sanity check */
|
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig->digest_start[0] = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
sig->digest_start[1] = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
|
1999-04-26 15:53:01 +00:00
|
|
|
if( is_v4 && sig->pubkey_algo ) { /*extract required information */
|
1998-03-09 21:44:06 +00:00
|
|
|
const byte *p;
|
1998-10-01 07:23:00 +00:00
|
|
|
|
1998-12-14 20:22:42 +00:00
|
|
|
/* set sig->flags.unknown_critical if there is a
|
|
|
|
* critical bit set for packets which we do not understand */
|
2001-08-09 09:34:25 +00:00
|
|
|
if( !parse_sig_subpkt (sig->hashed, SIGSUBPKT_TEST_CRITICAL, NULL)
|
|
|
|
|| !parse_sig_subpkt (sig->unhashed, SIGSUBPKT_TEST_CRITICAL,
|
1998-12-14 20:22:42 +00:00
|
|
|
NULL) )
|
|
|
|
{
|
|
|
|
sig->flags.unknown_critical = 1;
|
|
|
|
}
|
|
|
|
|
2001-08-09 09:34:25 +00:00
|
|
|
p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_SIG_CREATED, NULL );
|
1998-03-09 21:44:06 +00:00
|
|
|
if( !p )
|
|
|
|
log_error("signature packet without timestamp\n");
|
|
|
|
else
|
|
|
|
sig->timestamp = buffer_to_u32(p);
|
1998-08-05 16:51:59 +00:00
|
|
|
p = parse_sig_subpkt2( sig, SIGSUBPKT_ISSUER, NULL );
|
1998-03-09 21:44:06 +00:00
|
|
|
if( !p )
|
|
|
|
log_error("signature packet without keyid\n");
|
|
|
|
else {
|
|
|
|
sig->keyid[0] = buffer_to_u32(p);
|
|
|
|
sig->keyid[1] = buffer_to_u32(p+4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( list_mode ) {
|
1998-05-04 18:49:26 +00:00
|
|
|
printf(":signature packet: algo %d, keyid %08lX%08lX\n"
|
1998-03-09 21:44:06 +00:00
|
|
|
"\tversion %d, created %lu, md5len %d, sigclass %02x\n"
|
|
|
|
"\tdigest algo %d, begin of digest %02x %02x\n",
|
1998-05-04 18:49:26 +00:00
|
|
|
sig->pubkey_algo,
|
1998-02-03 12:09:20 +00:00
|
|
|
(ulong)sig->keyid[0], (ulong)sig->keyid[1],
|
1998-03-09 21:44:06 +00:00
|
|
|
sig->version, (ulong)sig->timestamp, md5_len, sig->sig_class,
|
|
|
|
sig->digest_algo,
|
|
|
|
sig->digest_start[0], sig->digest_start[1] );
|
|
|
|
if( is_v4 ) {
|
2001-08-09 09:34:25 +00:00
|
|
|
parse_sig_subpkt (sig->hashed, SIGSUBPKT_LIST_HASHED, NULL );
|
|
|
|
parse_sig_subpkt (sig->unhashed, SIGSUBPKT_LIST_UNHASHED, NULL);
|
1997-11-24 22:24:04 +00:00
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
1998-06-13 06:59:14 +00:00
|
|
|
|
1999-05-08 17:35:16 +00:00
|
|
|
ndata = pubkey_get_nsig(sig->pubkey_algo);
|
|
|
|
if( !ndata ) {
|
|
|
|
if( list_mode )
|
|
|
|
printf("\tunknown algorithm %d\n", sig->pubkey_algo );
|
|
|
|
unknown_pubkey_warning( sig->pubkey_algo );
|
|
|
|
/* we store the plain material in data[0], so that we are able
|
|
|
|
* to write it back with build_packet() */
|
|
|
|
sig->data[0] = mpi_set_opaque(NULL, read_rest(inp, pktlen), pktlen );
|
|
|
|
pktlen = 0;
|
1998-07-02 19:31:46 +00:00
|
|
|
}
|
|
|
|
else {
|
1999-05-08 17:35:16 +00:00
|
|
|
for( i=0; i < ndata; i++ ) {
|
|
|
|
n = pktlen;
|
|
|
|
sig->data[i] = mpi_read(inp, &n, 0 );
|
|
|
|
pktlen -=n;
|
|
|
|
if( list_mode ) {
|
|
|
|
printf("\tdata: ");
|
|
|
|
mpi_print(stdout, sig->data[i], mpi_print_mode );
|
|
|
|
putchar('\n');
|
1998-07-02 19:31:46 +00:00
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (!sig->data[i])
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
skip_rest(inp, pktlen);
|
1998-03-09 21:44:06 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-02 19:36:53 +00:00
|
|
|
static int
|
|
|
|
parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PKT_onepass_sig *ops )
|
|
|
|
{
|
|
|
|
int version;
|
2001-03-12 20:21:50 +00:00
|
|
|
int rc = 0;
|
1997-12-02 19:36:53 +00:00
|
|
|
|
|
|
|
if( pktlen < 13 ) {
|
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-12-02 19:36:53 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
version = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( version != 3 ) {
|
|
|
|
log_error("onepass_sig with unknown version %d\n", version);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-12-02 19:36:53 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
ops->sig_class = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
ops->digest_algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
ops->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
ops->keyid[0] = read_32(inp); pktlen -= 4;
|
|
|
|
ops->keyid[1] = read_32(inp); pktlen -= 4;
|
|
|
|
ops->last = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( list_mode )
|
|
|
|
printf(":onepass_sig packet: keyid %08lX%08lX\n"
|
|
|
|
"\tversion %d, sigclass %02x, digest %d, pubkey %d, last=%d\n",
|
1998-02-03 12:09:20 +00:00
|
|
|
(ulong)ops->keyid[0], (ulong)ops->keyid[1],
|
1997-12-02 19:36:53 +00:00
|
|
|
version, ops->sig_class,
|
|
|
|
ops->digest_algo, ops->pubkey_algo, ops->last );
|
|
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
skip_rest(inp, pktlen);
|
2001-03-12 20:21:50 +00:00
|
|
|
return rc;
|
1997-12-02 19:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
1998-06-29 12:30:57 +00:00
|
|
|
parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
|
1997-11-18 14:06:00 +00:00
|
|
|
byte *hdr, int hdrlen, PACKET *pkt )
|
|
|
|
{
|
|
|
|
int i, version, algorithm;
|
|
|
|
unsigned n;
|
1998-10-16 16:00:17 +00:00
|
|
|
unsigned long timestamp, expiredate;
|
1998-06-29 12:30:57 +00:00
|
|
|
int npkey, nskey;
|
1997-11-24 22:24:04 +00:00
|
|
|
int is_v4=0;
|
1998-02-24 18:50:46 +00:00
|
|
|
int rc=0;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
version = iobuf_get_noeof(inp); pktlen--;
|
1998-06-29 12:30:57 +00:00
|
|
|
if( pkttype == PKT_PUBLIC_SUBKEY && version == '#' ) {
|
1998-05-03 19:35:33 +00:00
|
|
|
/* early versions of G10 use old PGP comments packets;
|
|
|
|
* luckily all those comments are started by a hash */
|
1998-04-02 10:30:03 +00:00
|
|
|
if( list_mode ) {
|
1998-05-03 19:35:33 +00:00
|
|
|
printf(":rfc1991 comment packet: \"" );
|
1998-04-02 10:30:03 +00:00
|
|
|
for( ; pktlen; pktlen-- ) {
|
|
|
|
int c;
|
|
|
|
c = iobuf_get_noeof(inp);
|
|
|
|
if( c >= ' ' && c <= 'z' )
|
|
|
|
putchar(c);
|
|
|
|
else
|
|
|
|
printf("\\x%02x", c );
|
|
|
|
}
|
|
|
|
printf("\"\n");
|
|
|
|
}
|
|
|
|
skip_rest(inp, pktlen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if( version == 4 )
|
1997-11-24 22:24:04 +00:00
|
|
|
is_v4=1;
|
|
|
|
else if( version != 2 && version != 3 ) {
|
1997-11-18 14:06:00 +00:00
|
|
|
log_error("packet(%d) with unknown version %d\n", pkttype, version);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1998-04-02 10:30:03 +00:00
|
|
|
if( pktlen < 11 ) {
|
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-04-02 10:30:03 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
timestamp = read_32(inp); pktlen -= 4;
|
1997-11-24 22:24:04 +00:00
|
|
|
if( is_v4 )
|
1998-10-16 16:00:17 +00:00
|
|
|
expiredate = 0; /* have to get it from the selfsignature */
|
1998-01-16 21:15:24 +00:00
|
|
|
else {
|
1998-10-16 16:00:17 +00:00
|
|
|
unsigned short ndays;
|
|
|
|
ndays = read_16(inp); pktlen -= 2;
|
|
|
|
if( ndays )
|
|
|
|
expiredate = timestamp + ndays * 86400L;
|
|
|
|
else
|
|
|
|
expiredate = 0;
|
1998-01-16 21:15:24 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
algorithm = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( list_mode )
|
1998-01-16 21:15:24 +00:00
|
|
|
printf(":%s key packet:\n"
|
1998-10-16 16:00:17 +00:00
|
|
|
"\tversion %d, algo %d, created %lu, expires %lu\n",
|
1998-06-29 12:30:57 +00:00
|
|
|
pkttype == PKT_PUBLIC_KEY? "public" :
|
|
|
|
pkttype == PKT_SECRET_KEY? "secret" :
|
|
|
|
pkttype == PKT_PUBLIC_SUBKEY? "public sub" :
|
|
|
|
pkttype == PKT_SECRET_SUBKEY? "secret sub" : "??",
|
1998-10-16 16:00:17 +00:00
|
|
|
version, algorithm, timestamp, expiredate );
|
1998-06-29 12:30:57 +00:00
|
|
|
|
|
|
|
if( pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY ) {
|
|
|
|
PKT_secret_key *sk = pkt->pkt.secret_key;
|
|
|
|
|
|
|
|
sk->timestamp = timestamp;
|
1998-10-16 16:00:17 +00:00
|
|
|
sk->expiredate = expiredate;
|
1998-06-29 12:30:57 +00:00
|
|
|
sk->hdrbytes = hdrlen;
|
|
|
|
sk->version = version;
|
1999-01-07 17:05:48 +00:00
|
|
|
sk->is_primary = pkttype == PKT_SECRET_KEY;
|
1998-06-29 12:30:57 +00:00
|
|
|
sk->pubkey_algo = algorithm;
|
2001-02-08 16:28:28 +00:00
|
|
|
sk->req_usage = 0;
|
1998-07-06 10:23:57 +00:00
|
|
|
sk->pubkey_usage = 0; /* not yet used */
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
else {
|
1998-06-29 12:30:57 +00:00
|
|
|
PKT_public_key *pk = pkt->pkt.public_key;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-06-29 12:30:57 +00:00
|
|
|
pk->timestamp = timestamp;
|
1998-10-16 16:00:17 +00:00
|
|
|
pk->expiredate = expiredate;
|
1998-06-29 12:30:57 +00:00
|
|
|
pk->hdrbytes = hdrlen;
|
|
|
|
pk->version = version;
|
|
|
|
pk->pubkey_algo = algorithm;
|
2001-02-08 16:28:28 +00:00
|
|
|
pk->req_usage = 0;
|
1998-07-06 10:23:57 +00:00
|
|
|
pk->pubkey_usage = 0; /* not yet used */
|
2001-02-08 16:28:28 +00:00
|
|
|
pk->is_revoked = 0;
|
1998-07-09 13:37:17 +00:00
|
|
|
pk->keyid[0] = 0;
|
|
|
|
pk->keyid[1] = 0;
|
1998-06-29 12:30:57 +00:00
|
|
|
}
|
|
|
|
nskey = pubkey_get_nskey( algorithm );
|
|
|
|
npkey = pubkey_get_npkey( algorithm );
|
|
|
|
if( !npkey ) {
|
|
|
|
if( list_mode )
|
|
|
|
printf("\tunknown algorithm %d\n", algorithm );
|
1998-07-02 19:31:46 +00:00
|
|
|
unknown_pubkey_warning( algorithm );
|
1998-06-29 12:30:57 +00:00
|
|
|
}
|
1998-02-24 18:50:46 +00:00
|
|
|
|
|
|
|
|
1998-08-05 16:51:59 +00:00
|
|
|
if( pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
PKT_secret_key *sk = pkt->pkt.secret_key;
|
1999-06-26 10:23:06 +00:00
|
|
|
byte temp[16];
|
1998-02-24 18:50:46 +00:00
|
|
|
|
1998-07-02 19:31:46 +00:00
|
|
|
if( !npkey ) {
|
1999-05-08 17:35:16 +00:00
|
|
|
sk->skey[0] = mpi_set_opaque( NULL,
|
|
|
|
read_rest(inp, pktlen), pktlen );
|
|
|
|
pktlen = 0;
|
1998-07-02 19:31:46 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1998-06-29 12:30:57 +00:00
|
|
|
for(i=0; i < npkey; i++ ) {
|
|
|
|
n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
|
|
|
|
if( list_mode ) {
|
|
|
|
printf( "\tskey[%d]: ", i);
|
|
|
|
mpi_print(stdout, sk->skey[i], mpi_print_mode );
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (!sk->skey[i])
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-06-29 12:30:57 +00:00
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (rc) /* one of the MPIs were bad */
|
|
|
|
goto leave;
|
1998-06-29 12:30:57 +00:00
|
|
|
sk->protect.algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
if( sk->protect.algo ) {
|
|
|
|
sk->is_protected = 1;
|
|
|
|
sk->protect.s2k.count = 0;
|
|
|
|
if( sk->protect.algo == 255 ) {
|
|
|
|
if( pktlen < 3 ) {
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
1998-02-24 18:50:46 +00:00
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
sk->protect.algo = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
sk->protect.s2k.mode = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
sk->protect.s2k.hash_algo = iobuf_get_noeof(inp); pktlen--;
|
2000-01-14 17:26:00 +00:00
|
|
|
/* check for the special GNU extension */
|
|
|
|
if( is_v4 && sk->protect.s2k.mode == 101 ) {
|
|
|
|
for(i=0; i < 4 && pktlen; i++, pktlen-- )
|
|
|
|
temp[i] = iobuf_get_noeof(inp);
|
|
|
|
if( i < 4 || memcmp( temp, "GNU", 3 ) ) {
|
|
|
|
if( list_mode )
|
|
|
|
printf( "\tunknown S2K %d\n",
|
|
|
|
sk->protect.s2k.mode );
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
/* here we know that it is a gnu extension
|
|
|
|
* What follows is the GNU protection mode:
|
|
|
|
* All values have special meanings
|
|
|
|
* and they are mapped in the mode with a base of 1000.
|
|
|
|
*/
|
|
|
|
sk->protect.s2k.mode = 1000 + temp[3];
|
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
switch( sk->protect.s2k.mode ) {
|
|
|
|
case 1:
|
1998-08-05 16:51:59 +00:00
|
|
|
case 3:
|
1998-06-29 12:30:57 +00:00
|
|
|
for(i=0; i < 8 && pktlen; i++, pktlen-- )
|
|
|
|
temp[i] = iobuf_get_noeof(inp);
|
|
|
|
memcpy(sk->protect.s2k.salt, temp, 8 );
|
|
|
|
break;
|
1998-02-24 18:50:46 +00:00
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
switch( sk->protect.s2k.mode ) {
|
|
|
|
case 0: if( list_mode ) printf( "\tsimple S2K" );
|
|
|
|
break;
|
|
|
|
case 1: if( list_mode ) printf( "\tsalted S2K" );
|
|
|
|
break;
|
1998-08-05 16:51:59 +00:00
|
|
|
case 3: if( list_mode ) printf( "\titer+salt S2K" );
|
1998-06-29 12:30:57 +00:00
|
|
|
break;
|
2000-01-14 17:26:00 +00:00
|
|
|
case 1001: if( list_mode ) printf( "\tgnu-dummy S2K" );
|
|
|
|
break;
|
1998-06-29 12:30:57 +00:00
|
|
|
default:
|
|
|
|
if( list_mode )
|
2000-01-14 17:26:00 +00:00
|
|
|
printf( "\tunknown %sS2K %d\n",
|
|
|
|
sk->protect.s2k.mode < 1000? "":"GNU ",
|
|
|
|
sk->protect.s2k.mode );
|
1998-02-24 18:50:46 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
|
1997-11-24 22:24:04 +00:00
|
|
|
if( list_mode ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
printf(", algo: %d, hash: %d",
|
|
|
|
sk->protect.algo,
|
|
|
|
sk->protect.s2k.hash_algo );
|
|
|
|
if( sk->protect.s2k.mode == 1
|
1998-08-05 16:51:59 +00:00
|
|
|
|| sk->protect.s2k.mode == 3 ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
printf(", salt: ");
|
|
|
|
for(i=0; i < 8; i++ )
|
|
|
|
printf("%02x", sk->protect.s2k.salt[i]);
|
|
|
|
}
|
1997-11-24 22:24:04 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
|
1998-08-05 16:51:59 +00:00
|
|
|
if( sk->protect.s2k.mode == 3 ) {
|
|
|
|
if( pktlen < 1 ) {
|
1998-03-09 21:44:06 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-08-05 16:51:59 +00:00
|
|
|
sk->protect.s2k.count = iobuf_get(inp);
|
|
|
|
pktlen--;
|
1998-09-28 19:25:31 +00:00
|
|
|
if( list_mode )
|
|
|
|
printf("\tprotect count: %lu\n",
|
|
|
|
(ulong)sk->protect.s2k.count);
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
}
|
|
|
|
else { /* old version; no S2K, so we set mode to 0, hash MD5 */
|
|
|
|
sk->protect.s2k.mode = 0;
|
1999-04-09 10:34:44 +00:00
|
|
|
sk->protect.s2k.hash_algo = DIGEST_ALGO_MD5;
|
1998-06-29 12:30:57 +00:00
|
|
|
if( list_mode )
|
|
|
|
printf( "\tprotect algo: %d (hash algo: %d)\n",
|
|
|
|
sk->protect.algo, sk->protect.s2k.hash_algo );
|
|
|
|
}
|
1999-04-18 08:18:52 +00:00
|
|
|
/* It is really ugly that we don't know the size
|
|
|
|
* of the IV here in cases we are not aware of the algorithm.
|
|
|
|
* so a
|
|
|
|
* sk->protect.ivlen = cipher_get_blocksize(sk->protect.algo);
|
|
|
|
* won't work. The only solution I see is to hardwire it here.
|
1999-06-26 10:23:06 +00:00
|
|
|
* NOTE: if you change the ivlen above 16, don't forget to
|
|
|
|
* enlarge temp.
|
1999-04-18 08:18:52 +00:00
|
|
|
*/
|
|
|
|
switch( sk->protect.algo ) {
|
|
|
|
case 7: case 8: case 9: /* reserved for AES */
|
|
|
|
case 10: /* Twofish */
|
|
|
|
sk->protect.ivlen = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sk->protect.ivlen = 8;
|
|
|
|
}
|
2000-01-14 17:26:00 +00:00
|
|
|
if( sk->protect.s2k.mode == 1001 )
|
|
|
|
sk->protect.ivlen = 0;
|
|
|
|
|
1999-04-18 08:18:52 +00:00
|
|
|
if( pktlen < sk->protect.ivlen ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
|
|
|
goto leave;
|
|
|
|
}
|
1999-04-18 08:18:52 +00:00
|
|
|
for(i=0; i < sk->protect.ivlen && pktlen; i++, pktlen-- )
|
1998-06-29 12:30:57 +00:00
|
|
|
temp[i] = iobuf_get_noeof(inp);
|
|
|
|
if( list_mode ) {
|
|
|
|
printf( "\tprotect IV: ");
|
1999-04-18 08:18:52 +00:00
|
|
|
for(i=0; i < sk->protect.ivlen; i++ )
|
1998-06-29 12:30:57 +00:00
|
|
|
printf(" %02x", temp[i] );
|
|
|
|
putchar('\n');
|
|
|
|
}
|
1999-04-18 08:18:52 +00:00
|
|
|
memcpy(sk->protect.iv, temp, sk->protect.ivlen );
|
1998-06-29 12:30:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
sk->is_protected = 0;
|
|
|
|
/* It does not make sense to read it into secure memory.
|
|
|
|
* If the user is so careless, not to protect his secret key,
|
|
|
|
* we can assume, that he operates an open system :=(.
|
|
|
|
* So we put the key into secure memory when we unprotect it. */
|
2000-01-14 17:26:00 +00:00
|
|
|
if( sk->protect.s2k.mode == 1001 ) {
|
|
|
|
/* better set some dummy stuff here */
|
|
|
|
sk->skey[npkey] = mpi_set_opaque(NULL, m_strdup("dummydata"), 10);
|
|
|
|
pktlen = 0;
|
|
|
|
}
|
|
|
|
else if( is_v4 && sk->is_protected ) {
|
1998-08-08 19:27:00 +00:00
|
|
|
/* ugly; the length is encrypted too, so we read all
|
1998-08-05 16:51:59 +00:00
|
|
|
* stuff up to the end of the packet into the first
|
|
|
|
* skey element */
|
|
|
|
sk->skey[npkey] = mpi_set_opaque(NULL,
|
1999-05-08 17:35:16 +00:00
|
|
|
read_rest(inp, pktlen), pktlen );
|
1998-08-05 16:51:59 +00:00
|
|
|
pktlen = 0;
|
1998-06-29 12:30:57 +00:00
|
|
|
if( list_mode ) {
|
1998-08-05 16:51:59 +00:00
|
|
|
printf("\tencrypted stuff follows\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* v3 method: the mpi length is not encrypted */
|
|
|
|
for(i=npkey; i < nskey; i++ ) {
|
|
|
|
n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
|
2001-08-31 12:51:41 +00:00
|
|
|
if( sk->is_protected && sk->skey[i] )
|
1998-08-05 16:51:59 +00:00
|
|
|
mpi_set_protect_flag(sk->skey[i]);
|
|
|
|
if( list_mode ) {
|
|
|
|
printf( "\tskey[%d]: ", i);
|
|
|
|
if( sk->is_protected )
|
|
|
|
printf( "[encrypted]\n");
|
|
|
|
else {
|
|
|
|
mpi_print(stdout, sk->skey[i], mpi_print_mode );
|
|
|
|
putchar('\n');
|
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (!sk->skey[i])
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (rc)
|
|
|
|
goto leave;
|
1997-11-24 22:24:04 +00:00
|
|
|
|
1998-08-05 16:51:59 +00:00
|
|
|
sk->csum = read_16(inp); pktlen -= 2;
|
|
|
|
if( list_mode ) {
|
|
|
|
printf("\tchecksum: %04hx\n", sk->csum);
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1998-06-29 12:30:57 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PKT_public_key *pk = pkt->pkt.public_key;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-07-02 19:31:46 +00:00
|
|
|
if( !npkey ) {
|
1999-05-08 17:35:16 +00:00
|
|
|
pk->pkey[0] = mpi_set_opaque( NULL,
|
|
|
|
read_rest(inp, pktlen), pktlen );
|
|
|
|
pktlen = 0;
|
1998-07-02 19:31:46 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1998-06-29 12:30:57 +00:00
|
|
|
for(i=0; i < npkey; i++ ) {
|
|
|
|
n = pktlen; pk->pkey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
|
1997-11-18 14:06:00 +00:00
|
|
|
if( list_mode ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
printf( "\tpkey[%d]: ", i);
|
|
|
|
mpi_print(stdout, pk->pkey[i], mpi_print_mode );
|
|
|
|
putchar('\n');
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (!pk->pkey[i])
|
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2001-08-31 12:51:41 +00:00
|
|
|
if (rc)
|
|
|
|
goto leave;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
skip_rest(inp, pktlen);
|
1998-02-24 18:50:46 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
|
|
|
|
{
|
|
|
|
byte *p;
|
|
|
|
|
1999-01-24 17:16:40 +00:00
|
|
|
packet->pkt.user_id = m_alloc(sizeof *packet->pkt.user_id + pktlen);
|
2001-08-10 14:04:32 +00:00
|
|
|
packet->pkt.user_id->ref = 1;
|
1997-11-18 14:06:00 +00:00
|
|
|
packet->pkt.user_id->len = pktlen;
|
1999-11-12 19:32:00 +00:00
|
|
|
packet->pkt.user_id->photo = NULL;
|
|
|
|
packet->pkt.user_id->photolen = 0;
|
2001-03-12 20:21:50 +00:00
|
|
|
packet->pkt.user_id->is_primary = 0;
|
|
|
|
packet->pkt.user_id->is_revoked = 0;
|
|
|
|
packet->pkt.user_id->created = 0;
|
|
|
|
packet->pkt.user_id->help_key_usage = 0;
|
|
|
|
packet->pkt.user_id->help_key_expire = 0;
|
2001-08-10 14:04:32 +00:00
|
|
|
packet->pkt.user_id->prefs = NULL;
|
2001-03-12 20:21:50 +00:00
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
p = packet->pkt.user_id->name;
|
1999-02-16 13:16:33 +00:00
|
|
|
for( ; pktlen; pktlen--, p++ )
|
1997-11-18 14:06:00 +00:00
|
|
|
*p = iobuf_get_noeof(inp);
|
1999-01-24 17:16:40 +00:00
|
|
|
*p = 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
if( list_mode ) {
|
|
|
|
int n = packet->pkt.user_id->len;
|
1999-09-01 13:40:07 +00:00
|
|
|
printf(":user ID packet: \"");
|
2001-03-12 20:21:50 +00:00
|
|
|
/* fixme: Hey why don't we replace this with print_string?? */
|
1997-11-18 14:06:00 +00:00
|
|
|
for(p=packet->pkt.user_id->name; n; p++, n-- ) {
|
|
|
|
if( *p >= ' ' && *p <= 'z' )
|
|
|
|
putchar(*p);
|
|
|
|
else
|
|
|
|
printf("\\x%02x", *p );
|
|
|
|
}
|
|
|
|
printf("\"\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-01-16 21:15:24 +00:00
|
|
|
|
1999-11-12 19:32:00 +00:00
|
|
|
/****************
|
|
|
|
* PGP generates a packet of type 17. We assume this is a photo ID and
|
|
|
|
* simply store it here as a comment packet.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_photo_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
|
|
|
|
{
|
|
|
|
byte *p;
|
|
|
|
|
|
|
|
packet->pkt.user_id = m_alloc(sizeof *packet->pkt.user_id + 30);
|
2001-08-10 14:04:32 +00:00
|
|
|
packet->pkt.user_id->ref = 1;
|
1999-11-12 19:32:00 +00:00
|
|
|
sprintf( packet->pkt.user_id->name, "[image of size %lu]", pktlen );
|
|
|
|
packet->pkt.user_id->len = strlen(packet->pkt.user_id->name);
|
2001-07-26 09:37:47 +00:00
|
|
|
packet->pkt.user_id->is_primary = 0;
|
|
|
|
packet->pkt.user_id->is_revoked = 0;
|
|
|
|
packet->pkt.user_id->created = 0;
|
|
|
|
packet->pkt.user_id->help_key_usage = 0;
|
|
|
|
packet->pkt.user_id->help_key_expire = 0;
|
2001-08-10 14:04:32 +00:00
|
|
|
packet->pkt.user_id->prefs = NULL;
|
1999-11-12 19:32:00 +00:00
|
|
|
|
|
|
|
packet->pkt.user_id->photo = m_alloc(sizeof *packet->pkt.user_id + pktlen);
|
|
|
|
packet->pkt.user_id->photolen = pktlen;
|
|
|
|
p = packet->pkt.user_id->photo;
|
|
|
|
for( ; pktlen; pktlen--, p++ )
|
|
|
|
*p = iobuf_get_noeof(inp);
|
|
|
|
|
|
|
|
if( list_mode ) {
|
|
|
|
printf(":photo_id packet: %s\n", packet->pkt.user_id->name );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-01-16 21:15:24 +00:00
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
static int
|
|
|
|
parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
1998-02-11 03:25:44 +00:00
|
|
|
byte *p;
|
|
|
|
|
|
|
|
packet->pkt.comment = m_alloc(sizeof *packet->pkt.comment + pktlen - 1);
|
|
|
|
packet->pkt.comment->len = pktlen;
|
|
|
|
p = packet->pkt.comment->data;
|
|
|
|
for( ; pktlen; pktlen--, p++ )
|
|
|
|
*p = iobuf_get_noeof(inp);
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
if( list_mode ) {
|
1998-02-11 03:25:44 +00:00
|
|
|
int n = packet->pkt.comment->len;
|
1998-05-03 19:35:33 +00:00
|
|
|
printf(":%scomment packet: \"", pkttype == PKT_OLD_COMMENT?
|
|
|
|
"OpenPGP draft " : "" );
|
1998-02-11 03:25:44 +00:00
|
|
|
for(p=packet->pkt.comment->data; n; p++, n-- ) {
|
|
|
|
if( *p >= ' ' && *p <= 'z' )
|
|
|
|
putchar(*p);
|
1997-11-18 14:06:00 +00:00
|
|
|
else
|
1998-02-11 03:25:44 +00:00
|
|
|
printf("\\x%02x", *p );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
printf("\"\n");
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
return 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1999-07-08 14:24:35 +00:00
|
|
|
parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = iobuf_get_noeof(inp);
|
1999-07-08 14:24:35 +00:00
|
|
|
pkt->pkt.ring_trust = m_alloc( sizeof *pkt->pkt.ring_trust );
|
|
|
|
pkt->pkt.ring_trust->trustval = c;
|
2001-09-25 15:20:59 +00:00
|
|
|
pkt->pkt.ring_trust->sigcache = 0;
|
|
|
|
if (!c && pktlen==2) {
|
|
|
|
c = iobuf_get_noeof (inp);
|
|
|
|
/* we require that bit 7 of the sigcache is 0 (easier eof handling)*/
|
|
|
|
if ( !(c & 0x80) )
|
|
|
|
pkt->pkt.ring_trust->sigcache = c;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
if( list_mode )
|
2001-09-25 15:20:59 +00:00
|
|
|
printf(":trust packet: flag=%02x sigcache=%02x\n",
|
|
|
|
pkt->pkt.ring_trust->trustval,
|
|
|
|
pkt->pkt.ring_trust->sigcache);
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
1998-07-02 19:31:46 +00:00
|
|
|
parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *pkt, int new_ctb )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
2001-03-12 20:21:50 +00:00
|
|
|
int rc = 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
int mode, namelen;
|
|
|
|
PKT_plaintext *pt;
|
|
|
|
byte *p;
|
|
|
|
int c, i;
|
|
|
|
|
|
|
|
if( pktlen && pktlen < 6 ) {
|
|
|
|
log_error("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
|
|
|
|
namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
|
|
|
|
pt = pkt->pkt.plaintext = m_alloc(sizeof *pkt->pkt.plaintext + namelen -1);
|
1998-07-02 19:31:46 +00:00
|
|
|
pt->new_ctb = new_ctb;
|
1997-11-18 14:06:00 +00:00
|
|
|
pt->mode = mode;
|
|
|
|
pt->namelen = namelen;
|
|
|
|
if( pktlen ) {
|
|
|
|
for( i=0; pktlen > 4 && i < namelen; pktlen--, i++ )
|
|
|
|
pt->name[i] = iobuf_get_noeof(inp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( i=0; i < namelen; i++ )
|
|
|
|
if( (c=iobuf_get(inp)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
pt->name[i] = c;
|
|
|
|
}
|
|
|
|
pt->timestamp = read_32(inp); if( pktlen) pktlen -= 4;
|
|
|
|
pt->len = pktlen;
|
|
|
|
pt->buf = inp;
|
|
|
|
pktlen = 0;
|
|
|
|
|
|
|
|
if( list_mode ) {
|
|
|
|
printf(":literal data packet:\n"
|
|
|
|
"\tmode %c, created %lu, name=\"",
|
|
|
|
mode >= ' ' && mode <'z'? mode : '?',
|
1998-02-03 12:09:20 +00:00
|
|
|
(ulong)pt->timestamp );
|
1997-11-18 14:06:00 +00:00
|
|
|
for(p=pt->name,i=0; i < namelen; p++, i++ ) {
|
|
|
|
if( *p >= ' ' && *p <= 'z' )
|
|
|
|
putchar(*p);
|
|
|
|
else
|
|
|
|
printf("\\x%02x", *p );
|
|
|
|
}
|
1998-02-03 12:09:20 +00:00
|
|
|
printf("\",\n\traw data: %lu bytes\n", (ulong)pt->len );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
2001-03-12 20:21:50 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
1998-07-02 19:31:46 +00:00
|
|
|
parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *pkt, int new_ctb )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
|
|
|
PKT_compressed *zd;
|
|
|
|
|
|
|
|
/* pktlen is here 0, but data follows
|
|
|
|
* (this should be the last object in a file or
|
|
|
|
* the compress algorithm should know the length)
|
|
|
|
*/
|
|
|
|
zd = pkt->pkt.compressed = m_alloc(sizeof *pkt->pkt.compressed );
|
|
|
|
zd->len = 0; /* not yet used */
|
|
|
|
zd->algorithm = iobuf_get_noeof(inp);
|
1998-07-02 19:31:46 +00:00
|
|
|
zd->new_ctb = new_ctb;
|
1997-11-18 14:06:00 +00:00
|
|
|
zd->buf = inp;
|
|
|
|
if( list_mode )
|
|
|
|
printf(":compressed packet: algo=%d\n", zd->algorithm);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
1998-07-02 19:31:46 +00:00
|
|
|
parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *pkt, int new_ctb )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
2001-03-12 20:21:50 +00:00
|
|
|
int rc = 0;
|
1997-12-01 10:33:23 +00:00
|
|
|
PKT_encrypted *ed;
|
2001-02-08 16:28:28 +00:00
|
|
|
unsigned long orig_pktlen = pktlen;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1997-12-01 10:33:23 +00:00
|
|
|
ed = pkt->pkt.encrypted = m_alloc(sizeof *pkt->pkt.encrypted );
|
1997-11-18 14:06:00 +00:00
|
|
|
ed->len = pktlen;
|
2001-04-05 12:21:43 +00:00
|
|
|
/* we don't know the extralen which is (cipher_blocksize+2)
|
|
|
|
because the algorithm ist not specified in this packet.
|
|
|
|
However, it is only important to know this for somesanity
|
|
|
|
checks on the pkacet length - it doesn't matter that we can't
|
|
|
|
do it */
|
|
|
|
ed->extralen = 0;
|
1997-11-18 14:06:00 +00:00
|
|
|
ed->buf = NULL;
|
1998-07-02 19:31:46 +00:00
|
|
|
ed->new_ctb = new_ctb;
|
1999-05-17 20:03:24 +00:00
|
|
|
ed->mdc_method = 0;
|
|
|
|
if( pkttype == PKT_ENCRYPTED_MDC ) {
|
|
|
|
/* fixme: add some pktlen sanity checks */
|
2000-06-05 10:27:46 +00:00
|
|
|
int version;
|
1999-05-17 20:03:24 +00:00
|
|
|
|
2001-02-08 16:28:28 +00:00
|
|
|
version = iobuf_get_noeof(inp);
|
|
|
|
if (orig_pktlen)
|
|
|
|
pktlen--;
|
1999-05-17 20:03:24 +00:00
|
|
|
if( version != 1 ) {
|
|
|
|
log_error("encrypted_mdc packet with unknown version %d\n",
|
|
|
|
version);
|
2000-10-04 13:51:27 +00:00
|
|
|
/*skip_rest(inp, pktlen); should we really do this? */
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1999-05-17 20:03:24 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
2000-06-05 10:27:46 +00:00
|
|
|
ed->mdc_method = DIGEST_ALGO_SHA1;
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2001-02-08 16:28:28 +00:00
|
|
|
if( orig_pktlen && pktlen < 10 ) { /* actually this is blocksize+2 */
|
1997-11-18 14:06:00 +00:00
|
|
|
log_error("packet(%d) too short\n", pkttype);
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1997-11-18 14:06:00 +00:00
|
|
|
skip_rest(inp, pktlen);
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-04-14 17:51:16 +00:00
|
|
|
if( list_mode ) {
|
2001-02-08 16:28:28 +00:00
|
|
|
if( orig_pktlen )
|
|
|
|
printf(":encrypted data packet:\n\tlength: %lu\n", orig_pktlen);
|
1997-11-18 14:06:00 +00:00
|
|
|
else
|
|
|
|
printf(":encrypted data packet:\n\tlength: unknown\n");
|
1999-05-17 20:03:24 +00:00
|
|
|
if( ed->mdc_method )
|
|
|
|
printf("\tmdc_method: %d\n", ed->mdc_method );
|
1998-04-14 17:51:16 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
ed->buf = inp;
|
|
|
|
pktlen = 0;
|
|
|
|
|
|
|
|
leave:
|
2001-03-12 20:21:50 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 10:27:46 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
|
|
|
|
PACKET *pkt, int new_ctb )
|
|
|
|
{
|
2001-03-12 20:21:50 +00:00
|
|
|
int rc = 0;
|
2000-06-05 10:27:46 +00:00
|
|
|
PKT_mdc *mdc;
|
|
|
|
byte *p;
|
|
|
|
|
|
|
|
mdc = pkt->pkt.mdc= m_alloc(sizeof *pkt->pkt.mdc );
|
|
|
|
if( list_mode )
|
|
|
|
printf(":mdc packet: length=%lu\n", pktlen);
|
|
|
|
if( !new_ctb || pktlen != 20 ) {
|
|
|
|
log_error("mdc_packet with invalid encoding\n");
|
2001-03-12 20:21:50 +00:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
2000-06-05 10:27:46 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
p = mdc->hash;
|
|
|
|
for( ; pktlen; pktlen--, p++ )
|
|
|
|
*p = iobuf_get_noeof(inp);
|
|
|
|
|
|
|
|
leave:
|
2001-03-12 20:21:50 +00:00
|
|
|
return rc;
|
2000-06-05 10:27:46 +00:00
|
|
|
}
|
|
|
|
|
2000-10-13 15:03:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This packet is internally generated by PGG (by armor.c) to
|
|
|
|
* transfer some information to the lower layer. To make sure that
|
|
|
|
* this packet is really a GPG faked one and not one comming from outside,
|
|
|
|
* we first check that tehre is a unique tag in it.
|
|
|
|
* The format of such a control packet is:
|
|
|
|
* n byte session marker
|
2001-03-27 09:23:14 +00:00
|
|
|
* 1 byte control type CTRLPKT_xxxxx
|
2000-10-13 15:03:48 +00:00
|
|
|
* m byte control data
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_gpg_control( IOBUF inp,
|
|
|
|
int pkttype, unsigned long pktlen, PACKET *packet )
|
|
|
|
{
|
|
|
|
byte *p;
|
|
|
|
const byte *sesmark;
|
|
|
|
size_t sesmarklen;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( list_mode )
|
|
|
|
printf(":packet 63: length %lu ", pktlen);
|
|
|
|
|
|
|
|
sesmark = get_session_marker ( &sesmarklen );
|
|
|
|
if ( pktlen < sesmarklen+1 ) /* 1 is for the control bytes */
|
|
|
|
goto skipit;
|
|
|
|
for( i=0; i < sesmarklen; i++, pktlen-- ) {
|
|
|
|
if ( sesmark[i] != iobuf_get_noeof(inp) )
|
|
|
|
goto skipit;
|
|
|
|
}
|
|
|
|
if ( list_mode )
|
|
|
|
puts ("- gpg control packet");
|
|
|
|
|
|
|
|
packet->pkt.gpg_control = m_alloc(sizeof *packet->pkt.gpg_control
|
|
|
|
+ pktlen - 1);
|
|
|
|
packet->pkt.gpg_control->control = iobuf_get_noeof(inp); pktlen--;
|
|
|
|
packet->pkt.gpg_control->datalen = pktlen;
|
|
|
|
p = packet->pkt.gpg_control->data;
|
|
|
|
for( ; pktlen; pktlen--, p++ )
|
|
|
|
*p = iobuf_get_noeof(inp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
skipit:
|
|
|
|
if ( list_mode ) {
|
2001-09-09 16:09:19 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
i=0;
|
2000-10-13 15:03:48 +00:00
|
|
|
printf("- private (rest length %lu)\n", pktlen);
|
|
|
|
if( iobuf_in_block_mode(inp) ) {
|
|
|
|
while( (c=iobuf_get(inp)) != -1 )
|
|
|
|
dump_hex_line(c, &i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for( ; pktlen; pktlen-- )
|
|
|
|
dump_hex_line(iobuf_get(inp), &i);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
skip_rest(inp,pktlen);
|
|
|
|
return G10ERR_INVALID_PACKET;
|
|
|
|
}
|
|
|
|
|
2001-04-05 12:21:43 +00:00
|
|
|
/* create a gpg control packet to be used internally as a placeholder */
|
|
|
|
PACKET *
|
|
|
|
create_gpg_control( ctrlpkttype_t type, const byte *data, size_t datalen )
|
|
|
|
{
|
|
|
|
PACKET *packet;
|
|
|
|
byte *p;
|
|
|
|
|
|
|
|
packet = m_alloc( sizeof *packet );
|
|
|
|
init_packet(packet);
|
|
|
|
packet->pkttype = PKT_GPG_CONTROL;
|
|
|
|
packet->pkt.gpg_control = m_alloc(sizeof *packet->pkt.gpg_control
|
|
|
|
+ datalen - 1);
|
|
|
|
packet->pkt.gpg_control->control = type;
|
|
|
|
packet->pkt.gpg_control->datalen = datalen;
|
|
|
|
p = packet->pkt.gpg_control->data;
|
|
|
|
for( ; datalen; datalen--, p++ )
|
|
|
|
*p = *data++;
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2000-10-13 15:03:48 +00:00
|
|
|
|