blob: c41b1be9bff5bc8aafe72aa275f21a55524951be [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdio.h>
31
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020032#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020033 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020034int main( int argc, char *argv[] )
35{
36 ((void) argc);
37 ((void) argv);
38
39 printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020040 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020041 "not defined.\n");
42 return( 0 );
43}
44#else
45
Paul Bakker4593aea2009-02-09 22:32:35 +000046#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020047#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000048
Paul Bakkerd98030e2009-05-02 15:13:40 +000049#if defined _MSC_VER && !defined snprintf
50#define snprintf _snprintf
51#endif
52
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020053
Paul Bakker40ea7de2009-05-03 10:18:48 +000054#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000055
Paul Bakkeref3f8c72013-06-24 13:01:08 +020056const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000057{
Paul Bakkerd98030e2009-05-02 15:13:40 +000058 "client1.crt",
59 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000060 "server1.crt",
61 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000062 "cert_sha224.crt",
63 "cert_sha256.crt",
64 "cert_sha384.crt",
65 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000066};
67
Paul Bakkeref3f8c72013-06-24 13:01:08 +020068const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000069{
Paul Bakkerd98030e2009-05-02 15:13:40 +000070 "client1.key",
71 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000072 "server1.key",
73 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000074 "cert_digest.key",
75 "cert_digest.key",
76 "cert_digest.key",
77 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000078};
79
Paul Bakkercce9d772011-11-18 14:26:47 +000080int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000081{
82 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020083 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000084 x509_crl crl;
85 char buf[10240];
86
Paul Bakkercce9d772011-11-18 14:26:47 +000087 ((void) argc);
88 ((void) argv);
89
Paul Bakker369d2eb2013-09-18 11:58:25 +020090 x509_crt_init( &cacert );
91 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000092
93 /*
94 * 1.1. Load the trusted CA
95 */
96 printf( "\n . Loading the CA root certificate ..." );
97 fflush( stdout );
98
Paul Bakker4593aea2009-02-09 22:32:35 +000099 /*
100 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +0200101 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000102 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200103 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000104 if( ret != 0 )
105 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200106 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000107 goto exit;
108 }
109
110 printf( " ok\n" );
111
Paul Bakkerddf26b42013-09-18 13:46:23 +0200112 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000113 printf("%s\n", buf );
114
Paul Bakkerd98030e2009-05-02 15:13:40 +0000115 /*
116 * 1.2. Load the CRL
117 */
118 printf( " . Loading the CRL ..." );
119 fflush( stdout );
120
Paul Bakkerddf26b42013-09-18 13:46:23 +0200121 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000122 if( ret != 0 )
123 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124 printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000125 goto exit;
126 }
127
128 printf( " ok\n" );
129
Paul Bakkerddf26b42013-09-18 13:46:23 +0200130 x509_crl_info( buf, 1024, "CRL: ", &crl );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000131 printf("%s\n", buf );
132
Paul Bakker4593aea2009-02-09 22:32:35 +0000133 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
134 {
135 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000136 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000137 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000138 char name[512];
139 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200140 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200141 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000142
Paul Bakker369d2eb2013-09-18 11:58:25 +0200143 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200144 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000145
146 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
147
148 printf( " . Loading the client certificate %s...", name );
149 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000150
Paul Bakkerddf26b42013-09-18 13:46:23 +0200151 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000152 if( ret != 0 )
153 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200154 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155 goto exit;
156 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000157
Paul Bakkerd98030e2009-05-02 15:13:40 +0000158 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000159
160 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000161 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000162 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000163 printf( " . Verify the client certificate with CA certificate..." );
164 fflush( stdout );
165
Paul Bakkerddf26b42013-09-18 13:46:23 +0200166 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
167 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000168 if( ret != 0 )
169 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000170 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
171 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000172 if( flags & BADCERT_CN_MISMATCH )
173 printf( " CN_MISMATCH " );
174 if( flags & BADCERT_EXPIRED )
175 printf( " EXPIRED " );
176 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000177 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000178 if( flags & BADCERT_NOT_TRUSTED )
179 printf( " NOT_TRUSTED " );
180 if( flags & BADCRL_NOT_TRUSTED )
181 printf( " CRL_NOT_TRUSTED " );
182 if( flags & BADCRL_EXPIRED )
183 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000184 } else {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200185 printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000186 goto exit;
187 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000188 }
189
190 printf( " ok\n" );
191
192 /*
193 * 1.5. Load own private key
194 */
195 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000196
197 printf( " . Loading the client private key %s...", name );
198 fflush( stdout );
199
Paul Bakker36713e82013-09-17 13:25:29 +0200200 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000201 if( ret != 0 )
202 {
Paul Bakker36713e82013-09-17 13:25:29 +0200203 printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000204 goto exit;
205 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000206
Paul Bakkerd98030e2009-05-02 15:13:40 +0000207 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000208
Paul Bakkerd98030e2009-05-02 15:13:40 +0000209 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200210 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000211 */
212 printf( " . Verify the client certificate with private key..." );
213 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000214
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200215
216 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200217 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200218 {
219 printf( " failed\n ! certificate's key is not RSA\n\n" );
220 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
221 goto exit;
222 }
223
Paul Bakker36713e82013-09-17 13:25:29 +0200224 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000225 if( ret != 0 )
226 {
227 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
228 goto exit;
229 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000230
Paul Bakker36713e82013-09-17 13:25:29 +0200231 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000232 if( ret != 0 )
233 {
234 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
235 goto exit;
236 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000237
Paul Bakker36713e82013-09-17 13:25:29 +0200238 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000239 if( ret != 0 )
240 {
241 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
242 goto exit;
243 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000244
Paul Bakkerd98030e2009-05-02 15:13:40 +0000245 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000246
Paul Bakker36713e82013-09-17 13:25:29 +0200247 x509_crt_free( &clicert );
248 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000249 }
250
251exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200252 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000253 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000254
Paul Bakkercce9d772011-11-18 14:26:47 +0000255#if defined(_WIN32)
Paul Bakker4593aea2009-02-09 22:32:35 +0000256 printf( " + Press Enter to exit this program.\n" );
257 fflush( stdout ); getchar();
258#endif
259
260 return( ret );
261}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200262#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
263 POLARSSL_X509_CRL_PARSE_C */