2004-02-26 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / packet.h
blob2d87c9c7d0859a5a540bd9ba91151752d1c47460
1 /* packet.h - packet definitions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 #ifndef G10_PACKET_H
23 #define G10_PACKET_H
25 #include "gpg.h"
26 #include <gcrypt.h>
28 #include "types.h"
29 #include "../common/iobuf.h"
30 #include "../jnlib/strlist.h"
31 #include "cipher.h"
32 #include "filter.h"
33 #include "global.h"
35 #define DEBUG_PARSE_PACKET 1
37 typedef enum {
38 PKT_NONE =0,
39 PKT_PUBKEY_ENC =1, /* public key encrypted packet */
40 PKT_SIGNATURE =2, /* secret key encrypted packet */
41 PKT_SYMKEY_ENC =3, /* session key packet (OpenPGP)*/
42 PKT_ONEPASS_SIG =4, /* one pass sig packet (OpenPGP)*/
43 PKT_SECRET_KEY =5, /* secret key */
44 PKT_PUBLIC_KEY =6, /* public key */
45 PKT_SECRET_SUBKEY =7, /* secret subkey (OpenPGP) */
46 PKT_COMPRESSED =8, /* compressed data packet */
47 PKT_ENCRYPTED =9, /* conventional encrypted data */
48 PKT_MARKER =10, /* marker packet (OpenPGP) */
49 PKT_PLAINTEXT =11, /* plaintext data with filename and mode */
50 PKT_RING_TRUST =12, /* keyring trust packet */
51 PKT_USER_ID =13, /* user id packet */
52 PKT_PUBLIC_SUBKEY =14, /* public subkey (OpenPGP) */
53 PKT_OLD_COMMENT =16, /* comment packet from an OpenPGP draft */
54 PKT_ATTRIBUTE =17, /* PGP's attribute packet */
55 PKT_ENCRYPTED_MDC =18, /* integrity protected encrypted data */
56 PKT_MDC =19, /* manipulation detection code packet */
57 PKT_COMMENT =61, /* new comment packet (private) */
58 PKT_GPG_CONTROL =63 /* internal control packet */
59 } pkttype_t;
61 typedef struct packet_struct PACKET;
63 /* PKT_GPG_CONTROL types */
64 typedef enum {
65 CTRLPKT_CLEARSIGN_START = 1,
66 CTRLPKT_PIPEMODE = 2,
67 CTRLPKT_PLAINTEXT_MARK =3
68 } ctrlpkttype_t;
70 typedef enum {
71 PREFTYPE_NONE = 0,
72 PREFTYPE_SYM = 1,
73 PREFTYPE_HASH = 2,
74 PREFTYPE_ZIP = 3
75 } preftype_t;
77 typedef struct {
78 byte type;
79 byte value;
80 } prefitem_t;
82 typedef struct {
83 int mode;
84 byte hash_algo;
85 byte salt[8];
86 u32 count;
87 } STRING2KEY;
89 typedef struct {
90 byte version;
91 byte cipher_algo; /* cipher algorithm used */
92 STRING2KEY s2k;
93 byte seskeylen; /* keylength in byte or 0 for no seskey */
94 byte seskey[1];
95 } PKT_symkey_enc;
97 typedef struct {
98 u32 keyid[2]; /* 64 bit keyid */
99 byte version;
100 byte pubkey_algo; /* algorithm used for public key scheme */
101 byte throw_keyid;
102 gcry_mpi_t data[PUBKEY_MAX_NENC];
103 } PKT_pubkey_enc;
106 typedef struct {
107 u32 keyid[2]; /* 64 bit keyid */
108 byte sig_class; /* sig classification */
109 byte digest_algo; /* algorithm used for digest */
110 byte pubkey_algo; /* algorithm used for public key scheme */
111 byte last; /* a stupid flag */
112 } PKT_onepass_sig;
115 typedef struct {
116 size_t size; /* allocated */
117 size_t len; /* used */
118 byte data[1];
119 } subpktarea_t;
121 struct revocation_key {
122 byte class;
123 byte algid;
124 byte fpr[MAX_FINGERPRINT_LEN];
127 typedef struct {
128 ulong local_id; /* internal use, valid if > 0 */
129 struct {
130 unsigned checked:1; /* signature has been checked */
131 unsigned valid:1; /* signature is good (if checked is set) */
132 unsigned unknown_critical:1;
133 unsigned exportable:1;
134 unsigned revocable:1;
135 unsigned policy_url:1; /* At least one policy URL is present */
136 unsigned notation:1; /* At least one notation is present */
137 unsigned pref_ks:1; /* At least one preferred keyserver is present */
138 unsigned expired:1;
139 } flags;
140 u32 keyid[2]; /* 64 bit keyid */
141 u32 timestamp; /* signature made */
142 u32 expiredate; /* expires at this date or 0 if not at all */
143 byte version;
144 byte sig_class; /* sig classification, append for MD calculation*/
145 byte pubkey_algo; /* algorithm used for public key scheme */
146 /* (PUBKEY_ALGO_xxx) */
147 byte digest_algo; /* algorithm used for digest (DIGEST_ALGO_xxxx) */
148 byte trust_depth;
149 byte trust_value;
150 const byte *trust_regexp;
151 struct revocation_key **revkey;
152 int numrevkeys;
153 subpktarea_t *hashed; /* all subpackets with hashed data (v4 only) */
154 subpktarea_t *unhashed; /* ditto for unhashed data */
155 byte digest_start[2]; /* first 2 bytes of the digest */
156 gcry_mpi_t data[PUBKEY_MAX_NSIG];
157 } PKT_signature;
159 #define ATTRIB_IMAGE 1
161 /* This is the cooked form of attributes */
162 struct user_attribute {
163 byte type;
164 const byte *data;
165 u32 len;
168 typedef struct {
169 int ref; /* reference counter */
170 int len; /* length of the name */
171 struct user_attribute *attribs;
172 int numattribs;
173 byte *attrib_data; /* if this is not NULL, the packet is an attribute */
174 unsigned long attrib_len;
175 byte *namehash;
176 int help_key_usage;
177 u32 help_key_expire;
178 int help_full_count;
179 int help_marginal_count;
180 int is_primary; /* 2 if set via the primary flag, 1 if calculated */
181 int is_revoked;
182 int is_expired;
183 u32 expiredate; /* expires at this date or 0 if not at all */
184 prefitem_t *prefs; /* list of preferences (may be NULL)*/
185 int mdc_feature;
186 int ks_modify;
187 u32 created; /* according to the self-signature */
188 byte selfsigversion;
189 char name[1];
190 } PKT_user_id;
193 /****************
194 * Note about the pkey/skey elements: We assume that the secret keys
195 * has the same elemts as the public key at the begin of the array, so
196 * that npkey < nskey and it is possible to compare the secret and
197 * public keys by comparing the first npkey elements of pkey against skey.
199 typedef struct {
200 u32 timestamp; /* key made */
201 u32 expiredate; /* expires at this date or 0 if not at all */
202 u32 max_expiredate; /* must not expire past this date */
203 byte hdrbytes; /* number of header bytes */
204 byte version;
205 byte selfsigversion; /* highest version of all of the self-sigs */
206 byte pubkey_algo; /* algorithm used for public key scheme */
207 byte pubkey_usage; /* for now only used to pass it to getkey() */
208 byte req_usage; /* hack to pass a request to getkey() */
209 byte req_algo; /* Ditto */
210 u32 has_expired; /* set to the expiration date if expired */
211 int is_revoked; /* key has been revoked */
212 int is_valid; /* key (especially subkey) is valid */
213 int dont_cache; /* do not cache this */
214 ulong local_id; /* internal use, valid if > 0 */
215 u32 main_keyid[2]; /* keyid of the primary key */
216 u32 keyid[2]; /* calculated by keyid_from_pk() */
217 byte is_primary;
218 byte is_disabled; /* 0 for unset, 1 for enabled, 2 for disabled. */
219 prefitem_t *prefs; /* list of preferences (may be NULL) */
220 int mdc_feature; /* mdc feature set */
221 PKT_user_id *user_id; /* if != NULL: found by that uid */
222 struct revocation_key *revkey;
223 int numrevkeys;
224 u32 trust_timestamp;
225 byte trust_depth;
226 byte trust_value;
227 const byte *trust_regexp;
228 gcry_mpi_t pkey[PUBKEY_MAX_NPKEY];
229 } PKT_public_key;
231 /* Evaluates as true if the pk is disabled, and false if it isn't. If
232 there is no disable value cached, fill one in. */
233 #define pk_is_disabled(a) (((a)->is_disabled)?((a)->is_disabled==2):(cache_disabled_value((a))))
235 typedef struct {
236 u32 timestamp; /* key made */
237 u32 expiredate; /* expires at this date or 0 if not at all */
238 u32 max_expiredate; /* must not expire past this date */
239 byte hdrbytes; /* number of header bytes */
240 byte version;
241 byte pubkey_algo; /* algorithm used for public key scheme */
242 byte pubkey_usage;
243 byte req_usage;
244 byte req_algo;
245 u32 has_expired; /* set to the expiration date if expired */
246 int is_revoked; /* key has been revoked */
247 int is_valid; /* key (especially subkey) is valid */
248 u32 main_keyid[2]; /* keyid of the primary key */
249 u32 keyid[2];
250 byte is_primary;
251 byte is_protected; /* The secret info is protected and must */
252 /* be decrypted before use, the protected */
253 /* MPIs are simply (void*) pointers to memory */
254 /* and should never be passed to a mpi_xxx() */
255 struct {
256 byte algo; /* cipher used to protect the secret information*/
257 byte sha1chk; /* SHA1 is used instead of a 16 bit checksum */
258 STRING2KEY s2k;
259 byte ivlen; /* used length of the iv */
260 byte iv[16]; /* initialization vector for CFB mode */
261 } protect;
262 gcry_mpi_t skey[PUBKEY_MAX_NSKEY];
263 u16 csum; /* checksum */
264 } PKT_secret_key;
267 typedef struct {
268 int len; /* length of data */
269 char data[1];
270 } PKT_comment;
272 typedef struct {
273 u32 len; /* reserved */
274 byte new_ctb;
275 byte algorithm;
276 iobuf_t buf; /* iobuf_t reference */
277 } PKT_compressed;
279 typedef struct {
280 u32 len; /* length of encrypted data */
281 int extralen; /* this is (blocksize+2) */
282 byte new_ctb; /* uses a new CTB */
283 byte mdc_method; /* > 0: integrity protected encrypted data packet */
284 iobuf_t buf; /* iobuf_t reference */
285 } PKT_encrypted;
287 typedef struct {
288 byte hash[20];
289 } PKT_mdc;
291 typedef struct {
292 unsigned int trustval;
293 unsigned int sigcache;
294 } PKT_ring_trust;
296 typedef struct {
297 u32 len; /* length of encrypted data */
298 iobuf_t buf; /* iobuf_t reference */
299 byte new_ctb;
300 byte is_partial; /* partial length encoded */
301 int mode;
302 u32 timestamp;
303 int namelen;
304 char name[1];
305 } PKT_plaintext;
307 typedef struct {
308 int control;
309 size_t datalen;
310 char data[1];
311 } PKT_gpg_control;
313 /* combine all packets into a union */
314 struct packet_struct {
315 pkttype_t pkttype;
316 union {
317 void *generic;
318 PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */
319 PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */
320 PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */
321 PKT_signature *signature; /* PKT_SIGNATURE */
322 PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */
323 PKT_secret_key *secret_key; /* PKT_SECRET_[SUB]KEY */
324 PKT_comment *comment; /* PKT_COMMENT */
325 PKT_user_id *user_id; /* PKT_USER_ID */
326 PKT_compressed *compressed; /* PKT_COMPRESSED */
327 PKT_encrypted *encrypted; /* PKT_ENCRYPTED[_MDC] */
328 PKT_mdc *mdc; /* PKT_MDC */
329 PKT_ring_trust *ring_trust; /* PKT_RING_TRUST */
330 PKT_plaintext *plaintext; /* PKT_PLAINTEXT */
331 PKT_gpg_control *gpg_control; /* PKT_GPG_CONTROL */
332 } pkt;
335 #define init_packet(a) do { (a)->pkttype = 0; \
336 (a)->pkt.generic = NULL; \
337 } while(0)
339 typedef enum {
340 SIGSUBPKT_TEST_CRITICAL=-3,
341 SIGSUBPKT_LIST_UNHASHED=-2,
342 SIGSUBPKT_LIST_HASHED =-1,
343 SIGSUBPKT_NONE = 0,
344 SIGSUBPKT_SIG_CREATED = 2, /* signature creation time */
345 SIGSUBPKT_SIG_EXPIRE = 3, /* signature expiration time */
346 SIGSUBPKT_EXPORTABLE = 4, /* exportable */
347 SIGSUBPKT_TRUST = 5, /* trust signature */
348 SIGSUBPKT_REGEXP = 6, /* regular expression */
349 SIGSUBPKT_REVOCABLE = 7, /* revocable */
350 SIGSUBPKT_KEY_EXPIRE = 9, /* key expiration time */
351 SIGSUBPKT_ARR =10, /* additional recipient request */
352 SIGSUBPKT_PREF_SYM =11, /* preferred symmetric algorithms */
353 SIGSUBPKT_REV_KEY =12, /* revocation key */
354 SIGSUBPKT_ISSUER =16, /* issuer key ID */
355 SIGSUBPKT_NOTATION =20, /* notation data */
356 SIGSUBPKT_PREF_HASH =21, /* preferred hash algorithms */
357 SIGSUBPKT_PREF_COMPR =22, /* preferred compression algorithms */
358 SIGSUBPKT_KS_FLAGS =23, /* key server preferences */
359 SIGSUBPKT_PREF_KS =24, /* preferred key server */
360 SIGSUBPKT_PRIMARY_UID =25, /* primary user id */
361 SIGSUBPKT_POLICY =26, /* policy URL */
362 SIGSUBPKT_KEY_FLAGS =27, /* key flags */
363 SIGSUBPKT_SIGNERS_UID =28, /* signer's user id */
364 SIGSUBPKT_REVOC_REASON =29, /* reason for revocation */
365 SIGSUBPKT_FEATURES =30, /* feature flags */
367 SIGSUBPKT_FLAG_CRITICAL=128
368 } sigsubpkttype_t;
371 /*-- mainproc.c --*/
372 int proc_packets( void *ctx, iobuf_t a );
373 int proc_signature_packets( void *ctx, iobuf_t a,
374 STRLIST signedfiles, const char *sigfile );
375 int proc_encryption_packets( void *ctx, iobuf_t a );
376 int list_packets( iobuf_t a );
378 /*-- parse-packet.c --*/
379 int set_packet_list_mode( int mode );
381 #if DEBUG_PARSE_PACKET
382 int dbg_search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid,
383 const char* file, int lineno );
384 int dbg_parse_packet( iobuf_t inp, PACKET *ret_pkt,
385 const char* file, int lineno );
386 int dbg_copy_all_packets( iobuf_t inp, iobuf_t out,
387 const char* file, int lineno );
388 int dbg_copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff,
389 const char* file, int lineno );
390 int dbg_skip_some_packets( iobuf_t inp, unsigned n,
391 const char* file, int lineno );
392 #define search_packet( a,b,c,d ) \
393 dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ )
394 #define parse_packet( a, b ) \
395 dbg_parse_packet( (a), (b), __FILE__, __LINE__ )
396 #define copy_all_packets( a,b ) \
397 dbg_copy_all_packets((a),(b), __FILE__, __LINE__ )
398 #define copy_some_packets( a,b,c ) \
399 dbg_copy_some_packets((a),(b),(c), __FILE__, __LINE__ )
400 #define skip_some_packets( a,b ) \
401 dbg_skip_some_packets((a),(b), __FILE__, __LINE__ )
402 #else
403 int search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid );
404 int parse_packet( iobuf_t inp, PACKET *ret_pkt);
405 int copy_all_packets( iobuf_t inp, iobuf_t out );
406 int copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff );
407 int skip_some_packets( iobuf_t inp, unsigned n );
408 #endif
410 const byte *enum_sig_subpkt ( const subpktarea_t *subpkts,
411 sigsubpkttype_t reqtype,
412 size_t *ret_n, int *start, int *critical );
413 const byte *parse_sig_subpkt ( const subpktarea_t *buffer,
414 sigsubpkttype_t reqtype,
415 size_t *ret_n );
416 const byte *parse_sig_subpkt2 ( PKT_signature *sig,
417 sigsubpkttype_t reqtype,
418 size_t *ret_n );
419 int parse_one_sig_subpkt( const byte *buffer, size_t n, int type );
420 void parse_revkeys(PKT_signature *sig);
421 int parse_attribute_subpkts(PKT_user_id *uid);
422 void make_attribute_uidname(PKT_user_id *uid, size_t max_namelen);
423 PACKET *create_gpg_control ( ctrlpkttype_t type,
424 const byte *data,
425 size_t datalen );
427 /*-- build-packet.c --*/
428 int build_packet( iobuf_t inp, PACKET *pkt );
429 u32 calc_packet_length( PACKET *pkt );
430 void hash_public_key( MD_HANDLE md, PKT_public_key *pk );
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);
439 /*-- free-packet.c --*/
440 void free_symkey_enc( PKT_symkey_enc *enc );
441 void free_pubkey_enc( PKT_pubkey_enc *enc );
442 void free_seckey_enc( PKT_signature *enc );
443 int digest_algo_from_sig( PKT_signature *sig );
444 void release_public_key_parts( PKT_public_key *pk );
445 void free_public_key( PKT_public_key *key );
446 void release_secret_key_parts( PKT_secret_key *sk );
447 void free_secret_key( PKT_secret_key *sk );
448 void free_attributes(PKT_user_id *uid);
449 void free_user_id( PKT_user_id *uid );
450 void free_comment( PKT_comment *rem );
451 void free_packet( PACKET *pkt );
452 prefitem_t *copy_prefs (const prefitem_t *prefs);
453 PKT_public_key *copy_public_key( PKT_public_key *d, PKT_public_key *s );
454 void copy_public_parts_to_secret_key( PKT_public_key *pk, PKT_secret_key *sk );
455 PKT_secret_key *copy_secret_key( PKT_secret_key *d, PKT_secret_key *s );
456 PKT_signature *copy_signature( PKT_signature *d, PKT_signature *s );
457 PKT_user_id *scopy_user_id (PKT_user_id *sd );
458 int cmp_public_keys( PKT_public_key *a, PKT_public_key *b );
459 int cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b );
460 int cmp_signatures( PKT_signature *a, PKT_signature *b );
461 int cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk );
462 int cmp_user_ids( PKT_user_id *a, PKT_user_id *b );
465 /*-- sig-check.c --*/
466 int signature_check( PKT_signature *sig, MD_HANDLE digest );
467 int signature_check2( PKT_signature *sig, MD_HANDLE digest, u32 *r_expiredate,
468 int *r_expired, int *r_revoked, PKT_public_key *ret_pk );
470 /*-- seckey-cert.c --*/
471 int is_secret_key_protected( PKT_secret_key *sk );
472 int check_secret_key( PKT_secret_key *sk, int retries );
473 int protect_secret_key( PKT_secret_key *sk, DEK *dek );
475 /*-- pubkey-enc.c --*/
476 int get_session_key( PKT_pubkey_enc *k, DEK *dek );
477 int get_override_session_key( DEK *dek, const char *string );
479 /*-- compress.c --*/
480 int handle_compressed( void *ctx, PKT_compressed *cd,
481 int (*callback)(iobuf_t, void *), void *passthru );
483 /*-- encr-data.c --*/
484 int decrypt_data( void *ctx, PKT_encrypted *ed, DEK *dek );
486 /*-- plaintext.c --*/
487 int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
488 int nooutput, int clearsig, int *create_failed );
489 int ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
490 const char *inname, int textmode );
492 /*-- comment.c --*/
493 int write_comment( iobuf_t out, const char *s );
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*/