Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Generic ASN.1 parsing |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 21 | |
Agathiyan Bragadeesh | fca0861 | 2023-09-04 15:45:37 +0100 | [diff] [blame] | 22 | #if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_X509_CREATE_C) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/asn1.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 25 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 26 | #include "mbedtls/error.h" |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 27 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_BIGNUM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/bignum.h" |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 35 | |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 36 | /* |
| 37 | * ASN.1 DER decoding routines |
| 38 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | int mbedtls_asn1_get_len(unsigned char **p, |
| 40 | const unsigned char *end, |
| 41 | size_t *len) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 42 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | if ((end - *p) < 1) { |
| 44 | return MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 45 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 46 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | if ((**p & 0x80) == 0) { |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 48 | *len = *(*p)++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | } else { |
Dave Rodgman | ef6795d | 2023-09-12 14:42:46 +0100 | [diff] [blame] | 50 | int n = (**p) & 0x7F; |
| 51 | if (n == 0 || n > 4) { |
| 52 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 53 | } |
| 54 | if ((end - *p) <= n) { |
| 55 | return MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 56 | } |
| 57 | *len = 0; |
| 58 | (*p)++; |
| 59 | while (n--) { |
| 60 | *len = (*len << 8) | **p; |
| 61 | (*p)++; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | if (*len > (size_t) (end - *p)) { |
| 66 | return MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 67 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | return 0; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | int mbedtls_asn1_get_tag(unsigned char **p, |
| 73 | const unsigned char *end, |
| 74 | size_t *len, int tag) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 75 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | if ((end - *p) < 1) { |
| 77 | return MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 78 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | if (**p != tag) { |
| 81 | return MBEDTLS_ERR_ASN1_UNEXPECTED_TAG; |
| 82 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 83 | |
| 84 | (*p)++; |
| 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | return mbedtls_asn1_get_len(p, end, len); |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 87 | } |
Agathiyan Bragadeesh | fca0861 | 2023-09-04 15:45:37 +0100 | [diff] [blame] | 88 | #endif /* MBEDTLS_ASN1_PARSE_C || MBEDTLS_X509_CREATE_C */ |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 89 | |
Agathiyan Bragadeesh | fca0861 | 2023-09-04 15:45:37 +0100 | [diff] [blame] | 90 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | int mbedtls_asn1_get_bool(unsigned char **p, |
| 92 | const unsigned char *end, |
| 93 | int *val) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 94 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 95 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 96 | size_t len; |
| 97 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_BOOLEAN)) != 0) { |
| 99 | return ret; |
| 100 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | if (len != 1) { |
| 103 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 104 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | *val = (**p != 0) ? 1 : 0; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 107 | (*p)++; |
| 108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | return 0; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | static int asn1_get_tagged_int(unsigned char **p, |
| 113 | const unsigned char *end, |
| 114 | int tag, int *val) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 115 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 116 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 117 | size_t len; |
| 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, tag)) != 0) { |
| 120 | return ret; |
| 121 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 122 | |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 123 | /* |
| 124 | * len==0 is malformed (0 must be represented as 020100 for INTEGER, |
| 125 | * or 0A0100 for ENUMERATED tags |
| 126 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if (len == 0) { |
| 128 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 129 | } |
Gilles Peskine | 9fd9794 | 2019-10-10 19:27:53 +0200 | [diff] [blame] | 130 | /* This is a cryptography library. Reject negative integers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | if ((**p & 0x80) != 0) { |
| 132 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 133 | } |
Gilles Peskine | f7d6acd | 2019-03-01 18:06:08 +0100 | [diff] [blame] | 134 | |
Gilles Peskine | 9fd9794 | 2019-10-10 19:27:53 +0200 | [diff] [blame] | 135 | /* Skip leading zeros. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | while (len > 0 && **p == 0) { |
| 137 | ++(*p); |
Gilles Peskine | f7d6acd | 2019-03-01 18:06:08 +0100 | [diff] [blame] | 138 | --len; |
| 139 | } |
Gilles Peskine | 9fd9794 | 2019-10-10 19:27:53 +0200 | [diff] [blame] | 140 | |
| 141 | /* Reject integers that don't fit in an int. This code assumes that |
| 142 | * the int type has no padding bit. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | if (len > sizeof(int)) { |
| 144 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 145 | } |
| 146 | if (len == sizeof(int) && (**p & 0x80) != 0) { |
| 147 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 148 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 149 | |
| 150 | *val = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | while (len-- > 0) { |
| 152 | *val = (*val << 8) | **p; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 153 | (*p)++; |
| 154 | } |
| 155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | return 0; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | int mbedtls_asn1_get_int(unsigned char **p, |
| 160 | const unsigned char *end, |
| 161 | int *val) |
| 162 | { |
| 163 | return asn1_get_tagged_int(p, end, MBEDTLS_ASN1_INTEGER, val); |
| 164 | } |
| 165 | |
| 166 | int mbedtls_asn1_get_enum(unsigned char **p, |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 167 | const unsigned char *end, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | int *val) |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 169 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | return asn1_get_tagged_int(p, end, MBEDTLS_ASN1_ENUMERATED, val); |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_BIGNUM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | int mbedtls_asn1_get_mpi(unsigned char **p, |
| 175 | const unsigned char *end, |
| 176 | mbedtls_mpi *X) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 177 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 178 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 179 | size_t len; |
| 180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) { |
| 182 | return ret; |
| 183 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | ret = mbedtls_mpi_read_binary(X, *p, len); |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 186 | |
| 187 | *p += len; |
| 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | return ret; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end, |
| 194 | mbedtls_asn1_bitstring *bs) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 195 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 196 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 197 | |
| 198 | /* Certificate type is a single byte bitstring */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | if ((ret = mbedtls_asn1_get_tag(p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING)) != 0) { |
| 200 | return ret; |
| 201 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 202 | |
| 203 | /* Check length, subtract one for actual bit string length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | if (bs->len < 1) { |
| 205 | return MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 206 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 207 | bs->len -= 1; |
| 208 | |
| 209 | /* Get number of unused bits, ensure unused bits <= 7 */ |
| 210 | bs->unused_bits = **p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | if (bs->unused_bits > 7) { |
| 212 | return MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 213 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 214 | (*p)++; |
| 215 | |
| 216 | /* Get actual bitstring */ |
| 217 | bs->p = *p; |
| 218 | *p += bs->len; |
| 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | if (*p != end) { |
| 221 | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 222 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | return 0; |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 227 | /* |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 228 | * Traverse an ASN.1 "SEQUENCE OF <tag>" |
| 229 | * and call a callback for each entry found. |
| 230 | */ |
| 231 | int mbedtls_asn1_traverse_sequence_of( |
| 232 | unsigned char **p, |
| 233 | const unsigned char *end, |
Hanno Becker | 34aada2 | 2020-02-03 10:39:55 +0000 | [diff] [blame] | 234 | unsigned char tag_must_mask, unsigned char tag_must_val, |
| 235 | unsigned char tag_may_mask, unsigned char tag_may_val, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | int (*cb)(void *ctx, int tag, |
| 237 | unsigned char *start, size_t len), |
| 238 | void *ctx) |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 239 | { |
| 240 | int ret; |
| 241 | size_t len; |
| 242 | |
| 243 | /* Get main sequence tag */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 245 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 246 | return ret; |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | if (*p + len != end) { |
| 250 | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 251 | } |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | while (*p < end) { |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 254 | unsigned char const tag = *(*p)++; |
| 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | if ((tag & tag_must_mask) != tag_must_val) { |
| 257 | return MBEDTLS_ERR_ASN1_UNEXPECTED_TAG; |
| 258 | } |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | if ((ret = mbedtls_asn1_get_len(p, end, &len)) != 0) { |
| 261 | return ret; |
| 262 | } |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | if ((tag & tag_may_mask) == tag_may_val) { |
| 265 | if (cb != NULL) { |
| 266 | ret = cb(ctx, tag, *p, len); |
| 267 | if (ret != 0) { |
| 268 | return ret; |
| 269 | } |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | *p += len; |
| 274 | } |
| 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | return 0; |
Hanno Becker | 199b709 | 2019-09-11 14:21:26 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 280 | * Get a bit string without unused bits |
| 281 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end, |
| 283 | size_t *len) |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 284 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 285 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | if ((ret = mbedtls_asn1_get_tag(p, end, len, MBEDTLS_ASN1_BIT_STRING)) != 0) { |
| 288 | return ret; |
| 289 | } |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | if (*len == 0) { |
| 292 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 293 | } |
| 294 | --(*len); |
Gilles Peskine | e40d120 | 2019-03-01 18:08:35 +0100 | [diff] [blame] | 295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | if (**p != 0) { |
| 297 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 298 | } |
| 299 | ++(*p); |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | return 0; |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | void mbedtls_asn1_sequence_free(mbedtls_asn1_sequence *seq) |
Hanno Becker | 12ae27d | 2019-09-11 14:20:09 +0100 | [diff] [blame] | 305 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | while (seq != NULL) { |
Hanno Becker | 12ae27d | 2019-09-11 14:20:09 +0100 | [diff] [blame] | 307 | mbedtls_asn1_sequence *next = seq->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | mbedtls_free(seq); |
Hanno Becker | 12ae27d | 2019-09-11 14:20:09 +0100 | [diff] [blame] | 309 | seq = next; |
| 310 | } |
| 311 | } |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | typedef struct { |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 314 | int tag; |
| 315 | mbedtls_asn1_sequence *cur; |
| 316 | } asn1_get_sequence_of_cb_ctx_t; |
| 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | static int asn1_get_sequence_of_cb(void *ctx, |
| 319 | int tag, |
| 320 | unsigned char *start, |
| 321 | size_t len) |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 322 | { |
| 323 | asn1_get_sequence_of_cb_ctx_t *cb_ctx = |
| 324 | (asn1_get_sequence_of_cb_ctx_t *) ctx; |
| 325 | mbedtls_asn1_sequence *cur = |
| 326 | cb_ctx->cur; |
| 327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if (cur->buf.p != NULL) { |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 329 | cur->next = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence)); |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | if (cur->next == NULL) { |
| 333 | return MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
| 334 | } |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 335 | |
| 336 | cur = cur->next; |
| 337 | } |
| 338 | |
| 339 | cur->buf.p = start; |
| 340 | cur->buf.len = len; |
| 341 | cur->buf.tag = tag; |
| 342 | |
| 343 | cb_ctx->cur = cur; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | return 0; |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 347 | /* |
| 348 | * Parses and splits an ASN.1 "SEQUENCE OF <tag>" |
| 349 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | int mbedtls_asn1_get_sequence_of(unsigned char **p, |
| 351 | const unsigned char *end, |
| 352 | mbedtls_asn1_sequence *cur, |
| 353 | int tag) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 354 | { |
Hanno Becker | 1505f63 | 2019-09-11 14:25:26 +0100 | [diff] [blame] | 355 | asn1_get_sequence_of_cb_ctx_t cb_ctx = { tag, cur }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | memset(cur, 0, sizeof(mbedtls_asn1_sequence)); |
| 357 | return mbedtls_asn1_traverse_sequence_of( |
| 358 | p, end, 0xFF, tag, 0, 0, |
| 359 | asn1_get_sequence_of_cb, &cb_ctx); |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | int mbedtls_asn1_get_alg(unsigned char **p, |
| 363 | const unsigned char *end, |
| 364 | mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params) |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 365 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 366 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 367 | size_t len; |
| 368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 370 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 371 | return ret; |
| 372 | } |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | if ((end - *p) < 1) { |
| 375 | return MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 376 | } |
Manuel Pégourié-Gonnard | ba77bbf | 2013-08-15 13:38:13 +0200 | [diff] [blame] | 377 | |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 378 | alg->tag = **p; |
Manuel Pégourié-Gonnard | ba77bbf | 2013-08-15 13:38:13 +0200 | [diff] [blame] | 379 | end = *p + len; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | if ((ret = mbedtls_asn1_get_tag(p, end, &alg->len, MBEDTLS_ASN1_OID)) != 0) { |
| 382 | return ret; |
| 383 | } |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 384 | |
| 385 | alg->p = *p; |
| 386 | *p += alg->len; |
| 387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | if (*p == end) { |
| 389 | mbedtls_platform_zeroize(params, sizeof(mbedtls_asn1_buf)); |
| 390 | return 0; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | params->tag = **p; |
| 394 | (*p)++; |
| 395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | if ((ret = mbedtls_asn1_get_len(p, end, ¶ms->len)) != 0) { |
| 397 | return ret; |
| 398 | } |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 399 | |
| 400 | params->p = *p; |
| 401 | *p += params->len; |
| 402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | if (*p != end) { |
| 404 | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 405 | } |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | return 0; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | int mbedtls_asn1_get_alg_null(unsigned char **p, |
| 411 | const unsigned char *end, |
| 412 | mbedtls_asn1_buf *alg) |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 413 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 414 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_asn1_buf params; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | memset(¶ms, 0, sizeof(mbedtls_asn1_buf)); |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | if ((ret = mbedtls_asn1_get_alg(p, end, alg, ¶ms)) != 0) { |
| 420 | return ret; |
| 421 | } |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | if ((params.tag != MBEDTLS_ASN1_NULL && params.tag != 0) || params.len != 0) { |
| 424 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 425 | } |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | return 0; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Glenn Strauss | 82ba274 | 2022-11-04 04:01:23 -0400 | [diff] [blame] | 430 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur) |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 432 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (cur == NULL) { |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 434 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | } |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | mbedtls_free(cur->oid.p); |
| 438 | mbedtls_free(cur->val.p); |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | mbedtls_platform_zeroize(cur, sizeof(mbedtls_asn1_named_data)); |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 441 | } |
Glenn Strauss | 82ba274 | 2022-11-04 04:01:23 -0400 | [diff] [blame] | 442 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head) |
Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 445 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | mbedtls_asn1_named_data *cur; |
Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | while ((cur = *head) != NULL) { |
Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 449 | *head = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | mbedtls_free(cur->oid.p); |
| 451 | mbedtls_free(cur->val.p); |
| 452 | mbedtls_free(cur); |
Paul Bakker | c547cc9 | 2013-09-09 12:01:23 +0200 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | void mbedtls_asn1_free_named_data_list_shallow(mbedtls_asn1_named_data *name) |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 457 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | for (mbedtls_asn1_named_data *next; name != NULL; name = next) { |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 459 | next = name->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | mbedtls_free(name); |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | const mbedtls_asn1_named_data *mbedtls_asn1_find_named_data(const mbedtls_asn1_named_data *list, |
| 465 | const char *oid, size_t len) |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 466 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | while (list != NULL) { |
| 468 | if (list->oid.len == len && |
| 469 | memcmp(list->oid.p, oid, len) == 0) { |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | |
| 473 | list = list->next; |
| 474 | } |
| 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | return list; |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | #endif /* MBEDTLS_ASN1_PARSE_C */ |