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