blob: eb9baf8c2e3b089a9c620222a4c59bcfb48dafa0 [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 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000149 if( flags & BADCERT_CN_MISMATCH )
150 printf( " CN_MISMATCH " );
151 if( flags & BADCERT_EXPIRED )
152 printf( " EXPIRED " );
153 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000154 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000155 if( flags & BADCERT_NOT_TRUSTED )
156 printf( " NOT_TRUSTED " );
157 if( flags & BADCRL_NOT_TRUSTED )
158 printf( " CRL_NOT_TRUSTED " );
159 if( flags & BADCRL_EXPIRED )
160 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000161 } else {
162 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
163 goto exit;
164 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000165 }
166
167 printf( " ok\n" );
168
169 /*
170 * 1.5. Load own private key
171 */
172 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000173
174 printf( " . Loading the client private key %s...", name );
175 fflush( stdout );
176
Paul Bakkerd98030e2009-05-02 15:13:40 +0000177 ret = x509parse_keyfile( &rsa, name, NULL );
178 if( ret != 0 )
179 {
180 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
181 goto exit;
182 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000183
Paul Bakkerd98030e2009-05-02 15:13:40 +0000184 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000185
Paul Bakkerd98030e2009-05-02 15:13:40 +0000186 /*
187 * 1.5. Verify certificate validity with private key
188 */
189 printf( " . Verify the client certificate with private key..." );
190 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000191
Paul Bakkerd98030e2009-05-02 15:13:40 +0000192 ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N);
193 if( ret != 0 )
194 {
195 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
196 goto exit;
197 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000198
Paul Bakkerd98030e2009-05-02 15:13:40 +0000199 ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E);
200 if( ret != 0 )
201 {
202 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
203 goto exit;
204 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000205
Paul Bakkerd98030e2009-05-02 15:13:40 +0000206 ret = rsa_check_privkey( &rsa );
207 if( ret != 0 )
208 {
209 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
210 goto exit;
211 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000212
Paul Bakkerd98030e2009-05-02 15:13:40 +0000213 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000214
Paul Bakkerd98030e2009-05-02 15:13:40 +0000215 x509_free( &clicert );
216 rsa_free( &rsa );
Paul Bakker4593aea2009-02-09 22:32:35 +0000217 }
218
219exit:
Paul Bakker4593aea2009-02-09 22:32:35 +0000220 x509_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000221 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000222
223#ifdef WIN32
224 printf( " + Press Enter to exit this program.\n" );
225 fflush( stdout ); getchar();
226#endif
227
228 return( ret );
229}