blob: 25eeed46659a2b350e90a3f5f1e9ca7f4f87619e [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker4593aea2009-02-09 22:32:35 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker4593aea2009-02-09 22:32:35 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_snprintf snprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \
38 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/certs.h"
40#include "mbedtls/x509_crt.h"
Rich Evans18b78c72015-02-11 14:06:19 +000041
Paul Bakker4593aea2009-02-09 22:32:35 +000042#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <string.h>
44#endif
45
Rich Evans18b78c72015-02-11 14:06:19 +000046#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
49 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_X509_CRL_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000050int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf("MBEDTLS_RSA_C and/or MBEDTLS_X509_CRT_PARSE_C "
53 "MBEDTLS_FS_IO and/or MBEDTLS_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020054 "not defined.\n");
55 return( 0 );
56}
57#else
Paul Bakkeref3f8c72013-06-24 13:01:08 +020058const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000059{
Paul Bakkerd98030e2009-05-02 15:13:40 +000060 "client1.crt",
61 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000062 "server1.crt",
63 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000064 "cert_sha224.crt",
65 "cert_sha256.crt",
66 "cert_sha384.crt",
67 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000068};
69
Paul Bakkeref3f8c72013-06-24 13:01:08 +020070const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000071{
Paul Bakkerd98030e2009-05-02 15:13:40 +000072 "client1.key",
73 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000074 "server1.key",
75 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000076 "cert_digest.key",
77 "cert_digest.key",
78 "cert_digest.key",
79 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000080};
81
Rich Evans85b05ec2015-02-12 11:37:29 +000082int main( void )
Paul Bakker4593aea2009-02-09 22:32:35 +000083{
84 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_x509_crt cacert;
86 mbedtls_x509_crl crl;
Paul Bakkerd98030e2009-05-02 15:13:40 +000087 char buf[10240];
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_x509_crt_init( &cacert );
90 mbedtls_x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000091
92 /*
93 * 1.1. Load the trusted CA
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_printf( "\n . Loading the CA root certificate ..." );
Paul Bakker4593aea2009-02-09 22:32:35 +000096 fflush( stdout );
97
Paul Bakker4593aea2009-02-09 22:32:35 +000098 /*
99 * Alternatively, you may load the CA certificates from a .pem or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * .crt file by calling mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000103 if( ret != 0 )
104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000106 goto exit;
107 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert );
112 mbedtls_printf("%s\n", buf );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000113
Paul Bakkerd98030e2009-05-02 15:13:40 +0000114 /*
115 * 1.2. Load the CRL
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000118 fflush( stdout );
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000121 if( ret != 0 )
122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000124 goto exit;
125 }
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl );
130 mbedtls_printf("%s\n", buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000131
Paul Bakker4593aea2009-02-09 22:32:35 +0000132 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
133 {
134 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000135 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000136 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000137 char name[512];
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200138 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_crt clicert;
140 mbedtls_pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_crt_init( &clicert );
143 mbedtls_pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " . Loading the client certificate %s...", name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000148 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 ret = mbedtls_x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000151 if( ret != 0 )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000154 goto exit;
155 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000158
159 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000160 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000163 fflush( stdout );
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200166 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000167 if( ret != 0 )
168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000170 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100171 char vrfy_buf[512];
172
173 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200174 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100175 mbedtls_printf( "%s\n", vrfy_buf );
176 }
177 else
178 {
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200179 mbedtls_printf( " failed\n ! mbedtls_x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000180 goto exit;
181 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000182 }
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000185
186 /*
187 * 1.5. Load own private key
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000192 fflush( stdout );
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 ret = mbedtls_pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000195 if( ret != 0 )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000198 goto exit;
199 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000202
Paul Bakkerd98030e2009-05-02 15:13:40 +0000203 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200204 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000207 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000208
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200209
210 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
214 ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200215 goto exit;
216 }
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000219 if( ret != 0 )
220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000222 goto exit;
223 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000226 if( ret != 0 )
227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000229 goto exit;
230 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000233 if( ret != 0 )
234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000236 goto exit;
237 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_crt_free( &clicert );
242 mbedtls_pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000243 }
244
245exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_crt_free( &cacert );
247 mbedtls_x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000248
Paul Bakkercce9d772011-11-18 14:26:47 +0000249#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000251 fflush( stdout ); getchar();
252#endif
253
254 return( ret );
255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
257 MBEDTLS_X509_CRL_PARSE_C */