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