diff options
-rw-r--r-- | src/lib/libcrypto/bytestring/bs_ber.c | 2 | ||||
-rw-r--r-- | src/lib/libcrypto/bytestring/bs_cbb.c | 2 | ||||
-rw-r--r-- | src/lib/libcrypto/bytestring/bs_cbs.c | 107 | ||||
-rw-r--r-- | src/lib/libcrypto/bytestring/bytestring.h | 44 |
4 files changed, 151 insertions, 4 deletions
diff --git a/src/lib/libcrypto/bytestring/bs_ber.c b/src/lib/libcrypto/bytestring/bs_ber.c index e77c98b223..c9779c8965 100644 --- a/src/lib/libcrypto/bytestring/bs_ber.c +++ b/src/lib/libcrypto/bytestring/bs_ber.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bs_ber.c,v 1.1 2021/11/20 18:10:52 jsing Exp $ */ | 1 | /* $OpenBSD: bs_ber.c,v 1.2 2021/12/15 18:02:39 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014, Google Inc. | 3 | * Copyright (c) 2014, Google Inc. |
4 | * | 4 | * |
diff --git a/src/lib/libcrypto/bytestring/bs_cbb.c b/src/lib/libcrypto/bytestring/bs_cbb.c index 2aaee30a06..1fa358d6c4 100644 --- a/src/lib/libcrypto/bytestring/bs_cbb.c +++ b/src/lib/libcrypto/bytestring/bs_cbb.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bs_cbb.c,v 1.1 2021/11/20 18:10:52 jsing Exp $ */ | 1 | /* $OpenBSD: bs_cbb.c,v 1.2 2021/12/15 18:02:39 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014, Google Inc. | 3 | * Copyright (c) 2014, Google Inc. |
4 | * | 4 | * |
diff --git a/src/lib/libcrypto/bytestring/bs_cbs.c b/src/lib/libcrypto/bytestring/bs_cbs.c index 06283abd94..e2bb54e460 100644 --- a/src/lib/libcrypto/bytestring/bs_cbs.c +++ b/src/lib/libcrypto/bytestring/bs_cbs.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bs_cbs.c,v 1.1 2021/11/20 18:10:52 jsing Exp $ */ | 1 | /* $OpenBSD: bs_cbs.c,v 1.2 2021/12/15 18:02:39 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014, Google Inc. | 3 | * Copyright (c) 2014, Google Inc. |
4 | * | 4 | * |
@@ -47,6 +47,16 @@ cbs_get(CBS *cbs, const uint8_t **p, size_t n) | |||
47 | return 1; | 47 | return 1; |
48 | } | 48 | } |
49 | 49 | ||
50 | static int | ||
51 | cbs_peek(CBS *cbs, const uint8_t **p, size_t n) | ||
52 | { | ||
53 | if (cbs->len < n) | ||
54 | return 0; | ||
55 | |||
56 | *p = cbs->data; | ||
57 | return 1; | ||
58 | } | ||
59 | |||
50 | size_t | 60 | size_t |
51 | CBS_offset(const CBS *cbs) | 61 | CBS_offset(const CBS *cbs) |
52 | { | 62 | { |
@@ -191,6 +201,34 @@ CBS_get_u32(CBS *cbs, uint32_t *out) | |||
191 | } | 201 | } |
192 | 202 | ||
193 | int | 203 | int |
204 | CBS_get_u64(CBS *cbs, uint64_t *out) | ||
205 | { | ||
206 | uint32_t a, b; | ||
207 | |||
208 | if (cbs->len < 8) | ||
209 | return 0; | ||
210 | |||
211 | if (!CBS_get_u32(cbs, &a)) | ||
212 | return 0; | ||
213 | if (!CBS_get_u32(cbs, &b)) | ||
214 | return 0; | ||
215 | |||
216 | *out = (uint64_t)a << 32 | b; | ||
217 | return 1; | ||
218 | } | ||
219 | |||
220 | int | ||
221 | CBS_get_last_u8(CBS *cbs, uint8_t *out) | ||
222 | { | ||
223 | if (cbs->len == 0) | ||
224 | return 0; | ||
225 | |||
226 | *out = cbs->data[cbs->len - 1]; | ||
227 | cbs->len--; | ||
228 | return 1; | ||
229 | } | ||
230 | |||
231 | int | ||
194 | CBS_get_bytes(CBS *cbs, CBS *out, size_t len) | 232 | CBS_get_bytes(CBS *cbs, CBS *out, size_t len) |
195 | { | 233 | { |
196 | const uint8_t *v; | 234 | const uint8_t *v; |
@@ -231,6 +269,73 @@ CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) | |||
231 | return cbs_get_length_prefixed(cbs, out, 3); | 269 | return cbs_get_length_prefixed(cbs, out, 3); |
232 | } | 270 | } |
233 | 271 | ||
272 | static int | ||
273 | cbs_peek_u(CBS *cbs, uint32_t *out, size_t len) | ||
274 | { | ||
275 | uint32_t result = 0; | ||
276 | size_t i; | ||
277 | const uint8_t *data; | ||
278 | |||
279 | if (len < 1 || len > 4) | ||
280 | return 0; | ||
281 | |||
282 | if (!cbs_peek(cbs, &data, len)) | ||
283 | return 0; | ||
284 | |||
285 | for (i = 0; i < len; i++) { | ||
286 | result <<= 8; | ||
287 | result |= data[i]; | ||
288 | } | ||
289 | *out = result; | ||
290 | return 1; | ||
291 | } | ||
292 | |||
293 | int | ||
294 | CBS_peek_u8(CBS *cbs, uint8_t *out) | ||
295 | { | ||
296 | const uint8_t *v; | ||
297 | |||
298 | if (!cbs_peek(cbs, &v, 1)) | ||
299 | return 0; | ||
300 | |||
301 | *out = *v; | ||
302 | return 1; | ||
303 | } | ||
304 | |||
305 | int | ||
306 | CBS_peek_u16(CBS *cbs, uint16_t *out) | ||
307 | { | ||
308 | uint32_t v; | ||
309 | |||
310 | if (!cbs_peek_u(cbs, &v, 2)) | ||
311 | return 0; | ||
312 | |||
313 | *out = v; | ||
314 | return 1; | ||
315 | } | ||
316 | |||
317 | int | ||
318 | CBS_peek_u24(CBS *cbs, uint32_t *out) | ||
319 | { | ||
320 | return cbs_peek_u(cbs, out, 3); | ||
321 | } | ||
322 | |||
323 | int | ||
324 | CBS_peek_u32(CBS *cbs, uint32_t *out) | ||
325 | { | ||
326 | return cbs_peek_u(cbs, out, 4); | ||
327 | } | ||
328 | |||
329 | int | ||
330 | CBS_peek_last_u8(CBS *cbs, uint8_t *out) | ||
331 | { | ||
332 | if (cbs->len == 0) | ||
333 | return 0; | ||
334 | |||
335 | *out = cbs->data[cbs->len - 1]; | ||
336 | return 1; | ||
337 | } | ||
338 | |||
234 | int | 339 | int |
235 | CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag, | 340 | CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag, |
236 | size_t *out_header_len) | 341 | size_t *out_header_len) |
diff --git a/src/lib/libcrypto/bytestring/bytestring.h b/src/lib/libcrypto/bytestring/bytestring.h index 7728ef8de2..54d8f54ce2 100644 --- a/src/lib/libcrypto/bytestring/bytestring.h +++ b/src/lib/libcrypto/bytestring/bytestring.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bytestring.h,v 1.1 2021/11/20 18:10:52 jsing Exp $ */ | 1 | /* $OpenBSD: bytestring.h,v 1.2 2021/12/15 18:02:39 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014, Google Inc. | 3 | * Copyright (c) 2014, Google Inc. |
4 | * | 4 | * |
@@ -134,6 +134,18 @@ int CBS_get_u24(CBS *cbs, uint32_t *out); | |||
134 | int CBS_get_u32(CBS *cbs, uint32_t *out); | 134 | int CBS_get_u32(CBS *cbs, uint32_t *out); |
135 | 135 | ||
136 | /* | 136 | /* |
137 | * CBS_get_u64 sets |*out| to the next, big-endian uint64_t value from |cbs| | ||
138 | * and advances |cbs|. It returns one on success and zero on error. | ||
139 | */ | ||
140 | int CBS_get_u64(CBS *cbs, uint64_t *out); | ||
141 | |||
142 | /* | ||
143 | * CBS_get_last_u8 sets |*out| to the last uint8_t from |cbs| and shortens | ||
144 | * |cbs|. It returns one on success and zero on error. | ||
145 | */ | ||
146 | int CBS_get_last_u8(CBS *cbs, uint8_t *out); | ||
147 | |||
148 | /* | ||
137 | * CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances | 149 | * CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances |
138 | * |cbs|. It returns one on success and zero on error. | 150 | * |cbs|. It returns one on success and zero on error. |
139 | */ | 151 | */ |
@@ -160,6 +172,36 @@ int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out); | |||
160 | */ | 172 | */ |
161 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); | 173 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); |
162 | 174 | ||
175 | /* | ||
176 | * CBS_peek_u8 sets |*out| to the next uint8_t from |cbs|, but does not advance | ||
177 | * |cbs|. It returns one on success and zero on error. | ||
178 | */ | ||
179 | int CBS_peek_u8(CBS *cbs, uint8_t *out); | ||
180 | |||
181 | /* | ||
182 | * CBS_peek_u16 sets |*out| to the next, big-endian uint16_t from |cbs|, but | ||
183 | * does not advance |cbs|. It returns one on success and zero on error. | ||
184 | */ | ||
185 | int CBS_peek_u16(CBS *cbs, uint16_t *out); | ||
186 | |||
187 | /* | ||
188 | * CBS_peek_u24 sets |*out| to the next, big-endian 24-bit value from |cbs|, but | ||
189 | * does not advance |cbs|. It returns one on success and zero on error. | ||
190 | */ | ||
191 | int CBS_peek_u24(CBS *cbs, uint32_t *out); | ||
192 | |||
193 | /* | ||
194 | * CBS_peek_u32 sets |*out| to the next, big-endian uint32_t value from |cbs|, | ||
195 | * but does not advance |cbs|. It returns one on success and zero on error. | ||
196 | */ | ||
197 | int CBS_peek_u32(CBS *cbs, uint32_t *out); | ||
198 | |||
199 | /* | ||
200 | * CBS_peek_last_u8 sets |*out| to the last uint8_t from |cbs|, but does not | ||
201 | * shorten |cbs|. It returns one on success and zero on error. | ||
202 | */ | ||
203 | int CBS_peek_last_u8(CBS *cbs, uint8_t *out); | ||
204 | |||
163 | 205 | ||
164 | /* Parsing ASN.1 */ | 206 | /* Parsing ASN.1 */ |
165 | 207 | ||