blob: 570b635251de9b743fb6b0d94eaa643b117b736e [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (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 Bakker4593aea2009-02-09 22:32:35 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker4593aea2009-02-09 22:32:35 +000029
30#include <string.h>
31#include <stdio.h>
32
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020033#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020034 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020035int main( int argc, char *argv[] )
36{
37 ((void) argc);
38 ((void) argv);
39
40 printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020041 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020042 "not defined.\n");
43 return( 0 );
44}
45#else
46
Paul Bakker4593aea2009-02-09 22:32:35 +000047#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020048#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000049
Paul Bakkerd98030e2009-05-02 15:13:40 +000050#if defined _MSC_VER && !defined snprintf
51#define snprintf _snprintf
52#endif
53
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020054
Paul Bakker40ea7de2009-05-03 10:18:48 +000055#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000056
Paul Bakkeref3f8c72013-06-24 13:01:08 +020057const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000058{
Paul Bakkerd98030e2009-05-02 15:13:40 +000059 "client1.crt",
60 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000061 "server1.crt",
62 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000063 "cert_sha224.crt",
64 "cert_sha256.crt",
65 "cert_sha384.crt",
66 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000067};
68
Paul Bakkeref3f8c72013-06-24 13:01:08 +020069const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000070{
Paul Bakkerd98030e2009-05-02 15:13:40 +000071 "client1.key",
72 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000073 "server1.key",
74 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000075 "cert_digest.key",
76 "cert_digest.key",
77 "cert_digest.key",
78 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000079};
80
Paul Bakkercce9d772011-11-18 14:26:47 +000081int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000082{
83 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020084 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000085 x509_crl crl;
86 char buf[10240];
87
Paul Bakkercce9d772011-11-18 14:26:47 +000088 ((void) argc);
89 ((void) argv);
90
Paul Bakker369d2eb2013-09-18 11:58:25 +020091 x509_crt_init( &cacert );
92 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000093
94 /*
95 * 1.1. Load the trusted CA
96 */
97 printf( "\n . Loading the CA root certificate ..." );
98 fflush( stdout );
99
Paul Bakker4593aea2009-02-09 22:32:35 +0000100 /*
101 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +0200102 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000103 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200104 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000105 if( ret != 0 )
106 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200107 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000108 goto exit;
109 }
110
111 printf( " ok\n" );
112
Paul Bakkerddf26b42013-09-18 13:46:23 +0200113 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000114 printf("%s\n", buf );
115
Paul Bakkerd98030e2009-05-02 15:13:40 +0000116 /*
117 * 1.2. Load the CRL
118 */
119 printf( " . Loading the CRL ..." );
120 fflush( stdout );
121
Paul Bakkerddf26b42013-09-18 13:46:23 +0200122 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000123 if( ret != 0 )
124 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200125 printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000126 goto exit;
127 }
128
129 printf( " ok\n" );
130
Paul Bakkerddf26b42013-09-18 13:46:23 +0200131 x509_crl_info( buf, 1024, "CRL: ", &crl );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000132 printf("%s\n", buf );
133
Paul Bakker4593aea2009-02-09 22:32:35 +0000134 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
135 {
136 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000137 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000138 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000139 char name[512];
140 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200141 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200142 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000143
Paul Bakker369d2eb2013-09-18 11:58:25 +0200144 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200145 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000146
147 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
148
149 printf( " . Loading the client certificate %s...", name );
150 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000151
Paul Bakkerddf26b42013-09-18 13:46:23 +0200152 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000153 if( ret != 0 )
154 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200155 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000156 goto exit;
157 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000158
Paul Bakkerd98030e2009-05-02 15:13:40 +0000159 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000160
161 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000162 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000163 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000164 printf( " . Verify the client certificate with CA certificate..." );
165 fflush( stdout );
166
Paul Bakkerddf26b42013-09-18 13:46:23 +0200167 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
168 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000169 if( ret != 0 )
170 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000171 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
172 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000173 if( flags & BADCERT_CN_MISMATCH )
174 printf( " CN_MISMATCH " );
175 if( flags & BADCERT_EXPIRED )
176 printf( " EXPIRED " );
177 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000178 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000179 if( flags & BADCERT_NOT_TRUSTED )
180 printf( " NOT_TRUSTED " );
181 if( flags & BADCRL_NOT_TRUSTED )
182 printf( " CRL_NOT_TRUSTED " );
183 if( flags & BADCRL_EXPIRED )
184 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000185 } else {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200186 printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000187 goto exit;
188 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000189 }
190
191 printf( " ok\n" );
192
193 /*
194 * 1.5. Load own private key
195 */
196 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000197
198 printf( " . Loading the client private key %s...", name );
199 fflush( stdout );
200
Paul Bakker36713e82013-09-17 13:25:29 +0200201 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000202 if( ret != 0 )
203 {
Paul Bakker36713e82013-09-17 13:25:29 +0200204 printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000205 goto exit;
206 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000207
Paul Bakkerd98030e2009-05-02 15:13:40 +0000208 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000209
Paul Bakkerd98030e2009-05-02 15:13:40 +0000210 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200211 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000212 */
213 printf( " . Verify the client certificate with private key..." );
214 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000215
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200216
217 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200218 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200219 {
220 printf( " failed\n ! certificate's key is not RSA\n\n" );
221 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
222 goto exit;
223 }
224
Paul Bakker36713e82013-09-17 13:25:29 +0200225 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000226 if( ret != 0 )
227 {
228 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
229 goto exit;
230 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000231
Paul Bakker36713e82013-09-17 13:25:29 +0200232 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000233 if( ret != 0 )
234 {
235 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
236 goto exit;
237 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000238
Paul Bakker36713e82013-09-17 13:25:29 +0200239 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000240 if( ret != 0 )
241 {
242 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
243 goto exit;
244 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000245
Paul Bakkerd98030e2009-05-02 15:13:40 +0000246 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000247
Paul Bakker36713e82013-09-17 13:25:29 +0200248 x509_crt_free( &clicert );
249 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000250 }
251
252exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200253 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000254 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000255
Paul Bakkercce9d772011-11-18 14:26:47 +0000256#if defined(_WIN32)
Paul Bakker4593aea2009-02-09 22:32:35 +0000257 printf( " + Press Enter to exit this program.\n" );
258 fflush( stdout ); getchar();
259#endif
260
261 return( ret );
262}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200263#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
264 POLARSSL_X509_CRL_PARSE_C */