blob: 75ecb020188c153e5fd393fcbd06ee2cb7836699 [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
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
Paul Bakker5690efc2011-05-26 13:16:06 +000030#include "polarssl/config.h"
31
Paul Bakker4593aea2009-02-09 22:32:35 +000032#include "polarssl/certs.h"
33#include "polarssl/x509.h"
34
Paul Bakkerd98030e2009-05-02 15:13:40 +000035#if defined _MSC_VER && !defined snprintf
36#define snprintf _snprintf
37#endif
38
Paul Bakker40ea7de2009-05-03 10:18:48 +000039#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000040
Paul Bakkere0225e42013-06-06 12:52:24 +020041const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000042{
Paul Bakkerd98030e2009-05-02 15:13:40 +000043 "client1.crt",
44 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000045 "server1.crt",
46 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000047 "cert_sha224.crt",
48 "cert_sha256.crt",
49 "cert_sha384.crt",
50 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000051};
52
Paul Bakkere0225e42013-06-06 12:52:24 +020053const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000054{
Paul Bakkerd98030e2009-05-02 15:13:40 +000055 "client1.key",
56 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000057 "server1.key",
58 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000059 "cert_digest.key",
60 "cert_digest.key",
61 "cert_digest.key",
62 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000063};
64
Paul Bakker5690efc2011-05-26 13:16:06 +000065#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
66 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000067int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000068{
Paul Bakkercce9d772011-11-18 14:26:47 +000069 ((void) argc);
70 ((void) argv);
71
Paul Bakker5690efc2011-05-26 13:16:06 +000072 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 Bakkercce9d772011-11-18 14:26:47 +000077int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000078{
79 int ret, i;
Paul Bakkerd98030e2009-05-02 15:13:40 +000080 x509_cert cacert;
81 x509_crl crl;
82 char buf[10240];
83
Paul Bakkercce9d772011-11-18 14:26:47 +000084 ((void) argc);
85 ((void) argv);
86
Paul Bakkerd98030e2009-05-02 15:13:40 +000087 memset( &cacert, 0, sizeof( x509_cert ) );
88 memset( &crl, 0, sizeof( x509_crl ) );
Paul Bakker4593aea2009-02-09 22:32:35 +000089
90 /*
91 * 1.1. Load the trusted CA
92 */
93 printf( "\n . Loading the CA root certificate ..." );
94 fflush( stdout );
95
Paul Bakker4593aea2009-02-09 22:32:35 +000096 /*
97 * Alternatively, you may load the CA certificates from a .pem or
98 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
99 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000100 ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000101 if( ret != 0 )
102 {
103 printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret );
104 goto exit;
105 }
106
107 printf( " ok\n" );
108
Paul Bakker40ea7de2009-05-03 10:18:48 +0000109 x509parse_cert_info( buf, 1024, "CRT: ", &cacert );
110 printf("%s\n", buf );
111
Paul Bakkerd98030e2009-05-02 15:13:40 +0000112 /*
113 * 1.2. Load the CRL
114 */
115 printf( " . Loading the CRL ..." );
116 fflush( stdout );
117
118 ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" );
119 if( ret != 0 )
120 {
121 printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret );
122 goto exit;
123 }
124
125 printf( " ok\n" );
126
127 x509parse_crl_info( buf, 1024, "CRL: ", &crl );
128 printf("%s\n", buf );
129
Paul Bakker4593aea2009-02-09 22:32:35 +0000130 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
131 {
132 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000133 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000134 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000135 char name[512];
136 int flags;
137 x509_cert clicert;
138 rsa_context rsa;
Paul Bakker4593aea2009-02-09 22:32:35 +0000139
140 memset( &clicert, 0, sizeof( x509_cert ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000141 memset( &rsa, 0, sizeof( rsa_context ) );
142
143 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
144
145 printf( " . Loading the client certificate %s...", name );
146 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000147
Paul Bakker69e095c2011-12-10 21:55:01 +0000148 ret = x509parse_crtfile( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000149 if( ret != 0 )
150 {
151 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
152 goto exit;
153 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000154
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000156
157 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000158 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000159 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000160 printf( " . Verify the client certificate with CA certificate..." );
161 fflush( stdout );
162
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000163 ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000164 if( ret != 0 )
165 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000166 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
167 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000168 if( flags & BADCERT_CN_MISMATCH )
169 printf( " CN_MISMATCH " );
170 if( flags & BADCERT_EXPIRED )
171 printf( " EXPIRED " );
172 if( flags & BADCERT_REVOKED )
Paul Bakker40ea7de2009-05-03 10:18:48 +0000173 printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000174 if( flags & BADCERT_NOT_TRUSTED )
175 printf( " NOT_TRUSTED " );
176 if( flags & BADCRL_NOT_TRUSTED )
177 printf( " CRL_NOT_TRUSTED " );
178 if( flags & BADCRL_EXPIRED )
179 printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000180 } else {
181 printf( " failed\n ! x509parse_verify returned %d\n\n", ret );
182 goto exit;
183 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000184 }
185
186 printf( " ok\n" );
187
188 /*
189 * 1.5. Load own private key
190 */
191 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000192
193 printf( " . Loading the client private key %s...", name );
194 fflush( stdout );
195
Paul Bakkerd98030e2009-05-02 15:13:40 +0000196 ret = x509parse_keyfile( &rsa, name, NULL );
197 if( ret != 0 )
198 {
199 printf( " failed\n ! x509parse_key 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 /*
206 * 1.5. Verify certificate validity with private key
207 */
208 printf( " . Verify the client certificate with private key..." );
209 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000210
Paul Bakkerd98030e2009-05-02 15:13:40 +0000211 ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N);
212 if( ret != 0 )
213 {
214 printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
215 goto exit;
216 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000217
Paul Bakkerd98030e2009-05-02 15:13:40 +0000218 ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E);
219 if( ret != 0 )
220 {
221 printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
222 goto exit;
223 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000224
Paul Bakkerd98030e2009-05-02 15:13:40 +0000225 ret = rsa_check_privkey( &rsa );
226 if( ret != 0 )
227 {
228 printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
229 goto exit;
230 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000231
Paul Bakkerd98030e2009-05-02 15:13:40 +0000232 printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000233
Paul Bakkerd98030e2009-05-02 15:13:40 +0000234 x509_free( &clicert );
235 rsa_free( &rsa );
Paul Bakker4593aea2009-02-09 22:32:35 +0000236 }
237
238exit:
Paul Bakker4593aea2009-02-09 22:32:35 +0000239 x509_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000240 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000241
Paul Bakkercce9d772011-11-18 14:26:47 +0000242#if defined(_WIN32)
Paul Bakker4593aea2009-02-09 22:32:35 +0000243 printf( " + Press Enter to exit this program.\n" );
244 fflush( stdout ); getchar();
245#endif
246
247 return( ret );
248}
Paul Bakker5690efc2011-05-26 13:16:06 +0000249#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C &&
250 POLARSSL_FS_IO */