1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2012-2019,2021,2022) Apple Inc. All rights reserved. 2*2c2f96dcSApple OSS Distributions * 3*2c2f96dcSApple OSS Distributions * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which 4*2c2f96dcSApple OSS Distributions * is contained in the License.txt file distributed with corecrypto) and only to 5*2c2f96dcSApple OSS Distributions * people who accept that license. IMPORTANT: Any license rights granted to you by 6*2c2f96dcSApple OSS Distributions * Apple Inc. (if any) are limited to internal use within your organization only on 7*2c2f96dcSApple OSS Distributions * devices and computers you own or control, for the sole purpose of verifying the 8*2c2f96dcSApple OSS Distributions * security characteristics and correct functioning of the Apple Software. You may 9*2c2f96dcSApple OSS Distributions * not, directly or indirectly, redistribute the Apple Software or any portions thereof. 10*2c2f96dcSApple OSS Distributions */ 11*2c2f96dcSApple OSS Distributions 12*2c2f96dcSApple OSS Distributions #ifndef _CORECRYPTO_CCDER_H_ 13*2c2f96dcSApple OSS Distributions #define _CORECRYPTO_CCDER_H_ 14*2c2f96dcSApple OSS Distributions 15*2c2f96dcSApple OSS Distributions #include <corecrypto/cc.h> 16*2c2f96dcSApple OSS Distributions #include <corecrypto/ccasn1.h> 17*2c2f96dcSApple OSS Distributions #include <corecrypto/ccn.h> 18*2c2f96dcSApple OSS Distributions #include <corecrypto/ccder_blob.h> 19*2c2f96dcSApple OSS Distributions 20*2c2f96dcSApple OSS Distributions /* DER types to be used with ccder_decode and ccder_encode functions. */ 21*2c2f96dcSApple OSS Distributions #define CCDER_EOL CCASN1_EOL 22*2c2f96dcSApple OSS Distributions #define CCDER_BOOLEAN CCASN1_BOOLEAN 23*2c2f96dcSApple OSS Distributions #define CCDER_INTEGER CCASN1_INTEGER 24*2c2f96dcSApple OSS Distributions #define CCDER_BIT_STRING CCASN1_BIT_STRING 25*2c2f96dcSApple OSS Distributions #define CCDER_OCTET_STRING CCASN1_OCTET_STRING 26*2c2f96dcSApple OSS Distributions #define CCDER_NULL CCASN1_NULL 27*2c2f96dcSApple OSS Distributions #define CCDER_OBJECT_IDENTIFIER CCASN1_OBJECT_IDENTIFIER 28*2c2f96dcSApple OSS Distributions #define CCDER_OBJECT_DESCRIPTOR CCASN1_OBJECT_DESCRIPTOR 29*2c2f96dcSApple OSS Distributions /* External or instance-of 0x08 */ 30*2c2f96dcSApple OSS Distributions #define CCDER_REAL CCASN1_REAL 31*2c2f96dcSApple OSS Distributions #define CCDER_ENUMERATED CCASN1_ENUMERATED 32*2c2f96dcSApple OSS Distributions #define CCDER_EMBEDDED_PDV CCASN1_EMBEDDED_PDV 33*2c2f96dcSApple OSS Distributions #define CCDER_UTF8_STRING CCASN1_UTF8_STRING 34*2c2f96dcSApple OSS Distributions /* 0x0d */ 35*2c2f96dcSApple OSS Distributions /* 0x0e */ 36*2c2f96dcSApple OSS Distributions /* 0x0f */ 37*2c2f96dcSApple OSS Distributions #define CCDER_SEQUENCE CCASN1_SEQUENCE 38*2c2f96dcSApple OSS Distributions #define CCDER_SET CCASN1_SET 39*2c2f96dcSApple OSS Distributions #define CCDER_NUMERIC_STRING CCASN1_NUMERIC_STRING 40*2c2f96dcSApple OSS Distributions #define CCDER_PRINTABLE_STRING CCASN1_PRINTABLE_STRING 41*2c2f96dcSApple OSS Distributions #define CCDER_T61_STRING CCASN1_T61_STRING 42*2c2f96dcSApple OSS Distributions #define CCDER_VIDEOTEX_STRING CCASN1_VIDEOTEX_STRING 43*2c2f96dcSApple OSS Distributions #define CCDER_IA5_STRING CCASN1_IA5_STRING 44*2c2f96dcSApple OSS Distributions #define CCDER_UTC_TIME CCASN1_UTC_TIME 45*2c2f96dcSApple OSS Distributions #define CCDER_GENERALIZED_TIME CCASN1_GENERALIZED_TIME 46*2c2f96dcSApple OSS Distributions #define CCDER_GRAPHIC_STRING CCASN1_GRAPHIC_STRING 47*2c2f96dcSApple OSS Distributions #define CCDER_VISIBLE_STRING CCASN1_VISIBLE_STRING 48*2c2f96dcSApple OSS Distributions #define CCDER_GENERAL_STRING CCASN1_GENERAL_STRING 49*2c2f96dcSApple OSS Distributions #define CCDER_UNIVERSAL_STRING CCASN1_UNIVERSAL_STRING 50*2c2f96dcSApple OSS Distributions /* 0x1d */ 51*2c2f96dcSApple OSS Distributions #define CCDER_BMP_STRING CCASN1_BMP_STRING 52*2c2f96dcSApple OSS Distributions #define CCDER_HIGH_TAG_NUMBER CCASN1_HIGH_TAG_NUMBER 53*2c2f96dcSApple OSS Distributions #define CCDER_TELETEX_STRING CCDER_T61_STRING 54*2c2f96dcSApple OSS Distributions 55*2c2f96dcSApple OSS Distributions #ifdef CCDER_MULTIBYTE_TAGS 56*2c2f96dcSApple OSS Distributions #define CCDER_TAG_MASK ((ccder_tag)~0) 57*2c2f96dcSApple OSS Distributions #define CCDER_TAGNUM_MASK ((ccder_tag) ~((ccder_tag)7 << (sizeof(ccder_tag) * 8 - 3))) 58*2c2f96dcSApple OSS Distributions 59*2c2f96dcSApple OSS Distributions #define CCDER_METHOD_MASK ((ccder_tag)1 << (sizeof(ccder_tag) * 8 - 3)) 60*2c2f96dcSApple OSS Distributions #define CCDER_PRIMITIVE ((ccder_tag)0 << (sizeof(ccder_tag) * 8 - 3)) 61*2c2f96dcSApple OSS Distributions #define CCDER_CONSTRUCTED ((ccder_tag)1 << (sizeof(ccder_tag) * 8 - 3)) 62*2c2f96dcSApple OSS Distributions 63*2c2f96dcSApple OSS Distributions #define CCDER_CLASS_MASK ((ccder_tag)3 << (sizeof(ccder_tag) * 8 - 2)) 64*2c2f96dcSApple OSS Distributions #define CCDER_UNIVERSAL ((ccder_tag)0 << (sizeof(ccder_tag) * 8 - 2)) 65*2c2f96dcSApple OSS Distributions #define CCDER_APPLICATION ((ccder_tag)1 << (sizeof(ccder_tag) * 8 - 2)) 66*2c2f96dcSApple OSS Distributions #define CCDER_CONTEXT_SPECIFIC ((ccder_tag)2 << (sizeof(ccder_tag) * 8 - 2)) 67*2c2f96dcSApple OSS Distributions #define CCDER_PRIVATE ((ccder_tag)3 << (sizeof(ccder_tag) * 8 - 2)) 68*2c2f96dcSApple OSS Distributions #else /* !CCDER_MULTIBYTE_TAGS */ 69*2c2f96dcSApple OSS Distributions #define CCDER_TAG_MASK CCASN1_TAG_MASK 70*2c2f96dcSApple OSS Distributions #define CCDER_TAGNUM_MASK CCASN1_TAGNUM_MASK 71*2c2f96dcSApple OSS Distributions 72*2c2f96dcSApple OSS Distributions #define CCDER_METHOD_MASK CCASN1_METHOD_MASK 73*2c2f96dcSApple OSS Distributions #define CCDER_PRIMITIVE CCASN1_PRIMITIVE 74*2c2f96dcSApple OSS Distributions #define CCDER_CONSTRUCTED CCASN1_CONSTRUCTED 75*2c2f96dcSApple OSS Distributions 76*2c2f96dcSApple OSS Distributions #define CCDER_CLASS_MASK CCASN1_CLASS_MASK 77*2c2f96dcSApple OSS Distributions #define CCDER_UNIVERSAL CCASN1_UNIVERSAL 78*2c2f96dcSApple OSS Distributions #define CCDER_APPLICATION CCASN1_APPLICATION 79*2c2f96dcSApple OSS Distributions #define CCDER_CONTEXT_SPECIFIC CCASN1_CONTEXT_SPECIFIC 80*2c2f96dcSApple OSS Distributions #define CCDER_PRIVATE CCASN1_PRIVATE 81*2c2f96dcSApple OSS Distributions #endif /* !CCDER_MULTIBYTE_TAGS */ 82*2c2f96dcSApple OSS Distributions #define CCDER_CONSTRUCTED_SET (CCDER_SET | CCDER_CONSTRUCTED) 83*2c2f96dcSApple OSS Distributions #define CCDER_CONSTRUCTED_SEQUENCE (CCDER_SEQUENCE | CCDER_CONSTRUCTED) 84*2c2f96dcSApple OSS Distributions 85*2c2f96dcSApple OSS Distributions // MARK: - ccder_sizeof_ functions 86*2c2f96dcSApple OSS Distributions 87*2c2f96dcSApple OSS Distributions /* Returns the size of an asn1 encoded item of length l in bytes. */ 88*2c2f96dcSApple OSS Distributions CC_CONST 89*2c2f96dcSApple OSS Distributions size_t ccder_sizeof(ccder_tag tag, size_t len); 90*2c2f96dcSApple OSS Distributions 91*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL 92*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_overflow(ccder_tag tag, size_t nbytes, bool *overflowed); 93*2c2f96dcSApple OSS Distributions 94*2c2f96dcSApple OSS Distributions CC_PURE 95*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_implicit_integer(ccder_tag implicit_tag, cc_size n, const cc_unit *s); 96*2c2f96dcSApple OSS Distributions 97*2c2f96dcSApple OSS Distributions CC_PURE 98*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_implicit_octet_string(ccder_tag implicit_tag, cc_size n, const cc_unit *s); 99*2c2f96dcSApple OSS Distributions 100*2c2f96dcSApple OSS Distributions CC_CONST 101*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_implicit_raw_octet_string(ccder_tag implicit_tag, size_t s_size); 102*2c2f96dcSApple OSS Distributions 103*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL 104*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_implicit_raw_octet_string_overflow(ccder_tag implicit_tag, size_t s_size, bool *overflowed); 105*2c2f96dcSApple OSS Distributions 106*2c2f96dcSApple OSS Distributions CC_CONST 107*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_implicit_uint64(ccder_tag implicit_tag, uint64_t value); 108*2c2f96dcSApple OSS Distributions 109*2c2f96dcSApple OSS Distributions CC_PURE 110*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_integer(cc_size n, const cc_unit *s); 111*2c2f96dcSApple OSS Distributions 112*2c2f96dcSApple OSS Distributions CC_CONST 113*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_len(size_t len); 114*2c2f96dcSApple OSS Distributions 115*2c2f96dcSApple OSS Distributions CC_PURE 116*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_octet_string(cc_size n, const cc_unit *s); 117*2c2f96dcSApple OSS Distributions 118*2c2f96dcSApple OSS Distributions CC_PURE 119*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_oid(ccoid_t oid); 120*2c2f96dcSApple OSS Distributions 121*2c2f96dcSApple OSS Distributions CC_CONST 122*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_raw_octet_string(size_t s_size); 123*2c2f96dcSApple OSS Distributions 124*2c2f96dcSApple OSS Distributions CC_CONST 125*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_tag(ccder_tag tag); 126*2c2f96dcSApple OSS Distributions 127*2c2f96dcSApple OSS Distributions CC_CONST 128*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_uint64(uint64_t value); 129*2c2f96dcSApple OSS Distributions 130*2c2f96dcSApple OSS Distributions CC_PURE 131*2c2f96dcSApple OSS Distributions size_t ccder_sizeof_eckey(size_t priv_size, ccoid_t oid, size_t pub_size); 132*2c2f96dcSApple OSS Distributions 133*2c2f96dcSApple OSS Distributions /* alias of ccder_sizeof_eckey */ 134*2c2f96dcSApple OSS Distributions CC_PURE 135*2c2f96dcSApple OSS Distributions size_t ccder_encode_eckey_size(size_t priv_size, ccoid_t oid, size_t pub_size); 136*2c2f96dcSApple OSS Distributions 137*2c2f96dcSApple OSS Distributions /* All of the original functions are unavailable in a ptrcheck build. */ 138*2c2f96dcSApple OSS Distributions // MARK: - Encode/decode functions, unavailable in ptrcheck 139*2c2f96dcSApple OSS Distributions 140*2c2f96dcSApple OSS Distributions /* Encode a tag backwards, der_end should point to one byte past the end of 141*2c2f96dcSApple OSS Distributions destination for the tag, returns a pointer to the first byte of the tag. 142*2c2f96dcSApple OSS Distributions Returns NULL if there is an encoding error. */ 143*2c2f96dcSApple OSS Distributions CC_NONNULL((2)) cc_ptrcheck_unavailable() 144*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_tag(ccder_tag tag, const uint8_t *der, uint8_t *der_end); 145*2c2f96dcSApple OSS Distributions 146*2c2f96dcSApple OSS Distributions /* Returns a pointer to the start of the len field. returns NULL if there 147*2c2f96dcSApple OSS Distributions is an encoding error. */ 148*2c2f96dcSApple OSS Distributions CC_NONNULL((2)) cc_ptrcheck_unavailable() 149*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_len(size_t len, const uint8_t *der, uint8_t *der_end); 150*2c2f96dcSApple OSS Distributions 151*2c2f96dcSApple OSS Distributions /* der_end should point to the first byte of the content of this der item. */ 152*2c2f96dcSApple OSS Distributions CC_NONNULL((3)) cc_ptrcheck_unavailable() 153*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_tl(ccder_tag tag, size_t len, const uint8_t *der, uint8_t *der_end); 154*2c2f96dcSApple OSS Distributions 155*2c2f96dcSApple OSS Distributions CC_PURE CC_NONNULL((2)) cc_ptrcheck_unavailable() 156*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_body_nocopy(size_t size, const uint8_t *der, uint8_t *der_end); 157*2c2f96dcSApple OSS Distributions 158*2c2f96dcSApple OSS Distributions /* Encode the tag and length of a constructed object. der is the lower 159*2c2f96dcSApple OSS Distributions bound, der_end is one byte paste where we want to write the length and 160*2c2f96dcSApple OSS Distributions body_end is one byte past the end of the body of the der object we are 161*2c2f96dcSApple OSS Distributions encoding the tag and length of. */ 162*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 3)) cc_ptrcheck_unavailable() 163*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_constructed_tl(ccder_tag tag, const uint8_t *body_end, const uint8_t *der, uint8_t *der_end); 164*2c2f96dcSApple OSS Distributions 165*2c2f96dcSApple OSS Distributions /* Encodes oid into der and returns der + ccder_sizeof_oid(oid). */ 166*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2)) cc_ptrcheck_unavailable() 167*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_oid(ccoid_t oid, const uint8_t *der, uint8_t *der_end); 168*2c2f96dcSApple OSS Distributions 169*2c2f96dcSApple OSS Distributions CC_NONNULL((3, 4)) cc_ptrcheck_unavailable() 170*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_implicit_integer(ccder_tag implicit_tag, cc_size n, const cc_unit *s, const uint8_t *der, uint8_t *der_end); 171*2c2f96dcSApple OSS Distributions 172*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 3)) cc_ptrcheck_unavailable() 173*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_integer(cc_size n, const cc_unit *s, const uint8_t *der, uint8_t *der_end); 174*2c2f96dcSApple OSS Distributions 175*2c2f96dcSApple OSS Distributions CC_NONNULL((3)) cc_ptrcheck_unavailable() 176*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_implicit_uint64(ccder_tag implicit_tag, uint64_t value, const uint8_t *der, uint8_t *der_end); 177*2c2f96dcSApple OSS Distributions 178*2c2f96dcSApple OSS Distributions CC_NONNULL((2)) cc_ptrcheck_unavailable() 179*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_uint64(uint64_t value, const uint8_t *der, uint8_t *der_end); 180*2c2f96dcSApple OSS Distributions 181*2c2f96dcSApple OSS Distributions CC_NONNULL((3, 4)) cc_ptrcheck_unavailable() 182*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_implicit_octet_string(ccder_tag implicit_tag, cc_size n, const cc_unit *s, const uint8_t *der, uint8_t *der_end); 183*2c2f96dcSApple OSS Distributions 184*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 3)) cc_ptrcheck_unavailable() 185*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_octet_string(cc_size n, const cc_unit *s, const uint8_t *der, uint8_t *der_end); 186*2c2f96dcSApple OSS Distributions 187*2c2f96dcSApple OSS Distributions CC_NONNULL((3, 4)) cc_ptrcheck_unavailable() 188*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_implicit_raw_octet_string(ccder_tag implicit_tag, 189*2c2f96dcSApple OSS Distributions size_t s_size, 190*2c2f96dcSApple OSS Distributions const uint8_t *s, 191*2c2f96dcSApple OSS Distributions const uint8_t *der, 192*2c2f96dcSApple OSS Distributions uint8_t *der_end); 193*2c2f96dcSApple OSS Distributions 194*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 3)) cc_ptrcheck_unavailable() 195*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_raw_octet_string(size_t s_size, const uint8_t *s, const uint8_t *der, uint8_t *der_end); 196*2c2f96dcSApple OSS Distributions 197*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 5, 6)) cc_ptrcheck_unavailable() 198*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_eckey(size_t priv_size, 199*2c2f96dcSApple OSS Distributions const uint8_t *priv_key, 200*2c2f96dcSApple OSS Distributions ccoid_t oid, 201*2c2f96dcSApple OSS Distributions size_t pub_size, 202*2c2f96dcSApple OSS Distributions const uint8_t *pub_key, 203*2c2f96dcSApple OSS Distributions uint8_t *der, 204*2c2f96dcSApple OSS Distributions uint8_t *der_end); 205*2c2f96dcSApple OSS Distributions 206*2c2f96dcSApple OSS Distributions /* ccder_encode_body COPIES the body into the der. 207*2c2f96dcSApple OSS Distributions It's inefficient – especially when you already have to convert to get to 208*2c2f96dcSApple OSS Distributions the form for the body. 209*2c2f96dcSApple OSS Distributions see encode integer for the right way to unify conversion and insertion */ 210*2c2f96dcSApple OSS Distributions CC_NONNULL((3)) cc_ptrcheck_unavailable() 211*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_body(size_t size, const uint8_t *body, const uint8_t *der, uint8_t *der_end); 212*2c2f96dcSApple OSS Distributions 213*2c2f96dcSApple OSS Distributions /* Returns a pointer to the start of the length field, and returns the decoded tag in tag. 214*2c2f96dcSApple OSS Distributions returns NULL if there is a decoding error. */ 215*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3)) cc_ptrcheck_unavailable() 216*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_tag(ccder_tag *tagp, const uint8_t *der, const uint8_t *der_end); 217*2c2f96dcSApple OSS Distributions 218*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3)) cc_ptrcheck_unavailable() 219*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_len(size_t *lenp, const uint8_t *der, const uint8_t *der_end); 220*2c2f96dcSApple OSS Distributions 221*2c2f96dcSApple OSS Distributions /*! 222*2c2f96dcSApple OSS Distributions @function ccder_decode_len_strict 223*2c2f96dcSApple OSS Distributions @abstract Decode the length of a DER encoded item 224*2c2f96dcSApple OSS Distributions 225*2c2f96dcSApple OSS Distributions @param lenp Pointer to the length of the DER item 226*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 227*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 228*2c2f96dcSApple OSS Distributions 229*2c2f96dcSApple OSS Distributions @result First byte after the parsed length or NULL if the length is not valid (i.e. when the length isn't DER encoded) 230*2c2f96dcSApple OSS Distributions */ 231*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3)) cc_ptrcheck_unavailable() 232*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_len_strict(size_t *lenp, const uint8_t *der, const uint8_t *der_end); 233*2c2f96dcSApple OSS Distributions 234*2c2f96dcSApple OSS Distributions /* Returns a pointer to the start of the der object, and returns the length in len. 235*2c2f96dcSApple OSS Distributions returns NULL if there is a decoding error. */ 236*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 4)) cc_ptrcheck_unavailable() 237*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_tl(ccder_tag expected_tag, size_t *lenp, const uint8_t *der, const uint8_t *der_end); 238*2c2f96dcSApple OSS Distributions 239*2c2f96dcSApple OSS Distributions /*! 240*2c2f96dcSApple OSS Distributions @function ccder_decode_tl_strict 241*2c2f96dcSApple OSS Distributions @abstract Decode a tag and length from a DER object given an expected tag. 242*2c2f96dcSApple OSS Distributions 243*2c2f96dcSApple OSS Distributions @param expected_tag Tag of expected DER object pointed to by `der` 244*2c2f96dcSApple OSS Distributions @param lenp Output length of DER object 245*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 246*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 247*2c2f96dcSApple OSS Distributions 248*2c2f96dcSApple OSS Distributions @result Pointer to the DER object with the length contained in `lenp` otherwise NULL. 249*2c2f96dcSApple OSS Distributions */ 250*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 4)) cc_ptrcheck_unavailable() 251*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_tl_strict(ccder_tag expected_tag, size_t *lenp, const uint8_t *der, const uint8_t *der_end); 252*2c2f96dcSApple OSS Distributions 253*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 4)) cc_ptrcheck_unavailable() 254*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_constructed_tl(ccder_tag expected_tag, 255*2c2f96dcSApple OSS Distributions const uint8_t **body_end, 256*2c2f96dcSApple OSS Distributions const uint8_t *der, 257*2c2f96dcSApple OSS Distributions const uint8_t *der_end); 258*2c2f96dcSApple OSS Distributions 259*2c2f96dcSApple OSS Distributions /*! 260*2c2f96dcSApple OSS Distributions @function ccder_decode_constructed_tl_strict 261*2c2f96dcSApple OSS Distributions @abstract Decode a tag and length from a contstructed DER object given an expected tag. 262*2c2f96dcSApple OSS Distributions 263*2c2f96dcSApple OSS Distributions @param expected_tag Tag of expected DER object pointed to by `der` 264*2c2f96dcSApple OSS Distributions @param body_end Pointer to hold the end of the sequence 265*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 266*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 267*2c2f96dcSApple OSS Distributions 268*2c2f96dcSApple OSS Distributions @result Pointer to the first DER object within the constructed object and the length of the total constructed object 269*2c2f96dcSApple OSS Distributions contained in `lenp`; NULL otherwise. 270*2c2f96dcSApple OSS Distributions */ 271*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 4)) cc_ptrcheck_unavailable() 272*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_constructed_tl_strict(ccder_tag expected_tag, const uint8_t **body_end, const uint8_t *der, const uint8_t *der_end); 273*2c2f96dcSApple OSS Distributions 274*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3)) cc_ptrcheck_unavailable() 275*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_sequence_tl(const uint8_t **body_end, const uint8_t *der, const uint8_t *der_end); 276*2c2f96dcSApple OSS Distributions 277*2c2f96dcSApple OSS Distributions /*! 278*2c2f96dcSApple OSS Distributions @function ccder_decode_sequence_tl_strict 279*2c2f96dcSApple OSS Distributions @abstract Decode a DER sequence. 280*2c2f96dcSApple OSS Distributions 281*2c2f96dcSApple OSS Distributions @param body_end Pointer to hold the end of the sequence 282*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 283*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 284*2c2f96dcSApple OSS Distributions 285*2c2f96dcSApple OSS Distributions @result Pointer to the first DER object within the sequence otherwise NULL. 286*2c2f96dcSApple OSS Distributions */ 287*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3)) cc_ptrcheck_unavailable() 288*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_sequence_tl_strict(const uint8_t **body_end, const uint8_t *der, const uint8_t *der_end); 289*2c2f96dcSApple OSS Distributions 290*2c2f96dcSApple OSS Distributions /*! 291*2c2f96dcSApple OSS Distributions @function ccder_decode_uint_n 292*2c2f96dcSApple OSS Distributions @abstract length in cc_unit of a der unsigned integer after skipping the leading zeroes 293*2c2f96dcSApple OSS Distributions 294*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 295*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 296*2c2f96dcSApple OSS Distributions @param n Output the number of cc_unit required to represent the number 297*2c2f96dcSApple OSS Distributions 298*2c2f96dcSApple OSS Distributions @result First byte after the parsed integer or 299*2c2f96dcSApple OSS Distributions NULL if the integer is not valid (negative) or reach der_end when reading the integer 300*2c2f96dcSApple OSS Distributions */ 301*2c2f96dcSApple OSS Distributions CC_NONNULL((3)) cc_ptrcheck_unavailable() 302*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_uint_n(cc_size *n, const uint8_t *der, const uint8_t *der_end); 303*2c2f96dcSApple OSS Distributions 304*2c2f96dcSApple OSS Distributions /*! 305*2c2f96dcSApple OSS Distributions @function ccder_decode_uint 306*2c2f96dcSApple OSS Distributions @abstract Represent in cc_unit a ber unsigned integer after skipping the leading zeroes 307*2c2f96dcSApple OSS Distributions 308*2c2f96dcSApple OSS Distributions @param der Beginning of input BER buffer 309*2c2f96dcSApple OSS Distributions @param der_end End of input BER buffer 310*2c2f96dcSApple OSS Distributions @param n Number of cc_unit allocated for r 311*2c2f96dcSApple OSS Distributions @param r Allocated array of cc_unit to copy the integer into. 312*2c2f96dcSApple OSS Distributions 313*2c2f96dcSApple OSS Distributions @result First byte after the parsed integer or 314*2c2f96dcSApple OSS Distributions NULL if the integer is not valid (negative) 315*2c2f96dcSApple OSS Distributions reach der_end when reading the integer 316*2c2f96dcSApple OSS Distributions n cc_unit is not enough to represent the integer 317*2c2f96dcSApple OSS Distributions */ 318*2c2f96dcSApple OSS Distributions CC_NONNULL((4)) cc_ptrcheck_unavailable() 319*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_uint(cc_size n, cc_unit *r, const uint8_t *der, const uint8_t *der_end); 320*2c2f96dcSApple OSS Distributions 321*2c2f96dcSApple OSS Distributions /*! 322*2c2f96dcSApple OSS Distributions @function ccder_decode_uint_strict 323*2c2f96dcSApple OSS Distributions @abstract Represent in cc_unit a der unsigned integer after skipping the leading zeroes 324*2c2f96dcSApple OSS Distributions 325*2c2f96dcSApple OSS Distributions @param n Number of cc_unit allocated for r 326*2c2f96dcSApple OSS Distributions @param r Allocated array of cc_unit to copy the integer into. 327*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 328*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 329*2c2f96dcSApple OSS Distributions 330*2c2f96dcSApple OSS Distributions @result First byte after the parsed integer or NULL if the integer is not valid. 331*2c2f96dcSApple OSS Distributions */ 332*2c2f96dcSApple OSS Distributions CC_NONNULL((4)) cc_ptrcheck_unavailable() 333*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_uint_strict(cc_size n, cc_unit *r, const uint8_t *der, const uint8_t *der_end); 334*2c2f96dcSApple OSS Distributions 335*2c2f96dcSApple OSS Distributions CC_NONNULL((3)) cc_ptrcheck_unavailable() 336*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_uint64(uint64_t *r, const uint8_t *der, const uint8_t *der_end); 337*2c2f96dcSApple OSS Distributions 338*2c2f96dcSApple OSS Distributions /* Decode SEQUENCE { r, s -- (unsigned)integer } in ber into r and s. 339*2c2f96dcSApple OSS Distributions Returns NULL on decode errors, returns pointer just past the end of the 340*2c2f96dcSApple OSS Distributions sequence of integers otherwise. */ 341*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 3, 5)) cc_ptrcheck_unavailable() 342*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_seqii(cc_size n, cc_unit *r, cc_unit *s, const uint8_t *der, const uint8_t *der_end); 343*2c2f96dcSApple OSS Distributions 344*2c2f96dcSApple OSS Distributions /*! 345*2c2f96dcSApple OSS Distributions @function ccder_decode_seqii_strict 346*2c2f96dcSApple OSS Distributions @abstract Parse a DER sequence of two integers. 347*2c2f96dcSApple OSS Distributions 348*2c2f96dcSApple OSS Distributions @param n The maximum unit size of the integers. 349*2c2f96dcSApple OSS Distributions @param r First integer output 350*2c2f96dcSApple OSS Distributions @param s Second integer output 351*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 352*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 353*2c2f96dcSApple OSS Distributions 354*2c2f96dcSApple OSS Distributions @result Null on error, otherwise a pointer just past the end of the sequence buffer 355*2c2f96dcSApple OSS Distributions */ 356*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 3, 5)) cc_ptrcheck_unavailable() 357*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_seqii_strict(cc_size n, cc_unit *r, cc_unit *s, const uint8_t *der, const uint8_t *der_end); 358*2c2f96dcSApple OSS Distributions 359*2c2f96dcSApple OSS Distributions /*! 360*2c2f96dcSApple OSS Distributions @function ccder_decode_oid 361*2c2f96dcSApple OSS Distributions @abstract Parse a DER sequence representing an oid. 362*2c2f96dcSApple OSS Distributions 363*2c2f96dcSApple OSS Distributions @param oidp Pointer to OID 364*2c2f96dcSApple OSS Distributions @param der Beginning of input DER buffer 365*2c2f96dcSApple OSS Distributions @param der_end End of input DER buffer 366*2c2f96dcSApple OSS Distributions 367*2c2f96dcSApple OSS Distributions @result Null on error, otherwise a pointer just past the end of the sequence buffer. 368*2c2f96dcSApple OSS Distributions 369*2c2f96dcSApple OSS Distributions @warning In case of error, *oidp is set to NULL. 370*2c2f96dcSApple OSS Distributions Otherwise, *oidp is a pointer to a buffer of "unsigned char" of size >= 2. 371*2c2f96dcSApple OSS Distributions */ 372*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3)) cc_ptrcheck_unavailable() 373*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_oid(ccoid_t *oidp, const uint8_t *der, const uint8_t *der_end); 374*2c2f96dcSApple OSS Distributions 375*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 4)) cc_ptrcheck_unavailable() 376*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_bitstring(const uint8_t **bit_string, size_t *bit_length, const uint8_t *der, const uint8_t *der_end); 377*2c2f96dcSApple OSS Distributions 378*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3, 4, 5, 6, 7)) cc_ptrcheck_unavailable() 379*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_eckey(uint64_t *version, 380*2c2f96dcSApple OSS Distributions size_t *priv_size, 381*2c2f96dcSApple OSS Distributions const uint8_t **priv_key, 382*2c2f96dcSApple OSS Distributions ccoid_t *oid, 383*2c2f96dcSApple OSS Distributions size_t *pub_size, 384*2c2f96dcSApple OSS Distributions const uint8_t **pub_key, 385*2c2f96dcSApple OSS Distributions const uint8_t *der, 386*2c2f96dcSApple OSS Distributions const uint8_t *der_end); 387*2c2f96dcSApple OSS Distributions 388*2c2f96dcSApple OSS Distributions // MARK: - 389*2c2f96dcSApple OSS Distributions 390*2c2f96dcSApple OSS Distributions #define CC_EC_OID_SECP192R1 \ 391*2c2f96dcSApple OSS Distributions { \ 392*2c2f96dcSApple OSS Distributions ((unsigned char *)"\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x01") \ 393*2c2f96dcSApple OSS Distributions } 394*2c2f96dcSApple OSS Distributions #define CC_EC_OID_SECP256R1 \ 395*2c2f96dcSApple OSS Distributions { \ 396*2c2f96dcSApple OSS Distributions ((unsigned char *)"\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07") \ 397*2c2f96dcSApple OSS Distributions } 398*2c2f96dcSApple OSS Distributions #define CC_EC_OID_SECP224R1 \ 399*2c2f96dcSApple OSS Distributions { \ 400*2c2f96dcSApple OSS Distributions ((unsigned char *)"\x06\x05\x2B\x81\x04\x00\x21") \ 401*2c2f96dcSApple OSS Distributions } 402*2c2f96dcSApple OSS Distributions #define CC_EC_OID_SECP384R1 \ 403*2c2f96dcSApple OSS Distributions { \ 404*2c2f96dcSApple OSS Distributions ((unsigned char *)"\x06\x05\x2B\x81\x04\x00\x22") \ 405*2c2f96dcSApple OSS Distributions } 406*2c2f96dcSApple OSS Distributions #define CC_EC_OID_SECP521R1 \ 407*2c2f96dcSApple OSS Distributions { \ 408*2c2f96dcSApple OSS Distributions ((unsigned char *)"\x06\x05\x2B\x81\x04\x00\x23") \ 409*2c2f96dcSApple OSS Distributions } 410*2c2f96dcSApple OSS Distributions 411*2c2f96dcSApple OSS Distributions #endif /* _CORECRYPTO_CCDER_H_ */ 412