Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
Embed:
(wiki syntax)
Show/hide line numbers
asn1.h
Go to the documentation of this file.
00001 /** 00002 * \file asn1.h 00003 * 00004 * \brief Generic ASN.1 parsing 00005 * 00006 * Copyright (C) 2006-2013, Brainspark B.V. 00007 * 00008 * This file is part of PolarSSL (http://www.polarssl.org) 00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> 00010 * 00011 * All rights reserved. 00012 * 00013 * This program is free software; you can redistribute it and/or modify 00014 * it under the terms of the GNU General Public License as published by 00015 * the Free Software Foundation; either version 2 of the License, or 00016 * (at your option) any later version. 00017 * 00018 * This program is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 * GNU General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License along 00024 * with this program; if not, write to the Free Software Foundation, Inc., 00025 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00026 */ 00027 #ifndef POLARSSL_ASN1_H 00028 #define POLARSSL_ASN1_H 00029 00030 #if !defined(POLARSSL_CONFIG_FILE) 00031 #include "config.h" 00032 #else 00033 #include POLARSSL_CONFIG_FILE 00034 #endif 00035 00036 #if defined(POLARSSL_BIGNUM_C) 00037 #include "bignum.h" 00038 #endif 00039 00040 #include <string.h> 00041 00042 /** 00043 * \addtogroup asn1_module 00044 * \{ 00045 */ 00046 00047 /** 00048 * \name ASN1 Error codes 00049 * These error codes are OR'ed to X509 error codes for 00050 * higher error granularity. 00051 * ASN1 is a standard to specify data structures. 00052 * \{ 00053 */ 00054 #define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */ 00055 #define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */ 00056 #define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */ 00057 #define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */ 00058 #define POLARSSL_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */ 00059 #define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x006A /**< Memory allocation failed */ 00060 #define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */ 00061 00062 /* \} name */ 00063 00064 /** 00065 * \name DER constants 00066 * These constants comply with DER encoded the ANS1 type tags. 00067 * DER encoding uses hexadecimal representation. 00068 * An example DER sequence is:\n 00069 * - 0x02 -- tag indicating INTEGER 00070 * - 0x01 -- length in octets 00071 * - 0x05 -- value 00072 * Such sequences are typically read into \c ::x509_buf. 00073 * \{ 00074 */ 00075 #define ASN1_BOOLEAN 0x01 00076 #define ASN1_INTEGER 0x02 00077 #define ASN1_BIT_STRING 0x03 00078 #define ASN1_OCTET_STRING 0x04 00079 #define ASN1_NULL 0x05 00080 #define ASN1_OID 0x06 00081 #define ASN1_UTF8_STRING 0x0C 00082 #define ASN1_SEQUENCE 0x10 00083 #define ASN1_SET 0x11 00084 #define ASN1_PRINTABLE_STRING 0x13 00085 #define ASN1_T61_STRING 0x14 00086 #define ASN1_IA5_STRING 0x16 00087 #define ASN1_UTC_TIME 0x17 00088 #define ASN1_GENERALIZED_TIME 0x18 00089 #define ASN1_UNIVERSAL_STRING 0x1C 00090 #define ASN1_BMP_STRING 0x1E 00091 #define ASN1_PRIMITIVE 0x00 00092 #define ASN1_CONSTRUCTED 0x20 00093 #define ASN1_CONTEXT_SPECIFIC 0x80 00094 /* \} name */ 00095 /* \} addtogroup asn1_module */ 00096 00097 /** Returns the size of the binary string, without the trailing \\0 */ 00098 #define OID_SIZE(x) (sizeof(x) - 1) 00099 00100 /** Compares two asn1_buf structures for the same OID. Only works for 00101 * 'defined' oid_str values (OID_HMAC_SHA1), you cannot use a 'unsigned 00102 * char *oid' here! 00103 */ 00104 #define OID_CMP(oid_str, oid_buf) \ 00105 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \ 00106 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0 ) 00107 00108 #ifdef __cplusplus 00109 extern "C" { 00110 #endif 00111 00112 /** 00113 * \name Functions to parse ASN.1 data structures 00114 * \{ 00115 */ 00116 00117 /** 00118 * Type-length-value structure that allows for ASN1 using DER. 00119 */ 00120 typedef struct _asn1_buf 00121 { 00122 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */ 00123 size_t len; /**< ASN1 length, e.g. in octets. */ 00124 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */ 00125 } 00126 asn1_buf; 00127 00128 /** 00129 * Container for ASN1 bit strings. 00130 */ 00131 typedef struct _asn1_bitstring 00132 { 00133 size_t len; /**< ASN1 length, e.g. in octets. */ 00134 unsigned char unused_bits; /**< Number of unused bits at the end of the string */ 00135 unsigned char *p; /**< Raw ASN1 data for the bit string */ 00136 } 00137 asn1_bitstring; 00138 00139 /** 00140 * Container for a sequence of ASN.1 items 00141 */ 00142 typedef struct _asn1_sequence 00143 { 00144 asn1_buf buf; /**< Buffer containing the given ASN.1 item. */ 00145 struct _asn1_sequence *next; /**< The next entry in the sequence. */ 00146 } 00147 asn1_sequence; 00148 00149 /** 00150 * Container for a sequence or list of 'named' ASN.1 data items 00151 */ 00152 typedef struct _asn1_named_data 00153 { 00154 asn1_buf oid; /**< The object identifier. */ 00155 asn1_buf val; /**< The named value. */ 00156 struct _asn1_named_data *next; /**< The next entry in the sequence. */ 00157 } 00158 asn1_named_data; 00159 00160 /** 00161 * \brief Get the length of an ASN.1 element. 00162 * Updates the pointer to immediately behind the length. 00163 * 00164 * \param p The position in the ASN.1 data 00165 * \param end End of data 00166 * \param len The variable that will receive the value 00167 * 00168 * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching 00169 * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is 00170 * unparseable. 00171 */ 00172 int asn1_get_len( unsigned char **p, 00173 const unsigned char *end, 00174 size_t *len ); 00175 00176 /** 00177 * \brief Get the tag and length of the tag. Check for the requested tag. 00178 * Updates the pointer to immediately behind the tag and length. 00179 * 00180 * \param p The position in the ASN.1 data 00181 * \param end End of data 00182 * \param len The variable that will receive the length 00183 * \param tag The expected tag 00184 * 00185 * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did 00186 * not match requested tag, or another specific ASN.1 error code. 00187 */ 00188 int asn1_get_tag( unsigned char **p, 00189 const unsigned char *end, 00190 size_t *len, int tag ); 00191 00192 /** 00193 * \brief Retrieve a boolean ASN.1 tag and its value. 00194 * Updates the pointer to immediately behind the full tag. 00195 * 00196 * \param p The position in the ASN.1 data 00197 * \param end End of data 00198 * \param val The variable that will receive the value 00199 * 00200 * \return 0 if successful or a specific ASN.1 error code. 00201 */ 00202 int asn1_get_bool( unsigned char **p, 00203 const unsigned char *end, 00204 int *val ); 00205 00206 /** 00207 * \brief Retrieve an integer ASN.1 tag and its value. 00208 * Updates the pointer to immediately behind the full tag. 00209 * 00210 * \param p The position in the ASN.1 data 00211 * \param end End of data 00212 * \param val The variable that will receive the value 00213 * 00214 * \return 0 if successful or a specific ASN.1 error code. 00215 */ 00216 int asn1_get_int( unsigned char **p, 00217 const unsigned char *end, 00218 int *val ); 00219 00220 /** 00221 * \brief Retrieve a bitstring ASN.1 tag and its value. 00222 * Updates the pointer to immediately behind the full tag. 00223 * 00224 * \param p The position in the ASN.1 data 00225 * \param end End of data 00226 * \param bs The variable that will receive the value 00227 * 00228 * \return 0 if successful or a specific ASN.1 error code. 00229 */ 00230 int asn1_get_bitstring( unsigned char **p, const unsigned char *end, 00231 asn1_bitstring *bs); 00232 00233 /** 00234 * \brief Retrieve a bitstring ASN.1 tag without unused bits and its 00235 * value. 00236 * Updates the pointer to the beginning of the bit/octet string. 00237 * 00238 * \param p The position in the ASN.1 data 00239 * \param end End of data 00240 * \param len Length of the actual bit/octect string in bytes 00241 * 00242 * \return 0 if successful or a specific ASN.1 error code. 00243 */ 00244 int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end, 00245 size_t *len ); 00246 00247 /** 00248 * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>" 00249 * Updated the pointer to immediately behind the full sequence tag. 00250 * 00251 * \param p The position in the ASN.1 data 00252 * \param end End of data 00253 * \param cur First variable in the chain to fill 00254 * \param tag Type of sequence 00255 * 00256 * \return 0 if successful or a specific ASN.1 error code. 00257 */ 00258 int asn1_get_sequence_of( unsigned char **p, 00259 const unsigned char *end, 00260 asn1_sequence *cur, 00261 int tag); 00262 00263 #if defined(POLARSSL_BIGNUM_C) 00264 /** 00265 * \brief Retrieve a MPI value from an integer ASN.1 tag. 00266 * Updates the pointer to immediately behind the full tag. 00267 * 00268 * \param p The position in the ASN.1 data 00269 * \param end End of data 00270 * \param X The MPI that will receive the value 00271 * 00272 * \return 0 if successful or a specific ASN.1 or MPI error code. 00273 */ 00274 int asn1_get_mpi( unsigned char **p, 00275 const unsigned char *end, 00276 mpi *X ); 00277 #endif /* POLARSSL_BIGNUM_C */ 00278 00279 /** 00280 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence. 00281 * Updates the pointer to immediately behind the full 00282 * AlgorithmIdentifier. 00283 * 00284 * \param p The position in the ASN.1 data 00285 * \param end End of data 00286 * \param alg The buffer to receive the OID 00287 * \param params The buffer to receive the params (if any) 00288 * 00289 * \return 0 if successful or a specific ASN.1 or MPI error code. 00290 */ 00291 int asn1_get_alg( unsigned char **p, 00292 const unsigned char *end, 00293 asn1_buf *alg, asn1_buf *params ); 00294 00295 /** 00296 * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no 00297 * params. 00298 * Updates the pointer to immediately behind the full 00299 * AlgorithmIdentifier. 00300 * 00301 * \param p The position in the ASN.1 data 00302 * \param end End of data 00303 * \param alg The buffer to receive the OID 00304 * 00305 * \return 0 if successful or a specific ASN.1 or MPI error code. 00306 */ 00307 int asn1_get_alg_null( unsigned char **p, 00308 const unsigned char *end, 00309 asn1_buf *alg ); 00310 00311 /** 00312 * \brief Find a specific named_data entry in a sequence or list based on 00313 * the OID. 00314 * 00315 * \param list The list to seek through 00316 * \param oid The OID to look for 00317 * \param len Size of the OID 00318 * 00319 * \return NULL if not found, or a pointer to the existing entry. 00320 */ 00321 asn1_named_data *asn1_find_named_data( asn1_named_data *list, 00322 const char *oid, size_t len ); 00323 00324 /** 00325 * \brief Free a asn1_named_data entry 00326 * 00327 * \param entry The named data entry to free 00328 */ 00329 void asn1_free_named_data( asn1_named_data *entry ); 00330 00331 /** 00332 * \brief Free all entries in a asn1_named_data list 00333 * Head will be set to NULL 00334 * 00335 * \param head Pointer to the head of the list of named data entries to free 00336 */ 00337 void asn1_free_named_data_list( asn1_named_data **head ); 00338 00339 #ifdef __cplusplus 00340 } 00341 #endif 00342 00343 #endif /* asn1.h */ 00344 00345
Generated on Tue Jul 12 2022 19:40:15 by 1.7.2