1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: bytestring.h,v 1.23 2022/01/06 14:30:30 jsing Exp $ */ 2f5b1c8a1SJohn Marino /* 3f5b1c8a1SJohn Marino * Copyright (c) 2014, Google Inc. 4f5b1c8a1SJohn Marino * 5f5b1c8a1SJohn Marino * Permission to use, copy, modify, and/or distribute this software for any 6f5b1c8a1SJohn Marino * purpose with or without fee is hereby granted, provided that the above 7f5b1c8a1SJohn Marino * copyright notice and this permission notice appear in all copies. 8f5b1c8a1SJohn Marino * 9f5b1c8a1SJohn Marino * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10f5b1c8a1SJohn Marino * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11f5b1c8a1SJohn Marino * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 12f5b1c8a1SJohn Marino * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13f5b1c8a1SJohn Marino * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 14f5b1c8a1SJohn Marino * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 15*de0e0e4dSAntonio Huete Jimenez * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*de0e0e4dSAntonio Huete Jimenez */ 17f5b1c8a1SJohn Marino 18f5b1c8a1SJohn Marino #ifndef OPENSSL_HEADER_BYTESTRING_H 19f5b1c8a1SJohn Marino #define OPENSSL_HEADER_BYTESTRING_H 20f5b1c8a1SJohn Marino 21f5b1c8a1SJohn Marino #include <sys/types.h> 22f5b1c8a1SJohn Marino #include <stdint.h> 23f5b1c8a1SJohn Marino 2472c33676SMaxim Ag __BEGIN_HIDDEN_DECLS 2572c33676SMaxim Ag 26f5b1c8a1SJohn Marino /* 27f5b1c8a1SJohn Marino * Bytestrings are used for parsing and building TLS and ASN.1 messages. 28f5b1c8a1SJohn Marino * 29f5b1c8a1SJohn Marino * A "CBS" (CRYPTO ByteString) represents a string of bytes in memory and 30f5b1c8a1SJohn Marino * provides utility functions for safely parsing length-prefixed structures 31f5b1c8a1SJohn Marino * like TLS and ASN.1 from it. 32f5b1c8a1SJohn Marino * 33f5b1c8a1SJohn Marino * A "CBB" (CRYPTO ByteBuilder) is a memory buffer that grows as needed and 34f5b1c8a1SJohn Marino * provides utility functions for building length-prefixed messages. 35f5b1c8a1SJohn Marino */ 36f5b1c8a1SJohn Marino 37f5b1c8a1SJohn Marino /* CRYPTO ByteString */ 38f5b1c8a1SJohn Marino typedef struct cbs_st { 39f5b1c8a1SJohn Marino const uint8_t *data; 40f5b1c8a1SJohn Marino size_t initial_len; 41f5b1c8a1SJohn Marino size_t len; 42f5b1c8a1SJohn Marino } CBS; 43f5b1c8a1SJohn Marino 44f5b1c8a1SJohn Marino /* 45f5b1c8a1SJohn Marino * CBS_init sets |cbs| to point to |data|. It does not take ownership of 46f5b1c8a1SJohn Marino * |data|. 47f5b1c8a1SJohn Marino */ 48f5b1c8a1SJohn Marino void CBS_init(CBS *cbs, const uint8_t *data, size_t len); 49f5b1c8a1SJohn Marino 50f5b1c8a1SJohn Marino /* 51f5b1c8a1SJohn Marino * CBS_skip advances |cbs| by |len| bytes. It returns one on success and zero 52f5b1c8a1SJohn Marino * otherwise. 53f5b1c8a1SJohn Marino */ 54f5b1c8a1SJohn Marino int CBS_skip(CBS *cbs, size_t len); 55f5b1c8a1SJohn Marino 56f5b1c8a1SJohn Marino /* 57f5b1c8a1SJohn Marino * CBS_data returns a pointer to the contents of |cbs|. 58f5b1c8a1SJohn Marino */ 59f5b1c8a1SJohn Marino const uint8_t *CBS_data(const CBS *cbs); 60f5b1c8a1SJohn Marino 61f5b1c8a1SJohn Marino /* 62f5b1c8a1SJohn Marino * CBS_len returns the number of bytes remaining in |cbs|. 63f5b1c8a1SJohn Marino */ 64f5b1c8a1SJohn Marino size_t CBS_len(const CBS *cbs); 65f5b1c8a1SJohn Marino 66f5b1c8a1SJohn Marino /* 67f5b1c8a1SJohn Marino * CBS_offset returns the current offset into the original data of |cbs|. 68f5b1c8a1SJohn Marino */ 69f5b1c8a1SJohn Marino size_t CBS_offset(const CBS *cbs); 70f5b1c8a1SJohn Marino 71f5b1c8a1SJohn Marino /* 72f5b1c8a1SJohn Marino * CBS_stow copies the current contents of |cbs| into |*out_ptr| and 73f5b1c8a1SJohn Marino * |*out_len|. If |*out_ptr| is not NULL, the contents are freed with 74f5b1c8a1SJohn Marino * free. It returns one on success and zero on allocation failure. On 75f5b1c8a1SJohn Marino * success, |*out_ptr| should be freed with free. If |cbs| is empty, 76f5b1c8a1SJohn Marino * |*out_ptr| will be NULL. 77f5b1c8a1SJohn Marino */ 78f5b1c8a1SJohn Marino int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len); 79f5b1c8a1SJohn Marino 80f5b1c8a1SJohn Marino /* 81f5b1c8a1SJohn Marino * CBS_strdup copies the current contents of |cbs| into |*out_ptr| as a 82f5b1c8a1SJohn Marino * NUL-terminated C string. If |*out_ptr| is not NULL, the contents are freed 83f5b1c8a1SJohn Marino * with free. It returns one on success and zero on allocation 84f5b1c8a1SJohn Marino * failure. On success, |*out_ptr| should be freed with free. 85f5b1c8a1SJohn Marino * 86f5b1c8a1SJohn Marino * NOTE: If |cbs| contains NUL bytes, the string will be truncated. Call 87f5b1c8a1SJohn Marino * |CBS_contains_zero_byte(cbs)| to check for NUL bytes. 88f5b1c8a1SJohn Marino */ 89f5b1c8a1SJohn Marino int CBS_strdup(const CBS *cbs, char **out_ptr); 90f5b1c8a1SJohn Marino 91f5b1c8a1SJohn Marino /* 92f5b1c8a1SJohn Marino * CBS_write_bytes writes all of the remaining data from |cbs| into |dst| 93f5b1c8a1SJohn Marino * if it is at most |dst_len| bytes. If |copied| is not NULL, it will be set 94f5b1c8a1SJohn Marino * to the amount copied. It returns one on success and zero otherwise. 95f5b1c8a1SJohn Marino */ 96f5b1c8a1SJohn Marino int CBS_write_bytes(const CBS *cbs, uint8_t *dst, size_t dst_len, 97f5b1c8a1SJohn Marino size_t *copied); 98f5b1c8a1SJohn Marino 99f5b1c8a1SJohn Marino /* 100f5b1c8a1SJohn Marino * CBS_contains_zero_byte returns one if the current contents of |cbs| contains 101f5b1c8a1SJohn Marino * a NUL byte and zero otherwise. 102f5b1c8a1SJohn Marino */ 103f5b1c8a1SJohn Marino int CBS_contains_zero_byte(const CBS *cbs); 104f5b1c8a1SJohn Marino 105f5b1c8a1SJohn Marino /* 106f5b1c8a1SJohn Marino * CBS_mem_equal compares the current contents of |cbs| with the |len| bytes 107f5b1c8a1SJohn Marino * starting at |data|. If they're equal, it returns one, otherwise zero. If the 108f5b1c8a1SJohn Marino * lengths match, it uses a constant-time comparison. 109f5b1c8a1SJohn Marino */ 110f5b1c8a1SJohn Marino int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len); 111f5b1c8a1SJohn Marino 112f5b1c8a1SJohn Marino /* 113f5b1c8a1SJohn Marino * CBS_get_u8 sets |*out| to the next uint8_t from |cbs| and advances |cbs|. It 114f5b1c8a1SJohn Marino * returns one on success and zero on error. 115f5b1c8a1SJohn Marino */ 116f5b1c8a1SJohn Marino int CBS_get_u8(CBS *cbs, uint8_t *out); 117f5b1c8a1SJohn Marino 118f5b1c8a1SJohn Marino /* 119f5b1c8a1SJohn Marino * CBS_get_u16 sets |*out| to the next, big-endian uint16_t from |cbs| and 120f5b1c8a1SJohn Marino * advances |cbs|. It returns one on success and zero on error. 121f5b1c8a1SJohn Marino */ 122f5b1c8a1SJohn Marino int CBS_get_u16(CBS *cbs, uint16_t *out); 123f5b1c8a1SJohn Marino 124f5b1c8a1SJohn Marino /* 125f5b1c8a1SJohn Marino * CBS_get_u24 sets |*out| to the next, big-endian 24-bit value from |cbs| and 126f5b1c8a1SJohn Marino * advances |cbs|. It returns one on success and zero on error. 127f5b1c8a1SJohn Marino */ 128f5b1c8a1SJohn Marino int CBS_get_u24(CBS *cbs, uint32_t *out); 129f5b1c8a1SJohn Marino 130f5b1c8a1SJohn Marino /* 131f5b1c8a1SJohn Marino * CBS_get_u32 sets |*out| to the next, big-endian uint32_t value from |cbs| 132f5b1c8a1SJohn Marino * and advances |cbs|. It returns one on success and zero on error. 133f5b1c8a1SJohn Marino */ 134f5b1c8a1SJohn Marino int CBS_get_u32(CBS *cbs, uint32_t *out); 135f5b1c8a1SJohn Marino 136f5b1c8a1SJohn Marino /* 137*de0e0e4dSAntonio Huete Jimenez * CBS_get_u64 sets |*out| to the next, big-endian uint64_t value from |cbs| 138*de0e0e4dSAntonio Huete Jimenez * and advances |cbs|. It returns one on success and zero on error. 139*de0e0e4dSAntonio Huete Jimenez */ 140*de0e0e4dSAntonio Huete Jimenez int CBS_get_u64(CBS *cbs, uint64_t *out); 141*de0e0e4dSAntonio Huete Jimenez 142*de0e0e4dSAntonio Huete Jimenez /* 143*de0e0e4dSAntonio Huete Jimenez * CBS_get_last_u8 sets |*out| to the last uint8_t from |cbs| and shortens 144*de0e0e4dSAntonio Huete Jimenez * |cbs|. It returns one on success and zero on error. 145*de0e0e4dSAntonio Huete Jimenez */ 146*de0e0e4dSAntonio Huete Jimenez int CBS_get_last_u8(CBS *cbs, uint8_t *out); 147*de0e0e4dSAntonio Huete Jimenez 148*de0e0e4dSAntonio Huete Jimenez /* 149f5b1c8a1SJohn Marino * CBS_get_bytes sets |*out| to the next |len| bytes from |cbs| and advances 150f5b1c8a1SJohn Marino * |cbs|. It returns one on success and zero on error. 151f5b1c8a1SJohn Marino */ 152f5b1c8a1SJohn Marino int CBS_get_bytes(CBS *cbs, CBS *out, size_t len); 153f5b1c8a1SJohn Marino 154f5b1c8a1SJohn Marino /* 155f5b1c8a1SJohn Marino * CBS_get_u8_length_prefixed sets |*out| to the contents of an 8-bit, 156f5b1c8a1SJohn Marino * length-prefixed value from |cbs| and advances |cbs| over it. It returns one 157f5b1c8a1SJohn Marino * on success and zero on error. 158f5b1c8a1SJohn Marino */ 159f5b1c8a1SJohn Marino int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out); 160f5b1c8a1SJohn Marino 161f5b1c8a1SJohn Marino /* 162f5b1c8a1SJohn Marino * CBS_get_u16_length_prefixed sets |*out| to the contents of a 16-bit, 163f5b1c8a1SJohn Marino * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It 164f5b1c8a1SJohn Marino * returns one on success and zero on error. 165f5b1c8a1SJohn Marino */ 166f5b1c8a1SJohn Marino int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out); 167f5b1c8a1SJohn Marino 168f5b1c8a1SJohn Marino /* 169f5b1c8a1SJohn Marino * CBS_get_u24_length_prefixed sets |*out| to the contents of a 24-bit, 170f5b1c8a1SJohn Marino * big-endian, length-prefixed value from |cbs| and advances |cbs| over it. It 171f5b1c8a1SJohn Marino * returns one on success and zero on error. 172f5b1c8a1SJohn Marino */ 173f5b1c8a1SJohn Marino int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out); 174f5b1c8a1SJohn Marino 175*de0e0e4dSAntonio Huete Jimenez /* 176*de0e0e4dSAntonio Huete Jimenez * CBS_peek_u8 sets |*out| to the next uint8_t from |cbs|, but does not advance 177*de0e0e4dSAntonio Huete Jimenez * |cbs|. It returns one on success and zero on error. 178*de0e0e4dSAntonio Huete Jimenez */ 179*de0e0e4dSAntonio Huete Jimenez int CBS_peek_u8(CBS *cbs, uint8_t *out); 180*de0e0e4dSAntonio Huete Jimenez 181*de0e0e4dSAntonio Huete Jimenez /* 182*de0e0e4dSAntonio Huete Jimenez * CBS_peek_u16 sets |*out| to the next, big-endian uint16_t from |cbs|, but 183*de0e0e4dSAntonio Huete Jimenez * does not advance |cbs|. It returns one on success and zero on error. 184*de0e0e4dSAntonio Huete Jimenez */ 185*de0e0e4dSAntonio Huete Jimenez int CBS_peek_u16(CBS *cbs, uint16_t *out); 186*de0e0e4dSAntonio Huete Jimenez 187*de0e0e4dSAntonio Huete Jimenez /* 188*de0e0e4dSAntonio Huete Jimenez * CBS_peek_u24 sets |*out| to the next, big-endian 24-bit value from |cbs|, but 189*de0e0e4dSAntonio Huete Jimenez * does not advance |cbs|. It returns one on success and zero on error. 190*de0e0e4dSAntonio Huete Jimenez */ 191*de0e0e4dSAntonio Huete Jimenez int CBS_peek_u24(CBS *cbs, uint32_t *out); 192*de0e0e4dSAntonio Huete Jimenez 193*de0e0e4dSAntonio Huete Jimenez /* 194*de0e0e4dSAntonio Huete Jimenez * CBS_peek_u32 sets |*out| to the next, big-endian uint32_t value from |cbs|, 195*de0e0e4dSAntonio Huete Jimenez * but does not advance |cbs|. It returns one on success and zero on error. 196*de0e0e4dSAntonio Huete Jimenez */ 197*de0e0e4dSAntonio Huete Jimenez int CBS_peek_u32(CBS *cbs, uint32_t *out); 198*de0e0e4dSAntonio Huete Jimenez 199*de0e0e4dSAntonio Huete Jimenez /* 200*de0e0e4dSAntonio Huete Jimenez * CBS_peek_last_u8 sets |*out| to the last uint8_t from |cbs|, but does not 201*de0e0e4dSAntonio Huete Jimenez * shorten |cbs|. It returns one on success and zero on error. 202*de0e0e4dSAntonio Huete Jimenez */ 203*de0e0e4dSAntonio Huete Jimenez int CBS_peek_last_u8(CBS *cbs, uint8_t *out); 204*de0e0e4dSAntonio Huete Jimenez 205f5b1c8a1SJohn Marino 206f5b1c8a1SJohn Marino /* Parsing ASN.1 */ 207f5b1c8a1SJohn Marino 208f5b1c8a1SJohn Marino /* 209f5b1c8a1SJohn Marino * While an identifier can be multiple octets, this library only handles the 210f5b1c8a1SJohn Marino * single octet variety currently. This limits support up to tag number 30 211f5b1c8a1SJohn Marino * since tag number 31 is a reserved value to indicate multiple octets. 212f5b1c8a1SJohn Marino */ 213f5b1c8a1SJohn Marino 214f5b1c8a1SJohn Marino /* Bits 8 and 7: class tag type: See X.690 section 8.1.2.2. */ 215f5b1c8a1SJohn Marino #define CBS_ASN1_UNIVERSAL 0x00 216f5b1c8a1SJohn Marino #define CBS_ASN1_APPLICATION 0x40 217f5b1c8a1SJohn Marino #define CBS_ASN1_CONTEXT_SPECIFIC 0x80 218f5b1c8a1SJohn Marino #define CBS_ASN1_PRIVATE 0xc0 219f5b1c8a1SJohn Marino 220f5b1c8a1SJohn Marino /* Bit 6: Primitive or constructed: See X.690 section 8.1.2.3. */ 221f5b1c8a1SJohn Marino #define CBS_ASN1_PRIMITIVE 0x00 222f5b1c8a1SJohn Marino #define CBS_ASN1_CONSTRUCTED 0x20 223f5b1c8a1SJohn Marino 224f5b1c8a1SJohn Marino /* 225f5b1c8a1SJohn Marino * Bits 5 to 1 are the tag number. See X.680 section 8.6 for tag numbers of 226f5b1c8a1SJohn Marino * the universal class. 227f5b1c8a1SJohn Marino */ 228f5b1c8a1SJohn Marino 229f5b1c8a1SJohn Marino /* 230f5b1c8a1SJohn Marino * Common universal identifier octets. 231f5b1c8a1SJohn Marino * See X.690 section 8.1 and X.680 section 8.6 for universal tag numbers. 232f5b1c8a1SJohn Marino * 233f5b1c8a1SJohn Marino * Note: These definitions are the cause of some of the strange behavior in 234f5b1c8a1SJohn Marino * CBS's bs_ber.c. 235f5b1c8a1SJohn Marino * 236f5b1c8a1SJohn Marino * In BER, it is the sender's option to use primitive or constructed for 237f5b1c8a1SJohn Marino * bitstring (X.690 section 8.6.1) and octetstring (X.690 section 8.7.1). 238f5b1c8a1SJohn Marino * 239f5b1c8a1SJohn Marino * In DER, bitstring and octetstring are required to be primitive 240f5b1c8a1SJohn Marino * (X.690 section 10.2). 241f5b1c8a1SJohn Marino */ 242f5b1c8a1SJohn Marino #define CBS_ASN1_BOOLEAN (CBS_ASN1_UNIVERSAL | CBS_ASN1_PRIMITIVE | 0x1) 243f5b1c8a1SJohn Marino #define CBS_ASN1_INTEGER (CBS_ASN1_UNIVERSAL | CBS_ASN1_PRIMITIVE | 0x2) 244f5b1c8a1SJohn Marino #define CBS_ASN1_BITSTRING (CBS_ASN1_UNIVERSAL | CBS_ASN1_PRIMITIVE | 0x3) 245f5b1c8a1SJohn Marino #define CBS_ASN1_OCTETSTRING (CBS_ASN1_UNIVERSAL | CBS_ASN1_PRIMITIVE | 0x4) 246f5b1c8a1SJohn Marino #define CBS_ASN1_OBJECT (CBS_ASN1_UNIVERSAL | CBS_ASN1_PRIMITIVE | 0x6) 247f5b1c8a1SJohn Marino #define CBS_ASN1_ENUMERATED (CBS_ASN1_UNIVERSAL | CBS_ASN1_PRIMITIVE | 0xa) 248f5b1c8a1SJohn Marino #define CBS_ASN1_SEQUENCE (CBS_ASN1_UNIVERSAL | CBS_ASN1_CONSTRUCTED | 0x10) 249f5b1c8a1SJohn Marino #define CBS_ASN1_SET (CBS_ASN1_UNIVERSAL | CBS_ASN1_CONSTRUCTED | 0x11) 250f5b1c8a1SJohn Marino 251f5b1c8a1SJohn Marino /* 252f5b1c8a1SJohn Marino * CBS_get_asn1 sets |*out| to the contents of DER-encoded, ASN.1 element (not 253f5b1c8a1SJohn Marino * including tag and length bytes) and advances |cbs| over it. The ASN.1 254f5b1c8a1SJohn Marino * element must match |tag_value|. It returns one on success and zero 255f5b1c8a1SJohn Marino * on error. 256f5b1c8a1SJohn Marino * 257f5b1c8a1SJohn Marino * Tag numbers greater than 30 are not supported (i.e. short form only). 258f5b1c8a1SJohn Marino */ 259f5b1c8a1SJohn Marino int CBS_get_asn1(CBS *cbs, CBS *out, unsigned int tag_value); 260f5b1c8a1SJohn Marino 261f5b1c8a1SJohn Marino /* 262f5b1c8a1SJohn Marino * CBS_get_asn1_element acts like |CBS_get_asn1| but |out| will include the 263f5b1c8a1SJohn Marino * ASN.1 header bytes too. 264f5b1c8a1SJohn Marino */ 265f5b1c8a1SJohn Marino int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned int tag_value); 266f5b1c8a1SJohn Marino 267f5b1c8a1SJohn Marino /* 268f5b1c8a1SJohn Marino * CBS_peek_asn1_tag looks ahead at the next ASN.1 tag and returns one 269f5b1c8a1SJohn Marino * if the next ASN.1 element on |cbs| would have tag |tag_value|. If 270f5b1c8a1SJohn Marino * |cbs| is empty or the tag does not match, it returns zero. Note: if 271f5b1c8a1SJohn Marino * it returns one, CBS_get_asn1 may still fail if the rest of the 272f5b1c8a1SJohn Marino * element is malformed. 273f5b1c8a1SJohn Marino */ 274f5b1c8a1SJohn Marino int CBS_peek_asn1_tag(const CBS *cbs, unsigned int tag_value); 275f5b1c8a1SJohn Marino 276f5b1c8a1SJohn Marino /* 277f5b1c8a1SJohn Marino * CBS_get_any_asn1_element sets |*out| to contain the next ASN.1 element from 278f5b1c8a1SJohn Marino * |*cbs| (including header bytes) and advances |*cbs|. It sets |*out_tag| to 279f5b1c8a1SJohn Marino * the tag number and |*out_header_len| to the length of the ASN.1 header. 280f5b1c8a1SJohn Marino * Each of |out|, |out_tag|, and |out_header_len| may be NULL to ignore 281f5b1c8a1SJohn Marino * the value. 282f5b1c8a1SJohn Marino * 283f5b1c8a1SJohn Marino * Tag numbers greater than 30 are not supported (i.e. short form only). 284f5b1c8a1SJohn Marino */ 285f5b1c8a1SJohn Marino int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag, 286f5b1c8a1SJohn Marino size_t *out_header_len); 287f5b1c8a1SJohn Marino 288f5b1c8a1SJohn Marino /* 289f5b1c8a1SJohn Marino * CBS_get_asn1_uint64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1| 290f5b1c8a1SJohn Marino * and sets |*out| to its value. It returns one on success and zero on error, 291f5b1c8a1SJohn Marino * where error includes the integer being negative, or too large to represent 292f5b1c8a1SJohn Marino * in 64 bits. 293f5b1c8a1SJohn Marino */ 294f5b1c8a1SJohn Marino int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out); 295f5b1c8a1SJohn Marino 296f5b1c8a1SJohn Marino /* 297f5b1c8a1SJohn Marino * CBS_get_optional_asn1 gets an optional explicitly-tagged element 298f5b1c8a1SJohn Marino * from |cbs| tagged with |tag| and sets |*out| to its contents. If 299f5b1c8a1SJohn Marino * present, it sets |*out_present| to one, otherwise zero. It returns 300f5b1c8a1SJohn Marino * one on success, whether or not the element was present, and zero on 301f5b1c8a1SJohn Marino * decode failure. 302f5b1c8a1SJohn Marino */ 303f5b1c8a1SJohn Marino int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, 304f5b1c8a1SJohn Marino unsigned int tag); 305f5b1c8a1SJohn Marino 306f5b1c8a1SJohn Marino /* 307f5b1c8a1SJohn Marino * CBS_get_optional_asn1_octet_string gets an optional 308f5b1c8a1SJohn Marino * explicitly-tagged OCTET STRING from |cbs|. If present, it sets 309f5b1c8a1SJohn Marino * |*out| to the string and |*out_present| to one. Otherwise, it sets 310f5b1c8a1SJohn Marino * |*out| to empty and |*out_present| to zero. |out_present| may be 311f5b1c8a1SJohn Marino * NULL. It returns one on success, whether or not the element was 312f5b1c8a1SJohn Marino * present, and zero on decode failure. 313f5b1c8a1SJohn Marino */ 314f5b1c8a1SJohn Marino int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present, 315f5b1c8a1SJohn Marino unsigned int tag); 316f5b1c8a1SJohn Marino 317f5b1c8a1SJohn Marino /* 318f5b1c8a1SJohn Marino * CBS_get_optional_asn1_uint64 gets an optional explicitly-tagged 319f5b1c8a1SJohn Marino * INTEGER from |cbs|. If present, it sets |*out| to the 320f5b1c8a1SJohn Marino * value. Otherwise, it sets |*out| to |default_value|. It returns one 321f5b1c8a1SJohn Marino * on success, whether or not the element was present, and zero on 322f5b1c8a1SJohn Marino * decode failure. 323f5b1c8a1SJohn Marino */ 324f5b1c8a1SJohn Marino int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned int tag, 325f5b1c8a1SJohn Marino uint64_t default_value); 326f5b1c8a1SJohn Marino 327f5b1c8a1SJohn Marino /* 328f5b1c8a1SJohn Marino * CBS_get_optional_asn1_bool gets an optional, explicitly-tagged BOOLEAN from 329f5b1c8a1SJohn Marino * |cbs|. If present, it sets |*out| to either zero or one, based on the 330f5b1c8a1SJohn Marino * boolean. Otherwise, it sets |*out| to |default_value|. It returns one on 331f5b1c8a1SJohn Marino * success, whether or not the element was present, and zero on decode 332f5b1c8a1SJohn Marino * failure. 333f5b1c8a1SJohn Marino */ 334f5b1c8a1SJohn Marino int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned int tag, 335f5b1c8a1SJohn Marino int default_value); 336f5b1c8a1SJohn Marino 337f5b1c8a1SJohn Marino 338f5b1c8a1SJohn Marino /* 339f5b1c8a1SJohn Marino * CRYPTO ByteBuilder. 340f5b1c8a1SJohn Marino * 341f5b1c8a1SJohn Marino * |CBB| objects allow one to build length-prefixed serialisations. A |CBB| 342f5b1c8a1SJohn Marino * object is associated with a buffer and new buffers are created with 343f5b1c8a1SJohn Marino * |CBB_init|. Several |CBB| objects can point at the same buffer when a 344f5b1c8a1SJohn Marino * length-prefix is pending, however only a single |CBB| can be 'current' at 345f5b1c8a1SJohn Marino * any one time. For example, if one calls |CBB_add_u8_length_prefixed| then 346f5b1c8a1SJohn Marino * the new |CBB| points at the same buffer as the original. But if the original 347f5b1c8a1SJohn Marino * |CBB| is used then the length prefix is written out and the new |CBB| must 348f5b1c8a1SJohn Marino * not be used again. 349f5b1c8a1SJohn Marino * 350f5b1c8a1SJohn Marino * If one needs to force a length prefix to be written out because a |CBB| is 351f5b1c8a1SJohn Marino * going out of scope, use |CBB_flush|. 352f5b1c8a1SJohn Marino */ 353f5b1c8a1SJohn Marino 354f5b1c8a1SJohn Marino struct cbb_buffer_st { 355f5b1c8a1SJohn Marino uint8_t *buf; 356f5b1c8a1SJohn Marino 357f5b1c8a1SJohn Marino /* The number of valid bytes. */ 358f5b1c8a1SJohn Marino size_t len; 359f5b1c8a1SJohn Marino 360f5b1c8a1SJohn Marino /* The size of buf. */ 361f5b1c8a1SJohn Marino size_t cap; 362f5b1c8a1SJohn Marino 363f5b1c8a1SJohn Marino /* 364f5b1c8a1SJohn Marino * One iff |buf| is owned by this object. If not then |buf| cannot be 365f5b1c8a1SJohn Marino * resized. 366f5b1c8a1SJohn Marino */ 367f5b1c8a1SJohn Marino char can_resize; 368f5b1c8a1SJohn Marino }; 369f5b1c8a1SJohn Marino 370f5b1c8a1SJohn Marino typedef struct cbb_st { 371f5b1c8a1SJohn Marino struct cbb_buffer_st *base; 372f5b1c8a1SJohn Marino 373f5b1c8a1SJohn Marino /* 374f5b1c8a1SJohn Marino * offset is the offset from the start of |base->buf| to the position of any 375f5b1c8a1SJohn Marino * pending length-prefix. 376f5b1c8a1SJohn Marino */ 377f5b1c8a1SJohn Marino size_t offset; 378f5b1c8a1SJohn Marino 379f5b1c8a1SJohn Marino /* child points to a child CBB if a length-prefix is pending. */ 380f5b1c8a1SJohn Marino struct cbb_st *child; 381f5b1c8a1SJohn Marino 382f5b1c8a1SJohn Marino /* 383f5b1c8a1SJohn Marino * pending_len_len contains the number of bytes in a pending length-prefix, 384f5b1c8a1SJohn Marino * or zero if no length-prefix is pending. 385f5b1c8a1SJohn Marino */ 386f5b1c8a1SJohn Marino uint8_t pending_len_len; 387f5b1c8a1SJohn Marino 388f5b1c8a1SJohn Marino char pending_is_asn1; 389f5b1c8a1SJohn Marino 390f5b1c8a1SJohn Marino /* 391f5b1c8a1SJohn Marino * is_top_level is true iff this is a top-level |CBB| (as opposed to a child 392f5b1c8a1SJohn Marino * |CBB|). Top-level objects are valid arguments for |CBB_finish|. 393f5b1c8a1SJohn Marino */ 394f5b1c8a1SJohn Marino char is_top_level; 395f5b1c8a1SJohn Marino } CBB; 396f5b1c8a1SJohn Marino 397f5b1c8a1SJohn Marino /* 398f5b1c8a1SJohn Marino * CBB_init initialises |cbb| with |initial_capacity|. Since a |CBB| grows as 399f5b1c8a1SJohn Marino * needed, the |initial_capacity| is just a hint. It returns one on success or 400f5b1c8a1SJohn Marino * zero on error. 401f5b1c8a1SJohn Marino */ 402f5b1c8a1SJohn Marino int CBB_init(CBB *cbb, size_t initial_capacity); 403f5b1c8a1SJohn Marino 404f5b1c8a1SJohn Marino /* 405f5b1c8a1SJohn Marino * CBB_init_fixed initialises |cbb| to write to |len| bytes at |buf|. Since 406f5b1c8a1SJohn Marino * |buf| cannot grow, trying to write more than |len| bytes will cause CBB 407f5b1c8a1SJohn Marino * functions to fail. It returns one on success or zero on error. 408f5b1c8a1SJohn Marino */ 409f5b1c8a1SJohn Marino int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len); 410f5b1c8a1SJohn Marino 411f5b1c8a1SJohn Marino /* 412f5b1c8a1SJohn Marino * CBB_cleanup frees all resources owned by |cbb| and other |CBB| objects 413f5b1c8a1SJohn Marino * writing to the same buffer. This should be used in an error case where a 414f5b1c8a1SJohn Marino * serialisation is abandoned. 415f5b1c8a1SJohn Marino */ 416f5b1c8a1SJohn Marino void CBB_cleanup(CBB *cbb); 417f5b1c8a1SJohn Marino 418f5b1c8a1SJohn Marino /* 419f5b1c8a1SJohn Marino * CBB_finish completes any pending length prefix and sets |*out_data| to a 420f5b1c8a1SJohn Marino * malloced buffer and |*out_len| to the length of that buffer. The caller 421f5b1c8a1SJohn Marino * takes ownership of the buffer and, unless the buffer was fixed with 422f5b1c8a1SJohn Marino * |CBB_init_fixed|, must call |free| when done. 423f5b1c8a1SJohn Marino * 424f5b1c8a1SJohn Marino * It can only be called on a "top level" |CBB|, i.e. one initialised with 425f5b1c8a1SJohn Marino * |CBB_init| or |CBB_init_fixed|. It returns one on success and zero on 426f5b1c8a1SJohn Marino * error. 427f5b1c8a1SJohn Marino */ 428f5b1c8a1SJohn Marino int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len); 429f5b1c8a1SJohn Marino 430f5b1c8a1SJohn Marino /* 431f5b1c8a1SJohn Marino * CBB_flush causes any pending length prefixes to be written out and any child 432f5b1c8a1SJohn Marino * |CBB| objects of |cbb| to be invalidated. It returns one on success or zero 433f5b1c8a1SJohn Marino * on error. 434f5b1c8a1SJohn Marino */ 435f5b1c8a1SJohn Marino int CBB_flush(CBB *cbb); 436f5b1c8a1SJohn Marino 437f5b1c8a1SJohn Marino /* 43872c33676SMaxim Ag * CBB_discard_child discards the current unflushed child of |cbb|. Neither the 43972c33676SMaxim Ag * child's contents nor the length prefix will be included in the output. 44072c33676SMaxim Ag */ 44172c33676SMaxim Ag void CBB_discard_child(CBB *cbb); 44272c33676SMaxim Ag 44372c33676SMaxim Ag /* 444f5b1c8a1SJohn Marino * CBB_add_u8_length_prefixed sets |*out_contents| to a new child of |cbb|. The 445f5b1c8a1SJohn Marino * data written to |*out_contents| will be prefixed in |cbb| with an 8-bit 446f5b1c8a1SJohn Marino * length. It returns one on success or zero on error. 447f5b1c8a1SJohn Marino */ 448f5b1c8a1SJohn Marino int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents); 449f5b1c8a1SJohn Marino 450f5b1c8a1SJohn Marino /* 451f5b1c8a1SJohn Marino * CBB_add_u16_length_prefixed sets |*out_contents| to a new child of |cbb|. 452f5b1c8a1SJohn Marino * The data written to |*out_contents| will be prefixed in |cbb| with a 16-bit, 453f5b1c8a1SJohn Marino * big-endian length. It returns one on success or zero on error. 454f5b1c8a1SJohn Marino */ 455f5b1c8a1SJohn Marino int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents); 456f5b1c8a1SJohn Marino 457f5b1c8a1SJohn Marino /* 458f5b1c8a1SJohn Marino * CBB_add_u24_length_prefixed sets |*out_contents| to a new child of |cbb|. 459f5b1c8a1SJohn Marino * The data written to |*out_contents| will be prefixed in |cbb| with a 24-bit, 460f5b1c8a1SJohn Marino * big-endian length. It returns one on success or zero on error. 461f5b1c8a1SJohn Marino */ 462f5b1c8a1SJohn Marino int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents); 463f5b1c8a1SJohn Marino 464f5b1c8a1SJohn Marino /* 465f5b1c8a1SJohn Marino * CBB_add_asn sets |*out_contents| to a |CBB| into which the contents of an 466f5b1c8a1SJohn Marino * ASN.1 object can be written. The |tag| argument will be used as the tag for 467f5b1c8a1SJohn Marino * the object. Passing in |tag| number 31 will return in an error since only 468f5b1c8a1SJohn Marino * single octet identifiers are supported. It returns one on success or zero 469f5b1c8a1SJohn Marino * on error. 470f5b1c8a1SJohn Marino */ 471f5b1c8a1SJohn Marino int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned int tag); 472f5b1c8a1SJohn Marino 473f5b1c8a1SJohn Marino /* 474f5b1c8a1SJohn Marino * CBB_add_bytes appends |len| bytes from |data| to |cbb|. It returns one on 475f5b1c8a1SJohn Marino * success and zero otherwise. 476f5b1c8a1SJohn Marino */ 477f5b1c8a1SJohn Marino int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len); 478f5b1c8a1SJohn Marino 479f5b1c8a1SJohn Marino /* 480f5b1c8a1SJohn Marino * CBB_add_space appends |len| bytes to |cbb| and sets |*out_data| to point to 481f5b1c8a1SJohn Marino * the beginning of that space. The caller must then write |len| bytes of 482f5b1c8a1SJohn Marino * actual contents to |*out_data|. It returns one on success and zero 483f5b1c8a1SJohn Marino * otherwise. 484f5b1c8a1SJohn Marino */ 485f5b1c8a1SJohn Marino int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len); 486f5b1c8a1SJohn Marino 487f5b1c8a1SJohn Marino /* 488f5b1c8a1SJohn Marino * CBB_add_u8 appends an 8-bit number from |value| to |cbb|. It returns one on 489f5b1c8a1SJohn Marino * success and zero otherwise. 490f5b1c8a1SJohn Marino */ 491f5b1c8a1SJohn Marino int CBB_add_u8(CBB *cbb, size_t value); 492f5b1c8a1SJohn Marino 493f5b1c8a1SJohn Marino /* 494f5b1c8a1SJohn Marino * CBB_add_u8 appends a 16-bit, big-endian number from |value| to |cbb|. It 495f5b1c8a1SJohn Marino * returns one on success and zero otherwise. 496f5b1c8a1SJohn Marino */ 497f5b1c8a1SJohn Marino int CBB_add_u16(CBB *cbb, size_t value); 498f5b1c8a1SJohn Marino 499f5b1c8a1SJohn Marino /* 500f5b1c8a1SJohn Marino * CBB_add_u24 appends a 24-bit, big-endian number from |value| to |cbb|. It 501f5b1c8a1SJohn Marino * returns one on success and zero otherwise. 502f5b1c8a1SJohn Marino */ 503f5b1c8a1SJohn Marino int CBB_add_u24(CBB *cbb, size_t value); 504f5b1c8a1SJohn Marino 505f5b1c8a1SJohn Marino /* 50672c33676SMaxim Ag * CBB_add_u32 appends a 32-bit, big-endian number from |value| to |cbb|. It 50772c33676SMaxim Ag * returns one on success and zero otherwise. 50872c33676SMaxim Ag */ 50972c33676SMaxim Ag int CBB_add_u32(CBB *cbb, size_t value); 51072c33676SMaxim Ag 51172c33676SMaxim Ag /* 512*de0e0e4dSAntonio Huete Jimenez * CBB_add_u64 appends a 64-bit, big-endian number from |value| to |cbb|. It 513*de0e0e4dSAntonio Huete Jimenez * returns one on success and zero otherwise. 514*de0e0e4dSAntonio Huete Jimenez */ 515*de0e0e4dSAntonio Huete Jimenez int CBB_add_u64(CBB *cbb, uint64_t value); 516*de0e0e4dSAntonio Huete Jimenez 517*de0e0e4dSAntonio Huete Jimenez /* 518f5b1c8a1SJohn Marino * CBB_add_asn1_uint64 writes an ASN.1 INTEGER into |cbb| using |CBB_add_asn1| 519f5b1c8a1SJohn Marino * and writes |value| in its contents. It returns one on success and zero on 520f5b1c8a1SJohn Marino * error. 521f5b1c8a1SJohn Marino */ 522f5b1c8a1SJohn Marino int CBB_add_asn1_uint64(CBB *cbb, uint64_t value); 523f5b1c8a1SJohn Marino 524f5b1c8a1SJohn Marino #ifdef LIBRESSL_INTERNAL 525f5b1c8a1SJohn Marino /* 526f5b1c8a1SJohn Marino * CBS_dup sets |out| to point to cbs's |data| and |len|. It results in two 527f5b1c8a1SJohn Marino * CBS that point to the same buffer. 528f5b1c8a1SJohn Marino */ 529f5b1c8a1SJohn Marino void CBS_dup(const CBS *cbs, CBS *out); 530f5b1c8a1SJohn Marino 531f5b1c8a1SJohn Marino /* 532f5b1c8a1SJohn Marino * cbs_get_any_asn1_element sets |*out| to contain the next ASN.1 element from 533f5b1c8a1SJohn Marino * |*cbs| (including header bytes) and advances |*cbs|. It sets |*out_tag| to 534f5b1c8a1SJohn Marino * the tag number and |*out_header_len| to the length of the ASN.1 header. If 535f5b1c8a1SJohn Marino * strict mode is disabled and the element has indefinite length then |*out| 536f5b1c8a1SJohn Marino * will only contain the header. Each of |out|, |out_tag|, and 537f5b1c8a1SJohn Marino * |out_header_len| may be NULL to ignore the value. 538f5b1c8a1SJohn Marino * 539f5b1c8a1SJohn Marino * Tag numbers greater than 30 are not supported (i.e. short form only). 540f5b1c8a1SJohn Marino */ 541f5b1c8a1SJohn Marino int cbs_get_any_asn1_element_internal(CBS *cbs, CBS *out, unsigned int *out_tag, 542f5b1c8a1SJohn Marino size_t *out_header_len, int strict); 543f5b1c8a1SJohn Marino 544f5b1c8a1SJohn Marino /* 545f5b1c8a1SJohn Marino * CBS_asn1_indefinite_to_definite reads an ASN.1 structure from |in|. If it 546f5b1c8a1SJohn Marino * finds indefinite-length elements that otherwise appear to be valid DER, it 547f5b1c8a1SJohn Marino * attempts to convert the DER-like data to DER and sets |*out| and 548f5b1c8a1SJohn Marino * |*out_length| to describe a malloced buffer containing the DER data. 549f5b1c8a1SJohn Marino * Additionally, |*in| will be advanced over the ASN.1 data. 550f5b1c8a1SJohn Marino * 551f5b1c8a1SJohn Marino * If it doesn't find any indefinite-length elements then it sets |*out| to 552f5b1c8a1SJohn Marino * NULL and |*in| is unmodified. 553f5b1c8a1SJohn Marino * 554f5b1c8a1SJohn Marino * This is NOT a conversion from BER to DER. There are many restrictions when 555f5b1c8a1SJohn Marino * dealing with DER data. This is only concerned with one: indefinite vs. 556f5b1c8a1SJohn Marino * definite form. However, this suffices to handle the PKCS#7 and PKCS#12 output 557f5b1c8a1SJohn Marino * from NSS. 558f5b1c8a1SJohn Marino * 559f5b1c8a1SJohn Marino * It returns one on success and zero otherwise. 560f5b1c8a1SJohn Marino */ 561f5b1c8a1SJohn Marino int CBS_asn1_indefinite_to_definite(CBS *in, uint8_t **out, size_t *out_len); 562f5b1c8a1SJohn Marino #endif /* LIBRESSL_INTERNAL */ 563f5b1c8a1SJohn Marino 56472c33676SMaxim Ag __END_HIDDEN_DECLS 565f5b1c8a1SJohn Marino 566f5b1c8a1SJohn Marino #endif /* OPENSSL_HEADER_BYTESTRING_H */ 567