Fixed EOF detection for encrypted packets.
[gnupg.git] / g10 / packet.h
blobcb2f0c9f15411563c944facb2eccf3ba66659c2b
1 /* packet.h - OpenPGP packet definitions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #ifndef G10_PACKET_H
22 #define G10_PACKET_H
24 #include "types.h"
25 #include "../common/iobuf.h"
26 #include "../jnlib/strlist.h"
27 #include "cipher.h"
28 #include "filter.h"
29 #include "../common/openpgpdefs.h"
31 #define DEBUG_PARSE_PACKET 1
34 typedef struct packet_struct PACKET;
36 /* PKT_GPG_CONTROL types */
37 typedef enum {
38 CTRLPKT_CLEARSIGN_START = 1,
39 CTRLPKT_PIPEMODE = 2,
40 CTRLPKT_PLAINTEXT_MARK =3
41 } ctrlpkttype_t;
43 typedef enum {
44 PREFTYPE_NONE = 0,
45 PREFTYPE_SYM = 1,
46 PREFTYPE_HASH = 2,
47 PREFTYPE_ZIP = 3
48 } preftype_t;
50 typedef struct {
51 byte type;
52 byte value;
53 } prefitem_t;
55 typedef struct {
56 int mode;
57 byte hash_algo;
58 byte salt[8];
59 u32 count;
60 } STRING2KEY;
62 typedef struct {
63 byte version;
64 byte cipher_algo; /* cipher algorithm used */
65 STRING2KEY s2k;
66 byte seskeylen; /* keylength in byte or 0 for no seskey */
67 byte seskey[1];
68 } PKT_symkey_enc;
70 typedef struct {
71 u32 keyid[2]; /* 64 bit keyid */
72 byte version;
73 byte pubkey_algo; /* algorithm used for public key scheme */
74 byte throw_keyid;
75 gcry_mpi_t data[PUBKEY_MAX_NENC];
76 } PKT_pubkey_enc;
79 typedef struct {
80 u32 keyid[2]; /* 64 bit keyid */
81 byte sig_class; /* sig classification */
82 byte digest_algo; /* algorithm used for digest */
83 byte pubkey_algo; /* algorithm used for public key scheme */
84 byte last; /* a stupid flag */
85 } PKT_onepass_sig;
88 typedef struct {
89 size_t size; /* allocated */
90 size_t len; /* used */
91 byte data[1];
92 } subpktarea_t;
94 struct revocation_key {
95 byte class;
96 byte algid;
97 byte fpr[MAX_FINGERPRINT_LEN];
101 /* Object to keep information about a PKA DNS record. */
102 typedef struct
104 int valid; /* An actual PKA record exists for EMAIL. */
105 int checked; /* Set to true if the FPR has been checked against the
106 actual key. */
107 char *uri; /* Malloced string with the URI. NULL if the URI is
108 not available.*/
109 unsigned char fpr[20]; /* The fingerprint as stored in the PKA RR. */
110 char email[1];/* The email address from the notation data. */
111 } pka_info_t;
114 /* Object to keep information pertaining to a signature. */
115 typedef struct
117 struct
119 unsigned checked:1; /* Signature has been checked. */
120 unsigned valid:1; /* Signature is good (if checked is set). */
121 unsigned chosen_selfsig:1; /* A selfsig that is the chosen one. */
122 unsigned unknown_critical:1;
123 unsigned exportable:1;
124 unsigned revocable:1;
125 unsigned policy_url:1; /* At least one policy URL is present */
126 unsigned notation:1; /* At least one notation is present */
127 unsigned pref_ks:1; /* At least one preferred keyserver is present */
128 unsigned expired:1;
129 unsigned pka_tried:1; /* Set if we tried to retrieve the PKA record. */
130 } flags;
131 u32 keyid[2]; /* 64 bit keyid */
132 u32 timestamp; /* Signature made (seconds since Epoch). */
133 u32 expiredate; /* Expires at this date or 0 if not at all. */
134 byte version;
135 byte sig_class; /* Sig classification, append for MD calculation. */
136 byte pubkey_algo; /* Algorithm used for public key scheme */
137 /* (PUBKEY_ALGO_xxx) */
138 byte digest_algo; /* Algorithm used for digest (DIGEST_ALGO_xxxx). */
139 byte trust_depth;
140 byte trust_value;
141 const byte *trust_regexp;
142 struct revocation_key **revkey;
143 int numrevkeys;
144 pka_info_t *pka_info; /* Malloced PKA data or NULL if not
145 available. See also flags.pka_tried. */
146 subpktarea_t *hashed; /* All subpackets with hashed data (v4 only). */
147 subpktarea_t *unhashed; /* Ditto for unhashed data. */
148 byte digest_start[2]; /* First 2 bytes of the digest. */
149 gcry_mpi_t data[PUBKEY_MAX_NSIG];
150 } PKT_signature;
152 #define ATTRIB_IMAGE 1
154 /* This is the cooked form of attributes */
155 struct user_attribute {
156 byte type;
157 const byte *data;
158 u32 len;
161 typedef struct
163 int ref; /* reference counter */
164 int len; /* length of the name */
165 struct user_attribute *attribs;
166 int numattribs;
167 byte *attrib_data; /* if this is not NULL, the packet is an attribute */
168 unsigned long attrib_len;
169 byte *namehash;
170 int help_key_usage;
171 u32 help_key_expire;
172 int help_full_count;
173 int help_marginal_count;
174 int is_primary; /* 2 if set via the primary flag, 1 if calculated */
175 int is_revoked;
176 int is_expired;
177 u32 expiredate; /* expires at this date or 0 if not at all */
178 prefitem_t *prefs; /* list of preferences (may be NULL)*/
179 u32 created; /* according to the self-signature */
180 byte selfsigversion;
181 struct
183 /* TODO: Move more flags here */
184 unsigned mdc:1;
185 unsigned ks_modify:1;
186 unsigned compacted:1;
187 } flags;
188 char name[1];
189 } PKT_user_id;
191 struct revoke_info
193 /* revoked at this date */
194 u32 date;
195 /* the keyid of the revoking key (selfsig or designated revoker) */
196 u32 keyid[2];
197 /* the algo of the revoking key */
198 byte algo;
201 /****************
202 * Note about the pkey/skey elements: We assume that the secret keys
203 * has the same elemts as the public key at the begin of the array, so
204 * that npkey < nskey and it is possible to compare the secret and
205 * public keys by comparing the first npkey elements of pkey againts skey.
207 typedef struct {
208 u32 timestamp; /* key made */
209 u32 expiredate; /* expires at this date or 0 if not at all */
210 u32 max_expiredate; /* must not expire past this date */
211 struct revoke_info revoked;
212 byte hdrbytes; /* number of header bytes */
213 byte version;
214 byte selfsigversion; /* highest version of all of the self-sigs */
215 byte pubkey_algo; /* algorithm used for public key scheme */
216 byte pubkey_usage; /* for now only used to pass it to getkey() */
217 byte req_usage; /* hack to pass a request to getkey() */
218 byte req_algo; /* Ditto */
219 u32 has_expired; /* set to the expiration date if expired */
220 int is_revoked; /* key has been revoked, 1 if by the
221 owner, 2 if by a designated revoker */
222 int maybe_revoked; /* a designated revocation is present, but
223 without the key to check it */
224 int is_valid; /* key (especially subkey) is valid */
225 int dont_cache; /* do not cache this */
226 byte backsig; /* 0=none, 1=bad, 2=good */
227 u32 main_keyid[2]; /* keyid of the primary key */
228 u32 keyid[2]; /* calculated by keyid_from_pk() */
229 byte is_primary;
230 byte is_disabled; /* 0 for unset, 1 for enabled, 2 for disabled. */
231 prefitem_t *prefs; /* list of preferences (may be NULL) */
232 int mdc_feature; /* mdc feature set */
233 PKT_user_id *user_id; /* if != NULL: found by that uid */
234 struct revocation_key *revkey;
235 int numrevkeys;
236 u32 trust_timestamp;
237 byte trust_depth;
238 byte trust_value;
239 const byte *trust_regexp;
240 gcry_mpi_t pkey[PUBKEY_MAX_NPKEY];
241 } PKT_public_key;
243 /* Evaluates as true if the pk is disabled, and false if it isn't. If
244 there is no disable value cached, fill one in. */
245 #define pk_is_disabled(a) (((a)->is_disabled)?((a)->is_disabled==2):(cache_disabled_value((a))))
247 typedef struct {
248 u32 timestamp; /* key made */
249 u32 expiredate; /* expires at this date or 0 if not at all */
250 u32 max_expiredate; /* must not expire past this date */
251 byte hdrbytes; /* number of header bytes */
252 byte version;
253 byte pubkey_algo; /* algorithm used for public key scheme */
254 byte pubkey_usage;
255 byte req_usage;
256 byte req_algo;
257 u32 has_expired; /* set to the expiration date if expired */
258 int is_revoked; /* key has been revoked */
259 int is_valid; /* key (especially subkey) is valid */
260 u32 main_keyid[2]; /* keyid of the primary key */
261 u32 keyid[2];
262 byte is_primary;
263 byte is_protected; /* The secret info is protected and must */
264 /* be decrypted before use, the protected */
265 /* MPIs are simply (void*) pointers to memory */
266 /* and should never be passed to a mpi_xxx() */
267 struct {
268 byte algo; /* cipher used to protect the secret information*/
269 byte sha1chk; /* SHA1 is used instead of a 16 bit checksum */
270 STRING2KEY s2k;
271 byte ivlen; /* used length of the iv */
272 byte iv[16]; /* initialization vector for CFB mode */
273 } protect;
274 gcry_mpi_t skey[PUBKEY_MAX_NSKEY];
275 u16 csum; /* checksum */
276 } PKT_secret_key;
279 typedef struct {
280 int len; /* length of data */
281 char data[1];
282 } PKT_comment;
284 typedef struct {
285 u32 len; /* reserved */
286 byte new_ctb;
287 byte algorithm;
288 iobuf_t buf; /* IOBUF reference */
289 } PKT_compressed;
291 typedef struct {
292 u32 len; /* length of encrypted data */
293 int extralen; /* this is (blocksize+2) */
294 byte new_ctb; /* uses a new CTB */
295 byte is_partial; /* partial length encoded */
296 byte mdc_method; /* > 0: integrity protected encrypted data packet */
297 iobuf_t buf; /* IOBUF reference */
298 } PKT_encrypted;
300 typedef struct {
301 byte hash[20];
302 } PKT_mdc;
304 typedef struct {
305 unsigned int trustval;
306 unsigned int sigcache;
307 } PKT_ring_trust;
309 typedef struct {
310 u32 len; /* length of encrypted data */
311 iobuf_t buf; /* IOBUF reference */
312 byte new_ctb;
313 byte is_partial; /* partial length encoded */
314 int mode;
315 u32 timestamp;
316 int namelen;
317 char name[1];
318 } PKT_plaintext;
320 typedef struct {
321 int control;
322 size_t datalen;
323 char data[1];
324 } PKT_gpg_control;
326 /* combine all packets into a union */
327 struct packet_struct {
328 pkttype_t pkttype;
329 union {
330 void *generic;
331 PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */
332 PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */
333 PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */
334 PKT_signature *signature; /* PKT_SIGNATURE */
335 PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */
336 PKT_secret_key *secret_key; /* PKT_SECRET_[SUB]KEY */
337 PKT_comment *comment; /* PKT_COMMENT */
338 PKT_user_id *user_id; /* PKT_USER_ID */
339 PKT_compressed *compressed; /* PKT_COMPRESSED */
340 PKT_encrypted *encrypted; /* PKT_ENCRYPTED[_MDC] */
341 PKT_mdc *mdc; /* PKT_MDC */
342 PKT_ring_trust *ring_trust; /* PKT_RING_TRUST */
343 PKT_plaintext *plaintext; /* PKT_PLAINTEXT */
344 PKT_gpg_control *gpg_control; /* PKT_GPG_CONTROL */
345 } pkt;
348 #define init_packet(a) do { (a)->pkttype = 0; \
349 (a)->pkt.generic = NULL; \
350 } while(0)
353 struct notation
355 char *name;
356 char *value;
357 char *altvalue;
358 unsigned char *bdat;
359 size_t blen;
360 struct
362 unsigned int critical:1;
363 unsigned int ignore:1;
364 } flags;
365 struct notation *next;
368 /*-- mainproc.c --*/
369 void reset_literals_seen(void);
370 int proc_packets( void *ctx, iobuf_t a );
371 int proc_signature_packets( void *ctx, iobuf_t a,
372 strlist_t signedfiles, const char *sigfile );
373 int proc_signature_packets_by_fd ( void *anchor, IOBUF a, int signed_data_fd );
374 int proc_encryption_packets( void *ctx, iobuf_t a );
375 int list_packets( iobuf_t a );
377 /*-- parse-packet.c --*/
378 int set_packet_list_mode( int mode );
380 #if DEBUG_PARSE_PACKET
381 int dbg_search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid,
382 const char* file, int lineno );
383 int dbg_parse_packet( iobuf_t inp, PACKET *ret_pkt,
384 const char* file, int lineno );
385 int dbg_copy_all_packets( iobuf_t inp, iobuf_t out,
386 const char* file, int lineno );
387 int dbg_copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff,
388 const char* file, int lineno );
389 int dbg_skip_some_packets( iobuf_t inp, unsigned n,
390 const char* file, int lineno );
391 #define search_packet( a,b,c,d ) \
392 dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ )
393 #define parse_packet( a, b ) \
394 dbg_parse_packet( (a), (b), __FILE__, __LINE__ )
395 #define copy_all_packets( a,b ) \
396 dbg_copy_all_packets((a),(b), __FILE__, __LINE__ )
397 #define copy_some_packets( a,b,c ) \
398 dbg_copy_some_packets((a),(b),(c), __FILE__, __LINE__ )
399 #define skip_some_packets( a,b ) \
400 dbg_skip_some_packets((a),(b), __FILE__, __LINE__ )
401 #else
402 int search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid );
403 int parse_packet( iobuf_t inp, PACKET *ret_pkt);
404 int copy_all_packets( iobuf_t inp, iobuf_t out );
405 int copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff );
406 int skip_some_packets( iobuf_t inp, unsigned n );
407 #endif
409 int parse_signature( iobuf_t inp, int pkttype, unsigned long pktlen,
410 PKT_signature *sig );
411 const byte *enum_sig_subpkt ( const subpktarea_t *subpkts,
412 sigsubpkttype_t reqtype,
413 size_t *ret_n, int *start, int *critical );
414 const byte *parse_sig_subpkt ( const subpktarea_t *buffer,
415 sigsubpkttype_t reqtype,
416 size_t *ret_n );
417 const byte *parse_sig_subpkt2 ( PKT_signature *sig,
418 sigsubpkttype_t reqtype,
419 size_t *ret_n );
420 int parse_one_sig_subpkt( const byte *buffer, size_t n, int type );
421 void parse_revkeys(PKT_signature *sig);
422 int parse_attribute_subpkts(PKT_user_id *uid);
423 void make_attribute_uidname(PKT_user_id *uid, size_t max_namelen);
424 PACKET *create_gpg_control ( ctrlpkttype_t type,
425 const byte *data,
426 size_t datalen );
428 /*-- build-packet.c --*/
429 int build_packet( iobuf_t inp, PACKET *pkt );
430 u32 calc_packet_length( PACKET *pkt );
431 void build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type,
432 const byte *buffer, size_t buflen );
433 void build_sig_subpkt_from_sig( PKT_signature *sig );
434 int delete_sig_subpkt(subpktarea_t *buffer, sigsubpkttype_t type );
435 void build_attribute_subpkt(PKT_user_id *uid,byte type,
436 const void *buf,u32 buflen,
437 const void *header,u32 headerlen);
438 struct notation *string_to_notation(const char *string,int is_utf8);
439 struct notation *sig_to_notation(PKT_signature *sig);
440 void free_notation(struct notation *notation);
442 /*-- free-packet.c --*/
443 void free_symkey_enc( PKT_symkey_enc *enc );
444 void free_pubkey_enc( PKT_pubkey_enc *enc );
445 void free_seckey_enc( PKT_signature *enc );
446 int digest_algo_from_sig( PKT_signature *sig );
447 void release_public_key_parts( PKT_public_key *pk );
448 void free_public_key( PKT_public_key *key );
449 void release_secret_key_parts( PKT_secret_key *sk );
450 void free_secret_key( PKT_secret_key *sk );
451 void free_attributes(PKT_user_id *uid);
452 void free_user_id( PKT_user_id *uid );
453 void free_comment( PKT_comment *rem );
454 void free_packet( PACKET *pkt );
455 prefitem_t *copy_prefs (const prefitem_t *prefs);
456 PKT_public_key *copy_public_key( PKT_public_key *d, PKT_public_key *s );
457 void copy_public_parts_to_secret_key( PKT_public_key *pk, PKT_secret_key *sk );
458 PKT_secret_key *copy_secret_key( PKT_secret_key *d, PKT_secret_key *s );
459 PKT_signature *copy_signature( PKT_signature *d, PKT_signature *s );
460 PKT_user_id *scopy_user_id (PKT_user_id *sd );
461 int cmp_public_keys( PKT_public_key *a, PKT_public_key *b );
462 int cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b );
463 int cmp_signatures( PKT_signature *a, PKT_signature *b );
464 int cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk );
465 int cmp_user_ids( PKT_user_id *a, PKT_user_id *b );
468 /*-- sig-check.c --*/
469 int signature_check( PKT_signature *sig, gcry_md_hd_t digest );
470 int signature_check2( PKT_signature *sig, gcry_md_hd_t digest, u32 *r_expiredate,
471 int *r_expired, int *r_revoked, PKT_public_key *ret_pk );
473 /*-- seckey-cert.c --*/
474 int is_secret_key_protected( PKT_secret_key *sk );
475 int check_secret_key( PKT_secret_key *sk, int retries );
476 int protect_secret_key( PKT_secret_key *sk, DEK *dek );
478 /*-- pubkey-enc.c --*/
479 int get_session_key( PKT_pubkey_enc *k, DEK *dek );
480 int get_override_session_key( DEK *dek, const char *string );
482 /*-- compress.c --*/
483 int handle_compressed( void *ctx, PKT_compressed *cd,
484 int (*callback)(iobuf_t, void *), void *passthru );
486 /*-- encr-data.c --*/
487 int decrypt_data( void *ctx, PKT_encrypted *ed, DEK *dek );
489 /*-- plaintext.c --*/
490 int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
491 int nooutput, int clearsig );
492 int ask_for_detached_datafile( gcry_md_hd_t md, gcry_md_hd_t md2,
493 const char *inname, int textmode );
495 /*-- sign.c --*/
496 int make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
497 PKT_user_id *uid, PKT_public_key *subpk,
498 PKT_secret_key *sk, int sigclass, int digest_algo,
499 int sigversion, u32 timestamp, u32 duration,
500 int (*mksubpkt)(PKT_signature *, void *),
501 void *opaque );
502 int update_keysig_packet( PKT_signature **ret_sig,
503 PKT_signature *orig_sig,
504 PKT_public_key *pk,
505 PKT_user_id *uid,
506 PKT_public_key *subpk,
507 PKT_secret_key *sk,
508 int (*mksubpkt)(PKT_signature *, void *),
509 void *opaque );
511 /*-- keygen.c --*/
512 PKT_user_id *generate_user_id (KBNODE keyblock);
514 #endif /*G10_PACKET_H*/