blob: 3e006d32d95d02aa1e6da8858ec9fb3a9eba2a3d [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
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_RSA_C) && defined(POLARSSL_X509_CRT_PARSE_C) &&\
37 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)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020053int main( int argc, char *argv[] )
54{
55 ((void) argc);
56 ((void) argv);
57
Rich Evansf90016a2015-01-19 14:26:37 +000058 polarssl_printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020059 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020060 "not defined.\n");
61 return( 0 );
62}
63#else
Paul Bakkeref3f8c72013-06-24 13:01:08 +020064const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000065{
Paul Bakkerd98030e2009-05-02 15:13:40 +000066 "client1.crt",
67 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000068 "server1.crt",
69 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000070 "cert_sha224.crt",
71 "cert_sha256.crt",
72 "cert_sha384.crt",
73 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000074};
75
Paul Bakkeref3f8c72013-06-24 13:01:08 +020076const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000077{
Paul Bakkerd98030e2009-05-02 15:13:40 +000078 "client1.key",
79 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000080 "server1.key",
81 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000082 "cert_digest.key",
83 "cert_digest.key",
84 "cert_digest.key",
85 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000086};
87
Paul Bakkercce9d772011-11-18 14:26:47 +000088int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000089{
90 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020091 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000092 x509_crl crl;
93 char buf[10240];
94
Paul Bakkercce9d772011-11-18 14:26:47 +000095 ((void) argc);
96 ((void) argv);
97
Paul Bakker369d2eb2013-09-18 11:58:25 +020098 x509_crt_init( &cacert );
99 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000100
101 /*
102 * 1.1. Load the trusted CA
103 */
Rich Evansf90016a2015-01-19 14:26:37 +0000104 polarssl_printf( "\n . Loading the CA root certificate ..." );
Paul Bakker4593aea2009-02-09 22:32:35 +0000105 fflush( stdout );
106
Paul Bakker4593aea2009-02-09 22:32:35 +0000107 /*
108 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000110 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200111 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000112 if( ret != 0 )
113 {
Rich Evansf90016a2015-01-19 14:26:37 +0000114 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000115 goto exit;
116 }
117
Rich Evansf90016a2015-01-19 14:26:37 +0000118 polarssl_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000119
Paul Bakkerddf26b42013-09-18 13:46:23 +0200120 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Rich Evansf90016a2015-01-19 14:26:37 +0000121 polarssl_printf("%s\n", buf );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000122
Paul Bakkerd98030e2009-05-02 15:13:40 +0000123 /*
124 * 1.2. Load the CRL
125 */
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000127 fflush( stdout );
128
Paul Bakkerddf26b42013-09-18 13:46:23 +0200129 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000130 if( ret != 0 )
131 {
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000133 goto exit;
134 }
135
Rich Evansf90016a2015-01-19 14:26:37 +0000136 polarssl_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000137
Paul Bakkerddf26b42013-09-18 13:46:23 +0200138 x509_crl_info( buf, 1024, "CRL: ", &crl );
Rich Evansf90016a2015-01-19 14:26:37 +0000139 polarssl_printf("%s\n", buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000140
Paul Bakker4593aea2009-02-09 22:32:35 +0000141 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
142 {
143 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000144 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000145 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000146 char name[512];
147 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200148 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200149 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000150
Paul Bakker369d2eb2013-09-18 11:58:25 +0200151 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200152 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000153
154 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
155
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( " . Loading the client certificate %s...", name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000157 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000158
Paul Bakkerddf26b42013-09-18 13:46:23 +0200159 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000160 if( ret != 0 )
161 {
Rich Evansf90016a2015-01-19 14:26:37 +0000162 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000163 goto exit;
164 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000165
Rich Evansf90016a2015-01-19 14:26:37 +0000166 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000167
168 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000169 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000170 */
Rich Evansf90016a2015-01-19 14:26:37 +0000171 polarssl_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000172 fflush( stdout );
173
Paul Bakkerddf26b42013-09-18 13:46:23 +0200174 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
175 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000176 if( ret != 0 )
177 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000178 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
179 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000180 if( flags & BADCERT_CN_MISMATCH )
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_printf( " CN_MISMATCH " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000182 if( flags & BADCERT_EXPIRED )
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " EXPIRED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000184 if( flags & BADCERT_REVOKED )
Rich Evansf90016a2015-01-19 14:26:37 +0000185 polarssl_printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000186 if( flags & BADCERT_NOT_TRUSTED )
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " NOT_TRUSTED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000188 if( flags & BADCRL_NOT_TRUSTED )
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " CRL_NOT_TRUSTED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000190 if( flags & BADCRL_EXPIRED )
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000192 } else {
Rich Evansf90016a2015-01-19 14:26:37 +0000193 polarssl_printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000194 goto exit;
195 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000196 }
197
Rich Evansf90016a2015-01-19 14:26:37 +0000198 polarssl_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000199
200 /*
201 * 1.5. Load own private key
202 */
203 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000204
Rich Evansf90016a2015-01-19 14:26:37 +0000205 polarssl_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000206 fflush( stdout );
207
Paul Bakker36713e82013-09-17 13:25:29 +0200208 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000209 if( ret != 0 )
210 {
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000212 goto exit;
213 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000214
Rich Evansf90016a2015-01-19 14:26:37 +0000215 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000216
Paul Bakkerd98030e2009-05-02 15:13:40 +0000217 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200218 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000219 */
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000221 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000222
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200223
224 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200225 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200226 {
Rich Evansf90016a2015-01-19 14:26:37 +0000227 polarssl_printf( " failed\n ! certificate's key is not RSA\n\n" );
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200228 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
229 goto exit;
230 }
231
Paul Bakker36713e82013-09-17 13:25:29 +0200232 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000233 if( ret != 0 )
234 {
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000236 goto exit;
237 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000238
Paul Bakker36713e82013-09-17 13:25:29 +0200239 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000240 if( ret != 0 )
241 {
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000243 goto exit;
244 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000245
Paul Bakker36713e82013-09-17 13:25:29 +0200246 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000247 if( ret != 0 )
248 {
Rich Evansf90016a2015-01-19 14:26:37 +0000249 polarssl_printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000250 goto exit;
251 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000252
Rich Evansf90016a2015-01-19 14:26:37 +0000253 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000254
Paul Bakker36713e82013-09-17 13:25:29 +0200255 x509_crt_free( &clicert );
256 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000257 }
258
259exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200260 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000261 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000262
Paul Bakkercce9d772011-11-18 14:26:47 +0000263#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000264 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000265 fflush( stdout ); getchar();
266#endif
267
268 return( ret );
269}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200270#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
271 POLARSSL_X509_CRL_PARSE_C */