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