| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSL certificate functionality tests | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| 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 | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> | 
| Andres Amaya Garcia | 357b0b2 | 2018-04-29 22:04:03 +0100 | [diff] [blame] | 32 | #include <stdlib.h> | 
|  | 33 | #define mbedtls_snprintf        snprintf | 
|  | 34 | #define mbedtls_printf          printf | 
| Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS | 
| Andres Amaya Garcia | 357b0b2 | 2018-04-29 22:04:03 +0100 | [diff] [blame] | 36 | #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE | 
|  | 37 | #endif /* MBEDTLS_PLATFORM_C */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 40 | defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/certs.h" | 
|  | 42 | #include "mbedtls/x509_crt.h" | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 |  | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 44 | #include <stdio.h> | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 45 | #include <string.h> | 
|  | 46 | #endif | 
|  | 47 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 48 | #define MAX_CLIENT_CERTS    8 | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 49 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #if !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \ | 
|  | 51 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_X509_CRL_PARSE_C) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 52 | int main( void ) | 
| Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 53 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | mbedtls_printf("MBEDTLS_RSA_C and/or MBEDTLS_X509_CRT_PARSE_C " | 
|  | 55 | "MBEDTLS_FS_IO and/or MBEDTLS_X509_CRL_PARSE_C " | 
| Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 56 | "not defined.\n"); | 
|  | 57 | return( 0 ); | 
|  | 58 | } | 
|  | 59 | #else | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 60 | const char *client_certificates[MAX_CLIENT_CERTS] = | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 61 | { | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 62 | "client1.crt", | 
|  | 63 | "client2.crt", | 
| Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 64 | "server1.crt", | 
|  | 65 | "server2.crt", | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 66 | "cert_sha224.crt", | 
|  | 67 | "cert_sha256.crt", | 
|  | 68 | "cert_sha384.crt", | 
|  | 69 | "cert_sha512.crt" | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 70 | }; | 
|  | 71 |  | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 72 | const char *client_private_keys[MAX_CLIENT_CERTS] = | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 73 | { | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 74 | "client1.key", | 
|  | 75 | "client2.key", | 
| Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 76 | "server1.key", | 
|  | 77 | "server2.key", | 
| Paul Bakker | f17ed28 | 2011-02-09 17:10:48 +0000 | [diff] [blame] | 78 | "cert_digest.key", | 
|  | 79 | "cert_digest.key", | 
|  | 80 | "cert_digest.key", | 
|  | 81 | "cert_digest.key" | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 82 | }; | 
|  | 83 |  | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 84 | int main( void ) | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 85 | { | 
| Andres Amaya Garcia | 357b0b2 | 2018-04-29 22:04:03 +0100 | [diff] [blame] | 86 | int ret = 1, i; | 
|  | 87 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_x509_crt cacert; | 
|  | 89 | mbedtls_x509_crl crl; | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 90 | char buf[10240]; | 
|  | 91 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | mbedtls_x509_crt_init( &cacert ); | 
|  | 93 | mbedtls_x509_crl_init( &crl ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 94 |  | 
|  | 95 | /* | 
|  | 96 | * 1.1. Load the trusted CA | 
|  | 97 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | mbedtls_printf( "\n  . Loading the CA root certificate ..." ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 99 | fflush( stdout ); | 
|  | 100 |  | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 101 | /* | 
|  | 102 | * Alternatively, you may load the CA certificates from a .pem or | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | * .crt file by calling mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ). | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 104 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 106 | if( ret != 0 ) | 
|  | 107 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse_file returned %d\n\n", ret ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 109 | goto exit; | 
|  | 110 | } | 
|  | 111 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 113 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert ); | 
|  | 115 | mbedtls_printf("%s\n", buf ); | 
| Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 116 |  | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 117 | /* | 
|  | 118 | * 1.2. Load the CRL | 
|  | 119 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | mbedtls_printf( "  . Loading the CRL ..." ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 121 | fflush( stdout ); | 
|  | 122 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 124 | if( ret != 0 ) | 
|  | 125 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | mbedtls_printf( " failed\n  !  mbedtls_x509_crl_parse_file returned %d\n\n", ret ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 127 | goto exit; | 
|  | 128 | } | 
|  | 129 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 131 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl ); | 
|  | 133 | mbedtls_printf("%s\n", buf ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 134 |  | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 135 | for( i = 0; i < MAX_CLIENT_CERTS; i++ ) | 
|  | 136 | { | 
|  | 137 | /* | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 138 | * 1.3. Load own certificate | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 139 | */ | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 140 | char    name[512]; | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 141 | uint32_t flags; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | mbedtls_x509_crt clicert; | 
|  | 143 | mbedtls_pk_context pk; | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 144 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_x509_crt_init( &clicert ); | 
|  | 146 | mbedtls_pk_init( &pk ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 147 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 149 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | mbedtls_printf( "  . Loading the client certificate %s...", name ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 151 | fflush( stdout ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 152 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | ret = mbedtls_x509_crt_parse_file( &clicert, name ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 154 | if( ret != 0 ) | 
|  | 155 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse_file returned %d\n\n", ret ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 157 | goto exit; | 
|  | 158 | } | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 159 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 161 |  | 
|  | 162 | /* | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 163 | * 1.4. Verify certificate validity with CA certificate | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 164 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_printf( "  . Verify the client certificate with CA certificate..." ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 166 | fflush( stdout ); | 
|  | 167 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, | 
| Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 169 | NULL ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 170 | if( ret != 0 ) | 
|  | 171 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ) | 
| Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 173 | { | 
| Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 174 | char vrfy_buf[512]; | 
|  | 175 |  | 
|  | 176 | mbedtls_printf( " failed\n" ); | 
| Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 177 | mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), "  ! ", flags ); | 
| Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 178 | mbedtls_printf( "%s\n", vrfy_buf ); | 
|  | 179 | } | 
|  | 180 | else | 
|  | 181 | { | 
| Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 182 | mbedtls_printf( " failed\n  !  mbedtls_x509_crt_verify returned %d\n\n", ret ); | 
| Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 183 | goto exit; | 
|  | 184 | } | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | /* | 
|  | 190 | * 1.5. Load own private key | 
|  | 191 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 193 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | mbedtls_printf( "  . Loading the client private key %s...", name ); | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 195 | fflush( stdout ); | 
|  | 196 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | ret = mbedtls_pk_parse_keyfile( &pk, name, NULL ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 198 | if( ret != 0 ) | 
|  | 199 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | mbedtls_printf( " failed\n  !  mbedtls_pk_parse_keyfile returned %d\n\n", ret ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 201 | goto exit; | 
|  | 202 | } | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 203 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 205 |  | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 206 | /* | 
| Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 207 | * 1.6. Verify certificate validity with private key | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 208 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_printf( "  . Verify the client certificate with private key..." ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 210 | fflush( stdout ); | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 211 |  | 
| Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 212 |  | 
|  | 213 | /* EC NOT IMPLEMENTED YET */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) ) | 
| Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 215 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_printf( " failed\n  !  certificate's key is not RSA\n\n" ); | 
| Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 217 | goto exit; | 
|  | 218 | } | 
|  | 219 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 221 | if( ret != 0 ) | 
|  | 222 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | mbedtls_printf( " failed\n  !  mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 224 | goto exit; | 
|  | 225 | } | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 226 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 228 | if( ret != 0 ) | 
|  | 229 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | mbedtls_printf( " failed\n  !  mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 231 | goto exit; | 
|  | 232 | } | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 233 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 235 | if( ret != 0 ) | 
|  | 236 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | mbedtls_printf( " failed\n  !  mbedtls_rsa_check_privkey returned %d\n\n", ret ); | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 238 | goto exit; | 
|  | 239 | } | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | mbedtls_x509_crt_free( &clicert ); | 
|  | 244 | mbedtls_pk_free( &pk ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
| Andres Amaya Garcia | 357b0b2 | 2018-04-29 22:04:03 +0100 | [diff] [blame] | 247 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 248 |  | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 249 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_x509_crt_free( &cacert ); | 
|  | 251 | mbedtls_x509_crl_free( &crl ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 252 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 253 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_printf( "  + Press Enter to exit this program.\n" ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 255 | fflush( stdout ); getchar(); | 
|  | 256 | #endif | 
|  | 257 |  | 
| Andres Amaya Garcia | 357b0b2 | 2018-04-29 22:04:03 +0100 | [diff] [blame] | 258 | return( exit_code ); | 
| Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 259 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | #endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && | 
|  | 261 | MBEDTLS_X509_CRL_PARSE_C */ |