Includes library modifications to allow access to AIN_4 (AIN_0 / 5)
mbd_os/features/mbedtls/src/asn1parse.c@0:eafc3fd41f75, 2016-09-20 (annotated)
- Committer:
- bryantaylor
- Date:
- Tue Sep 20 21:26:12 2016 +0000
- Revision:
- 0:eafc3fd41f75
hackathon
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bryantaylor | 0:eafc3fd41f75 | 1 | /* |
bryantaylor | 0:eafc3fd41f75 | 2 | * Generic ASN.1 parsing |
bryantaylor | 0:eafc3fd41f75 | 3 | * |
bryantaylor | 0:eafc3fd41f75 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
bryantaylor | 0:eafc3fd41f75 | 5 | * SPDX-License-Identifier: Apache-2.0 |
bryantaylor | 0:eafc3fd41f75 | 6 | * |
bryantaylor | 0:eafc3fd41f75 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
bryantaylor | 0:eafc3fd41f75 | 8 | * not use this file except in compliance with the License. |
bryantaylor | 0:eafc3fd41f75 | 9 | * You may obtain a copy of the License at |
bryantaylor | 0:eafc3fd41f75 | 10 | * |
bryantaylor | 0:eafc3fd41f75 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
bryantaylor | 0:eafc3fd41f75 | 12 | * |
bryantaylor | 0:eafc3fd41f75 | 13 | * Unless required by applicable law or agreed to in writing, software |
bryantaylor | 0:eafc3fd41f75 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
bryantaylor | 0:eafc3fd41f75 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bryantaylor | 0:eafc3fd41f75 | 16 | * See the License for the specific language governing permissions and |
bryantaylor | 0:eafc3fd41f75 | 17 | * limitations under the License. |
bryantaylor | 0:eafc3fd41f75 | 18 | * |
bryantaylor | 0:eafc3fd41f75 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
bryantaylor | 0:eafc3fd41f75 | 20 | */ |
bryantaylor | 0:eafc3fd41f75 | 21 | |
bryantaylor | 0:eafc3fd41f75 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
bryantaylor | 0:eafc3fd41f75 | 23 | #include "mbedtls/config.h" |
bryantaylor | 0:eafc3fd41f75 | 24 | #else |
bryantaylor | 0:eafc3fd41f75 | 25 | #include MBEDTLS_CONFIG_FILE |
bryantaylor | 0:eafc3fd41f75 | 26 | #endif |
bryantaylor | 0:eafc3fd41f75 | 27 | |
bryantaylor | 0:eafc3fd41f75 | 28 | #if defined(MBEDTLS_ASN1_PARSE_C) |
bryantaylor | 0:eafc3fd41f75 | 29 | |
bryantaylor | 0:eafc3fd41f75 | 30 | #include "mbedtls/asn1.h" |
bryantaylor | 0:eafc3fd41f75 | 31 | |
bryantaylor | 0:eafc3fd41f75 | 32 | #include <string.h> |
bryantaylor | 0:eafc3fd41f75 | 33 | |
bryantaylor | 0:eafc3fd41f75 | 34 | #if defined(MBEDTLS_BIGNUM_C) |
bryantaylor | 0:eafc3fd41f75 | 35 | #include "mbedtls/bignum.h" |
bryantaylor | 0:eafc3fd41f75 | 36 | #endif |
bryantaylor | 0:eafc3fd41f75 | 37 | |
bryantaylor | 0:eafc3fd41f75 | 38 | #if defined(MBEDTLS_PLATFORM_C) |
bryantaylor | 0:eafc3fd41f75 | 39 | #include "mbedtls/platform.h" |
bryantaylor | 0:eafc3fd41f75 | 40 | #else |
bryantaylor | 0:eafc3fd41f75 | 41 | #include <stdlib.h> |
bryantaylor | 0:eafc3fd41f75 | 42 | #define mbedtls_calloc calloc |
bryantaylor | 0:eafc3fd41f75 | 43 | #define mbedtls_free free |
bryantaylor | 0:eafc3fd41f75 | 44 | #endif |
bryantaylor | 0:eafc3fd41f75 | 45 | |
bryantaylor | 0:eafc3fd41f75 | 46 | /* Implementation that should never be optimized out by the compiler */ |
bryantaylor | 0:eafc3fd41f75 | 47 | static void mbedtls_zeroize( void *v, size_t n ) { |
bryantaylor | 0:eafc3fd41f75 | 48 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
bryantaylor | 0:eafc3fd41f75 | 49 | } |
bryantaylor | 0:eafc3fd41f75 | 50 | |
bryantaylor | 0:eafc3fd41f75 | 51 | /* |
bryantaylor | 0:eafc3fd41f75 | 52 | * ASN.1 DER decoding routines |
bryantaylor | 0:eafc3fd41f75 | 53 | */ |
bryantaylor | 0:eafc3fd41f75 | 54 | int mbedtls_asn1_get_len( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 55 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 56 | size_t *len ) |
bryantaylor | 0:eafc3fd41f75 | 57 | { |
bryantaylor | 0:eafc3fd41f75 | 58 | if( ( end - *p ) < 1 ) |
bryantaylor | 0:eafc3fd41f75 | 59 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 60 | |
bryantaylor | 0:eafc3fd41f75 | 61 | if( ( **p & 0x80 ) == 0 ) |
bryantaylor | 0:eafc3fd41f75 | 62 | *len = *(*p)++; |
bryantaylor | 0:eafc3fd41f75 | 63 | else |
bryantaylor | 0:eafc3fd41f75 | 64 | { |
bryantaylor | 0:eafc3fd41f75 | 65 | switch( **p & 0x7F ) |
bryantaylor | 0:eafc3fd41f75 | 66 | { |
bryantaylor | 0:eafc3fd41f75 | 67 | case 1: |
bryantaylor | 0:eafc3fd41f75 | 68 | if( ( end - *p ) < 2 ) |
bryantaylor | 0:eafc3fd41f75 | 69 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 70 | |
bryantaylor | 0:eafc3fd41f75 | 71 | *len = (*p)[1]; |
bryantaylor | 0:eafc3fd41f75 | 72 | (*p) += 2; |
bryantaylor | 0:eafc3fd41f75 | 73 | break; |
bryantaylor | 0:eafc3fd41f75 | 74 | |
bryantaylor | 0:eafc3fd41f75 | 75 | case 2: |
bryantaylor | 0:eafc3fd41f75 | 76 | if( ( end - *p ) < 3 ) |
bryantaylor | 0:eafc3fd41f75 | 77 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 78 | |
bryantaylor | 0:eafc3fd41f75 | 79 | *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2]; |
bryantaylor | 0:eafc3fd41f75 | 80 | (*p) += 3; |
bryantaylor | 0:eafc3fd41f75 | 81 | break; |
bryantaylor | 0:eafc3fd41f75 | 82 | |
bryantaylor | 0:eafc3fd41f75 | 83 | case 3: |
bryantaylor | 0:eafc3fd41f75 | 84 | if( ( end - *p ) < 4 ) |
bryantaylor | 0:eafc3fd41f75 | 85 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 86 | |
bryantaylor | 0:eafc3fd41f75 | 87 | *len = ( (size_t)(*p)[1] << 16 ) | |
bryantaylor | 0:eafc3fd41f75 | 88 | ( (size_t)(*p)[2] << 8 ) | (*p)[3]; |
bryantaylor | 0:eafc3fd41f75 | 89 | (*p) += 4; |
bryantaylor | 0:eafc3fd41f75 | 90 | break; |
bryantaylor | 0:eafc3fd41f75 | 91 | |
bryantaylor | 0:eafc3fd41f75 | 92 | case 4: |
bryantaylor | 0:eafc3fd41f75 | 93 | if( ( end - *p ) < 5 ) |
bryantaylor | 0:eafc3fd41f75 | 94 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 95 | |
bryantaylor | 0:eafc3fd41f75 | 96 | *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) | |
bryantaylor | 0:eafc3fd41f75 | 97 | ( (size_t)(*p)[3] << 8 ) | (*p)[4]; |
bryantaylor | 0:eafc3fd41f75 | 98 | (*p) += 5; |
bryantaylor | 0:eafc3fd41f75 | 99 | break; |
bryantaylor | 0:eafc3fd41f75 | 100 | |
bryantaylor | 0:eafc3fd41f75 | 101 | default: |
bryantaylor | 0:eafc3fd41f75 | 102 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
bryantaylor | 0:eafc3fd41f75 | 103 | } |
bryantaylor | 0:eafc3fd41f75 | 104 | } |
bryantaylor | 0:eafc3fd41f75 | 105 | |
bryantaylor | 0:eafc3fd41f75 | 106 | if( *len > (size_t) ( end - *p ) ) |
bryantaylor | 0:eafc3fd41f75 | 107 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 108 | |
bryantaylor | 0:eafc3fd41f75 | 109 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 110 | } |
bryantaylor | 0:eafc3fd41f75 | 111 | |
bryantaylor | 0:eafc3fd41f75 | 112 | int mbedtls_asn1_get_tag( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 113 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 114 | size_t *len, int tag ) |
bryantaylor | 0:eafc3fd41f75 | 115 | { |
bryantaylor | 0:eafc3fd41f75 | 116 | if( ( end - *p ) < 1 ) |
bryantaylor | 0:eafc3fd41f75 | 117 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 118 | |
bryantaylor | 0:eafc3fd41f75 | 119 | if( **p != tag ) |
bryantaylor | 0:eafc3fd41f75 | 120 | return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
bryantaylor | 0:eafc3fd41f75 | 121 | |
bryantaylor | 0:eafc3fd41f75 | 122 | (*p)++; |
bryantaylor | 0:eafc3fd41f75 | 123 | |
bryantaylor | 0:eafc3fd41f75 | 124 | return( mbedtls_asn1_get_len( p, end, len ) ); |
bryantaylor | 0:eafc3fd41f75 | 125 | } |
bryantaylor | 0:eafc3fd41f75 | 126 | |
bryantaylor | 0:eafc3fd41f75 | 127 | int mbedtls_asn1_get_bool( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 128 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 129 | int *val ) |
bryantaylor | 0:eafc3fd41f75 | 130 | { |
bryantaylor | 0:eafc3fd41f75 | 131 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 132 | size_t len; |
bryantaylor | 0:eafc3fd41f75 | 133 | |
bryantaylor | 0:eafc3fd41f75 | 134 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 135 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 136 | |
bryantaylor | 0:eafc3fd41f75 | 137 | if( len != 1 ) |
bryantaylor | 0:eafc3fd41f75 | 138 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
bryantaylor | 0:eafc3fd41f75 | 139 | |
bryantaylor | 0:eafc3fd41f75 | 140 | *val = ( **p != 0 ) ? 1 : 0; |
bryantaylor | 0:eafc3fd41f75 | 141 | (*p)++; |
bryantaylor | 0:eafc3fd41f75 | 142 | |
bryantaylor | 0:eafc3fd41f75 | 143 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 144 | } |
bryantaylor | 0:eafc3fd41f75 | 145 | |
bryantaylor | 0:eafc3fd41f75 | 146 | int mbedtls_asn1_get_int( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 147 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 148 | int *val ) |
bryantaylor | 0:eafc3fd41f75 | 149 | { |
bryantaylor | 0:eafc3fd41f75 | 150 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 151 | size_t len; |
bryantaylor | 0:eafc3fd41f75 | 152 | |
bryantaylor | 0:eafc3fd41f75 | 153 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 154 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 155 | |
bryantaylor | 0:eafc3fd41f75 | 156 | if( len > sizeof( int ) || ( **p & 0x80 ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 157 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
bryantaylor | 0:eafc3fd41f75 | 158 | |
bryantaylor | 0:eafc3fd41f75 | 159 | *val = 0; |
bryantaylor | 0:eafc3fd41f75 | 160 | |
bryantaylor | 0:eafc3fd41f75 | 161 | while( len-- > 0 ) |
bryantaylor | 0:eafc3fd41f75 | 162 | { |
bryantaylor | 0:eafc3fd41f75 | 163 | *val = ( *val << 8 ) | **p; |
bryantaylor | 0:eafc3fd41f75 | 164 | (*p)++; |
bryantaylor | 0:eafc3fd41f75 | 165 | } |
bryantaylor | 0:eafc3fd41f75 | 166 | |
bryantaylor | 0:eafc3fd41f75 | 167 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 168 | } |
bryantaylor | 0:eafc3fd41f75 | 169 | |
bryantaylor | 0:eafc3fd41f75 | 170 | #if defined(MBEDTLS_BIGNUM_C) |
bryantaylor | 0:eafc3fd41f75 | 171 | int mbedtls_asn1_get_mpi( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 172 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 173 | mbedtls_mpi *X ) |
bryantaylor | 0:eafc3fd41f75 | 174 | { |
bryantaylor | 0:eafc3fd41f75 | 175 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 176 | size_t len; |
bryantaylor | 0:eafc3fd41f75 | 177 | |
bryantaylor | 0:eafc3fd41f75 | 178 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 179 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 180 | |
bryantaylor | 0:eafc3fd41f75 | 181 | ret = mbedtls_mpi_read_binary( X, *p, len ); |
bryantaylor | 0:eafc3fd41f75 | 182 | |
bryantaylor | 0:eafc3fd41f75 | 183 | *p += len; |
bryantaylor | 0:eafc3fd41f75 | 184 | |
bryantaylor | 0:eafc3fd41f75 | 185 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 186 | } |
bryantaylor | 0:eafc3fd41f75 | 187 | #endif /* MBEDTLS_BIGNUM_C */ |
bryantaylor | 0:eafc3fd41f75 | 188 | |
bryantaylor | 0:eafc3fd41f75 | 189 | int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 190 | mbedtls_asn1_bitstring *bs) |
bryantaylor | 0:eafc3fd41f75 | 191 | { |
bryantaylor | 0:eafc3fd41f75 | 192 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 193 | |
bryantaylor | 0:eafc3fd41f75 | 194 | /* Certificate type is a single byte bitstring */ |
bryantaylor | 0:eafc3fd41f75 | 195 | if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 196 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 197 | |
bryantaylor | 0:eafc3fd41f75 | 198 | /* Check length, subtract one for actual bit string length */ |
bryantaylor | 0:eafc3fd41f75 | 199 | if( bs->len < 1 ) |
bryantaylor | 0:eafc3fd41f75 | 200 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 201 | bs->len -= 1; |
bryantaylor | 0:eafc3fd41f75 | 202 | |
bryantaylor | 0:eafc3fd41f75 | 203 | /* Get number of unused bits, ensure unused bits <= 7 */ |
bryantaylor | 0:eafc3fd41f75 | 204 | bs->unused_bits = **p; |
bryantaylor | 0:eafc3fd41f75 | 205 | if( bs->unused_bits > 7 ) |
bryantaylor | 0:eafc3fd41f75 | 206 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
bryantaylor | 0:eafc3fd41f75 | 207 | (*p)++; |
bryantaylor | 0:eafc3fd41f75 | 208 | |
bryantaylor | 0:eafc3fd41f75 | 209 | /* Get actual bitstring */ |
bryantaylor | 0:eafc3fd41f75 | 210 | bs->p = *p; |
bryantaylor | 0:eafc3fd41f75 | 211 | *p += bs->len; |
bryantaylor | 0:eafc3fd41f75 | 212 | |
bryantaylor | 0:eafc3fd41f75 | 213 | if( *p != end ) |
bryantaylor | 0:eafc3fd41f75 | 214 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
bryantaylor | 0:eafc3fd41f75 | 215 | |
bryantaylor | 0:eafc3fd41f75 | 216 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 217 | } |
bryantaylor | 0:eafc3fd41f75 | 218 | |
bryantaylor | 0:eafc3fd41f75 | 219 | /* |
bryantaylor | 0:eafc3fd41f75 | 220 | * Get a bit string without unused bits |
bryantaylor | 0:eafc3fd41f75 | 221 | */ |
bryantaylor | 0:eafc3fd41f75 | 222 | int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 223 | size_t *len ) |
bryantaylor | 0:eafc3fd41f75 | 224 | { |
bryantaylor | 0:eafc3fd41f75 | 225 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 226 | |
bryantaylor | 0:eafc3fd41f75 | 227 | if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 228 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 229 | |
bryantaylor | 0:eafc3fd41f75 | 230 | if( (*len)-- < 2 || *(*p)++ != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 231 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 232 | |
bryantaylor | 0:eafc3fd41f75 | 233 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 234 | } |
bryantaylor | 0:eafc3fd41f75 | 235 | |
bryantaylor | 0:eafc3fd41f75 | 236 | |
bryantaylor | 0:eafc3fd41f75 | 237 | |
bryantaylor | 0:eafc3fd41f75 | 238 | /* |
bryantaylor | 0:eafc3fd41f75 | 239 | * Parses and splits an ASN.1 "SEQUENCE OF <tag>" |
bryantaylor | 0:eafc3fd41f75 | 240 | */ |
bryantaylor | 0:eafc3fd41f75 | 241 | int mbedtls_asn1_get_sequence_of( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 242 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 243 | mbedtls_asn1_sequence *cur, |
bryantaylor | 0:eafc3fd41f75 | 244 | int tag) |
bryantaylor | 0:eafc3fd41f75 | 245 | { |
bryantaylor | 0:eafc3fd41f75 | 246 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 247 | size_t len; |
bryantaylor | 0:eafc3fd41f75 | 248 | mbedtls_asn1_buf *buf; |
bryantaylor | 0:eafc3fd41f75 | 249 | |
bryantaylor | 0:eafc3fd41f75 | 250 | /* Get main sequence tag */ |
bryantaylor | 0:eafc3fd41f75 | 251 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
bryantaylor | 0:eafc3fd41f75 | 252 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 253 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 254 | |
bryantaylor | 0:eafc3fd41f75 | 255 | if( *p + len != end ) |
bryantaylor | 0:eafc3fd41f75 | 256 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
bryantaylor | 0:eafc3fd41f75 | 257 | |
bryantaylor | 0:eafc3fd41f75 | 258 | while( *p < end ) |
bryantaylor | 0:eafc3fd41f75 | 259 | { |
bryantaylor | 0:eafc3fd41f75 | 260 | buf = &(cur->buf); |
bryantaylor | 0:eafc3fd41f75 | 261 | buf->tag = **p; |
bryantaylor | 0:eafc3fd41f75 | 262 | |
bryantaylor | 0:eafc3fd41f75 | 263 | if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 264 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 265 | |
bryantaylor | 0:eafc3fd41f75 | 266 | buf->p = *p; |
bryantaylor | 0:eafc3fd41f75 | 267 | *p += buf->len; |
bryantaylor | 0:eafc3fd41f75 | 268 | |
bryantaylor | 0:eafc3fd41f75 | 269 | /* Allocate and assign next pointer */ |
bryantaylor | 0:eafc3fd41f75 | 270 | if( *p < end ) |
bryantaylor | 0:eafc3fd41f75 | 271 | { |
bryantaylor | 0:eafc3fd41f75 | 272 | cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1, |
bryantaylor | 0:eafc3fd41f75 | 273 | sizeof( mbedtls_asn1_sequence ) ); |
bryantaylor | 0:eafc3fd41f75 | 274 | |
bryantaylor | 0:eafc3fd41f75 | 275 | if( cur->next == NULL ) |
bryantaylor | 0:eafc3fd41f75 | 276 | return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); |
bryantaylor | 0:eafc3fd41f75 | 277 | |
bryantaylor | 0:eafc3fd41f75 | 278 | cur = cur->next; |
bryantaylor | 0:eafc3fd41f75 | 279 | } |
bryantaylor | 0:eafc3fd41f75 | 280 | } |
bryantaylor | 0:eafc3fd41f75 | 281 | |
bryantaylor | 0:eafc3fd41f75 | 282 | /* Set final sequence entry's next pointer to NULL */ |
bryantaylor | 0:eafc3fd41f75 | 283 | cur->next = NULL; |
bryantaylor | 0:eafc3fd41f75 | 284 | |
bryantaylor | 0:eafc3fd41f75 | 285 | if( *p != end ) |
bryantaylor | 0:eafc3fd41f75 | 286 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
bryantaylor | 0:eafc3fd41f75 | 287 | |
bryantaylor | 0:eafc3fd41f75 | 288 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 289 | } |
bryantaylor | 0:eafc3fd41f75 | 290 | |
bryantaylor | 0:eafc3fd41f75 | 291 | int mbedtls_asn1_get_alg( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 292 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 293 | mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params ) |
bryantaylor | 0:eafc3fd41f75 | 294 | { |
bryantaylor | 0:eafc3fd41f75 | 295 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 296 | size_t len; |
bryantaylor | 0:eafc3fd41f75 | 297 | |
bryantaylor | 0:eafc3fd41f75 | 298 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
bryantaylor | 0:eafc3fd41f75 | 299 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 300 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 301 | |
bryantaylor | 0:eafc3fd41f75 | 302 | if( ( end - *p ) < 1 ) |
bryantaylor | 0:eafc3fd41f75 | 303 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 304 | |
bryantaylor | 0:eafc3fd41f75 | 305 | alg->tag = **p; |
bryantaylor | 0:eafc3fd41f75 | 306 | end = *p + len; |
bryantaylor | 0:eafc3fd41f75 | 307 | |
bryantaylor | 0:eafc3fd41f75 | 308 | if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 309 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 310 | |
bryantaylor | 0:eafc3fd41f75 | 311 | alg->p = *p; |
bryantaylor | 0:eafc3fd41f75 | 312 | *p += alg->len; |
bryantaylor | 0:eafc3fd41f75 | 313 | |
bryantaylor | 0:eafc3fd41f75 | 314 | if( *p == end ) |
bryantaylor | 0:eafc3fd41f75 | 315 | { |
bryantaylor | 0:eafc3fd41f75 | 316 | mbedtls_zeroize( params, sizeof(mbedtls_asn1_buf) ); |
bryantaylor | 0:eafc3fd41f75 | 317 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 318 | } |
bryantaylor | 0:eafc3fd41f75 | 319 | |
bryantaylor | 0:eafc3fd41f75 | 320 | params->tag = **p; |
bryantaylor | 0:eafc3fd41f75 | 321 | (*p)++; |
bryantaylor | 0:eafc3fd41f75 | 322 | |
bryantaylor | 0:eafc3fd41f75 | 323 | if( ( ret = mbedtls_asn1_get_len( p, end, ¶ms->len ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 324 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 325 | |
bryantaylor | 0:eafc3fd41f75 | 326 | params->p = *p; |
bryantaylor | 0:eafc3fd41f75 | 327 | *p += params->len; |
bryantaylor | 0:eafc3fd41f75 | 328 | |
bryantaylor | 0:eafc3fd41f75 | 329 | if( *p != end ) |
bryantaylor | 0:eafc3fd41f75 | 330 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
bryantaylor | 0:eafc3fd41f75 | 331 | |
bryantaylor | 0:eafc3fd41f75 | 332 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 333 | } |
bryantaylor | 0:eafc3fd41f75 | 334 | |
bryantaylor | 0:eafc3fd41f75 | 335 | int mbedtls_asn1_get_alg_null( unsigned char **p, |
bryantaylor | 0:eafc3fd41f75 | 336 | const unsigned char *end, |
bryantaylor | 0:eafc3fd41f75 | 337 | mbedtls_asn1_buf *alg ) |
bryantaylor | 0:eafc3fd41f75 | 338 | { |
bryantaylor | 0:eafc3fd41f75 | 339 | int ret; |
bryantaylor | 0:eafc3fd41f75 | 340 | mbedtls_asn1_buf params; |
bryantaylor | 0:eafc3fd41f75 | 341 | |
bryantaylor | 0:eafc3fd41f75 | 342 | memset( ¶ms, 0, sizeof(mbedtls_asn1_buf) ); |
bryantaylor | 0:eafc3fd41f75 | 343 | |
bryantaylor | 0:eafc3fd41f75 | 344 | if( ( ret = mbedtls_asn1_get_alg( p, end, alg, ¶ms ) ) != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 345 | return( ret ); |
bryantaylor | 0:eafc3fd41f75 | 346 | |
bryantaylor | 0:eafc3fd41f75 | 347 | if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 ) |
bryantaylor | 0:eafc3fd41f75 | 348 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
bryantaylor | 0:eafc3fd41f75 | 349 | |
bryantaylor | 0:eafc3fd41f75 | 350 | return( 0 ); |
bryantaylor | 0:eafc3fd41f75 | 351 | } |
bryantaylor | 0:eafc3fd41f75 | 352 | |
bryantaylor | 0:eafc3fd41f75 | 353 | void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur ) |
bryantaylor | 0:eafc3fd41f75 | 354 | { |
bryantaylor | 0:eafc3fd41f75 | 355 | if( cur == NULL ) |
bryantaylor | 0:eafc3fd41f75 | 356 | return; |
bryantaylor | 0:eafc3fd41f75 | 357 | |
bryantaylor | 0:eafc3fd41f75 | 358 | mbedtls_free( cur->oid.p ); |
bryantaylor | 0:eafc3fd41f75 | 359 | mbedtls_free( cur->val.p ); |
bryantaylor | 0:eafc3fd41f75 | 360 | |
bryantaylor | 0:eafc3fd41f75 | 361 | mbedtls_zeroize( cur, sizeof( mbedtls_asn1_named_data ) ); |
bryantaylor | 0:eafc3fd41f75 | 362 | } |
bryantaylor | 0:eafc3fd41f75 | 363 | |
bryantaylor | 0:eafc3fd41f75 | 364 | void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head ) |
bryantaylor | 0:eafc3fd41f75 | 365 | { |
bryantaylor | 0:eafc3fd41f75 | 366 | mbedtls_asn1_named_data *cur; |
bryantaylor | 0:eafc3fd41f75 | 367 | |
bryantaylor | 0:eafc3fd41f75 | 368 | while( ( cur = *head ) != NULL ) |
bryantaylor | 0:eafc3fd41f75 | 369 | { |
bryantaylor | 0:eafc3fd41f75 | 370 | *head = cur->next; |
bryantaylor | 0:eafc3fd41f75 | 371 | mbedtls_asn1_free_named_data( cur ); |
bryantaylor | 0:eafc3fd41f75 | 372 | mbedtls_free( cur ); |
bryantaylor | 0:eafc3fd41f75 | 373 | } |
bryantaylor | 0:eafc3fd41f75 | 374 | } |
bryantaylor | 0:eafc3fd41f75 | 375 | |
bryantaylor | 0:eafc3fd41f75 | 376 | mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list, |
bryantaylor | 0:eafc3fd41f75 | 377 | const char *oid, size_t len ) |
bryantaylor | 0:eafc3fd41f75 | 378 | { |
bryantaylor | 0:eafc3fd41f75 | 379 | while( list != NULL ) |
bryantaylor | 0:eafc3fd41f75 | 380 | { |
bryantaylor | 0:eafc3fd41f75 | 381 | if( list->oid.len == len && |
bryantaylor | 0:eafc3fd41f75 | 382 | memcmp( list->oid.p, oid, len ) == 0 ) |
bryantaylor | 0:eafc3fd41f75 | 383 | { |
bryantaylor | 0:eafc3fd41f75 | 384 | break; |
bryantaylor | 0:eafc3fd41f75 | 385 | } |
bryantaylor | 0:eafc3fd41f75 | 386 | |
bryantaylor | 0:eafc3fd41f75 | 387 | list = list->next; |
bryantaylor | 0:eafc3fd41f75 | 388 | } |
bryantaylor | 0:eafc3fd41f75 | 389 | |
bryantaylor | 0:eafc3fd41f75 | 390 | return( list ); |
bryantaylor | 0:eafc3fd41f75 | 391 | } |
bryantaylor | 0:eafc3fd41f75 | 392 | |
bryantaylor | 0:eafc3fd41f75 | 393 | #endif /* MBEDTLS_ASN1_PARSE_C */ |