1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-10-31 20:08:43 +01:00
gnupg/g10/packet.h

397 lines
14 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* packet.h - packet read/write stuff
* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
* This file is part of GnuPG.
1997-11-18 15:06:00 +01:00
*
* GnuPG is free software; you can redistribute it and/or modify
1997-11-18 15:06:00 +01: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.
*
* GnuPG is distributed in the hope that it will be useful,
1997-11-18 15:06:00 +01: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
*/
#ifndef GPG_PACKET_H
#define GPG_PACKET_H
1997-11-18 15:06:00 +01:00
#include "types.h"
#include "iobuf.h"
#include "filter.h"
#ifndef DID_MPI_TYPEDEF
typedef struct gcry_mpi *MPI;
#define DID_MPI_TYPEDEF
#endif
#define GNUPG_MAX_NPKEY 4
#define GNUPG_MAX_NSKEY 6
#define GNUPG_MAX_NSIG 2
#define GNUPG_MAX_NENC 2
#define DEBUG_PARSE_PACKET 1
1997-12-01 11:33:23 +01:00
typedef enum {
PKT_NONE =0,
PKT_PUBKEY_ENC =1, /* public key encrypted packet */
PKT_SIGNATURE =2, /* secret key encrypted packet */
1998-05-03 17:42:08 +02:00
PKT_SYMKEY_ENC =3, /* session key packet (OpenPGP)*/
1997-12-01 11:33:23 +01:00
PKT_ONEPASS_SIG =4, /* one pass sig packet (OpenPGP)*/
1998-06-29 14:30:57 +02:00
PKT_SECRET_KEY =5, /* secret key */
PKT_PUBLIC_KEY =6, /* public key */
PKT_SECRET_SUBKEY =7, /* secret subkey (OpenPGP) */
1997-12-01 11:33:23 +01:00
PKT_COMPRESSED =8, /* compressed data packet */
PKT_ENCRYPTED =9, /* conventional encrypted data */
PKT_MARKER =10, /* marker packet (OpenPGP) */
PKT_PLAINTEXT =11, /* plaintext data with filename and mode */
PKT_RING_TRUST =12, /* keyring trust packet */
PKT_USER_ID =13, /* user id packet */
1998-06-29 14:30:57 +02:00
PKT_PUBLIC_SUBKEY =14, /* public subkey (OpenPGP) */
1998-05-03 21:35:33 +02:00
PKT_OLD_COMMENT =16, /* comment packet from an OpenPGP draft */
PKT_PHOTO_ID =17, /* PGP's photo ID */
PKT_ENCRYPTED_MDC =18, /* integrity protected encrypted data */
PKT_MDC =19, /* manipulaion detection code packet */
PKT_COMMENT =61, /* new comment packet (private) */
1997-12-01 11:33:23 +01:00
} pkttype_t;
1997-11-18 15:06:00 +01:00
typedef struct packet_struct PACKET;
1998-05-03 17:42:08 +02:00
typedef struct {
int mode;
1998-05-03 17:42:08 +02:00
byte hash_algo;
byte salt[8];
u32 count;
} STRING2KEY;
typedef struct {
byte version;
byte cipher_algo; /* cipher algorithm used */
STRING2KEY s2k;
byte seskeylen; /* keylength in byte or 0 for no seskey */
byte seskey[1];
} PKT_symkey_enc;
1997-11-18 15:06:00 +01:00
typedef struct {
u32 keyid[2]; /* 64 bit keyid */
1998-03-19 16:27:29 +01:00
byte version;
1997-11-18 15:06:00 +01:00
byte pubkey_algo; /* algorithm used for public key scheme */
1998-09-11 07:47:32 +02:00
byte throw_keyid;
MPI data[GNUPG_MAX_NENC];
1997-11-18 15:06:00 +01:00
} PKT_pubkey_enc;
1997-12-02 20:36:53 +01:00
typedef struct {
u32 keyid[2]; /* 64 bit keyid */
byte sig_class; /* sig classification */
byte digest_algo; /* algorithm used for digest */
byte pubkey_algo; /* algorithm used for public key scheme */
byte last; /* a stupid flag */
} PKT_onepass_sig;
1997-11-18 15:06:00 +01:00
typedef struct {
1998-01-16 22:15:24 +01:00
ulong local_id; /* internal use, valid if > 0 */
1998-10-01 09:23:00 +02:00
struct {
unsigned checked:1; /* signature has been checked */
unsigned valid:1; /* signature is good (if checked is set) */
unsigned unknown_critical:1;
} flags;
u32 keyid[2]; /* 64 bit keyid */
1997-11-18 15:06:00 +01:00
u32 timestamp; /* signature made */
1998-03-09 22:44:06 +01:00
byte version;
1997-11-18 15:06:00 +01:00
byte sig_class; /* sig classification, append for MD calculation*/
byte pubkey_algo; /* algorithm used for public key scheme */
/* (GCRY_PK_xxx) */
1998-03-09 22:44:06 +01:00
byte digest_algo; /* algorithm used for digest (DIGEST_ALGO_xxxx) */
byte *hashed_data; /* all subpackets with hashed data (v4 only) */
byte *unhashed_data; /* ditto for unhashed data */
byte digest_start[2]; /* first 2 bytes of the digest */
MPI data[GNUPG_MAX_NSIG];
1997-11-18 15:06:00 +01:00
} PKT_signature;
1998-06-13 08:59:14 +02:00
/****************
* Note about the pkey/skey elements: We assume that the secret keys
* has the same elemts as the public key at the begin of the array, so
* that npkey < nskey and it is possible to compare the secret and
* public keys by comparing the first npkey elements of pkey againts skey.
*/
1997-11-18 15:06:00 +01:00
typedef struct {
1998-06-29 14:30:57 +02:00
u32 timestamp; /* key made */
1998-10-16 18:00:17 +02:00
u32 expiredate; /* expires at this date or 0 if not at all */
1998-01-16 22:15:24 +01:00
byte hdrbytes; /* number of header bytes */
byte version;
1997-11-18 15:06:00 +01:00
byte pubkey_algo; /* algorithm used for public key scheme */
byte pubkey_usage; /* the actual allowed usage as set by getkey() */
u32 created; /* according to the self-signature */
byte req_usage; /* hack to pass a request to getkey() */
byte req_algo; /* Ditto */
u32 has_expired; /* set to the expiration date if expired */
int is_revoked; /* key has been revoked */
int is_valid; /* key (especially subkey) is valid */
1998-01-16 22:15:24 +01:00
ulong local_id; /* internal use, valid if > 0 */
u32 main_keyid[2]; /* keyid of the primary key */
1998-07-09 15:37:17 +02:00
u32 keyid[2]; /* calculated by keyid_from_pk() */
1998-08-05 18:51:59 +02:00
byte *namehash; /* if != NULL: found by this name */
MPI pkey[GNUPG_MAX_NPKEY];
1998-06-29 14:30:57 +02:00
} PKT_public_key;
1997-11-18 15:06:00 +01:00
typedef struct {
1998-06-29 14:30:57 +02:00
u32 timestamp; /* key made */
1998-10-16 18:00:17 +02:00
u32 expiredate; /* expires at this date or 0 if not at all */
1998-01-16 22:15:24 +01:00
byte hdrbytes; /* number of header bytes */
byte version;
1997-11-18 15:06:00 +01:00
byte pubkey_algo; /* algorithm used for public key scheme */
1998-07-06 12:23:57 +02:00
byte pubkey_usage;
u32 created; /* according to the self-signature */
byte req_usage;
byte req_algo;
u32 has_expired; /* set to the expiration date if expired */
int is_revoked; /* key has been revoked */
int is_valid; /* key (especially subkey) is valid */
u32 main_keyid[2]; /* keyid of the primary key */
u32 keyid[2];
byte is_primary;
1998-04-14 19:51:16 +02:00
byte is_protected; /* The secret info is protected and must */
/* be decrypted before use, the protected */
1998-04-02 12:30:03 +02:00
/* MPIs are simply (void*) pointers to memory */
/* and should never be passed to a mpi_xxx() */
struct {
1998-04-14 19:51:16 +02:00
byte algo; /* cipher used to protect the secret information*/
1998-05-03 17:42:08 +02:00
STRING2KEY s2k;
byte ivlen; /* used length of the iv */
byte iv[16]; /* initialization vector for CFB mode */
1998-04-02 12:30:03 +02:00
} protect;
MPI skey[GNUPG_MAX_NSKEY];
1998-04-02 12:30:03 +02:00
u16 csum; /* checksum */
1998-06-29 14:30:57 +02:00
} PKT_secret_key;
1997-11-18 15:06:00 +01:00
typedef struct {
int len; /* length of data */
char data[1];
} PKT_comment;
typedef struct {
ulong stored_at; /* the stream offset where it was stored
* by build-packet */
1997-11-18 15:06:00 +01:00
int len; /* length of the name */
char *photo; /* if this is not NULL, the packet is a photo ID */
int photolen; /* and the length of the photo */
int help_key_usage;
u32 help_key_expire;
int is_primary;
u32 created; /* according to the self-signature */
1997-11-18 15:06:00 +01:00
char name[1];
} PKT_user_id;
typedef struct {
u32 len; /* reserved */
byte new_ctb;
1997-11-18 15:06:00 +01:00
byte algorithm;
IOBUF buf; /* IOBUF reference */
} PKT_compressed;
typedef struct {
u32 len; /* length of encrypted data */
byte new_ctb; /* uses a new CTB */
byte mdc_method; /* > 0: integrity protected encrypted data packet */
1997-11-18 15:06:00 +01:00
IOBUF buf; /* IOBUF reference */
1997-12-01 11:33:23 +01:00
} PKT_encrypted;
1997-11-18 15:06:00 +01:00
typedef struct {
byte hash[20];
} PKT_mdc;
typedef struct {
unsigned int trustval;
} PKT_ring_trust;
1997-11-18 15:06:00 +01:00
typedef struct {
u32 len; /* length of encrypted data */
IOBUF buf; /* IOBUF reference */
byte new_ctb;
1997-11-18 15:06:00 +01:00
int mode;
u32 timestamp;
int namelen;
char name[1];
} PKT_plaintext;
1997-11-18 15:06:00 +01:00
/* combine all packets into a union */
struct packet_struct {
1997-12-01 11:33:23 +01:00
pkttype_t pkttype;
1997-11-18 15:06:00 +01:00
union {
void *generic;
1998-05-03 17:42:08 +02:00
PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */
1997-12-01 11:33:23 +01:00
PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */
1997-12-02 20:36:53 +01:00
PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */
1997-12-01 11:33:23 +01:00
PKT_signature *signature; /* PKT_SIGNATURE */
1998-06-29 14:30:57 +02:00
PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */
PKT_secret_key *secret_key; /* PKT_SECRET_[SUB]KEY */
1997-11-18 15:06:00 +01:00
PKT_comment *comment; /* PKT_COMMENT */
PKT_user_id *user_id; /* PKT_USER_ID */
PKT_compressed *compressed; /* PKT_COMPRESSED */
PKT_encrypted *encrypted; /* PKT_ENCRYPTED[_MDC] */
PKT_mdc *mdc; /* PKT_MDC */
PKT_ring_trust *ring_trust; /* PKT_RING_TRUST */
1997-11-18 15:06:00 +01:00
PKT_plaintext *plaintext; /* PKT_PLAINTEXT */
} pkt;
};
#define init_packet(a) do { (a)->pkttype = 0; \
(a)->pkt.generic = NULL; \
} while(0)
1998-05-13 19:53:36 +02:00
typedef enum {
SIGSUBPKT_TEST_CRITICAL=-3,
1998-05-13 19:53:36 +02:00
SIGSUBPKT_LIST_UNHASHED=-2,
SIGSUBPKT_LIST_HASHED =-1,
SIGSUBPKT_NONE = 0,
SIGSUBPKT_SIG_CREATED = 2, /* signature creation time */
SIGSUBPKT_SIG_EXPIRE = 3, /* signature expiration time */
SIGSUBPKT_EXPORTABLE = 4, /* exportable */
SIGSUBPKT_TRUST = 5, /* trust signature */
SIGSUBPKT_REGEXP = 6, /* regular expression */
SIGSUBPKT_REVOCABLE = 7, /* revocable */
SIGSUBPKT_KEY_EXPIRE = 9, /* key expiration time */
SIGSUBPKT_ARR =10, /* additional recipient request */
SIGSUBPKT_PREF_SYM =11, /* preferred symmetric algorithms */
SIGSUBPKT_REV_KEY =12, /* revocation key */
SIGSUBPKT_ISSUER =16, /* issuer key ID */
SIGSUBPKT_NOTATION =20, /* notation data */
SIGSUBPKT_PREF_HASH =21, /* preferred hash algorithms */
SIGSUBPKT_PREF_COMPR =22, /* preferred compression algorithms */
SIGSUBPKT_KS_FLAGS =23, /* key server preferences */
SIGSUBPKT_PREF_KS =24, /* preferred key server */
SIGSUBPKT_PRIMARY_UID =25, /* primary user id */
SIGSUBPKT_POLICY =26, /* policy URL */
SIGSUBPKT_KEY_FLAGS =27, /* key flags */
1998-06-09 17:14:06 +02:00
SIGSUBPKT_SIGNERS_UID =28, /* signer's user id */
SIGSUBPKT_REVOC_REASON =29, /* reason for revocation */
2000-10-11 19:26:49 +02:00
SIGSUBPKT_FEATURES =30, /* feature flags */
SIGSUBPKT_PRIV_ADD_SIG =101,/* signatur is also valid for this uid */
SIGSUBPKT_FLAG_CRITICAL=128
1998-05-13 19:53:36 +02:00
} sigsubpkttype_t;
1997-11-18 15:06:00 +01:00
/*-- mainproc.c --*/
int proc_packets( void *ctx, IOBUF a );
int proc_signature_packets( void *ctx, IOBUF a,
STRLIST signedfiles, const char *sigfile );
int proc_encryption_packets( void *ctx, IOBUF a );
1998-01-02 21:40:10 +01:00
int list_packets( IOBUF a );
1997-11-18 15:06:00 +01:00
/*-- parse-packet.c --*/
int set_packet_list_mode( int mode );
#if DEBUG_PARSE_PACKET
int dbg_search_packet( IOBUF inp, PACKET *pkt, int pkttype, ulong *retpos, const char* file, int lineno );
int dbg_parse_packet( IOBUF inp, PACKET *ret_pkt, ulong *pos,
const char* file, int lineno );
int dbg_copy_all_packets( IOBUF inp, IOBUF out, const char* file, int lineno );
int dbg_copy_some_packets( IOBUF inp, IOBUF out, ulong stopoff, const char* file, int lineno );
int dbg_skip_some_packets( IOBUF inp, unsigned n, const char* file, int lineno );
#define search_packet( a,b,c,d ) dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ )
#define parse_packet( a, b, c ) dbg_parse_packet( (a), (b), (c), __FILE__, __LINE__ )
#define copy_all_packets( a,b ) dbg_copy_all_packets((a),(b), __FILE__, __LINE__ )
#define copy_some_packets( a,b,c ) dbg_copy_some_packets((a),(b),(c), __FILE__, __LINE__ )
#define skip_some_packets( a,b ) dbg_skip_some_packets((a),(b), __FILE__, __LINE__ )
#else
1997-12-12 13:03:58 +01:00
int search_packet( IOBUF inp, PACKET *pkt, int pkttype, ulong *retpos );
int parse_packet( IOBUF inp, PACKET *ret_pkt, ulong *retpos);
int copy_all_packets( IOBUF inp, IOBUF out );
int copy_some_packets( IOBUF inp, IOBUF out, ulong stopoff );
int skip_some_packets( IOBUF inp, unsigned n );
#endif
const byte *enum_sig_subpkt( const byte *buffer, sigsubpkttype_t reqtype,
size_t *ret_n, int *start );
const byte *parse_sig_subpkt( const byte *buffer, sigsubpkttype_t reqtype,
size_t *ret_n );
1998-08-05 18:51:59 +02:00
const byte *parse_sig_subpkt2( PKT_signature *sig,
sigsubpkttype_t reqtype, size_t *ret_n );
1997-11-18 15:06:00 +01:00
/*-- build-packet.c --*/
int build_packet( IOBUF inp, PACKET *pkt );
u32 calc_packet_length( PACKET *pkt );
void hash_public_key( GCRY_MD_HD md, PKT_public_key *pk );
1998-05-13 19:53:36 +02:00
void build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type,
const byte *buffer, size_t buflen );
void build_sig_subpkt_from_sig( PKT_signature *sig );
1997-11-18 15:06:00 +01:00
/*-- free-packet.c --*/
1998-05-03 17:42:08 +02:00
void free_symkey_enc( PKT_symkey_enc *enc );
1997-11-18 15:06:00 +01:00
void free_pubkey_enc( PKT_pubkey_enc *enc );
void free_seckey_enc( PKT_signature *enc );
1998-01-30 17:23:16 +01:00
int digest_algo_from_sig( PKT_signature *sig );
1998-06-29 14:30:57 +02:00
void release_public_key_parts( PKT_public_key *pk );
void free_public_key( PKT_public_key *key );
void release_secret_key_parts( PKT_secret_key *sk );
void free_secret_key( PKT_secret_key *sk );
1997-11-18 15:06:00 +01:00
void free_user_id( PKT_user_id *uid );
void free_comment( PKT_comment *rem );
void free_packet( PACKET *pkt );
1998-06-29 14:30:57 +02:00
PKT_public_key *copy_public_key( PKT_public_key *d, PKT_public_key *s );
1998-08-05 18:51:59 +02:00
PKT_public_key *copy_public_key_new_namehash( PKT_public_key *d,
PKT_public_key *s,
const byte *namehash );
void copy_public_parts_to_secret_key( PKT_public_key *pk,
PKT_secret_key *sk );
1998-06-29 14:30:57 +02:00
PKT_secret_key *copy_secret_key( PKT_secret_key *d, PKT_secret_key *s );
1998-07-29 21:35:05 +02:00
PKT_signature *copy_signature( PKT_signature *d, PKT_signature *s );
PKT_user_id *copy_user_id( PKT_user_id *d, PKT_user_id *s );
int cmp_public_keys( PKT_public_key *a, PKT_public_key *b );
int cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b );
1998-10-18 17:21:22 +02:00
int cmp_signatures( PKT_signature *a, PKT_signature *b );
1998-06-29 14:30:57 +02:00
int cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk );
1998-02-17 21:48:52 +01:00
int cmp_user_ids( PKT_user_id *a, PKT_user_id *b );
1997-11-18 15:06:00 +01:00
/*-- sig-check.c --*/
int signature_check( PKT_signature *sig, GCRY_MD_HD digest );
1997-11-18 15:06:00 +01:00
/*-- seckey-cert.c --*/
1998-06-29 14:30:57 +02:00
int is_secret_key_protected( PKT_secret_key *sk );
1998-09-11 07:47:32 +02:00
int check_secret_key( PKT_secret_key *sk, int retries );
1998-06-29 14:30:57 +02:00
int protect_secret_key( PKT_secret_key *sk, DEK *dek );
1997-11-18 15:06:00 +01:00
/*-- pubkey-enc.c --*/
int get_session_key( PKT_pubkey_enc *k, DEK *dek );
1997-11-23 16:38:27 +01:00
/*-- compress.c --*/
int handle_compressed( void *ctx, PKT_compressed *cd,
1998-03-09 22:44:06 +01:00
int (*callback)(IOBUF, void *), void *passthru );
1997-11-18 15:06:00 +01:00
/*-- encr-data.c --*/
int decrypt_data( void *ctx, PKT_encrypted *ed, DEK *dek );
1997-11-18 15:06:00 +01:00
/*-- plaintext.c --*/
1998-07-08 11:29:43 +02:00
int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
int nooutput, int clearsig );
int ask_for_detached_datafile( GCRY_MD_HD md, GCRY_MD_HD md2,
const char *inname, int textmode );
1997-11-24 12:04:11 +01:00
/*-- comment.c --*/
int write_comment( IOBUF out, const char *s );
1997-12-09 13:46:23 +01:00
/*-- sign.c --*/
1998-06-29 14:30:57 +02:00
int make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
PKT_user_id *uid, PKT_public_key *subpk,
PKT_secret_key *sk,
1998-06-25 12:19:08 +02:00
int sigclass, int digest_algo,
int (*mksubpkt)(PKT_signature *, void *),
void *opaque );
1997-11-18 15:06:00 +01:00
1998-07-29 21:35:05 +02:00
/*-- keygen.c --*/
PKT_user_id *generate_user_id(void);
#endif /*GPG_PACKET_H*/