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