blob: 1e5126b2b79830d2292f85b164e155298c1cf791 [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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"
36#include "polarssl/x509.h"
37
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
44char *client_certificates[MAX_CLIENT_CERTS] =
45{
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 Bakkera1d3e5f2009-03-28 17:30:26 +000056char *client_private_keys[MAX_CLIENT_CERTS] =
57{
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) || \
69 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
70int main( void )
71{
72 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
73 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
74 return( 0 );
75}
76#else
Paul Bakker4593aea2009-02-09 22:32:35 +000077int main( void )
78{
79 int ret, i;
Paul Bakkerd98030e2009-05-02 15:13:40 +000080 x509_cert cacert;
81 x509_crl crl;
82 char buf[10240];
83
84 memset( &cacert, 0, sizeof( x509_cert ) );
85 memset( &crl, 0, sizeof( x509_crl ) );
Paul Bakker4593aea2009-02-09 22:32:35 +000086
87 /*
88 * 1.1. Load the trusted CA
89 */
90 printf( "\n . Loading the CA root certificate ..." );
91 fflush( stdout );
92
Paul Bakker4593aea2009-02-09 22:32:35 +000093 /*
94 * Alternatively, you may load the CA certificates from a .pem or
95 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
96 */
97 ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" );
98 if( ret != 0 )
99 {
100 printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret );
101 goto exit;
102 }
103
104 printf( " ok\n" );
105
Paul Bakker40ea7de2009-05-03 10:18:48 +0000106 x509parse_cert_info( buf, 1024, "CRT: ", &cacert );
107 printf("%s\n", buf );
108
Paul Bakkerd98030e2009-05-02 15:13:40 +0000109 /*
110 * 1.2. Load the CRL
111 */
112 printf( " . Loading the CRL ..." );
113 fflush( stdout );
114
115 ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" );
116 if( ret != 0 )
117 {
118 printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret );
119 goto exit;
120 }
121
122 printf( " ok\n" );
123
124 x509parse_crl_info( buf, 1024, "CRL: ", &crl );
125 printf("%s\n", buf );
126
Paul Bakker4593aea2009-02-09 22:32:35 +0000127 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
128 {
129 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000130 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000131 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000132 char name[512];
133 int flags;
134 x509_cert clicert;
135 rsa_context rsa;
Paul Bakker4593aea2009-02-09 22:32:35 +0000136
137 memset( &clicert, 0, sizeof( x509_cert ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000138 memset( &rsa, 0, sizeof( rsa_context ) );
139
140 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
141
142 printf( " . Loading the client certificate %s...", name );
143 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000144
145 ret = x509parse_crtfile( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000146 if( ret != 0 )
147 {
148 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
149 goto exit;
150 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000151
Paul Bakkerd98030e2009-05-02 15:13:40 +0000152 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000153
154 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000156 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000157 printf( " . Verify the client certificate with CA certificate..." );
158 fflush( stdout );
159
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000160 ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000161 if( ret != 0 )
162 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000163 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
164 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000165 if( flags & BADCERT_CN_MISMATCH )
166 printf( " CN_MISMATCH " );
167 if( flags & BADCERT_EXPIRED )
168 printf( " EXPIRED " );
169 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000170 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000171 if( flags & BADCERT_NOT_TRUSTED )
172 printf( " NOT_TRUSTED " );
173 if( flags & BADCRL_NOT_TRUSTED )
174 printf( " CRL_NOT_TRUSTED " );
175 if( flags & BADCRL_EXPIRED )
176 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000177 } else {
178 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
179 goto exit;
180 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000181 }
182
183 printf( " ok\n" );
184
185 /*
186 * 1.5. Load own private key
187 */
188 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000189
190 printf( " . Loading the client private key %s...", name );
191 fflush( stdout );
192
Paul Bakkerd98030e2009-05-02 15:13:40 +0000193 ret = x509parse_keyfile( &rsa, name, NULL );
194 if( ret != 0 )
195 {
196 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
197 goto exit;
198 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000199
Paul Bakkerd98030e2009-05-02 15:13:40 +0000200 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000201
Paul Bakkerd98030e2009-05-02 15:13:40 +0000202 /*
203 * 1.5. Verify certificate validity with private key
204 */
205 printf( " . Verify the client certificate with private key..." );
206 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000207
Paul Bakkerd98030e2009-05-02 15:13:40 +0000208 ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N);
209 if( ret != 0 )
210 {
211 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
212 goto exit;
213 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000214
Paul Bakkerd98030e2009-05-02 15:13:40 +0000215 ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E);
216 if( ret != 0 )
217 {
218 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
219 goto exit;
220 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000221
Paul Bakkerd98030e2009-05-02 15:13:40 +0000222 ret = rsa_check_privkey( &rsa );
223 if( ret != 0 )
224 {
225 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
226 goto exit;
227 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000228
Paul Bakkerd98030e2009-05-02 15:13:40 +0000229 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000230
Paul Bakkerd98030e2009-05-02 15:13:40 +0000231 x509_free( &clicert );
232 rsa_free( &rsa );
Paul Bakker4593aea2009-02-09 22:32:35 +0000233 }
234
235exit:
Paul Bakker4593aea2009-02-09 22:32:35 +0000236 x509_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000237 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000238
239#ifdef WIN32
240 printf( " + Press Enter to exit this program.\n" );
241 fflush( stdout ); getchar();
242#endif
243
244 return( ret );
245}
Paul Bakker5690efc2011-05-26 13:16:06 +0000246#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C &&
247 POLARSSL_FS_IO */