blob: 57f5f844fd801e93ff0822c4c87cf871bd9bc68c [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) || \
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020032 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020033int 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 "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020039 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020040 "not defined.\n");
41 return( 0 );
42}
43#else
44
Paul Bakker4593aea2009-02-09 22:32:35 +000045#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020046#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000047
Paul Bakkerd98030e2009-05-02 15:13:40 +000048#if defined _MSC_VER && !defined snprintf
49#define snprintf _snprintf
50#endif
51
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020052
Paul Bakker40ea7de2009-05-03 10:18:48 +000053#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000054
Paul Bakkeref3f8c72013-06-24 13:01:08 +020055const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000056{
Paul Bakkerd98030e2009-05-02 15:13:40 +000057 "client1.crt",
58 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000059 "server1.crt",
60 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000061 "cert_sha224.crt",
62 "cert_sha256.crt",
63 "cert_sha384.crt",
64 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000065};
66
Paul Bakkeref3f8c72013-06-24 13:01:08 +020067const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000068{
Paul Bakkerd98030e2009-05-02 15:13:40 +000069 "client1.key",
70 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000071 "server1.key",
72 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000073 "cert_digest.key",
74 "cert_digest.key",
75 "cert_digest.key",
76 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000077};
78
Paul Bakkercce9d772011-11-18 14:26:47 +000079int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000080{
81 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020082 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000083 x509_crl crl;
84 char buf[10240];
85
Paul Bakkercce9d772011-11-18 14:26:47 +000086 ((void) argc);
87 ((void) argv);
88
Paul Bakker369d2eb2013-09-18 11:58:25 +020089 x509_crt_init( &cacert );
90 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000091
92 /*
93 * 1.1. Load the trusted CA
94 */
95 printf( "\n . Loading the CA root certificate ..." );
96 fflush( stdout );
97
Paul Bakker4593aea2009-02-09 22:32:35 +000098 /*
99 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +0200100 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000101 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200102 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000103 if( ret != 0 )
104 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200105 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000106 goto exit;
107 }
108
109 printf( " ok\n" );
110
Paul Bakkerddf26b42013-09-18 13:46:23 +0200111 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000112 printf("%s\n", buf );
113
Paul Bakkerd98030e2009-05-02 15:13:40 +0000114 /*
115 * 1.2. Load the CRL
116 */
117 printf( " . Loading the CRL ..." );
118 fflush( stdout );
119
Paul Bakkerddf26b42013-09-18 13:46:23 +0200120 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000121 if( ret != 0 )
122 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200123 printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000124 goto exit;
125 }
126
127 printf( " ok\n" );
128
Paul Bakkerddf26b42013-09-18 13:46:23 +0200129 x509_crl_info( buf, 1024, "CRL: ", &crl );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000130 printf("%s\n", buf );
131
Paul Bakker4593aea2009-02-09 22:32:35 +0000132 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
133 {
134 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000135 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000136 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000137 char name[512];
138 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200139 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200140 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000141
Paul Bakker369d2eb2013-09-18 11:58:25 +0200142 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200143 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000144
145 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
146
147 printf( " . Loading the client certificate %s...", name );
148 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000149
Paul Bakkerddf26b42013-09-18 13:46:23 +0200150 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000151 if( ret != 0 )
152 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200153 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000154 goto exit;
155 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000156
Paul Bakkerd98030e2009-05-02 15:13:40 +0000157 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000158
159 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000160 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000161 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000162 printf( " . Verify the client certificate with CA certificate..." );
163 fflush( stdout );
164
Paul Bakkerddf26b42013-09-18 13:46:23 +0200165 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
166 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000167 if( ret != 0 )
168 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000169 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
170 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000171 if( flags & BADCERT_CN_MISMATCH )
172 printf( " CN_MISMATCH " );
173 if( flags & BADCERT_EXPIRED )
174 printf( " EXPIRED " );
175 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000176 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000177 if( flags & BADCERT_NOT_TRUSTED )
178 printf( " NOT_TRUSTED " );
179 if( flags & BADCRL_NOT_TRUSTED )
180 printf( " CRL_NOT_TRUSTED " );
181 if( flags & BADCRL_EXPIRED )
182 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000183 } else {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200184 printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000185 goto exit;
186 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000187 }
188
189 printf( " ok\n" );
190
191 /*
192 * 1.5. Load own private key
193 */
194 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000195
196 printf( " . Loading the client private key %s...", name );
197 fflush( stdout );
198
Paul Bakker36713e82013-09-17 13:25:29 +0200199 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000200 if( ret != 0 )
201 {
Paul Bakker36713e82013-09-17 13:25:29 +0200202 printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000203 goto exit;
204 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000205
Paul Bakkerd98030e2009-05-02 15:13:40 +0000206 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000207
Paul Bakkerd98030e2009-05-02 15:13:40 +0000208 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200209 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000210 */
211 printf( " . Verify the client certificate with private key..." );
212 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000213
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200214
215 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200216 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200217 {
218 printf( " failed\n ! certificate's key is not RSA\n\n" );
219 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
220 goto exit;
221 }
222
Paul Bakker36713e82013-09-17 13:25:29 +0200223 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000224 if( ret != 0 )
225 {
226 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
227 goto exit;
228 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000229
Paul Bakker36713e82013-09-17 13:25:29 +0200230 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000231 if( ret != 0 )
232 {
233 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
234 goto exit;
235 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000236
Paul Bakker36713e82013-09-17 13:25:29 +0200237 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000238 if( ret != 0 )
239 {
240 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
241 goto exit;
242 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000243
Paul Bakkerd98030e2009-05-02 15:13:40 +0000244 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000245
Paul Bakker36713e82013-09-17 13:25:29 +0200246 x509_crt_free( &clicert );
247 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000248 }
249
250exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200251 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000252 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000253
Paul Bakkercce9d772011-11-18 14:26:47 +0000254#if defined(_WIN32)
Paul Bakker4593aea2009-02-09 22:32:35 +0000255 printf( " + Press Enter to exit this program.\n" );
256 fflush( stdout ); getchar();
257#endif
258
259 return( ret );
260}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200261#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
262 POLARSSL_X509_CRL_PARSE_C */