summaryrefslogtreecommitdiff
path: root/src/lib/libssl/bs_cbs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/bs_cbs.c')
-rw-r--r--src/lib/libssl/bs_cbs.c79
1 files changed, 78 insertions, 1 deletions
diff --git a/src/lib/libssl/bs_cbs.c b/src/lib/libssl/bs_cbs.c
index 97b0163f3f..63c078c9b0 100644
--- a/src/lib/libssl/bs_cbs.c
+++ b/src/lib/libssl/bs_cbs.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bs_cbs.c,v 1.23 2021/12/15 17:30:20 jsing Exp $ */ 1/* $OpenBSD: bs_cbs.c,v 1.24 2021/12/15 17:36:49 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
50static int
51cbs_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
50size_t 60size_t
51CBS_offset(const CBS *cbs) 61CBS_offset(const CBS *cbs)
52{ 62{
@@ -259,6 +269,73 @@ CBS_get_u24_length_prefixed(CBS *cbs, CBS *out)
259 return cbs_get_length_prefixed(cbs, out, 3); 269 return cbs_get_length_prefixed(cbs, out, 3);
260} 270}
261 271
272static int
273cbs_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
293int
294CBS_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
305int
306CBS_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
317int
318CBS_peek_u24(CBS *cbs, uint32_t *out)
319{
320 return cbs_peek_u(cbs, out, 3);
321}
322
323int
324CBS_peek_u32(CBS *cbs, uint32_t *out)
325{
326 return cbs_peek_u(cbs, out, 4);
327}
328
329int
330CBS_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
262int 339int
263CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag, 340CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag,
264 size_t *out_header_len) 341 size_t *out_header_len)