summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bytestring/bs_cbs.c
diff options
context:
space:
mode:
authorjsing <>2021-12-15 18:02:39 +0000
committerjsing <>2021-12-15 18:02:39 +0000
commite3e94694ef20846013caace50301b13531189c5a (patch)
treefcf31a167501ad72a989d06a8350397a1a29de03 /src/lib/libcrypto/bytestring/bs_cbs.c
parente61e8eab0ed72cba26134860e9976f836728d877 (diff)
downloadopenbsd-e3e94694ef20846013caace50301b13531189c5a.tar.gz
openbsd-e3e94694ef20846013caace50301b13531189c5a.tar.bz2
openbsd-e3e94694ef20846013caace50301b13531189c5a.zip
Sync bytestring with libssl.
Diffstat (limited to 'src/lib/libcrypto/bytestring/bs_cbs.c')
-rw-r--r--src/lib/libcrypto/bytestring/bs_cbs.c107
1 files changed, 106 insertions, 1 deletions
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
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{
@@ -191,6 +201,34 @@ CBS_get_u32(CBS *cbs, uint32_t *out)
191} 201}
192 202
193int 203int
204CBS_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
220int
221CBS_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
231int
194CBS_get_bytes(CBS *cbs, CBS *out, size_t len) 232CBS_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
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
234int 339int
235CBS_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,
236 size_t *out_header_len) 341 size_t *out_header_len)