2006-09-06 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / packet.h
blob2aaf3b902a320065e733a773f624d9917238057d
1 /* packet.h - OpenPGP packet definitions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 * 2006 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #ifndef G10_PACKET_H
24 #define G10_PACKET_H
26 #include "types.h"
27 #include "../common/iobuf.h"
28 #include "../jnlib/strlist.h"
29 #include "cipher.h"
30 #include "filter.h"
31 #include "../common/openpgpdefs.h"
33 #define DEBUG_PARSE_PACKET 1
36 typedef struct packet_struct PACKET;
38 /* PKT_GPG_CONTROL types */
39 typedef enum {
40 CTRLPKT_CLEARSIGN_START = 1,
41 CTRLPKT_PIPEMODE = 2,
42 CTRLPKT_PLAINTEXT_MARK =3
43 } ctrlpkttype_t;
45 typedef enum {
46 PREFTYPE_NONE = 0,
47 PREFTYPE_SYM = 1,
48 PREFTYPE_HASH = 2,
49 PREFTYPE_ZIP = 3
50 } preftype_t;
52 typedef struct {
53 byte type;
54 byte value;
55 } prefitem_t;
57 typedef struct {
58 int mode;
59 byte hash_algo;
60 byte salt[8];
61 u32 count;
62 } STRING2KEY;
64 typedef struct {
65 byte version;
66 byte cipher_algo; /* cipher algorithm used */
67 STRING2KEY s2k;
68 byte seskeylen; /* keylength in byte or 0 for no seskey */
69 byte seskey[1];
70 } PKT_symkey_enc;
72 typedef struct {
73 u32 keyid[2]; /* 64 bit keyid */
74 byte version;
75 byte pubkey_algo; /* algorithm used for public key scheme */
76 byte throw_keyid;
77 gcry_mpi_t data[PUBKEY_MAX_NENC];
78 } PKT_pubkey_enc;
81 typedef struct {
82 u32 keyid[2]; /* 64 bit keyid */
83 byte sig_class; /* sig classification */
84 byte digest_algo; /* algorithm used for digest */
85 byte pubkey_algo; /* algorithm used for public key scheme */
86 byte last; /* a stupid flag */
87 } PKT_onepass_sig;
90 typedef struct {
91 size_t size; /* allocated */
92 size_t len; /* used */
93 byte data[1];
94 } subpktarea_t;
96 struct revocation_key {
97 byte class;
98 byte algid;
99 byte fpr[MAX_FINGERPRINT_LEN];
103 /* Object to keep information about a PKA DNS record. */
104 typedef struct
106 int valid; /* An actual PKA record exists for EMAIL. */
107 int checked; /* Set to true if the FPR has been checked against the
108 actual key. */
109 char *uri; /* Malloced string with the URI. NULL if the URI is
110 not available.*/
111 unsigned char fpr[20]; /* The fingerprint as stored in the PKA RR. */
112 char email[1];/* The email address from the notation data. */
113 } pka_info_t;
116 /* Object to keep information pertaining to a signature. */
117 typedef struct
119 struct
121 unsigned checked:1; /* Signature has been checked. */
122 unsigned valid:1; /* Signature is good (if checked is set). */
123 unsigned chosen_selfsig:1; /* A selfsig that is the chosen one. */
124 unsigned unknown_critical:1;
125 unsigned exportable:1;
126 unsigned revocable:1;
127 unsigned policy_url:1; /* At least one policy URL is present */
128 unsigned notation:1; /* At least one notation is present */
129 unsigned pref_ks:1; /* At least one preferred keyserver is present */
130 unsigned expired:1;
131 unsigned pka_tried:1; /* Set if we tried to retrieve the PKA record. */
132 } flags;
133 u32 keyid[2]; /* 64 bit keyid */
134 u32 timestamp; /* Signature made (seconds since Epoch). */
135 u32 expiredate; /* Expires at this date or 0 if not at all. */
136 byte version;
137 byte sig_class; /* Sig classification, append for MD calculation. */
138 byte pubkey_algo; /* Algorithm used for public key scheme */
139 /* (PUBKEY_ALGO_xxx) */
140 byte digest_algo; /* Algorithm used for digest (DIGEST_ALGO_xxxx). */
141 byte trust_depth;
142 byte trust_value;
143 const byte *trust_regexp;
144 struct revocation_key **revkey;
145 int numrevkeys;
146 pka_info_t *pka_info; /* Malloced PKA data or NULL if not
147 available. See also flags.pka_tried. */
148 subpktarea_t *hashed; /* All subpackets with hashed data (v4 only). */
149 subpktarea_t *unhashed; /* Ditto for unhashed data. */
150 byte digest_start[2]; /* First 2 bytes of the digest. */
151 gcry_mpi_t data[PUBKEY_MAX_NSIG];
152 } PKT_signature;
154 #define ATTRIB_IMAGE 1
156 /* This is the cooked form of attributes */
157 struct user_attribute {
158 byte type;
159 const byte *data;
160 u32 len;
163 typedef struct
165 int ref; /* reference counter */
166 int len; /* length of the name */
167 struct user_attribute *attribs;
168 int numattribs;
169 byte *attrib_data; /* if this is not NULL, the packet is an attribute */
170 unsigned long attrib_len;
171 byte *namehash;
172 int help_key_usage;
173 u32 help_key_expire;
174 int help_full_count;
175 int help_marginal_count;
176 int is_primary; /* 2 if set via the primary flag, 1 if calculated */
177 int is_revoked;
178 int is_expired;
179 u32 expiredate; /* expires at this date or 0 if not at all */
180 prefitem_t *prefs; /* list of preferences (may be NULL)*/
181 u32 created; /* according to the self-signature */
182 byte selfsigversion;
183 struct
185 /* TODO: Move more flags here */
186 unsigned mdc:1;
187 unsigned ks_modify:1;
188 unsigned compacted:1;
189 } flags;
190 char name[1];
191 } PKT_user_id;
193 struct revoke_info
195 /* revoked at this date */
196 u32 date;
197 /* the keyid of the revoking key (selfsig or designated revoker) */
198 u32 keyid[2];
199 /* the algo of the revoking key */
200 byte algo;
203 /****************
204 * Note about the pkey/skey elements: We assume that the secret keys
205 * has the same elemts as the public key at the begin of the array, so
206 * that npkey < nskey and it is possible to compare the secret and
207 * public keys by comparing the first npkey elements of pkey againts skey.
209 typedef struct {
210 u32 timestamp; /* key made */
211 u32 expiredate; /* expires at this date or 0 if not at all */
212 u32 max_expiredate; /* must not expire past this date */
213 struct revoke_info revoked;
214 byte hdrbytes; /* number of header bytes */
215 byte version;
216 byte selfsigversion; /* highest version of all of the self-sigs */
217 byte pubkey_algo; /* algorithm used for public key scheme */
218 byte pubkey_usage; /* for now only used to pass it to getkey() */
219 byte req_usage; /* hack to pass a request to getkey() */
220 byte req_algo; /* Ditto */
221 u32 has_expired; /* set to the expiration date if expired */
222 int is_revoked; /* key has been revoked, 1 if by the
223 owner, 2 if by a designated revoker */
224 int maybe_revoked; /* a designated revocation is present, but
225 without the key to check it */
226 int is_valid; /* key (especially subkey) is valid */
227 int dont_cache; /* do not cache this */
228 byte backsig; /* 0=none, 1=bad, 2=good */
229 u32 main_keyid[2]; /* keyid of the primary key */
230 u32 keyid[2]; /* calculated by keyid_from_pk() */
231 byte is_primary;
232 byte is_disabled; /* 0 for unset, 1 for enabled, 2 for disabled. */
233 prefitem_t *prefs; /* list of preferences (may be NULL) */
234 int mdc_feature; /* mdc feature set */
235 PKT_user_id *user_id; /* if != NULL: found by that uid */
236 struct revocation_key *revkey;
237 int numrevkeys;
238 u32 trust_timestamp;
239 byte trust_depth;
240 byte trust_value;
241 const byte *trust_regexp;
242 gcry_mpi_t pkey[PUBKEY_MAX_NPKEY];
243 } PKT_public_key;
245 /* Evaluates as true if the pk is disabled, and false if it isn't. If
246 there is no disable value cached, fill one in. */
247 #define pk_is_disabled(a) (((a)->is_disabled)?((a)->is_disabled==2):(cache_disabled_value((a))))
249 typedef struct {
250 u32 timestamp; /* key made */
251 u32 expiredate; /* expires at this date or 0 if not at all */
252 u32 max_expiredate; /* must not expire past this date */
253 byte hdrbytes; /* number of header bytes */
254 byte version;
255 byte pubkey_algo; /* algorithm used for public key scheme */
256 byte pubkey_usage;
257 byte req_usage;
258 byte req_algo;
259 u32 has_expired; /* set to the expiration date if expired */
260 int is_revoked; /* key has been revoked */
261 int is_valid; /* key (especially subkey) is valid */
262 u32 main_keyid[2]; /* keyid of the primary key */
263 u32 keyid[2];
264 byte is_primary;
265 byte is_protected; /* The secret info is protected and must */
266 /* be decrypted before use, the protected */
267 /* MPIs are simply (void*) pointers to memory */
268 /* and should never be passed to a mpi_xxx() */
269 struct {
270 byte algo; /* cipher used to protect the secret information*/
271 byte sha1chk; /* SHA1 is used instead of a 16 bit checksum */
272 STRING2KEY s2k;
273 byte ivlen; /* used length of the iv */
274 byte iv[16]; /* initialization vector for CFB mode */
275 } protect;
276 gcry_mpi_t skey[PUBKEY_MAX_NSKEY];
277 u16 csum; /* checksum */
278 } PKT_secret_key;
281 typedef struct {
282 int len; /* length of data */
283 char data[1];
284 } PKT_comment;
286 typedef struct {
287 u32 len; /* reserved */
288 byte new_ctb;
289 byte algorithm;
290 iobuf_t buf; /* IOBUF reference */
291 } PKT_compressed;
293 typedef struct {
294 u32 len; /* length of encrypted data */
295 int extralen; /* this is (blocksize+2) */
296 byte new_ctb; /* uses a new CTB */
297 byte is_partial; /* partial length encoded */
298 byte mdc_method; /* > 0: integrity protected encrypted data packet */
299 iobuf_t buf; /* IOBUF reference */
300 } PKT_encrypted;
302 typedef struct {
303 byte hash[20];
304 } PKT_mdc;
306 typedef struct {
307 unsigned int trustval;
308 unsigned int sigcache;
309 } PKT_ring_trust;
311 typedef struct {
312 u32 len; /* length of encrypted data */
313 iobuf_t buf; /* IOBUF reference */
314 byte new_ctb;
315 byte is_partial; /* partial length encoded */
316 int mode;
317 u32 timestamp;
318 int namelen;
319 char name[1];
320 } PKT_plaintext;
322 typedef struct {
323 int control;
324 size_t datalen;
325 char data[1];
326 } PKT_gpg_control;
328 /* combine all packets into a union */
329 struct packet_struct {
330 pkttype_t pkttype;
331 union {
332 void *generic;
333 PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */
334 PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */
335 PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */
336 PKT_signature *signature; /* PKT_SIGNATURE */
337 PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */
338 PKT_secret_key *secret_key; /* PKT_SECRET_[SUB]KEY */
339 PKT_comment *comment; /* PKT_COMMENT */
340 PKT_user_id *user_id; /* PKT_USER_ID */
341 PKT_compressed *compressed; /* PKT_COMPRESSED */
342 PKT_encrypted *encrypted; /* PKT_ENCRYPTED[_MDC] */
343 PKT_mdc *mdc; /* PKT_MDC */
344 PKT_ring_trust *ring_trust; /* PKT_RING_TRUST */
345 PKT_plaintext *plaintext; /* PKT_PLAINTEXT */
346 PKT_gpg_control *gpg_control; /* PKT_GPG_CONTROL */
347 } pkt;
350 #define init_packet(a) do { (a)->pkttype = 0; \
351 (a)->pkt.generic = NULL; \
352 } while(0)
355 struct notation
357 char *name;
358 char *value;
359 char *altvalue;
360 unsigned char *bdat;
361 size_t blen;
362 struct
364 unsigned int critical:1;
365 unsigned int ignore:1;
366 } flags;
367 struct notation *next;
370 /*-- mainproc.c --*/
371 int proc_packets( void *ctx, iobuf_t a );
372 int proc_signature_packets( void *ctx, iobuf_t a,
373 STRLIST signedfiles, const char *sigfile );
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(void);
514 #endif /*G10_PACKET_H*/