[crypto] Add SHA-512/224 algorithm
[ipxe.git] / src / include / ipxe / asn1.h
1 #ifndef _IPXE_ASN1_H
2 #define _IPXE_ASN1_H
3
4 /** @file
5 *
6 * ASN.1 encoding
7 *
8 */
9
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
12 #include <stdint.h>
13 #include <time.h>
14 #include <ipxe/tables.h>
15
16 /** An ASN.1 object cursor */
17 struct asn1_cursor {
18 /** Start of data */
19 const void *data;
20 /** Length of data */
21 size_t len;
22 };
23
24 /** An ASN.1 object builder */
25 struct asn1_builder {
26 /** Data
27 *
28 * This is always dynamically allocated. If @c data is NULL
29 * while @len is non-zero, this indicates that a memory
30 * allocation error has occurred during the building process.
31 */
32 void *data;
33 /** Length of data */
34 size_t len;
35 };
36
37 /** Maximum (viable) length of ASN.1 length
38 *
39 * While in theory unlimited, this length is sufficient to contain a
40 * size_t.
41 */
42 #define ASN1_MAX_LEN_LEN ( 1 + sizeof ( size_t ) )
43
44 /** An ASN.1 header */
45 struct asn1_builder_header {
46 /** Type */
47 uint8_t type;
48 /** Length (encoded) */
49 uint8_t length[ASN1_MAX_LEN_LEN];
50 } __attribute__ (( packed ));
51
52 /** ASN.1 end */
53 #define ASN1_END 0x00
54
55 /** ASN.1 boolean */
56 #define ASN1_BOOLEAN 0x01
57
58 /** ASN.1 integer */
59 #define ASN1_INTEGER 0x02
60
61 /** ASN.1 bit string */
62 #define ASN1_BIT_STRING 0x03
63
64 /** ASN.1 octet string */
65 #define ASN1_OCTET_STRING 0x04
66
67 /** ASN.1 null */
68 #define ASN1_NULL 0x05
69
70 /** ASN.1 object identifier */
71 #define ASN1_OID 0x06
72
73 /** ASN.1 enumeration */
74 #define ASN1_ENUMERATED 0x0a
75
76 /** ASN.1 UTC time */
77 #define ASN1_UTC_TIME 0x17
78
79 /** ASN.1 generalized time */
80 #define ASN1_GENERALIZED_TIME 0x18
81
82 /** ASN.1 sequence */
83 #define ASN1_SEQUENCE 0x30
84
85 /** ASN.1 set */
86 #define ASN1_SET 0x31
87
88 /** ASN.1 implicit tag */
89 #define ASN1_IMPLICIT_TAG( number) ( 0x80 | (number) )
90
91 /** ASN.1 explicit tag */
92 #define ASN1_EXPLICIT_TAG( number) ( 0xa0 | (number) )
93
94 /** ASN.1 "any tag" magic value */
95 #define ASN1_ANY -1U
96
97 /** Initial OID byte */
98 #define ASN1_OID_INITIAL( first, second ) ( ( (first) * 40 ) + (second) )
99
100 /** Single-byte OID value
101 *
102 * Valid for values up to 127
103 */
104 #define ASN1_OID_SINGLE( value ) ( (value) & 0x7f )
105
106 /** Double-byte OID value
107 *
108 * Valid for values up to 16383
109 */
110 #define ASN1_OID_DOUBLE( value ) \
111 ( 0x80 | ( ( (value) >> 7 ) & 0x7f ) ), ASN1_OID_SINGLE ( (value) )
112
113 /** Double-byte OID value
114 *
115 * Valid for values up to 2097151
116 */
117 #define ASN1_OID_TRIPLE( value ) \
118 ( 0x80 | ( ( (value) >> 14 ) & 0x7f ) ), ASN1_OID_DOUBLE ( (value) )
119
120 /** ASN.1 OID for rsaEncryption (1.2.840.113549.1.1.1) */
121 #define ASN1_OID_RSAENCRYPTION \
122 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
123 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 1 ), \
124 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 1 )
125
126 /** ASN.1 OID for md5WithRSAEncryption (1.2.840.113549.1.1.4) */
127 #define ASN1_OID_MD5WITHRSAENCRYPTION \
128 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
129 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 1 ), \
130 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 4 )
131
132 /** ASN.1 OID for sha1WithRSAEncryption (1.2.840.113549.1.1.5) */
133 #define ASN1_OID_SHA1WITHRSAENCRYPTION \
134 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
135 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 1 ), \
136 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 )
137
138 /** ASN.1 OID for sha256WithRSAEncryption (1.2.840.113549.1.1.11) */
139 #define ASN1_OID_SHA256WITHRSAENCRYPTION \
140 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
141 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 1 ), \
142 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 11 )
143
144 /** ASN.1 OID for id-md5 (1.2.840.113549.2.5) */
145 #define ASN1_OID_MD5 \
146 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
147 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 2 ), \
148 ASN1_OID_SINGLE ( 5 )
149
150 /** ASN.1 OID for id-sha1 (1.3.14.3.2.26) */
151 #define ASN1_OID_SHA1 \
152 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 14 ), \
153 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 2 ), \
154 ASN1_OID_SINGLE ( 26 )
155
156 /** ASN.1 OID for id-sha256 (2.16.840.1.101.3.4.2.1) */
157 #define ASN1_OID_SHA256 \
158 ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \
159 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \
160 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \
161 ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 1 )
162
163 /** ASN.1 OID for id-sha384 (2.16.840.1.101.3.4.2.2) */
164 #define ASN1_OID_SHA384 \
165 ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \
166 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \
167 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \
168 ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 2 )
169
170 /** ASN.1 OID for id-sha512 (2.16.840.1.101.3.4.2.3) */
171 #define ASN1_OID_SHA512 \
172 ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \
173 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \
174 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \
175 ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 3 )
176
177 /** ASN.1 OID for id-sha224 (2.16.840.1.101.3.4.2.4) */
178 #define ASN1_OID_SHA224 \
179 ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \
180 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \
181 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \
182 ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 4 )
183
184 /** ASN.1 OID for id-sha512-224 (2.16.840.1.101.3.4.2.5) */
185 #define ASN1_OID_SHA512_224 \
186 ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \
187 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \
188 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \
189 ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 5 )
190
191 /** ASN.1 OID for id-sha512-256 (2.16.840.1.101.3.4.2.6) */
192 #define ASN1_OID_SHA512_256 \
193 ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \
194 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \
195 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \
196 ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 6 )
197
198 /** ASN.1 OID for commonName (2.5.4.3) */
199 #define ASN1_OID_COMMON_NAME \
200 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 4 ), \
201 ASN1_OID_SINGLE ( 3 )
202
203 /** ASN.1 OID for id-ce-keyUsage (2.5.29.15) */
204 #define ASN1_OID_KEYUSAGE \
205 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
206 ASN1_OID_SINGLE ( 15 )
207
208 /** ASN.1 OID for id-ce-basicConstraints (2.5.29.19) */
209 #define ASN1_OID_BASICCONSTRAINTS \
210 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
211 ASN1_OID_SINGLE ( 19 )
212
213 /** ASN.1 OID for id-ce-extKeyUsage (2.5.29.37) */
214 #define ASN1_OID_EXTKEYUSAGE \
215 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
216 ASN1_OID_SINGLE ( 37 )
217
218 /** ASN.1 OID for id-kp-codeSigning (1.3.6.1.5.5.7.3.3) */
219 #define ASN1_OID_CODESIGNING \
220 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
221 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
222 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
223 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 3 )
224
225 /** ASN.1 OID for pkcs-signedData (1.2.840.113549.1.7.2) */
226 #define ASN1_OID_SIGNEDDATA \
227 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
228 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 1 ), \
229 ASN1_OID_SINGLE ( 7 ), ASN1_OID_SINGLE ( 2 )
230
231 /** ASN.1 OID for id-pe-authorityInfoAccess (1.3.6.1.5.5.7.1.1) */
232 #define ASN1_OID_AUTHORITYINFOACCESS \
233 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
234 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
235 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
236 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 1 )
237
238 /** ASN.1 OID for id-ad-ocsp (1.3.6.1.5.5.7.48.1) */
239 #define ASN1_OID_OCSP \
240 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
241 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
242 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
243 ASN1_OID_SINGLE ( 48 ), ASN1_OID_SINGLE ( 1 )
244
245 /** ASN.1 OID for id-pkix-ocsp-basic ( 1.3.6.1.5.5.7.48.1.1) */
246 #define ASN1_OID_OCSP_BASIC \
247 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
248 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
249 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
250 ASN1_OID_SINGLE ( 48 ), ASN1_OID_SINGLE ( 1 ), \
251 ASN1_OID_SINGLE ( 1 )
252
253 /** ASN.1 OID for id-kp-OCSPSigning (1.3.6.1.5.5.7.3.9) */
254 #define ASN1_OID_OCSPSIGNING \
255 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
256 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
257 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
258 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 9 )
259
260 /** ASN.1 OID for id-ce-subjectAltName (2.5.29.17) */
261 #define ASN1_OID_SUBJECTALTNAME \
262 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
263 ASN1_OID_SINGLE ( 17 )
264
265 /** Define an ASN.1 cursor containing an OID */
266 #define ASN1_OID_CURSOR( oid_value ) { \
267 .data = oid_value, \
268 .len = sizeof ( oid_value ), \
269 }
270
271 /** An ASN.1 OID-identified algorithm */
272 struct asn1_algorithm {
273 /** Name */
274 const char *name;
275 /** Object identifier */
276 struct asn1_cursor oid;
277 /** Public-key algorithm (if applicable) */
278 struct pubkey_algorithm *pubkey;
279 /** Digest algorithm (if applicable) */
280 struct digest_algorithm *digest;
281 };
282
283 /** ASN.1 OID-identified algorithms */
284 #define ASN1_ALGORITHMS __table ( struct asn1_algorithm, "asn1_algorithms" )
285
286 /** Declare an ASN.1 OID-identified algorithm */
287 #define __asn1_algorithm __table_entry ( ASN1_ALGORITHMS, 01 )
288
289 /** An ASN.1 bit string */
290 struct asn1_bit_string {
291 /** Data */
292 const void *data;
293 /** Length */
294 size_t len;
295 /** Unused bits at end of data */
296 unsigned int unused;
297 } __attribute__ (( packed ));
298
299 /**
300 * Extract ASN.1 type
301 *
302 * @v cursor ASN.1 object cursor
303 * @ret type Type
304 */
305 static inline __attribute__ (( always_inline )) unsigned int
306 asn1_type ( const struct asn1_cursor *cursor ) {
307 return ( *( ( const uint8_t * ) cursor->data ) );
308 }
309
310 extern void asn1_invalidate_cursor ( struct asn1_cursor *cursor );
311 extern int asn1_enter ( struct asn1_cursor *cursor, unsigned int type );
312 extern int asn1_skip_if_exists ( struct asn1_cursor *cursor,
313 unsigned int type );
314 extern int asn1_skip ( struct asn1_cursor *cursor, unsigned int type );
315 extern int asn1_shrink ( struct asn1_cursor *cursor, unsigned int type );
316 extern int asn1_enter_any ( struct asn1_cursor *cursor );
317 extern int asn1_skip_any ( struct asn1_cursor *cursor );
318 extern int asn1_shrink_any ( struct asn1_cursor *cursor );
319 extern int asn1_boolean ( const struct asn1_cursor *cursor );
320 extern int asn1_integer ( const struct asn1_cursor *cursor, int *value );
321 extern int asn1_bit_string ( const struct asn1_cursor *cursor,
322 struct asn1_bit_string *bits );
323 extern int asn1_integral_bit_string ( const struct asn1_cursor *cursor,
324 struct asn1_bit_string *bits );
325 extern int asn1_compare ( const struct asn1_cursor *cursor1,
326 const struct asn1_cursor *cursor2 );
327 extern int asn1_algorithm ( const struct asn1_cursor *cursor,
328 struct asn1_algorithm **algorithm );
329 extern int asn1_pubkey_algorithm ( const struct asn1_cursor *cursor,
330 struct asn1_algorithm **algorithm );
331 extern int asn1_digest_algorithm ( const struct asn1_cursor *cursor,
332 struct asn1_algorithm **algorithm );
333 extern int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
334 struct asn1_algorithm **algorithm );
335 extern int asn1_generalized_time ( const struct asn1_cursor *cursor,
336 time_t *time );
337 extern int asn1_prepend_raw ( struct asn1_builder *builder, const void *data,
338 size_t len );
339 extern int asn1_prepend ( struct asn1_builder *builder, unsigned int type,
340 const void *data, size_t len );
341 extern int asn1_wrap ( struct asn1_builder *builder, unsigned int type );
342
343 #endif /* _IPXE_ASN1_H */