blob: a8362d3161aedef391aa8b8edb2076e4c3fdfa07 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
Paul Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020035#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
36 !defined(POLARSSL_FS_IO)
37int 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 "
43 "not defined.\n");
44 return( 0 );
45}
46#else
47
Paul Bakker4593aea2009-02-09 22:32:35 +000048#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020049#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000050
Paul Bakkerd98030e2009-05-02 15:13:40 +000051#if defined _MSC_VER && !defined snprintf
52#define snprintf _snprintf
53#endif
54
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020055
Paul Bakker40ea7de2009-05-03 10:18:48 +000056#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000057
Paul Bakkeref3f8c72013-06-24 13:01:08 +020058const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000059{
Paul Bakkerd98030e2009-05-02 15:13:40 +000060 "client1.crt",
61 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000062 "server1.crt",
63 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000064 "cert_sha224.crt",
65 "cert_sha256.crt",
66 "cert_sha384.crt",
67 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000068};
69
Paul Bakkeref3f8c72013-06-24 13:01:08 +020070const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000071{
Paul Bakkerd98030e2009-05-02 15:13:40 +000072 "client1.key",
73 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000074 "server1.key",
75 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000076 "cert_digest.key",
77 "cert_digest.key",
78 "cert_digest.key",
79 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000080};
81
Paul Bakkercce9d772011-11-18 14:26:47 +000082int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000083{
84 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020085 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000086 x509_crl crl;
87 char buf[10240];
88
Paul Bakkercce9d772011-11-18 14:26:47 +000089 ((void) argc);
90 ((void) argv);
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 */
98 printf( "\n . Loading the CA root certificate ..." );
99 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 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200108 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000109 goto exit;
110 }
111
112 printf( " ok\n" );
113
Paul Bakkerddf26b42013-09-18 13:46:23 +0200114 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000115 printf("%s\n", buf );
116
Paul Bakkerd98030e2009-05-02 15:13:40 +0000117 /*
118 * 1.2. Load the CRL
119 */
120 printf( " . Loading the CRL ..." );
121 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 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200126 printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000127 goto exit;
128 }
129
130 printf( " ok\n" );
131
Paul Bakkerddf26b42013-09-18 13:46:23 +0200132 x509_crl_info( buf, 1024, "CRL: ", &crl );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000133 printf("%s\n", buf );
134
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
150 printf( " . Loading the client certificate %s...", name );
151 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 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200156 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
Paul Bakkerd98030e2009-05-02 15:13:40 +0000160 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 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000165 printf( " . Verify the client certificate with CA certificate..." );
166 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 )
175 printf( " CN_MISMATCH " );
176 if( flags & BADCERT_EXPIRED )
177 printf( " EXPIRED " );
178 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000179 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000180 if( flags & BADCERT_NOT_TRUSTED )
181 printf( " NOT_TRUSTED " );
182 if( flags & BADCRL_NOT_TRUSTED )
183 printf( " CRL_NOT_TRUSTED " );
184 if( flags & BADCRL_EXPIRED )
185 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000186 } else {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200187 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
192 printf( " ok\n" );
193
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
199 printf( " . Loading the client private key %s...", name );
200 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 {
Paul Bakker36713e82013-09-17 13:25:29 +0200205 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
Paul Bakkerd98030e2009-05-02 15:13:40 +0000209 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 */
214 printf( " . Verify the client certificate with private key..." );
215 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 {
221 printf( " failed\n ! certificate's key is not RSA\n\n" );
222 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 {
229 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
230 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 {
236 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
237 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 {
243 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
244 goto exit;
245 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000246
Paul Bakkerd98030e2009-05-02 15:13:40 +0000247 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)
Paul Bakker4593aea2009-02-09 22:32:35 +0000258 printf( " + Press Enter to exit this program.\n" );
259 fflush( stdout ); getchar();
260#endif
261
262 return( ret );
263}
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +0200264#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO */