blob: 517cd5b1144352299aefa9e53c707898058351a5 [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/**
2 * \file asn1.h
3 *
4 * \brief Generic ASN.1 parsing
5 *
Paul Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerefc30292011-11-10 14:43:23 +00007 *
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerccdb0282011-12-15 19:49:51 +000031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakkerefc30292011-11-10 14:43:23 +000035
36#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerccdb0282011-12-15 19:49:51 +000037#include "bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000038#endif
39
40#include <string.h>
41
42/**
43 * \addtogroup asn1_module
44 * \{
45 */
46
47/**
48 * \name ASN1 Error codes
49 * These error codes are OR'ed to X509 error codes for
50 * higher error granularity.
51 * ASN1 is a standard to specify data structures.
52 * \{
53 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054#define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */
55#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */
56#define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */
57#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */
58#define POLARSSL_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */
59#define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x006A /**< Memory allocation failed */
Paul Bakker05888152012-02-16 10:26:57 +000060#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
61
Paul Bakkerefc30292011-11-10 14:43:23 +000062/* \} name */
63
64/**
65 * \name DER constants
66 * These constants comply with DER encoded the ANS1 type tags.
67 * DER encoding uses hexadecimal representation.
68 * An example DER sequence is:\n
69 * - 0x02 -- tag indicating INTEGER
70 * - 0x01 -- length in octets
71 * - 0x05 -- value
72 * Such sequences are typically read into \c ::x509_buf.
73 * \{
74 */
75#define ASN1_BOOLEAN 0x01
76#define ASN1_INTEGER 0x02
77#define ASN1_BIT_STRING 0x03
78#define ASN1_OCTET_STRING 0x04
79#define ASN1_NULL 0x05
80#define ASN1_OID 0x06
81#define ASN1_UTF8_STRING 0x0C
82#define ASN1_SEQUENCE 0x10
83#define ASN1_SET 0x11
84#define ASN1_PRINTABLE_STRING 0x13
85#define ASN1_T61_STRING 0x14
86#define ASN1_IA5_STRING 0x16
87#define ASN1_UTC_TIME 0x17
88#define ASN1_GENERALIZED_TIME 0x18
89#define ASN1_UNIVERSAL_STRING 0x1C
90#define ASN1_BMP_STRING 0x1E
91#define ASN1_PRIMITIVE 0x00
92#define ASN1_CONSTRUCTED 0x20
93#define ASN1_CONTEXT_SPECIFIC 0x80
94/* \} name */
95/* \} addtogroup asn1_module */
96
97/** Returns the size of the binary string, without the trailing \\0 */
98#define OID_SIZE(x) (sizeof(x) - 1)
99
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100100/** Compares two asn1_buf structures for the same OID. Only works for
101 * 'defined' oid_str values (OID_HMAC_SHA1), you cannot use a 'unsigned
102 * char *oid' here!
Paul Bakkere5eae762013-08-26 12:05:14 +0200103 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200104#define OID_CMP(oid_str, oid_buf) \
105 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
106 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0 )
107
Paul Bakkerefc30292011-11-10 14:43:23 +0000108#ifdef __cplusplus
109extern "C" {
110#endif
111
112/**
113 * \name Functions to parse ASN.1 data structures
114 * \{
115 */
116
117/**
118 * Type-length-value structure that allows for ASN1 using DER.
119 */
120typedef struct _asn1_buf
121{
122 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
123 size_t len; /**< ASN1 length, e.g. in octets. */
124 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
125}
126asn1_buf;
127
128/**
129 * Container for ASN1 bit strings.
130 */
131typedef struct _asn1_bitstring
132{
133 size_t len; /**< ASN1 length, e.g. in octets. */
134 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
135 unsigned char *p; /**< Raw ASN1 data for the bit string */
136}
137asn1_bitstring;
138
139/**
140 * Container for a sequence of ASN.1 items
141 */
142typedef struct _asn1_sequence
143{
144 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
145 struct _asn1_sequence *next; /**< The next entry in the sequence. */
146}
147asn1_sequence;
148
149/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200150 * Container for a sequence or list of 'named' ASN.1 data items
151 */
152typedef struct _asn1_named_data
153{
154 asn1_buf oid; /**< The object identifier. */
155 asn1_buf val; /**< The named value. */
156 struct _asn1_named_data *next; /**< The next entry in the sequence. */
157}
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 );
277#endif
278
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 */