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