blob: fd713f50f37fac8e0951d89abb79197954ba5c93 [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
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +010036#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
Simon Butcher63cb97e2018-12-06 17:43:31 +000084#if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C)
85void mbedtls_param_failed( char* failure_condition, char* file, int line )
86{
87 mbedtls_printf("%s:%i: Input param failed - %sn", file, line, failure_condition );
88 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
89}
90#endif
91
Rich Evans85b05ec2015-02-12 11:37:29 +000092int main( void )
Paul Bakker4593aea2009-02-09 22:32:35 +000093{
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +010094 int ret = 1, i;
95 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_x509_crt cacert;
97 mbedtls_x509_crl crl;
Paul Bakkerd98030e2009-05-02 15:13:40 +000098 char buf[10240];
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509_crt_init( &cacert );
101 mbedtls_x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000102
103 /*
104 * 1.1. Load the trusted CA
105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_printf( "\n . Loading the CA root certificate ..." );
Paul Bakker4593aea2009-02-09 22:32:35 +0000107 fflush( stdout );
108
Paul Bakker4593aea2009-02-09 22:32:35 +0000109 /*
110 * Alternatively, you may load the CA certificates from a .pem or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 * .crt file by calling mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000114 if( ret != 0 )
115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000117 goto exit;
118 }
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert );
123 mbedtls_printf("%s\n", buf );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000124
Paul Bakkerd98030e2009-05-02 15:13:40 +0000125 /*
126 * 1.2. Load the CRL
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000129 fflush( stdout );
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000132 if( ret != 0 )
133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl );
141 mbedtls_printf("%s\n", buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000142
Paul Bakker4593aea2009-02-09 22:32:35 +0000143 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
144 {
145 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000146 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000147 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000148 char name[512];
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200149 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_x509_crt clicert;
151 mbedtls_pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_x509_crt_init( &clicert );
154 mbedtls_pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " . Loading the client certificate %s...", name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000159 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 ret = mbedtls_x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000162 if( ret != 0 )
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000165 goto exit;
166 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000169
170 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000171 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000174 fflush( stdout );
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200177 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000178 if( ret != 0 )
179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000181 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100182 char vrfy_buf[512];
183
184 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200185 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100186 mbedtls_printf( "%s\n", vrfy_buf );
187 }
188 else
189 {
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200190 mbedtls_printf( " failed\n ! mbedtls_x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000191 goto exit;
192 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000193 }
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000196
197 /*
198 * 1.5. Load own private key
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000203 fflush( stdout );
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 ret = mbedtls_pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000206 if( ret != 0 )
207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000209 goto exit;
210 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000213
Paul Bakkerd98030e2009-05-02 15:13:40 +0000214 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200215 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000218 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000219
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200220
221 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200225 goto exit;
226 }
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000229 if( ret != 0 )
230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000232 goto exit;
233 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000236 if( ret != 0 )
237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000239 goto exit;
240 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000243 if( ret != 0 )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000246 goto exit;
247 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_x509_crt_free( &clicert );
252 mbedtls_pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000253 }
254
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +0100255 exit_code = MBEDTLS_EXIT_SUCCESS;
256
Paul Bakker4593aea2009-02-09 22:32:35 +0000257exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_x509_crt_free( &cacert );
259 mbedtls_x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000260
Paul Bakkercce9d772011-11-18 14:26:47 +0000261#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000263 fflush( stdout ); getchar();
264#endif
265
Andres Amaya Garcia357b0b22018-04-29 22:04:03 +0100266 return( exit_code );
Paul Bakker4593aea2009-02-09 22:32:35 +0000267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
269 MBEDTLS_X509_CRL_PARSE_C */