blob: 92bb70b608b3dfd730b459a8ece122dc61b1a33e [file] [log] [blame]
Paul Bakkerefc30292011-11-10 14:43:23 +00001/**
2 * \file asn1.h
3 *
4 * \brief Generic ASN.1 parsing
5 *
Paul Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerefc30292011-11-10 14:43:23 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerefc30292011-11-10 14:43:23 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_ASN1_H
25#define POLARSSL_ASN1_H
26
Paul Bakkerccdb0282011-12-15 19:49:51 +000027#include "config.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000028
29#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerccdb0282011-12-15 19:49:51 +000030#include "bignum.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000031#endif
32
33#include <string.h>
34
35/**
36 * \addtogroup asn1_module
37 * \{
38 */
39
40/**
41 * \name ASN1 Error codes
42 * These error codes are OR'ed to X509 error codes for
43 * higher error granularity.
44 * ASN1 is a standard to specify data structures.
45 * \{
46 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000047#define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */
48#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */
49#define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */
50#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */
51#define POLARSSL_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */
52#define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x006A /**< Memory allocation failed */
Paul Bakker05888152012-02-16 10:26:57 +000053#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
54
Paul Bakkerefc30292011-11-10 14:43:23 +000055/* \} 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
Paul Bakker8b21f7a2012-01-13 13:29:05 +0000214 * \param tag Type of sequence
Paul Bakkerefc30292011-11-10 14:43:23 +0000215 *
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 */