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