[crypto] Add SHA-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-sha224 (2.16.840.1.101.3.4.2.4) */
164 #define ASN1_OID_SHA224 \
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 ( 4 )
169
170 /** ASN.1 OID for commonName (2.5.4.3) */
171 #define ASN1_OID_COMMON_NAME \
172 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 4 ), \
173 ASN1_OID_SINGLE ( 3 )
174
175 /** ASN.1 OID for id-ce-keyUsage (2.5.29.15) */
176 #define ASN1_OID_KEYUSAGE \
177 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
178 ASN1_OID_SINGLE ( 15 )
179
180 /** ASN.1 OID for id-ce-basicConstraints (2.5.29.19) */
181 #define ASN1_OID_BASICCONSTRAINTS \
182 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
183 ASN1_OID_SINGLE ( 19 )
184
185 /** ASN.1 OID for id-ce-extKeyUsage (2.5.29.37) */
186 #define ASN1_OID_EXTKEYUSAGE \
187 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
188 ASN1_OID_SINGLE ( 37 )
189
190 /** ASN.1 OID for id-kp-codeSigning (1.3.6.1.5.5.7.3.3) */
191 #define ASN1_OID_CODESIGNING \
192 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
193 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
194 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
195 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 3 )
196
197 /** ASN.1 OID for pkcs-signedData (1.2.840.113549.1.7.2) */
198 #define ASN1_OID_SIGNEDDATA \
199 ASN1_OID_INITIAL ( 1, 2 ), ASN1_OID_DOUBLE ( 840 ), \
200 ASN1_OID_TRIPLE ( 113549 ), ASN1_OID_SINGLE ( 1 ), \
201 ASN1_OID_SINGLE ( 7 ), ASN1_OID_SINGLE ( 2 )
202
203 /** ASN.1 OID for id-pe-authorityInfoAccess (1.3.6.1.5.5.7.1.1) */
204 #define ASN1_OID_AUTHORITYINFOACCESS \
205 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
206 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
207 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
208 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 1 )
209
210 /** ASN.1 OID for id-ad-ocsp (1.3.6.1.5.5.7.48.1) */
211 #define ASN1_OID_OCSP \
212 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
213 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
214 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
215 ASN1_OID_SINGLE ( 48 ), ASN1_OID_SINGLE ( 1 )
216
217 /** ASN.1 OID for id-pkix-ocsp-basic ( 1.3.6.1.5.5.7.48.1.1) */
218 #define ASN1_OID_OCSP_BASIC \
219 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
220 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
221 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
222 ASN1_OID_SINGLE ( 48 ), ASN1_OID_SINGLE ( 1 ), \
223 ASN1_OID_SINGLE ( 1 )
224
225 /** ASN.1 OID for id-kp-OCSPSigning (1.3.6.1.5.5.7.3.9) */
226 #define ASN1_OID_OCSPSIGNING \
227 ASN1_OID_INITIAL ( 1, 3 ), ASN1_OID_SINGLE ( 6 ), \
228 ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 5 ), \
229 ASN1_OID_SINGLE ( 5 ), ASN1_OID_SINGLE ( 7 ), \
230 ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 9 )
231
232 /** ASN.1 OID for id-ce-subjectAltName (2.5.29.17) */
233 #define ASN1_OID_SUBJECTALTNAME \
234 ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 29 ), \
235 ASN1_OID_SINGLE ( 17 )
236
237 /** Define an ASN.1 cursor containing an OID */
238 #define ASN1_OID_CURSOR( oid_value ) { \
239 .data = oid_value, \
240 .len = sizeof ( oid_value ), \
241 }
242
243 /** An ASN.1 OID-identified algorithm */
244 struct asn1_algorithm {
245 /** Name */
246 const char *name;
247 /** Object identifier */
248 struct asn1_cursor oid;
249 /** Public-key algorithm (if applicable) */
250 struct pubkey_algorithm *pubkey;
251 /** Digest algorithm (if applicable) */
252 struct digest_algorithm *digest;
253 };
254
255 /** ASN.1 OID-identified algorithms */
256 #define ASN1_ALGORITHMS __table ( struct asn1_algorithm, "asn1_algorithms" )
257
258 /** Declare an ASN.1 OID-identified algorithm */
259 #define __asn1_algorithm __table_entry ( ASN1_ALGORITHMS, 01 )
260
261 /** An ASN.1 bit string */
262 struct asn1_bit_string {
263 /** Data */
264 const void *data;
265 /** Length */
266 size_t len;
267 /** Unused bits at end of data */
268 unsigned int unused;
269 } __attribute__ (( packed ));
270
271 /**
272 * Extract ASN.1 type
273 *
274 * @v cursor ASN.1 object cursor
275 * @ret type Type
276 */
277 static inline __attribute__ (( always_inline )) unsigned int
278 asn1_type ( const struct asn1_cursor *cursor ) {
279 return ( *( ( const uint8_t * ) cursor->data ) );
280 }
281
282 extern void asn1_invalidate_cursor ( struct asn1_cursor *cursor );
283 extern int asn1_enter ( struct asn1_cursor *cursor, unsigned int type );
284 extern int asn1_skip_if_exists ( struct asn1_cursor *cursor,
285 unsigned int type );
286 extern int asn1_skip ( struct asn1_cursor *cursor, unsigned int type );
287 extern int asn1_shrink ( struct asn1_cursor *cursor, unsigned int type );
288 extern int asn1_enter_any ( struct asn1_cursor *cursor );
289 extern int asn1_skip_any ( struct asn1_cursor *cursor );
290 extern int asn1_shrink_any ( struct asn1_cursor *cursor );
291 extern int asn1_boolean ( const struct asn1_cursor *cursor );
292 extern int asn1_integer ( const struct asn1_cursor *cursor, int *value );
293 extern int asn1_bit_string ( const struct asn1_cursor *cursor,
294 struct asn1_bit_string *bits );
295 extern int asn1_integral_bit_string ( const struct asn1_cursor *cursor,
296 struct asn1_bit_string *bits );
297 extern int asn1_compare ( const struct asn1_cursor *cursor1,
298 const struct asn1_cursor *cursor2 );
299 extern int asn1_algorithm ( const struct asn1_cursor *cursor,
300 struct asn1_algorithm **algorithm );
301 extern int asn1_pubkey_algorithm ( const struct asn1_cursor *cursor,
302 struct asn1_algorithm **algorithm );
303 extern int asn1_digest_algorithm ( const struct asn1_cursor *cursor,
304 struct asn1_algorithm **algorithm );
305 extern int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
306 struct asn1_algorithm **algorithm );
307 extern int asn1_generalized_time ( const struct asn1_cursor *cursor,
308 time_t *time );
309 extern int asn1_prepend_raw ( struct asn1_builder *builder, const void *data,
310 size_t len );
311 extern int asn1_prepend ( struct asn1_builder *builder, unsigned int type,
312 const void *data, size_t len );
313 extern int asn1_wrap ( struct asn1_builder *builder, unsigned int type );
314
315 #endif /* _IPXE_ASN1_H */