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