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