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