root/openpgpsdk/trunk/src/packet.h

Revision 31 (checked in by ben, 8 years ago)

Add times and doxygen target/conf.

  • Property svn:keywords set to Id
Line 
1 /** \file packet.h
2  * packet related headers.
3  *
4  * $Id$
5  */
6
7 #include <time.h>
8 #include <openssl/bn.h>
9
10 /************************************/
11 /* Packet Tags - RFC2440bis-12, 4.2 */
12 /************************************/
13
14 /** Packet Tag - Bit 7 Mask (this bit is always set).
15  * The first byte of a packet is the "Packet Tag".  It always
16  * has bit 7 set.  This is the mask for it.
17  *
18  * \see RFC2440bis-12 4.2
19  */
20 #define OPS_PTAG_ALWAYS_SET             0x80
21
22 /** Packet Tag - New Format Flag.
23  * Bit 6 of the Packet Tag is the packet format indicator.
24  * If it is set, the new format is used, if cleared the
25  * old format is used.
26  *
27  * \see RFC2440bis-12 4.2
28  */
29 #define OPS_PTAG_NEW_FORMAT             0x40
30
31
32 /** Old Packet Format: Mask for content tag.
33  * In the old packet format bits 5 to 2 (including)
34  * are the content tag.  This is the mask to apply
35  * to the packet tag.  Note that you need to
36  * shift by #OPS_PTAG_OF_CONTENT_TAG_SHIFT bits.
37  *
38  * \see RFC2440bis-12 4.2
39  */
40 #define OPS_PTAG_OF_CONTENT_TAG_MASK    0x3c
41 /** Old Packet Format: Offset for the content tag.
42  * As described at #OPS_PTAG_OF_CONTENT_TAG_MASK the
43  * content tag needs to be shifted after being masked
44  * out from the Packet Tag.
45  *
46  * \see RFC2440bis-12 4.2
47  */
48 #define OPS_PTAG_OF_CONTENT_TAG_SHIFT   2
49 /** Old Packet Format: Mask for length type.
50  * Bits 1 and 0 of the packet tag are the length type
51  * in the old packet format.
52  *
53  * See #ops_ptag_of_lt_t for the meaning of the values.
54  *
55  * \see RFC2440bis-12 4.2
56  */
57 #define OPS_PTAG_OF_LENGTH_TYPE_MASK    0x03
58
59
60 /** Old Packet Format Lengths.
61  * Defines the meanings of the 2 bits for length type in the
62  * old packet format.
63  *
64  * \see RFC2440bis-12 4.2.1
65  */
66 typedef enum
67     {
68     OPS_PTAG_OF_LT_ONE_BYTE             =0x00, /*!< Packet has a 1 byte length - header is 2 bytes long. */
69     OPS_PTAG_OF_LT_TWO_BYTE             =0x01, /*!< Packet has a 2 byte length - header is 3 bytes long. */
70     OPS_PTAG_OF_LT_FOUR_BYTE            =0x02, /*!< Packet has a 4 byte length - header is 5 bytes long. */
71     OPS_PTAG_OF_LT_INDETERMINATE        =0x03  /*!< Packet has a indeterminate length. */
72     } ops_ptag_of_lt_t;
73
74
75 /** New Packet Format: Mask for content tag.
76  * In the new packet format the 6 rightmost bits
77  * are the content tag.  This is the mask to apply
78  * to the packet tag.  Note that you need to
79  * shift by #OPS_PTAG_NF_CONTENT_TAG_SHIFT bits.
80  *
81  * \see RFC2440bis-12 4.2
82  */
83 #define OPS_PTAG_NF_CONTENT_TAG_MASK    0x3f
84 /** New Packet Format: Offset for the content tag.
85  * As described at #OPS_PTAG_NF_CONTENT_TAG_MASK the
86  * content tag needs to be shifted after being masked
87  * out from the Packet Tag.
88  *
89  * \see RFC2440bis-12 4.2
90  */
91 #define OPS_PTAG_NF_CONTENT_TAG_SHIFT   0
92
93
94
95 /* PTag Content Tags */
96 /***************************/
97
98 /** Package Tags (aka Content Tags) and signatue subpacket types.
99  * This enumerates all rfc-defined packet tag values and the
100  * signature subpacket type values that we understand.
101  *
102  * \see RFC2440bis-12 4.3
103  * \see RFC2440bis-12 5.2.3.1
104  */
105 typedef enum
106     {
107     OPS_PTAG_CT_RESERVED                = 0,    /*!< Reserved - a packet tag must not have this value */
108     OPS_PTAG_CT_PK_SESSION_KEY          = 1,    /*!< Public-Key Encrypted Session Key Packet */
109     OPS_PTAG_CT_SIGNATURE               = 2,    /*!< Signature Packet */
110     OPS_PTAG_CT_SK_SESSION_KEY          = 3,    /*!< Symmetric-Key Encrypted Session Key Packet */
111     OPS_PTAG_CT_ONE_PASS_SIGNATURE      = 4,    /*!< One-Pass Signature Packet */
112     OPS_PTAG_CT_SECRET_KEY              = 5,    /*!< Secret Key Packet */
113     OPS_PTAG_CT_PUBLIC_KEY              = 6,    /*!< Public Key Packet */
114     OPS_PTAG_CT_SECRET_SUBKEY           = 7,    /*!< Secret Subkey Packet */
115     OPS_PTAG_CT_COMPRESSED              = 8,    /*!< Compressed Data Packet */
116     OPS_PTAG_CT_SK_DATA                 = 9,    /*!< Symmetrically Encrypted Data Packet */
117     OPS_PTAG_CT_MARKER                  =10,    /*!< Marker Packet */
118     OPS_PTAG_CT_LITERAL_DATA            =11,    /*!< Literal Data Packet */
119     OPS_PTAG_CT_TRUST                   =12,    /*!< Trust Packet */
120     OPS_PTAG_CT_USER_ID                 =13,    /*!< User ID Packet */
121     OPS_PTAG_CT_PUBLIC_SUBKEY           =14,    /*!< Public Subkey Packet */
122     OPS_PTAG_CT_RESERVED2               =15,    /*!< reserved */
123     OPS_PTAG_CT_RESERVED3               =16,    /*!< reserved */
124     OPS_PTAG_CT_USER_ATTRIBUTE          =17,    /*!< User Attribute Packet */
125     OPS_PTAG_CT_SK_IP_DATA              =18,    /*!< Sym. Encrypted and Integrity Protected Data Packet */
126     OPS_PTAG_CT_MDC                     =19,    /*!< Modification Detection Code Packet */
127
128     OPS_PARSER_ERROR                    =0x100, /*!< Internal Use: Parser Error */
129     OPS_PARSER_PTAG                     =0x101, /*!< Internal Use: The packet is the "Packet Tag" itself - used when
130                                                      callback sends back the PTag. */
131     OPS_PTAG_RAW_SS                     =0x102, /*!< Internal Use: content is raw sig subtag */
132     OPS_PTAG_SS_ALL                     =0x103, /*!< Internal Use: select all subtags */
133
134     /* signature subpackets (0x200-2ff) (type+0x200) */
135     /* only those we can parse are listed here */
136     OPS_PTAG_SIGNATURE_SUBPACKET_BASE   =0x200,         /*!< Base for signature subpacket types - All signature type
137                                                              values are relative to this value. */
138     OPS_PTAG_SS_CREATION_TIME           =0x200+2,       /*!< signature creation time */
139     OPS_PTAG_SS_EXPIRATION_TIME         =0x200+3,       /*!< signature expiration time */
140     OPS_PTAG_SS_TRUST                   =0x200+5        /*!< trust signature */
141     } ops_content_tag_t;
142
143 /** Structure to hold one parse error string. */
144 typedef struct
145     {
146     const char *error; /*!< error message. */
147     } ops_parser_error_t;
148
149 /** Structure to hold one packet tag.
150  * \see RFC2440bis-12 4.2
151  */
152 typedef struct
153     {
154     unsigned            new_format;     /*!< Whether this packet tag is new (true) or old format (false) */
155     unsigned            content_tag;    /*!< content_tag value - See #ops_content_tag_t for meanings */
156     ops_ptag_of_lt_t    length_type;    /*!< Length type (#ops_ptag_of_lt_t) - only if this packet tag is old format */
157     unsigned            length;         /*!< The length of the packet.  This value is set when we read and compute the
158                                           length information, not at the same moment we create the packet tag structure.
159                                           Only defined if #length_read is set. */  /* XXX: Ben, is this correct? */
160     unsigned            length_read;    /*!< How much bytes of this packet we have read so far - for internal use
161                                           only. */
162     } ops_ptag_t;
163
164 /** Public Key Algorithm Numbers.
165  * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP.
166  *
167  * This lists algorith numbers for public key algorithms.
168  *
169  * \see RFC2440bis-12 9.1
170  */
171 typedef enum
172     {
173     OPS_PKA_RSA                 =1,     /*!< RSA (Encrypt or Sign) */
174     OPS_PKA_RSA_ENCRYPT_ONLY    =2,     /*!< RSA Encrypt-Only (deprecated - \see RFC2440bis-12 12.4) */
175     OPS_PKA_RSA_SIGN_ONLY       =3,     /*!< RSA Sign-Only (deprecated - \see RFC2440bis-12 12.4) */
176     OPS_PKA_ELGAMAL             =16,    /*!< Elgamal (Encrypt-Only) */
177     OPS_PKA_DSA                 =17     /*!< DSA (Digital Signature Algorithm) */
178     } ops_public_key_algorithm_t;
179
180 /** Structure to hold one DSA public key parameters.
181  *
182  * \see RFC2440bis-12 5.5.2
183  */
184 typedef struct
185     {
186     BIGNUM *p;  /*!< DSA prime p */
187     BIGNUM *q;  /*!< DSA group order q */
188     BIGNUM *g;  /*!< DSA group generator g */
189     BIGNUM *y;  /*!< DSA public key value y (= g^x mod p with x being the secret) */
190     } ops_dsa_public_key_t;
191
192 /** Structure to hold on RSA public key.
193  *
194  * \see RFC2440bis-12 5.5.2
195  */
196 typedef struct
197     {
198     BIGNUM *n;  /*!< RSA public modulus n */
199     BIGNUM *e;  /*!< RSA public encryptiong exponent e */
200     } ops_rsa_public_key_t;
201
202 /** Structure to hold on ElGamal public key parameters.
203  *
204  * \see RFC2440bis-12 5.5.2
205  */
206 typedef struct
207     {
208     BIGNUM *p;  /*!< ElGamal prime p */
209     BIGNUM *g;  /*!< ElGamal group generator g */
210     BIGNUM *y;  /*!< ElGamal public key value y (= g^x mod p with x being the secret) */
211     } ops_elgamel_public_key_t;
212
213 /** Union to hold public key parameters of any algorithm */
214 typedef union
215     {
216     ops_dsa_public_key_t        dsa;            /*!< A DSA public key */
217     ops_rsa_public_key_t        rsa;            /*!< An RSA public key */
218     ops_elgamel_public_key_t    elgamel;        /*!< An ElGamal public key */
219     } ops_public_key_union_t;
220
221 /** Struture to hold one pgp public key */
222 typedef struct
223     {
224     unsigned                    version;        /*!< version of the key (v3, v4...) */
225     time_t                      creation_time;  /*!< when the key was created.  Note that interpretation varies with key
226                                                   version. */
227     unsigned                    days_valid;     /*!< validity period of the key in days since creation.  A value of 0
228                                                   has a special meaning indicating this key does not expire.  Only
229                                                   used with v3 keys. */
230     ops_public_key_algorithm_t  algorithm;      /*!< Public Key Algorithm type */
231     ops_public_key_union_t      key;            /*!< Public Key Parameters */
232     } ops_public_key_t;
233
234 /** Struture to hold one user id */
235 typedef struct
236     {
237     char *                      user_id;        /*!< User ID string */
238     } ops_user_id_t;
239
240 /** Signature Version.
241  * OpenPGP has two different signature versions: version 3 and version 4.
242  *
243  * \see RFC2440bis-12 5.2
244  */
245 typedef enum
246     {
247     OPS_SIG_V3=3,       /*<! Version 3 Signature */
248     OPS_SIG_V4=4,       /*<! Version 4 Signature */
249     } ops_sig_version_t;
250
251 /** Signature Type.
252  * OpenPGP defines different signature types that allow giving different meanings to signatures.  Signature types
253  * include 0x10 for generitc User ID certifications (used when Ben signs Weasel's key), Subkey binding signatures,
254  * document signatures, key revocations, etc.
255  *
256  * Different types are used in different places, and most make only sense in their intended location (for instance a
257  * subkey binding has no place on a UserID).
258  *
259  * \see RFC2440bis-12 5.2.1
260  */
261 typedef enum
262     {
263     OPS_SIG_BINARY      =0x00,  /*<! Signature of a binary document */
264     OPS_SIG_TEXT        =0x01,  /*<! Signature of a canonical text document */
265     OPS_SIG_STANDALONE  =0x02,  /*<! Standalone signature */
266
267     OPS_CERT_GENERIC    =0x10,  /*<! Generic certification of a User ID and Public Key packet */
268     OPS_CERT_PERSONA    =0x11,  /*<! Persona certification of a User ID and Public Key packet */
269     OPS_CERT_CASUAL     =0x12,  /*<! Casual certification of a User ID and Public Key packet */
270     OPS_CERT_POSITIVE   =0x13,  /*<! Positive certification of a User ID and Public Key packet */
271
272     OPS_SIG_SUBKEY      =0x18,  /*<! Subkey Binding Signature */
273     OPS_SIG_PRIMARY     =0x19,  /*<! Primary Key Binding Signature */
274     OPS_SIG_DIRECT      =0x1f,  /*<! Signature directly on a key */
275
276     OPS_SIG_REV_KEY     =0x20,  /*<! Key revocation signature */
277     OPS_SIG_REV_SUBKEY  =0x28,  /*<! Subkey revocation signature */
278     OPS_SIG_REV_CERT    =0x30,  /*<! Certification revocation signature */
279
280     OPS_SIG_TIMESTAMP   =0x40,  /*<! Timestamp signature */
281
282     OPS_SIG_3RD_PARTY   =0x50,  /*<! Third-Party Confirmation signature */
283     } ops_sig_type_t;
284
285 /** Hashing Algorithm Numbers.
286  * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP.
287  *
288  * This lists algorith numbers for hash algorithms.
289  *
290  * \see RFC2440bis-12 9.1
291  */
292 typedef enum
293     {
294     OPS_HASH_MD5        = 1,    /*!< MD5 */
295     OPS_HASH_SHA1       = 2,    /*!< SHA-1 */
296     OPS_HASH_RIPEMD     = 3,    /*!< RIPEMD160 */
297
298     OPS_HASH_SHA256     = 8,    /*!< SHA256 */
299     OPS_HASH_SHA384     = 9,    /*!< SHA384 */
300     OPS_HASH_SHA512     =10,    /*!< SHA512 */
301     } ops_hash_algorithm_t;
302
303 /** Struct to hold parameters of an RSA signature */
304 typedef struct
305     {
306     BIGNUM                      *sig;   /*!< the signature value (m^d % n) */
307     } ops_rsa_signature_t;
308
309 /** Struct to hold parameters of a DSA signature */
310 typedef struct
311     {
312     BIGNUM                      *r;     /*!< DSA value r */
313     BIGNUM                      *s;     /*!< DSA value s */
314     } ops_dsa_signature_t;
315
316 /** Union to hold signature parameters of any algorithm */
317 typedef union
318     {
319     ops_rsa_signature_t         rsa;    /*!< An RSA Signature */
320     ops_dsa_signature_t         dsa;    /*!< A DSA Signature */
321     } ops_signature_union_t;
322
323 /** Struct to hold a signature packet.
324  *
325  * \see RFC2440bis-12 5.2.2
326  * \see RFC2440bis-12 5.2.3
327  */
328 typedef struct
329     {
330     ops_sig_version_t           version;        /*!< signature version number */
331     ops_sig_type_t              type;           /*!< signature type value */
332     time_t                      creation_time;  /*!< creation time of the signature - only with v3 signatures*/
333     unsigned char               signer_id[8];   /*!< Eight-octet key ID of signer*/
334     ops_public_key_algorithm_t  key_algorithm;  /*!< public key algorithm number */
335     ops_hash_algorithm_t        hash_algorithm; /*!< hashing algorithm number */
336     unsigned char               hash2[2];       /*!< high 2 bytes of hashed value - for quick test */
337     ops_signature_union_t       signature;      /*!< signature parameters */
338     } ops_signature_t;
339
340 /** The raw bytes of a signature subpacket */
341
342 typedef struct
343     {
344     ops_content_tag_t           tag;
345     size_t                      length;
346     unsigned char               *raw;
347     } ops_ss_raw_t;
348
349 /** Signature Subpacket Type 5, Trust Level */
350
351 typedef struct
352     {
353     unsigned char               level;
354     unsigned char               amount;
355     } ops_ss_trust_t;
356
357 typedef struct
358     {
359     time_t                      time;
360     } ops_ss_time_t;
361
362 typedef union
363     {
364     ops_parser_error_t          error;
365     ops_ptag_t                  ptag;
366     ops_public_key_t            public_key;
367     ops_user_id_t               user_id;
368     ops_signature_t             signature;
369     ops_ss_raw_t                ss_raw;
370     ops_ss_trust_t              ss_trust;
371     ops_ss_time_t               ss_time;
372     } ops_parser_content_union_t;
373
374 typedef struct
375     {
376     ops_content_tag_t           tag;
377     unsigned char               critical; /* for signature subpackets */
378     ops_parser_content_union_t  content;
379     } ops_parser_content_t;
380
381 /* vim:set textwidth=120: */
382 /* vim:set ts=8: */
Note: See TracBrowser for help on using the browser.