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
29 #include "../common/iobuf.h"
30 #include "../jnlib/strlist.h"
35 #define DEBUG_PARSE_PACKET 1
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 */
61 typedef struct packet_struct PACKET
;
63 /* PKT_GPG_CONTROL types */
65 CTRLPKT_CLEARSIGN_START
= 1,
67 CTRLPKT_PLAINTEXT_MARK
=3
91 byte cipher_algo
; /* cipher algorithm used */
93 byte seskeylen
; /* keylength in byte or 0 for no seskey */
98 u32 keyid
[2]; /* 64 bit keyid */
100 byte pubkey_algo
; /* algorithm used for public key scheme */
102 gcry_mpi_t data
[PUBKEY_MAX_NENC
];
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 */
116 size_t size
; /* allocated */
117 size_t len
; /* used */
121 struct revocation_key
{
124 byte fpr
[MAX_FINGERPRINT_LEN
];
128 ulong local_id
; /* internal use, valid if > 0 */
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 */
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 */
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) */
150 const byte
*trust_regexp
;
151 struct revocation_key
**revkey
;
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
];
159 #define ATTRIB_IMAGE 1
161 /* This is the cooked form of attributes */
162 struct user_attribute
{
169 int ref
; /* reference counter */
170 int len
; /* length of the name */
171 struct user_attribute
*attribs
;
173 byte
*attrib_data
; /* if this is not NULL, the packet is an attribute */
174 unsigned long attrib_len
;
179 int help_marginal_count
;
180 int is_primary
; /* 2 if set via the primary flag, 1 if calculated */
183 u32 expiredate
; /* expires at this date or 0 if not at all */
184 prefitem_t
*prefs
; /* list of preferences (may be NULL)*/
187 u32 created
; /* according to the self-signature */
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.
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 */
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() */
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
;
227 const byte
*trust_regexp
;
228 gcry_mpi_t pkey
[PUBKEY_MAX_NPKEY
];
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))))
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 */
241 byte pubkey_algo
; /* algorithm used for public key scheme */
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 */
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() */
256 byte algo
; /* cipher used to protect the secret information*/
257 byte sha1chk
; /* SHA1 is used instead of a 16 bit checksum */
259 byte ivlen
; /* used length of the iv */
260 byte iv
[16]; /* initialization vector for CFB mode */
262 gcry_mpi_t skey
[PUBKEY_MAX_NSKEY
];
263 u16 csum
; /* checksum */
268 int len
; /* length of data */
273 u32 len
; /* reserved */
276 iobuf_t buf
; /* iobuf_t reference */
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 */
292 unsigned int trustval
;
293 unsigned int sigcache
;
297 u32 len
; /* length of encrypted data */
298 iobuf_t buf
; /* iobuf_t reference */
300 byte is_partial
; /* partial length encoded */
313 /* combine all packets into a union */
314 struct packet_struct
{
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 */
335 #define init_packet(a) do { (a)->pkttype = 0; \
336 (a)->pkt.generic = NULL; \
340 SIGSUBPKT_TEST_CRITICAL
=-3,
341 SIGSUBPKT_LIST_UNHASHED
=-2,
342 SIGSUBPKT_LIST_HASHED
=-1,
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
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__ )
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
);
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
,
416 const byte
*parse_sig_subpkt2 ( PKT_signature
*sig
,
417 sigsubpkttype_t reqtype
,
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
,
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
);
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
);
493 int write_comment( iobuf_t out
, const char *s
);
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 *),
502 int update_keysig_packet( PKT_signature
**ret_sig
,
503 PKT_signature
*orig_sig
,
506 PKT_public_key
*subpk
,
508 int (*mksubpkt
)(PKT_signature
*, void *),
512 PKT_user_id
*generate_user_id(void);
514 #endif /*G10_PACKET_H*/