blob: eacdd082bde59ffe1f376caac1cb1c3d9820d5b5 [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
Paul Bakker9af723c2014-05-01 13:03:14 +020042/**
Paul Bakkerefc30292011-11-10 14:43:23 +000043 * \addtogroup asn1_module
Paul Bakker9af723c2014-05-01 13:03:14 +020044 * \{
Paul Bakkerefc30292011-11-10 14:43:23 +000045 */
Paul Bakker9af723c2014-05-01 13:03:14 +020046
Paul Bakkerefc30292011-11-10 14:43:23 +000047/**
48 * \name ASN1 Error codes
49 * These error codes are OR'ed to X509 error codes for
Paul Bakker9af723c2014-05-01 13:03:14 +020050 * higher error granularity.
Paul Bakkerefc30292011-11-10 14:43:23 +000051 * 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é-Gonnarde76b7502014-01-23 19:15:29 +0100100/**
101 * Compares an asn1_buf structure to a reference OID.
102 *
103 * Only works for 'defined' oid_str values (OID_HMAC_SHA1), you cannot use a
104 * 'unsigned char *oid' here!
105 *
106 * Warning: returns true when the OIDs are equal (unlike memcmp)!
Paul Bakkere5eae762013-08-26 12:05:14 +0200107 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200108#define OID_CMP(oid_str, oid_buf) \
109 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
110 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0 )
111
Paul Bakkerefc30292011-11-10 14:43:23 +0000112#ifdef __cplusplus
113extern "C" {
114#endif
115
116/**
117 * \name Functions to parse ASN.1 data structures
118 * \{
119 */
120
121/**
122 * Type-length-value structure that allows for ASN1 using DER.
123 */
124typedef struct _asn1_buf
125{
126 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
127 size_t len; /**< ASN1 length, e.g. in octets. */
128 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
129}
130asn1_buf;
131
132/**
133 * Container for ASN1 bit strings.
134 */
135typedef struct _asn1_bitstring
136{
137 size_t len; /**< ASN1 length, e.g. in octets. */
138 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
139 unsigned char *p; /**< Raw ASN1 data for the bit string */
140}
141asn1_bitstring;
142
143/**
144 * Container for a sequence of ASN.1 items
145 */
146typedef struct _asn1_sequence
147{
148 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
149 struct _asn1_sequence *next; /**< The next entry in the sequence. */
150}
151asn1_sequence;
152
153/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200154 * Container for a sequence or list of 'named' ASN.1 data items
155 */
156typedef struct _asn1_named_data
157{
158 asn1_buf oid; /**< The object identifier. */
159 asn1_buf val; /**< The named value. */
160 struct _asn1_named_data *next; /**< The next entry in the sequence. */
161}
162asn1_named_data;
163
164/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200165 * \brief Get the length of an ASN.1 element.
166 * Updates the pointer to immediately behind the length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000167 *
168 * \param p The position in the ASN.1 data
169 * \param end End of data
170 * \param len The variable that will receive the value
171 *
172 * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching
173 * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is
174 * unparseable.
175 */
176int asn1_get_len( unsigned char **p,
177 const unsigned char *end,
178 size_t *len );
179
180/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200181 * \brief Get the tag and length of the tag. Check for the requested tag.
182 * Updates the pointer to immediately behind the tag and length.
Paul Bakkerefc30292011-11-10 14:43:23 +0000183 *
184 * \param p The position in the ASN.1 data
185 * \param end End of data
186 * \param len The variable that will receive the length
187 * \param tag The expected tag
188 *
189 * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did
190 * not match requested tag, or another specific ASN.1 error code.
191 */
192int asn1_get_tag( unsigned char **p,
193 const unsigned char *end,
194 size_t *len, int tag );
195
196/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200197 * \brief Retrieve a boolean ASN.1 tag and its value.
198 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000199 *
200 * \param p The position in the ASN.1 data
201 * \param end End of data
202 * \param val The variable that will receive the value
203 *
204 * \return 0 if successful or a specific ASN.1 error code.
205 */
206int asn1_get_bool( unsigned char **p,
207 const unsigned char *end,
208 int *val );
209
210/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200211 * \brief Retrieve an integer ASN.1 tag and its value.
212 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000213 *
214 * \param p The position in the ASN.1 data
215 * \param end End of data
216 * \param val The variable that will receive the value
217 *
218 * \return 0 if successful or a specific ASN.1 error code.
219 */
220int asn1_get_int( unsigned char **p,
221 const unsigned char *end,
222 int *val );
223
224/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200225 * \brief Retrieve a bitstring ASN.1 tag and its value.
226 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000227 *
228 * \param p The position in the ASN.1 data
229 * \param end End of data
230 * \param bs The variable that will receive the value
231 *
232 * \return 0 if successful or a specific ASN.1 error code.
233 */
234int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
235 asn1_bitstring *bs);
236
237/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200238 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
239 * value.
240 * Updates the pointer to the beginning of the bit/octet string.
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200241 *
242 * \param p The position in the ASN.1 data
243 * \param end End of data
244 * \param len Length of the actual bit/octect string in bytes
245 *
246 * \return 0 if successful or a specific ASN.1 error code.
247 */
248int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
249 size_t *len );
250
251/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200252 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
253 * Updated the pointer to immediately behind the full sequence tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000254 *
255 * \param p The position in the ASN.1 data
256 * \param end End of data
257 * \param cur First variable in the chain to fill
Paul Bakker8b21f7a2012-01-13 13:29:05 +0000258 * \param tag Type of sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000259 *
260 * \return 0 if successful or a specific ASN.1 error code.
261 */
262int asn1_get_sequence_of( unsigned char **p,
263 const unsigned char *end,
264 asn1_sequence *cur,
265 int tag);
266
267#if defined(POLARSSL_BIGNUM_C)
268/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200269 * \brief Retrieve a MPI value from an integer ASN.1 tag.
270 * Updates the pointer to immediately behind the full tag.
Paul Bakkerefc30292011-11-10 14:43:23 +0000271 *
272 * \param p The position in the ASN.1 data
273 * \param end End of data
274 * \param X The MPI that will receive the value
275 *
276 * \return 0 if successful or a specific ASN.1 or MPI error code.
277 */
278int asn1_get_mpi( unsigned char **p,
279 const unsigned char *end,
280 mpi *X );
Paul Bakker9af723c2014-05-01 13:03:14 +0200281#endif /* POLARSSL_BIGNUM_C */
Paul Bakkerefc30292011-11-10 14:43:23 +0000282
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200283/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200284 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
285 * Updates the pointer to immediately behind the full
286 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200287 *
288 * \param p The position in the ASN.1 data
289 * \param end End of data
290 * \param alg The buffer to receive the OID
291 * \param params The buffer to receive the params (if any)
292 *
293 * \return 0 if successful or a specific ASN.1 or MPI error code.
294 */
295int asn1_get_alg( unsigned char **p,
296 const unsigned char *end,
297 asn1_buf *alg, asn1_buf *params );
298
299/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200300 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
301 * params.
302 * Updates the pointer to immediately behind the full
303 * AlgorithmIdentifier.
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200304 *
305 * \param p The position in the ASN.1 data
306 * \param end End of data
307 * \param alg The buffer to receive the OID
308 *
309 * \return 0 if successful or a specific ASN.1 or MPI error code.
310 */
311int asn1_get_alg_null( unsigned char **p,
312 const unsigned char *end,
313 asn1_buf *alg );
314
Paul Bakkere5eae762013-08-26 12:05:14 +0200315/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200316 * \brief Find a specific named_data entry in a sequence or list based on
317 * the OID.
Paul Bakkere5eae762013-08-26 12:05:14 +0200318 *
319 * \param list The list to seek through
320 * \param oid The OID to look for
321 * \param len Size of the OID
322 *
323 * \return NULL if not found, or a pointer to the existing entry.
324 */
325asn1_named_data *asn1_find_named_data( asn1_named_data *list,
326 const char *oid, size_t len );
327
328/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200329 * \brief Free a asn1_named_data entry
Paul Bakkere5eae762013-08-26 12:05:14 +0200330 *
331 * \param entry The named data entry to free
332 */
333void asn1_free_named_data( asn1_named_data *entry );
334
Paul Bakkerc547cc92013-09-09 12:01:23 +0200335/**
Paul Bakkercdda0972013-09-09 12:51:29 +0200336 * \brief Free all entries in a asn1_named_data list
337 * Head will be set to NULL
Paul Bakkerc547cc92013-09-09 12:01:23 +0200338 *
339 * \param head Pointer to the head of the list of named data entries to free
340 */
341void asn1_free_named_data_list( asn1_named_data **head );
342
Paul Bakkerefc30292011-11-10 14:43:23 +0000343#ifdef __cplusplus
344}
345#endif
346
347#endif /* asn1.h */