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