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