blob: 2553dba012651d2e0b118cb61a92097f5127f284 [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakker4593aea2009-02-09 22:32:35 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker4593aea2009-02-09 22:32:35 +000031
32#include <string.h>
33#include <stdio.h>
34
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020035#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020036 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020037int main( int argc, char *argv[] )
38{
39 ((void) argc);
40 ((void) argv);
41
42 printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020043 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020044 "not defined.\n");
45 return( 0 );
46}
47#else
48
Paul Bakker4593aea2009-02-09 22:32:35 +000049#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020050#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000051
Paul Bakkerd98030e2009-05-02 15:13:40 +000052#if defined _MSC_VER && !defined snprintf
53#define snprintf _snprintf
54#endif
55
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020056
Paul Bakker40ea7de2009-05-03 10:18:48 +000057#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000058
Paul Bakkeref3f8c72013-06-24 13:01:08 +020059const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000060{
Paul Bakkerd98030e2009-05-02 15:13:40 +000061 "client1.crt",
62 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000063 "server1.crt",
64 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000065 "cert_sha224.crt",
66 "cert_sha256.crt",
67 "cert_sha384.crt",
68 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000069};
70
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000072{
Paul Bakkerd98030e2009-05-02 15:13:40 +000073 "client1.key",
74 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000075 "server1.key",
76 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000077 "cert_digest.key",
78 "cert_digest.key",
79 "cert_digest.key",
80 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000081};
82
Paul Bakkercce9d772011-11-18 14:26:47 +000083int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000084{
85 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020086 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000087 x509_crl crl;
88 char buf[10240];
89
Paul Bakkercce9d772011-11-18 14:26:47 +000090 ((void) argc);
91 ((void) argv);
92
Paul Bakker369d2eb2013-09-18 11:58:25 +020093 x509_crt_init( &cacert );
94 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000095
96 /*
97 * 1.1. Load the trusted CA
98 */
99 printf( "\n . Loading the CA root certificate ..." );
100 fflush( stdout );
101
Paul Bakker4593aea2009-02-09 22:32:35 +0000102 /*
103 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +0200104 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000105 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200106 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000107 if( ret != 0 )
108 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000110 goto exit;
111 }
112
113 printf( " ok\n" );
114
Paul Bakkerddf26b42013-09-18 13:46:23 +0200115 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000116 printf("%s\n", buf );
117
Paul Bakkerd98030e2009-05-02 15:13:40 +0000118 /*
119 * 1.2. Load the CRL
120 */
121 printf( " . Loading the CRL ..." );
122 fflush( stdout );
123
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000125 if( ret != 0 )
126 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000128 goto exit;
129 }
130
131 printf( " ok\n" );
132
Paul Bakkerddf26b42013-09-18 13:46:23 +0200133 x509_crl_info( buf, 1024, "CRL: ", &crl );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000134 printf("%s\n", buf );
135
Paul Bakker4593aea2009-02-09 22:32:35 +0000136 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
137 {
138 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000139 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000140 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000141 char name[512];
142 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200143 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200144 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000145
Paul Bakker369d2eb2013-09-18 11:58:25 +0200146 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200147 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000148
149 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
150
151 printf( " . Loading the client certificate %s...", name );
152 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000153
Paul Bakkerddf26b42013-09-18 13:46:23 +0200154 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155 if( ret != 0 )
156 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200157 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000158 goto exit;
159 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000160
Paul Bakkerd98030e2009-05-02 15:13:40 +0000161 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000162
163 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000164 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000165 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000166 printf( " . Verify the client certificate with CA certificate..." );
167 fflush( stdout );
168
Paul Bakkerddf26b42013-09-18 13:46:23 +0200169 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
170 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000171 if( ret != 0 )
172 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000173 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
174 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000175 if( flags & BADCERT_CN_MISMATCH )
176 printf( " CN_MISMATCH " );
177 if( flags & BADCERT_EXPIRED )
178 printf( " EXPIRED " );
179 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000180 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000181 if( flags & BADCERT_NOT_TRUSTED )
182 printf( " NOT_TRUSTED " );
183 if( flags & BADCRL_NOT_TRUSTED )
184 printf( " CRL_NOT_TRUSTED " );
185 if( flags & BADCRL_EXPIRED )
186 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000187 } else {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200188 printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000189 goto exit;
190 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000191 }
192
193 printf( " ok\n" );
194
195 /*
196 * 1.5. Load own private key
197 */
198 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000199
200 printf( " . Loading the client private key %s...", name );
201 fflush( stdout );
202
Paul Bakker36713e82013-09-17 13:25:29 +0200203 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000204 if( ret != 0 )
205 {
Paul Bakker36713e82013-09-17 13:25:29 +0200206 printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000207 goto exit;
208 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000209
Paul Bakkerd98030e2009-05-02 15:13:40 +0000210 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000211
Paul Bakkerd98030e2009-05-02 15:13:40 +0000212 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200213 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000214 */
215 printf( " . Verify the client certificate with private key..." );
216 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000217
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200218
219 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200220 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200221 {
222 printf( " failed\n ! certificate's key is not RSA\n\n" );
223 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
224 goto exit;
225 }
226
Paul Bakker36713e82013-09-17 13:25:29 +0200227 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000228 if( ret != 0 )
229 {
230 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
231 goto exit;
232 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000233
Paul Bakker36713e82013-09-17 13:25:29 +0200234 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000235 if( ret != 0 )
236 {
237 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
238 goto exit;
239 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000240
Paul Bakker36713e82013-09-17 13:25:29 +0200241 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000242 if( ret != 0 )
243 {
244 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
245 goto exit;
246 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000247
Paul Bakkerd98030e2009-05-02 15:13:40 +0000248 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000249
Paul Bakker36713e82013-09-17 13:25:29 +0200250 x509_crt_free( &clicert );
251 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000252 }
253
254exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200255 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000256 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000257
Paul Bakkercce9d772011-11-18 14:26:47 +0000258#if defined(_WIN32)
Paul Bakker4593aea2009-02-09 22:32:35 +0000259 printf( " + Press Enter to exit this program.\n" );
260 fflush( stdout ); getchar();
261#endif
262
263 return( ret );
264}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200265#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
266 POLARSSL_X509_CRL_PARSE_C */