blob: 66f620463e54460eac76314469f62e5cf3fd468c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#ifndef OPENSSL_HEADER_BYTESTRING_H
16#define OPENSSL_HEADER_BYTESTRING_H
17
18#include <openssl/base.h>
19
David Benjamin6b3ab722017-09-21 02:37:58 -040020#include <openssl/span.h>
21
Adam Langley95c29f32014-06-20 12:00:00 -070022#if defined(__cplusplus)
23extern "C" {
24#endif
25
26
David Benjamin4512b792017-08-18 19:21:50 -040027// Bytestrings are used for parsing and building TLS and ASN.1 messages.
28//
29// A "CBS" (CRYPTO ByteString) represents a string of bytes in memory and
30// provides utility functions for safely parsing length-prefixed structures
31// like TLS and ASN.1 from it.
32//
33// A "CBB" (CRYPTO ByteBuilder) is a memory buffer that grows as needed and
34// provides utility functions for building length-prefixed messages.
Adam Langley95c29f32014-06-20 12:00:00 -070035
36
David Benjamin4512b792017-08-18 19:21:50 -040037// CRYPTO ByteString
Adam Langley95c29f32014-06-20 12:00:00 -070038
39struct cbs_st {
40 const uint8_t *data;
41 size_t len;
David Benjamin6b3ab722017-09-21 02:37:58 -040042
43#if !defined(BORINGSSL_NO_CXX)
44 // Allow implicit conversions to bssl::Span<const uint8_t>.
45 operator bssl::Span<const uint8_t>() const {
46 return bssl::MakeConstSpan(data, len);
47 }
48#endif
Adam Langley95c29f32014-06-20 12:00:00 -070049};
50
David Benjamin4512b792017-08-18 19:21:50 -040051// CBS_init sets |cbs| to point to |data|. It does not take ownership of
52// |data|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070053OPENSSL_EXPORT void CBS_init(CBS *cbs, const uint8_t *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -070054
David Benjamin4512b792017-08-18 19:21:50 -040055// CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero
56// otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070057OPENSSL_EXPORT int CBS_skip(CBS *cbs, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -070058
David Benjamin4512b792017-08-18 19:21:50 -040059// CBS_data returns a pointer to the contents of |cbs|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070060OPENSSL_EXPORT const uint8_t *CBS_data(const CBS *cbs);
Adam Langley95c29f32014-06-20 12:00:00 -070061
David Benjamin4512b792017-08-18 19:21:50 -040062// CBS_len returns the number of bytes remaining in |cbs|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070063OPENSSL_EXPORT size_t CBS_len(const CBS *cbs);
Adam Langley95c29f32014-06-20 12:00:00 -070064
David Benjamin4512b792017-08-18 19:21:50 -040065// CBS_stow copies the current contents of |cbs| into |*out_ptr| and
66// |*out_len|. If |*out_ptr| is not NULL, the contents are freed with
67// OPENSSL_free. It returns one on success and zero on allocation failure. On
68// success, |*out_ptr| should be freed with OPENSSL_free. If |cbs| is empty,
69// |*out_ptr| will be NULL.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070070OPENSSL_EXPORT int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len);
David Benjamin03973092014-06-24 23:27:17 -040071
David Benjamin4512b792017-08-18 19:21:50 -040072// CBS_strdup copies the current contents of |cbs| into |*out_ptr| as a
73// NUL-terminated C string. If |*out_ptr| is not NULL, the contents are freed
74// with OPENSSL_free. It returns one on success and zero on allocation
75// failure. On success, |*out_ptr| should be freed with OPENSSL_free.
76//
77// NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call
78// |CBS_contains_zero_byte(cbs)| to check for NUL bytes.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070079OPENSSL_EXPORT int CBS_strdup(const CBS *cbs, char **out_ptr);
David Benjamined439582014-07-14 19:13:02 -040080
David Benjamin4512b792017-08-18 19:21:50 -040081// CBS_contains_zero_byte returns one if the current contents of |cbs| contains
82// a NUL byte and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070083OPENSSL_EXPORT int CBS_contains_zero_byte(const CBS *cbs);
David Benjamined439582014-07-14 19:13:02 -040084
David Benjamin4512b792017-08-18 19:21:50 -040085// CBS_mem_equal compares the current contents of |cbs| with the |len| bytes
86// starting at |data|. If they're equal, it returns one, otherwise zero. If the
87// lengths match, it uses a constant-time comparison.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070088OPENSSL_EXPORT int CBS_mem_equal(const CBS *cbs, const uint8_t *data,
89 size_t len);
David Benjamin22f9bcc2014-07-13 12:29:21 -040090
David Benjamin4512b792017-08-18 19:21:50 -040091// CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It
92// returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070093OPENSSL_EXPORT int CBS_get_u8(CBS *cbs, uint8_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -070094
David Benjamin4512b792017-08-18 19:21:50 -040095// CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and
96// advances |cbs|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070097OPENSSL_EXPORT int CBS_get_u16(CBS *cbs, uint16_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -070098
David Benjamin4512b792017-08-18 19:21:50 -040099// CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and
100// advances |cbs|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700101OPENSSL_EXPORT int CBS_get_u24(CBS *cbs, uint32_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700102
David Benjamin4512b792017-08-18 19:21:50 -0400103// CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs|
104// and advances |cbs|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700105OPENSSL_EXPORT int CBS_get_u32(CBS *cbs, uint32_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700106
David Benjamin4512b792017-08-18 19:21:50 -0400107// CBS_get_last_u8 sets |*out| to the last uint8_t from |cbs| and shortens
108// |cbs|. It returns one on success and zero on error.
David Benjamina7810c12016-06-06 18:54:51 -0400109OPENSSL_EXPORT int CBS_get_last_u8(CBS *cbs, uint8_t *out);
110
David Benjamin4512b792017-08-18 19:21:50 -0400111// CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances
112// |cbs|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700113OPENSSL_EXPORT int CBS_get_bytes(CBS *cbs, CBS *out, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700114
David Benjamin4512b792017-08-18 19:21:50 -0400115// CBS_copy_bytes copies the next |len| bytes from |cbs| to |out| and advances
116// |cbs|. It returns one on success and zero on error.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400117OPENSSL_EXPORT int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len);
118
David Benjamin4512b792017-08-18 19:21:50 -0400119// CBS_get_u8_length_prefixed sets |*out| to the contents of an 8-bit,
120// length-prefixed value from |cbs| and advances |cbs| over it. It returns one
121// on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700122OPENSSL_EXPORT int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700123
David Benjamin4512b792017-08-18 19:21:50 -0400124// CBS_get_u16_length_prefixed sets |*out| to the contents of a 16-bit,
125// big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It
126// returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700127OPENSSL_EXPORT int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjamin4512b792017-08-18 19:21:50 -0400129// CBS_get_u24_length_prefixed sets |*out| to the contents of a 24-bit,
130// big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It
131// returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700132OPENSSL_EXPORT int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700133
134
David Benjamin4512b792017-08-18 19:21:50 -0400135// Parsing ASN.1
Adam Langley95c29f32014-06-20 12:00:00 -0700136
David Benjamin4512b792017-08-18 19:21:50 -0400137// The following values are tag numbers for UNIVERSAL elements.
David Benjamin2c45fa02017-04-11 23:07:13 -0400138#define CBS_ASN1_BOOLEAN 0x1u
139#define CBS_ASN1_INTEGER 0x2u
140#define CBS_ASN1_BITSTRING 0x3u
141#define CBS_ASN1_OCTETSTRING 0x4u
142#define CBS_ASN1_NULL 0x5u
143#define CBS_ASN1_OBJECT 0x6u
144#define CBS_ASN1_ENUMERATED 0xau
145#define CBS_ASN1_UTF8STRING 0xcu
146#define CBS_ASN1_SEQUENCE (0x10u | CBS_ASN1_CONSTRUCTED)
147#define CBS_ASN1_SET (0x11u | CBS_ASN1_CONSTRUCTED)
148#define CBS_ASN1_NUMERICSTRING 0x12u
149#define CBS_ASN1_PRINTABLESTRING 0x13u
150#define CBS_ASN1_T61STRING 0x14u
151#define CBS_ASN1_VIDEOTEXSTRING 0x15u
152#define CBS_ASN1_IA5STRING 0x16u
153#define CBS_ASN1_UTCTIME 0x17u
154#define CBS_ASN1_GENERALIZEDTIME 0x18u
155#define CBS_ASN1_GRAPHICSTRING 0x19u
156#define CBS_ASN1_VISIBLESTRING 0x1au
157#define CBS_ASN1_GENERALSTRING 0x1bu
158#define CBS_ASN1_UNIVERSALSTRING 0x1cu
159#define CBS_ASN1_BMPSTRING 0x1eu
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin4512b792017-08-18 19:21:50 -0400161// CBS_ASN1_CONSTRUCTED may be ORed into a tag to toggle the constructed
162// bit. |CBS| and |CBB| APIs consider the constructed bit to be part of the
163// tag.
David Benjamin2c45fa02017-04-11 23:07:13 -0400164#define CBS_ASN1_CONSTRUCTED 0x20u
David Benjamin1db42fb2016-08-19 16:08:45 -0400165
David Benjamin4512b792017-08-18 19:21:50 -0400166// The following values specify the constructed bit or tag class and may be ORed
167// into a tag number to produce the final tag. If none is used, the tag will be
168// UNIVERSAL.
169//
170// Note that although they currently match the DER serialization, consumers must
171// use these bits rather than make assumptions about the representation. This is
172// to allow for tag numbers beyond 31 in the future.
David Benjamin2c45fa02017-04-11 23:07:13 -0400173#define CBS_ASN1_APPLICATION 0x40u
174#define CBS_ASN1_CONTEXT_SPECIFIC 0x80u
175#define CBS_ASN1_PRIVATE 0xc0u
David Benjamin1db42fb2016-08-19 16:08:45 -0400176
David Benjamin4512b792017-08-18 19:21:50 -0400177// CBS_ASN1_CLASS_MASK may be ANDed with a tag to query its class.
David Benjamin2c45fa02017-04-11 23:07:13 -0400178#define CBS_ASN1_CLASS_MASK 0xc0u
David Benjamin1db42fb2016-08-19 16:08:45 -0400179
David Benjamin4512b792017-08-18 19:21:50 -0400180// CBS_ASN1_TAG_NUMBER_MASK may be ANDed with a tag to query its number.
David Benjamin2c45fa02017-04-11 23:07:13 -0400181#define CBS_ASN1_TAG_NUMBER_MASK 0x1fu
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjamin4512b792017-08-18 19:21:50 -0400183// CBS_get_asn1 sets |*out| to the contents of DER-encoded, ASN.1 element (not
184// including tag and length bytes) and advances |cbs| over it. The ASN.1
185// element must match |tag_value|. It returns one on success and zero
186// on error.
187//
188// Tag numbers greater than 30 are not supported (i.e. short form only).
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700189OPENSSL_EXPORT int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value);
Adam Langley95c29f32014-06-20 12:00:00 -0700190
David Benjamin4512b792017-08-18 19:21:50 -0400191// CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the
192// ASN.1 header bytes too.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700193OPENSSL_EXPORT int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value);
Adam Langley95c29f32014-06-20 12:00:00 -0700194
David Benjamin4512b792017-08-18 19:21:50 -0400195// CBS_peek_asn1_tag looks ahead at the next ASN.1 tag and returns one
196// if the next ASN.1 element on |cbs| would have tag |tag_value|. If
197// |cbs| is empty or the tag does not match, it returns zero. Note: if
198// it returns one, CBS_get_asn1 may still fail if the rest of the
199// element is malformed.
David Benjaminb6986172014-10-09 19:39:15 -0400200OPENSSL_EXPORT int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value);
201
David Benjamin4512b792017-08-18 19:21:50 -0400202// CBS_get_any_asn1 sets |*out| to contain the next ASN.1 element from |*cbs|
203// (not including tag and length bytes), sets |*out_tag| to the tag number, and
204// advances |*cbs|. It returns one on success and zero on error. Either of |out|
205// and |out_tag| may be NULL to ignore the value.
206//
207// Tag numbers greater than 30 are not supported (i.e. short form only).
David Benjamin455919d2016-09-30 15:05:32 -0400208OPENSSL_EXPORT int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag);
209
David Benjamin4512b792017-08-18 19:21:50 -0400210// CBS_get_any_asn1_element sets |*out| to contain the next ASN.1 element from
211// |*cbs| (including header bytes) and advances |*cbs|. It sets |*out_tag| to
212// the tag number and |*out_header_len| to the length of the ASN.1 header. Each
213// of |out|, |out_tag|, and |out_header_len| may be NULL to ignore the value.
214//
215// Tag numbers greater than 30 are not supported (i.e. short form only).
David Benjamin9b04d652014-08-27 20:19:50 -0400216OPENSSL_EXPORT int CBS_get_any_asn1_element(CBS *cbs, CBS *out,
217 unsigned *out_tag,
218 size_t *out_header_len);
219
David Benjamin4512b792017-08-18 19:21:50 -0400220// CBS_get_any_ber_asn1_element acts the same as |CBS_get_any_asn1_element| but
221// also allows indefinite-length elements to be returned. In that case,
222// |*out_header_len| and |CBS_len(out)| will both be two as only the header is
223// returned, otherwise it behaves the same as the previous function.
Adam Langley521d4b82015-06-05 14:27:00 -0700224OPENSSL_EXPORT int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out,
225 unsigned *out_tag,
226 size_t *out_header_len);
227
David Benjamin4512b792017-08-18 19:21:50 -0400228// CBS_get_asn1_uint64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1|
229// and sets |*out| to its value. It returns one on success and zero on error,
230// where error includes the integer being negative, or too large to represent
231// in 64 bits.
Adam Langley9c01e002014-08-21 10:54:06 -0700232OPENSSL_EXPORT int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out);
233
David Benjamin4512b792017-08-18 19:21:50 -0400234// CBS_get_optional_asn1 gets an optional explicitly-tagged element from |cbs|
235// tagged with |tag| and sets |*out| to its contents. If present and if
236// |out_present| is not NULL, it sets |*out_present| to one, otherwise zero. It
237// returns one on success, whether or not the element was present, and zero on
238// decode failure.
David Benjamin83fd6b62014-10-19 04:33:38 -0400239OPENSSL_EXPORT int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present,
240 unsigned tag);
241
David Benjamin4512b792017-08-18 19:21:50 -0400242// CBS_get_optional_asn1_octet_string gets an optional
243// explicitly-tagged OCTET STRING from |cbs|. If present, it sets
244// |*out| to the string and |*out_present| to one. Otherwise, it sets
245// |*out| to empty and |*out_present| to zero. |out_present| may be
246// NULL. It returns one on success, whether or not the element was
247// present, and zero on decode failure.
David Benjamin83fd6b62014-10-19 04:33:38 -0400248OPENSSL_EXPORT int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out,
249 int *out_present,
250 unsigned tag);
251
David Benjamin4512b792017-08-18 19:21:50 -0400252// CBS_get_optional_asn1_uint64 gets an optional explicitly-tagged
253// INTEGER from |cbs|. If present, it sets |*out| to the
254// value. Otherwise, it sets |*out| to |default_value|. It returns one
255// on success, whether or not the element was present, and zero on
256// decode failure.
David Benjamin83fd6b62014-10-19 04:33:38 -0400257OPENSSL_EXPORT int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out,
258 unsigned tag,
259 uint64_t default_value);
260
David Benjamin4512b792017-08-18 19:21:50 -0400261// CBS_get_optional_asn1_bool gets an optional, explicitly-tagged BOOLEAN from
262// |cbs|. If present, it sets |*out| to either zero or one, based on the
263// boolean. Otherwise, it sets |*out| to |default_value|. It returns one on
264// success, whether or not the element was present, and zero on decode
265// failure.
Adam Langley75712922014-10-10 16:23:43 -0700266OPENSSL_EXPORT int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
267 int default_value);
268
David Benjamin4512b792017-08-18 19:21:50 -0400269// CBS_is_valid_asn1_bitstring returns one if |cbs| is a valid ASN.1 BIT STRING
270// and zero otherwise.
David Benjaminfcd714d2016-12-13 20:48:19 -0500271OPENSSL_EXPORT int CBS_is_valid_asn1_bitstring(const CBS *cbs);
272
David Benjamin4512b792017-08-18 19:21:50 -0400273// CBS_asn1_bitstring_has_bit returns one if |cbs| is a valid ASN.1 BIT STRING
274// and the specified bit is present and set. Otherwise, it returns zero. |bit|
275// is indexed starting from zero.
David Benjaminfcd714d2016-12-13 20:48:19 -0500276OPENSSL_EXPORT int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit);
277
Adam Langley95c29f32014-06-20 12:00:00 -0700278
David Benjamin4512b792017-08-18 19:21:50 -0400279// CRYPTO ByteBuilder.
280//
281// |CBB| objects allow one to build length-prefixed serialisations. A |CBB|
282// object is associated with a buffer and new buffers are created with
283// |CBB_init|. Several |CBB| objects can point at the same buffer when a
284// length-prefix is pending, however only a single |CBB| can be 'current' at
285// any one time. For example, if one calls |CBB_add_u8_length_prefixed| then
286// the new |CBB| points at the same buffer as the original. But if the original
287// |CBB| is used then the length prefix is written out and the new |CBB| must
288// not be used again.
289//
290// If one needs to force a length prefix to be written out because a |CBB| is
291// going out of scope, use |CBB_flush|. If an operation on a |CBB| fails, it is
292// in an undefined state and must not be used except to call |CBB_cleanup|.
Adam Langley95c29f32014-06-20 12:00:00 -0700293
294struct cbb_buffer_st {
295 uint8_t *buf;
David Benjamin4512b792017-08-18 19:21:50 -0400296 size_t len; // The number of valid bytes.
297 size_t cap; // The size of buf.
Adam Langley95c29f32014-06-20 12:00:00 -0700298 char can_resize; /* One iff |buf| is owned by this object. If not then |buf|
299 cannot be resized. */
David Benjamin93a034a2016-07-19 07:08:24 +0200300 char error; /* One iff there was an error writing to this CBB. All future
301 operations will fail. */
Adam Langley95c29f32014-06-20 12:00:00 -0700302};
303
304struct cbb_st {
305 struct cbb_buffer_st *base;
David Benjamin4512b792017-08-18 19:21:50 -0400306 // child points to a child CBB if a length-prefix is pending.
David Benjamine6d1e5a2015-11-06 16:13:40 -0500307 CBB *child;
David Benjamin4512b792017-08-18 19:21:50 -0400308 // offset is the number of bytes from the start of |base->buf| to this |CBB|'s
309 // pending length prefix.
David Benjamina01deee2015-12-08 18:56:31 -0500310 size_t offset;
David Benjamin4512b792017-08-18 19:21:50 -0400311 // pending_len_len contains the number of bytes in this |CBB|'s pending
312 // length-prefix, or zero if no length-prefix is pending.
Adam Langley95c29f32014-06-20 12:00:00 -0700313 uint8_t pending_len_len;
314 char pending_is_asn1;
David Benjamin4512b792017-08-18 19:21:50 -0400315 // is_top_level is true iff this is a top-level |CBB| (as opposed to a child
316 // |CBB|). Top-level objects are valid arguments for |CBB_finish|.
Adam Langley95c29f32014-06-20 12:00:00 -0700317 char is_top_level;
318};
319
David Benjamin4512b792017-08-18 19:21:50 -0400320// CBB_zero sets an uninitialised |cbb| to the zero state. It must be
321// initialised with |CBB_init| or |CBB_init_fixed| before use, but it is safe to
322// call |CBB_cleanup| without a successful |CBB_init|. This may be used for more
323// uniform cleanup of a |CBB|.
David Benjamina8653202015-06-28 01:26:10 -0400324OPENSSL_EXPORT void CBB_zero(CBB *cbb);
325
David Benjamin4512b792017-08-18 19:21:50 -0400326// CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as
327// needed, the |initial_capacity| is just a hint. It returns one on success or
328// zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700329OPENSSL_EXPORT int CBB_init(CBB *cbb, size_t initial_capacity);
Adam Langley95c29f32014-06-20 12:00:00 -0700330
David Benjamin4512b792017-08-18 19:21:50 -0400331// CBB_init_fixed initialises |cbb| to write to |len| bytes at |buf|. Since
332// |buf| cannot grow, trying to write more than |len| bytes will cause CBB
333// functions to fail. It returns one on success or zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700334OPENSSL_EXPORT int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin4512b792017-08-18 19:21:50 -0400336// CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects
337// writing to the same buffer. This should be used in an error case where a
338// serialisation is abandoned.
339//
340// This function can only be called on a "top level" |CBB|, i.e. one initialised
341// with |CBB_init| or |CBB_init_fixed|, or a |CBB| set to the zero state with
342// |CBB_zero|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700343OPENSSL_EXPORT void CBB_cleanup(CBB *cbb);
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin4512b792017-08-18 19:21:50 -0400345// CBB_finish completes any pending length prefix and sets |*out_data| to a
346// malloced buffer and |*out_len| to the length of that buffer. The caller
347// takes ownership of the buffer and, unless the buffer was fixed with
348// |CBB_init_fixed|, must call |OPENSSL_free| when done.
349//
350// It can only be called on a "top level" |CBB|, i.e. one initialised with
351// |CBB_init| or |CBB_init_fixed|. It returns one on success and zero on
352// error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700353OPENSSL_EXPORT int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700354
David Benjamin4512b792017-08-18 19:21:50 -0400355// CBB_flush causes any pending length prefixes to be written out and any child
356// |CBB| objects of |cbb| to be invalidated. This allows |cbb| to continue to be
357// used after the children go out of scope, e.g. when local |CBB| objects are
358// added as children to a |CBB| that persists after a function returns. This
359// function returns one on success or zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700360OPENSSL_EXPORT int CBB_flush(CBB *cbb);
Adam Langley95c29f32014-06-20 12:00:00 -0700361
David Benjamin4512b792017-08-18 19:21:50 -0400362// CBB_data returns a pointer to the bytes written to |cbb|. It does not flush
363// |cbb|. The pointer is valid until the next operation to |cbb|.
364//
365// To avoid unfinalized length prefixes, it is a fatal error to call this on a
366// CBB with any active children.
David Benjamin2a0b3912015-12-18 01:01:21 -0500367OPENSSL_EXPORT const uint8_t *CBB_data(const CBB *cbb);
368
David Benjamin4512b792017-08-18 19:21:50 -0400369// CBB_len returns the number of bytes written to |cbb|. It does not flush
370// |cbb|.
371//
372// To avoid unfinalized length prefixes, it is a fatal error to call this on a
373// CBB with any active children.
Adam Langley614c66a2015-06-12 15:26:58 -0700374OPENSSL_EXPORT size_t CBB_len(const CBB *cbb);
375
David Benjamin4512b792017-08-18 19:21:50 -0400376// CBB_add_u8_length_prefixed sets |*out_contents| to a new child of |cbb|. The
377// data written to |*out_contents| will be prefixed in |cbb| with an 8-bit
378// length. It returns one on success or zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700379OPENSSL_EXPORT int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents);
Adam Langley95c29f32014-06-20 12:00:00 -0700380
David Benjamin4512b792017-08-18 19:21:50 -0400381// CBB_add_u16_length_prefixed sets |*out_contents| to a new child of |cbb|.
382// The data written to |*out_contents| will be prefixed in |cbb| with a 16-bit,
383// big-endian length. It returns one on success or zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700384OPENSSL_EXPORT int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents);
Adam Langley95c29f32014-06-20 12:00:00 -0700385
David Benjamin4512b792017-08-18 19:21:50 -0400386// CBB_add_u24_length_prefixed sets |*out_contents| to a new child of |cbb|.
387// The data written to |*out_contents| will be prefixed in |cbb| with a 24-bit,
388// big-endian length. It returns one on success or zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700389OPENSSL_EXPORT int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents);
Adam Langley95c29f32014-06-20 12:00:00 -0700390
David Benjamin4512b792017-08-18 19:21:50 -0400391// CBB_add_asn1 sets |*out_contents| to a |CBB| into which the contents of an
392// ASN.1 object can be written. The |tag| argument will be used as the tag for
393// the object. Passing in |tag| number 31 will return in an error since only
394// single octet identifiers are supported. It returns one on success or zero
395// on error.
David Benjamin1db42fb2016-08-19 16:08:45 -0400396OPENSSL_EXPORT int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag);
Adam Langley95c29f32014-06-20 12:00:00 -0700397
David Benjamin4512b792017-08-18 19:21:50 -0400398// CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on
399// success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700400OPENSSL_EXPORT int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700401
David Benjamin4512b792017-08-18 19:21:50 -0400402// CBB_add_space appends |len| bytes to |cbb| and sets |*out_data| to point to
403// the beginning of that space. The caller must then write |len| bytes of
404// actual contents to |*out_data|. It returns one on success and zero
405// otherwise.
Adam Langleyeeb9f492014-08-06 16:29:56 -0700406OPENSSL_EXPORT int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len);
407
David Benjamin4512b792017-08-18 19:21:50 -0400408// CBB_reserve ensures |cbb| has room for |len| additional bytes and sets
409// |*out_data| to point to the beginning of that space. It returns one on
410// success and zero otherwise. The caller may write up to |len| bytes to
411// |*out_data| and call |CBB_did_write| to complete the write. |*out_data| is
412// valid until the next operation on |cbb| or an ancestor |CBB|.
David Benjamin4cc671c2015-12-17 01:06:10 -0500413OPENSSL_EXPORT int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len);
414
David Benjamin4512b792017-08-18 19:21:50 -0400415// CBB_did_write advances |cbb| by |len| bytes, assuming the space has been
416// written to by the caller. It returns one on success and zero on error.
David Benjamin4cc671c2015-12-17 01:06:10 -0500417OPENSSL_EXPORT int CBB_did_write(CBB *cbb, size_t len);
418
David Benjamin4512b792017-08-18 19:21:50 -0400419// CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on
420// success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700421OPENSSL_EXPORT int CBB_add_u8(CBB *cbb, uint8_t value);
Adam Langley95c29f32014-06-20 12:00:00 -0700422
David Benjamin4512b792017-08-18 19:21:50 -0400423// CBB_add_u16 appends a 16-bit, big-endian number from |value| to |cbb|. It
424// returns one on success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700425OPENSSL_EXPORT int CBB_add_u16(CBB *cbb, uint16_t value);
Adam Langley95c29f32014-06-20 12:00:00 -0700426
David Benjamin4512b792017-08-18 19:21:50 -0400427// CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It
428// returns one on success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700429OPENSSL_EXPORT int CBB_add_u24(CBB *cbb, uint32_t value);
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin4512b792017-08-18 19:21:50 -0400431// CBB_add_u32 appends a 32-bit, big-endian number from |value| to |cbb|. It
432// returns one on success and zero otherwise.
David Benjaminbb076e32016-06-21 23:13:10 -0400433OPENSSL_EXPORT int CBB_add_u32(CBB *cbb, uint32_t value);
434
David Benjamin4512b792017-08-18 19:21:50 -0400435// CBB_discard_child discards the current unflushed child of |cbb|. Neither the
436// child's contents nor the length prefix will be included in the output.
David Benjamine8d53502015-10-10 14:13:23 -0400437OPENSSL_EXPORT void CBB_discard_child(CBB *cbb);
438
David Benjamin4512b792017-08-18 19:21:50 -0400439// CBB_add_asn1_uint64 writes an ASN.1 INTEGER into |cbb| using |CBB_add_asn1|
440// and writes |value| in its contents. It returns one on success and zero on
441// error.
David Benjaminb5b68542014-10-19 13:14:30 -0400442OPENSSL_EXPORT int CBB_add_asn1_uint64(CBB *cbb, uint64_t value);
Adam Langley95c29f32014-06-20 12:00:00 -0700443
David Benjamin5b082e82014-12-26 00:54:52 -0500444
Adam Langley95c29f32014-06-20 12:00:00 -0700445#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400446} // extern C
David Benjaminf0e935d2016-09-06 18:10:19 -0400447
448
449#if !defined(BORINGSSL_NO_CXX)
450extern "C++" {
451
452namespace bssl {
453
454using ScopedCBB = internal::StackAllocated<CBB, void, CBB_zero, CBB_cleanup>;
455
456} // namespace bssl
457
458} // extern C++
459#endif
460
Adam Langley95c29f32014-06-20 12:00:00 -0700461#endif
462
David Benjamin4512b792017-08-18 19:21:50 -0400463#endif // OPENSSL_HEADER_BYTESTRING_H