initial commit; it works
[psslcertgen.git] / src / libpolarssl / asn1.h
blob893292dfa66fe17a7172270fd63836c960f50500
1 /**
2 * \file asn1.h
4 * \brief Generic ASN.1 parsing
6 * Copyright (C) 2006-2011, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
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.
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.
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.
27 #ifndef POLARSSL_ASN1_H
28 #define POLARSSL_ASN1_H
30 #include "config.h"
32 #if defined(POLARSSL_BIGNUM_C)
33 #include "bignum.h"
34 #endif
36 #include <string.h>
38 /**
39 * \addtogroup asn1_module
40 * \{
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 * \{
50 #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 */
56 #define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
58 /* \} name */
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 * \{
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 */
93 /** Returns the size of the binary string, without the trailing \\0 */
94 #define OID_SIZE(x) (sizeof(x) - 1)
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
101 * \name Functions to parse ASN.1 data structures
102 * \{
106 * Type-length-value structure that allows for ASN1 using DER.
108 typedef struct _asn1_buf
110 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
111 size_t len; /**< ASN1 length, e.g. in octets. */
112 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
114 asn1_buf;
117 * Container for ASN1 bit strings.
119 typedef struct _asn1_bitstring
121 size_t len; /**< ASN1 length, e.g. in octets. */
122 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
123 unsigned char *p; /**< Raw ASN1 data for the bit string */
125 asn1_bitstring;
128 * Container for a sequence of ASN.1 items
130 typedef struct _asn1_sequence
132 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
133 struct _asn1_sequence *next; /**< The next entry in the sequence. */
135 asn1_sequence;
138 * Get the length of an ASN.1 element.
139 * Updates the pointer to immediately behind the length.
141 * \param p The position in the ASN.1 data
142 * \param end End of data
143 * \param len The variable that will receive the value
145 * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching
146 * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is
147 * unparseable.
149 int asn1_get_len( unsigned char **p,
150 const unsigned char *end,
151 size_t *len );
154 * Get the tag and length of the tag. Check for the requested tag.
155 * Updates the pointer to immediately behind the tag and length.
157 * \param p The position in the ASN.1 data
158 * \param end End of data
159 * \param len The variable that will receive the length
160 * \param tag The expected tag
162 * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did
163 * not match requested tag, or another specific ASN.1 error code.
165 int asn1_get_tag( unsigned char **p,
166 const unsigned char *end,
167 size_t *len, int tag );
170 * Retrieve a boolean ASN.1 tag and its value.
171 * Updates the pointer to immediately behind the full tag.
173 * \param p The position in the ASN.1 data
174 * \param end End of data
175 * \param val The variable that will receive the value
177 * \return 0 if successful or a specific ASN.1 error code.
179 int asn1_get_bool( unsigned char **p,
180 const unsigned char *end,
181 int *val );
184 * Retrieve an integer ASN.1 tag and its value.
185 * Updates the pointer to immediately behind the full tag.
187 * \param p The position in the ASN.1 data
188 * \param end End of data
189 * \param val The variable that will receive the value
191 * \return 0 if successful or a specific ASN.1 error code.
193 int asn1_get_int( unsigned char **p,
194 const unsigned char *end,
195 int *val );
198 * Retrieve a bitstring ASN.1 tag and its value.
199 * Updates the pointer to immediately behind the full tag.
201 * \param p The position in the ASN.1 data
202 * \param end End of data
203 * \param bs The variable that will receive the value
205 * \return 0 if successful or a specific ASN.1 error code.
207 int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
208 asn1_bitstring *bs);
211 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
212 * Updated the pointer to immediately behind the full sequence tag.
214 * \param p The position in the ASN.1 data
215 * \param end End of data
216 * \param cur First variable in the chain to fill
217 * \param tag Type of sequence
219 * \return 0 if successful or a specific ASN.1 error code.
221 int asn1_get_sequence_of( unsigned char **p,
222 const unsigned char *end,
223 asn1_sequence *cur,
224 int tag);
226 #if defined(POLARSSL_BIGNUM_C)
228 * Retrieve a MPI value from an integer ASN.1 tag.
229 * Updates the pointer to immediately behind the full tag.
231 * \param p The position in the ASN.1 data
232 * \param end End of data
233 * \param X The MPI that will receive the value
235 * \return 0 if successful or a specific ASN.1 or MPI error code.
237 int asn1_get_mpi( unsigned char **p,
238 const unsigned char *end,
239 mpi *X );
240 #endif
242 #ifdef __cplusplus
244 #endif
246 #endif /* asn1.h */