diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libssl/bytestring.h | 340 |
1 files changed, 219 insertions, 121 deletions
diff --git a/src/lib/libssl/bytestring.h b/src/lib/libssl/bytestring.h index 3c4e8eaaf3..09414af056 100644 --- a/src/lib/libssl/bytestring.h +++ b/src/lib/libssl/bytestring.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bytestring.h,v 1.1 2015/02/06 09:36:16 doug Exp $ */ | 1 | /* $OpenBSD: bytestring.h,v 1.2 2015/02/06 22:22:33 doug Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014, Google Inc. | 3 | * Copyright (c) 2014, Google Inc. |
4 | * | 4 | * |
@@ -26,96 +26,127 @@ extern "C" { | |||
26 | 26 | ||
27 | #include <openssl/opensslconf.h> | 27 | #include <openssl/opensslconf.h> |
28 | 28 | ||
29 | /* Bytestrings are used for parsing and building TLS and ASN.1 messages. | 29 | /* |
30 | * Bytestrings are used for parsing and building TLS and ASN.1 messages. | ||
30 | * | 31 | * |
31 | * A "CBS" (CRYPTO ByteString) represents a string of bytes in memory and | 32 | * A "CBS" (CRYPTO ByteString) represents a string of bytes in memory and |
32 | * provides utility functions for safely parsing length-prefixed structures | 33 | * provides utility functions for safely parsing length-prefixed structures |
33 | * like TLS and ASN.1 from it. | 34 | * like TLS and ASN.1 from it. |
34 | * | 35 | * |
35 | * A "CBB" (CRYPTO ByteBuilder) is a memory buffer that grows as needed and | 36 | * A "CBB" (CRYPTO ByteBuilder) is a memory buffer that grows as needed and |
36 | * provides utility functions for building length-prefixed messages. */ | 37 | * provides utility functions for building length-prefixed messages. |
37 | 38 | */ | |
38 | 39 | ||
39 | /* CRYPTO ByteString */ | 40 | /* CRYPTO ByteString */ |
40 | |||
41 | typedef struct cbs_st { | 41 | typedef struct cbs_st { |
42 | const uint8_t *data; | 42 | const uint8_t *data; |
43 | size_t len; | 43 | size_t len; |
44 | } CBS; | 44 | } CBS; |
45 | 45 | ||
46 | /* CBS_init sets |cbs| to point to |data|. It does not take ownership of | 46 | /* |
47 | * |data|. */ | 47 | * CBS_init sets |cbs| to point to |data|. It does not take ownership of |
48 | * |data|. | ||
49 | */ | ||
48 | void CBS_init(CBS *cbs, const uint8_t *data, size_t len); | 50 | void CBS_init(CBS *cbs, const uint8_t *data, size_t len); |
49 | 51 | ||
50 | /* CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero | 52 | /* |
51 | * otherwise. */ | 53 | * CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero |
54 | * otherwise. | ||
55 | */ | ||
52 | int CBS_skip(CBS *cbs, size_t len); | 56 | int CBS_skip(CBS *cbs, size_t len); |
53 | 57 | ||
54 | /* CBS_data returns a pointer to the contains of |cbs|. */ | 58 | /* |
59 | * CBS_data returns a pointer to the contains of |cbs|. | ||
60 | */ | ||
55 | const uint8_t *CBS_data(const CBS *cbs); | 61 | const uint8_t *CBS_data(const CBS *cbs); |
56 | 62 | ||
57 | /* CBS_len returns the number of bytes remaining in |cbs|. */ | 63 | /* |
64 | * CBS_len returns the number of bytes remaining in |cbs|. | ||
65 | */ | ||
58 | size_t CBS_len(const CBS *cbs); | 66 | size_t CBS_len(const CBS *cbs); |
59 | 67 | ||
60 | /* CBS_stow copies the current contents of |cbs| into |*out_ptr| and | 68 | /* |
69 | * 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 | 70 | * |*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 | 71 | * 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, | 72 | * success, |*out_ptr| should be freed with OPENSSL_free. If |cbs| is empty, |
64 | * |*out_ptr| will be NULL. */ | 73 | * |*out_ptr| will be NULL. |
74 | */ | ||
65 | int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len); | 75 | int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len); |
66 | 76 | ||
67 | /* CBS_strdup copies the current contents of |cbs| into |*out_ptr| as a | 77 | /* |
78 | * 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 | 79 | * 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 | 80 | * with OPENSSL_free. It returns one on success and zero on allocation |
70 | * failure. On success, |*out_ptr| should be freed with OPENSSL_free. | 81 | * failure. On success, |*out_ptr| should be freed with OPENSSL_free. |
71 | * | 82 | * |
72 | * NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call | 83 | * NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call |
73 | * |CBS_contains_zero_byte(cbs)| to check for NUL bytes. */ | 84 | * |CBS_contains_zero_byte(cbs)| to check for NUL bytes. |
85 | */ | ||
74 | int CBS_strdup(const CBS *cbs, char **out_ptr); | 86 | int CBS_strdup(const CBS *cbs, char **out_ptr); |
75 | 87 | ||
76 | /* CBS_contains_zero_byte returns one if the current contents of |cbs| contains | 88 | /* |
77 | * a NUL byte and zero otherwise. */ | 89 | * CBS_contains_zero_byte returns one if the current contents of |cbs| contains |
90 | * a NUL byte and zero otherwise. | ||
91 | */ | ||
78 | int CBS_contains_zero_byte(const CBS *cbs); | 92 | int CBS_contains_zero_byte(const CBS *cbs); |
79 | 93 | ||
80 | /* CBS_mem_equal compares the current contents of |cbs| with the |len| bytes | 94 | /* |
95 | * 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 | 96 | * starting at |data|. If they're equal, it returns one, otherwise zero. If the |
82 | * lengths match, it uses a constant-time comparison. */ | 97 | * lengths match, it uses a constant-time comparison. |
83 | int CBS_mem_equal(const CBS *cbs, const uint8_t *data, | 98 | */ |
84 | size_t len); | 99 | int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len); |
85 | 100 | ||
86 | /* CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It | 101 | /* |
87 | * returns one on success and zero on error. */ | 102 | * CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It |
103 | * returns one on success and zero on error. | ||
104 | */ | ||
88 | int CBS_get_u8(CBS *cbs, uint8_t *out); | 105 | int CBS_get_u8(CBS *cbs, uint8_t *out); |
89 | 106 | ||
90 | /* CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and | 107 | /* |
91 | * advances |cbs|. It returns one on success and zero on error. */ | 108 | * CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and |
109 | * advances |cbs|. It returns one on success and zero on error. | ||
110 | */ | ||
92 | int CBS_get_u16(CBS *cbs, uint16_t *out); | 111 | int CBS_get_u16(CBS *cbs, uint16_t *out); |
93 | 112 | ||
94 | /* CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and | 113 | /* |
95 | * advances |cbs|. It returns one on success and zero on error. */ | 114 | * CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and |
115 | * advances |cbs|. It returns one on success and zero on error. | ||
116 | */ | ||
96 | int CBS_get_u24(CBS *cbs, uint32_t *out); | 117 | int CBS_get_u24(CBS *cbs, uint32_t *out); |
97 | 118 | ||
98 | /* CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs| | 119 | /* |
99 | * and advances |cbs|. It returns one on success and zero on error. */ | 120 | * CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs| |
121 | * and advances |cbs|. It returns one on success and zero on error. | ||
122 | */ | ||
100 | int CBS_get_u32(CBS *cbs, uint32_t *out); | 123 | int CBS_get_u32(CBS *cbs, uint32_t *out); |
101 | 124 | ||
102 | /* CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances | 125 | /* |
103 | * |cbs|. It returns one on success and zero on error. */ | 126 | * CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances |
127 | * |cbs|. It returns one on success and zero on error. | ||
128 | */ | ||
104 | int CBS_get_bytes(CBS *cbs, CBS *out, size_t len); | 129 | int CBS_get_bytes(CBS *cbs, CBS *out, size_t len); |
105 | 130 | ||
106 | /* CBS_get_u8_length_prefixed sets |*out| to the contents of an 8-bit, | 131 | /* |
132 | * 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 | 133 | * length-prefixed value from |cbs| and advances |cbs| over it. It returns one |
108 | * on success and zero on error. */ | 134 | * on success and zero on error. |
135 | */ | ||
109 | int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out); | 136 | int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out); |
110 | 137 | ||
111 | /* CBS_get_u16_length_prefixed sets |*out| to the contents of a 16-bit, | 138 | /* |
139 | * 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 | 140 | * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It |
113 | * returns one on success and zero on error. */ | 141 | * returns one on success and zero on error. |
142 | */ | ||
114 | int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out); | 143 | int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out); |
115 | 144 | ||
116 | /* CBS_get_u24_length_prefixed sets |*out| to the contents of a 24-bit, | 145 | /* |
146 | * 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 | 147 | * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It |
118 | * returns one on success and zero on error. */ | 148 | * returns one on success and zero on error. |
149 | */ | ||
119 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); | 150 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); |
120 | 151 | ||
121 | 152 | ||
@@ -133,80 +164,95 @@ int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); | |||
133 | #define CBS_ASN1_CONSTRUCTED 0x20 | 164 | #define CBS_ASN1_CONSTRUCTED 0x20 |
134 | #define CBS_ASN1_CONTEXT_SPECIFIC 0x80 | 165 | #define CBS_ASN1_CONTEXT_SPECIFIC 0x80 |
135 | 166 | ||
136 | /* CBS_get_asn1 sets |*out| to the contents of DER-encoded, ASN.1 element (not | 167 | /* |
168 | * 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 | 169 | * 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 | 170 | * element must match |tag_value|. It returns one on success and zero |
139 | * on error. | 171 | * on error. |
140 | * | 172 | * |
141 | * Tag numbers greater than 31 are not supported. */ | 173 | * Tag numbers greater than 31 are not supported. |
174 | */ | ||
142 | int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value); | 175 | int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value); |
143 | 176 | ||
144 | /* CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the | 177 | /* |
145 | * ASN.1 header bytes too. */ | 178 | * CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the |
179 | * ASN.1 header bytes too. | ||
180 | */ | ||
146 | int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value); | 181 | int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value); |
147 | 182 | ||
148 | /* CBS_peek_asn1_tag looks ahead at the next ASN.1 tag and returns one | 183 | /* |
184 | * 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 | 185 | * 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 | 186 | * |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 | 187 | * it returns one, CBS_get_asn1 may still fail if the rest of the |
152 | * element is malformed. */ | 188 | * element is malformed. |
189 | */ | ||
153 | int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value); | 190 | int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value); |
154 | 191 | ||
155 | /* CBS_get_any_asn1_element sets |*out| to contain the next ASN.1 element from | 192 | /* |
193 | * 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 | 194 | * |*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 | 195 | * 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 | 196 | * 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 | 197 | * header. Each of |out|, |out_tag|, and |out_header_len| may be NULL to ignore |
160 | * the value. | 198 | * the value. |
161 | * | 199 | * |
162 | * Tag numbers greater than 31 are not supported. */ | 200 | * Tag numbers greater than 31 are not supported. |
163 | int CBS_get_any_asn1_element(CBS *cbs, CBS *out, | 201 | */ |
164 | unsigned *out_tag, | 202 | int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
165 | size_t *out_header_len); | 203 | size_t *out_header_len); |
166 | 204 | ||
167 | /* CBS_get_asn1_uint64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1| | 205 | /* |
206 | * 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, | 207 | * 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 | 208 | * where error includes the integer being negative, or too large to represent |
170 | * in 64 bits. */ | 209 | * in 64 bits. |
210 | */ | ||
171 | int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out); | 211 | int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out); |
172 | 212 | ||
173 | /* CBS_get_optional_asn1 gets an optional explicitly-tagged element | 213 | /* |
214 | * CBS_get_optional_asn1 gets an optional explicitly-tagged element | ||
174 | * from |cbs| tagged with |tag| and sets |*out| to its contents. If | 215 | * from |cbs| tagged with |tag| and sets |*out| to its contents. If |
175 | * present, it sets |*out_present| to one, otherwise zero. It returns | 216 | * 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 | 217 | * one on success, whether or not the element was present, and zero on |
177 | * decode failure. */ | 218 | * decode failure. |
178 | int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, | 219 | */ |
179 | unsigned tag); | 220 | int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag); |
180 | 221 | ||
181 | /* CBS_get_optional_asn1_octet_string gets an optional | 222 | /* |
223 | * CBS_get_optional_asn1_octet_string gets an optional | ||
182 | * explicitly-tagged OCTET STRING from |cbs|. If present, it sets | 224 | * explicitly-tagged OCTET STRING from |cbs|. If present, it sets |
183 | * |*out| to the string and |*out_present| to one. Otherwise, it sets | 225 | * |*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 | 226 | * |*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 | 227 | * NULL. It returns one on success, whether or not the element was |
186 | * present, and zero on decode failure. */ | 228 | * present, and zero on decode failure. |
187 | int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, | 229 | */ |
188 | int *out_present, | 230 | int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present, |
189 | unsigned tag); | 231 | unsigned tag); |
190 | 232 | ||
191 | /* CBS_get_optional_asn1_uint64 gets an optional explicitly-tagged | 233 | /* |
234 | * CBS_get_optional_asn1_uint64 gets an optional explicitly-tagged | ||
192 | * INTEGER from |cbs|. If present, it sets |*out| to the | 235 | * INTEGER from |cbs|. If present, it sets |*out| to the |
193 | * value. Otherwise, it sets |*out| to |default_value|. It returns one | 236 | * value. Otherwise, it sets |*out| to |default_value|. It returns one |
194 | * on success, whether or not the element was present, and zero on | 237 | * on success, whether or not the element was present, and zero on |
195 | * decode failure. */ | 238 | * decode failure. |
196 | int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, | 239 | */ |
197 | unsigned tag, | 240 | int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag, |
198 | uint64_t default_value); | 241 | uint64_t default_value); |
199 | 242 | ||
200 | /* CBS_get_optional_asn1_bool gets an optional, explicitly-tagged BOOLEAN from | 243 | /* |
244 | * 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 | 245 | * |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 | 246 | * 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 | 247 | * success, whether or not the element was present, and zero on decode |
204 | * failure. */ | 248 | * failure. |
249 | */ | ||
205 | int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag, | 250 | int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag, |
206 | int default_value); | 251 | int default_value); |
207 | 252 | ||
208 | 253 | ||
209 | /* CRYPTO ByteBuilder. | 254 | /* |
255 | * CRYPTO ByteBuilder. | ||
210 | * | 256 | * |
211 | * |CBB| objects allow one to build length-prefixed serialisations. A |CBB| | 257 | * |CBB| objects allow one to build length-prefixed serialisations. A |CBB| |
212 | * object is associated with a buffer and new buffers are created with | 258 | * object is associated with a buffer and new buffers are created with |
@@ -218,111 +264,162 @@ int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag, | |||
218 | * not be used again. | 264 | * not be used again. |
219 | * | 265 | * |
220 | * If one needs to force a length prefix to be written out because a |CBB| is | 266 | * If one needs to force a length prefix to be written out because a |CBB| is |
221 | * going out of scope, use |CBB_flush|. */ | 267 | * going out of scope, use |CBB_flush|. |
268 | */ | ||
222 | 269 | ||
223 | struct cbb_buffer_st { | 270 | struct cbb_buffer_st { |
224 | uint8_t *buf; | 271 | uint8_t *buf; |
225 | size_t len; /* The number of valid bytes. */ | 272 | |
226 | size_t cap; /* The size of buf. */ | 273 | /* The number of valid bytes. */ |
227 | char can_resize; /* One iff |buf| is owned by this object. If not then |buf| | 274 | size_t len; |
228 | cannot be resized. */ | 275 | |
276 | /* The size of buf. */ | ||
277 | size_t cap; | ||
278 | |||
279 | /* | ||
280 | * One iff |buf| is owned by this object. If not then |buf| cannot be | ||
281 | * resized. | ||
282 | */ | ||
283 | char can_resize; | ||
229 | }; | 284 | }; |
230 | 285 | ||
231 | typedef struct cbb_st { | 286 | typedef struct cbb_st { |
232 | struct cbb_buffer_st *base; | 287 | struct cbb_buffer_st *base; |
233 | /* offset is the offset from the start of |base->buf| to the position of any | 288 | |
234 | * pending length-prefix. */ | 289 | /* |
235 | size_t offset; | 290 | * offset is the offset from the start of |base->buf| to the position of any |
236 | /* child points to a child CBB if a length-prefix is pending. */ | 291 | * pending length-prefix. |
237 | struct cbb_st *child; | 292 | */ |
238 | /* pending_len_len contains the number of bytes in a pending length-prefix, | 293 | size_t offset; |
239 | * or zero if no length-prefix is pending. */ | 294 | |
240 | uint8_t pending_len_len; | 295 | /* child points to a child CBB if a length-prefix is pending. */ |
241 | char pending_is_asn1; | 296 | struct cbb_st *child; |
242 | /* is_top_level is true iff this is a top-level |CBB| (as opposed to a child | 297 | |
243 | * |CBB|). Top-level objects are valid arguments for |CBB_finish|. */ | 298 | /* |
244 | char is_top_level; | 299 | * pending_len_len contains the number of bytes in a pending length-prefix, |
300 | * or zero if no length-prefix is pending. | ||
301 | */ | ||
302 | uint8_t pending_len_len; | ||
303 | |||
304 | char pending_is_asn1; | ||
305 | |||
306 | /* | ||
307 | * is_top_level is true iff this is a top-level |CBB| (as opposed to a child | ||
308 | * |CBB|). Top-level objects are valid arguments for |CBB_finish|. | ||
309 | */ | ||
310 | char is_top_level; | ||
245 | } CBB; | 311 | } CBB; |
246 | 312 | ||
247 | /* CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as | 313 | /* |
314 | * 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 | 315 | * needed, the |initial_capacity| is just a hint. It returns one on success or |
249 | * zero on error. */ | 316 | * zero on error. |
317 | */ | ||
250 | int CBB_init(CBB *cbb, size_t initial_capacity); | 318 | int CBB_init(CBB *cbb, size_t initial_capacity); |
251 | 319 | ||
252 | /* CBB_init_fixed initialises |cbb| to write to |len| bytes at |buf|. Since | 320 | /* |
321 | * 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 | 322 | * |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. */ | 323 | * functions to fail. It returns one on success or zero on error. |
324 | */ | ||
255 | int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len); | 325 | int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len); |
256 | 326 | ||
257 | /* CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects | 327 | /* |
328 | * 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 | 329 | * writing to the same buffer. This should be used in an error case where a |
259 | * serialisation is abandoned. */ | 330 | * serialisation is abandoned. |
331 | */ | ||
260 | void CBB_cleanup(CBB *cbb); | 332 | void CBB_cleanup(CBB *cbb); |
261 | 333 | ||
262 | /* CBB_finish completes any pending length prefix and sets |*out_data| to a | 334 | /* |
335 | * 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 | 336 | * 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 | 337 | * takes ownership of the buffer and, unless the buffer was fixed with |
265 | * |CBB_init_fixed|, must call |OPENSSL_free| when done. | 338 | * |CBB_init_fixed|, must call |OPENSSL_free| when done. |
266 | * | 339 | * |
267 | * It can only be called on a "top level" |CBB|, i.e. one initialised with | 340 | * 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 | 341 | * |CBB_init| or |CBB_init_fixed|. It returns one on success and zero on |
269 | * error. */ | 342 | * error. |
343 | */ | ||
270 | int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len); | 344 | int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len); |
271 | 345 | ||
272 | /* CBB_flush causes any pending length prefixes to be written out and any child | 346 | /* |
347 | * 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 | 348 | * |CBB| objects of |cbb| to be invalidated. It returns one on success or zero |
274 | * on error. */ | 349 | * on error. |
350 | */ | ||
275 | int CBB_flush(CBB *cbb); | 351 | int CBB_flush(CBB *cbb); |
276 | 352 | ||
277 | /* CBB_add_u8_length_prefixed sets |*out_contents| to a new child of |cbb|. The | 353 | /* |
354 | * 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 | 355 | * 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. */ | 356 | * length. It returns one on success or zero on error. |
357 | */ | ||
280 | int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents); | 358 | int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents); |
281 | 359 | ||
282 | /* CBB_add_u16_length_prefixed sets |*out_contents| to a new child of |cbb|. | 360 | /* |
361 | * 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, | 362 | * 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. */ | 363 | * big-endian length. It returns one on success or zero on error. |
364 | */ | ||
285 | int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents); | 365 | int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents); |
286 | 366 | ||
287 | /* CBB_add_u24_length_prefixed sets |*out_contents| to a new child of |cbb|. | 367 | /* |
368 | * 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, | 369 | * 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. */ | 370 | * big-endian length. It returns one on success or zero on error. |
371 | */ | ||
290 | int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents); | 372 | int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents); |
291 | 373 | ||
292 | /* CBB_add_asn sets |*out_contents| to a |CBB| into which the contents of an | 374 | /* |
375 | * 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 | 376 | * 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. */ | 377 | * the object. It returns one on success or zero on error. |
378 | */ | ||
295 | int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag); | 379 | int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag); |
296 | 380 | ||
297 | /* CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on | 381 | /* |
298 | * success and zero otherwise. */ | 382 | * CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on |
383 | * success and zero otherwise. | ||
384 | */ | ||
299 | int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len); | 385 | int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len); |
300 | 386 | ||
301 | /* CBB_add_space appends |len| bytes to |cbb| and sets |*out_data| to point to | 387 | /* |
388 | * 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 | 389 | * 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 | 390 | * actual contents to |*out_data|. It returns one on success and zero |
304 | * otherwise. */ | 391 | * otherwise. |
392 | */ | ||
305 | int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len); | 393 | int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len); |
306 | 394 | ||
307 | /* CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on | 395 | /* |
308 | * success and zero otherwise. */ | 396 | * CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on |
397 | * success and zero otherwise. | ||
398 | */ | ||
309 | int CBB_add_u8(CBB *cbb, uint8_t value); | 399 | int CBB_add_u8(CBB *cbb, uint8_t value); |
310 | 400 | ||
311 | /* CBB_add_u8 appends a 16-bit, big-endian number from |value| to |cbb|. It | 401 | /* |
312 | * returns one on success and zero otherwise. */ | 402 | * CBB_add_u8 appends a 16-bit, big-endian number from |value| to |cbb|. It |
403 | * returns one on success and zero otherwise. | ||
404 | */ | ||
313 | int CBB_add_u16(CBB *cbb, uint16_t value); | 405 | int CBB_add_u16(CBB *cbb, uint16_t value); |
314 | 406 | ||
315 | /* CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It | 407 | /* |
316 | * returns one on success and zero otherwise. */ | 408 | * CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It |
409 | * returns one on success and zero otherwise. | ||
410 | */ | ||
317 | int CBB_add_u24(CBB *cbb, uint32_t value); | 411 | int CBB_add_u24(CBB *cbb, uint32_t value); |
318 | 412 | ||
319 | /* CBB_add_asn1_uint64 writes an ASN.1 INTEGER into |cbb| using |CBB_add_asn1| | 413 | /* |
414 | * 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 | 415 | * and writes |value| in its contents. It returns one on success and zero on |
321 | * error. */ | 416 | * error. |
417 | */ | ||
322 | int CBB_add_asn1_uint64(CBB *cbb, uint64_t value); | 418 | int CBB_add_asn1_uint64(CBB *cbb, uint64_t value); |
323 | 419 | ||
324 | #ifdef LIBRESSL_INTERNAL | 420 | #ifdef LIBRESSL_INTERNAL |
325 | /* CBS_asn1_ber_to_der reads an ASN.1 structure from |in|. If it finds | 421 | /* |
422 | * 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 | 423 | * 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 | 424 | * 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. | 425 | * the DER data. Additionally, |*in| will be advanced over the ASN.1 data. |
@@ -335,7 +432,8 @@ int CBB_add_asn1_uint64(CBB *cbb, uint64_t value); | |||
335 | * structure itself. However, this sufficies to handle the PKCS#7 and #12 output | 432 | * structure itself. However, this sufficies to handle the PKCS#7 and #12 output |
336 | * from NSS. | 433 | * from NSS. |
337 | * | 434 | * |
338 | * It returns one on success and zero otherwise. */ | 435 | * It returns one on success and zero otherwise. |
436 | */ | ||
339 | int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len); | 437 | int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len); |
340 | #endif /* LIBRESSL_INTERNAL */ | 438 | #endif /* LIBRESSL_INTERNAL */ |
341 | 439 | ||