blob: 42f5c59280c5ff1a0bf3d6a5324398565481f996 [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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000027
28#include <string.h>
29#include <stdio.h>
30
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020031#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
32 !defined(POLARSSL_FS_IO)
33int main( int argc, char *argv[] )
34{
35 ((void) argc);
36 ((void) argv);
37
38 printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
39 "not defined.\n");
40 return( 0 );
41}
42#else
43
Paul Bakker4593aea2009-02-09 22:32:35 +000044#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020045#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000046
Paul Bakkerd98030e2009-05-02 15:13:40 +000047#if defined _MSC_VER && !defined snprintf
48#define snprintf _snprintf
49#endif
50
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020051
Paul Bakker40ea7de2009-05-03 10:18:48 +000052#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000053
Paul Bakkeref3f8c72013-06-24 13:01:08 +020054const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000055{
Paul Bakkerd98030e2009-05-02 15:13:40 +000056 "client1.crt",
57 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000058 "server1.crt",
59 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000060 "cert_sha224.crt",
61 "cert_sha256.crt",
62 "cert_sha384.crt",
63 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000064};
65
Paul Bakkeref3f8c72013-06-24 13:01:08 +020066const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000067{
Paul Bakkerd98030e2009-05-02 15:13:40 +000068 "client1.key",
69 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000070 "server1.key",
71 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000072 "cert_digest.key",
73 "cert_digest.key",
74 "cert_digest.key",
75 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000076};
77
Paul Bakkercce9d772011-11-18 14:26:47 +000078int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000079{
80 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020081 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000082 x509_crl crl;
83 char buf[10240];
84
Paul Bakkercce9d772011-11-18 14:26:47 +000085 ((void) argc);
86 ((void) argv);
87
Paul Bakker369d2eb2013-09-18 11:58:25 +020088 x509_crt_init( &cacert );
89 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000090
91 /*
92 * 1.1. Load the trusted CA
93 */
94 printf( "\n . Loading the CA root certificate ..." );
95 fflush( stdout );
96
Paul Bakker4593aea2009-02-09 22:32:35 +000097 /*
98 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +020099 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000100 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200101 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000102 if( ret != 0 )
103 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200104 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000105 goto exit;
106 }
107
108 printf( " ok\n" );
109
Paul Bakkerddf26b42013-09-18 13:46:23 +0200110 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000111 printf("%s\n", buf );
112
Paul Bakkerd98030e2009-05-02 15:13:40 +0000113 /*
114 * 1.2. Load the CRL
115 */
116 printf( " . Loading the CRL ..." );
117 fflush( stdout );
118
Paul Bakkerddf26b42013-09-18 13:46:23 +0200119 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000120 if( ret != 0 )
121 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200122 printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000123 goto exit;
124 }
125
126 printf( " ok\n" );
127
Paul Bakkerddf26b42013-09-18 13:46:23 +0200128 x509_crl_info( buf, 1024, "CRL: ", &crl );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000129 printf("%s\n", buf );
130
Paul Bakker4593aea2009-02-09 22:32:35 +0000131 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
132 {
133 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000134 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000135 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000136 char name[512];
137 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200138 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200139 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000140
Paul Bakker369d2eb2013-09-18 11:58:25 +0200141 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200142 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000143
144 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
145
146 printf( " . Loading the client certificate %s...", name );
147 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000148
Paul Bakkerddf26b42013-09-18 13:46:23 +0200149 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000150 if( ret != 0 )
151 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200152 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000153 goto exit;
154 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000155
Paul Bakkerd98030e2009-05-02 15:13:40 +0000156 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000157
158 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000159 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000160 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000161 printf( " . Verify the client certificate with CA certificate..." );
162 fflush( stdout );
163
Paul Bakkerddf26b42013-09-18 13:46:23 +0200164 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
165 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000166 if( ret != 0 )
167 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000168 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
169 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000170 if( flags & BADCERT_CN_MISMATCH )
171 printf( " CN_MISMATCH " );
172 if( flags & BADCERT_EXPIRED )
173 printf( " EXPIRED " );
174 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000175 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000176 if( flags & BADCERT_NOT_TRUSTED )
177 printf( " NOT_TRUSTED " );
178 if( flags & BADCRL_NOT_TRUSTED )
179 printf( " CRL_NOT_TRUSTED " );
180 if( flags & BADCRL_EXPIRED )
181 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000182 } else {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200183 printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000184 goto exit;
185 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000186 }
187
188 printf( " ok\n" );
189
190 /*
191 * 1.5. Load own private key
192 */
193 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000194
195 printf( " . Loading the client private key %s...", name );
196 fflush( stdout );
197
Paul Bakker36713e82013-09-17 13:25:29 +0200198 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000199 if( ret != 0 )
200 {
Paul Bakker36713e82013-09-17 13:25:29 +0200201 printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000202 goto exit;
203 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000204
Paul Bakkerd98030e2009-05-02 15:13:40 +0000205 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000206
Paul Bakkerd98030e2009-05-02 15:13:40 +0000207 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200208 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000209 */
210 printf( " . Verify the client certificate with private key..." );
211 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000212
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200213
214 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200215 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200216 {
217 printf( " failed\n ! certificate's key is not RSA\n\n" );
218 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
219 goto exit;
220 }
221
Paul Bakker36713e82013-09-17 13:25:29 +0200222 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000223 if( ret != 0 )
224 {
225 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
226 goto exit;
227 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000228
Paul Bakker36713e82013-09-17 13:25:29 +0200229 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000230 if( ret != 0 )
231 {
232 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
233 goto exit;
234 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000235
Paul Bakker36713e82013-09-17 13:25:29 +0200236 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000237 if( ret != 0 )
238 {
239 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
240 goto exit;
241 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000242
Paul Bakkerd98030e2009-05-02 15:13:40 +0000243 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000244
Paul Bakker36713e82013-09-17 13:25:29 +0200245 x509_crt_free( &clicert );
246 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000247 }
248
249exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200250 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000251 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000252
Paul Bakkercce9d772011-11-18 14:26:47 +0000253#if defined(_WIN32)
Paul Bakker4593aea2009-02-09 22:32:35 +0000254 printf( " + Press Enter to exit this program.\n" );
255 fflush( stdout ); getchar();
256#endif
257
258 return( ret );
259}
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +0200260#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO */