Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 2 | * X.509 common functions for parsing and verification |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 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 | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 21 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 22 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
| 23 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
| 24 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 25 | * |
| 26 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
| 27 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 28 | */ |
| 29 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 30 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_X509_USE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/x509.h" |
| 35 | #include "mbedtls/asn1.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 36 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/oid.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | |
Rich Evans | 36796df | 2015-02-12 18:27:14 +0000 | [diff] [blame] | 39 | #include <stdio.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 43 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 46 | #if defined(MBEDTLS_ASN1_WRITE_C) |
| 47 | #include "mbedtls/asn1write.h" |
| 48 | #endif |
| 49 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 51 | |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_HAVE_TIME) |
| 53 | #include "mbedtls/platform_time.h" |
| 54 | #endif |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 55 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 56 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 57 | #include <time.h> |
| 58 | #endif |
| 59 | |
Demi Marie Obenour | 690b8c9 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 60 | #define CHECK(code) \ |
| 61 | do { \ |
| 62 | if ((ret = (code)) != 0) { \ |
| 63 | return ret; \ |
| 64 | } \ |
| 65 | } while (0) |
| 66 | |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 67 | #define CHECK_RANGE(min, max, val) \ |
Demi Marie Obenour | 690b8c9 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 68 | do { \ |
| 69 | if ((val) < (min) || (val) > (max)) { \ |
| 70 | return ret; \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 71 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | } while (0) |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 73 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 74 | /* |
| 75 | * CertificateSerialNumber ::= INTEGER |
| 76 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end, |
| 78 | mbedtls_x509_buf *serial) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 79 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 80 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 81 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | if ((end - *p) < 1) { |
| 83 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 84 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 85 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 86 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) && |
| 88 | **p != MBEDTLS_ASN1_INTEGER) { |
| 89 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 90 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 91 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 92 | |
| 93 | serial->tag = *(*p)++; |
| 94 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) { |
| 96 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret); |
| 97 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | |
| 99 | serial->p = *p; |
| 100 | *p += serial->len; |
| 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Get an algorithm identifier without parameters (eg for signatures) |
| 106 | * |
| 107 | * AlgorithmIdentifier ::= SEQUENCE { |
| 108 | * algorithm OBJECT IDENTIFIER, |
| 109 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 110 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end, |
| 112 | mbedtls_x509_buf *alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 113 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 114 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) { |
| 117 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 118 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 124 | * Parse an algorithm identifier with (optional) parameters |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 125 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end, |
| 127 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 128 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 129 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) { |
| 132 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 133 | } |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | return 0; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Convert md type to string |
| 140 | */ |
Dave Rodgman | ffabb7b | 2023-06-28 16:22:50 +0100 | [diff] [blame] | 141 | #if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | static inline const char *md_type_to_string(mbedtls_md_type_t md_alg) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 144 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | switch (md_alg) { |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 146 | #if defined(MBEDTLS_MD_CAN_MD5) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | case MBEDTLS_MD_MD5: |
| 148 | return "MD5"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 149 | #endif |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 150 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | case MBEDTLS_MD_SHA1: |
| 152 | return "SHA1"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 153 | #endif |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 154 | #if defined(MBEDTLS_MD_CAN_SHA224) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | case MBEDTLS_MD_SHA224: |
| 156 | return "SHA224"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 157 | #endif |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 158 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | case MBEDTLS_MD_SHA256: |
| 160 | return "SHA256"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 161 | #endif |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 162 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | case MBEDTLS_MD_SHA384: |
| 164 | return "SHA384"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 165 | #endif |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 166 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | case MBEDTLS_MD_SHA512: |
| 168 | return "SHA512"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 169 | #endif |
Manuel Pégourié-Gonnard | 49e67f8 | 2023-03-16 11:39:20 +0100 | [diff] [blame] | 170 | #if defined(MBEDTLS_MD_CAN_RIPEMD160) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | case MBEDTLS_MD_RIPEMD160: |
| 172 | return "RIPEMD160"; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 173 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | case MBEDTLS_MD_NONE: |
| 175 | return NULL; |
| 176 | default: |
| 177 | return NULL; |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Dave Rodgman | f032c98 | 2023-06-29 12:09:27 +0100 | [diff] [blame] | 181 | #endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */ |
Dave Rodgman | ffabb7b | 2023-06-28 16:22:50 +0100 | [diff] [blame] | 182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 184 | /* |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 185 | * HashAlgorithm ::= AlgorithmIdentifier |
| 186 | * |
| 187 | * AlgorithmIdentifier ::= SEQUENCE { |
| 188 | * algorithm OBJECT IDENTIFIER, |
| 189 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 190 | * |
| 191 | * For HashAlgorithm, parameters MUST be NULL or absent. |
| 192 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 194 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 195 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 196 | unsigned char *p; |
| 197 | const unsigned char *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | mbedtls_x509_buf md_oid; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 199 | size_t len; |
| 200 | |
| 201 | /* Make sure we got a SEQUENCE and setup bounds */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 203 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 204 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 205 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 206 | |
Andrzej Kurek | feaebc5 | 2020-07-16 04:37:41 -0400 | [diff] [blame] | 207 | p = alg->p; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 208 | end = p + alg->len; |
| 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | if (p >= end) { |
| 211 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 212 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 213 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 214 | |
| 215 | /* Parse md_oid */ |
| 216 | md_oid.tag = *p; |
| 217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) { |
| 219 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 220 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 221 | |
| 222 | md_oid.p = p; |
| 223 | p += md_oid.len; |
| 224 | |
| 225 | /* Get md_alg from md_oid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) { |
| 227 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 228 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 229 | |
| 230 | /* Make sure params is absent of NULL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | if (p == end) { |
| 232 | return 0; |
| 233 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) { |
| 236 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 237 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | if (p != end) { |
| 240 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 241 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 242 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | return 0; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 248 | * RSASSA-PSS-params ::= SEQUENCE { |
| 249 | * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier, |
| 250 | * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier, |
| 251 | * saltLength [2] INTEGER DEFAULT 20, |
| 252 | * trailerField [3] INTEGER DEFAULT 1 } |
| 253 | * -- Note that the tags in this Sequence are explicit. |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 254 | * |
| 255 | * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value |
| 256 | * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 257 | * option. Enforce this at parsing time. |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 258 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params, |
| 260 | mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, |
| 261 | int *salt_len) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 262 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 263 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 264 | unsigned char *p; |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 265 | const unsigned char *end, *end2; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 266 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | mbedtls_x509_buf alg_id, alg_params; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 268 | |
| 269 | /* First set everything to defaults */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | *md_alg = MBEDTLS_MD_SHA1; |
| 271 | *mgf_md = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 272 | *salt_len = 20; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 273 | |
| 274 | /* Make sure params is a SEQUENCE and setup bounds */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 276 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 277 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 278 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 279 | |
| 280 | p = (unsigned char *) params->p; |
| 281 | end = p + params->len; |
| 282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | if (p == end) { |
| 284 | return 0; |
| 285 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 286 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 287 | /* |
| 288 | * HashAlgorithm |
| 289 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 291 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 292 | 0)) == 0) { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 293 | end2 = p + len; |
| 294 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 295 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) { |
| 297 | return ret; |
| 298 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) { |
| 301 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 302 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | if (p != end2) { |
| 305 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 306 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 307 | } |
| 308 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 309 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 310 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | if (p == end) { |
| 313 | return 0; |
| 314 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 315 | |
| 316 | /* |
| 317 | * MaskGenAlgorithm |
| 318 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 320 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 321 | 1)) == 0) { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 322 | end2 = p + len; |
| 323 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 324 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) { |
| 326 | return ret; |
| 327 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 328 | |
| 329 | /* Only MFG1 is recognised for now */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) { |
| 331 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE, |
| 332 | MBEDTLS_ERR_OID_NOT_FOUND); |
| 333 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 334 | |
| 335 | /* Parse HashAlgorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) { |
| 337 | return ret; |
| 338 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | if (p != end2) { |
| 341 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 342 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 343 | } |
| 344 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 345 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 346 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | if (p == end) { |
| 349 | return 0; |
| 350 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 351 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 352 | /* |
| 353 | * salt_len |
| 354 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 356 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 357 | 2)) == 0) { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 358 | end2 = p + len; |
| 359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) { |
| 361 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 362 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | if (p != end2) { |
| 365 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 366 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 367 | } |
| 368 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 369 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 370 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (p == end) { |
| 373 | return 0; |
| 374 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 376 | /* |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 377 | * trailer_field (if present, must be 1) |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 378 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 380 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 381 | 3)) == 0) { |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 382 | int trailer_field; |
| 383 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 384 | end2 = p + len; |
| 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) { |
| 387 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 388 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | if (p != end2) { |
| 391 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 392 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 393 | } |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (trailer_field != 1) { |
| 396 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 397 | } |
| 398 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 399 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 400 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | if (p != end) { |
| 403 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 404 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 405 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | return 0; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 408 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 410 | |
| 411 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 412 | * AttributeTypeAndValue ::= SEQUENCE { |
| 413 | * type AttributeType, |
| 414 | * value AttributeValue } |
| 415 | * |
| 416 | * AttributeType ::= OBJECT IDENTIFIER |
| 417 | * |
| 418 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 419 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | static int x509_get_attr_type_value(unsigned char **p, |
| 421 | const unsigned char *end, |
| 422 | mbedtls_x509_name *cur) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 423 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 424 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 425 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | mbedtls_x509_buf *oid; |
| 427 | mbedtls_x509_buf *val; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 430 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 431 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
| 432 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 433 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 434 | end = *p + len; |
| 435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 436 | if ((end - *p) < 1) { |
| 437 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 438 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 439 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 440 | |
| 441 | oid = &cur->oid; |
| 442 | oid->tag = **p; |
| 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) { |
| 445 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
| 446 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 447 | |
| 448 | oid->p = *p; |
| 449 | *p += oid->len; |
| 450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | if ((end - *p) < 1) { |
| 452 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 453 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 454 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING && |
| 458 | **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | **p != MBEDTLS_ASN1_BIT_STRING) { |
| 460 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 461 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 462 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 463 | |
| 464 | val = &cur->val; |
| 465 | val->tag = *(*p)++; |
| 466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) { |
| 468 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
| 469 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 470 | |
| 471 | val->p = *p; |
| 472 | *p += val->len; |
| 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | if (*p != end) { |
| 475 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 476 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 479 | cur->next = NULL; |
| 480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 485 | * Name ::= CHOICE { -- only one possibility for now -- |
| 486 | * rdnSequence RDNSequence } |
| 487 | * |
| 488 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 489 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 490 | * RelativeDistinguishedName ::= |
| 491 | * SET OF AttributeTypeAndValue |
| 492 | * |
| 493 | * AttributeTypeAndValue ::= SEQUENCE { |
| 494 | * type AttributeType, |
| 495 | * value AttributeValue } |
| 496 | * |
| 497 | * AttributeType ::= OBJECT IDENTIFIER |
| 498 | * |
| 499 | * AttributeValue ::= ANY DEFINED BY AttributeType |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 500 | * |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 501 | * The data structure is optimized for the common case where each RDN has only |
| 502 | * one element, which is represented as a list of AttributeTypeAndValue. |
| 503 | * For the general case we still use a flat list, but we mark elements of the |
| 504 | * same set so that they are "merged" together in the functions that consume |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 505 | * this list, eg mbedtls_x509_dn_gets(). |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 506 | * |
David Horstmann | 11307a1 | 2022-10-17 18:10:23 +0100 | [diff] [blame] | 507 | * On success, this function may allocate a linked list starting at cur->next |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 508 | * that must later be free'd by the caller using mbedtls_free(). In error |
| 509 | * cases, this function frees all allocated memory internally and the caller |
| 510 | * has no freeing responsibilities. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 511 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end, |
| 513 | mbedtls_x509_name *cur) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 514 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 515 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 516 | size_t set_len; |
| 517 | const unsigned char *end_set; |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 518 | mbedtls_x509_name *head = cur; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 519 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 520 | /* don't use recursion, we'd risk stack overflow if not optimized */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | while (1) { |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 522 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 523 | * parse SET |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 524 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | if ((ret = mbedtls_asn1_get_tag(p, end, &set_len, |
| 526 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) { |
| 527 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 528 | goto error; |
| 529 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 530 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 531 | end_set = *p + set_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | while (1) { |
| 534 | if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) { |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 535 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (*p == end_set) { |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 539 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | } |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 541 | |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 542 | /* Mark this item as being no the only one in a set */ |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 543 | cur->next_merged = 1; |
| 544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name)); |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | if (cur->next == NULL) { |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 548 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 549 | goto error; |
| 550 | } |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 551 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 552 | cur = cur->next; |
| 553 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 554 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 555 | /* |
| 556 | * continue until end of SEQUENCE is reached |
| 557 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | if (*p == end) { |
| 559 | return 0; |
| 560 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | if (cur->next == NULL) { |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 565 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 566 | goto error; |
| 567 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 568 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 569 | cur = cur->next; |
| 570 | } |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 571 | |
| 572 | error: |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 573 | /* Skip the first element as we did not allocate it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | mbedtls_asn1_free_named_data_list_shallow(head->next); |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 575 | head->next = NULL; |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 580 | static int x509_date_is_valid(const mbedtls_x509_time *t) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 581 | { |
David Horstmann | cdf5283 | 2023-07-05 09:58:03 +0100 | [diff] [blame] | 582 | unsigned int month_days; |
| 583 | unsigned int year; |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 584 | switch (t->mon) { |
| 585 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: |
David Horstmann | cdf5283 | 2023-07-05 09:58:03 +0100 | [diff] [blame] | 586 | month_days = 31; |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 587 | break; |
| 588 | case 4: case 6: case 9: case 11: |
David Horstmann | cdf5283 | 2023-07-05 09:58:03 +0100 | [diff] [blame] | 589 | month_days = 30; |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 590 | break; |
| 591 | case 2: |
David Horstmann | cdf5283 | 2023-07-05 09:58:03 +0100 | [diff] [blame] | 592 | year = (unsigned int) t->year; |
| 593 | month_days = ((year & 3) || (!(year % 100) |
| 594 | && (year % 400))) |
| 595 | ? 28 : 29; |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 596 | break; |
| 597 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | return MBEDTLS_ERR_X509_INVALID_DATE; |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 599 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 600 | |
David Horstmann | b1d27bc | 2023-07-05 10:00:31 +0100 | [diff] [blame] | 601 | if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */ |
David Horstmann | 3ae1c4c | 2023-07-05 11:15:08 +0100 | [diff] [blame] | 602 | /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */ |
David Horstmann | b1d27bc | 2023-07-05 10:00:31 +0100 | [diff] [blame] | 603 | (unsigned int) t->year > 9999 || /* (0 - 9999) */ |
| 604 | (unsigned int) t->hour > 23 || /* (0 - 23) */ |
| 605 | (unsigned int) t->min > 59 || /* (0 - 59) */ |
| 606 | (unsigned int) t->sec > 59) { /* (0 - 59) */ |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 607 | return MBEDTLS_ERR_X509_INVALID_DATE; |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 608 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | return 0; |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 613 | static int x509_parse2_int(const unsigned char *p) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 614 | { |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 615 | uint32_t d1 = p[0] - '0'; |
| 616 | uint32_t d2 = p[1] - '0'; |
| 617 | return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 620 | /* |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 621 | * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) |
| 622 | * field. |
| 623 | */ |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 624 | static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm, |
| 625 | size_t yearlen) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 626 | { |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 627 | int x; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 628 | |
| 629 | /* |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 630 | * Parse year, month, day, hour, minute, second |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 631 | */ |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 632 | tm->year = x509_parse2_int(p); |
| 633 | if (tm->year < 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 634 | return MBEDTLS_ERR_X509_INVALID_DATE; |
| 635 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 636 | |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 637 | if (4 == yearlen) { |
| 638 | x = tm->year * 100; |
| 639 | p += 2; |
| 640 | tm->year = x509_parse2_int(p); |
| 641 | if (tm->year < 0) { |
| 642 | return MBEDTLS_ERR_X509_INVALID_DATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | } else { |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 645 | x = (tm->year < 50) ? 2000 : 1900; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 646 | } |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 647 | tm->year += x; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 648 | |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 649 | tm->mon = x509_parse2_int(p + 2); |
| 650 | tm->day = x509_parse2_int(p + 4); |
| 651 | tm->hour = x509_parse2_int(p + 6); |
| 652 | tm->min = x509_parse2_int(p + 8); |
| 653 | tm->sec = x509_parse2_int(p + 10); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 654 | |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 655 | return x509_date_is_valid(tm); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 659 | * Time ::= CHOICE { |
| 660 | * utcTime UTCTime, |
| 661 | * generalTime GeneralizedTime } |
| 662 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end, |
| 664 | mbedtls_x509_time *tm) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 665 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 666 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 667 | size_t len, year_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 668 | unsigned char tag; |
| 669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | if ((end - *p) < 1) { |
| 671 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
| 672 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 673 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 674 | |
| 675 | tag = **p; |
| 676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | if (tag == MBEDTLS_ASN1_UTC_TIME) { |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 678 | year_len = 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) { |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 680 | year_len = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | } else { |
| 682 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
| 683 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 684 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 685 | |
| 686 | (*p)++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | ret = mbedtls_asn1_get_len(p, end, &len); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | if (ret != 0) { |
| 690 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret); |
| 691 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 692 | |
Glenn Strauss | 06c31fc | 2022-06-30 13:07:55 -0400 | [diff] [blame] | 693 | /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */ |
| 694 | if (len != year_len + 10 && |
| 695 | !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) { |
| 696 | return MBEDTLS_ERR_X509_INVALID_DATE; |
| 697 | } |
| 698 | |
| 699 | (*p) += len; |
| 700 | return x509_parse_time(*p - len, tm, year_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 704 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 705 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 706 | size_t len; |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 707 | int tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | if ((end - *p) < 1) { |
| 710 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, |
| 711 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 712 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 713 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 714 | tag_type = **p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) { |
| 717 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret); |
| 718 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 719 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 720 | sig->tag = tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 721 | sig->len = len; |
| 722 | sig->p = *p; |
| 723 | |
| 724 | *p += len; |
| 725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 726 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 729 | /* |
| 730 | * Get signature algorithm from alg OID and optional parameters |
| 731 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
| 733 | mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, |
| 734 | void **sig_opts) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 735 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 736 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | if (*sig_opts != NULL) { |
| 739 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 740 | } |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) { |
| 743 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret); |
| 744 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 746 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options)); |
| 751 | if (pss_opts == NULL) { |
| 752 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 753 | } |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | ret = mbedtls_x509_get_rsassa_pss_params(sig_params, |
| 756 | md_alg, |
| 757 | &pss_opts->mgf1_hash_id, |
| 758 | &pss_opts->expected_salt_len); |
| 759 | if (ret != 0) { |
| 760 | mbedtls_free(pss_opts); |
| 761 | return ret; |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 762 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 763 | |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 764 | *sig_opts = (void *) pss_opts; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 767 | { |
| 768 | /* Make sure parameters are absent or NULL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) || |
| 770 | sig_params->len != 0) { |
| 771 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 772 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* |
| 779 | * X.509 Extensions (No parsing of extensions, pointer should |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 780 | * be either manually updated or extensions should be parsed!) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 781 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end, |
| 783 | mbedtls_x509_buf *ext, int tag) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 784 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 785 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 786 | size_t len; |
| 787 | |
Hanno Becker | 3cddba8 | 2019-02-11 14:33:36 +0000 | [diff] [blame] | 788 | /* Extension structure use EXPLICIT tagging. That is, the actual |
| 789 | * `Extensions` structure is wrapped by a tag-length pair using |
| 790 | * the respective context-specific tag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 791 | ret = mbedtls_asn1_get_tag(p, end, &ext->len, |
| 792 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag); |
| 793 | if (ret != 0) { |
| 794 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 795 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 796 | |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 797 | ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag; |
| 798 | ext->p = *p; |
| 799 | end = *p + ext->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 800 | |
| 801 | /* |
| 802 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 803 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 805 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 806 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 807 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | if (end != *p + len) { |
| 810 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 811 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 812 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 814 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Agathiyan Bragadeesh | 9d2507c | 2023-07-24 16:35:57 +0100 | [diff] [blame] | 817 | static char nibble_to_hex_digit(int i) |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 818 | { |
Agathiyan Bragadeesh | f0e1ac5 | 2023-07-24 16:43:36 +0100 | [diff] [blame] | 819 | return (i < 10) ? (i + '0') : (i - 10 + 'A'); |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 822 | /* |
| 823 | * Store the name in printable form into buf; no more |
| 824 | * than size characters will be written |
| 825 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 826 | int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 827 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 828 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 5adffb2 | 2023-08-10 15:50:57 +0100 | [diff] [blame] | 829 | size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start; |
| 830 | unsigned char asn1_tag_len_buf[10]; |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 831 | unsigned char *asn1_len_p; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 832 | unsigned char c, merge = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | const mbedtls_x509_name *name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 834 | const char *short_name = NULL; |
Agathiyan Bragadeesh | a1f5c2d | 2023-08-02 17:08:52 +0100 | [diff] [blame] | 835 | char lowbits, highbits; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
Agathiyan Bragadeesh | c9d74f3 | 2023-07-31 17:25:44 +0100 | [diff] [blame] | 837 | int print_hexstring; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | memset(s, 0, sizeof(s)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 840 | |
| 841 | name = dn; |
| 842 | p = buf; |
| 843 | n = size; |
| 844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | while (name != NULL) { |
| 846 | if (!name->oid.p) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 847 | name = name->next; |
| 848 | continue; |
| 849 | } |
| 850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | if (name != dn) { |
| 852 | ret = mbedtls_snprintf(p, n, merge ? " + " : ", "); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 853 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 854 | } |
| 855 | |
Agathiyan Bragadeesh | 0a4b6d8 | 2023-08-02 15:05:57 +0100 | [diff] [blame] | 856 | print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) && |
| 857 | (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) && |
| 858 | (name->val.tag != MBEDTLS_ASN1_IA5_STRING); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 859 | |
Agathiyan Bragadeesh | c9d74f3 | 2023-07-31 17:25:44 +0100 | [diff] [blame] | 860 | if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) { |
| 861 | ret = mbedtls_snprintf(p, n, "%s=", short_name); |
| 862 | } else { |
Agathiyan Bragadeesh | af70c7d | 2023-08-10 16:39:23 +0100 | [diff] [blame] | 863 | if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) { |
Agathiyan Bragadeesh | f3b9724 | 2023-08-22 16:37:11 +0100 | [diff] [blame] | 864 | n -= ret; |
| 865 | p += ret; |
Agathiyan Bragadeesh | af70c7d | 2023-08-10 16:39:23 +0100 | [diff] [blame] | 866 | ret = mbedtls_snprintf(p, n, "="); |
Agathiyan Bragadeesh | c9d74f3 | 2023-07-31 17:25:44 +0100 | [diff] [blame] | 867 | print_hexstring = 1; |
Agathiyan Bragadeesh | 8aa74ab | 2023-08-22 16:42:27 +0100 | [diff] [blame] | 868 | } else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) { |
| 869 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame^] | 870 | } else { |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 871 | ret = mbedtls_snprintf(p, n, "\?\?="); |
| 872 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | } |
Agathiyan Bragadeesh | c9d74f3 | 2023-07-31 17:25:44 +0100 | [diff] [blame] | 874 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 875 | |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 876 | if (print_hexstring) { |
Agathiyan Bragadeesh | ee642d9 | 2023-08-10 14:08:27 +0100 | [diff] [blame] | 877 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 878 | s[0] = '#'; |
Agathiyan Bragadeesh | 0eb6673 | 2023-07-31 16:10:07 +0100 | [diff] [blame] | 879 | |
Agathiyan Bragadeesh | 5adffb2 | 2023-08-10 15:50:57 +0100 | [diff] [blame] | 880 | asn1_len_p = asn1_tag_len_buf + 10; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame^] | 881 | if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) { |
Agathiyan Bragadeesh | 07f472a | 2023-08-22 16:29:39 +0100 | [diff] [blame] | 882 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 883 | } |
| 884 | asn1_len_size = ret; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame^] | 885 | if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) { |
Agathiyan Bragadeesh | 07f472a | 2023-08-22 16:29:39 +0100 | [diff] [blame] | 886 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 887 | } |
| 888 | asn1_tag_size = ret; |
Agathiyan Bragadeesh | 5adffb2 | 2023-08-10 15:50:57 +0100 | [diff] [blame] | 889 | asn1_tag_len_buf_start = 10 - asn1_len_size - asn1_tag_size; |
| 890 | for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) { |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 891 | if (j + 1 >= sizeof(s) - 1) { |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 892 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 893 | } |
Agathiyan Bragadeesh | 5adffb2 | 2023-08-10 15:50:57 +0100 | [diff] [blame] | 894 | c = asn1_tag_len_buf[asn1_tag_len_buf_start+i]; |
Agathiyan Bragadeesh | a1f5c2d | 2023-08-02 17:08:52 +0100 | [diff] [blame] | 895 | lowbits = (c & 0x0F); |
Agathiyan Bragadeesh | 2bf09a6 | 2023-08-10 14:37:00 +0100 | [diff] [blame] | 896 | highbits = c >> 4; |
Agathiyan Bragadeesh | 9d2507c | 2023-07-24 16:35:57 +0100 | [diff] [blame] | 897 | s[j++] = nibble_to_hex_digit(highbits); |
Agathiyan Bragadeesh | 5adffb2 | 2023-08-10 15:50:57 +0100 | [diff] [blame] | 898 | s[j++] = nibble_to_hex_digit(lowbits); |
| 899 | } |
| 900 | for (i = 0; i < name->val.len; i++) { |
| 901 | if (j + 1 >= sizeof(s) - 1) { |
| 902 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 903 | } |
| 904 | c = name->val.p[i]; |
| 905 | lowbits = (c & 0x0F); |
| 906 | highbits = c >> 4; |
| 907 | s[j++] = nibble_to_hex_digit(highbits); |
| 908 | s[j++] = nibble_to_hex_digit(lowbits); |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 909 | } |
Agathiyan Bragadeesh | ee642d9 | 2023-08-10 14:08:27 +0100 | [diff] [blame] | 910 | #else |
Agathiyan Bragadeesh | 0eb6673 | 2023-07-31 16:10:07 +0100 | [diff] [blame] | 911 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
Agathiyan Bragadeesh | ee642d9 | 2023-08-10 14:08:27 +0100 | [diff] [blame] | 912 | #endif |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 913 | } else { |
| 914 | for (i = 0, j = 0; i < name->val.len; i++, j++) { |
| 915 | if (j >= sizeof(s) - 1) { |
| 916 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 917 | } |
| 918 | |
| 919 | c = name->val.p[i]; |
| 920 | // Special characters requiring escaping, RFC 4514 Section 2.4 |
Agathiyan Bragadeesh | 022f86f | 2023-08-22 16:56:04 +0100 | [diff] [blame] | 921 | if (c == '\0') { |
| 922 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame^] | 923 | } else { |
Agathiyan Bragadeesh | a7f9630 | 2023-08-10 16:03:27 +0100 | [diff] [blame] | 924 | if (strchr(",=+<>;\"\\", c) || |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 925 | ((i == 0) && strchr("# ", c)) || |
| 926 | ((i == name->val.len-1) && (c == ' '))) { |
| 927 | if (j + 1 >= sizeof(s) - 1) { |
| 928 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 929 | } |
| 930 | s[j++] = '\\'; |
| 931 | } |
| 932 | } |
| 933 | if (c < 32 || c >= 127) { |
| 934 | if (j + 3 >= sizeof(s) - 1) { |
| 935 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 936 | } |
| 937 | s[j++] = '\\'; |
Agathiyan Bragadeesh | a1f5c2d | 2023-08-02 17:08:52 +0100 | [diff] [blame] | 938 | lowbits = (c & 0x0F); |
Agathiyan Bragadeesh | 2bf09a6 | 2023-08-10 14:37:00 +0100 | [diff] [blame] | 939 | highbits = c >> 4; |
Agathiyan Bragadeesh | ddc720d | 2023-07-26 15:51:49 +0100 | [diff] [blame] | 940 | s[j++] = nibble_to_hex_digit(highbits); |
| 941 | s[j] = nibble_to_hex_digit(lowbits); |
| 942 | } else { |
| 943 | s[j] = c; |
| 944 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 945 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 946 | } |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 947 | s[j] = '\0'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 948 | ret = mbedtls_snprintf(p, n, "%s", s); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 949 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 950 | |
| 951 | merge = name->next_merged; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 952 | name = name->next; |
| 953 | } |
| 954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Store the serial in printable form into buf; no more |
| 960 | * than size characters will be written |
| 961 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 963 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 964 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 965 | size_t i, n, nr; |
| 966 | char *p; |
| 967 | |
| 968 | p = buf; |
| 969 | n = size; |
| 970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 971 | nr = (serial->len <= 32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 972 | ? serial->len : 28; |
| 973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | for (i = 0; i < nr; i++) { |
| 975 | if (i == 0 && nr > 1 && serial->p[i] == 0x0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 976 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | ret = mbedtls_snprintf(p, n, "%02X%s", |
| 980 | serial->p[i], (i < nr - 1) ? ":" : ""); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 981 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | if (nr != serial->len) { |
| 985 | ret = mbedtls_snprintf(p, n, "...."); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 986 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 992 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 993 | /* |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 994 | * Helper for writing signature algorithms |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 995 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 996 | int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid, |
| 997 | mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, |
| 998 | const void *sig_opts) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 999 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1000 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1001 | char *p = buf; |
| 1002 | size_t n = size; |
| 1003 | const char *desc = NULL; |
| 1004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc); |
| 1006 | if (ret != 0) { |
| 1007 | ret = mbedtls_snprintf(p, n, "???"); |
| 1008 | } else { |
| 1009 | ret = mbedtls_snprintf(p, n, "%s", desc); |
| 1010 | } |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1011 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1013 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | if (pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1015 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | const char *name = md_type_to_string(md_alg); |
| 1020 | const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1022 | ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)", |
| 1023 | name ? name : "???", |
| 1024 | mgf_name ? mgf_name : "???", |
| 1025 | (unsigned int) pss_opts->expected_salt_len); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1026 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1027 | } |
| 1028 | #else |
| 1029 | ((void) pk_alg); |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 1030 | ((void) md_alg); |
| 1031 | ((void) sig_opts); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | return (int) (size - n); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1035 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 1036 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1037 | |
| 1038 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1039 | * Helper for writing "RSA key size", "EC key size", etc |
| 1040 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1042 | { |
| 1043 | char *p = buf; |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 1044 | size_t n = buf_size; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1045 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | ret = mbedtls_snprintf(p, n, "%s key size", name); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1048 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1050 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Glenn Strauss | 416dc03 | 2022-06-30 00:38:53 -0400 | [diff] [blame] | 1053 | int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, |
| 1054 | const mbedtls_x509_time *t2) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1055 | { |
Glenn Strauss | 5aef297 | 2022-06-30 04:38:02 -0400 | [diff] [blame] | 1056 | int x; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1057 | |
Glenn Strauss | 5aef297 | 2022-06-30 04:38:02 -0400 | [diff] [blame] | 1058 | x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) - |
| 1059 | ((t2->year << 9) | (t2->mon << 5) | (t2->day))); |
| 1060 | if (x != 0) { |
| 1061 | return x; |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 1062 | } |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1063 | |
Glenn Strauss | 5aef297 | 2022-06-30 04:38:02 -0400 | [diff] [blame] | 1064 | x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) - |
| 1065 | ((t2->hour << 12) | (t2->min << 6) | (t2->sec))); |
| 1066 | return x; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1067 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1068 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1069 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Glenn Strauss | 61d9930 | 2022-06-30 05:25:56 -0400 | [diff] [blame] | 1070 | int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1071 | { |
Glenn Strauss | 811eeb2 | 2022-06-30 05:28:50 -0400 | [diff] [blame] | 1072 | struct tm tm; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1073 | |
Glenn Strauss | 811eeb2 | 2022-06-30 05:28:50 -0400 | [diff] [blame] | 1074 | if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) { |
| 1075 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1077 | |
Glenn Strauss | 811eeb2 | 2022-06-30 05:28:50 -0400 | [diff] [blame] | 1078 | now->year = tm.tm_year + 1900; |
| 1079 | now->mon = tm.tm_mon + 1; |
| 1080 | now->day = tm.tm_mday; |
| 1081 | now->hour = tm.tm_hour; |
| 1082 | now->min = tm.tm_min; |
| 1083 | now->sec = tm.tm_sec; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1084 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1085 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1086 | |
Glenn Strauss | 61d9930 | 2022-06-30 05:25:56 -0400 | [diff] [blame] | 1087 | static int x509_get_current_time(mbedtls_x509_time *now) |
| 1088 | { |
| 1089 | return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now); |
| 1090 | } |
| 1091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1093 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | if (x509_get_current_time(&now) != 0) { |
| 1097 | return 1; |
| 1098 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1099 | |
Glenn Strauss | 416dc03 | 2022-06-30 00:38:53 -0400 | [diff] [blame] | 1100 | return mbedtls_x509_time_cmp(to, &now) < 0; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1103 | int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | if (x509_get_current_time(&now) != 0) { |
| 1108 | return 1; |
| 1109 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1110 | |
Glenn Strauss | 416dc03 | 2022-06-30 00:38:53 -0400 | [diff] [blame] | 1111 | return mbedtls_x509_time_cmp(from, &now) > 0; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1114 | #else /* MBEDTLS_HAVE_TIME_DATE */ |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1116 | int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1117 | { |
| 1118 | ((void) to); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1120 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1123 | { |
| 1124 | ((void) from); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | return 0; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1126 | } |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1127 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1128 | |
| 1129 | /* Common functions for parsing CRT and CSR. */ |
| 1130 | #if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) |
| 1131 | /* |
| 1132 | * OtherName ::= SEQUENCE { |
| 1133 | * type-id OBJECT IDENTIFIER, |
| 1134 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
| 1135 | * |
| 1136 | * HardwareModuleName ::= SEQUENCE { |
| 1137 | * hwType OBJECT IDENTIFIER, |
| 1138 | * hwSerialNum OCTET STRING } |
| 1139 | * |
| 1140 | * NOTE: we currently only parse and use otherName of type HwModuleName, |
| 1141 | * as defined in RFC 4108. |
| 1142 | */ |
| 1143 | static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name, |
| 1144 | mbedtls_x509_san_other_name *other_name) |
| 1145 | { |
| 1146 | int ret = 0; |
| 1147 | size_t len; |
| 1148 | unsigned char *p = subject_alt_name->p; |
| 1149 | const unsigned char *end = p + subject_alt_name->len; |
| 1150 | mbedtls_x509_buf cur_oid; |
| 1151 | |
| 1152 | if ((subject_alt_name->tag & |
| 1153 | (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) != |
| 1154 | (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) { |
| 1155 | /* |
| 1156 | * The given subject alternative name is not of type "othername". |
| 1157 | */ |
| 1158 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1159 | } |
| 1160 | |
| 1161 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1162 | MBEDTLS_ASN1_OID)) != 0) { |
| 1163 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1164 | } |
| 1165 | |
| 1166 | cur_oid.tag = MBEDTLS_ASN1_OID; |
| 1167 | cur_oid.p = p; |
| 1168 | cur_oid.len = len; |
| 1169 | |
| 1170 | /* |
| 1171 | * Only HwModuleName is currently supported. |
| 1172 | */ |
| 1173 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) { |
| 1174 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 1175 | } |
| 1176 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1177 | p += len; |
| 1178 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1179 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != |
| 1180 | 0) { |
| 1181 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1182 | } |
| 1183 | |
Hanno Becker | dae916b | 2019-09-13 14:21:13 +0100 | [diff] [blame] | 1184 | if (end != p + len) { |
| 1185 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1186 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1187 | } |
| 1188 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1189 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1190 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1191 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1192 | } |
| 1193 | |
Hanno Becker | dae916b | 2019-09-13 14:21:13 +0100 | [diff] [blame] | 1194 | if (end != p + len) { |
| 1195 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1196 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1197 | } |
| 1198 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1199 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) { |
| 1200 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1201 | } |
| 1202 | |
| 1203 | other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID; |
| 1204 | other_name->value.hardware_module_name.oid.p = p; |
| 1205 | other_name->value.hardware_module_name.oid.len = len; |
| 1206 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1207 | p += len; |
| 1208 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1209 | MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1210 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1211 | } |
| 1212 | |
| 1213 | other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING; |
| 1214 | other_name->value.hardware_module_name.val.p = p; |
| 1215 | other_name->value.hardware_module_name.val.len = len; |
| 1216 | p += len; |
| 1217 | if (p != end) { |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1218 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1219 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1220 | } |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Przemek Stekiel | 21903ec | 2023-02-21 08:32:37 +0100 | [diff] [blame] | 1224 | /* Check mbedtls_x509_get_subject_alt_name for detailed description. |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1225 | * |
Przemek Stekiel | 21903ec | 2023-02-21 08:32:37 +0100 | [diff] [blame] | 1226 | * In some cases while parsing subject alternative names the sequence tag is optional |
| 1227 | * (e.g. CertSerialNumber). This function is designed to handle such case. |
Przemek Stekiel | 725688b | 2023-04-04 22:49:44 +0200 | [diff] [blame] | 1228 | */ |
Przemek Stekiel | 21903ec | 2023-02-21 08:32:37 +0100 | [diff] [blame] | 1229 | int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p, |
| 1230 | const unsigned char *end, |
| 1231 | mbedtls_x509_sequence *subject_alt_name) |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1232 | { |
| 1233 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 21903ec | 2023-02-21 08:32:37 +0100 | [diff] [blame] | 1234 | size_t tag_len; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1235 | mbedtls_asn1_sequence *cur = subject_alt_name; |
| 1236 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1237 | while (*p < end) { |
Andrzej Kurek | 154a605 | 2023-04-30 14:11:49 -0400 | [diff] [blame] | 1238 | mbedtls_x509_subject_alternative_name tmp_san_name; |
Hanno Becker | ae8f8c4 | 2019-09-13 12:24:56 +0100 | [diff] [blame] | 1239 | mbedtls_x509_buf tmp_san_buf; |
Andrzej Kurek | 154a605 | 2023-04-30 14:11:49 -0400 | [diff] [blame] | 1240 | memset(&tmp_san_name, 0, sizeof(tmp_san_name)); |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1241 | |
Hanno Becker | ae8f8c4 | 2019-09-13 12:24:56 +0100 | [diff] [blame] | 1242 | tmp_san_buf.tag = **p; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1243 | (*p)++; |
Hanno Becker | ae8f8c4 | 2019-09-13 12:24:56 +0100 | [diff] [blame] | 1244 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1245 | if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) { |
| 1246 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1247 | } |
| 1248 | |
Hanno Becker | ae8f8c4 | 2019-09-13 12:24:56 +0100 | [diff] [blame] | 1249 | tmp_san_buf.p = *p; |
| 1250 | tmp_san_buf.len = tag_len; |
| 1251 | |
| 1252 | if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) != |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1253 | MBEDTLS_ASN1_CONTEXT_SPECIFIC) { |
| 1254 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1255 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 1256 | } |
| 1257 | |
| 1258 | /* |
Andrzej Kurek | 154a605 | 2023-04-30 14:11:49 -0400 | [diff] [blame] | 1259 | * Check that the SAN is structured correctly by parsing it. |
| 1260 | * The SAN structure is discarded afterwards. |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1261 | */ |
Andrzej Kurek | 154a605 | 2023-04-30 14:11:49 -0400 | [diff] [blame] | 1262 | ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name); |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1263 | /* |
| 1264 | * In case the extension is malformed, return an error, |
| 1265 | * and clear the allocated sequences. |
| 1266 | */ |
| 1267 | if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) { |
| 1268 | mbedtls_asn1_sequence_free(subject_alt_name->next); |
| 1269 | subject_alt_name->next = NULL; |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
Andrzej Kurek | 154a605 | 2023-04-30 14:11:49 -0400 | [diff] [blame] | 1273 | mbedtls_x509_free_subject_alt_name(&tmp_san_name); |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1274 | /* Allocate and assign next pointer */ |
| 1275 | if (cur->buf.p != NULL) { |
| 1276 | if (cur->next != NULL) { |
| 1277 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS; |
| 1278 | } |
| 1279 | |
| 1280 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence)); |
| 1281 | |
| 1282 | if (cur->next == NULL) { |
| 1283 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1284 | MBEDTLS_ERR_ASN1_ALLOC_FAILED); |
| 1285 | } |
| 1286 | |
| 1287 | cur = cur->next; |
| 1288 | } |
| 1289 | |
Hanno Becker | ae8f8c4 | 2019-09-13 12:24:56 +0100 | [diff] [blame] | 1290 | cur->buf = tmp_san_buf; |
| 1291 | *p += tmp_san_buf.len; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* Set final sequence entry's next pointer to NULL */ |
| 1295 | cur->next = NULL; |
| 1296 | |
| 1297 | if (*p != end) { |
| 1298 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1299 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1300 | } |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
Przemek Stekiel | 21903ec | 2023-02-21 08:32:37 +0100 | [diff] [blame] | 1305 | /* |
| 1306 | * SubjectAltName ::= GeneralNames |
| 1307 | * |
| 1308 | * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 1309 | * |
| 1310 | * GeneralName ::= CHOICE { |
| 1311 | * otherName [0] OtherName, |
| 1312 | * rfc822Name [1] IA5String, |
| 1313 | * dNSName [2] IA5String, |
| 1314 | * x400Address [3] ORAddress, |
| 1315 | * directoryName [4] Name, |
| 1316 | * ediPartyName [5] EDIPartyName, |
| 1317 | * uniformResourceIdentifier [6] IA5String, |
| 1318 | * iPAddress [7] OCTET STRING, |
| 1319 | * registeredID [8] OBJECT IDENTIFIER } |
| 1320 | * |
| 1321 | * OtherName ::= SEQUENCE { |
| 1322 | * type-id OBJECT IDENTIFIER, |
| 1323 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
| 1324 | * |
| 1325 | * EDIPartyName ::= SEQUENCE { |
| 1326 | * nameAssigner [0] DirectoryString OPTIONAL, |
| 1327 | * partyName [1] DirectoryString } |
| 1328 | * |
| 1329 | * We list all types, but use the following GeneralName types from RFC 5280: |
| 1330 | * "dnsName", "uniformResourceIdentifier" and "hardware_module_name" |
| 1331 | * of type "otherName", as defined in RFC 4108. |
| 1332 | */ |
| 1333 | int mbedtls_x509_get_subject_alt_name(unsigned char **p, |
| 1334 | const unsigned char *end, |
| 1335 | mbedtls_x509_sequence *subject_alt_name) |
| 1336 | { |
| 1337 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1338 | size_t len; |
| 1339 | |
| 1340 | /* Get main sequence tag */ |
| 1341 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 1342 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1343 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1344 | } |
| 1345 | |
| 1346 | if (*p + len != end) { |
| 1347 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1348 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1349 | } |
| 1350 | |
| 1351 | return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name); |
| 1352 | } |
| 1353 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1354 | int mbedtls_x509_get_ns_cert_type(unsigned char **p, |
| 1355 | const unsigned char *end, |
| 1356 | unsigned char *ns_cert_type) |
| 1357 | { |
| 1358 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1359 | mbedtls_x509_bitstring bs = { 0, 0, NULL }; |
| 1360 | |
| 1361 | if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) { |
| 1362 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1363 | } |
| 1364 | |
Przemek Stekiel | 32e2091 | 2023-01-25 14:26:15 +0100 | [diff] [blame] | 1365 | /* A bitstring with no flags set is still technically valid, as it will mean |
| 1366 | that the certificate has no designated purpose at the time of creation. */ |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1367 | if (bs.len == 0) { |
| 1368 | *ns_cert_type = 0; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | if (bs.len != 1) { |
| 1373 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1374 | MBEDTLS_ERR_ASN1_INVALID_LENGTH); |
| 1375 | } |
| 1376 | |
| 1377 | /* Get actual bitstring */ |
| 1378 | *ns_cert_type = *bs.p; |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | int mbedtls_x509_get_key_usage(unsigned char **p, |
| 1383 | const unsigned char *end, |
| 1384 | unsigned int *key_usage) |
| 1385 | { |
| 1386 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1387 | size_t i; |
| 1388 | mbedtls_x509_bitstring bs = { 0, 0, NULL }; |
| 1389 | |
| 1390 | if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) { |
| 1391 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 1392 | } |
| 1393 | |
Przemek Stekiel | 32e2091 | 2023-01-25 14:26:15 +0100 | [diff] [blame] | 1394 | /* A bitstring with no flags set is still technically valid, as it will mean |
| 1395 | that the certificate has no designated purpose at the time of creation. */ |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1396 | if (bs.len == 0) { |
| 1397 | *key_usage = 0; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1401 | /* Get actual bitstring */ |
| 1402 | *key_usage = 0; |
| 1403 | for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) { |
| 1404 | *key_usage |= (unsigned int) bs.p[i] << (8*i); |
| 1405 | } |
| 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf, |
| 1411 | mbedtls_x509_subject_alternative_name *san) |
| 1412 | { |
| 1413 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1414 | switch (san_buf->tag & |
| 1415 | (MBEDTLS_ASN1_TAG_CLASS_MASK | |
| 1416 | MBEDTLS_ASN1_TAG_VALUE_MASK)) { |
| 1417 | /* |
| 1418 | * otherName |
| 1419 | */ |
| 1420 | case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME): |
| 1421 | { |
| 1422 | mbedtls_x509_san_other_name other_name; |
| 1423 | |
| 1424 | ret = x509_get_other_name(san_buf, &other_name); |
| 1425 | if (ret != 0) { |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name)); |
| 1430 | san->type = MBEDTLS_X509_SAN_OTHER_NAME; |
| 1431 | memcpy(&san->san.other_name, |
| 1432 | &other_name, sizeof(other_name)); |
| 1433 | |
| 1434 | } |
| 1435 | break; |
Andrzej Kurek | 7a05fab | 2023-02-13 10:03:07 -0500 | [diff] [blame] | 1436 | /* |
| 1437 | * uniformResourceIdentifier |
| 1438 | */ |
| 1439 | case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER): |
| 1440 | { |
| 1441 | memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name)); |
| 1442 | san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1443 | |
Andrzej Kurek | 7a05fab | 2023-02-13 10:03:07 -0500 | [diff] [blame] | 1444 | memcpy(&san->san.unstructured_name, |
| 1445 | san_buf, sizeof(*san_buf)); |
| 1446 | |
| 1447 | } |
| 1448 | break; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1449 | /* |
| 1450 | * dNSName |
| 1451 | */ |
| 1452 | case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME): |
| 1453 | { |
| 1454 | memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name)); |
| 1455 | san->type = MBEDTLS_X509_SAN_DNS_NAME; |
| 1456 | |
| 1457 | memcpy(&san->san.unstructured_name, |
| 1458 | san_buf, sizeof(*san_buf)); |
Przemek Stekiel | ecee12f | 2023-02-09 14:43:49 +0100 | [diff] [blame] | 1459 | } |
| 1460 | break; |
Przemek Stekiel | 0ab5b93 | 2023-05-29 16:30:50 +0200 | [diff] [blame] | 1461 | /* |
| 1462 | * IP address |
| 1463 | */ |
| 1464 | case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS): |
| 1465 | { |
| 1466 | memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name)); |
| 1467 | san->type = MBEDTLS_X509_SAN_IP_ADDRESS; |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1468 | // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported |
| 1469 | if (san_buf->len == 4 || san_buf->len == 16) { |
| 1470 | memcpy(&san->san.unstructured_name, |
| 1471 | san_buf, sizeof(*san_buf)); |
| 1472 | } else { |
| 1473 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1474 | } |
Przemek Stekiel | 0ab5b93 | 2023-05-29 16:30:50 +0200 | [diff] [blame] | 1475 | } |
| 1476 | break; |
Przemek Stekiel | ecee12f | 2023-02-09 14:43:49 +0100 | [diff] [blame] | 1477 | /* |
Andrzej Kurek | 154a605 | 2023-04-30 14:11:49 -0400 | [diff] [blame] | 1478 | * rfc822Name |
Przemek Stekiel | ecee12f | 2023-02-09 14:43:49 +0100 | [diff] [blame] | 1479 | */ |
| 1480 | case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME): |
| 1481 | { |
| 1482 | memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name)); |
| 1483 | san->type = MBEDTLS_X509_SAN_RFC822_NAME; |
| 1484 | memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf)); |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1485 | } |
| 1486 | break; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1487 | /* |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1488 | * directoryName |
| 1489 | */ |
| 1490 | case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME): |
| 1491 | { |
| 1492 | size_t name_len; |
| 1493 | unsigned char *p = san_buf->p; |
| 1494 | memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name)); |
| 1495 | san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME; |
| 1496 | |
| 1497 | ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len, |
| 1498 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 1499 | |
| 1500 | if (ret != 0) { |
Andrzej Kurek | bf8ccd8 | 2023-02-13 07:13:30 -0500 | [diff] [blame] | 1501 | return ret; |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | if ((ret = mbedtls_x509_get_name(&p, p + name_len, |
| 1505 | &san->san.directory_name)) != 0) { |
Andrzej Kurek | bf8ccd8 | 2023-02-13 07:13:30 -0500 | [diff] [blame] | 1506 | return ret; |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | break; |
| 1510 | /* |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1511 | * Type not supported |
| 1512 | */ |
| 1513 | default: |
| 1514 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 1515 | } |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
Andrzej Kurek | d40c2b6 | 2023-02-13 07:01:59 -0500 | [diff] [blame] | 1519 | void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san) |
| 1520 | { |
| 1521 | if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) { |
| 1522 | mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next); |
| 1523 | } |
| 1524 | } |
| 1525 | |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1526 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1527 | int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size, |
| 1528 | const mbedtls_x509_sequence |
| 1529 | *subject_alt_name, |
| 1530 | const char *prefix) |
| 1531 | { |
| 1532 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1533 | size_t i; |
| 1534 | size_t n = *size; |
| 1535 | char *p = *buf; |
| 1536 | const mbedtls_x509_sequence *cur = subject_alt_name; |
| 1537 | mbedtls_x509_subject_alternative_name san; |
| 1538 | int parse_ret; |
| 1539 | |
| 1540 | while (cur != NULL) { |
| 1541 | memset(&san, 0, sizeof(san)); |
| 1542 | parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san); |
| 1543 | if (parse_ret != 0) { |
| 1544 | if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) { |
| 1545 | ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix); |
| 1546 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1547 | } else { |
| 1548 | ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix); |
| 1549 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1550 | } |
| 1551 | cur = cur->next; |
| 1552 | continue; |
| 1553 | } |
| 1554 | |
| 1555 | switch (san.type) { |
| 1556 | /* |
| 1557 | * otherName |
| 1558 | */ |
| 1559 | case MBEDTLS_X509_SAN_OTHER_NAME: |
| 1560 | { |
| 1561 | mbedtls_x509_san_other_name *other_name = &san.san.other_name; |
| 1562 | |
| 1563 | ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix); |
| 1564 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1565 | |
| 1566 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, |
| 1567 | &other_name->value.hardware_module_name.oid) != 0) { |
| 1568 | ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix); |
| 1569 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1570 | ret = |
| 1571 | mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix); |
| 1572 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1573 | |
| 1574 | ret = mbedtls_oid_get_numeric_string(p, |
| 1575 | n, |
| 1576 | &other_name->value.hardware_module_name.oid); |
| 1577 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1578 | |
| 1579 | ret = |
| 1580 | mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix); |
| 1581 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1582 | |
| 1583 | for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) { |
| 1584 | ret = mbedtls_snprintf(p, |
| 1585 | n, |
| 1586 | "%02X", |
| 1587 | other_name->value.hardware_module_name.val.p[i]); |
| 1588 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1589 | } |
| 1590 | }/* MBEDTLS_OID_ON_HW_MODULE_NAME */ |
| 1591 | } |
| 1592 | break; |
Andrzej Kurek | 7a05fab | 2023-02-13 10:03:07 -0500 | [diff] [blame] | 1593 | /* |
| 1594 | * uniformResourceIdentifier |
| 1595 | */ |
| 1596 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 1597 | { |
| 1598 | ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix); |
| 1599 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1600 | if (san.san.unstructured_name.len >= n) { |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1601 | if (n > 0) { |
| 1602 | *p = '\0'; |
| 1603 | } |
Andrzej Kurek | 7a05fab | 2023-02-13 10:03:07 -0500 | [diff] [blame] | 1604 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 1605 | } |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1606 | |
Andrzej Kurek | 7a05fab | 2023-02-13 10:03:07 -0500 | [diff] [blame] | 1607 | memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len); |
| 1608 | p += san.san.unstructured_name.len; |
| 1609 | n -= san.san.unstructured_name.len; |
| 1610 | } |
| 1611 | break; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1612 | /* |
| 1613 | * dNSName |
Przemek Stekiel | 5b9e416 | 2023-02-15 12:56:37 +0100 | [diff] [blame] | 1614 | * RFC822 Name |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1615 | */ |
| 1616 | case MBEDTLS_X509_SAN_DNS_NAME: |
Przemek Stekiel | 5b9e416 | 2023-02-15 12:56:37 +0100 | [diff] [blame] | 1617 | case MBEDTLS_X509_SAN_RFC822_NAME: |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1618 | { |
Przemek Stekiel | 82d250d | 2023-02-15 15:00:50 +0100 | [diff] [blame] | 1619 | const char *dns_name = "dNSName"; |
| 1620 | const char *rfc822_name = "rfc822Name"; |
Przemek Stekiel | 5b9e416 | 2023-02-15 12:56:37 +0100 | [diff] [blame] | 1621 | |
Przemek Stekiel | 82d250d | 2023-02-15 15:00:50 +0100 | [diff] [blame] | 1622 | ret = mbedtls_snprintf(p, n, |
| 1623 | "\n%s %s : ", |
| 1624 | prefix, |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1625 | san.type == |
| 1626 | MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name); |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1627 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1628 | if (san.san.unstructured_name.len >= n) { |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1629 | if (n > 0) { |
| 1630 | *p = '\0'; |
| 1631 | } |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1632 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 1633 | } |
| 1634 | |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1635 | memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len); |
| 1636 | p += san.san.unstructured_name.len; |
| 1637 | n -= san.san.unstructured_name.len; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1638 | } |
| 1639 | break; |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1640 | /* |
| 1641 | * iPAddress |
| 1642 | */ |
| 1643 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
| 1644 | { |
| 1645 | ret = mbedtls_snprintf(p, n, "\n%s %s : ", |
| 1646 | prefix, "iPAddress"); |
| 1647 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1648 | if (san.san.unstructured_name.len >= n) { |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1649 | if (n > 0) { |
| 1650 | *p = '\0'; |
| 1651 | } |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1652 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 1653 | } |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1654 | |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1655 | unsigned char *ip = san.san.unstructured_name.p; |
| 1656 | // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported |
| 1657 | if (san.san.unstructured_name.len == 4) { |
Przemek Stekiel | 4d3fc21 | 2023-06-06 11:40:32 +0200 | [diff] [blame] | 1658 | ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); |
| 1659 | MBEDTLS_X509_SAFE_SNPRINTF; |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1660 | } else if (san.san.unstructured_name.len == 16) { |
Przemek Stekiel | 4d3fc21 | 2023-06-06 11:40:32 +0200 | [diff] [blame] | 1661 | ret = mbedtls_snprintf(p, n, |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1662 | "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X", |
| 1663 | ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], |
Przemek Stekiel | 4d3fc21 | 2023-06-06 11:40:32 +0200 | [diff] [blame] | 1664 | ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13], |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1665 | ip[14], ip[15]); |
Przemek Stekiel | 4d3fc21 | 2023-06-06 11:40:32 +0200 | [diff] [blame] | 1666 | MBEDTLS_X509_SAFE_SNPRINTF; |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1667 | } else { |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1668 | if (n > 0) { |
| 1669 | *p = '\0'; |
| 1670 | } |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1671 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1672 | } |
Przemek Stekiel | 093c97d | 2023-06-02 10:11:32 +0200 | [diff] [blame] | 1673 | } |
| 1674 | break; |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1675 | /* |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1676 | * directoryName |
| 1677 | */ |
| 1678 | case MBEDTLS_X509_SAN_DIRECTORY_NAME: |
| 1679 | { |
| 1680 | ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix); |
Andrzej Kurek | 5f0c6e8 | 2023-02-27 16:03:41 -0500 | [diff] [blame] | 1681 | if (ret < 0 || (size_t) ret >= n) { |
| 1682 | mbedtls_x509_free_subject_alt_name(&san); |
| 1683 | } |
| 1684 | |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1685 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1686 | ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name); |
Andrzej Kurek | d40c2b6 | 2023-02-13 07:01:59 -0500 | [diff] [blame] | 1687 | |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1688 | if (ret < 0) { |
Andrzej Kurek | 5f0c6e8 | 2023-02-27 16:03:41 -0500 | [diff] [blame] | 1689 | mbedtls_x509_free_subject_alt_name(&san); |
Przemek Stekiel | 01cb6eb | 2023-06-05 16:38:13 +0200 | [diff] [blame] | 1690 | if (n > 0) { |
| 1691 | *p = '\0'; |
| 1692 | } |
Andrzej Kurek | e12b01d | 2023-01-10 06:47:38 -0500 | [diff] [blame] | 1693 | return ret; |
| 1694 | } |
| 1695 | |
| 1696 | p += ret; |
| 1697 | n -= ret; |
| 1698 | } |
| 1699 | break; |
| 1700 | /* |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1701 | * Type not supported, skip item. |
| 1702 | */ |
| 1703 | default: |
| 1704 | ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix); |
| 1705 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1706 | break; |
| 1707 | } |
| 1708 | |
Andrzej Kurek | d40c2b6 | 2023-02-13 07:01:59 -0500 | [diff] [blame] | 1709 | /* So far memory is freed only in the case of directoryName |
Andrzej Kurek | 5f0c6e8 | 2023-02-27 16:03:41 -0500 | [diff] [blame] | 1710 | * parsing succeeding, as mbedtls_x509_get_name allocates memory. */ |
Andrzej Kurek | d40c2b6 | 2023-02-13 07:01:59 -0500 | [diff] [blame] | 1711 | mbedtls_x509_free_subject_alt_name(&san); |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1712 | cur = cur->next; |
| 1713 | } |
| 1714 | |
| 1715 | *p = '\0'; |
| 1716 | |
| 1717 | *size = n; |
| 1718 | *buf = p; |
| 1719 | |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
Demi Marie Obenour | 690b8c9 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 1723 | #define PRINT_ITEM(i) \ |
| 1724 | do { \ |
| 1725 | ret = mbedtls_snprintf(p, n, "%s" i, sep); \ |
| 1726 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
| 1727 | sep = ", "; \ |
| 1728 | } while (0) |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1729 | |
Demi Marie Obenour | 690b8c9 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 1730 | #define CERT_TYPE(type, name) \ |
| 1731 | do { \ |
| 1732 | if (ns_cert_type & (type)) { \ |
| 1733 | PRINT_ITEM(name); \ |
| 1734 | } \ |
| 1735 | } while (0) |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1736 | |
| 1737 | int mbedtls_x509_info_cert_type(char **buf, size_t *size, |
| 1738 | unsigned char ns_cert_type) |
| 1739 | { |
| 1740 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1741 | size_t n = *size; |
| 1742 | char *p = *buf; |
| 1743 | const char *sep = ""; |
| 1744 | |
| 1745 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client"); |
| 1746 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server"); |
| 1747 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email"); |
| 1748 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing"); |
| 1749 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved"); |
| 1750 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA"); |
| 1751 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA"); |
| 1752 | CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA"); |
| 1753 | |
| 1754 | *size = n; |
| 1755 | *buf = p; |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
Demi Marie Obenour | 690b8c9 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 1760 | #define KEY_USAGE(code, name) \ |
| 1761 | do { \ |
| 1762 | if ((key_usage) & (code)) { \ |
| 1763 | PRINT_ITEM(name); \ |
| 1764 | } \ |
| 1765 | } while (0) |
Przemek Stekiel | cf6ff0f | 2023-01-17 10:26:40 +0100 | [diff] [blame] | 1766 | |
| 1767 | int mbedtls_x509_info_key_usage(char **buf, size_t *size, |
| 1768 | unsigned int key_usage) |
| 1769 | { |
| 1770 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1771 | size_t n = *size; |
| 1772 | char *p = *buf; |
| 1773 | const char *sep = ""; |
| 1774 | |
| 1775 | KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature"); |
| 1776 | KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation"); |
| 1777 | KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment"); |
| 1778 | KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment"); |
| 1779 | KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement"); |
| 1780 | KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign"); |
| 1781 | KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign"); |
| 1782 | KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only"); |
| 1783 | KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only"); |
| 1784 | |
| 1785 | *size = n; |
| 1786 | *buf = p; |
| 1787 | |
| 1788 | return 0; |
| 1789 | } |
| 1790 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
| 1791 | #endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1792 | #endif /* MBEDTLS_X509_USE_C */ |