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