Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file asn1.h |
| 3 | * |
| 4 | * \brief Generic ASN.1 parsing |
| 5 | * |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_ASN1_H |
| 28 | #define POLARSSL_ASN1_H |
| 29 | |
Paul Bakker | ccdb028 | 2011-12-15 19:49:51 +0000 | [diff] [blame] | 30 | #include "config.h" |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 31 | |
| 32 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | ccdb028 | 2011-12-15 19:49:51 +0000 | [diff] [blame] | 33 | #include "bignum.h" |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | /** |
| 39 | * \addtogroup asn1_module |
| 40 | * \{ |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * \name ASN1 Error codes |
| 45 | * These error codes are OR'ed to X509 error codes for |
| 46 | * higher error granularity. |
| 47 | * ASN1 is a standard to specify data structures. |
| 48 | * \{ |
| 49 | */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 50 | #define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */ |
| 51 | #define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */ |
| 52 | #define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */ |
| 53 | #define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */ |
| 54 | #define POLARSSL_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */ |
| 55 | #define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x006A /**< Memory allocation failed */ |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 56 | #define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */ |
| 57 | |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 58 | /* \} name */ |
| 59 | |
| 60 | /** |
| 61 | * \name DER constants |
| 62 | * These constants comply with DER encoded the ANS1 type tags. |
| 63 | * DER encoding uses hexadecimal representation. |
| 64 | * An example DER sequence is:\n |
| 65 | * - 0x02 -- tag indicating INTEGER |
| 66 | * - 0x01 -- length in octets |
| 67 | * - 0x05 -- value |
| 68 | * Such sequences are typically read into \c ::x509_buf. |
| 69 | * \{ |
| 70 | */ |
| 71 | #define ASN1_BOOLEAN 0x01 |
| 72 | #define ASN1_INTEGER 0x02 |
| 73 | #define ASN1_BIT_STRING 0x03 |
| 74 | #define ASN1_OCTET_STRING 0x04 |
| 75 | #define ASN1_NULL 0x05 |
| 76 | #define ASN1_OID 0x06 |
| 77 | #define ASN1_UTF8_STRING 0x0C |
| 78 | #define ASN1_SEQUENCE 0x10 |
| 79 | #define ASN1_SET 0x11 |
| 80 | #define ASN1_PRINTABLE_STRING 0x13 |
| 81 | #define ASN1_T61_STRING 0x14 |
| 82 | #define ASN1_IA5_STRING 0x16 |
| 83 | #define ASN1_UTC_TIME 0x17 |
| 84 | #define ASN1_GENERALIZED_TIME 0x18 |
| 85 | #define ASN1_UNIVERSAL_STRING 0x1C |
| 86 | #define ASN1_BMP_STRING 0x1E |
| 87 | #define ASN1_PRIMITIVE 0x00 |
| 88 | #define ASN1_CONSTRUCTED 0x20 |
| 89 | #define ASN1_CONTEXT_SPECIFIC 0x80 |
| 90 | /* \} name */ |
| 91 | /* \} addtogroup asn1_module */ |
| 92 | |
| 93 | /** Returns the size of the binary string, without the trailing \\0 */ |
| 94 | #define OID_SIZE(x) (sizeof(x) - 1) |
| 95 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 96 | /** Compares two asn1_buf structures for the same OID */ |
| 97 | #define OID_CMP(oid_str, oid_buf) \ |
| 98 | ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \ |
| 99 | memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0 ) |
| 100 | |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 101 | #ifdef __cplusplus |
| 102 | extern "C" { |
| 103 | #endif |
| 104 | |
| 105 | /** |
| 106 | * \name Functions to parse ASN.1 data structures |
| 107 | * \{ |
| 108 | */ |
| 109 | |
| 110 | /** |
| 111 | * Type-length-value structure that allows for ASN1 using DER. |
| 112 | */ |
| 113 | typedef struct _asn1_buf |
| 114 | { |
| 115 | int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */ |
| 116 | size_t len; /**< ASN1 length, e.g. in octets. */ |
| 117 | unsigned char *p; /**< ASN1 data, e.g. in ASCII. */ |
| 118 | } |
| 119 | asn1_buf; |
| 120 | |
| 121 | /** |
| 122 | * Container for ASN1 bit strings. |
| 123 | */ |
| 124 | typedef struct _asn1_bitstring |
| 125 | { |
| 126 | size_t len; /**< ASN1 length, e.g. in octets. */ |
| 127 | unsigned char unused_bits; /**< Number of unused bits at the end of the string */ |
| 128 | unsigned char *p; /**< Raw ASN1 data for the bit string */ |
| 129 | } |
| 130 | asn1_bitstring; |
| 131 | |
| 132 | /** |
| 133 | * Container for a sequence of ASN.1 items |
| 134 | */ |
| 135 | typedef struct _asn1_sequence |
| 136 | { |
| 137 | asn1_buf buf; /**< Buffer containing the given ASN.1 item. */ |
| 138 | struct _asn1_sequence *next; /**< The next entry in the sequence. */ |
| 139 | } |
| 140 | asn1_sequence; |
| 141 | |
| 142 | /** |
| 143 | * Get the length of an ASN.1 element. |
| 144 | * Updates the pointer to immediately behind the length. |
| 145 | * |
| 146 | * \param p The position in the ASN.1 data |
| 147 | * \param end End of data |
| 148 | * \param len The variable that will receive the value |
| 149 | * |
| 150 | * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching |
| 151 | * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is |
| 152 | * unparseable. |
| 153 | */ |
| 154 | int asn1_get_len( unsigned char **p, |
| 155 | const unsigned char *end, |
| 156 | size_t *len ); |
| 157 | |
| 158 | /** |
| 159 | * Get the tag and length of the tag. Check for the requested tag. |
| 160 | * Updates the pointer to immediately behind the tag and length. |
| 161 | * |
| 162 | * \param p The position in the ASN.1 data |
| 163 | * \param end End of data |
| 164 | * \param len The variable that will receive the length |
| 165 | * \param tag The expected tag |
| 166 | * |
| 167 | * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did |
| 168 | * not match requested tag, or another specific ASN.1 error code. |
| 169 | */ |
| 170 | int asn1_get_tag( unsigned char **p, |
| 171 | const unsigned char *end, |
| 172 | size_t *len, int tag ); |
| 173 | |
| 174 | /** |
| 175 | * Retrieve a boolean ASN.1 tag and its value. |
| 176 | * Updates the pointer to immediately behind the full tag. |
| 177 | * |
| 178 | * \param p The position in the ASN.1 data |
| 179 | * \param end End of data |
| 180 | * \param val The variable that will receive the value |
| 181 | * |
| 182 | * \return 0 if successful or a specific ASN.1 error code. |
| 183 | */ |
| 184 | int asn1_get_bool( unsigned char **p, |
| 185 | const unsigned char *end, |
| 186 | int *val ); |
| 187 | |
| 188 | /** |
| 189 | * Retrieve an integer ASN.1 tag and its value. |
| 190 | * Updates the pointer to immediately behind the full tag. |
| 191 | * |
| 192 | * \param p The position in the ASN.1 data |
| 193 | * \param end End of data |
| 194 | * \param val The variable that will receive the value |
| 195 | * |
| 196 | * \return 0 if successful or a specific ASN.1 error code. |
| 197 | */ |
| 198 | int asn1_get_int( unsigned char **p, |
| 199 | const unsigned char *end, |
| 200 | int *val ); |
| 201 | |
| 202 | /** |
| 203 | * Retrieve a bitstring ASN.1 tag and its value. |
| 204 | * Updates the pointer to immediately behind the full tag. |
| 205 | * |
| 206 | * \param p The position in the ASN.1 data |
| 207 | * \param end End of data |
| 208 | * \param bs The variable that will receive the value |
| 209 | * |
| 210 | * \return 0 if successful or a specific ASN.1 error code. |
| 211 | */ |
| 212 | int asn1_get_bitstring( unsigned char **p, const unsigned char *end, |
| 213 | asn1_bitstring *bs); |
| 214 | |
| 215 | /** |
| 216 | * Parses and splits an ASN.1 "SEQUENCE OF <tag>" |
| 217 | * Updated the pointer to immediately behind the full sequence tag. |
| 218 | * |
| 219 | * \param p The position in the ASN.1 data |
| 220 | * \param end End of data |
| 221 | * \param cur First variable in the chain to fill |
Paul Bakker | 8b21f7a | 2012-01-13 13:29:05 +0000 | [diff] [blame] | 222 | * \param tag Type of sequence |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 223 | * |
| 224 | * \return 0 if successful or a specific ASN.1 error code. |
| 225 | */ |
| 226 | int asn1_get_sequence_of( unsigned char **p, |
| 227 | const unsigned char *end, |
| 228 | asn1_sequence *cur, |
| 229 | int tag); |
| 230 | |
| 231 | #if defined(POLARSSL_BIGNUM_C) |
| 232 | /** |
| 233 | * Retrieve a MPI value from an integer ASN.1 tag. |
| 234 | * Updates the pointer to immediately behind the full tag. |
| 235 | * |
| 236 | * \param p The position in the ASN.1 data |
| 237 | * \param end End of data |
| 238 | * \param X The MPI that will receive the value |
| 239 | * |
| 240 | * \return 0 if successful or a specific ASN.1 or MPI error code. |
| 241 | */ |
| 242 | int asn1_get_mpi( unsigned char **p, |
| 243 | const unsigned char *end, |
| 244 | mpi *X ); |
| 245 | #endif |
| 246 | |
| 247 | #ifdef __cplusplus |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | #endif /* asn1.h */ |