blob: 393651fca91b473ec4da3aa3d501a876b388e05e [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker4593aea2009-02-09 22:32:35 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000036#if defined(POLARSSL_RSA_C) && defined(POLARSSL_X509_CRT_PARSE_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000037 defined(POLARSSL_FS_IO) && defined(POLARSSL_X509_CRL_PARSE_C)
38#include "polarssl/certs.h"
39#include "polarssl/x509_crt.h"
40
Paul Bakker4593aea2009-02-09 22:32:35 +000041#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000042#include <string.h>
43#endif
44
45#if defined _MSC_VER && !defined snprintf
46#define snprintf _snprintf
47#endif
48
49#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000050
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020051#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020052 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000053int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020054{
Rich Evansf90016a2015-01-19 14:26:37 +000055 polarssl_printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020056 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020057 "not defined.\n");
58 return( 0 );
59}
60#else
Paul Bakkeref3f8c72013-06-24 13:01:08 +020061const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000062{
Paul Bakkerd98030e2009-05-02 15:13:40 +000063 "client1.crt",
64 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000065 "server1.crt",
66 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000067 "cert_sha224.crt",
68 "cert_sha256.crt",
69 "cert_sha384.crt",
70 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000071};
72
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000074{
Paul Bakkerd98030e2009-05-02 15:13:40 +000075 "client1.key",
76 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000077 "server1.key",
78 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000079 "cert_digest.key",
80 "cert_digest.key",
81 "cert_digest.key",
82 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000083};
84
Rich Evans85b05ec2015-02-12 11:37:29 +000085int main( void )
Paul Bakker4593aea2009-02-09 22:32:35 +000086{
87 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020088 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000089 x509_crl crl;
90 char buf[10240];
91
Paul Bakker369d2eb2013-09-18 11:58:25 +020092 x509_crt_init( &cacert );
93 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000094
95 /*
96 * 1.1. Load the trusted CA
97 */
Rich Evansf90016a2015-01-19 14:26:37 +000098 polarssl_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
Paul Bakkerddf26b42013-09-18 13:46:23 +0200103 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000104 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200105 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000106 if( ret != 0 )
107 {
Rich Evansf90016a2015-01-19 14:26:37 +0000108 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000109 goto exit;
110 }
111
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000113
Paul Bakkerddf26b42013-09-18 13:46:23 +0200114 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_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 */
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000121 fflush( stdout );
122
Paul Bakkerddf26b42013-09-18 13:46:23 +0200123 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000124 if( ret != 0 )
125 {
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000127 goto exit;
128 }
129
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000131
Paul Bakkerddf26b42013-09-18 13:46:23 +0200132 x509_crl_info( buf, 1024, "CRL: ", &crl );
Rich Evansf90016a2015-01-19 14:26:37 +0000133 polarssl_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];
141 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200142 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200143 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000144
Paul Bakker369d2eb2013-09-18 11:58:25 +0200145 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200146 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000147
148 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
149
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_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
Paul Bakkerddf26b42013-09-18 13:46:23 +0200153 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000154 if( ret != 0 )
155 {
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( " failed\n ! 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
Rich Evansf90016a2015-01-19 14:26:37 +0000160 polarssl_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 */
Rich Evansf90016a2015-01-19 14:26:37 +0000165 polarssl_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000166 fflush( stdout );
167
Paul Bakkerddf26b42013-09-18 13:46:23 +0200168 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
169 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000170 if( ret != 0 )
171 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000172 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
173 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000174 if( flags & BADCERT_CN_MISMATCH )
Rich Evansf90016a2015-01-19 14:26:37 +0000175 polarssl_printf( " CN_MISMATCH " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000176 if( flags & BADCERT_EXPIRED )
Rich Evansf90016a2015-01-19 14:26:37 +0000177 polarssl_printf( " EXPIRED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000178 if( flags & BADCERT_REVOKED )
Rich Evansf90016a2015-01-19 14:26:37 +0000179 polarssl_printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000180 if( flags & BADCERT_NOT_TRUSTED )
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_printf( " NOT_TRUSTED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000182 if( flags & BADCRL_NOT_TRUSTED )
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " CRL_NOT_TRUSTED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000184 if( flags & BADCRL_EXPIRED )
Rich Evansf90016a2015-01-19 14:26:37 +0000185 polarssl_printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000186 } else {
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000188 goto exit;
189 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000190 }
191
Rich Evansf90016a2015-01-19 14:26:37 +0000192 polarssl_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000193
194 /*
195 * 1.5. Load own private key
196 */
197 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000198
Rich Evansf90016a2015-01-19 14:26:37 +0000199 polarssl_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000200 fflush( stdout );
201
Paul Bakker36713e82013-09-17 13:25:29 +0200202 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000203 if( ret != 0 )
204 {
Rich Evansf90016a2015-01-19 14:26:37 +0000205 polarssl_printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000206 goto exit;
207 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000208
Rich Evansf90016a2015-01-19 14:26:37 +0000209 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000210
Paul Bakkerd98030e2009-05-02 15:13:40 +0000211 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200212 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000213 */
Rich Evansf90016a2015-01-19 14:26:37 +0000214 polarssl_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000215 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000216
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200217
218 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200219 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200220 {
Rich Evansf90016a2015-01-19 14:26:37 +0000221 polarssl_printf( " failed\n ! certificate's key is not RSA\n\n" );
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200222 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
223 goto exit;
224 }
225
Paul Bakker36713e82013-09-17 13:25:29 +0200226 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000227 if( ret != 0 )
228 {
Rich Evansf90016a2015-01-19 14:26:37 +0000229 polarssl_printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000230 goto exit;
231 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000232
Paul Bakker36713e82013-09-17 13:25:29 +0200233 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000234 if( ret != 0 )
235 {
Rich Evansf90016a2015-01-19 14:26:37 +0000236 polarssl_printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000237 goto exit;
238 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000239
Paul Bakker36713e82013-09-17 13:25:29 +0200240 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000241 if( ret != 0 )
242 {
Rich Evansf90016a2015-01-19 14:26:37 +0000243 polarssl_printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000244 goto exit;
245 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000246
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000248
Paul Bakker36713e82013-09-17 13:25:29 +0200249 x509_crt_free( &clicert );
250 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000251 }
252
253exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200254 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000255 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000256
Paul Bakkercce9d772011-11-18 14:26:47 +0000257#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000258 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000259 fflush( stdout ); getchar();
260#endif
261
262 return( ret );
263}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200264#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
265 POLARSSL_X509_CRL_PARSE_C */