blob: f1044cf1599932e65bc977d0896f327a9fa7396b [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
Paul Bakker4593aea2009-02-09 22:32:35 +000035#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020036#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000037
Paul Bakkerd98030e2009-05-02 15:13:40 +000038#if defined _MSC_VER && !defined snprintf
39#define snprintf _snprintf
40#endif
41
Paul Bakker40ea7de2009-05-03 10:18:48 +000042#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000043
Paul Bakkeref3f8c72013-06-24 13:01:08 +020044const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000045{
Paul Bakkerd98030e2009-05-02 15:13:40 +000046 "client1.crt",
47 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000048 "server1.crt",
49 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000050 "cert_sha224.crt",
51 "cert_sha256.crt",
52 "cert_sha384.crt",
53 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000054};
55
Paul Bakkeref3f8c72013-06-24 13:01:08 +020056const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000057{
Paul Bakkerd98030e2009-05-02 15:13:40 +000058 "client1.key",
59 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000060 "server1.key",
61 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000062 "cert_digest.key",
63 "cert_digest.key",
64 "cert_digest.key",
65 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000066};
67
Paul Bakker5690efc2011-05-26 13:16:06 +000068#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020069 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_PK_PARSE_C) || \
70 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000071int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000072{
Paul Bakkercce9d772011-11-18 14:26:47 +000073 ((void) argc);
74 ((void) argv);
75
Paul Bakker5690efc2011-05-26 13:16:06 +000076 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020077 "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_FS_IO and/or "
78 "POLARSSL_PK_PARSE_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000079 return( 0 );
80}
81#else
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 Bakkerd98030e2009-05-02 15:13:40 +000085 x509_cert cacert;
86 x509_crl crl;
87 char buf[10240];
88
Paul Bakkercce9d772011-11-18 14:26:47 +000089 ((void) argc);
90 ((void) argv);
91
Paul Bakkerd98030e2009-05-02 15:13:40 +000092 memset( &cacert, 0, sizeof( x509_cert ) );
93 memset( &crl, 0, sizeof( x509_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
103 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
104 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000105 ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000106 if( ret != 0 )
107 {
108 printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret );
109 goto exit;
110 }
111
112 printf( " ok\n" );
113
Paul Bakker40ea7de2009-05-03 10:18:48 +0000114 x509parse_cert_info( buf, 1024, "CRT: ", &cacert );
115 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
123 ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" );
124 if( ret != 0 )
125 {
126 printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret );
127 goto exit;
128 }
129
130 printf( " ok\n" );
131
132 x509parse_crl_info( buf, 1024, "CRL: ", &crl );
133 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;
142 x509_cert clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200143 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000144
145 memset( &clicert, 0, sizeof( x509_cert ) );
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 Bakker69e095c2011-12-10 21:55:01 +0000153 ret = x509parse_crtfile( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000154 if( ret != 0 )
155 {
156 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
157 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 Bakkerb63b0af2011-01-13 17:54:59 +0000168 ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, 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 {
186 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
187 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}
Paul Bakker36713e82013-09-17 13:25:29 +0200263#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C &&
264 POLARSSL_FS_IO && POLARSSL_PK_PARSE_C */