blob: 7e5ed384b2a9c3a54e6c205f2378c595985b3635 [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker4593aea2009-02-09 22:32:35 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker4593aea2009-02-09 22:32:35 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +010032#include <stdlib.h>
33#define mbedtls_snprintf snprintf
34#define mbedtls_printf printf
35#define MBEDTLS_EXTI_SUCCESS EXIT_SUCCESS
36#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#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é-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/certs.h"
42#include "mbedtls/x509_crt.h"
Rich Evans18b78c72015-02-11 14:06:19 +000043
Paul Bakker4593aea2009-02-09 22:32:35 +000044#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <string.h>
46#endif
47
Rich Evans18b78c72015-02-11 14:06:19 +000048#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
51 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_X509_CRL_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000052int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 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é-Gonnard7831b0c2013-09-20 12:29:56 +020056 "not defined.\n");
57 return( 0 );
58}
59#else
Paul Bakkeref3f8c72013-06-24 13:01:08 +020060const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000061{
Paul Bakkerd98030e2009-05-02 15:13:40 +000062 "client1.crt",
63 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000064 "server1.crt",
65 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000066 "cert_sha224.crt",
67 "cert_sha256.crt",
68 "cert_sha384.crt",
69 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000070};
71
Paul Bakkeref3f8c72013-06-24 13:01:08 +020072const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000073{
Paul Bakkerd98030e2009-05-02 15:13:40 +000074 "client1.key",
75 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000076 "server1.key",
77 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000078 "cert_digest.key",
79 "cert_digest.key",
80 "cert_digest.key",
81 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000082};
83
Rich Evans85b05ec2015-02-12 11:37:29 +000084int main( void )
Paul Bakker4593aea2009-02-09 22:32:35 +000085{
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +010086 int ret = 1, i;
87 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_x509_crt cacert;
89 mbedtls_x509_crl crl;
Paul Bakkerd98030e2009-05-02 15:13:40 +000090 char buf[10240];
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_x509_crt_init( &cacert );
93 mbedtls_x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000094
95 /*
96 * 1.1. Load the trusted CA
97 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_printf( "\n . Loading the CA root certificate ..." );
Paul Bakker4593aea2009-02-09 22:32:35 +000099 fflush( stdout );
100
Paul Bakker4593aea2009-02-09 22:32:35 +0000101 /*
102 * Alternatively, you may load the CA certificates from a .pem or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * .crt file by calling mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000106 if( ret != 0 )
107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000109 goto exit;
110 }
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert );
115 mbedtls_printf("%s\n", buf );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000116
Paul Bakkerd98030e2009-05-02 15:13:40 +0000117 /*
118 * 1.2. Load the CRL
119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000121 fflush( stdout );
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000124 if( ret != 0 )
125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000127 goto exit;
128 }
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl );
133 mbedtls_printf("%s\n", buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000134
Paul Bakker4593aea2009-02-09 22:32:35 +0000135 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
136 {
137 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000138 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000139 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000140 char name[512];
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200141 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_crt clicert;
143 mbedtls_pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_crt_init( &clicert );
146 mbedtls_pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " . Loading the client certificate %s...", name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000151 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 ret = mbedtls_x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000154 if( ret != 0 )
155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000157 goto exit;
158 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000161
162 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000163 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000166 fflush( stdout );
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200169 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000170 if( ret != 0 )
171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000173 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100174 char vrfy_buf[512];
175
176 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200177 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100178 mbedtls_printf( "%s\n", vrfy_buf );
179 }
180 else
181 {
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200182 mbedtls_printf( " failed\n ! mbedtls_x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000183 goto exit;
184 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000185 }
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000188
189 /*
190 * 1.5. Load own private key
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000195 fflush( stdout );
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 ret = mbedtls_pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000198 if( ret != 0 )
199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000201 goto exit;
202 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000205
Paul Bakkerd98030e2009-05-02 15:13:40 +0000206 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200207 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000210 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000211
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200212
213 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200217 goto exit;
218 }
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000221 if( ret != 0 )
222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000224 goto exit;
225 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000228 if( ret != 0 )
229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000231 goto exit;
232 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000235 if( ret != 0 )
236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000238 goto exit;
239 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509_crt_free( &clicert );
244 mbedtls_pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000245 }
246
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +0100247 exit_code = MBEDTLS_EXIT_SUCCESS;
248
Paul Bakker4593aea2009-02-09 22:32:35 +0000249exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_x509_crt_free( &cacert );
251 mbedtls_x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000252
Paul Bakkercce9d772011-11-18 14:26:47 +0000253#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000255 fflush( stdout ); getchar();
256#endif
257
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +0100258 return( exit_code );
Paul Bakker4593aea2009-02-09 22:32:35 +0000259}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
261 MBEDTLS_X509_CRL_PARSE_C */