blob: f33d9a3a180e347fcf75ef419dad918a762fce22 [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
35#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \
39 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/certs.h"
41#include "mbedtls/x509_crt.h"
Rich Evans18b78c72015-02-11 14:06:19 +000042
Paul Bakker4593aea2009-02-09 22:32:35 +000043#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <string.h>
45#endif
46
47#if defined _MSC_VER && !defined snprintf
48#define snprintf _snprintf
49#endif
50
51#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
54 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_X509_CRL_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000055int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_printf("MBEDTLS_RSA_C and/or MBEDTLS_X509_CRT_PARSE_C "
58 "MBEDTLS_FS_IO and/or MBEDTLS_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020059 "not defined.\n");
60 return( 0 );
61}
62#else
Paul Bakkeref3f8c72013-06-24 13:01:08 +020063const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000064{
Paul Bakkerd98030e2009-05-02 15:13:40 +000065 "client1.crt",
66 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000067 "server1.crt",
68 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000069 "cert_sha224.crt",
70 "cert_sha256.crt",
71 "cert_sha384.crt",
72 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000073};
74
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000076{
Paul Bakkerd98030e2009-05-02 15:13:40 +000077 "client1.key",
78 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000079 "server1.key",
80 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000081 "cert_digest.key",
82 "cert_digest.key",
83 "cert_digest.key",
84 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000085};
86
Rich Evans85b05ec2015-02-12 11:37:29 +000087int main( void )
Paul Bakker4593aea2009-02-09 22:32:35 +000088{
89 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509_crt cacert;
91 mbedtls_x509_crl crl;
Paul Bakkerd98030e2009-05-02 15:13:40 +000092 char buf[10240];
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_x509_crt_init( &cacert );
95 mbedtls_x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000096
97 /*
98 * 1.1. Load the trusted CA
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( "\n . Loading the CA root certificate ..." );
Paul Bakker4593aea2009-02-09 22:32:35 +0000101 fflush( stdout );
102
Paul Bakker4593aea2009-02-09 22:32:35 +0000103 /*
104 * Alternatively, you may load the CA certificates from a .pem or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 * .crt file by calling mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000108 if( ret != 0 )
109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000111 goto exit;
112 }
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert );
117 mbedtls_printf("%s\n", buf );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000118
Paul Bakkerd98030e2009-05-02 15:13:40 +0000119 /*
120 * 1.2. Load the CRL
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000123 fflush( stdout );
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000126 if( ret != 0 )
127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000129 goto exit;
130 }
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl );
135 mbedtls_printf("%s\n", buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000136
Paul Bakker4593aea2009-02-09 22:32:35 +0000137 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
138 {
139 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000140 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000141 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000142 char name[512];
143 int flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_x509_crt clicert;
145 mbedtls_pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_crt_init( &clicert );
148 mbedtls_pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " . Loading the client certificate %s...", name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000153 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 ret = mbedtls_x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000156 if( ret != 0 )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000159 goto exit;
160 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000163
164 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000165 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000168 fflush( stdout );
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200171 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000172 if( ret != 0 )
173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000175 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100176 char vrfy_buf[512];
177
178 mbedtls_printf( " failed\n" );
179 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
180 mbedtls_printf( "%s\n", vrfy_buf );
181 }
182 else
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( " failed\n ! mbedtls_x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000185 goto exit;
186 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000187 }
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000190
191 /*
192 * 1.5. Load own private key
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000197 fflush( stdout );
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 ret = mbedtls_pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000200 if( ret != 0 )
201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000203 goto exit;
204 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000207
Paul Bakkerd98030e2009-05-02 15:13:40 +0000208 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200209 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000212 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000213
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200214
215 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
219 ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200220 goto exit;
221 }
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000224 if( ret != 0 )
225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000227 goto exit;
228 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000231 if( ret != 0 )
232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000234 goto exit;
235 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000238 if( ret != 0 )
239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000241 goto exit;
242 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_crt_free( &clicert );
247 mbedtls_pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000248 }
249
250exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_x509_crt_free( &cacert );
252 mbedtls_x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000253
Paul Bakkercce9d772011-11-18 14:26:47 +0000254#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000256 fflush( stdout ); getchar();
257#endif
258
259 return( ret );
260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
262 MBEDTLS_X509_CRL_PARSE_C */