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