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