]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ccder.h | |
3 | * corecrypto | |
4 | * | |
5 | * Created by Michael Brouwer on 2/28/12. | |
6 | * Copyright 2012 Apple Inc. All rights reserved. | |
7 | * | |
8 | */ | |
9 | ||
10 | #ifndef _CORECRYPTO_CCDER_H_ | |
11 | #define _CORECRYPTO_CCDER_H_ | |
12 | ||
13 | #include <corecrypto/ccasn1.h> | |
14 | #include <corecrypto/ccn.h> | |
15 | ||
16 | #define CCDER_MULTIBYTE_TAGS 1 | |
17 | ||
18 | #ifdef CCDER_MULTIBYTE_TAGS | |
19 | typedef unsigned long ccder_tag; | |
20 | #else | |
21 | typedef uint8_t ccder_tag; | |
22 | #endif | |
23 | ||
24 | /* DER types to be used with ccder_decode and ccder_encode functions. */ | |
25 | enum { | |
26 | CCDER_EOL = CCASN1_EOL, | |
27 | CCDER_BOOLEAN = CCASN1_BOOLEAN, | |
28 | CCDER_INTEGER = CCASN1_INTEGER, | |
29 | CCDER_BIT_STRING = CCASN1_BIT_STRING, | |
30 | CCDER_OCTET_STRING = CCASN1_OCTET_STRING, | |
31 | CCDER_NULL = CCASN1_NULL, | |
32 | CCDER_OBJECT_IDENTIFIER = CCASN1_OBJECT_IDENTIFIER, | |
33 | CCDER_OBJECT_DESCRIPTOR = CCASN1_OBJECT_DESCRIPTOR, | |
34 | /* External or instance-of 0x08 */ | |
35 | CCDER_REAL = CCASN1_REAL, | |
36 | CCDER_ENUMERATED = CCASN1_ENUMERATED, | |
37 | CCDER_EMBEDDED_PDV = CCASN1_EMBEDDED_PDV, | |
38 | CCDER_UTF8_STRING = CCASN1_UTF8_STRING, | |
39 | /* 0x0d */ | |
40 | /* 0x0e */ | |
41 | /* 0x0f */ | |
42 | CCDER_SEQUENCE = CCASN1_SEQUENCE, | |
43 | CCDER_SET = CCASN1_SET, | |
44 | CCDER_NUMERIC_STRING = CCASN1_NUMERIC_STRING, | |
45 | CCDER_PRINTABLE_STRING = CCASN1_PRINTABLE_STRING, | |
46 | CCDER_T61_STRING = CCASN1_T61_STRING, | |
47 | CCDER_VIDEOTEX_STRING = CCASN1_VIDEOTEX_STRING, | |
48 | CCDER_IA5_STRING = CCASN1_IA5_STRING, | |
49 | CCDER_UTC_TIME = CCASN1_UTC_TIME, | |
50 | CCDER_GENERALIZED_TIME = CCASN1_GENERALIZED_TIME, | |
51 | CCDER_GRAPHIC_STRING = CCASN1_GRAPHIC_STRING, | |
52 | CCDER_VISIBLE_STRING = CCASN1_VISIBLE_STRING, | |
53 | CCDER_GENERAL_STRING = CCASN1_GENERAL_STRING, | |
54 | CCDER_UNIVERSAL_STRING = CCASN1_UNIVERSAL_STRING, | |
55 | /* 0x1d */ | |
56 | CCDER_BMP_STRING = CCASN1_BMP_STRING, | |
57 | CCDER_HIGH_TAG_NUMBER = CCASN1_HIGH_TAG_NUMBER, | |
58 | CCDER_TELETEX_STRING = CCDER_T61_STRING, | |
59 | ||
60 | #ifdef CCDER_MULTIBYTE_TAGS | |
61 | CCDER_TAG_MASK = ((ccder_tag)~0), | |
62 | CCDER_TAGNUM_MASK = ((ccder_tag)~((ccder_tag)7 << (sizeof(ccder_tag) * 8 - 3))), | |
63 | ||
64 | CCDER_METHOD_MASK = ((ccder_tag)1 << (sizeof(ccder_tag) * 8 - 3)), | |
65 | CCDER_PRIMITIVE = ((ccder_tag)0 << (sizeof(ccder_tag) * 8 - 3)), | |
66 | CCDER_CONSTRUCTED = ((ccder_tag)1 << (sizeof(ccder_tag) * 8 - 3)), | |
67 | ||
68 | CCDER_CLASS_MASK = ((ccder_tag)3 << (sizeof(ccder_tag) * 8 - 2)), | |
69 | CCDER_UNIVERSAL = ((ccder_tag)0 << (sizeof(ccder_tag) * 8 - 2)), | |
70 | CCDER_APPLICATION = ((ccder_tag)1 << (sizeof(ccder_tag) * 8 - 2)), | |
71 | CCDER_CONTEXT_SPECIFIC = ((ccder_tag)2 << (sizeof(ccder_tag) * 8 - 2)), | |
72 | CCDER_PRIVATE = ((ccder_tag)3 << (sizeof(ccder_tag) * 8 - 2)), | |
73 | #else | |
74 | CCDER_TAG_MASK = CCASN1_TAG_MASK, | |
75 | CCDER_TAGNUM_MASK = CCASN1_TAGNUM_MASK, | |
76 | ||
77 | CCDER_METHOD_MASK = CCASN1_METHOD_MASK, | |
78 | CCDER_PRIMITIVE = CCASN1_PRIMITIVE, | |
79 | CCDER_CONSTRUCTED = CCASN1_CONSTRUCTED, | |
80 | ||
81 | CCDER_CLASS_MASK = CCASN1_CLASS_MASK, | |
82 | CCDER_UNIVERSAL = CCASN1_UNIVERSAL, | |
83 | CCDER_APPLICATION = CCASN1_APPLICATION, | |
84 | CCDER_CONTEXT_SPECIFIC = CCASN1_CONTEXT_SPECIFIC, | |
85 | CCDER_PRIVATE = CCASN1_PRIVATE, | |
86 | #endif | |
87 | CCDER_CONSTRUCTED_SET = CCDER_SET | CCDER_CONSTRUCTED, | |
88 | CCDER_CONSTRUCTED_SEQUENCE = CCDER_SEQUENCE | CCDER_CONSTRUCTED, | |
89 | }; | |
90 | ||
91 | ||
92 | #define CC_NO_INLINE | |
93 | // MARK: ccder_sizeof_ functions | |
94 | ||
95 | /* Returns the size of an asn1 encoded item of length l in bytes. */ | |
96 | CC_NO_INLINE CC_CONST | |
97 | size_t ccder_sizeof(ccder_tag tag, size_t len); | |
98 | ||
99 | CC_NO_INLINE CC_PURE | |
100 | size_t ccder_sizeof_implicit_integer(ccder_tag implicit_tag, | |
101 | cc_size n, const cc_unit *s); | |
102 | ||
103 | CC_NO_INLINE CC_PURE | |
104 | size_t ccder_sizeof_implicit_octet_string(ccder_tag implicit_tag, | |
105 | cc_size n, const cc_unit *s); | |
106 | ||
107 | CC_NO_INLINE CC_CONST | |
108 | size_t ccder_sizeof_implicit_raw_octet_string(ccder_tag implicit_tag, | |
109 | size_t s_size); | |
110 | CC_NO_INLINE CC_CONST | |
111 | size_t ccder_sizeof_implicit_uint64(ccder_tag implicit_tag, uint64_t value); | |
112 | ||
113 | CC_NO_INLINE CC_PURE | |
114 | size_t ccder_sizeof_integer(cc_size n, const cc_unit *s); | |
115 | ||
116 | CC_NO_INLINE CC_CONST | |
117 | size_t ccder_sizeof_len(size_t len); | |
118 | ||
119 | CC_NO_INLINE CC_PURE | |
120 | size_t ccder_sizeof_octet_string(cc_size n, const cc_unit *s); | |
121 | ||
122 | CC_NO_INLINE CC_PURE | |
123 | size_t ccder_sizeof_oid(ccoid_t oid); | |
124 | ||
125 | CC_NO_INLINE CC_CONST | |
126 | size_t ccder_sizeof_raw_octet_string(size_t s_size); | |
127 | ||
128 | CC_NO_INLINE CC_CONST | |
129 | size_t ccder_sizeof_tag(ccder_tag tag); | |
130 | ||
131 | CC_NO_INLINE CC_CONST | |
132 | size_t ccder_sizeof_uint64(uint64_t value); | |
133 | ||
134 | ||
135 | // MARK: ccder_encode_ functions. | |
136 | ||
137 | /* Encode a tag backwards, der_end should point to one byte past the end of | |
138 | destination for the tag, returns a pointer to the first byte of the tag. | |
139 | Returns NULL if there is an encoding error. */ | |
140 | CC_NO_INLINE CC_NONNULL2 | |
141 | uint8_t *ccder_encode_tag(ccder_tag tag, const uint8_t *der, uint8_t *der_end); | |
142 | ||
143 | /* Returns a pointer to the start of the len field. returns NULL if there | |
144 | is an encoding error. */ | |
145 | CC_NO_INLINE CC_NONNULL2 | |
146 | uint8_t * | |
147 | ccder_encode_len(size_t len, const uint8_t *der, uint8_t *der_end); | |
148 | ||
149 | /* der_end should point to the first byte of the content of this der item. */ | |
150 | CC_NO_INLINE CC_NONNULL3 | |
151 | uint8_t * | |
152 | ccder_encode_tl(ccder_tag tag, size_t len, const uint8_t *der, uint8_t *der_end); | |
153 | ||
154 | CC_NO_INLINE CC_PURE CC_NONNULL2 | |
155 | uint8_t * | |
156 | ccder_encode_body_nocopy(size_t size, const uint8_t *der, uint8_t *der_end); | |
157 | ||
158 | /* Encode the tag and length of a constructed object. der is the lower | |
159 | bound, der_end is one byte paste where we want to write the length and | |
160 | body_end is one byte past the end of the body of the der object we are | |
161 | encoding the tag and length of. */ | |
162 | CC_NO_INLINE CC_NONNULL((2, 3)) | |
163 | uint8_t * | |
164 | ccder_encode_constructed_tl(ccder_tag tag, const uint8_t *body_end, | |
165 | const uint8_t *der, uint8_t *der_end); | |
166 | ||
167 | /* Encodes oid into der and returns | |
168 | der + ccder_sizeof_oid(oid). */ | |
169 | CC_NO_INLINE CC_NONNULL_TU((1)) CC_NONNULL2 | |
170 | uint8_t *ccder_encode_oid(ccoid_t oid, const uint8_t *der, uint8_t *der_end); | |
171 | ||
172 | CC_NO_INLINE CC_NONNULL((3, 4)) | |
173 | uint8_t *ccder_encode_implicit_integer(ccder_tag implicit_tag, | |
174 | cc_size n, const cc_unit *s, | |
175 | const uint8_t *der, uint8_t *der_end); | |
176 | ||
177 | CC_NO_INLINE CC_NONNULL((2, 3)) | |
178 | uint8_t *ccder_encode_integer(cc_size n, const cc_unit *s, | |
179 | const uint8_t *der, uint8_t *der_end); | |
180 | ||
181 | CC_NO_INLINE CC_NONNULL3 | |
182 | uint8_t *ccder_encode_implicit_uint64(ccder_tag implicit_tag, | |
183 | uint64_t value, | |
184 | const uint8_t *der, uint8_t *der_end); | |
185 | ||
186 | CC_NO_INLINE CC_NONNULL2 | |
187 | uint8_t *ccder_encode_uint64(uint64_t value, | |
188 | const uint8_t *der, uint8_t *der_end); | |
189 | ||
190 | CC_NO_INLINE CC_NONNULL((3, 4)) | |
191 | uint8_t *ccder_encode_implicit_octet_string(ccder_tag implicit_tag, | |
192 | cc_size n, const cc_unit *s, | |
193 | const uint8_t *der, | |
194 | uint8_t *der_end); | |
195 | ||
196 | CC_NO_INLINE CC_NONNULL((2, 3)) | |
197 | uint8_t *ccder_encode_octet_string(cc_size n, const cc_unit *s, | |
198 | const uint8_t *der, uint8_t *der_end); | |
199 | ||
200 | CC_NO_INLINE CC_NONNULL((3, 4)) | |
201 | uint8_t *ccder_encode_implicit_raw_octet_string(ccder_tag implicit_tag, | |
202 | size_t s_size, const uint8_t *s, | |
203 | const uint8_t *der, | |
204 | uint8_t *der_end); | |
205 | ||
206 | CC_NO_INLINE CC_NONNULL((2, 3)) | |
207 | uint8_t *ccder_encode_raw_octet_string(size_t s_size, const uint8_t *s, | |
208 | const uint8_t *der, uint8_t *der_end); | |
209 | ||
210 | /* ccder_encode_body COPIES the body into the der. | |
211 | It's inefficient – especially when you already have to convert to get to | |
212 | the form for the body. | |
213 | see encode integer for the right way to unify conversion and insertion */ | |
214 | CC_NO_INLINE CC_NONNULL3 | |
215 | uint8_t * | |
216 | ccder_encode_body(size_t size, const uint8_t* body, | |
217 | const uint8_t *der, uint8_t *der_end); | |
218 | ||
219 | // MARK: ccder_decode_ functions. | |
220 | ||
221 | /* Returns a pointer to the start of the length field, and returns the decoded tag in tag. | |
222 | returns NULL if there is a decoding error. */ | |
223 | CC_NO_INLINE CC_NONNULL((1, 3)) | |
224 | const uint8_t *ccder_decode_tag(ccder_tag *tagp, const uint8_t *der, const uint8_t *der_end); | |
225 | ||
226 | CC_NO_INLINE CC_NONNULL((1, 3)) | |
227 | const uint8_t *ccder_decode_len(size_t *lenp, const uint8_t *der, const uint8_t *der_end); | |
228 | ||
229 | /* Returns a pointer to the start of the der object, and returns the length in len. | |
230 | returns NULL if there is a decoding error. */ | |
231 | CC_NO_INLINE CC_NONNULL((2, 4)) | |
232 | const uint8_t *ccder_decode_tl(ccder_tag expected_tag, size_t *lenp, | |
233 | const uint8_t *der, const uint8_t *der_end); | |
234 | ||
235 | CC_NO_INLINE CC_NONNULL((2, 4)) | |
236 | const uint8_t * | |
237 | ccder_decode_constructed_tl(ccder_tag expected_tag, const uint8_t **body_end, | |
238 | const uint8_t *der, const uint8_t *der_end); | |
239 | ||
240 | CC_NO_INLINE CC_NONNULL((1, 3)) | |
241 | const uint8_t * | |
242 | ccder_decode_sequence_tl(const uint8_t **body_end, | |
243 | const uint8_t *der, const uint8_t *der_end); | |
244 | ||
245 | CC_NO_INLINE CC_NONNULL((2, 4)) | |
246 | const uint8_t *ccder_decode_uint(cc_size n, cc_unit *r, | |
247 | const uint8_t *der, const uint8_t *der_end); | |
248 | ||
249 | CC_NO_INLINE CC_NONNULL((1, 3)) | |
250 | const uint8_t *ccder_decode_uint64(uint64_t* r, | |
251 | const uint8_t *der, const uint8_t *der_end); | |
252 | ||
253 | /* Decode SEQUENCE { r, s -- (unsigned)integer } in der into r and s. | |
254 | Returns NULL on decode errors, returns pointer just past the end of the | |
255 | sequence of integers otherwise. */ | |
256 | CC_NO_INLINE CC_NONNULL((2, 3, 5)) | |
257 | const uint8_t *ccder_decode_seqii(cc_size n, cc_unit *r, cc_unit *s, | |
258 | const uint8_t *der, const uint8_t *der_end); | |
259 | CC_NO_INLINE CC_NONNULL_TU((1)) CC_NONNULL((3)) | |
260 | const uint8_t *ccder_decode_oid(ccoid_t *oidp, | |
261 | const uint8_t *der, const uint8_t *der_end); | |
262 | ||
263 | CC_NO_INLINE CC_NONNULL_ALL | |
264 | const uint8_t *ccder_decode_bitstring(const uint8_t **bit_string, | |
265 | size_t *bit_length, | |
266 | const uint8_t *der, const uint8_t *der_end); | |
267 | ||
268 | CC_NO_INLINE CC_NONNULL_ALL | |
269 | const uint8_t *ccder_decode_eckey(uint64_t *version, | |
270 | size_t *priv_size, const uint8_t **priv_key, | |
271 | ccoid_t *oid, | |
272 | size_t *pub_size, const uint8_t **pub_key, | |
273 | const uint8_t *der, const uint8_t *der_end); | |
274 | ||
275 | #ifndef CCDER_MULTIBYTE_TAGS | |
276 | #include <corecrypto/ccder_decode_constructed_tl.h> | |
277 | #include <corecrypto/ccder_decode_len.h> | |
278 | #include <corecrypto/ccder_decode_oid.h> | |
279 | #include <corecrypto/ccder_decode_seqii.h> | |
280 | #include <corecrypto/ccder_decode_sequence_tl.h> | |
281 | #include <corecrypto/ccder_decode_tag.h> | |
282 | #include <corecrypto/ccder_decode_tl.h> | |
283 | #include <corecrypto/ccder_decode_uint.h> | |
284 | #include <corecrypto/ccder_decode_bitstring.h> | |
285 | #include <corecrypto/ccder_decode_eckey.h> | |
286 | #include <corecrypto/ccder_encode_body_nocopy.h> | |
287 | #include <corecrypto/ccder_encode_constructed_tl.h> | |
288 | #include <corecrypto/ccder_encode_implicit_integer.h> | |
289 | #include <corecrypto/ccder_encode_implicit_octet_string.h> | |
290 | #include <corecrypto/ccder_encode_implicit_uint64.h> | |
291 | #include <corecrypto/ccder_encode_integer.h> | |
292 | #include <corecrypto/ccder_encode_len.h> | |
293 | #include <corecrypto/ccder_encode_octet_string.h> | |
294 | #include <corecrypto/ccder_encode_oid.h> | |
295 | #include <corecrypto/ccder_encode_tag.h> | |
296 | #include <corecrypto/ccder_encode_tl.h> | |
297 | #include <corecrypto/ccder_encode_uint64.h> | |
298 | #include <corecrypto/ccder_sizeof.h> | |
299 | #include <corecrypto/ccder_sizeof_integer.h> | |
300 | #include <corecrypto/ccder_sizeof_len.h> | |
301 | #include <corecrypto/ccder_sizeof_oid.h> | |
302 | #include <corecrypto/ccder_sizeof_tag.h> | |
303 | #endif | |
304 | ||
305 | #endif /* _CORECRYPTO_CCDER_H_ */ |