root/openpgpsdk/trunk/src/packet.h

Revision 82 (checked in by rachel, 8 years ago)

cosmetic tidying up

  • Property svn:keywords set to Id
Line 
1 /** \file packet.h
2  * packet related headers.
3  *
4  * $Id$
5  */
6
7 #ifndef OPS_PACKET_H
8 #define OPS_PACKET_H
9
10 #include <time.h>
11 #include <openssl/bn.h>
12 #include "types.h"
13
14 /************************************/
15 /* Packet Tags - RFC2440bis-12, 4.2 */
16 /************************************/
17
18 /** Packet Tag - Bit 7 Mask (this bit is always set).
19  * The first byte of a packet is the "Packet Tag".  It always
20  * has bit 7 set.  This is the mask for it.
21  *
22  * \see RFC2440bis-12 4.2
23  */
24 #define OPS_PTAG_ALWAYS_SET             0x80
25
26 /** Packet Tag - New Format Flag.
27  * Bit 6 of the Packet Tag is the packet format indicator.
28  * If it is set, the new format is used, if cleared the
29  * old format is used.
30  *
31  * \see RFC2440bis-12 4.2
32  */
33 #define OPS_PTAG_NEW_FORMAT             0x40
34
35
36 /** Old Packet Format: Mask for content tag.
37  * In the old packet format bits 5 to 2 (including)
38  * are the content tag.  This is the mask to apply
39  * to the packet tag.  Note that you need to
40  * shift by #OPS_PTAG_OF_CONTENT_TAG_SHIFT bits.
41  *
42  * \see RFC2440bis-12 4.2
43  */
44 #define OPS_PTAG_OF_CONTENT_TAG_MASK    0x3c
45 /** Old Packet Format: Offset for the content tag.
46  * As described at #OPS_PTAG_OF_CONTENT_TAG_MASK the
47  * content tag needs to be shifted after being masked
48  * out from the Packet Tag.
49  *
50  * \see RFC2440bis-12 4.2
51  */
52 #define OPS_PTAG_OF_CONTENT_TAG_SHIFT   2
53 /** Old Packet Format: Mask for length type.
54  * Bits 1 and 0 of the packet tag are the length type
55  * in the old packet format.
56  *
57  * See #ops_ptag_of_lt_t for the meaning of the values.
58  *
59  * \see RFC2440bis-12 4.2
60  */
61 #define OPS_PTAG_OF_LENGTH_TYPE_MASK    0x03
62
63
64 /** Old Packet Format Lengths.
65  * Defines the meanings of the 2 bits for length type in the
66  * old packet format.
67  *
68  * \see RFC2440bis-12 4.2.1
69  */
70 typedef enum
71     {
72     OPS_PTAG_OF_LT_ONE_BYTE             =0x00, /*!< Packet has a 1 byte length - header is 2 bytes long. */
73     OPS_PTAG_OF_LT_TWO_BYTE             =0x01, /*!< Packet has a 2 byte length - header is 3 bytes long. */
74     OPS_PTAG_OF_LT_FOUR_BYTE            =0x02, /*!< Packet has a 4 byte length - header is 5 bytes long. */
75     OPS_PTAG_OF_LT_INDETERMINATE        =0x03  /*!< Packet has a indeterminate length. */
76     } ops_ptag_of_lt_t;
77
78
79 /** New Packet Format: Mask for content tag.
80  * In the new packet format the 6 rightmost bits
81  * are the content tag.  This is the mask to apply
82  * to the packet tag.  Note that you need to
83  * shift by #OPS_PTAG_NF_CONTENT_TAG_SHIFT bits.
84  *
85  * \see RFC2440bis-12 4.2
86  */
87 #define OPS_PTAG_NF_CONTENT_TAG_MASK    0x3f
88 /** New Packet Format: Offset for the content tag.
89  * As described at #OPS_PTAG_NF_CONTENT_TAG_MASK the
90  * content tag needs to be shifted after being masked
91  * out from the Packet Tag.
92  *
93  * \see RFC2440bis-12 4.2
94  */
95 #define OPS_PTAG_NF_CONTENT_TAG_SHIFT   0
96
97
98
99 /* PTag Content Tags */
100 /***************************/
101
102 /** Package Tags (aka Content Tags) and signatue subpacket types.
103  * This enumerates all rfc-defined packet tag values and the
104  * signature subpacket type values that we understand.
105  *
106  * \see RFC2440bis-12 4.3
107  * \see RFC2440bis-12 5.2.3.1
108  */
109 enum ops_content_tag_t
110     {
111     OPS_PTAG_CT_RESERVED                = 0,    /*!< Reserved - a packet tag must not have this value */
112     OPS_PTAG_CT_PK_SESSION_KEY          = 1,    /*!< Public-Key Encrypted Session Key Packet */
113     OPS_PTAG_CT_SIGNATURE               = 2,    /*!< Signature Packet */
114     OPS_PTAG_CT_SK_SESSION_KEY          = 3,    /*!< Symmetric-Key Encrypted Session Key Packet */
115     OPS_PTAG_CT_ONE_PASS_SIGNATURE      = 4,    /*!< One-Pass Signature Packet */
116     OPS_PTAG_CT_SECRET_KEY              = 5,    /*!< Secret Key Packet */
117     OPS_PTAG_CT_PUBLIC_KEY              = 6,    /*!< Public Key Packet */
118     OPS_PTAG_CT_SECRET_SUBKEY           = 7,    /*!< Secret Subkey Packet */
119     OPS_PTAG_CT_COMPRESSED              = 8,    /*!< Compressed Data Packet */
120     OPS_PTAG_CT_SK_DATA                 = 9,    /*!< Symmetrically Encrypted Data Packet */
121     OPS_PTAG_CT_MARKER                  =10,    /*!< Marker Packet */
122     OPS_PTAG_CT_LITERAL_DATA            =11,    /*!< Literal Data Packet */
123     OPS_PTAG_CT_TRUST                   =12,    /*!< Trust Packet */
124     OPS_PTAG_CT_USER_ID                 =13,    /*!< User ID Packet */
125     OPS_PTAG_CT_PUBLIC_SUBKEY           =14,    /*!< Public Subkey Packet */
126     OPS_PTAG_CT_RESERVED2               =15,    /*!< reserved */
127     OPS_PTAG_CT_RESERVED3               =16,    /*!< reserved */
128     OPS_PTAG_CT_USER_ATTRIBUTE          =17,    /*!< User Attribute Packet */
129     OPS_PTAG_CT_SK_IP_DATA              =18,    /*!< Sym. Encrypted and Integrity Protected Data Packet */
130     OPS_PTAG_CT_MDC                     =19,    /*!< Modification Detection Code Packet */
131
132     OPS_PARSER_ERROR                    =0x100, /*!< Internal Use: Parser Error */
133     OPS_PARSER_PTAG                     =0x101, /*!< Internal Use: The packet is the "Packet Tag" itself - used when
134                                                      callback sends back the PTag. */
135     OPS_PTAG_RAW_SS                     =0x102, /*!< Internal Use: content is raw sig subtag */
136     OPS_PTAG_SS_ALL                     =0x103, /*!< Internal Use: select all subtags */
137     OPS_PARSER_PACKET_END               =0x104,
138
139     /* signature subpackets (0x200-2ff) (type+0x200) */
140     /* only those we can parse are listed here */
141     OPS_PTAG_SIGNATURE_SUBPACKET_BASE   =0x200,         /*!< Base for signature subpacket types - All signature type
142                                                              values are relative to this value. */
143     OPS_PTAG_SS_CREATION_TIME           =0x200+2,       /*!< signature creation time */
144     OPS_PTAG_SS_EXPIRATION_TIME         =0x200+3,       /*!< signature expiration time */
145     OPS_PTAG_SS_TRUST                   =0x200+5,       /*!< trust signature */
146     OPS_PTAG_SS_REVOCABLE               =0x200+7,       /*!< revocable */
147     OPS_PTAG_SS_PREFERRED_SKA           =0x200+11,      /*!< preferred symmetric algorithms */
148     OPS_PTAG_SS_REVOCATION_KEY          =0x200+12,      /*!< revocation key */
149     OPS_PTAG_SS_ISSUER_KEY_ID           =0x200+16, /*!< issuer key ID */
150     OPS_PTAG_SS_PREFERRED_HASH          =0x200+21, /*!< preferred hash algorithms */
151     OPS_PTAG_SS_PREFERRED_COMPRESSION   =0x200+22, /*!< preferred compression algorithms */
152     OPS_PTAG_SS_PRIMARY_USER_ID         =0x200+25, /*!< primary User ID */
153     OPS_PTAG_SS_KEY_FLAGS               =0x200+27, /*!< key flags */
154     };
155
156 /** Structure to hold one parse error string. */
157 typedef struct
158     {
159     const char *error; /*!< error message. */
160     } ops_parser_error_t;
161
162 /** Structure to hold one packet tag.
163  * \see RFC2440bis-12 4.2
164  */
165 typedef struct
166     {
167     unsigned            new_format;     /*!< Whether this packet tag is new (true) or old format (false) */
168     unsigned            content_tag;    /*!< content_tag value - See #ops_content_tag_t for meanings */
169     ops_ptag_of_lt_t    length_type;    /*!< Length type (#ops_ptag_of_lt_t) - only if this packet tag is old format.  Set to 0 if new format. */
170     unsigned            length;         /*!< The length of the packet.  This value is set when we read and compute the
171                                           length information, not at the same moment we create the packet tag structure.
172                                           Only defined if #length_read is set. */  /* XXX: Ben, is this correct? */
173     //    unsigned              length_read;    /*!< How much bytes of this packet we have read so far - for internal use
174     //                                    only. */
175     } ops_ptag_t;
176
177 /** Public Key Algorithm Numbers.
178  * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP.
179  *
180  * This lists algorithm numbers for public key algorithms.
181  *
182  * \see RFC2440bis-12 9.1
183  */
184 typedef enum
185     {
186     OPS_PKA_RSA                 =1,     /*!< RSA (Encrypt or Sign) */
187     OPS_PKA_RSA_ENCRYPT_ONLY    =2,     /*!< RSA Encrypt-Only (deprecated - \see RFC2440bis-12 12.4) */
188     OPS_PKA_RSA_SIGN_ONLY       =3,     /*!< RSA Sign-Only (deprecated - \see RFC2440bis-12 12.4) */
189     OPS_PKA_ELGAMAL             =16,    /*!< Elgamal (Encrypt-Only) */
190     OPS_PKA_DSA                 =17     /*!< DSA (Digital Signature Algorithm) */
191     } ops_public_key_algorithm_t;
192
193 /** Structure to hold one DSA public key parameters.
194  *
195  * \see RFC2440bis-12 5.5.2
196  */
197 typedef struct
198     {
199     BIGNUM *p;  /*!< DSA prime p */
200     BIGNUM *q;  /*!< DSA group order q */
201     BIGNUM *g;  /*!< DSA group generator g */
202     BIGNUM *y;  /*!< DSA public key value y (= g^x mod p with x being the secret) */
203     } ops_dsa_public_key_t;
204
205 /** Structure to hold on RSA public key.
206  *
207  * \see RFC2440bis-12 5.5.2
208  */
209 typedef struct
210     {
211     BIGNUM *n;  /*!< RSA public modulus n */
212     BIGNUM *e;  /*!< RSA public encryptiong exponent e */
213     } ops_rsa_public_key_t;
214
215 /** Structure to hold on ElGamal public key parameters.
216  *
217  * \see RFC2440bis-12 5.5.2
218  */
219 typedef struct
220     {
221     BIGNUM *p;  /*!< ElGamal prime p */
222     BIGNUM *g;  /*!< ElGamal group generator g */
223     BIGNUM *y;  /*!< ElGamal public key value y (= g^x mod p with x being the secret) */
224     } ops_elgamal_public_key_t;
225
226 /** Union to hold public key parameters of any algorithm */
227 typedef union
228     {
229     ops_dsa_public_key_t        dsa;            /*!< A DSA public key */
230     ops_rsa_public_key_t        rsa;            /*!< An RSA public key */
231     ops_elgamal_public_key_t    elgamal;        /*!< An ElGamal public key */
232     } ops_public_key_union_t;
233
234 /** Structure to hold one pgp public key */
235 typedef struct
236     {
237     unsigned                    version;        /*!< version of the key (v3, v4...) */
238     time_t                      creation_time;  /*!< when the key was created.  Note that interpretation varies with key
239                                                   version. */
240     unsigned                    days_valid;     /*!< validity period of the key in days since creation.  A value of 0
241                                                   has a special meaning indicating this key does not expire.  Only
242                                                   used with v3 keys. */
243     ops_public_key_algorithm_t  algorithm;      /*!< Public Key Algorithm type */
244     ops_public_key_union_t      key;            /*!< Public Key Parameters */
245     } ops_public_key_t;
246
247 /** Symmetric Key Algorithm Numbers.
248  * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP.
249  *
250  * This lists algorithm numbers for symmetric key algorithms.
251  *
252  * \see RFC2440bis-12 9.2
253  */
254 typedef enum
255     {
256     OPS_SKA_PLAINTEXT   =0, /*!< Plaintext or unencrypted data */
257     OPS_SKA_IDEA        =1, /*!< IDEA */
258     OPS_SKA_TRIPLEDES   =2, /*!< TripleDES */
259     OPS_SKA_CAST5       =3, /*!< CAST5 */
260     OPS_SKA_BLOWFISH    =4, /*!< Blowfish */
261     OPS_SKA_AES_128     =7, /*!< AES with 128-bit key (AES) */
262     OPS_SKA_AES_192     =8, /*!< AES with 192-bit key */
263     OPS_SKA_AES_256     =9, /*!< AES with 256-bit key */
264     OPS_SKA_TWOFISH     =10, /*!< Twofish with 256-bit key (TWOFISH) */
265
266     } ops_symmetric_key_algorithm_t;
267
268 /** Structure to hold one trust packet's data */
269
270 typedef struct
271     {
272     int len;    /* length of data */
273     unsigned char * data;
274     } ops_trust_t;
275        
276 /** Structure to hold one user id */
277 typedef struct
278     {
279     char *                      user_id;        /*!< User ID string */
280     } ops_user_id_t;
281
282 /** Signature Version.
283  * OpenPGP has two different signature versions: version 3 and version 4.
284  *
285  * \see RFC2440bis-12 5.2
286  */
287 typedef enum
288     {
289     OPS_SIG_V3=3,       /*<! Version 3 Signature */
290     OPS_SIG_V4=4,       /*<! Version 4 Signature */
291     } ops_sig_version_t;
292
293 /** Signature Type.
294  * OpenPGP defines different signature types that allow giving different meanings to signatures.  Signature types
295  * include 0x10 for generitc User ID certifications (used when Ben signs Weasel's key), Subkey binding signatures,
296  * document signatures, key revocations, etc.
297  *
298  * Different types are used in different places, and most make only sense in their intended location (for instance a
299  * subkey binding has no place on a UserID).
300  *
301  * \see RFC2440bis-12 5.2.1
302  */
303 typedef enum
304     {
305     OPS_SIG_BINARY      =0x00,  /*<! Signature of a binary document */
306     OPS_SIG_TEXT        =0x01,  /*<! Signature of a canonical text document */
307     OPS_SIG_STANDALONE  =0x02,  /*<! Standalone signature */
308
309     OPS_CERT_GENERIC    =0x10,  /*<! Generic certification of a User ID and Public Key packet */
310     OPS_CERT_PERSONA    =0x11,  /*<! Persona certification of a User ID and Public Key packet */
311     OPS_CERT_CASUAL     =0x12,  /*<! Casual certification of a User ID and Public Key packet */
312     OPS_CERT_POSITIVE   =0x13,  /*<! Positive certification of a User ID and Public Key packet */
313
314     OPS_SIG_SUBKEY      =0x18,  /*<! Subkey Binding Signature */
315     OPS_SIG_PRIMARY     =0x19,  /*<! Primary Key Binding Signature */
316     OPS_SIG_DIRECT      =0x1f,  /*<! Signature directly on a key */
317
318     OPS_SIG_REV_KEY     =0x20,  /*<! Key revocation signature */
319     OPS_SIG_REV_SUBKEY  =0x28,  /*<! Subkey revocation signature */
320     OPS_SIG_REV_CERT    =0x30,  /*<! Certification revocation signature */
321
322     OPS_SIG_TIMESTAMP   =0x40,  /*<! Timestamp signature */
323
324     OPS_SIG_3RD_PARTY   =0x50,  /*<! Third-Party Confirmation signature */
325     } ops_sig_type_t;
326
327 /** Hashing Algorithm Numbers.
328  * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP.
329  *
330  * This lists algorithm numbers for hash algorithms.
331  *
332  * \see RFC2440bis-12 9.4
333  */
334 typedef enum
335     {
336     OPS_HASH_MD5        = 1,    /*!< MD5 */
337     OPS_HASH_SHA1       = 2,    /*!< SHA-1 */
338     OPS_HASH_RIPEMD     = 3,    /*!< RIPEMD160 */
339
340     OPS_HASH_SHA256     = 8,    /*!< SHA256 */
341     OPS_HASH_SHA384     = 9,    /*!< SHA384 */
342     OPS_HASH_SHA512     =10,    /*!< SHA512 */
343     } ops_hash_algorithm_t;
344
345 /** Struct to hold parameters of an RSA signature */
346 typedef struct
347     {
348     BIGNUM                      *sig;   /*!< the signature value (m^d % n) */
349     } ops_rsa_signature_t;
350
351 /** Struct to hold parameters of a DSA signature */
352 typedef struct
353     {
354     BIGNUM                      *r;     /*!< DSA value r */
355     BIGNUM                      *s;     /*!< DSA value s */
356     } ops_dsa_signature_t;
357
358 /** Union to hold signature parameters of any algorithm */
359 typedef union
360     {
361     ops_rsa_signature_t         rsa;    /*!< An RSA Signature */
362     ops_dsa_signature_t         dsa;    /*!< A DSA Signature */
363     } ops_signature_union_t;
364
365 /** Struct to hold a signature packet.
366  *
367  * \see RFC2440bis-12 5.2.2
368  * \see RFC2440bis-12 5.2.3
369  */
370 #define OPS_KEY_ID_SIZE         8
371 typedef struct
372     {
373     ops_sig_version_t           version;        /*!< signature version number */
374     ops_sig_type_t              type;           /*!< signature type value */
375     time_t                      creation_time;  /*!< creation time of the signature - only with v3 signatures*/
376     unsigned char               signer_id[OPS_KEY_ID_SIZE];     /*!< Eight-octet key ID of signer*/
377     ops_public_key_algorithm_t  key_algorithm;  /*!< public key algorithm number */
378     ops_hash_algorithm_t        hash_algorithm; /*!< hashing algorithm number */
379     unsigned char               hash2[2];       /*!< high 2 bytes of hashed value - for quick test */
380     ops_signature_union_t       signature;      /*!< signature parameters */
381     size_t                      v4_hashed_data_start; // only valid if accumulate is set
382     size_t                      v4_hashed_data_length;
383     } ops_signature_t;
384
385 /** The raw bytes of a signature subpacket */
386
387 typedef struct
388     {
389     ops_content_tag_t           tag;
390     size_t                      length;
391     unsigned char               *raw;
392     } ops_ss_raw_t;
393
394 /** Signature Subpacket Type 5, Trust Level */
395
396 typedef struct
397     {
398     unsigned char               level;
399     unsigned char               amount;
400     } ops_ss_trust_t;
401
402 typedef struct
403         {
404         ops_boolean_t   revocable;
405         } ops_ss_revocable_t;
406        
407 typedef struct
408     {
409     time_t                      time;
410     } ops_ss_time_t;
411
412 typedef struct
413     {
414     unsigned char               key_id[OPS_KEY_ID_SIZE];
415     } ops_ss_key_id_t;
416
417 typedef struct
418     {
419     size_t      len;    /* must use a length field in structure to
420                            determine where the algorithms stop.
421                            The value 0 may represent the plaintext algorithm
422                            so we cannot expect a null-terminated list */
423     unsigned char * data;
424     } ops_ss_preferred_ska_t;
425
426 typedef struct
427     {
428     size_t      len;   
429     unsigned char * data;
430     } ops_ss_preferred_hash_t;
431
432 typedef struct
433     {
434     size_t      len;   
435     unsigned char * data;
436     } ops_ss_preferred_compression_t;
437
438 typedef struct
439     {
440     size_t len;
441     unsigned char * data;
442     } ops_ss_key_flags_t;
443
444 typedef struct
445     {
446     size_t                      length;
447     unsigned char               *raw;
448     } ops_packet_t;
449
450 typedef enum
451     {
452     OPS_C_NONE=0,
453     OPS_C_ZIP=1,
454     OPS_C_ZLIB=2,
455     OPS_C_BZIP2=3,
456     } ops_compression_type_t;
457
458 // unlike most structures, this will feed its data as a stream
459 // to the application instead of directly including it
460 typedef struct
461     {
462     ops_compression_type_t      type;
463     } ops_compressed_t;
464
465 typedef struct
466     {
467     unsigned char               version;
468     ops_sig_type_t              sig_type;
469     ops_hash_algorithm_t        hash_algorithm;
470     ops_public_key_algorithm_t  key_algorithm;
471     unsigned char               keyid;
472     ops_boolean_t               nested;
473     } ops_one_pass_signature_t;
474
475 typedef struct
476     {
477     ops_boolean_t       primary_user_id;
478     } ops_ss_primary_user_id_t;
479
480 typedef struct
481     {
482     unsigned char       class;
483     unsigned char       algid;
484     unsigned char fingerprint[20];
485     } ops_ss_revocation_key_t;
486        
487 typedef union
488     {
489     ops_parser_error_t          error;
490     ops_ptag_t                  ptag;
491     ops_public_key_t            public_key;
492     ops_trust_t                 trust;
493     ops_user_id_t               user_id;
494     ops_signature_t             signature;
495     ops_ss_raw_t                ss_raw;
496     ops_ss_trust_t              ss_trust;
497     ops_ss_revocable_t  ss_revocable;
498     ops_ss_time_t               ss_time;
499     ops_ss_key_id_t             ss_issuer_key_id;
500     ops_packet_t                packet;
501     ops_compressed_t            compressed;
502     ops_one_pass_signature_t    one_pass_signature;
503     ops_ss_preferred_ska_t      ss_preferred_ska;
504     ops_ss_preferred_hash_t     ss_preferred_hash;
505     ops_ss_preferred_compression_t     ss_preferred_compression;
506     ops_ss_key_flags_t ss_key_flags;
507     ops_ss_primary_user_id_t    ss_primary_user_id;
508     ops_ss_revocation_key_t     ss_revocation_key;
509     } ops_parser_content_union_t;
510
511 struct ops_parser_content_t
512     {
513     ops_content_tag_t           tag;
514     unsigned char               critical; /* for signature subpackets */
515     ops_parser_content_union_t  content;
516     };
517
518 typedef struct
519     {
520     unsigned char               fingerprint[20];
521     unsigned                    length;
522     } ops_fingerprint_t;
523
524 void ops_init(void);
525 void ops_finish(void);
526 void ops_keyid(unsigned char keyid[OPS_KEY_ID_SIZE],
527                const ops_public_key_t *key);
528 void ops_fingerprint(ops_fingerprint_t *fp,const ops_public_key_t *key);
529 void ops_public_key_free(ops_public_key_t *key);
530 void ops_user_id_free(ops_user_id_t *id);
531 void ops_signature_free(ops_signature_t *sig);
532 void ops_trust_free(ops_trust_t * trust);
533 void ops_ss_preferred_ska_free(ops_ss_preferred_ska_t *ss_preferred_ska);
534 void ops_ss_preferred_hash_free(ops_ss_preferred_hash_t *ss_preferred_hash);
535 void ops_ss_preferred_compression_free(ops_ss_preferred_compression_t *ss_preferred_compression);
536 void ops_ss_key_flags_free(ops_ss_key_flags_t * ss_key_flags);
537 void ops_packet_free(ops_packet_t *packet);
538 void ops_parser_content_free(ops_parser_content_t *c);
539
540 /* vim:set textwidth=120: */
541 /* vim:set ts=8: */
542
543 #endif
544
Note: See TracBrowser for help on using the browser.