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