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