blob: 4c49110e3c93b9b6ee9416c9f9a131facfb4fc05 [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.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakker4593aea2009-02-09 22:32:35 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdio.h>
29
30#include "polarssl/certs.h"
31#include "polarssl/x509.h"
32
Paul Bakkerd98030e2009-05-02 15:13:40 +000033#if defined _MSC_VER && !defined snprintf
34#define snprintf _snprintf
35#endif
36
Paul Bakker40ea7de2009-05-03 10:18:48 +000037#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000038
39char *client_certificates[MAX_CLIENT_CERTS] =
40{
Paul Bakkerd98030e2009-05-02 15:13:40 +000041 "client1.crt",
42 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000043 "server1.crt",
44 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000045 "cert_sha224.crt",
46 "cert_sha256.crt",
47 "cert_sha384.crt",
48 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000049};
50
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000051char *client_private_keys[MAX_CLIENT_CERTS] =
52{
Paul Bakkerd98030e2009-05-02 15:13:40 +000053 "client1.key",
54 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000055 "server1.key",
56 "server2.key",
Paul Bakkerd98030e2009-05-02 15:13:40 +000057 "cert_sha224.key",
58 "cert_sha256.key",
59 "cert_sha384.key",
60 "cert_sha512.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000061};
62
Paul Bakker4593aea2009-02-09 22:32:35 +000063int main( void )
64{
65 int ret, i;
Paul Bakkerd98030e2009-05-02 15:13:40 +000066 x509_cert cacert;
67 x509_crl crl;
68 char buf[10240];
69
70 memset( &cacert, 0, sizeof( x509_cert ) );
71 memset( &crl, 0, sizeof( x509_crl ) );
Paul Bakker4593aea2009-02-09 22:32:35 +000072
73 /*
74 * 1.1. Load the trusted CA
75 */
76 printf( "\n . Loading the CA root certificate ..." );
77 fflush( stdout );
78
Paul Bakker4593aea2009-02-09 22:32:35 +000079 /*
80 * Alternatively, you may load the CA certificates from a .pem or
81 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
82 */
83 ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" );
84 if( ret != 0 )
85 {
86 printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret );
87 goto exit;
88 }
89
90 printf( " ok\n" );
91
Paul Bakker40ea7de2009-05-03 10:18:48 +000092 x509parse_cert_info( buf, 1024, "CRT: ", &cacert );
93 printf("%s\n", buf );
94
Paul Bakkerd98030e2009-05-02 15:13:40 +000095 /*
96 * 1.2. Load the CRL
97 */
98 printf( " . Loading the CRL ..." );
99 fflush( stdout );
100
101 ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" );
102 if( ret != 0 )
103 {
104 printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret );
105 goto exit;
106 }
107
108 printf( " ok\n" );
109
110 x509parse_crl_info( buf, 1024, "CRL: ", &crl );
111 printf("%s\n", buf );
112
Paul Bakker4593aea2009-02-09 22:32:35 +0000113 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
114 {
115 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000116 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000117 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000118 char name[512];
119 int flags;
120 x509_cert clicert;
121 rsa_context rsa;
Paul Bakker4593aea2009-02-09 22:32:35 +0000122
123 memset( &clicert, 0, sizeof( x509_cert ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000124 memset( &rsa, 0, sizeof( rsa_context ) );
125
126 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
127
128 printf( " . Loading the client certificate %s...", name );
129 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000130
131 ret = x509parse_crtfile( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000132 if( ret != 0 )
133 {
134 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
135 goto exit;
136 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000137
Paul Bakkerd98030e2009-05-02 15:13:40 +0000138 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000139
140 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000141 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000142 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000143 printf( " . Verify the client certificate with CA certificate..." );
144 fflush( stdout );
145
Paul Bakker40ea7de2009-05-03 10:18:48 +0000146 ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000147 if( ret != 0 )
148 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000149 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
150 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000151 if( flags & BADCERT_CN_MISMATCH )
152 printf( " CN_MISMATCH " );
153 if( flags & BADCERT_EXPIRED )
154 printf( " EXPIRED " );
155 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000156 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000157 if( flags & BADCERT_NOT_TRUSTED )
158 printf( " NOT_TRUSTED " );
159 if( flags & BADCRL_NOT_TRUSTED )
160 printf( " CRL_NOT_TRUSTED " );
161 if( flags & BADCRL_EXPIRED )
162 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000163 } else {
164 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
165 goto exit;
166 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000167 }
168
169 printf( " ok\n" );
170
171 /*
172 * 1.5. Load own private key
173 */
174 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000175
176 printf( " . Loading the client private key %s...", name );
177 fflush( stdout );
178
Paul Bakkerd98030e2009-05-02 15:13:40 +0000179 ret = x509parse_keyfile( &rsa, name, NULL );
180 if( ret != 0 )
181 {
182 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
183 goto exit;
184 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000185
Paul Bakkerd98030e2009-05-02 15:13:40 +0000186 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000187
Paul Bakkerd98030e2009-05-02 15:13:40 +0000188 /*
189 * 1.5. Verify certificate validity with private key
190 */
191 printf( " . Verify the client certificate with private key..." );
192 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000193
Paul Bakkerd98030e2009-05-02 15:13:40 +0000194 ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N);
195 if( ret != 0 )
196 {
197 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
198 goto exit;
199 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000200
Paul Bakkerd98030e2009-05-02 15:13:40 +0000201 ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E);
202 if( ret != 0 )
203 {
204 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
205 goto exit;
206 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000207
Paul Bakkerd98030e2009-05-02 15:13:40 +0000208 ret = rsa_check_privkey( &rsa );
209 if( ret != 0 )
210 {
211 printf( " failed\n ! rsa_check_privkey 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 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000216
Paul Bakkerd98030e2009-05-02 15:13:40 +0000217 x509_free( &clicert );
218 rsa_free( &rsa );
Paul Bakker4593aea2009-02-09 22:32:35 +0000219 }
220
221exit:
Paul Bakker4593aea2009-02-09 22:32:35 +0000222 x509_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000223 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000224
225#ifdef WIN32
226 printf( " + Press Enter to exit this program.\n" );
227 fflush( stdout ); getchar();
228#endif
229
230 return( ret );
231}