blob: 00488bae426af69e0628e7db2150580425d6836d [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
4 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#ifndef _CRT_SECURE_NO_DEPRECATE
22#define _CRT_SECURE_NO_DEPRECATE 1
23#endif
24
25#include <string.h>
26#include <stdio.h>
27
28#include "polarssl/certs.h"
29#include "polarssl/x509.h"
30
Paul Bakkerd98030e2009-05-02 15:13:40 +000031#if defined _MSC_VER && !defined snprintf
32#define snprintf _snprintf
33#endif
34
Paul Bakker40ea7de2009-05-03 10:18:48 +000035#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000036
37char *client_certificates[MAX_CLIENT_CERTS] =
38{
Paul Bakkerd98030e2009-05-02 15:13:40 +000039 "client1.crt",
40 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000041 "server1.crt",
42 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000043 "cert_sha224.crt",
44 "cert_sha256.crt",
45 "cert_sha384.crt",
46 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000047};
48
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000049char *client_private_keys[MAX_CLIENT_CERTS] =
50{
Paul Bakkerd98030e2009-05-02 15:13:40 +000051 "client1.key",
52 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000053 "server1.key",
54 "server2.key",
Paul Bakkerd98030e2009-05-02 15:13:40 +000055 "cert_sha224.key",
56 "cert_sha256.key",
57 "cert_sha384.key",
58 "cert_sha512.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000059};
60
Paul Bakker4593aea2009-02-09 22:32:35 +000061int main( void )
62{
63 int ret, i;
Paul Bakkerd98030e2009-05-02 15:13:40 +000064 x509_cert cacert;
65 x509_crl crl;
66 char buf[10240];
67
68 memset( &cacert, 0, sizeof( x509_cert ) );
69 memset( &crl, 0, sizeof( x509_crl ) );
Paul Bakker4593aea2009-02-09 22:32:35 +000070
71 /*
72 * 1.1. Load the trusted CA
73 */
74 printf( "\n . Loading the CA root certificate ..." );
75 fflush( stdout );
76
Paul Bakker4593aea2009-02-09 22:32:35 +000077 /*
78 * Alternatively, you may load the CA certificates from a .pem or
79 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
80 */
81 ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" );
82 if( ret != 0 )
83 {
84 printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret );
85 goto exit;
86 }
87
88 printf( " ok\n" );
89
Paul Bakker40ea7de2009-05-03 10:18:48 +000090 x509parse_cert_info( buf, 1024, "CRT: ", &cacert );
91 printf("%s\n", buf );
92
Paul Bakkerd98030e2009-05-02 15:13:40 +000093 /*
94 * 1.2. Load the CRL
95 */
96 printf( " . Loading the CRL ..." );
97 fflush( stdout );
98
99 ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" );
100 if( ret != 0 )
101 {
102 printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret );
103 goto exit;
104 }
105
106 printf( " ok\n" );
107
108 x509parse_crl_info( buf, 1024, "CRL: ", &crl );
109 printf("%s\n", buf );
110
Paul Bakker4593aea2009-02-09 22:32:35 +0000111 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
112 {
113 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000114 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000115 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000116 char name[512];
117 int flags;
118 x509_cert clicert;
119 rsa_context rsa;
Paul Bakker4593aea2009-02-09 22:32:35 +0000120
121 memset( &clicert, 0, sizeof( x509_cert ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000122 memset( &rsa, 0, sizeof( rsa_context ) );
123
124 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
125
126 printf( " . Loading the client certificate %s...", name );
127 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000128
129 ret = x509parse_crtfile( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000130 if( ret != 0 )
131 {
132 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
133 goto exit;
134 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000135
Paul Bakkerd98030e2009-05-02 15:13:40 +0000136 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000137
138 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000139 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000140 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000141 printf( " . Verify the client certificate with CA certificate..." );
142 fflush( stdout );
143
Paul Bakker40ea7de2009-05-03 10:18:48 +0000144 ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000145 if( ret != 0 )
146 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000147 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
148 {
149 if( flags == BADCERT_REVOKED )
150 printf( " REVOKED " );
151 } else {
152 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
153 goto exit;
154 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155 }
156
157 printf( " ok\n" );
158
159 /*
160 * 1.5. Load own private key
161 */
162 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000163
164 printf( " . Loading the client private key %s...", name );
165 fflush( stdout );
166
Paul Bakkerd98030e2009-05-02 15:13:40 +0000167 ret = x509parse_keyfile( &rsa, name, NULL );
168 if( ret != 0 )
169 {
170 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
171 goto exit;
172 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000173
Paul Bakkerd98030e2009-05-02 15:13:40 +0000174 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000175
Paul Bakkerd98030e2009-05-02 15:13:40 +0000176 /*
177 * 1.5. Verify certificate validity with private key
178 */
179 printf( " . Verify the client certificate with private key..." );
180 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000181
Paul Bakkerd98030e2009-05-02 15:13:40 +0000182 ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N);
183 if( ret != 0 )
184 {
185 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
186 goto exit;
187 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000188
Paul Bakkerd98030e2009-05-02 15:13:40 +0000189 ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E);
190 if( ret != 0 )
191 {
192 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
193 goto exit;
194 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000195
Paul Bakkerd98030e2009-05-02 15:13:40 +0000196 ret = rsa_check_privkey( &rsa );
197 if( ret != 0 )
198 {
199 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
200 goto exit;
201 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000202
Paul Bakkerd98030e2009-05-02 15:13:40 +0000203 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000204
Paul Bakkerd98030e2009-05-02 15:13:40 +0000205 x509_free( &clicert );
206 rsa_free( &rsa );
Paul Bakker4593aea2009-02-09 22:32:35 +0000207 }
208
209exit:
Paul Bakker4593aea2009-02-09 22:32:35 +0000210 x509_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000211 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000212
213#ifdef WIN32
214 printf( " + Press Enter to exit this program.\n" );
215 fflush( stdout ); getchar();
216#endif
217
218 return( ret );
219}