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