blob: 14171f74f7e61b52f4591963cabf45b6e963fe26 [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/**
2 * \file asn1.h
3 *
4 * \brief Generic ASN.1 parsing
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerefc30292011-11-10 14:43:23 +00007 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerefc30292011-11-10 14:43:23 +00009 *
Paul Bakkerefc30292011-11-10 14:43:23 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_ASN1_H
25#define POLARSSL_ASN1_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerccdb0282011-12-15 19:49:51 +000028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakkerefc30292011-11-10 14:43:23 +000032
33#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerccdb0282011-12-15 19:49:51 +000034#include "bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000035#endif
36
37#include <string.h>
38
Paul Bakker9af723c2014-05-01 13:03:14 +020039/**
Paul Bakkerefc30292011-11-10 14:43:23 +000040 * \addtogroup asn1_module
Paul Bakker9af723c2014-05-01 13:03:14 +020041 * \{
Paul Bakkerefc30292011-11-10 14:43:23 +000042 */
Paul Bakker9af723c2014-05-01 13:03:14 +020043
Paul Bakkerefc30292011-11-10 14:43:23 +000044/**
45 * \name ASN1 Error codes
46 * These error codes are OR'ed to X509 error codes for
Paul Bakker9af723c2014-05-01 13:03:14 +020047 * higher error granularity.
Paul Bakkerefc30292011-11-10 14:43:23 +000048 * ASN1 is a standard to specify data structures.
49 * \{
50 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051#define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */
52#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */
53#define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */
54#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */
55#define POLARSSL_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */
56#define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x006A /**< Memory allocation failed */
Paul Bakker05888152012-02-16 10:26:57 +000057#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
58
Paul Bakkerefc30292011-11-10 14:43:23 +000059/* \} name */
60
61/**
62 * \name DER constants
63 * These constants comply with DER encoded the ANS1 type tags.
64 * DER encoding uses hexadecimal representation.
65 * An example DER sequence is:\n
66 * - 0x02 -- tag indicating INTEGER
67 * - 0x01 -- length in octets
68 * - 0x05 -- value
69 * Such sequences are typically read into \c ::x509_buf.
70 * \{
71 */
72#define ASN1_BOOLEAN 0x01
73#define ASN1_INTEGER 0x02
74#define ASN1_BIT_STRING 0x03
75#define ASN1_OCTET_STRING 0x04
76#define ASN1_NULL 0x05
77#define ASN1_OID 0x06
78#define ASN1_UTF8_STRING 0x0C
79#define ASN1_SEQUENCE 0x10
80#define ASN1_SET 0x11
81#define ASN1_PRINTABLE_STRING 0x13
82#define ASN1_T61_STRING 0x14
83#define ASN1_IA5_STRING 0x16
84#define ASN1_UTC_TIME 0x17
85#define ASN1_GENERALIZED_TIME 0x18
86#define ASN1_UNIVERSAL_STRING 0x1C
87#define ASN1_BMP_STRING 0x1E
88#define ASN1_PRIMITIVE 0x00
89#define ASN1_CONSTRUCTED 0x20
90#define ASN1_CONTEXT_SPECIFIC 0x80
91/* \} name */
92/* \} addtogroup asn1_module */
93
94/** Returns the size of the binary string, without the trailing \\0 */
95#define OID_SIZE(x) (sizeof(x) - 1)
96
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +010097/**
98 * Compares an asn1_buf structure to a reference OID.
99 *
100 * Only works for 'defined' oid_str values (OID_HMAC_SHA1), you cannot use a
101 * 'unsigned char *oid' here!
102 *
103 * Warning: returns true when the OIDs are equal (unlike memcmp)!
Paul Bakkere5eae762013-08-26 12:05:14 +0200104 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200105#define OID_CMP(oid_str, oid_buf) \
106 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
107 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0 )
108
Paul Bakkerefc30292011-11-10 14:43:23 +0000109#ifdef __cplusplus
110extern "C" {
111#endif
112
113/**
114 * \name Functions to parse ASN.1 data structures
115 * \{
116 */
117
118/**
119 * Type-length-value structure that allows for ASN1 using DER.
120 */
121typedef struct _asn1_buf
122{
123 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
124 size_t len; /**< ASN1 length, e.g. in octets. */
125 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
126}
127asn1_buf;
128
129/**
130 * Container for ASN1 bit strings.
131 */
132typedef struct _asn1_bitstring
133{
134 size_t len; /**< ASN1 length, e.g. in octets. */
135 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
136 unsigned char *p; /**< Raw ASN1 data for the bit string */
137}
138asn1_bitstring;
139
140/**
141 * Container for a sequence of ASN.1 items
142 */
143typedef struct _asn1_sequence
144{
145 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
146 struct _asn1_sequence *next; /**< The next entry in the sequence. */
147}
148asn1_sequence;
149
150/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200151 * Container for a sequence or list of 'named' ASN.1 data items
152 */
153typedef struct _asn1_named_data
154{
155 asn1_buf oid; /**< The object identifier. */
156 asn1_buf val; /**< The named value. */
157 struct _asn1_named_data *next; /**< The next entry in the sequence. */
158}
159asn1_named_data;
160
161/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200162 * \brief Get the length of an ASN.1 element.
163 * Updates the pointer to immediately behind the length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000164 *
165 * \param p The position in the ASN.1 data
166 * \param end End of data
167 * \param len The variable that will receive the value
168 *
169 * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching
170 * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is
171 * unparseable.
172 */
173int asn1_get_len( unsigned char **p,
174 const unsigned char *end,
175 size_t *len );
176
177/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200178 * \brief Get the tag and length of the tag. Check for the requested tag.
179 * Updates the pointer to immediately behind the tag and length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000180 *
181 * \param p The position in the ASN.1 data
182 * \param end End of data
183 * \param len The variable that will receive the length
184 * \param tag The expected tag
185 *
186 * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did
187 * not match requested tag, or another specific ASN.1 error code.
188 */
189int asn1_get_tag( unsigned char **p,
190 const unsigned char *end,
191 size_t *len, int tag );
192
193/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200194 * \brief Retrieve a boolean ASN.1 tag and its value.
195 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000196 *
197 * \param p The position in the ASN.1 data
198 * \param end End of data
199 * \param val The variable that will receive the value
200 *
201 * \return 0 if successful or a specific ASN.1 error code.
202 */
203int asn1_get_bool( unsigned char **p,
204 const unsigned char *end,
205 int *val );
206
207/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200208 * \brief Retrieve an integer ASN.1 tag and its value.
209 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000210 *
211 * \param p The position in the ASN.1 data
212 * \param end End of data
213 * \param val The variable that will receive the value
214 *
215 * \return 0 if successful or a specific ASN.1 error code.
216 */
217int asn1_get_int( unsigned char **p,
218 const unsigned char *end,
219 int *val );
220
221/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200222 * \brief Retrieve a bitstring ASN.1 tag and its value.
223 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000224 *
225 * \param p The position in the ASN.1 data
226 * \param end End of data
227 * \param bs The variable that will receive the value
228 *
229 * \return 0 if successful or a specific ASN.1 error code.
230 */
231int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
232 asn1_bitstring *bs);
233
234/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200235 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
236 * value.
237 * Updates the pointer to the beginning of the bit/octet string.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200238 *
239 * \param p The position in the ASN.1 data
240 * \param end End of data
241 * \param len Length of the actual bit/octect string in bytes
242 *
243 * \return 0 if successful or a specific ASN.1 error code.
244 */
245int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
246 size_t *len );
247
248/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200249 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
250 * Updated the pointer to immediately behind the full sequence tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000251 *
252 * \param p The position in the ASN.1 data
253 * \param end End of data
254 * \param cur First variable in the chain to fill
Paul Bakker8b21f7a2012-01-13 13:29:05 +0000255 * \param tag Type of sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000256 *
257 * \return 0 if successful or a specific ASN.1 error code.
258 */
259int asn1_get_sequence_of( unsigned char **p,
260 const unsigned char *end,
261 asn1_sequence *cur,
262 int tag);
263
264#if defined(POLARSSL_BIGNUM_C)
265/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200266 * \brief Retrieve a MPI value from an integer ASN.1 tag.
267 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000268 *
269 * \param p The position in the ASN.1 data
270 * \param end End of data
271 * \param X The MPI that will receive the value
272 *
273 * \return 0 if successful or a specific ASN.1 or MPI error code.
274 */
275int asn1_get_mpi( unsigned char **p,
276 const unsigned char *end,
277 mpi *X );
Paul Bakker9af723c2014-05-01 13:03:14 +0200278#endif /* POLARSSL_BIGNUM_C */
Paul Bakkerefc30292011-11-10 14:43:23 +0000279
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200280/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200281 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
282 * Updates the pointer to immediately behind the full
283 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200284 *
285 * \param p The position in the ASN.1 data
286 * \param end End of data
287 * \param alg The buffer to receive the OID
288 * \param params The buffer to receive the params (if any)
289 *
290 * \return 0 if successful or a specific ASN.1 or MPI error code.
291 */
292int asn1_get_alg( unsigned char **p,
293 const unsigned char *end,
294 asn1_buf *alg, asn1_buf *params );
295
296/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200297 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
298 * params.
299 * Updates the pointer to immediately behind the full
300 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200301 *
302 * \param p The position in the ASN.1 data
303 * \param end End of data
304 * \param alg The buffer to receive the OID
305 *
306 * \return 0 if successful or a specific ASN.1 or MPI error code.
307 */
308int asn1_get_alg_null( unsigned char **p,
309 const unsigned char *end,
310 asn1_buf *alg );
311
Paul Bakkere5eae762013-08-26 12:05:14 +0200312/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200313 * \brief Find a specific named_data entry in a sequence or list based on
314 * the OID.
Paul Bakkere5eae762013-08-26 12:05:14 +0200315 *
316 * \param list The list to seek through
317 * \param oid The OID to look for
318 * \param len Size of the OID
319 *
320 * \return NULL if not found, or a pointer to the existing entry.
321 */
322asn1_named_data *asn1_find_named_data( asn1_named_data *list,
323 const char *oid, size_t len );
324
325/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200326 * \brief Free a asn1_named_data entry
Paul Bakkere5eae762013-08-26 12:05:14 +0200327 *
328 * \param entry The named data entry to free
329 */
330void asn1_free_named_data( asn1_named_data *entry );
331
Paul Bakkerc547cc92013-09-09 12:01:23 +0200332/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200333 * \brief Free all entries in a asn1_named_data list
334 * Head will be set to NULL
Paul Bakkerc547cc92013-09-09 12:01:23 +0200335 *
336 * \param head Pointer to the head of the list of named data entries to free
337 */
338void asn1_free_named_data_list( asn1_named_data **head );
339
Paul Bakkerefc30292011-11-10 14:43:23 +0000340#ifdef __cplusplus
341}
342#endif
343
344#endif /* asn1.h */