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