Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/asn1parse.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * Generic ASN.1 parsing |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | |
HannesTschofenig | 0:796d0f61a05b | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 27 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 28 | #else |
HannesTschofenig | 0:796d0f61a05b | 29 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 30 | #endif |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if defined(POLARSSL_ASN1_PARSE_C) |
HannesTschofenig | 0:796d0f61a05b | 33 | |
HannesTschofenig | 0:796d0f61a05b | 34 | #include "polarssl/asn1.h" |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #if defined(POLARSSL_BIGNUM_C) |
HannesTschofenig | 0:796d0f61a05b | 37 | #include "polarssl/bignum.h" |
HannesTschofenig | 0:796d0f61a05b | 38 | #endif |
HannesTschofenig | 0:796d0f61a05b | 39 | |
HannesTschofenig | 0:796d0f61a05b | 40 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 41 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 42 | #else |
HannesTschofenig | 0:796d0f61a05b | 43 | #define polarssl_malloc malloc |
HannesTschofenig | 0:796d0f61a05b | 44 | #define polarssl_free free |
HannesTschofenig | 0:796d0f61a05b | 45 | #endif |
HannesTschofenig | 0:796d0f61a05b | 46 | |
HannesTschofenig | 0:796d0f61a05b | 47 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 48 | #include <stdlib.h> |
HannesTschofenig | 0:796d0f61a05b | 49 | |
HannesTschofenig | 0:796d0f61a05b | 50 | /* |
HannesTschofenig | 0:796d0f61a05b | 51 | * ASN.1 DER decoding routines |
HannesTschofenig | 0:796d0f61a05b | 52 | */ |
HannesTschofenig | 0:796d0f61a05b | 53 | int asn1_get_len( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 54 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 55 | size_t *len ) |
HannesTschofenig | 0:796d0f61a05b | 56 | { |
HannesTschofenig | 0:796d0f61a05b | 57 | if( ( end - *p ) < 1 ) |
HannesTschofenig | 0:796d0f61a05b | 58 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 59 | |
HannesTschofenig | 0:796d0f61a05b | 60 | if( ( **p & 0x80 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 61 | *len = *(*p)++; |
HannesTschofenig | 0:796d0f61a05b | 62 | else |
HannesTschofenig | 0:796d0f61a05b | 63 | { |
HannesTschofenig | 0:796d0f61a05b | 64 | switch( **p & 0x7F ) |
HannesTschofenig | 0:796d0f61a05b | 65 | { |
HannesTschofenig | 0:796d0f61a05b | 66 | case 1: |
HannesTschofenig | 0:796d0f61a05b | 67 | if( ( end - *p ) < 2 ) |
HannesTschofenig | 0:796d0f61a05b | 68 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 69 | |
HannesTschofenig | 0:796d0f61a05b | 70 | *len = (*p)[1]; |
HannesTschofenig | 0:796d0f61a05b | 71 | (*p) += 2; |
HannesTschofenig | 0:796d0f61a05b | 72 | break; |
HannesTschofenig | 0:796d0f61a05b | 73 | |
HannesTschofenig | 0:796d0f61a05b | 74 | case 2: |
HannesTschofenig | 0:796d0f61a05b | 75 | if( ( end - *p ) < 3 ) |
HannesTschofenig | 0:796d0f61a05b | 76 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 77 | |
HannesTschofenig | 0:796d0f61a05b | 78 | *len = ( (*p)[1] << 8 ) | (*p)[2]; |
HannesTschofenig | 0:796d0f61a05b | 79 | (*p) += 3; |
HannesTschofenig | 0:796d0f61a05b | 80 | break; |
HannesTschofenig | 0:796d0f61a05b | 81 | |
HannesTschofenig | 0:796d0f61a05b | 82 | case 3: |
HannesTschofenig | 0:796d0f61a05b | 83 | if( ( end - *p ) < 4 ) |
HannesTschofenig | 0:796d0f61a05b | 84 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 85 | |
HannesTschofenig | 0:796d0f61a05b | 86 | *len = ( (*p)[1] << 16 ) | ( (*p)[2] << 8 ) | (*p)[3]; |
HannesTschofenig | 0:796d0f61a05b | 87 | (*p) += 4; |
HannesTschofenig | 0:796d0f61a05b | 88 | break; |
HannesTschofenig | 0:796d0f61a05b | 89 | |
HannesTschofenig | 0:796d0f61a05b | 90 | case 4: |
HannesTschofenig | 0:796d0f61a05b | 91 | if( ( end - *p ) < 5 ) |
HannesTschofenig | 0:796d0f61a05b | 92 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 93 | |
HannesTschofenig | 0:796d0f61a05b | 94 | *len = ( (*p)[1] << 24 ) | ( (*p)[2] << 16 ) | ( (*p)[3] << 8 ) | |
HannesTschofenig | 0:796d0f61a05b | 95 | (*p)[4]; |
HannesTschofenig | 0:796d0f61a05b | 96 | (*p) += 5; |
HannesTschofenig | 0:796d0f61a05b | 97 | break; |
HannesTschofenig | 0:796d0f61a05b | 98 | |
HannesTschofenig | 0:796d0f61a05b | 99 | default: |
HannesTschofenig | 0:796d0f61a05b | 100 | return( POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
HannesTschofenig | 0:796d0f61a05b | 101 | } |
HannesTschofenig | 0:796d0f61a05b | 102 | } |
HannesTschofenig | 0:796d0f61a05b | 103 | |
HannesTschofenig | 0:796d0f61a05b | 104 | if( *len > (size_t) ( end - *p ) ) |
HannesTschofenig | 0:796d0f61a05b | 105 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 106 | |
HannesTschofenig | 0:796d0f61a05b | 107 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 108 | } |
HannesTschofenig | 0:796d0f61a05b | 109 | |
HannesTschofenig | 0:796d0f61a05b | 110 | int asn1_get_tag( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 111 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 112 | size_t *len, int tag ) |
HannesTschofenig | 0:796d0f61a05b | 113 | { |
HannesTschofenig | 0:796d0f61a05b | 114 | if( ( end - *p ) < 1 ) |
HannesTschofenig | 0:796d0f61a05b | 115 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 116 | |
HannesTschofenig | 0:796d0f61a05b | 117 | if( **p != tag ) |
HannesTschofenig | 0:796d0f61a05b | 118 | return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); |
HannesTschofenig | 0:796d0f61a05b | 119 | |
HannesTschofenig | 0:796d0f61a05b | 120 | (*p)++; |
HannesTschofenig | 0:796d0f61a05b | 121 | |
HannesTschofenig | 0:796d0f61a05b | 122 | return( asn1_get_len( p, end, len ) ); |
HannesTschofenig | 0:796d0f61a05b | 123 | } |
HannesTschofenig | 0:796d0f61a05b | 124 | |
HannesTschofenig | 0:796d0f61a05b | 125 | int asn1_get_bool( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 126 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 127 | int *val ) |
HannesTschofenig | 0:796d0f61a05b | 128 | { |
HannesTschofenig | 0:796d0f61a05b | 129 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 130 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 131 | |
HannesTschofenig | 0:796d0f61a05b | 132 | if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 133 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 134 | |
HannesTschofenig | 0:796d0f61a05b | 135 | if( len != 1 ) |
HannesTschofenig | 0:796d0f61a05b | 136 | return( POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
HannesTschofenig | 0:796d0f61a05b | 137 | |
HannesTschofenig | 0:796d0f61a05b | 138 | *val = ( **p != 0 ) ? 1 : 0; |
HannesTschofenig | 0:796d0f61a05b | 139 | (*p)++; |
HannesTschofenig | 0:796d0f61a05b | 140 | |
HannesTschofenig | 0:796d0f61a05b | 141 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 142 | } |
HannesTschofenig | 0:796d0f61a05b | 143 | |
HannesTschofenig | 0:796d0f61a05b | 144 | int asn1_get_int( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 145 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 146 | int *val ) |
HannesTschofenig | 0:796d0f61a05b | 147 | { |
HannesTschofenig | 0:796d0f61a05b | 148 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 149 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 150 | |
HannesTschofenig | 0:796d0f61a05b | 151 | if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 152 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 153 | |
HannesTschofenig | 0:796d0f61a05b | 154 | if( len > sizeof( int ) || ( **p & 0x80 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 155 | return( POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
HannesTschofenig | 0:796d0f61a05b | 156 | |
HannesTschofenig | 0:796d0f61a05b | 157 | *val = 0; |
HannesTschofenig | 0:796d0f61a05b | 158 | |
HannesTschofenig | 0:796d0f61a05b | 159 | while( len-- > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 160 | { |
HannesTschofenig | 0:796d0f61a05b | 161 | *val = ( *val << 8 ) | **p; |
HannesTschofenig | 0:796d0f61a05b | 162 | (*p)++; |
HannesTschofenig | 0:796d0f61a05b | 163 | } |
HannesTschofenig | 0:796d0f61a05b | 164 | |
HannesTschofenig | 0:796d0f61a05b | 165 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 166 | } |
HannesTschofenig | 0:796d0f61a05b | 167 | |
HannesTschofenig | 0:796d0f61a05b | 168 | #if defined(POLARSSL_BIGNUM_C) |
HannesTschofenig | 0:796d0f61a05b | 169 | int asn1_get_mpi( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 170 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 171 | mpi *X ) |
HannesTschofenig | 0:796d0f61a05b | 172 | { |
HannesTschofenig | 0:796d0f61a05b | 173 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 174 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 175 | |
HannesTschofenig | 0:796d0f61a05b | 176 | if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 177 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 178 | |
HannesTschofenig | 0:796d0f61a05b | 179 | ret = mpi_read_binary( X, *p, len ); |
HannesTschofenig | 0:796d0f61a05b | 180 | |
HannesTschofenig | 0:796d0f61a05b | 181 | *p += len; |
HannesTschofenig | 0:796d0f61a05b | 182 | |
HannesTschofenig | 0:796d0f61a05b | 183 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 184 | } |
HannesTschofenig | 0:796d0f61a05b | 185 | #endif /* POLARSSL_BIGNUM_C */ |
HannesTschofenig | 0:796d0f61a05b | 186 | |
HannesTschofenig | 0:796d0f61a05b | 187 | int asn1_get_bitstring( unsigned char **p, const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 188 | asn1_bitstring *bs) |
HannesTschofenig | 0:796d0f61a05b | 189 | { |
HannesTschofenig | 0:796d0f61a05b | 190 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 191 | |
HannesTschofenig | 0:796d0f61a05b | 192 | /* Certificate type is a single byte bitstring */ |
HannesTschofenig | 0:796d0f61a05b | 193 | if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 194 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 195 | |
HannesTschofenig | 0:796d0f61a05b | 196 | /* Check length, subtract one for actual bit string length */ |
HannesTschofenig | 0:796d0f61a05b | 197 | if ( bs->len < 1 ) |
HannesTschofenig | 0:796d0f61a05b | 198 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 199 | bs->len -= 1; |
HannesTschofenig | 0:796d0f61a05b | 200 | |
HannesTschofenig | 0:796d0f61a05b | 201 | /* Get number of unused bits, ensure unused bits <= 7 */ |
HannesTschofenig | 0:796d0f61a05b | 202 | bs->unused_bits = **p; |
HannesTschofenig | 0:796d0f61a05b | 203 | if( bs->unused_bits > 7 ) |
HannesTschofenig | 0:796d0f61a05b | 204 | return( POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
HannesTschofenig | 0:796d0f61a05b | 205 | (*p)++; |
HannesTschofenig | 0:796d0f61a05b | 206 | |
HannesTschofenig | 0:796d0f61a05b | 207 | /* Get actual bitstring */ |
HannesTschofenig | 0:796d0f61a05b | 208 | bs->p = *p; |
HannesTschofenig | 0:796d0f61a05b | 209 | *p += bs->len; |
HannesTschofenig | 0:796d0f61a05b | 210 | |
HannesTschofenig | 0:796d0f61a05b | 211 | if( *p != end ) |
HannesTschofenig | 0:796d0f61a05b | 212 | return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 213 | |
HannesTschofenig | 0:796d0f61a05b | 214 | return 0; |
HannesTschofenig | 0:796d0f61a05b | 215 | } |
HannesTschofenig | 0:796d0f61a05b | 216 | |
HannesTschofenig | 0:796d0f61a05b | 217 | /* |
HannesTschofenig | 0:796d0f61a05b | 218 | * Get a bit string without unused bits |
HannesTschofenig | 0:796d0f61a05b | 219 | */ |
HannesTschofenig | 0:796d0f61a05b | 220 | int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 221 | size_t *len ) |
HannesTschofenig | 0:796d0f61a05b | 222 | { |
HannesTschofenig | 0:796d0f61a05b | 223 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 224 | |
HannesTschofenig | 0:796d0f61a05b | 225 | if( ( ret = asn1_get_tag( p, end, len, ASN1_BIT_STRING ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 226 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 227 | |
HannesTschofenig | 0:796d0f61a05b | 228 | if( (*len)-- < 2 || *(*p)++ != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 229 | return( POLARSSL_ERR_ASN1_INVALID_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 230 | |
HannesTschofenig | 0:796d0f61a05b | 231 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 232 | } |
HannesTschofenig | 0:796d0f61a05b | 233 | |
HannesTschofenig | 0:796d0f61a05b | 234 | |
HannesTschofenig | 0:796d0f61a05b | 235 | |
HannesTschofenig | 0:796d0f61a05b | 236 | /* |
HannesTschofenig | 0:796d0f61a05b | 237 | * Parses and splits an ASN.1 "SEQUENCE OF <tag>" |
HannesTschofenig | 0:796d0f61a05b | 238 | */ |
HannesTschofenig | 0:796d0f61a05b | 239 | int asn1_get_sequence_of( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 240 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 241 | asn1_sequence *cur, |
HannesTschofenig | 0:796d0f61a05b | 242 | int tag) |
HannesTschofenig | 0:796d0f61a05b | 243 | { |
HannesTschofenig | 0:796d0f61a05b | 244 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 245 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 246 | asn1_buf *buf; |
HannesTschofenig | 0:796d0f61a05b | 247 | |
HannesTschofenig | 0:796d0f61a05b | 248 | /* Get main sequence tag */ |
HannesTschofenig | 0:796d0f61a05b | 249 | if( ( ret = asn1_get_tag( p, end, &len, |
HannesTschofenig | 0:796d0f61a05b | 250 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 251 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 252 | |
HannesTschofenig | 0:796d0f61a05b | 253 | if( *p + len != end ) |
HannesTschofenig | 0:796d0f61a05b | 254 | return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 255 | |
HannesTschofenig | 0:796d0f61a05b | 256 | while( *p < end ) |
HannesTschofenig | 0:796d0f61a05b | 257 | { |
HannesTschofenig | 0:796d0f61a05b | 258 | buf = &(cur->buf); |
HannesTschofenig | 0:796d0f61a05b | 259 | buf->tag = **p; |
HannesTschofenig | 0:796d0f61a05b | 260 | |
HannesTschofenig | 0:796d0f61a05b | 261 | if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 262 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 263 | |
HannesTschofenig | 0:796d0f61a05b | 264 | buf->p = *p; |
HannesTschofenig | 0:796d0f61a05b | 265 | *p += buf->len; |
HannesTschofenig | 0:796d0f61a05b | 266 | |
HannesTschofenig | 0:796d0f61a05b | 267 | /* Allocate and assign next pointer */ |
HannesTschofenig | 0:796d0f61a05b | 268 | if (*p < end) |
HannesTschofenig | 0:796d0f61a05b | 269 | { |
HannesTschofenig | 0:796d0f61a05b | 270 | cur->next = (asn1_sequence *) polarssl_malloc( |
HannesTschofenig | 0:796d0f61a05b | 271 | sizeof( asn1_sequence ) ); |
HannesTschofenig | 0:796d0f61a05b | 272 | |
HannesTschofenig | 0:796d0f61a05b | 273 | if( cur->next == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 274 | return( POLARSSL_ERR_ASN1_MALLOC_FAILED ); |
HannesTschofenig | 0:796d0f61a05b | 275 | |
HannesTschofenig | 0:796d0f61a05b | 276 | cur = cur->next; |
HannesTschofenig | 0:796d0f61a05b | 277 | } |
HannesTschofenig | 0:796d0f61a05b | 278 | } |
HannesTschofenig | 0:796d0f61a05b | 279 | |
HannesTschofenig | 0:796d0f61a05b | 280 | /* Set final sequence entry's next pointer to NULL */ |
HannesTschofenig | 0:796d0f61a05b | 281 | cur->next = NULL; |
HannesTschofenig | 0:796d0f61a05b | 282 | |
HannesTschofenig | 0:796d0f61a05b | 283 | if( *p != end ) |
HannesTschofenig | 0:796d0f61a05b | 284 | return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 285 | |
HannesTschofenig | 0:796d0f61a05b | 286 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 287 | } |
HannesTschofenig | 0:796d0f61a05b | 288 | |
HannesTschofenig | 0:796d0f61a05b | 289 | int asn1_get_alg( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 290 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 291 | asn1_buf *alg, asn1_buf *params ) |
HannesTschofenig | 0:796d0f61a05b | 292 | { |
HannesTschofenig | 0:796d0f61a05b | 293 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 294 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 295 | |
HannesTschofenig | 0:796d0f61a05b | 296 | if( ( ret = asn1_get_tag( p, end, &len, |
HannesTschofenig | 0:796d0f61a05b | 297 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 298 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 299 | |
HannesTschofenig | 0:796d0f61a05b | 300 | if( ( end - *p ) < 1 ) |
HannesTschofenig | 0:796d0f61a05b | 301 | return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 302 | |
HannesTschofenig | 0:796d0f61a05b | 303 | alg->tag = **p; |
HannesTschofenig | 0:796d0f61a05b | 304 | end = *p + len; |
HannesTschofenig | 0:796d0f61a05b | 305 | |
HannesTschofenig | 0:796d0f61a05b | 306 | if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 307 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 308 | |
HannesTschofenig | 0:796d0f61a05b | 309 | alg->p = *p; |
HannesTschofenig | 0:796d0f61a05b | 310 | *p += alg->len; |
HannesTschofenig | 0:796d0f61a05b | 311 | |
HannesTschofenig | 0:796d0f61a05b | 312 | if( *p == end ) |
HannesTschofenig | 0:796d0f61a05b | 313 | { |
HannesTschofenig | 0:796d0f61a05b | 314 | memset( params, 0, sizeof(asn1_buf) ); |
HannesTschofenig | 0:796d0f61a05b | 315 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 316 | } |
HannesTschofenig | 0:796d0f61a05b | 317 | |
HannesTschofenig | 0:796d0f61a05b | 318 | params->tag = **p; |
HannesTschofenig | 0:796d0f61a05b | 319 | (*p)++; |
HannesTschofenig | 0:796d0f61a05b | 320 | |
HannesTschofenig | 0:796d0f61a05b | 321 | if( ( ret = asn1_get_len( p, end, ¶ms->len ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 322 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 323 | |
HannesTschofenig | 0:796d0f61a05b | 324 | params->p = *p; |
HannesTschofenig | 0:796d0f61a05b | 325 | *p += params->len; |
HannesTschofenig | 0:796d0f61a05b | 326 | |
HannesTschofenig | 0:796d0f61a05b | 327 | if( *p != end ) |
HannesTschofenig | 0:796d0f61a05b | 328 | return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 329 | |
HannesTschofenig | 0:796d0f61a05b | 330 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 331 | } |
HannesTschofenig | 0:796d0f61a05b | 332 | |
HannesTschofenig | 0:796d0f61a05b | 333 | int asn1_get_alg_null( unsigned char **p, |
HannesTschofenig | 0:796d0f61a05b | 334 | const unsigned char *end, |
HannesTschofenig | 0:796d0f61a05b | 335 | asn1_buf *alg ) |
HannesTschofenig | 0:796d0f61a05b | 336 | { |
HannesTschofenig | 0:796d0f61a05b | 337 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 338 | asn1_buf params; |
HannesTschofenig | 0:796d0f61a05b | 339 | |
HannesTschofenig | 0:796d0f61a05b | 340 | memset( ¶ms, 0, sizeof(asn1_buf) ); |
HannesTschofenig | 0:796d0f61a05b | 341 | |
HannesTschofenig | 0:796d0f61a05b | 342 | if( ( ret = asn1_get_alg( p, end, alg, ¶ms ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 343 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 344 | |
HannesTschofenig | 0:796d0f61a05b | 345 | if( ( params.tag != ASN1_NULL && params.tag != 0 ) || params.len != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 346 | return( POLARSSL_ERR_ASN1_INVALID_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 347 | |
HannesTschofenig | 0:796d0f61a05b | 348 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 349 | } |
HannesTschofenig | 0:796d0f61a05b | 350 | |
HannesTschofenig | 0:796d0f61a05b | 351 | void asn1_free_named_data( asn1_named_data *cur ) |
HannesTschofenig | 0:796d0f61a05b | 352 | { |
HannesTschofenig | 0:796d0f61a05b | 353 | if( cur == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 354 | return; |
HannesTschofenig | 0:796d0f61a05b | 355 | |
HannesTschofenig | 0:796d0f61a05b | 356 | polarssl_free( cur->oid.p ); |
HannesTschofenig | 0:796d0f61a05b | 357 | polarssl_free( cur->val.p ); |
HannesTschofenig | 0:796d0f61a05b | 358 | |
HannesTschofenig | 0:796d0f61a05b | 359 | memset( cur, 0, sizeof( asn1_named_data ) ); |
HannesTschofenig | 0:796d0f61a05b | 360 | } |
HannesTschofenig | 0:796d0f61a05b | 361 | |
HannesTschofenig | 0:796d0f61a05b | 362 | void asn1_free_named_data_list( asn1_named_data **head ) |
HannesTschofenig | 0:796d0f61a05b | 363 | { |
HannesTschofenig | 0:796d0f61a05b | 364 | asn1_named_data *cur; |
HannesTschofenig | 0:796d0f61a05b | 365 | |
HannesTschofenig | 0:796d0f61a05b | 366 | while( ( cur = *head ) != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 367 | { |
HannesTschofenig | 0:796d0f61a05b | 368 | *head = cur->next; |
HannesTschofenig | 0:796d0f61a05b | 369 | asn1_free_named_data( cur ); |
HannesTschofenig | 0:796d0f61a05b | 370 | polarssl_free( cur ); |
HannesTschofenig | 0:796d0f61a05b | 371 | } |
HannesTschofenig | 0:796d0f61a05b | 372 | } |
HannesTschofenig | 0:796d0f61a05b | 373 | |
HannesTschofenig | 0:796d0f61a05b | 374 | asn1_named_data *asn1_find_named_data( asn1_named_data *list, |
HannesTschofenig | 0:796d0f61a05b | 375 | const char *oid, size_t len ) |
HannesTschofenig | 0:796d0f61a05b | 376 | { |
HannesTschofenig | 0:796d0f61a05b | 377 | while( list != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 378 | { |
HannesTschofenig | 0:796d0f61a05b | 379 | if( list->oid.len == len && |
HannesTschofenig | 0:796d0f61a05b | 380 | memcmp( list->oid.p, oid, len ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 381 | { |
HannesTschofenig | 0:796d0f61a05b | 382 | break; |
HannesTschofenig | 0:796d0f61a05b | 383 | } |
HannesTschofenig | 0:796d0f61a05b | 384 | |
HannesTschofenig | 0:796d0f61a05b | 385 | list = list->next; |
HannesTschofenig | 0:796d0f61a05b | 386 | } |
HannesTschofenig | 0:796d0f61a05b | 387 | |
HannesTschofenig | 0:796d0f61a05b | 388 | return( list ); |
HannesTschofenig | 0:796d0f61a05b | 389 | } |
HannesTschofenig | 0:796d0f61a05b | 390 | |
HannesTschofenig | 0:796d0f61a05b | 391 | #endif /* POLARSSL_ASN1_PARSE_C */ |
HannesTschofenig | 0:796d0f61a05b | 392 | |
HannesTschofenig | 0:796d0f61a05b | 393 |