summaryrefslogtreecommitdiff
path: root/src/lib/libssl/bytestring.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/bytestring.h')
-rw-r--r--src/lib/libssl/bytestring.h346
1 files changed, 346 insertions, 0 deletions
diff --git a/src/lib/libssl/bytestring.h b/src/lib/libssl/bytestring.h
new file mode 100644
index 0000000000..3c4e8eaaf3
--- /dev/null
+++ b/src/lib/libssl/bytestring.h
@@ -0,0 +1,346 @@
1/* $OpenBSD: bytestring.h,v 1.1 2015/02/06 09:36:16 doug Exp $ */
2/*
3 * Copyright (c) 2014, Google Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
16
17#ifndef OPENSSL_HEADER_BYTESTRING_H
18#define OPENSSL_HEADER_BYTESTRING_H
19
20#if defined(__cplusplus)
21extern "C" {
22#endif
23
24#include <sys/types.h>
25#include <stdint.h>
26
27#include <openssl/opensslconf.h>
28
29/* Bytestrings are used for parsing and building TLS and ASN.1 messages.
30 *
31 * A "CBS" (CRYPTO ByteString) represents a string of bytes in memory and
32 * provides utility functions for safely parsing length-prefixed structures
33 * like TLS and ASN.1 from it.
34 *
35 * A "CBB" (CRYPTO ByteBuilder) is a memory buffer that grows as needed and
36 * provides utility functions for building length-prefixed messages. */
37
38
39/* CRYPTO ByteString */
40
41typedef struct cbs_st {
42 const uint8_t *data;
43 size_t len;
44} CBS;
45
46/* CBS_init sets |cbs| to point to |data|. It does not take ownership of
47 * |data|. */
48void CBS_init(CBS *cbs, const uint8_t *data, size_t len);
49
50/* CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero
51 * otherwise. */
52int CBS_skip(CBS *cbs, size_t len);
53
54/* CBS_data returns a pointer to the contains of |cbs|. */
55const uint8_t *CBS_data(const CBS *cbs);
56
57/* CBS_len returns the number of bytes remaining in |cbs|. */
58size_t CBS_len(const CBS *cbs);
59
60/* CBS_stow copies the current contents of |cbs| into |*out_ptr| and
61 * |*out_len|. If |*out_ptr| is not NULL, the contents are freed with
62 * OPENSSL_free. It returns one on success and zero on allocation failure. On
63 * success, |*out_ptr| should be freed with OPENSSL_free. If |cbs| is empty,
64 * |*out_ptr| will be NULL. */
65int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len);
66
67/* CBS_strdup copies the current contents of |cbs| into |*out_ptr| as a
68 * NUL-terminated C string. If |*out_ptr| is not NULL, the contents are freed
69 * with OPENSSL_free. It returns one on success and zero on allocation
70 * failure. On success, |*out_ptr| should be freed with OPENSSL_free.
71 *
72 * NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call
73 * |CBS_contains_zero_byte(cbs)| to check for NUL bytes. */
74int CBS_strdup(const CBS *cbs, char **out_ptr);
75
76/* CBS_contains_zero_byte returns one if the current contents of |cbs| contains
77 * a NUL byte and zero otherwise. */
78int CBS_contains_zero_byte(const CBS *cbs);
79
80/* CBS_mem_equal compares the current contents of |cbs| with the |len| bytes
81 * starting at |data|. If they're equal, it returns one, otherwise zero. If the
82 * lengths match, it uses a constant-time comparison. */
83int CBS_mem_equal(const CBS *cbs, const uint8_t *data,
84 size_t len);
85
86/* CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It
87 * returns one on success and zero on error. */
88int CBS_get_u8(CBS *cbs, uint8_t *out);
89
90/* CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and
91 * advances |cbs|. It returns one on success and zero on error. */
92int CBS_get_u16(CBS *cbs, uint16_t *out);
93
94/* CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and
95 * advances |cbs|. It returns one on success and zero on error. */
96int CBS_get_u24(CBS *cbs, uint32_t *out);
97
98/* CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs|
99 * and advances |cbs|. It returns one on success and zero on error. */
100int CBS_get_u32(CBS *cbs, uint32_t *out);
101
102/* CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances
103 * |cbs|. It returns one on success and zero on error. */
104int CBS_get_bytes(CBS *cbs, CBS *out, size_t len);
105
106/* CBS_get_u8_length_prefixed sets |*out| to the contents of an 8-bit,
107 * length-prefixed value from |cbs| and advances |cbs| over it. It returns one
108 * on success and zero on error. */
109int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out);
110
111/* CBS_get_u16_length_prefixed sets |*out| to the contents of a 16-bit,
112 * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It
113 * returns one on success and zero on error. */
114int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out);
115
116/* CBS_get_u24_length_prefixed sets |*out| to the contents of a 24-bit,
117 * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It
118 * returns one on success and zero on error. */
119int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out);
120
121
122/* Parsing ASN.1 */
123
124#define CBS_ASN1_BOOLEAN 0x1
125#define CBS_ASN1_INTEGER 0x2
126#define CBS_ASN1_BITSTRING 0x3
127#define CBS_ASN1_OCTETSTRING 0x4
128#define CBS_ASN1_OBJECT 0x6
129#define CBS_ASN1_ENUMERATED 0xa
130#define CBS_ASN1_SEQUENCE (0x10 | CBS_ASN1_CONSTRUCTED)
131#define CBS_ASN1_SET (0x11 | CBS_ASN1_CONSTRUCTED)
132
133#define CBS_ASN1_CONSTRUCTED 0x20
134#define CBS_ASN1_CONTEXT_SPECIFIC 0x80
135
136/* CBS_get_asn1 sets |*out| to the contents of DER-encoded, ASN.1 element (not
137 * including tag and length bytes) and advances |cbs| over it. The ASN.1
138 * element must match |tag_value|. It returns one on success and zero
139 * on error.
140 *
141 * Tag numbers greater than 31 are not supported. */
142int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value);
143
144/* CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the
145 * ASN.1 header bytes too. */
146int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value);
147
148/* CBS_peek_asn1_tag looks ahead at the next ASN.1 tag and returns one
149 * if the next ASN.1 element on |cbs| would have tag |tag_value|. If
150 * |cbs| is empty or the tag does not match, it returns zero. Note: if
151 * it returns one, CBS_get_asn1 may still fail if the rest of the
152 * element is malformed. */
153int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value);
154
155/* CBS_get_any_asn1_element sets |*out| to contain the next ASN.1 element from
156 * |*cbs| (including header bytes) and advances |*cbs|. It sets |*out_tag| to
157 * the tag number and |*out_header_len| to the length of the ASN.1 header. If
158 * the element has indefinite length then |*out| will only contain the
159 * header. Each of |out|, |out_tag|, and |out_header_len| may be NULL to ignore
160 * the value.
161 *
162 * Tag numbers greater than 31 are not supported. */
163int CBS_get_any_asn1_element(CBS *cbs, CBS *out,
164 unsigned *out_tag,
165 size_t *out_header_len);
166
167/* CBS_get_asn1_uint64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1|
168 * and sets |*out| to its value. It returns one on success and zero on error,
169 * where error includes the integer being negative, or too large to represent
170 * in 64 bits. */
171int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out);
172
173/* CBS_get_optional_asn1 gets an optional explicitly-tagged element
174 * from |cbs| tagged with |tag| and sets |*out| to its contents. If
175 * present, it sets |*out_present| to one, otherwise zero. It returns
176 * one on success, whether or not the element was present, and zero on
177 * decode failure. */
178int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present,
179 unsigned tag);
180
181/* CBS_get_optional_asn1_octet_string gets an optional
182 * explicitly-tagged OCTET STRING from |cbs|. If present, it sets
183 * |*out| to the string and |*out_present| to one. Otherwise, it sets
184 * |*out| to empty and |*out_present| to zero. |out_present| may be
185 * NULL. It returns one on success, whether or not the element was
186 * present, and zero on decode failure. */
187int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out,
188 int *out_present,
189 unsigned tag);
190
191/* CBS_get_optional_asn1_uint64 gets an optional explicitly-tagged
192 * INTEGER from |cbs|. If present, it sets |*out| to the
193 * value. Otherwise, it sets |*out| to |default_value|. It returns one
194 * on success, whether or not the element was present, and zero on
195 * decode failure. */
196int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out,
197 unsigned tag,
198 uint64_t default_value);
199
200/* CBS_get_optional_asn1_bool gets an optional, explicitly-tagged BOOLEAN from
201 * |cbs|. If present, it sets |*out| to either zero or one, based on the
202 * boolean. Otherwise, it sets |*out| to |default_value|. It returns one on
203 * success, whether or not the element was present, and zero on decode
204 * failure. */
205int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
206 int default_value);
207
208
209/* CRYPTO ByteBuilder.
210 *
211 * |CBB| objects allow one to build length-prefixed serialisations. A |CBB|
212 * object is associated with a buffer and new buffers are created with
213 * |CBB_init|. Several |CBB| objects can point at the same buffer when a
214 * length-prefix is pending, however only a single |CBB| can be 'current' at
215 * any one time. For example, if one calls |CBB_add_u8_length_prefixed| then
216 * the new |CBB| points at the same buffer as the original. But if the original
217 * |CBB| is used then the length prefix is written out and the new |CBB| must
218 * not be used again.
219 *
220 * If one needs to force a length prefix to be written out because a |CBB| is
221 * going out of scope, use |CBB_flush|. */
222
223struct cbb_buffer_st {
224 uint8_t *buf;
225 size_t len; /* The number of valid bytes. */
226 size_t cap; /* The size of buf. */
227 char can_resize; /* One iff |buf| is owned by this object. If not then |buf|
228 cannot be resized. */
229};
230
231typedef struct cbb_st {
232 struct cbb_buffer_st *base;
233 /* offset is the offset from the start of |base->buf| to the position of any
234 * pending length-prefix. */
235 size_t offset;
236 /* child points to a child CBB if a length-prefix is pending. */
237 struct cbb_st *child;
238 /* pending_len_len contains the number of bytes in a pending length-prefix,
239 * or zero if no length-prefix is pending. */
240 uint8_t pending_len_len;
241 char pending_is_asn1;
242 /* is_top_level is true iff this is a top-level |CBB| (as opposed to a child
243 * |CBB|). Top-level objects are valid arguments for |CBB_finish|. */
244 char is_top_level;
245} CBB;
246
247/* CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as
248 * needed, the |initial_capacity| is just a hint. It returns one on success or
249 * zero on error. */
250int CBB_init(CBB *cbb, size_t initial_capacity);
251
252/* CBB_init_fixed initialises |cbb| to write to |len| bytes at |buf|. Since
253 * |buf| cannot grow, trying to write more than |len| bytes will cause CBB
254 * functions to fail. It returns one on success or zero on error. */
255int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len);
256
257/* CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects
258 * writing to the same buffer. This should be used in an error case where a
259 * serialisation is abandoned. */
260void CBB_cleanup(CBB *cbb);
261
262/* CBB_finish completes any pending length prefix and sets |*out_data| to a
263 * malloced buffer and |*out_len| to the length of that buffer. The caller
264 * takes ownership of the buffer and, unless the buffer was fixed with
265 * |CBB_init_fixed|, must call |OPENSSL_free| when done.
266 *
267 * It can only be called on a "top level" |CBB|, i.e. one initialised with
268 * |CBB_init| or |CBB_init_fixed|. It returns one on success and zero on
269 * error. */
270int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len);
271
272/* CBB_flush causes any pending length prefixes to be written out and any child
273 * |CBB| objects of |cbb| to be invalidated. It returns one on success or zero
274 * on error. */
275int CBB_flush(CBB *cbb);
276
277/* CBB_add_u8_length_prefixed sets |*out_contents| to a new child of |cbb|. The
278 * data written to |*out_contents| will be prefixed in |cbb| with an 8-bit
279 * length. It returns one on success or zero on error. */
280int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents);
281
282/* CBB_add_u16_length_prefixed sets |*out_contents| to a new child of |cbb|.
283 * The data written to |*out_contents| will be prefixed in |cbb| with a 16-bit,
284 * big-endian length. It returns one on success or zero on error. */
285int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents);
286
287/* CBB_add_u24_length_prefixed sets |*out_contents| to a new child of |cbb|.
288 * The data written to |*out_contents| will be prefixed in |cbb| with a 24-bit,
289 * big-endian length. It returns one on success or zero on error. */
290int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents);
291
292/* CBB_add_asn sets |*out_contents| to a |CBB| into which the contents of an
293 * ASN.1 object can be written. The |tag| argument will be used as the tag for
294 * the object. It returns one on success or zero on error. */
295int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag);
296
297/* CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on
298 * success and zero otherwise. */
299int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len);
300
301/* CBB_add_space appends |len| bytes to |cbb| and sets |*out_data| to point to
302 * the beginning of that space. The caller must then write |len| bytes of
303 * actual contents to |*out_data|. It returns one on success and zero
304 * otherwise. */
305int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len);
306
307/* CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on
308 * success and zero otherwise. */
309int CBB_add_u8(CBB *cbb, uint8_t value);
310
311/* CBB_add_u8 appends a 16-bit, big-endian number from |value| to |cbb|. It
312 * returns one on success and zero otherwise. */
313int CBB_add_u16(CBB *cbb, uint16_t value);
314
315/* CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It
316 * returns one on success and zero otherwise. */
317int CBB_add_u24(CBB *cbb, uint32_t value);
318
319/* CBB_add_asn1_uint64 writes an ASN.1 INTEGER into |cbb| using |CBB_add_asn1|
320 * and writes |value| in its contents. It returns one on success and zero on
321 * error. */
322int CBB_add_asn1_uint64(CBB *cbb, uint64_t value);
323
324#ifdef LIBRESSL_INTERNAL
325/* CBS_asn1_ber_to_der reads an ASN.1 structure from |in|. If it finds
326 * indefinite-length elements then it attempts to convert the BER data to DER
327 * and sets |*out| and |*out_length| to describe a malloced buffer containing
328 * the DER data. Additionally, |*in| will be advanced over the ASN.1 data.
329 *
330 * If it doesn't find any indefinite-length elements then it sets |*out| to
331 * NULL and |*in| is unmodified.
332 *
333 * A sufficiently complex ASN.1 structure will break this function because it's
334 * not possible to generically convert BER to DER without knowledge of the
335 * structure itself. However, this sufficies to handle the PKCS#7 and #12 output
336 * from NSS.
337 *
338 * It returns one on success and zero otherwise. */
339int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len);
340#endif /* LIBRESSL_INTERNAL */
341
342#if defined(__cplusplus)
343} /* extern C */
344#endif
345
346#endif /* OPENSSL_HEADER_BYTESTRING_H */