blob: 195ebcb84db7cbabd86d3d1fd9c8a4256daefe5c [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
Paul Bakkerccdb0282011-12-15 19:49:51 +000030#include "config.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000031
32#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerccdb0282011-12-15 19:49:51 +000033#include "bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000034#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 Bakkerbdb912d2012-02-13 23:11:30 +000050#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 Bakker05888152012-02-16 10:26:57 +000056#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
57
Paul Bakkerefc30292011-11-10 14:43:23 +000058/* \} 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 Bakkerc70b9822013-04-07 22:00:46 +020096/** 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 Bakkerefc30292011-11-10 14:43:23 +0000101#ifdef __cplusplus
102extern "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 */
113typedef 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}
119asn1_buf;
120
121/**
122 * Container for ASN1 bit strings.
123 */
124typedef 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}
130asn1_bitstring;
131
132/**
133 * Container for a sequence of ASN.1 items
134 */
135typedef 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}
140asn1_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 */
154int 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 */
170int 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 */
184int 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 */
198int 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 */
212int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
213 asn1_bitstring *bs);
214
215/**
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200216 * Retrieve a bitstring ASN.1 tag without unused bits and its value.
217 * Updates the pointer to the beginning of the bit/octet string.
218 *
219 * \param p The position in the ASN.1 data
220 * \param end End of data
221 * \param len Length of the actual bit/octect string in bytes
222 *
223 * \return 0 if successful or a specific ASN.1 error code.
224 */
225int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
226 size_t *len );
227
228/**
Paul Bakkerefc30292011-11-10 14:43:23 +0000229 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
230 * Updated the pointer to immediately behind the full sequence tag.
231 *
232 * \param p The position in the ASN.1 data
233 * \param end End of data
234 * \param cur First variable in the chain to fill
Paul Bakker8b21f7a2012-01-13 13:29:05 +0000235 * \param tag Type of sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000236 *
237 * \return 0 if successful or a specific ASN.1 error code.
238 */
239int asn1_get_sequence_of( unsigned char **p,
240 const unsigned char *end,
241 asn1_sequence *cur,
242 int tag);
243
244#if defined(POLARSSL_BIGNUM_C)
245/**
246 * Retrieve a MPI value from an integer ASN.1 tag.
247 * Updates the pointer to immediately behind the full tag.
248 *
249 * \param p The position in the ASN.1 data
250 * \param end End of data
251 * \param X The MPI that will receive the value
252 *
253 * \return 0 if successful or a specific ASN.1 or MPI error code.
254 */
255int asn1_get_mpi( unsigned char **p,
256 const unsigned char *end,
257 mpi *X );
258#endif
259
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200260/**
261 * Retrieve an AlgorithmIdentifier ASN.1 sequence.
262 * Updates the pointer to immediately behind the full AlgorithmIdentifier.
263 *
264 * \param p The position in the ASN.1 data
265 * \param end End of data
266 * \param alg The buffer to receive the OID
267 * \param params The buffer to receive the params (if any)
268 *
269 * \return 0 if successful or a specific ASN.1 or MPI error code.
270 */
271int asn1_get_alg( unsigned char **p,
272 const unsigned char *end,
273 asn1_buf *alg, asn1_buf *params );
274
275/**
276 * Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no params.
277 * Updates the pointer to immediately behind the full AlgorithmIdentifier.
278 *
279 * \param p The position in the ASN.1 data
280 * \param end End of data
281 * \param alg The buffer to receive the OID
282 *
283 * \return 0 if successful or a specific ASN.1 or MPI error code.
284 */
285int asn1_get_alg_null( unsigned char **p,
286 const unsigned char *end,
287 asn1_buf *alg );
288
Paul Bakkerefc30292011-11-10 14:43:23 +0000289#ifdef __cplusplus
290}
291#endif
292
293#endif /* asn1.h */