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/>.
25 #include "../common/iobuf.h"
26 #include "../common/strlist.h"
29 #include "../common/openpgpdefs.h"
30 #include "../common/userids.h"
32 #define DEBUG_PARSE_PACKET 1
35 typedef struct packet_struct PACKET;
37 /* PKT_GPG_CONTROL types */
39 CTRLPKT_CLEARSIGN_START = 1,
41 CTRLPKT_PLAINTEXT_MARK =3
58 int mode; /* Must be an integer due to the GNU modes 1001 et al. */
66 byte cipher_algo; /* cipher algorithm used */
68 byte seskeylen; /* keylength in byte or 0 for no seskey */
73 u32 keyid[2]; /* 64 bit keyid */
75 byte pubkey_algo; /* algorithm used for public key scheme */
77 gcry_mpi_t data[PUBKEY_MAX_NENC];
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 */
91 size_t size; /* allocated */
92 size_t len; /* used */
96 struct revocation_key {
99 byte fpr[MAX_FINGERPRINT_LEN];
103 /* Object to keep information about a PKA DNS record. */
106 int valid; /* An actual PKA record exists for EMAIL. */
107 int checked; /* Set to true if the FPR has been checked against the
109 char *uri; /* Malloced string with the URI. NULL if the URI is
111 unsigned char fpr[20]; /* The fingerprint as stored in the PKA RR. */
112 char email[1];/* The email address from the notation data. */
116 /* Object to keep information pertaining to a signature. */
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 */
131 unsigned pka_tried:1; /* Set if we tried to retrieve the PKA record. */
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. */
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). */
143 const byte *trust_regexp;
144 struct revocation_key **revkey;
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];
154 #define ATTRIB_IMAGE 1
156 /* This is the cooked form of attributes. */
157 struct user_attribute {
164 /* (See also keybox-search-desc.h) */
165 struct gpg_pkt_user_id_s
167 int ref; /* reference counter */
168 int len; /* length of the name */
169 struct user_attribute *attribs;
171 byte *attrib_data; /* if this is not NULL, the packet is an attribute */
172 unsigned long attrib_len;
177 int help_marginal_count;
178 int is_primary; /* 2 if set via the primary flag, 1 if calculated */
181 u32 expiredate; /* expires at this date or 0 if not at all */
182 prefitem_t *prefs; /* list of preferences (may be NULL)*/
183 u32 created; /* according to the self-signature */
187 /* TODO: Move more flags here */
189 unsigned int ks_modify:1;
190 unsigned int compacted:1;
194 typedef struct gpg_pkt_user_id_s PKT_user_id;
200 /* revoked at this date */
202 /* the keyid of the revoking key (selfsig or designated revoker) */
204 /* the algo of the revoking key */
209 /* Information pertaining to secret keys. */
212 int is_protected:1; /* The secret info is protected and must */
213 /* be decrypted before use, the protected */
214 /* MPIs are simply (void*) pointers to memory */
215 /* and should never be passed to a mpi_xxx() */
216 int sha1chk:1; /* SHA1 is used instead of a 16 bit checksum */
217 u16 csum; /* Checksum for old protection modes. */
218 byte algo; /* Cipher used to protect the secret information. */
219 STRING2KEY s2k; /* S2K parameter. */
220 byte ivlen; /* Used length of the IV. */
221 byte iv[16]; /* Initialization vector for CFB mode. */
226 * We assume that secret keys have the same number of parameters as
227 * the public key and that the public parameters are the first items
228 * in the PKEY array. Thus NPKEY is always less than NSKEY and it is
229 * possible to compare the secret and public keys by comparing the
230 * first NPKEY elements of the PKEY array. Note that since GnuPG 2.1
231 * we don't use secret keys anymore directly because they are managed
232 * by gpg-agent. However for parsing OpenPGP key files we need a way
233 * to temporary store those secret keys. We do this by putting them
234 * into the public key structure and extending the PKEY field to NSKEY
235 * elements; the extra secret key information are stored in the
240 u32 timestamp; /* key made */
241 u32 expiredate; /* expires at this date or 0 if not at all */
242 u32 max_expiredate; /* must not expire past this date */
243 struct revoke_info revoked;
244 byte hdrbytes; /* number of header bytes */
246 byte selfsigversion; /* highest version of all of the self-sigs */
247 byte pubkey_algo; /* algorithm used for public key scheme */
248 byte pubkey_usage; /* for now only used to pass it to getkey() */
249 byte req_usage; /* hack to pass a request to getkey() */
250 byte req_algo; /* Ditto */
251 u32 has_expired; /* set to the expiration date if expired */
252 u32 main_keyid[2]; /* keyid of the primary key */
253 u32 keyid[2]; /* calculated by keyid_from_pk() */
254 prefitem_t *prefs; /* list of preferences (may be NULL) */
257 unsigned int mdc:1; /* MDC feature set. */
258 unsigned int disabled_valid:1;/* The next flag is valid. */
259 unsigned int disabled:1; /* The key has been disabled. */
260 unsigned int primary:1; /* This is a primary key. */
261 unsigned int revoked:2; /* Key has been revoked.
262 1 = revoked by the owner
263 2 = revoked by designated revoker. */
264 unsigned int maybe_revoked:1; /* A designated revocation is
265 present, but without the key to
267 unsigned int valid:1; /* Key (especially subkey) is valid. */
268 unsigned int dont_cache:1; /* Do not cache this key. */
269 unsigned int backsig:2; /* 0=none, 1=bad, 2=good. */
270 unsigned int serialno_valid:1;/* SERIALNO below is valid. */
272 PKT_user_id *user_id; /* If != NULL: found by that uid. */
273 struct revocation_key *revkey;
278 const byte *trust_regexp;
279 char *serialno; /* Malloced hex string or NULL if it is
280 likely not on a card. See also
281 flags.serialno_valid. */
282 struct seckey_info *seckey_info; /* If not NULL this malloced
283 structure describes a secret
285 gcry_mpi_t pkey[PUBKEY_MAX_NSKEY]; /* Right, NSKEY elements. */
288 /* Evaluates as true if the pk is disabled, and false if it isn't. If
289 there is no disable value cached, fill one in. */
290 #define pk_is_disabled(a) \
291 (((a)->flags.disabled_valid)? \
292 ((a)->flags.disabled):(cache_disabled_value((a))))
296 int len; /* length of data */
301 u32 len; /* reserved */
304 iobuf_t buf; /* IOBUF reference */
308 u32 len; /* Remaining length of encrypted data. */
309 int extralen; /* This is (blocksize+2). Used by build_packet. */
310 byte new_ctb; /* uses a new CTB */
311 byte is_partial; /* partial length encoded */
312 byte mdc_method; /* > 0: integrity protected encrypted data packet */
313 iobuf_t buf; /* IOBUF reference */
321 unsigned int trustval;
322 unsigned int sigcache;
326 u32 len; /* length of encrypted data */
327 iobuf_t buf; /* IOBUF reference */
329 byte is_partial; /* partial length encoded */
342 /* combine all packets into a union */
343 struct packet_struct {
347 PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */
348 PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */
349 PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */
350 PKT_signature *signature; /* PKT_SIGNATURE */
351 PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */
352 PKT_public_key *secret_key; /* PKT_SECRET_[SUB]KEY */
353 PKT_comment *comment; /* PKT_COMMENT */
354 PKT_user_id *user_id; /* PKT_USER_ID */
355 PKT_compressed *compressed; /* PKT_COMPRESSED */
356 PKT_encrypted *encrypted; /* PKT_ENCRYPTED[_MDC] */
357 PKT_mdc *mdc; /* PKT_MDC */
358 PKT_ring_trust *ring_trust; /* PKT_RING_TRUST */
359 PKT_plaintext *plaintext; /* PKT_PLAINTEXT */
360 PKT_gpg_control *gpg_control; /* PKT_GPG_CONTROL */
364 #define init_packet(a) do { (a)->pkttype = 0; \
365 (a)->pkt.generic = NULL; \
378 unsigned int critical:1;
379 unsigned int ignore:1;
381 struct notation *next;
385 void reset_literals_seen(void);
386 int proc_packets (ctrl_t ctrl, void *ctx, iobuf_t a );
387 int proc_signature_packets (ctrl_t ctrl, void *ctx, iobuf_t a,
388 strlist_t signedfiles, const char *sigfile );
389 int proc_signature_packets_by_fd (ctrl_t ctrl,
390 void *anchor, IOBUF a, int signed_data_fd );
391 int proc_encryption_packets (ctrl_t ctrl, void *ctx, iobuf_t a);
392 int list_packets( iobuf_t a );
394 /*-- parse-packet.c --*/
395 int set_packet_list_mode( int mode );
397 #if DEBUG_PARSE_PACKET
398 int dbg_search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid,
399 const char* file, int lineno );
400 int dbg_parse_packet( iobuf_t inp, PACKET *ret_pkt,
401 const char* file, int lineno );
402 int dbg_copy_all_packets( iobuf_t inp, iobuf_t out,
403 const char* file, int lineno );
404 int dbg_copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff,
405 const char* file, int lineno );
406 int dbg_skip_some_packets( iobuf_t inp, unsigned n,
407 const char* file, int lineno );
408 #define search_packet( a,b,c,d ) \
409 dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ )
410 #define parse_packet( a, b ) \
411 dbg_parse_packet( (a), (b), __FILE__, __LINE__ )
412 #define copy_all_packets( a,b ) \
413 dbg_copy_all_packets((a),(b), __FILE__, __LINE__ )
414 #define copy_some_packets( a,b,c ) \
415 dbg_copy_some_packets((a),(b),(c), __FILE__, __LINE__ )
416 #define skip_some_packets( a,b ) \
417 dbg_skip_some_packets((a),(b), __FILE__, __LINE__ )
419 int search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid );
420 int parse_packet( iobuf_t inp, PACKET *ret_pkt);
421 int copy_all_packets( iobuf_t inp, iobuf_t out );
422 int copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff );
423 int skip_some_packets( iobuf_t inp, unsigned n );
426 int parse_signature( iobuf_t inp, int pkttype, unsigned long pktlen,
427 PKT_signature *sig );
428 const byte *enum_sig_subpkt ( const subpktarea_t *subpkts,
429 sigsubpkttype_t reqtype,
430 size_t *ret_n, int *start, int *critical );
431 const byte *parse_sig_subpkt ( const subpktarea_t *buffer,
432 sigsubpkttype_t reqtype,
434 const byte *parse_sig_subpkt2 ( PKT_signature *sig,
435 sigsubpkttype_t reqtype,
437 int parse_one_sig_subpkt( const byte *buffer, size_t n, int type );
438 void parse_revkeys(PKT_signature *sig);
439 int parse_attribute_subpkts(PKT_user_id *uid);
440 void make_attribute_uidname(PKT_user_id *uid, size_t max_namelen);
441 PACKET *create_gpg_control ( ctrlpkttype_t type,
445 /*-- build-packet.c --*/
446 int build_packet( iobuf_t inp, PACKET *pkt );
447 gpg_error_t gpg_mpi_write (iobuf_t out, gcry_mpi_t a);
448 u32 calc_packet_length( PACKET *pkt );
449 void build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type,
450 const byte *buffer, size_t buflen );
451 void build_sig_subpkt_from_sig( PKT_signature *sig );
452 int delete_sig_subpkt(subpktarea_t *buffer, sigsubpkttype_t type );
453 void build_attribute_subpkt(PKT_user_id *uid,byte type,
454 const void *buf,u32 buflen,
455 const void *header,u32 headerlen);
456 struct notation *string_to_notation(const char *string,int is_utf8);
457 struct notation *sig_to_notation(PKT_signature *sig);
458 void free_notation(struct notation *notation);
460 /*-- free-packet.c --*/
461 void free_symkey_enc( PKT_symkey_enc *enc );
462 void free_pubkey_enc( PKT_pubkey_enc *enc );
463 void free_seckey_enc( PKT_signature *enc );
464 int digest_algo_from_sig( PKT_signature *sig );
465 void release_public_key_parts( PKT_public_key *pk );
466 void free_public_key( PKT_public_key *key );
467 void free_attributes(PKT_user_id *uid);
468 void free_user_id( PKT_user_id *uid );
469 void free_comment( PKT_comment *rem );
470 void free_packet( PACKET *pkt );
471 prefitem_t *copy_prefs (const prefitem_t *prefs);
472 PKT_public_key *copy_public_key( PKT_public_key *d, PKT_public_key *s );
473 PKT_signature *copy_signature( PKT_signature *d, PKT_signature *s );
474 PKT_user_id *scopy_user_id (PKT_user_id *sd );
475 int cmp_public_keys( PKT_public_key *a, PKT_public_key *b );
476 int cmp_signatures( PKT_signature *a, PKT_signature *b );
477 int cmp_user_ids( PKT_user_id *a, PKT_user_id *b );
480 /*-- sig-check.c --*/
481 int signature_check( PKT_signature *sig, gcry_md_hd_t digest );
482 int signature_check2( PKT_signature *sig, gcry_md_hd_t digest, u32 *r_expiredate,
483 int *r_expired, int *r_revoked, PKT_public_key *ret_pk );
486 /*-- pubkey-enc.c --*/
487 gpg_error_t get_session_key (PKT_pubkey_enc *k, DEK *dek);
488 gpg_error_t get_override_session_key (DEK *dek, const char *string);
491 int handle_compressed (ctrl_t ctrl, void *ctx, PKT_compressed *cd,
492 int (*callback)(iobuf_t, void *), void *passthru );
494 /*-- encr-data.c --*/
495 int decrypt_data (ctrl_t ctrl, void *ctx, PKT_encrypted *ed, DEK *dek );
497 /*-- plaintext.c --*/
498 int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
499 int nooutput, int clearsig );
500 int ask_for_detached_datafile( gcry_md_hd_t md, gcry_md_hd_t md2,
501 const char *inname, int textmode );
504 int make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
505 PKT_user_id *uid, PKT_public_key *subpk,
506 PKT_public_key *pksk, int sigclass, int digest_algo,
507 int sigversion, u32 timestamp, u32 duration,
508 int (*mksubpkt)(PKT_signature *, void *),
510 const char *cache_nonce);
511 int update_keysig_packet( PKT_signature **ret_sig,
512 PKT_signature *orig_sig,
515 PKT_public_key *subpk,
516 PKT_public_key *pksk,
517 int (*mksubpkt)(PKT_signature *, void *),
521 PKT_user_id *generate_user_id (KBNODE keyblock);
523 #endif /*G10_PACKET_H*/