1 /* packet.h - packet read/write stuff
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30 #define DEBUG_PARSE_PACKET 1
34 PKT_PUBKEY_ENC =1, /* public key encrypted packet */
35 PKT_SIGNATURE =2, /* secret key encrypted packet */
36 PKT_SYMKEY_ENC =3, /* session key packet (OpenPGP)*/
37 PKT_ONEPASS_SIG =4, /* one pass sig packet (OpenPGP)*/
38 PKT_SECRET_KEY =5, /* secret key */
39 PKT_PUBLIC_KEY =6, /* public key */
40 PKT_SECRET_SUBKEY =7, /* secret subkey (OpenPGP) */
41 PKT_COMPRESSED =8, /* compressed data packet */
42 PKT_ENCRYPTED =9, /* conventional encrypted data */
43 PKT_MARKER =10, /* marker packet (OpenPGP) */
44 PKT_PLAINTEXT =11, /* plaintext data with filename and mode */
45 PKT_RING_TRUST =12, /* keyring trust packet */
46 PKT_USER_ID =13, /* user id packet */
47 PKT_PUBLIC_SUBKEY =14, /* public subkey (OpenPGP) */
48 PKT_OLD_COMMENT =16, /* comment packet from an OpenPGP draft */
49 PKT_COMMENT =61 /* new comment packet (private) */
52 typedef struct packet_struct PACKET;
63 byte cipher_algo; /* cipher algorithm used */
65 byte seskeylen; /* keylength in byte or 0 for no seskey */
70 u32 keyid[2]; /* 64 bit keyid */
72 byte pubkey_algo; /* algorithm used for public key scheme */
74 MPI data[PUBKEY_MAX_NENC];
79 u32 keyid[2]; /* 64 bit keyid */
80 byte sig_class; /* sig classification */
81 byte digest_algo; /* algorithm used for digest */
82 byte pubkey_algo; /* algorithm used for public key scheme */
83 byte last; /* a stupid flag */
88 ulong local_id; /* internal use, valid if > 0 */
90 unsigned checked:1; /* signature has been checked */
91 unsigned valid:1; /* signature is good (if checked is set) */
92 unsigned unknown_critical:1;
94 u32 keyid[2]; /* 64 bit keyid */
95 u32 timestamp; /* signature made */
97 byte sig_class; /* sig classification, append for MD calculation*/
98 byte pubkey_algo; /* algorithm used for public key scheme */
99 /* (PUBKEY_ALGO_xxx) */
100 byte digest_algo; /* algorithm used for digest (DIGEST_ALGO_xxxx) */
101 byte *hashed_data; /* all subpackets with hashed data (v4 only) */
102 byte *unhashed_data; /* ditto for unhashed data */
103 byte digest_start[2]; /* first 2 bytes of the digest */
104 MPI data[PUBKEY_MAX_NSIG];
109 * Note about the pkey/skey elements: We assume that the secret keys
110 * has the same elemts as the public key at the begin of the array, so
111 * that npkey < nskey and it is possible to compare the secret and
112 * public keys by comparing the first npkey elements of pkey againts skey.
115 u32 timestamp; /* key made */
116 u32 expiredate; /* expires at this date or 0 if not at all */
117 byte hdrbytes; /* number of header bytes */
119 byte pubkey_algo; /* algorithm used for public key scheme */
120 byte pubkey_usage; /* for now only used to pass it to getkey() */
121 ulong local_id; /* internal use, valid if > 0 */
122 u32 keyid[2]; /* calculated by keyid_from_pk() */
123 byte *namehash; /* if != NULL: found by this name */
124 MPI pkey[PUBKEY_MAX_NPKEY];
128 u32 timestamp; /* key made */
129 u32 expiredate; /* expires at this date or 0 if not at all */
130 byte hdrbytes; /* number of header bytes */
132 byte pubkey_algo; /* algorithm used for public key scheme */
135 byte is_protected; /* The secret info is protected and must */
136 /* be decrypted before use, the protected */
137 /* MPIs are simply (void*) pointers to memory */
138 /* and should never be passed to a mpi_xxx() */
140 byte algo; /* cipher used to protect the secret information*/
142 byte ivlen; /* used length of the iv */
143 byte iv[16]; /* initialization vector for CFB mode */
145 MPI skey[PUBKEY_MAX_NSKEY];
146 u16 csum; /* checksum */
151 int len; /* length of data */
156 int len; /* length of the name */
161 u32 len; /* reserved */
164 IOBUF buf; /* IOBUF reference */
168 u32 len; /* length of encrypted data */
170 IOBUF buf; /* IOBUF reference */
174 u32 len; /* length of encrypted data */
175 IOBUF buf; /* IOBUF reference */
183 /* combine all packets into a union */
184 struct packet_struct {
188 PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */
189 PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */
190 PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */
191 PKT_signature *signature; /* PKT_SIGNATURE */
192 PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */
193 PKT_secret_key *secret_key; /* PKT_SECRET_[SUB]KEY */
194 PKT_comment *comment; /* PKT_COMMENT */
195 PKT_user_id *user_id; /* PKT_USER_ID */
196 PKT_compressed *compressed; /* PKT_COMPRESSED */
197 PKT_encrypted *encrypted; /* PKT_ENCRYPTED */
198 PKT_plaintext *plaintext; /* PKT_PLAINTEXT */
202 #define init_packet(a) do { (a)->pkttype = 0; \
203 (a)->pkt.generic = NULL; \
207 SIGSUBPKT_TEST_CRITICAL=-3,
208 SIGSUBPKT_LIST_UNHASHED=-2,
209 SIGSUBPKT_LIST_HASHED =-1,
211 SIGSUBPKT_SIG_CREATED = 2, /* signature creation time */
212 SIGSUBPKT_SIG_EXPIRE = 3, /* signature expiration time */
213 SIGSUBPKT_EXPORTABLE = 4, /* exportable */
214 SIGSUBPKT_TRUST = 5, /* trust signature */
215 SIGSUBPKT_REGEXP = 6, /* regular expression */
216 SIGSUBPKT_REVOCABLE = 7, /* revocable */
217 SIGSUBPKT_KEY_EXPIRE = 9, /* key expiration time */
218 SIGSUBPKT_ARR =10, /* additional recipient request */
219 SIGSUBPKT_PREF_SYM =11, /* preferred symmetric algorithms */
220 SIGSUBPKT_REV_KEY =12, /* revocation key */
221 SIGSUBPKT_ISSUER =16, /* issuer key ID */
222 SIGSUBPKT_NOTATION =20, /* notation data */
223 SIGSUBPKT_PREF_HASH =21, /* preferred hash algorithms */
224 SIGSUBPKT_PREF_COMPR =22, /* preferred compression algorithms */
225 SIGSUBPKT_KS_FLAGS =23, /* key server preferences */
226 SIGSUBPKT_PREF_KS =24, /* preferred key server */
227 SIGSUBPKT_PRIMARY_UID =25, /* primary user id */
228 SIGSUBPKT_POLICY =26, /* policy URL */
229 SIGSUBPKT_KEY_FLAGS =27, /* key flags */
230 SIGSUBPKT_SIGNERS_UID =28, /* signer's user id */
231 SIGSUBPKT_PRIV_ADD_SIG =101 /* signatur is also valid for this uid */
236 int proc_packets( IOBUF a );
237 int proc_signature_packets( IOBUF a, STRLIST signedfiles, const char *sigfile );
238 int proc_encryption_packets( IOBUF a );
239 int list_packets( IOBUF a );
241 /*-- parse-packet.c --*/
242 int set_packet_list_mode( int mode );
244 #if DEBUG_PARSE_PACKET
245 int dbg_search_packet( IOBUF inp, PACKET *pkt, int pkttype, ulong *retpos, const char* file, int lineno );
246 int dbg_parse_packet( IOBUF inp, PACKET *ret_pkt, const char* file, int lineno );
247 int dbg_copy_all_packets( IOBUF inp, IOBUF out, const char* file, int lineno );
248 int dbg_copy_some_packets( IOBUF inp, IOBUF out, ulong stopoff, const char* file, int lineno );
249 int dbg_skip_some_packets( IOBUF inp, unsigned n, const char* file, int lineno );
250 #define search_packet( a,b,c,d ) dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ )
251 #define parse_packet( a, b ) dbg_parse_packet( (a), (b), __FILE__, __LINE__ )
252 #define copy_all_packets( a,b ) dbg_copy_all_packets((a),(b), __FILE__, __LINE__ )
253 #define copy_some_packets( a,b,c ) dbg_copy_some_packets((a),(b),(c), __FILE__, __LINE__ )
254 #define skip_some_packets( a,b ) dbg_skip_some_packets((a),(b), __FILE__, __LINE__ )
256 int search_packet( IOBUF inp, PACKET *pkt, int pkttype, ulong *retpos );
257 int parse_packet( IOBUF inp, PACKET *ret_pkt);
258 int copy_all_packets( IOBUF inp, IOBUF out );
259 int copy_some_packets( IOBUF inp, IOBUF out, ulong stopoff );
260 int skip_some_packets( IOBUF inp, unsigned n );
263 const byte *parse_sig_subpkt( const byte *buffer,
264 sigsubpkttype_t reqtype, size_t *ret_n );
265 const byte *parse_sig_subpkt2( PKT_signature *sig,
266 sigsubpkttype_t reqtype, size_t *ret_n );
268 /*-- build-packet.c --*/
269 int build_packet( IOBUF inp, PACKET *pkt );
270 u32 calc_packet_length( PACKET *pkt );
271 void hash_public_key( MD_HANDLE md, PKT_public_key *pk );
272 void build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type,
273 const byte *buffer, size_t buflen );
274 void build_sig_subpkt_from_sig( PKT_signature *sig );
276 /*-- free-packet.c --*/
277 void free_symkey_enc( PKT_symkey_enc *enc );
278 void free_pubkey_enc( PKT_pubkey_enc *enc );
279 void free_seckey_enc( PKT_signature *enc );
280 int digest_algo_from_sig( PKT_signature *sig );
281 void release_public_key_parts( PKT_public_key *pk );
282 void free_public_key( PKT_public_key *key );
283 void release_secret_key_parts( PKT_secret_key *sk );
284 void free_secret_key( PKT_secret_key *sk );
285 void free_user_id( PKT_user_id *uid );
286 void free_comment( PKT_comment *rem );
287 void free_packet( PACKET *pkt );
288 PKT_public_key *copy_public_key( PKT_public_key *d, PKT_public_key *s );
289 PKT_public_key *copy_public_key_new_namehash( PKT_public_key *d,
291 const byte *namehash );
292 PKT_secret_key *copy_secret_key( PKT_secret_key *d, PKT_secret_key *s );
293 PKT_signature *copy_signature( PKT_signature *d, PKT_signature *s );
294 PKT_user_id *copy_user_id( PKT_user_id *d, PKT_user_id *s );
295 int cmp_public_keys( PKT_public_key *a, PKT_public_key *b );
296 int cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b );
297 int cmp_signatures( PKT_signature *a, PKT_signature *b );
298 int cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk );
299 int cmp_user_ids( PKT_user_id *a, PKT_user_id *b );
302 /*-- sig-check.c --*/
303 int signature_check( PKT_signature *sig, MD_HANDLE digest );
305 /*-- seckey-cert.c --*/
306 int is_secret_key_protected( PKT_secret_key *sk );
307 int check_secret_key( PKT_secret_key *sk, int retries );
308 int protect_secret_key( PKT_secret_key *sk, DEK *dek );
310 /*-- pubkey-enc.c --*/
311 int get_session_key( PKT_pubkey_enc *k, DEK *dek );
314 int handle_compressed( PKT_compressed *cd,
315 int (*callback)(IOBUF, void *), void *passthru );
317 /*-- encr-data.c --*/
318 int decrypt_data( PKT_encrypted *ed, DEK *dek );
319 int encrypt_data( PKT_encrypted *ed, DEK *dek );
321 /*-- plaintext.c --*/
322 int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
323 int nooutput, int clearsig );
324 int ask_for_detached_datafile( md_filter_context_t *mfx, const char *inname );
327 int write_comment( IOBUF out, const char *s );
330 int make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
331 PKT_user_id *uid, PKT_public_key *subpk,
333 int sigclass, int digest_algo,
334 int (*mksubpkt)(PKT_signature *, void *),
338 PKT_user_id *generate_user_id(void);
340 #endif /*G10_PACKET_H*/