mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/mbedtls/mbed-crypto/src/asn1parse.c@0:5b88d5760320, 2019-12-17 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 17 23:23:45 2019 +0000
- Revision:
- 0:5b88d5760320
- Child:
- 1:9db0e321a9f4
mbed-os5 only for TYBLE16
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 | 0:5b88d5760320 | 152 | if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 153 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
kenjiArai | 0:5b88d5760320 | 154 | |
kenjiArai | 0:5b88d5760320 | 155 | *val = 0; |
kenjiArai | 0:5b88d5760320 | 156 | |
kenjiArai | 0:5b88d5760320 | 157 | while( len-- > 0 ) |
kenjiArai | 0:5b88d5760320 | 158 | { |
kenjiArai | 0:5b88d5760320 | 159 | *val = ( *val << 8 ) | **p; |
kenjiArai | 0:5b88d5760320 | 160 | (*p)++; |
kenjiArai | 0:5b88d5760320 | 161 | } |
kenjiArai | 0:5b88d5760320 | 162 | |
kenjiArai | 0:5b88d5760320 | 163 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 164 | } |
kenjiArai | 0:5b88d5760320 | 165 | |
kenjiArai | 0:5b88d5760320 | 166 | #if defined(MBEDTLS_BIGNUM_C) |
kenjiArai | 0:5b88d5760320 | 167 | int mbedtls_asn1_get_mpi( unsigned char **p, |
kenjiArai | 0:5b88d5760320 | 168 | const unsigned char *end, |
kenjiArai | 0:5b88d5760320 | 169 | mbedtls_mpi *X ) |
kenjiArai | 0:5b88d5760320 | 170 | { |
kenjiArai | 0:5b88d5760320 | 171 | int ret; |
kenjiArai | 0:5b88d5760320 | 172 | size_t len; |
kenjiArai | 0:5b88d5760320 | 173 | |
kenjiArai | 0:5b88d5760320 | 174 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 175 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 176 | |
kenjiArai | 0:5b88d5760320 | 177 | ret = mbedtls_mpi_read_binary( X, *p, len ); |
kenjiArai | 0:5b88d5760320 | 178 | |
kenjiArai | 0:5b88d5760320 | 179 | *p += len; |
kenjiArai | 0:5b88d5760320 | 180 | |
kenjiArai | 0:5b88d5760320 | 181 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 182 | } |
kenjiArai | 0:5b88d5760320 | 183 | #endif /* MBEDTLS_BIGNUM_C */ |
kenjiArai | 0:5b88d5760320 | 184 | |
kenjiArai | 0:5b88d5760320 | 185 | int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end, |
kenjiArai | 0:5b88d5760320 | 186 | mbedtls_asn1_bitstring *bs) |
kenjiArai | 0:5b88d5760320 | 187 | { |
kenjiArai | 0:5b88d5760320 | 188 | int ret; |
kenjiArai | 0:5b88d5760320 | 189 | |
kenjiArai | 0:5b88d5760320 | 190 | /* Certificate type is a single byte bitstring */ |
kenjiArai | 0:5b88d5760320 | 191 | if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 192 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 193 | |
kenjiArai | 0:5b88d5760320 | 194 | /* Check length, subtract one for actual bit string length */ |
kenjiArai | 0:5b88d5760320 | 195 | if( bs->len < 1 ) |
kenjiArai | 0:5b88d5760320 | 196 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
kenjiArai | 0:5b88d5760320 | 197 | bs->len -= 1; |
kenjiArai | 0:5b88d5760320 | 198 | |
kenjiArai | 0:5b88d5760320 | 199 | /* Get number of unused bits, ensure unused bits <= 7 */ |
kenjiArai | 0:5b88d5760320 | 200 | bs->unused_bits = **p; |
kenjiArai | 0:5b88d5760320 | 201 | if( bs->unused_bits > 7 ) |
kenjiArai | 0:5b88d5760320 | 202 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
kenjiArai | 0:5b88d5760320 | 203 | (*p)++; |
kenjiArai | 0:5b88d5760320 | 204 | |
kenjiArai | 0:5b88d5760320 | 205 | /* Get actual bitstring */ |
kenjiArai | 0:5b88d5760320 | 206 | bs->p = *p; |
kenjiArai | 0:5b88d5760320 | 207 | *p += bs->len; |
kenjiArai | 0:5b88d5760320 | 208 | |
kenjiArai | 0:5b88d5760320 | 209 | if( *p != end ) |
kenjiArai | 0:5b88d5760320 | 210 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
kenjiArai | 0:5b88d5760320 | 211 | |
kenjiArai | 0:5b88d5760320 | 212 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 213 | } |
kenjiArai | 0:5b88d5760320 | 214 | |
kenjiArai | 0:5b88d5760320 | 215 | /* |
kenjiArai | 0:5b88d5760320 | 216 | * Get a bit string without unused bits |
kenjiArai | 0:5b88d5760320 | 217 | */ |
kenjiArai | 0:5b88d5760320 | 218 | int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end, |
kenjiArai | 0:5b88d5760320 | 219 | size_t *len ) |
kenjiArai | 0:5b88d5760320 | 220 | { |
kenjiArai | 0:5b88d5760320 | 221 | int ret; |
kenjiArai | 0:5b88d5760320 | 222 | |
kenjiArai | 0:5b88d5760320 | 223 | if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 224 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 225 | |
kenjiArai | 0:5b88d5760320 | 226 | if( (*len)-- < 2 || *(*p)++ != 0 ) |
kenjiArai | 0:5b88d5760320 | 227 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
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 | |
kenjiArai | 0:5b88d5760320 | 234 | /* |
kenjiArai | 0:5b88d5760320 | 235 | * Parses and splits an ASN.1 "SEQUENCE OF <tag>" |
kenjiArai | 0:5b88d5760320 | 236 | */ |
kenjiArai | 0:5b88d5760320 | 237 | int mbedtls_asn1_get_sequence_of( unsigned char **p, |
kenjiArai | 0:5b88d5760320 | 238 | const unsigned char *end, |
kenjiArai | 0:5b88d5760320 | 239 | mbedtls_asn1_sequence *cur, |
kenjiArai | 0:5b88d5760320 | 240 | int tag) |
kenjiArai | 0:5b88d5760320 | 241 | { |
kenjiArai | 0:5b88d5760320 | 242 | int ret; |
kenjiArai | 0:5b88d5760320 | 243 | size_t len; |
kenjiArai | 0:5b88d5760320 | 244 | mbedtls_asn1_buf *buf; |
kenjiArai | 0:5b88d5760320 | 245 | |
kenjiArai | 0:5b88d5760320 | 246 | /* Get main sequence tag */ |
kenjiArai | 0:5b88d5760320 | 247 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
kenjiArai | 0:5b88d5760320 | 248 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 249 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 250 | |
kenjiArai | 0:5b88d5760320 | 251 | if( *p + len != end ) |
kenjiArai | 0:5b88d5760320 | 252 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
kenjiArai | 0:5b88d5760320 | 253 | |
kenjiArai | 0:5b88d5760320 | 254 | while( *p < end ) |
kenjiArai | 0:5b88d5760320 | 255 | { |
kenjiArai | 0:5b88d5760320 | 256 | buf = &(cur->buf); |
kenjiArai | 0:5b88d5760320 | 257 | buf->tag = **p; |
kenjiArai | 0:5b88d5760320 | 258 | |
kenjiArai | 0:5b88d5760320 | 259 | if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 260 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 261 | |
kenjiArai | 0:5b88d5760320 | 262 | buf->p = *p; |
kenjiArai | 0:5b88d5760320 | 263 | *p += buf->len; |
kenjiArai | 0:5b88d5760320 | 264 | |
kenjiArai | 0:5b88d5760320 | 265 | /* Allocate and assign next pointer */ |
kenjiArai | 0:5b88d5760320 | 266 | if( *p < end ) |
kenjiArai | 0:5b88d5760320 | 267 | { |
kenjiArai | 0:5b88d5760320 | 268 | cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1, |
kenjiArai | 0:5b88d5760320 | 269 | sizeof( mbedtls_asn1_sequence ) ); |
kenjiArai | 0:5b88d5760320 | 270 | |
kenjiArai | 0:5b88d5760320 | 271 | if( cur->next == NULL ) |
kenjiArai | 0:5b88d5760320 | 272 | return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); |
kenjiArai | 0:5b88d5760320 | 273 | |
kenjiArai | 0:5b88d5760320 | 274 | cur = cur->next; |
kenjiArai | 0:5b88d5760320 | 275 | } |
kenjiArai | 0:5b88d5760320 | 276 | } |
kenjiArai | 0:5b88d5760320 | 277 | |
kenjiArai | 0:5b88d5760320 | 278 | /* Set final sequence entry's next pointer to NULL */ |
kenjiArai | 0:5b88d5760320 | 279 | cur->next = NULL; |
kenjiArai | 0:5b88d5760320 | 280 | |
kenjiArai | 0:5b88d5760320 | 281 | if( *p != end ) |
kenjiArai | 0:5b88d5760320 | 282 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
kenjiArai | 0:5b88d5760320 | 283 | |
kenjiArai | 0:5b88d5760320 | 284 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 285 | } |
kenjiArai | 0:5b88d5760320 | 286 | |
kenjiArai | 0:5b88d5760320 | 287 | int mbedtls_asn1_get_alg( unsigned char **p, |
kenjiArai | 0:5b88d5760320 | 288 | const unsigned char *end, |
kenjiArai | 0:5b88d5760320 | 289 | mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params ) |
kenjiArai | 0:5b88d5760320 | 290 | { |
kenjiArai | 0:5b88d5760320 | 291 | int ret; |
kenjiArai | 0:5b88d5760320 | 292 | size_t len; |
kenjiArai | 0:5b88d5760320 | 293 | |
kenjiArai | 0:5b88d5760320 | 294 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
kenjiArai | 0:5b88d5760320 | 295 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 296 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 297 | |
kenjiArai | 0:5b88d5760320 | 298 | if( ( end - *p ) < 1 ) |
kenjiArai | 0:5b88d5760320 | 299 | return( MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
kenjiArai | 0:5b88d5760320 | 300 | |
kenjiArai | 0:5b88d5760320 | 301 | alg->tag = **p; |
kenjiArai | 0:5b88d5760320 | 302 | end = *p + len; |
kenjiArai | 0:5b88d5760320 | 303 | |
kenjiArai | 0:5b88d5760320 | 304 | if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 305 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 306 | |
kenjiArai | 0:5b88d5760320 | 307 | alg->p = *p; |
kenjiArai | 0:5b88d5760320 | 308 | *p += alg->len; |
kenjiArai | 0:5b88d5760320 | 309 | |
kenjiArai | 0:5b88d5760320 | 310 | if( *p == end ) |
kenjiArai | 0:5b88d5760320 | 311 | { |
kenjiArai | 0:5b88d5760320 | 312 | mbedtls_platform_zeroize( params, sizeof(mbedtls_asn1_buf) ); |
kenjiArai | 0:5b88d5760320 | 313 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 314 | } |
kenjiArai | 0:5b88d5760320 | 315 | |
kenjiArai | 0:5b88d5760320 | 316 | params->tag = **p; |
kenjiArai | 0:5b88d5760320 | 317 | (*p)++; |
kenjiArai | 0:5b88d5760320 | 318 | |
kenjiArai | 0:5b88d5760320 | 319 | if( ( ret = mbedtls_asn1_get_len( p, end, ¶ms->len ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 320 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 321 | |
kenjiArai | 0:5b88d5760320 | 322 | params->p = *p; |
kenjiArai | 0:5b88d5760320 | 323 | *p += params->len; |
kenjiArai | 0:5b88d5760320 | 324 | |
kenjiArai | 0:5b88d5760320 | 325 | if( *p != end ) |
kenjiArai | 0:5b88d5760320 | 326 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
kenjiArai | 0:5b88d5760320 | 327 | |
kenjiArai | 0:5b88d5760320 | 328 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 329 | } |
kenjiArai | 0:5b88d5760320 | 330 | |
kenjiArai | 0:5b88d5760320 | 331 | int mbedtls_asn1_get_alg_null( unsigned char **p, |
kenjiArai | 0:5b88d5760320 | 332 | const unsigned char *end, |
kenjiArai | 0:5b88d5760320 | 333 | mbedtls_asn1_buf *alg ) |
kenjiArai | 0:5b88d5760320 | 334 | { |
kenjiArai | 0:5b88d5760320 | 335 | int ret; |
kenjiArai | 0:5b88d5760320 | 336 | mbedtls_asn1_buf params; |
kenjiArai | 0:5b88d5760320 | 337 | |
kenjiArai | 0:5b88d5760320 | 338 | memset( ¶ms, 0, sizeof(mbedtls_asn1_buf) ); |
kenjiArai | 0:5b88d5760320 | 339 | |
kenjiArai | 0:5b88d5760320 | 340 | if( ( ret = mbedtls_asn1_get_alg( p, end, alg, ¶ms ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 341 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 342 | |
kenjiArai | 0:5b88d5760320 | 343 | if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 ) |
kenjiArai | 0:5b88d5760320 | 344 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
kenjiArai | 0:5b88d5760320 | 345 | |
kenjiArai | 0:5b88d5760320 | 346 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 347 | } |
kenjiArai | 0:5b88d5760320 | 348 | |
kenjiArai | 0:5b88d5760320 | 349 | void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur ) |
kenjiArai | 0:5b88d5760320 | 350 | { |
kenjiArai | 0:5b88d5760320 | 351 | if( cur == NULL ) |
kenjiArai | 0:5b88d5760320 | 352 | return; |
kenjiArai | 0:5b88d5760320 | 353 | |
kenjiArai | 0:5b88d5760320 | 354 | mbedtls_free( cur->oid.p ); |
kenjiArai | 0:5b88d5760320 | 355 | mbedtls_free( cur->val.p ); |
kenjiArai | 0:5b88d5760320 | 356 | |
kenjiArai | 0:5b88d5760320 | 357 | mbedtls_platform_zeroize( cur, sizeof( mbedtls_asn1_named_data ) ); |
kenjiArai | 0:5b88d5760320 | 358 | } |
kenjiArai | 0:5b88d5760320 | 359 | |
kenjiArai | 0:5b88d5760320 | 360 | void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head ) |
kenjiArai | 0:5b88d5760320 | 361 | { |
kenjiArai | 0:5b88d5760320 | 362 | mbedtls_asn1_named_data *cur; |
kenjiArai | 0:5b88d5760320 | 363 | |
kenjiArai | 0:5b88d5760320 | 364 | while( ( cur = *head ) != NULL ) |
kenjiArai | 0:5b88d5760320 | 365 | { |
kenjiArai | 0:5b88d5760320 | 366 | *head = cur->next; |
kenjiArai | 0:5b88d5760320 | 367 | mbedtls_asn1_free_named_data( cur ); |
kenjiArai | 0:5b88d5760320 | 368 | mbedtls_free( cur ); |
kenjiArai | 0:5b88d5760320 | 369 | } |
kenjiArai | 0:5b88d5760320 | 370 | } |
kenjiArai | 0:5b88d5760320 | 371 | |
kenjiArai | 0:5b88d5760320 | 372 | mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list, |
kenjiArai | 0:5b88d5760320 | 373 | const char *oid, size_t len ) |
kenjiArai | 0:5b88d5760320 | 374 | { |
kenjiArai | 0:5b88d5760320 | 375 | while( list != NULL ) |
kenjiArai | 0:5b88d5760320 | 376 | { |
kenjiArai | 0:5b88d5760320 | 377 | if( list->oid.len == len && |
kenjiArai | 0:5b88d5760320 | 378 | memcmp( list->oid.p, oid, len ) == 0 ) |
kenjiArai | 0:5b88d5760320 | 379 | { |
kenjiArai | 0:5b88d5760320 | 380 | break; |
kenjiArai | 0:5b88d5760320 | 381 | } |
kenjiArai | 0:5b88d5760320 | 382 | |
kenjiArai | 0:5b88d5760320 | 383 | list = list->next; |
kenjiArai | 0:5b88d5760320 | 384 | } |
kenjiArai | 0:5b88d5760320 | 385 | |
kenjiArai | 0:5b88d5760320 | 386 | return( list ); |
kenjiArai | 0:5b88d5760320 | 387 | } |
kenjiArai | 0:5b88d5760320 | 388 | |
kenjiArai | 0:5b88d5760320 | 389 | #endif /* MBEDTLS_ASN1_PARSE_C */ |