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 | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 48 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 51 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 52 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 53 | #define mbedtls_printf printf |
| 54 | #define mbedtls_snprintf snprintf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 55 | #endif |
| 56 | |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_HAVE_TIME) |
| 58 | #include "mbedtls/platform_time.h" |
| 59 | #endif |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 60 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 61 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | #include <time.h> |
| 63 | #endif |
| 64 | |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 65 | #include "mbedtls/legacy_or_psa.h" |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 66 | |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 67 | #define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); } |
| 68 | #define CHECK_RANGE(min, max, val) \ |
| 69 | do \ |
| 70 | { \ |
| 71 | if( ( val ) < ( min ) || ( val ) > ( max ) ) \ |
| 72 | { \ |
| 73 | return( ret ); \ |
| 74 | } \ |
| 75 | } while( 0 ) |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 76 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 77 | /* |
| 78 | * CertificateSerialNumber ::= INTEGER |
| 79 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end, |
| 81 | mbedtls_x509_buf *serial ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 82 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 83 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 84 | |
| 85 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 86 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 87 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 88 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) && |
| 90 | **p != MBEDTLS_ASN1_INTEGER ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 91 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 92 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 93 | |
| 94 | serial->tag = *(*p)++; |
| 95 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 97 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | |
| 99 | serial->p = *p; |
| 100 | *p += serial->len; |
| 101 | |
| 102 | return( 0 ); |
| 103 | } |
| 104 | |
| 105 | /* Get an algorithm identifier without parameters (eg for signatures) |
| 106 | * |
| 107 | * AlgorithmIdentifier ::= SEQUENCE { |
| 108 | * algorithm OBJECT IDENTIFIER, |
| 109 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end, |
| 112 | mbedtls_x509_buf *alg ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 113 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 114 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 117 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 118 | |
| 119 | return( 0 ); |
| 120 | } |
| 121 | |
| 122 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 123 | * Parse an algorithm identifier with (optional) parameters |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 124 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end, |
| 126 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params ) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 127 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 128 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 131 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 132 | |
| 133 | return( 0 ); |
| 134 | } |
| 135 | |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Convert md type to string |
| 138 | */ |
| 139 | static inline const char* md_type_to_string( mbedtls_md_type_t md_alg ) |
| 140 | { |
| 141 | switch( md_alg ) |
| 142 | { |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 143 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 144 | case MBEDTLS_MD_MD5: |
| 145 | return( "MD5" ); |
| 146 | #endif |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 148 | case MBEDTLS_MD_SHA1: |
| 149 | return( "SHA1" ); |
| 150 | #endif |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 151 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 152 | case MBEDTLS_MD_SHA224: |
| 153 | return( "SHA224" ); |
| 154 | #endif |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 156 | case MBEDTLS_MD_SHA256: |
| 157 | return( "SHA256" ); |
| 158 | #endif |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 159 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 160 | case MBEDTLS_MD_SHA384: |
| 161 | return( "SHA384" ); |
| 162 | #endif |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 163 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 164 | case MBEDTLS_MD_SHA512: |
| 165 | return( "SHA512" ); |
| 166 | #endif |
Przemek Stekiel | fd18366 | 2022-08-02 15:29:20 +0200 | [diff] [blame] | 167 | #if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA) |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 168 | case MBEDTLS_MD_RIPEMD160: |
| 169 | return( "RIPEMD160" ); |
| 170 | #endif |
| 171 | case MBEDTLS_MD_NONE: |
| 172 | return( NULL ); |
| 173 | default: |
| 174 | return( NULL ); |
| 175 | } |
| 176 | } |
| 177 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 179 | /* |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 180 | * HashAlgorithm ::= AlgorithmIdentifier |
| 181 | * |
| 182 | * AlgorithmIdentifier ::= SEQUENCE { |
| 183 | * algorithm OBJECT IDENTIFIER, |
| 184 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 185 | * |
| 186 | * For HashAlgorithm, parameters MUST be NULL or absent. |
| 187 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | 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] | 189 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 190 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 191 | unsigned char *p; |
| 192 | const unsigned char *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | mbedtls_x509_buf md_oid; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 194 | size_t len; |
| 195 | |
| 196 | /* Make sure we got a SEQUENCE and setup bounds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 198 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 199 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 200 | |
Andrzej Kurek | feaebc5 | 2020-07-16 04:37:41 -0400 | [diff] [blame] | 201 | p = alg->p; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 202 | end = p + alg->len; |
| 203 | |
| 204 | if( p >= end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 205 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 206 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 207 | |
| 208 | /* Parse md_oid */ |
| 209 | md_oid.tag = *p; |
| 210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 212 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 213 | |
| 214 | md_oid.p = p; |
| 215 | p += md_oid.len; |
| 216 | |
| 217 | /* Get md_alg from md_oid */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 219 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 220 | |
| 221 | /* Make sure params is absent of NULL */ |
| 222 | if( p == end ) |
| 223 | return( 0 ); |
| 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 226 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 227 | |
| 228 | if( p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 229 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 230 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 231 | |
| 232 | return( 0 ); |
| 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 |
| 245 | * option. Enfore this at parsing time. |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 246 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [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, |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 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 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 264 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 265 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 266 | |
| 267 | p = (unsigned char *) params->p; |
| 268 | end = p + params->len; |
| 269 | |
| 270 | if( p == end ) |
| 271 | return( 0 ); |
| 272 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 273 | /* |
| 274 | * HashAlgorithm |
| 275 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 277 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 278 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 279 | end2 = p + len; |
| 280 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 281 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 ) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 283 | return( ret ); |
| 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 286 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 287 | |
| 288 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 289 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 290 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 291 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 293 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 294 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 295 | if( p == end ) |
| 296 | return( 0 ); |
| 297 | |
| 298 | /* |
| 299 | * MaskGenAlgorithm |
| 300 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 302 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 303 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 304 | end2 = p + len; |
| 305 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 306 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 ) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 308 | return( ret ); |
| 309 | |
| 310 | /* Only MFG1 is recognised for now */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 312 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE, |
| 313 | MBEDTLS_ERR_OID_NOT_FOUND ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 314 | |
| 315 | /* Parse HashAlgorithm */ |
| 316 | if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 ) |
| 317 | return( ret ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 318 | |
| 319 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 320 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 321 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 322 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 324 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 325 | |
| 326 | if( p == end ) |
| 327 | return( 0 ); |
| 328 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 329 | /* |
| 330 | * salt_len |
| 331 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 333 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 334 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 335 | end2 = p + len; |
| 336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 338 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 339 | |
| 340 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 341 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 342 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 343 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 345 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 346 | |
| 347 | if( p == end ) |
| 348 | return( 0 ); |
| 349 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 350 | /* |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 351 | * trailer_field (if present, must be 1) |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 352 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 354 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 355 | { |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 356 | int trailer_field; |
| 357 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 358 | end2 = p + len; |
| 359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 361 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 362 | |
| 363 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 364 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 365 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 366 | |
| 367 | if( trailer_field != 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 369 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 370 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 371 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 372 | |
| 373 | if( p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 374 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 375 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 376 | |
| 377 | return( 0 ); |
| 378 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 380 | |
| 381 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 382 | * AttributeTypeAndValue ::= SEQUENCE { |
| 383 | * type AttributeType, |
| 384 | * value AttributeValue } |
| 385 | * |
| 386 | * AttributeType ::= OBJECT IDENTIFIER |
| 387 | * |
| 388 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 389 | */ |
| 390 | static int x509_get_attr_type_value( unsigned char **p, |
| 391 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | mbedtls_x509_name *cur ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 393 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 394 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 395 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | mbedtls_x509_buf *oid; |
| 397 | mbedtls_x509_buf *val; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 400 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 401 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 402 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 403 | end = *p + len; |
| 404 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 405 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 406 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 407 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 408 | |
| 409 | oid = &cur->oid; |
| 410 | oid->tag = **p; |
| 411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 413 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 414 | |
| 415 | oid->p = *p; |
| 416 | *p += oid->len; |
| 417 | |
| 418 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 419 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 420 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING && |
| 423 | **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING && |
| 424 | **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING && |
| 425 | **p != MBEDTLS_ASN1_BIT_STRING ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 426 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 427 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | |
| 429 | val = &cur->val; |
| 430 | val->tag = *(*p)++; |
| 431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 433 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 434 | |
| 435 | val->p = *p; |
| 436 | *p += val->len; |
| 437 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 438 | if( *p != end ) |
| 439 | { |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 440 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 441 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 444 | cur->next = NULL; |
| 445 | |
| 446 | return( 0 ); |
| 447 | } |
| 448 | |
| 449 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 450 | * Name ::= CHOICE { -- only one possibility for now -- |
| 451 | * rdnSequence RDNSequence } |
| 452 | * |
| 453 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 454 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 455 | * RelativeDistinguishedName ::= |
| 456 | * SET OF AttributeTypeAndValue |
| 457 | * |
| 458 | * AttributeTypeAndValue ::= SEQUENCE { |
| 459 | * type AttributeType, |
| 460 | * value AttributeValue } |
| 461 | * |
| 462 | * AttributeType ::= OBJECT IDENTIFIER |
| 463 | * |
| 464 | * AttributeValue ::= ANY DEFINED BY AttributeType |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 465 | * |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 466 | * The data structure is optimized for the common case where each RDN has only |
| 467 | * one element, which is represented as a list of AttributeTypeAndValue. |
| 468 | * For the general case we still use a flat list, but we mark elements of the |
| 469 | * 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] | 470 | * this list, eg mbedtls_x509_dn_gets(). |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 471 | * |
David Horstmann | 11307a1 | 2022-10-17 18:10:23 +0100 | [diff] [blame] | 472 | * 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] | 473 | * that must later be free'd by the caller using mbedtls_free(). In error |
| 474 | * cases, this function frees all allocated memory internally and the caller |
| 475 | * has no freeing responsibilities. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 476 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end, |
| 478 | mbedtls_x509_name *cur ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 479 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 480 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 481 | size_t set_len; |
| 482 | const unsigned char *end_set; |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 483 | mbedtls_x509_name *head = cur; |
| 484 | mbedtls_x509_name *prev, *allocated; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 485 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 486 | /* don't use recursion, we'd risk stack overflow if not optimized */ |
| 487 | while( 1 ) |
| 488 | { |
| 489 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 490 | * parse SET |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 491 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len, |
| 493 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 ) |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 494 | { |
| 495 | ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ); |
| 496 | goto error; |
| 497 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 498 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 499 | end_set = *p + set_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 500 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 501 | while( 1 ) |
| 502 | { |
| 503 | if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 ) |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 504 | goto error; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 505 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 506 | if( *p == end_set ) |
| 507 | break; |
| 508 | |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 509 | /* 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] | 510 | cur->next_merged = 1; |
| 511 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 512 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 513 | |
| 514 | if( cur->next == NULL ) |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 515 | { |
| 516 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 517 | goto error; |
| 518 | } |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 519 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 520 | cur = cur->next; |
| 521 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 522 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 523 | /* |
| 524 | * continue until end of SEQUENCE is reached |
| 525 | */ |
| 526 | if( *p == end ) |
| 527 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 528 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 529 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 530 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 531 | if( cur->next == NULL ) |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 532 | { |
| 533 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 534 | goto error; |
| 535 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 536 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 537 | cur = cur->next; |
| 538 | } |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 539 | |
| 540 | error: |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 541 | /* Skip the first element as we did not allocate it */ |
| 542 | allocated = head->next; |
| 543 | |
David Horstmann | ed79483 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 544 | while( allocated != NULL ) |
| 545 | { |
| 546 | prev = allocated; |
| 547 | allocated = allocated->next; |
| 548 | |
| 549 | mbedtls_platform_zeroize( prev, sizeof( *prev ) ); |
| 550 | mbedtls_free( prev ); |
| 551 | } |
| 552 | |
| 553 | mbedtls_platform_zeroize( head, sizeof( *head ) ); |
| 554 | |
David Horstmann | 078250e | 2022-10-18 18:11:13 +0100 | [diff] [blame^] | 555 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 558 | static int x509_parse_int( unsigned char **p, size_t n, int *res ) |
| 559 | { |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 560 | *res = 0; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 561 | |
| 562 | for( ; n > 0; --n ) |
| 563 | { |
| 564 | if( ( **p < '0') || ( **p > '9' ) ) |
| 565 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 566 | |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 567 | *res *= 10; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 568 | *res += ( *(*p)++ - '0' ); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 569 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 570 | |
| 571 | return( 0 ); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 574 | static int x509_date_is_valid(const mbedtls_x509_time *t ) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 575 | { |
| 576 | int ret = MBEDTLS_ERR_X509_INVALID_DATE; |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 577 | int month_len; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 578 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 579 | CHECK_RANGE( 0, 9999, t->year ); |
| 580 | CHECK_RANGE( 0, 23, t->hour ); |
| 581 | CHECK_RANGE( 0, 59, t->min ); |
| 582 | CHECK_RANGE( 0, 59, t->sec ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 583 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 584 | switch( t->mon ) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 585 | { |
| 586 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 587 | month_len = 31; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 588 | break; |
| 589 | case 4: case 6: case 9: case 11: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 590 | month_len = 30; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 591 | break; |
| 592 | case 2: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 593 | if( ( !( t->year % 4 ) && t->year % 100 ) || |
| 594 | !( t->year % 400 ) ) |
| 595 | month_len = 29; |
| 596 | else |
| 597 | month_len = 28; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 598 | break; |
| 599 | default: |
| 600 | return( ret ); |
| 601 | } |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 602 | CHECK_RANGE( 1, month_len, t->day ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 603 | |
| 604 | return( 0 ); |
| 605 | } |
| 606 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 607 | /* |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 608 | * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) |
| 609 | * field. |
| 610 | */ |
| 611 | static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 612 | mbedtls_x509_time *tm ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 613 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 614 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | * Minimum length is 10 or 12 depending on yearlen |
| 618 | */ |
| 619 | if ( len < yearlen + 8 ) |
| 620 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 621 | len -= yearlen + 8; |
| 622 | |
| 623 | /* |
| 624 | * Parse year, month, day, hour, minute |
| 625 | */ |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 626 | CHECK( x509_parse_int( p, yearlen, &tm->year ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 627 | if ( 2 == yearlen ) |
| 628 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 629 | if ( tm->year < 50 ) |
| 630 | tm->year += 100; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 631 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 632 | tm->year += 1900; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 635 | CHECK( x509_parse_int( p, 2, &tm->mon ) ); |
| 636 | CHECK( x509_parse_int( p, 2, &tm->day ) ); |
| 637 | CHECK( x509_parse_int( p, 2, &tm->hour ) ); |
| 638 | CHECK( x509_parse_int( p, 2, &tm->min ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * Parse seconds if present |
| 642 | */ |
| 643 | if ( len >= 2 ) |
| 644 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 645 | CHECK( x509_parse_int( p, 2, &tm->sec ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 646 | len -= 2; |
| 647 | } |
| 648 | else |
| 649 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 650 | |
| 651 | /* |
| 652 | * Parse trailing 'Z' if present |
| 653 | */ |
| 654 | if ( 1 == len && 'Z' == **p ) |
| 655 | { |
| 656 | (*p)++; |
| 657 | len--; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * We should have parsed all characters at this point |
| 662 | */ |
| 663 | if ( 0 != len ) |
| 664 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 665 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 666 | CHECK( x509_date_is_valid( tm ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 667 | |
| 668 | return ( 0 ); |
| 669 | } |
| 670 | |
| 671 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 672 | * Time ::= CHOICE { |
| 673 | * utcTime UTCTime, |
| 674 | * generalTime GeneralizedTime } |
| 675 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 676 | int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 677 | mbedtls_x509_time *tm ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 678 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 679 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 680 | size_t len, year_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 681 | unsigned char tag; |
| 682 | |
| 683 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 684 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, |
| 685 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 686 | |
| 687 | tag = **p; |
| 688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | if( tag == MBEDTLS_ASN1_UTC_TIME ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 690 | year_len = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 692 | year_len = 4; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 693 | else |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 694 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, |
| 695 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 696 | |
| 697 | (*p)++; |
| 698 | ret = mbedtls_asn1_get_len( p, end, &len ); |
| 699 | |
| 700 | if( ret != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 701 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 702 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 703 | return x509_parse_time( p, len, year_len, tm ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | 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] | 707 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 708 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 709 | size_t len; |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 710 | int tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 711 | |
| 712 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 713 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, |
| 714 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 715 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 716 | tag_type = **p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 719 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 720 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 721 | sig->tag = tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 722 | sig->len = len; |
| 723 | sig->p = *p; |
| 724 | |
| 725 | *p += len; |
| 726 | |
| 727 | return( 0 ); |
| 728 | } |
| 729 | |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 730 | /* |
| 731 | * Get signature algorithm from alg OID and optional parameters |
| 732 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
| 734 | mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 735 | void **sig_opts ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 736 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 737 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 738 | |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 739 | if( *sig_opts != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 742 | if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 743 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 746 | if( *pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 747 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 749 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 750 | pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 751 | if( pss_opts == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 752 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 753 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | ret = mbedtls_x509_get_rsassa_pss_params( sig_params, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 755 | md_alg, |
| 756 | &pss_opts->mgf1_hash_id, |
| 757 | &pss_opts->expected_salt_len ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 758 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | mbedtls_free( pss_opts ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 761 | return( ret ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 762 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 763 | |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 764 | *sig_opts = (void *) pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 765 | } |
| 766 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 768 | { |
| 769 | /* Make sure parameters are absent or NULL */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) || |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 771 | sig_params->len != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 772 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 775 | return( 0 ); |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * X.509 Extensions (No parsing of extensions, pointer should |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 780 | * be either manually updated or extensions should be parsed!) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 781 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 783 | mbedtls_x509_buf *ext, int tag ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 784 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 785 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 786 | size_t len; |
| 787 | |
Hanno Becker | 3cddba8 | 2019-02-11 14:33:36 +0000 | [diff] [blame] | 788 | /* Extension structure use EXPLICIT tagging. That is, the actual |
| 789 | * `Extensions` structure is wrapped by a tag-length pair using |
| 790 | * the respective context-specific tag. */ |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 791 | ret = mbedtls_asn1_get_tag( p, end, &ext->len, |
| 792 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ); |
| 793 | if( ret != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 794 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 795 | |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 796 | ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag; |
| 797 | ext->p = *p; |
| 798 | end = *p + ext->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 802 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 804 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 805 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 806 | |
| 807 | if( end != *p + len ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 808 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 809 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 810 | |
| 811 | return( 0 ); |
| 812 | } |
| 813 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 814 | /* |
| 815 | * Store the name in printable form into buf; no more |
| 816 | * than size characters will be written |
| 817 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | 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] | 819 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 820 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 821 | size_t i, j, n; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 822 | unsigned char c, merge = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | const mbedtls_x509_name *name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 824 | const char *short_name = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 826 | |
| 827 | memset( s, 0, sizeof( s ) ); |
| 828 | |
| 829 | name = dn; |
| 830 | p = buf; |
| 831 | n = size; |
| 832 | |
| 833 | while( name != NULL ) |
| 834 | { |
| 835 | if( !name->oid.p ) |
| 836 | { |
| 837 | name = name->next; |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | if( name != dn ) |
| 842 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 843 | ret = mbedtls_snprintf( p, n, merge ? " + " : ", " ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 844 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 847 | ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 848 | |
| 849 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 850 | ret = mbedtls_snprintf( p, n, "%s=", short_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 851 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 852 | ret = mbedtls_snprintf( p, n, "\?\?=" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 853 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 854 | |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 855 | for( i = 0, j = 0; i < name->val.len; i++, j++ ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 856 | { |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 857 | if( j >= sizeof( s ) - 1 ) |
| 858 | return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 859 | |
| 860 | c = name->val.p[i]; |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 861 | // Special characters requiring escaping, RFC 1779 |
| 862 | if( c && strchr( ",=+<>#;\"\\", c ) ) |
| 863 | { |
| 864 | if( j + 1 >= sizeof( s ) - 1 ) |
Werner Lewis | 9b0e940 | 2022-06-27 12:01:22 +0100 | [diff] [blame] | 865 | return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 866 | s[j++] = '\\'; |
| 867 | } |
Koh M. Nakagawa | 46b8782 | 2020-05-16 10:08:09 +0900 | [diff] [blame] | 868 | if( c < 32 || c >= 127 ) |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 869 | s[j] = '?'; |
| 870 | else s[j] = c; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 871 | } |
Werner Lewis | b33dacd | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 872 | s[j] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 873 | ret = mbedtls_snprintf( p, n, "%s", s ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 874 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 875 | |
| 876 | merge = name->next_merged; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 877 | name = name->next; |
| 878 | } |
| 879 | |
| 880 | return( (int) ( size - n ) ); |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Store the serial in printable form into buf; no more |
| 885 | * than size characters will be written |
| 886 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | 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] | 888 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 889 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 890 | size_t i, n, nr; |
| 891 | char *p; |
| 892 | |
| 893 | p = buf; |
| 894 | n = size; |
| 895 | |
| 896 | nr = ( serial->len <= 32 ) |
| 897 | ? serial->len : 28; |
| 898 | |
| 899 | for( i = 0; i < nr; i++ ) |
| 900 | { |
| 901 | if( i == 0 && nr > 1 && serial->p[i] == 0x0 ) |
| 902 | continue; |
| 903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | ret = mbedtls_snprintf( p, n, "%02X%s", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 905 | serial->p[i], ( i < nr - 1 ) ? ":" : "" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 906 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | if( nr != serial->len ) |
| 910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | ret = mbedtls_snprintf( p, n, "...." ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 912 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | return( (int) ( size - n ) ); |
| 916 | } |
| 917 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 918 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 919 | /* |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 920 | * Helper for writing signature algorithms |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 921 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 922 | int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid, |
| 923 | mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 924 | const void *sig_opts ) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 925 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 926 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 927 | char *p = buf; |
| 928 | size_t n = size; |
| 929 | const char *desc = NULL; |
| 930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 932 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 933 | ret = mbedtls_snprintf( p, n, "???" ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 934 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | ret = mbedtls_snprintf( p, n, "%s", desc ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 936 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 938 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 939 | if( pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 940 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 941 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 944 | |
Przemek Stekiel | 9e30fc9 | 2022-06-27 12:48:35 +0200 | [diff] [blame] | 945 | const char *name = md_type_to_string( md_alg ); |
| 946 | 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] | 947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", |
Przemek Stekiel | 6230d0d | 2022-06-27 11:19:04 +0200 | [diff] [blame] | 949 | name ? name : "???", |
| 950 | mgf_name ? mgf_name : "???", |
Kenneth Soerensen | 518d435 | 2020-04-01 17:22:45 +0200 | [diff] [blame] | 951 | (unsigned int) pss_opts->expected_salt_len ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 952 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 953 | } |
| 954 | #else |
| 955 | ((void) pk_alg); |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 956 | ((void) md_alg); |
| 957 | ((void) sig_opts); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 959 | |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 960 | return( (int)( size - n ) ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 961 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 962 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 963 | |
| 964 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 965 | * Helper for writing "RSA key size", "EC key size", etc |
| 966 | */ |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 967 | 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] | 968 | { |
| 969 | char *p = buf; |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 970 | size_t n = buf_size; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 971 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 972 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | ret = mbedtls_snprintf( p, n, "%s key size", name ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 974 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 975 | |
| 976 | return( 0 ); |
| 977 | } |
| 978 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 979 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 980 | /* |
| 981 | * Set the time structure to the current time. |
| 982 | * Return 0 on success, non-zero on failure. |
| 983 | */ |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 984 | static int x509_get_current_time( mbedtls_x509_time *now ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 985 | { |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 986 | struct tm *lt, tm_buf; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 987 | mbedtls_time_t tt; |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 988 | int ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 989 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 990 | tt = mbedtls_time( NULL ); |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 991 | lt = mbedtls_platform_gmtime_r( &tt, &tm_buf ); |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 992 | |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 993 | if( lt == NULL ) |
| 994 | ret = -1; |
| 995 | else |
| 996 | { |
| 997 | now->year = lt->tm_year + 1900; |
| 998 | now->mon = lt->tm_mon + 1; |
| 999 | now->day = lt->tm_mday; |
| 1000 | now->hour = lt->tm_hour; |
| 1001 | now->min = lt->tm_min; |
| 1002 | now->sec = lt->tm_sec; |
| 1003 | } |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1004 | |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 1005 | return( ret ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1006 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1007 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1008 | /* |
| 1009 | * Return 0 if before <= after, 1 otherwise |
| 1010 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1012 | { |
| 1013 | if( before->year > after->year ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1014 | return( 1 ); |
| 1015 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1016 | if( before->year == after->year && |
| 1017 | before->mon > after->mon ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1018 | return( 1 ); |
| 1019 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1020 | if( before->year == after->year && |
| 1021 | before->mon == after->mon && |
| 1022 | before->day > after->day ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1023 | return( 1 ); |
| 1024 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1025 | if( before->year == after->year && |
| 1026 | before->mon == after->mon && |
| 1027 | before->day == after->day && |
| 1028 | before->hour > after->hour ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1029 | return( 1 ); |
| 1030 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1031 | if( before->year == after->year && |
| 1032 | before->mon == after->mon && |
| 1033 | before->day == after->day && |
| 1034 | before->hour == after->hour && |
| 1035 | before->min > after->min ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1036 | return( 1 ); |
| 1037 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1038 | if( before->year == after->year && |
| 1039 | before->mon == after->mon && |
| 1040 | before->day == after->day && |
| 1041 | before->hour == after->hour && |
| 1042 | before->min == after->min && |
| 1043 | before->sec > after->sec ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1044 | return( 1 ); |
| 1045 | |
| 1046 | return( 0 ); |
| 1047 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1048 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1049 | 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] | 1050 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1051 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1052 | |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1053 | if( x509_get_current_time( &now ) != 0 ) |
Manuel Pégourié-Gonnard | e7e8984 | 2015-06-22 19:15:32 +0200 | [diff] [blame] | 1054 | return( 1 ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1055 | |
| 1056 | return( x509_check_time( &now, to ) ); |
| 1057 | } |
| 1058 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1059 | 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] | 1060 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1061 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1062 | |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1063 | if( x509_get_current_time( &now ) != 0 ) |
Manuel Pégourié-Gonnard | e7e8984 | 2015-06-22 19:15:32 +0200 | [diff] [blame] | 1064 | return( 1 ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1065 | |
| 1066 | return( x509_check_time( from, &now ) ); |
| 1067 | } |
| 1068 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1069 | #else /* MBEDTLS_HAVE_TIME_DATE */ |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1070 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1071 | int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1072 | { |
| 1073 | ((void) to); |
| 1074 | return( 0 ); |
| 1075 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1076 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1077 | 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] | 1078 | { |
| 1079 | ((void) from); |
| 1080 | return( 0 ); |
| 1081 | } |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1082 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | #endif /* MBEDTLS_X509_USE_C */ |