blob: fc86fc8123a60be234bc85db7c07e03f49591b26 [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/**
2 * \file asn1.h
3 *
4 * \brief Generic ASN.1 parsing
5 *
6 * Copyright (C) 2006-2011, Brainspark B.V.
7 *
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
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_BIGNUM_C)
33#include "polarssl/bignum.h"
34#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 */
50#define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0014 /**< Out of data when parsing an ASN1 data structure. */
51#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0016 /**< ASN1 tag was of an unexpected value. */
52#define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0018 /**< Error when trying to determine the length or invalid length. */
53#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x001A /**< Actual length differs from expected length. */
54#define POLARSSL_ERR_ASN1_INVALID_DATA -0x001C /**< Data is invalid. (not used) */
55/* \} name */
56
57/**
58 * \name DER constants
59 * These constants comply with DER encoded the ANS1 type tags.
60 * DER encoding uses hexadecimal representation.
61 * An example DER sequence is:\n
62 * - 0x02 -- tag indicating INTEGER
63 * - 0x01 -- length in octets
64 * - 0x05 -- value
65 * Such sequences are typically read into \c ::x509_buf.
66 * \{
67 */
68#define ASN1_BOOLEAN 0x01
69#define ASN1_INTEGER 0x02
70#define ASN1_BIT_STRING 0x03
71#define ASN1_OCTET_STRING 0x04
72#define ASN1_NULL 0x05
73#define ASN1_OID 0x06
74#define ASN1_UTF8_STRING 0x0C
75#define ASN1_SEQUENCE 0x10
76#define ASN1_SET 0x11
77#define ASN1_PRINTABLE_STRING 0x13
78#define ASN1_T61_STRING 0x14
79#define ASN1_IA5_STRING 0x16
80#define ASN1_UTC_TIME 0x17
81#define ASN1_GENERALIZED_TIME 0x18
82#define ASN1_UNIVERSAL_STRING 0x1C
83#define ASN1_BMP_STRING 0x1E
84#define ASN1_PRIMITIVE 0x00
85#define ASN1_CONSTRUCTED 0x20
86#define ASN1_CONTEXT_SPECIFIC 0x80
87/* \} name */
88/* \} addtogroup asn1_module */
89
90/** Returns the size of the binary string, without the trailing \\0 */
91#define OID_SIZE(x) (sizeof(x) - 1)
92
93#ifdef __cplusplus
94extern "C" {
95#endif
96
97/**
98 * \name Functions to parse ASN.1 data structures
99 * \{
100 */
101
102/**
103 * Type-length-value structure that allows for ASN1 using DER.
104 */
105typedef struct _asn1_buf
106{
107 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
108 size_t len; /**< ASN1 length, e.g. in octets. */
109 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
110}
111asn1_buf;
112
113/**
114 * Container for ASN1 bit strings.
115 */
116typedef struct _asn1_bitstring
117{
118 size_t len; /**< ASN1 length, e.g. in octets. */
119 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
120 unsigned char *p; /**< Raw ASN1 data for the bit string */
121}
122asn1_bitstring;
123
124/**
125 * Container for a sequence of ASN.1 items
126 */
127typedef struct _asn1_sequence
128{
129 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
130 struct _asn1_sequence *next; /**< The next entry in the sequence. */
131}
132asn1_sequence;
133
134/**
135 * Get the length of an ASN.1 element.
136 * Updates the pointer to immediately behind the length.
137 *
138 * \param p The position in the ASN.1 data
139 * \param end End of data
140 * \param len The variable that will receive the value
141 *
142 * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching
143 * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is
144 * unparseable.
145 */
146int asn1_get_len( unsigned char **p,
147 const unsigned char *end,
148 size_t *len );
149
150/**
151 * Get the tag and length of the tag. Check for the requested tag.
152 * Updates the pointer to immediately behind the tag and length.
153 *
154 * \param p The position in the ASN.1 data
155 * \param end End of data
156 * \param len The variable that will receive the length
157 * \param tag The expected tag
158 *
159 * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did
160 * not match requested tag, or another specific ASN.1 error code.
161 */
162int asn1_get_tag( unsigned char **p,
163 const unsigned char *end,
164 size_t *len, int tag );
165
166/**
167 * Retrieve a boolean ASN.1 tag and its value.
168 * Updates the pointer to immediately behind the full tag.
169 *
170 * \param p The position in the ASN.1 data
171 * \param end End of data
172 * \param val The variable that will receive the value
173 *
174 * \return 0 if successful or a specific ASN.1 error code.
175 */
176int asn1_get_bool( unsigned char **p,
177 const unsigned char *end,
178 int *val );
179
180/**
181 * Retrieve an integer ASN.1 tag and its value.
182 * Updates the pointer to immediately behind the full tag.
183 *
184 * \param p The position in the ASN.1 data
185 * \param end End of data
186 * \param val The variable that will receive the value
187 *
188 * \return 0 if successful or a specific ASN.1 error code.
189 */
190int asn1_get_int( unsigned char **p,
191 const unsigned char *end,
192 int *val );
193
194/**
195 * Retrieve a bitstring ASN.1 tag and its value.
196 * Updates the pointer to immediately behind the full tag.
197 *
198 * \param p The position in the ASN.1 data
199 * \param end End of data
200 * \param bs The variable that will receive the value
201 *
202 * \return 0 if successful or a specific ASN.1 error code.
203 */
204int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
205 asn1_bitstring *bs);
206
207/**
208 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
209 * Updated the pointer to immediately behind the full sequence tag.
210 *
211 * \param p The position in the ASN.1 data
212 * \param end End of data
213 * \param cur First variable in the chain to fill
214 *
215 * \return 0 if successful or a specific ASN.1 error code.
216 */
217int asn1_get_sequence_of( unsigned char **p,
218 const unsigned char *end,
219 asn1_sequence *cur,
220 int tag);
221
222#if defined(POLARSSL_BIGNUM_C)
223/**
224 * Retrieve a MPI value from an integer ASN.1 tag.
225 * Updates the pointer to immediately behind the full tag.
226 *
227 * \param p The position in the ASN.1 data
228 * \param end End of data
229 * \param X The MPI that will receive the value
230 *
231 * \return 0 if successful or a specific ASN.1 or MPI error code.
232 */
233int asn1_get_mpi( unsigned char **p,
234 const unsigned char *end,
235 mpi *X );
236#endif
237
238#ifdef __cplusplus
239}
240#endif
241
242#endif /* asn1.h */