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