diff options
Diffstat (limited to 'src/lib/libcrypto/bytestring/bs_cbs.c')
| -rw-r--r-- | src/lib/libcrypto/bytestring/bs_cbs.c | 107 |
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 | ||
| 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) |
