blob: 01081ad163402d65adb1d2771bfb42f3ccccb434 [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é-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.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
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
34
Paul Bakkerefc30292011-11-10 14:43:23 +000035#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerccdb0282011-12-15 19:49:51 +000036#include "bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000037#endif
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!
Paul Bakkere5eae762013-08-26 12:05:14 +0200102 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200103#define OID_CMP(oid_str, oid_buf) \
Manuel Pégourié-Gonnard6e064372015-03-19 16:54:56 +0000104 ( ( OID_SIZE(oid_str) != (oid_buf)->len ) || \
105 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200106
Paul Bakkerefc30292011-11-10 14:43:23 +0000107#ifdef __cplusplus
108extern "C" {
109#endif
110
111/**
112 * \name Functions to parse ASN.1 data structures
113 * \{
114 */
115
116/**
117 * Type-length-value structure that allows for ASN1 using DER.
118 */
119typedef struct _asn1_buf
120{
121 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
122 size_t len; /**< ASN1 length, e.g. in octets. */
123 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
124}
125asn1_buf;
126
127/**
128 * Container for ASN1 bit strings.
129 */
130typedef struct _asn1_bitstring
131{
132 size_t len; /**< ASN1 length, e.g. in octets. */
133 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
134 unsigned char *p; /**< Raw ASN1 data for the bit string */
135}
136asn1_bitstring;
137
138/**
139 * Container for a sequence of ASN.1 items
140 */
141typedef struct _asn1_sequence
142{
143 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
144 struct _asn1_sequence *next; /**< The next entry in the sequence. */
145}
146asn1_sequence;
147
148/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200149 * Container for a sequence or list of 'named' ASN.1 data items
150 */
151typedef struct _asn1_named_data
152{
153 asn1_buf oid; /**< The object identifier. */
154 asn1_buf val; /**< The named value. */
155 struct _asn1_named_data *next; /**< The next entry in the sequence. */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000156 unsigned char next_merged; /**< Merge next item into the current one? */
Paul Bakkere5eae762013-08-26 12:05:14 +0200157}
158asn1_named_data;
159
160/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200161 * \brief Get the length of an ASN.1 element.
162 * Updates the pointer to immediately behind the length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000163 *
164 * \param p The position in the ASN.1 data
165 * \param end End of data
166 * \param len The variable that will receive the value
167 *
168 * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching
169 * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is
170 * unparseable.
171 */
172int asn1_get_len( unsigned char **p,
173 const unsigned char *end,
174 size_t *len );
175
176/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200177 * \brief Get the tag and length of the tag. Check for the requested tag.
178 * Updates the pointer to immediately behind the tag and length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000179 *
180 * \param p The position in the ASN.1 data
181 * \param end End of data
182 * \param len The variable that will receive the length
183 * \param tag The expected tag
184 *
185 * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did
186 * not match requested tag, or another specific ASN.1 error code.
187 */
188int asn1_get_tag( unsigned char **p,
189 const unsigned char *end,
190 size_t *len, int tag );
191
192/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200193 * \brief Retrieve a boolean ASN.1 tag and its value.
194 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000195 *
196 * \param p The position in the ASN.1 data
197 * \param end End of data
198 * \param val The variable that will receive the value
199 *
200 * \return 0 if successful or a specific ASN.1 error code.
201 */
202int asn1_get_bool( unsigned char **p,
203 const unsigned char *end,
204 int *val );
205
206/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200207 * \brief Retrieve an integer ASN.1 tag and its value.
208 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000209 *
210 * \param p The position in the ASN.1 data
211 * \param end End of data
212 * \param val The variable that will receive the value
213 *
214 * \return 0 if successful or a specific ASN.1 error code.
215 */
216int asn1_get_int( unsigned char **p,
217 const unsigned char *end,
218 int *val );
219
220/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200221 * \brief Retrieve a bitstring ASN.1 tag and its value.
222 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000223 *
224 * \param p The position in the ASN.1 data
225 * \param end End of data
226 * \param bs The variable that will receive the value
227 *
228 * \return 0 if successful or a specific ASN.1 error code.
229 */
230int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
231 asn1_bitstring *bs);
232
233/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200234 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
235 * value.
236 * Updates the pointer to the beginning of the bit/octet string.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200237 *
238 * \param p The position in the ASN.1 data
239 * \param end End of data
240 * \param len Length of the actual bit/octect string in bytes
241 *
242 * \return 0 if successful or a specific ASN.1 error code.
243 */
244int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
245 size_t *len );
246
247/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200248 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
249 * Updated the pointer to immediately behind the full sequence tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000250 *
251 * \param p The position in the ASN.1 data
252 * \param end End of data
253 * \param cur First variable in the chain to fill
Paul Bakker8b21f7a2012-01-13 13:29:05 +0000254 * \param tag Type of sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000255 *
256 * \return 0 if successful or a specific ASN.1 error code.
257 */
258int asn1_get_sequence_of( unsigned char **p,
259 const unsigned char *end,
260 asn1_sequence *cur,
261 int tag);
262
263#if defined(POLARSSL_BIGNUM_C)
264/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200265 * \brief Retrieve a MPI value from an integer ASN.1 tag.
266 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000267 *
268 * \param p The position in the ASN.1 data
269 * \param end End of data
270 * \param X The MPI that will receive the value
271 *
272 * \return 0 if successful or a specific ASN.1 or MPI error code.
273 */
274int asn1_get_mpi( unsigned char **p,
275 const unsigned char *end,
276 mpi *X );
Paul Bakker9af723c2014-05-01 13:03:14 +0200277#endif /* POLARSSL_BIGNUM_C */
Paul Bakkerefc30292011-11-10 14:43:23 +0000278
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200279/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200280 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
281 * Updates the pointer to immediately behind the full
282 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200283 *
284 * \param p The position in the ASN.1 data
285 * \param end End of data
286 * \param alg The buffer to receive the OID
287 * \param params The buffer to receive the params (if any)
288 *
289 * \return 0 if successful or a specific ASN.1 or MPI error code.
290 */
291int asn1_get_alg( unsigned char **p,
292 const unsigned char *end,
293 asn1_buf *alg, asn1_buf *params );
294
295/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200296 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
297 * params.
298 * Updates the pointer to immediately behind the full
299 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200300 *
301 * \param p The position in the ASN.1 data
302 * \param end End of data
303 * \param alg The buffer to receive the OID
304 *
305 * \return 0 if successful or a specific ASN.1 or MPI error code.
306 */
307int asn1_get_alg_null( unsigned char **p,
308 const unsigned char *end,
309 asn1_buf *alg );
310
Paul Bakkere5eae762013-08-26 12:05:14 +0200311/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200312 * \brief Find a specific named_data entry in a sequence or list based on
313 * the OID.
Paul Bakkere5eae762013-08-26 12:05:14 +0200314 *
315 * \param list The list to seek through
316 * \param oid The OID to look for
317 * \param len Size of the OID
318 *
319 * \return NULL if not found, or a pointer to the existing entry.
320 */
321asn1_named_data *asn1_find_named_data( asn1_named_data *list,
322 const char *oid, size_t len );
323
324/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200325 * \brief Free a asn1_named_data entry
Paul Bakkere5eae762013-08-26 12:05:14 +0200326 *
327 * \param entry The named data entry to free
328 */
329void asn1_free_named_data( asn1_named_data *entry );
330
Paul Bakkerc547cc92013-09-09 12:01:23 +0200331/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200332 * \brief Free all entries in a asn1_named_data list
333 * Head will be set to NULL
Paul Bakkerc547cc92013-09-09 12:01:23 +0200334 *
335 * \param head Pointer to the head of the list of named data entries to free
336 */
337void asn1_free_named_data_list( asn1_named_data **head );
338
Paul Bakkerefc30292011-11-10 14:43:23 +0000339#ifdef __cplusplus
340}
341#endif
342
343#endif /* asn1.h */