blob: a0c2e13d8afcb0e18819afb185547bfb42dcaa8f [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
35#define MAX_CLIENT_CERTS 6
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",
41 "cert_sha224.crt",
42 "cert_sha256.crt",
43 "cert_sha384.crt",
44 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000045};
46
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000047char *client_private_keys[MAX_CLIENT_CERTS] =
48{
Paul Bakkerd98030e2009-05-02 15:13:40 +000049 "client1.key",
50 "client2.key",
51 "cert_sha224.key",
52 "cert_sha256.key",
53 "cert_sha384.key",
54 "cert_sha512.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000055};
56
Paul Bakker4593aea2009-02-09 22:32:35 +000057int main( void )
58{
59 int ret, i;
Paul Bakkerd98030e2009-05-02 15:13:40 +000060 x509_cert cacert;
61 x509_crl crl;
62 char buf[10240];
63
64 memset( &cacert, 0, sizeof( x509_cert ) );
65 memset( &crl, 0, sizeof( x509_crl ) );
Paul Bakker4593aea2009-02-09 22:32:35 +000066
67 /*
68 * 1.1. Load the trusted CA
69 */
70 printf( "\n . Loading the CA root certificate ..." );
71 fflush( stdout );
72
Paul Bakker4593aea2009-02-09 22:32:35 +000073 /*
74 * Alternatively, you may load the CA certificates from a .pem or
75 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
76 */
77 ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" );
78 if( ret != 0 )
79 {
80 printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret );
81 goto exit;
82 }
83
84 printf( " ok\n" );
85
Paul Bakkerd98030e2009-05-02 15:13:40 +000086 /*
87 * 1.2. Load the CRL
88 */
89 printf( " . Loading the CRL ..." );
90 fflush( stdout );
91
92 ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" );
93 if( ret != 0 )
94 {
95 printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret );
96 goto exit;
97 }
98
99 printf( " ok\n" );
100
101 x509parse_crl_info( buf, 1024, "CRL: ", &crl );
102 printf("%s\n", buf );
103
Paul Bakker4593aea2009-02-09 22:32:35 +0000104 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
105 {
106 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000107 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000108 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000109 char name[512];
110 int flags;
111 x509_cert clicert;
112 rsa_context rsa;
Paul Bakker4593aea2009-02-09 22:32:35 +0000113
114 memset( &clicert, 0, sizeof( x509_cert ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000115 memset( &rsa, 0, sizeof( rsa_context ) );
116
117 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
118
119 printf( " . Loading the client certificate %s...", name );
120 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000121
122 ret = x509parse_crtfile( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000123 if( ret != 0 )
124 {
125 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
126 goto exit;
127 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000128
Paul Bakkerd98030e2009-05-02 15:13:40 +0000129 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000130
131 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000132 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000133 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000134 printf( " . Verify the client certificate with CA certificate..." );
135 fflush( stdout );
136
137 ret = x509parse_verify( &clicert, &cacert, NULL, &flags );
138 if( ret != 0 )
139 {
140 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
141 goto exit;
142 }
143
144 printf( " ok\n" );
145
146 /*
147 * 1.5. Load own private key
148 */
149 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000150
151 printf( " . Loading the client private key %s...", name );
152 fflush( stdout );
153
Paul Bakkerd98030e2009-05-02 15:13:40 +0000154 ret = x509parse_keyfile( &rsa, name, NULL );
155 if( ret != 0 )
156 {
157 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
158 goto exit;
159 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000160
Paul Bakkerd98030e2009-05-02 15:13:40 +0000161 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000162
Paul Bakkerd98030e2009-05-02 15:13:40 +0000163 /*
164 * 1.5. Verify certificate validity with private key
165 */
166 printf( " . Verify the client certificate with private key..." );
167 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000168
Paul Bakkerd98030e2009-05-02 15:13:40 +0000169 ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N);
170 if( ret != 0 )
171 {
172 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
173 goto exit;
174 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000175
Paul Bakkerd98030e2009-05-02 15:13:40 +0000176 ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E);
177 if( ret != 0 )
178 {
179 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
180 goto exit;
181 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000182
Paul Bakkerd98030e2009-05-02 15:13:40 +0000183 ret = rsa_check_privkey( &rsa );
184 if( ret != 0 )
185 {
186 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
187 goto exit;
188 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000189
Paul Bakkerd98030e2009-05-02 15:13:40 +0000190 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000191
Paul Bakkerd98030e2009-05-02 15:13:40 +0000192 x509_free( &clicert );
193 rsa_free( &rsa );
Paul Bakker4593aea2009-02-09 22:32:35 +0000194 }
195
196exit:
Paul Bakker4593aea2009-02-09 22:32:35 +0000197 x509_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000198 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000199
200#ifdef WIN32
201 printf( " + Press Enter to exit this program.\n" );
202 fflush( stdout ); getchar();
203#endif
204
205 return( ret );
206}