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