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