1*4882a593Smuzhiyun /** 2*4882a593Smuzhiyun * \file asn1.h 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * \brief Generic ASN.1 parsing 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7*4882a593Smuzhiyun * SPDX-License-Identifier: Apache-2.0 8*4882a593Smuzhiyun * 9*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License"); you may 10*4882a593Smuzhiyun * not use this file except in compliance with the License. 11*4882a593Smuzhiyun * You may obtain a copy of the License at 12*4882a593Smuzhiyun * 13*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0 14*4882a593Smuzhiyun * 15*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software 16*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17*4882a593Smuzhiyun * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18*4882a593Smuzhiyun * See the License for the specific language governing permissions and 19*4882a593Smuzhiyun * limitations under the License. 20*4882a593Smuzhiyun * 21*4882a593Smuzhiyun * This file is part of mbed TLS (https://tls.mbed.org) 22*4882a593Smuzhiyun */ 23*4882a593Smuzhiyun #ifndef MBEDTLS_ASN1_H 24*4882a593Smuzhiyun #define MBEDTLS_ASN1_H 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun #if !defined(MBEDTLS_CONFIG_FILE) 27*4882a593Smuzhiyun #include "config.h" 28*4882a593Smuzhiyun #else 29*4882a593Smuzhiyun #include MBEDTLS_CONFIG_FILE 30*4882a593Smuzhiyun #endif 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun #include <stddef.h> 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun #if defined(MBEDTLS_BIGNUM_C) 35*4882a593Smuzhiyun #include "bignum.h" 36*4882a593Smuzhiyun #endif 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun /** 39*4882a593Smuzhiyun * \addtogroup asn1_module 40*4882a593Smuzhiyun * \{ 41*4882a593Smuzhiyun */ 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /** 44*4882a593Smuzhiyun * \name ASN1 Error codes 45*4882a593Smuzhiyun * These error codes are OR'ed to X509 error codes for 46*4882a593Smuzhiyun * higher error granularity. 47*4882a593Smuzhiyun * ASN1 is a standard to specify data structures. 48*4882a593Smuzhiyun * \{ 49*4882a593Smuzhiyun */ 50*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */ 51*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */ 52*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */ 53*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */ 54*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */ 55*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_ALLOC_FAILED -0x006A /**< Memory allocation failed */ 56*4882a593Smuzhiyun #define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */ 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun /* \} name */ 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun /** 61*4882a593Smuzhiyun * \name DER constants 62*4882a593Smuzhiyun * These constants comply with DER encoded the ANS1 type tags. 63*4882a593Smuzhiyun * DER encoding uses hexadecimal representation. 64*4882a593Smuzhiyun * An example DER sequence is:\n 65*4882a593Smuzhiyun * - 0x02 -- tag indicating INTEGER 66*4882a593Smuzhiyun * - 0x01 -- length in octets 67*4882a593Smuzhiyun * - 0x05 -- value 68*4882a593Smuzhiyun * Such sequences are typically read into \c ::mbedtls_x509_buf. 69*4882a593Smuzhiyun * \{ 70*4882a593Smuzhiyun */ 71*4882a593Smuzhiyun #define MBEDTLS_ASN1_BOOLEAN 0x01 72*4882a593Smuzhiyun #define MBEDTLS_ASN1_INTEGER 0x02 73*4882a593Smuzhiyun #define MBEDTLS_ASN1_BIT_STRING 0x03 74*4882a593Smuzhiyun #define MBEDTLS_ASN1_OCTET_STRING 0x04 75*4882a593Smuzhiyun #define MBEDTLS_ASN1_NULL 0x05 76*4882a593Smuzhiyun #define MBEDTLS_ASN1_OID 0x06 77*4882a593Smuzhiyun #define MBEDTLS_ASN1_UTF8_STRING 0x0C 78*4882a593Smuzhiyun #define MBEDTLS_ASN1_SEQUENCE 0x10 79*4882a593Smuzhiyun #define MBEDTLS_ASN1_SET 0x11 80*4882a593Smuzhiyun #define MBEDTLS_ASN1_PRINTABLE_STRING 0x13 81*4882a593Smuzhiyun #define MBEDTLS_ASN1_T61_STRING 0x14 82*4882a593Smuzhiyun #define MBEDTLS_ASN1_IA5_STRING 0x16 83*4882a593Smuzhiyun #define MBEDTLS_ASN1_UTC_TIME 0x17 84*4882a593Smuzhiyun #define MBEDTLS_ASN1_GENERALIZED_TIME 0x18 85*4882a593Smuzhiyun #define MBEDTLS_ASN1_UNIVERSAL_STRING 0x1C 86*4882a593Smuzhiyun #define MBEDTLS_ASN1_BMP_STRING 0x1E 87*4882a593Smuzhiyun #define MBEDTLS_ASN1_PRIMITIVE 0x00 88*4882a593Smuzhiyun #define MBEDTLS_ASN1_CONSTRUCTED 0x20 89*4882a593Smuzhiyun #define MBEDTLS_ASN1_CONTEXT_SPECIFIC 0x80 90*4882a593Smuzhiyun /* \} name */ 91*4882a593Smuzhiyun /* \} addtogroup asn1_module */ 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun /** Returns the size of the binary string, without the trailing \\0 */ 94*4882a593Smuzhiyun #define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1) 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun /** 97*4882a593Smuzhiyun * Compares an mbedtls_asn1_buf structure to a reference OID. 98*4882a593Smuzhiyun * 99*4882a593Smuzhiyun * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a 100*4882a593Smuzhiyun * 'unsigned char *oid' here! 101*4882a593Smuzhiyun */ 102*4882a593Smuzhiyun #define MBEDTLS_OID_CMP(oid_str, oid_buf) \ 103*4882a593Smuzhiyun ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \ 104*4882a593Smuzhiyun memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 ) 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun #ifdef __cplusplus 107*4882a593Smuzhiyun extern "C" { 108*4882a593Smuzhiyun #endif 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun /** 111*4882a593Smuzhiyun * \name Functions to parse ASN.1 data structures 112*4882a593Smuzhiyun * \{ 113*4882a593Smuzhiyun */ 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun /** 116*4882a593Smuzhiyun * Type-length-value structure that allows for ASN1 using DER. 117*4882a593Smuzhiyun */ 118*4882a593Smuzhiyun typedef struct mbedtls_asn1_buf 119*4882a593Smuzhiyun { 120*4882a593Smuzhiyun int tag; /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */ 121*4882a593Smuzhiyun size_t len; /**< ASN1 length, in octets. */ 122*4882a593Smuzhiyun unsigned char *p; /**< ASN1 data, e.g. in ASCII. */ 123*4882a593Smuzhiyun } 124*4882a593Smuzhiyun mbedtls_asn1_buf; 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun /** 127*4882a593Smuzhiyun * Container for ASN1 bit strings. 128*4882a593Smuzhiyun */ 129*4882a593Smuzhiyun typedef struct mbedtls_asn1_bitstring 130*4882a593Smuzhiyun { 131*4882a593Smuzhiyun size_t len; /**< ASN1 length, in octets. */ 132*4882a593Smuzhiyun unsigned char unused_bits; /**< Number of unused bits at the end of the string */ 133*4882a593Smuzhiyun unsigned char *p; /**< Raw ASN1 data for the bit string */ 134*4882a593Smuzhiyun } 135*4882a593Smuzhiyun mbedtls_asn1_bitstring; 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun /** 138*4882a593Smuzhiyun * Container for a sequence of ASN.1 items 139*4882a593Smuzhiyun */ 140*4882a593Smuzhiyun typedef struct mbedtls_asn1_sequence 141*4882a593Smuzhiyun { 142*4882a593Smuzhiyun mbedtls_asn1_buf buf; /**< Buffer containing the given ASN.1 item. */ 143*4882a593Smuzhiyun struct mbedtls_asn1_sequence *next; /**< The next entry in the sequence. */ 144*4882a593Smuzhiyun } 145*4882a593Smuzhiyun mbedtls_asn1_sequence; 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun /** 148*4882a593Smuzhiyun * Container for a sequence or list of 'named' ASN.1 data items 149*4882a593Smuzhiyun */ 150*4882a593Smuzhiyun typedef struct mbedtls_asn1_named_data 151*4882a593Smuzhiyun { 152*4882a593Smuzhiyun mbedtls_asn1_buf oid; /**< The object identifier. */ 153*4882a593Smuzhiyun mbedtls_asn1_buf val; /**< The named value. */ 154*4882a593Smuzhiyun struct mbedtls_asn1_named_data *next; /**< The next entry in the sequence. */ 155*4882a593Smuzhiyun unsigned char next_merged; /**< Merge next item into the current one? */ 156*4882a593Smuzhiyun } 157*4882a593Smuzhiyun mbedtls_asn1_named_data; 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun /** 160*4882a593Smuzhiyun * \brief Get the length of an ASN.1 element. 161*4882a593Smuzhiyun * Updates the pointer to immediately behind the length. 162*4882a593Smuzhiyun * 163*4882a593Smuzhiyun * \param p The position in the ASN.1 data 164*4882a593Smuzhiyun * \param end End of data 165*4882a593Smuzhiyun * \param len The variable that will receive the value 166*4882a593Smuzhiyun * 167*4882a593Smuzhiyun * \return 0 if successful, MBEDTLS_ERR_ASN1_OUT_OF_DATA on reaching 168*4882a593Smuzhiyun * end of data, MBEDTLS_ERR_ASN1_INVALID_LENGTH if length is 169*4882a593Smuzhiyun * unparseable. 170*4882a593Smuzhiyun */ 171*4882a593Smuzhiyun int mbedtls_asn1_get_len( unsigned char **p, 172*4882a593Smuzhiyun const unsigned char *end, 173*4882a593Smuzhiyun size_t *len ); 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /** 176*4882a593Smuzhiyun * \brief Get the tag and length of the tag. Check for the requested tag. 177*4882a593Smuzhiyun * Updates the pointer to immediately behind the tag and length. 178*4882a593Smuzhiyun * 179*4882a593Smuzhiyun * \param p The position in the ASN.1 data 180*4882a593Smuzhiyun * \param end End of data 181*4882a593Smuzhiyun * \param len The variable that will receive the length 182*4882a593Smuzhiyun * \param tag The expected tag 183*4882a593Smuzhiyun * 184*4882a593Smuzhiyun * \return 0 if successful, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if tag did 185*4882a593Smuzhiyun * not match requested tag, or another specific ASN.1 error code. 186*4882a593Smuzhiyun */ 187*4882a593Smuzhiyun int mbedtls_asn1_get_tag( unsigned char **p, 188*4882a593Smuzhiyun const unsigned char *end, 189*4882a593Smuzhiyun size_t *len, int tag ); 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun /** 192*4882a593Smuzhiyun * \brief Retrieve a boolean ASN.1 tag and its value. 193*4882a593Smuzhiyun * Updates the pointer to immediately behind the full tag. 194*4882a593Smuzhiyun * 195*4882a593Smuzhiyun * \param p The position in the ASN.1 data 196*4882a593Smuzhiyun * \param end End of data 197*4882a593Smuzhiyun * \param val The variable that will receive the value 198*4882a593Smuzhiyun * 199*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 error code. 200*4882a593Smuzhiyun */ 201*4882a593Smuzhiyun int mbedtls_asn1_get_bool( unsigned char **p, 202*4882a593Smuzhiyun const unsigned char *end, 203*4882a593Smuzhiyun int *val ); 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun /** 206*4882a593Smuzhiyun * \brief Retrieve an integer ASN.1 tag and its value. 207*4882a593Smuzhiyun * Updates the pointer to immediately behind the full tag. 208*4882a593Smuzhiyun * 209*4882a593Smuzhiyun * \param p The position in the ASN.1 data 210*4882a593Smuzhiyun * \param end End of data 211*4882a593Smuzhiyun * \param val The variable that will receive the value 212*4882a593Smuzhiyun * 213*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 error code. 214*4882a593Smuzhiyun */ 215*4882a593Smuzhiyun int mbedtls_asn1_get_int( unsigned char **p, 216*4882a593Smuzhiyun const unsigned char *end, 217*4882a593Smuzhiyun int *val ); 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun /** 220*4882a593Smuzhiyun * \brief Retrieve a bitstring ASN.1 tag and its value. 221*4882a593Smuzhiyun * Updates the pointer to immediately behind the full tag. 222*4882a593Smuzhiyun * 223*4882a593Smuzhiyun * \param p The position in the ASN.1 data 224*4882a593Smuzhiyun * \param end End of data 225*4882a593Smuzhiyun * \param bs The variable that will receive the value 226*4882a593Smuzhiyun * 227*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 error code. 228*4882a593Smuzhiyun */ 229*4882a593Smuzhiyun int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end, 230*4882a593Smuzhiyun mbedtls_asn1_bitstring *bs); 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun /** 233*4882a593Smuzhiyun * \brief Retrieve a bitstring ASN.1 tag without unused bits and its 234*4882a593Smuzhiyun * value. 235*4882a593Smuzhiyun * Updates the pointer to the beginning of the bit/octet string. 236*4882a593Smuzhiyun * 237*4882a593Smuzhiyun * \param p The position in the ASN.1 data 238*4882a593Smuzhiyun * \param end End of data 239*4882a593Smuzhiyun * \param len Length of the actual bit/octect string in bytes 240*4882a593Smuzhiyun * 241*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 error code. 242*4882a593Smuzhiyun */ 243*4882a593Smuzhiyun int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end, 244*4882a593Smuzhiyun size_t *len ); 245*4882a593Smuzhiyun 246*4882a593Smuzhiyun /** 247*4882a593Smuzhiyun * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>" 248*4882a593Smuzhiyun * Updated the pointer to immediately behind the full sequence tag. 249*4882a593Smuzhiyun * 250*4882a593Smuzhiyun * \param p The position in the ASN.1 data 251*4882a593Smuzhiyun * \param end End of data 252*4882a593Smuzhiyun * \param cur First variable in the chain to fill 253*4882a593Smuzhiyun * \param tag Type of sequence 254*4882a593Smuzhiyun * 255*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 error code. 256*4882a593Smuzhiyun */ 257*4882a593Smuzhiyun int mbedtls_asn1_get_sequence_of( unsigned char **p, 258*4882a593Smuzhiyun const unsigned char *end, 259*4882a593Smuzhiyun mbedtls_asn1_sequence *cur, 260*4882a593Smuzhiyun int tag); 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun #if defined(MBEDTLS_BIGNUM_C) 263*4882a593Smuzhiyun /** 264*4882a593Smuzhiyun * \brief Retrieve a MPI value from an integer ASN.1 tag. 265*4882a593Smuzhiyun * Updates the pointer to immediately behind the full tag. 266*4882a593Smuzhiyun * 267*4882a593Smuzhiyun * \param p The position in the ASN.1 data 268*4882a593Smuzhiyun * \param end End of data 269*4882a593Smuzhiyun * \param X The MPI that will receive the value 270*4882a593Smuzhiyun * 271*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 or MPI error code. 272*4882a593Smuzhiyun */ 273*4882a593Smuzhiyun int mbedtls_asn1_get_mpi( unsigned char **p, 274*4882a593Smuzhiyun const unsigned char *end, 275*4882a593Smuzhiyun mbedtls_mpi *X ); 276*4882a593Smuzhiyun #endif /* MBEDTLS_BIGNUM_C */ 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun /** 279*4882a593Smuzhiyun * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence. 280*4882a593Smuzhiyun * Updates the pointer to immediately behind the full 281*4882a593Smuzhiyun * AlgorithmIdentifier. 282*4882a593Smuzhiyun * 283*4882a593Smuzhiyun * \param p The position in the ASN.1 data 284*4882a593Smuzhiyun * \param end End of data 285*4882a593Smuzhiyun * \param alg The buffer to receive the OID 286*4882a593Smuzhiyun * \param params The buffer to receive the params (if any) 287*4882a593Smuzhiyun * 288*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 or MPI error code. 289*4882a593Smuzhiyun */ 290*4882a593Smuzhiyun int mbedtls_asn1_get_alg( unsigned char **p, 291*4882a593Smuzhiyun const unsigned char *end, 292*4882a593Smuzhiyun mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params ); 293*4882a593Smuzhiyun 294*4882a593Smuzhiyun /** 295*4882a593Smuzhiyun * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no 296*4882a593Smuzhiyun * params. 297*4882a593Smuzhiyun * Updates the pointer to immediately behind the full 298*4882a593Smuzhiyun * AlgorithmIdentifier. 299*4882a593Smuzhiyun * 300*4882a593Smuzhiyun * \param p The position in the ASN.1 data 301*4882a593Smuzhiyun * \param end End of data 302*4882a593Smuzhiyun * \param alg The buffer to receive the OID 303*4882a593Smuzhiyun * 304*4882a593Smuzhiyun * \return 0 if successful or a specific ASN.1 or MPI error code. 305*4882a593Smuzhiyun */ 306*4882a593Smuzhiyun int mbedtls_asn1_get_alg_null( unsigned char **p, 307*4882a593Smuzhiyun const unsigned char *end, 308*4882a593Smuzhiyun mbedtls_asn1_buf *alg ); 309*4882a593Smuzhiyun 310*4882a593Smuzhiyun /** 311*4882a593Smuzhiyun * \brief Find a specific named_data entry in a sequence or list based on 312*4882a593Smuzhiyun * the OID. 313*4882a593Smuzhiyun * 314*4882a593Smuzhiyun * \param list The list to seek through 315*4882a593Smuzhiyun * \param oid The OID to look for 316*4882a593Smuzhiyun * \param len Size of the OID 317*4882a593Smuzhiyun * 318*4882a593Smuzhiyun * \return NULL if not found, or a pointer to the existing entry. 319*4882a593Smuzhiyun */ 320*4882a593Smuzhiyun mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list, 321*4882a593Smuzhiyun const char *oid, size_t len ); 322*4882a593Smuzhiyun 323*4882a593Smuzhiyun /** 324*4882a593Smuzhiyun * \brief Free a mbedtls_asn1_named_data entry 325*4882a593Smuzhiyun * 326*4882a593Smuzhiyun * \param entry The named data entry to free 327*4882a593Smuzhiyun */ 328*4882a593Smuzhiyun void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry ); 329*4882a593Smuzhiyun 330*4882a593Smuzhiyun /** 331*4882a593Smuzhiyun * \brief Free all entries in a mbedtls_asn1_named_data list 332*4882a593Smuzhiyun * Head will be set to NULL 333*4882a593Smuzhiyun * 334*4882a593Smuzhiyun * \param head Pointer to the head of the list of named data entries to free 335*4882a593Smuzhiyun */ 336*4882a593Smuzhiyun void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head ); 337*4882a593Smuzhiyun 338*4882a593Smuzhiyun #ifdef __cplusplus 339*4882a593Smuzhiyun } 340*4882a593Smuzhiyun #endif 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun #endif /* asn1.h */ 343