blob: c55090a97873bff6e416435579dc277752607af5 [file] [log] [blame]
Paul Bakker4593aea2009-02-09 22:32:35 +00001/*
2 * SSL certificate functionality tests
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker4593aea2009-02-09 22:32:35 +00008 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker4593aea2009-02-09 22:32:35 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker4593aea2009-02-09 22:32:35 +000035#include <string.h>
36#include <stdio.h>
37
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020038#if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020039 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020040int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
Rich Evansf90016a2015-01-19 14:26:37 +000045 polarssl_printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +020046 "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C "
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020047 "not defined.\n");
48 return( 0 );
49}
50#else
51
Paul Bakker4593aea2009-02-09 22:32:35 +000052#include "polarssl/certs.h"
Paul Bakker36713e82013-09-17 13:25:29 +020053#include "polarssl/x509_crt.h"
Paul Bakker4593aea2009-02-09 22:32:35 +000054
Paul Bakkerd98030e2009-05-02 15:13:40 +000055#if defined _MSC_VER && !defined snprintf
56#define snprintf _snprintf
57#endif
58
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020059
Paul Bakker40ea7de2009-05-03 10:18:48 +000060#define MAX_CLIENT_CERTS 8
Paul Bakker4593aea2009-02-09 22:32:35 +000061
Paul Bakkeref3f8c72013-06-24 13:01:08 +020062const char *client_certificates[MAX_CLIENT_CERTS] =
Paul Bakker4593aea2009-02-09 22:32:35 +000063{
Paul Bakkerd98030e2009-05-02 15:13:40 +000064 "client1.crt",
65 "client2.crt",
Paul Bakker40ea7de2009-05-03 10:18:48 +000066 "server1.crt",
67 "server2.crt",
Paul Bakkerd98030e2009-05-02 15:13:40 +000068 "cert_sha224.crt",
69 "cert_sha256.crt",
70 "cert_sha384.crt",
71 "cert_sha512.crt"
Paul Bakker4593aea2009-02-09 22:32:35 +000072};
73
Paul Bakkeref3f8c72013-06-24 13:01:08 +020074const char *client_private_keys[MAX_CLIENT_CERTS] =
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000075{
Paul Bakkerd98030e2009-05-02 15:13:40 +000076 "client1.key",
77 "client2.key",
Paul Bakker40ea7de2009-05-03 10:18:48 +000078 "server1.key",
79 "server2.key",
Paul Bakkerf17ed282011-02-09 17:10:48 +000080 "cert_digest.key",
81 "cert_digest.key",
82 "cert_digest.key",
83 "cert_digest.key"
Paul Bakkera1d3e5f2009-03-28 17:30:26 +000084};
85
Paul Bakkercce9d772011-11-18 14:26:47 +000086int main( int argc, char *argv[] )
Paul Bakker4593aea2009-02-09 22:32:35 +000087{
88 int ret, i;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020089 x509_crt cacert;
Paul Bakkerd98030e2009-05-02 15:13:40 +000090 x509_crl crl;
91 char buf[10240];
92
Paul Bakkercce9d772011-11-18 14:26:47 +000093 ((void) argc);
94 ((void) argv);
95
Paul Bakker369d2eb2013-09-18 11:58:25 +020096 x509_crt_init( &cacert );
97 x509_crl_init( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +000098
99 /*
100 * 1.1. Load the trusted CA
101 */
Rich Evansf90016a2015-01-19 14:26:37 +0000102 polarssl_printf( "\n . Loading the CA root certificate ..." );
Paul Bakker4593aea2009-02-09 22:32:35 +0000103 fflush( stdout );
104
Paul Bakker4593aea2009-02-09 22:32:35 +0000105 /*
106 * Alternatively, you may load the CA certificates from a .pem or
Paul Bakkerddf26b42013-09-18 13:46:23 +0200107 * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ).
Paul Bakker4593aea2009-02-09 22:32:35 +0000108 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109 ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000110 if( ret != 0 )
111 {
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker4593aea2009-02-09 22:32:35 +0000113 goto exit;
114 }
115
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( " ok\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000117
Paul Bakkerddf26b42013-09-18 13:46:23 +0200118 x509_crt_info( buf, 1024, "CRT: ", &cacert );
Rich Evansf90016a2015-01-19 14:26:37 +0000119 polarssl_printf("%s\n", buf );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000120
Paul Bakkerd98030e2009-05-02 15:13:40 +0000121 /*
122 * 1.2. Load the CRL
123 */
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf( " . Loading the CRL ..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000125 fflush( stdout );
126
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000128 if( ret != 0 )
129 {
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000131 goto exit;
132 }
133
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000135
Paul Bakkerddf26b42013-09-18 13:46:23 +0200136 x509_crl_info( buf, 1024, "CRL: ", &crl );
Rich Evansf90016a2015-01-19 14:26:37 +0000137 polarssl_printf("%s\n", buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000138
Paul Bakker4593aea2009-02-09 22:32:35 +0000139 for( i = 0; i < MAX_CLIENT_CERTS; i++ )
140 {
141 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000142 * 1.3. Load own certificate
Paul Bakker4593aea2009-02-09 22:32:35 +0000143 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000144 char name[512];
145 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200146 x509_crt clicert;
Paul Bakker36713e82013-09-17 13:25:29 +0200147 pk_context pk;
Paul Bakker4593aea2009-02-09 22:32:35 +0000148
Paul Bakker369d2eb2013-09-18 11:58:25 +0200149 x509_crt_init( &clicert );
Paul Bakker36713e82013-09-17 13:25:29 +0200150 pk_init( &pk );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000151
152 snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
153
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_printf( " . Loading the client certificate %s...", name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000155 fflush( stdout );
Paul Bakker4593aea2009-02-09 22:32:35 +0000156
Paul Bakkerddf26b42013-09-18 13:46:23 +0200157 ret = x509_crt_parse_file( &clicert, name );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000158 if( ret != 0 )
159 {
Rich Evansf90016a2015-01-19 14:26:37 +0000160 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000161 goto exit;
162 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000163
Rich Evansf90016a2015-01-19 14:26:37 +0000164 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000165
166 /*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000167 * 1.4. Verify certificate validity with CA certificate
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000168 */
Rich Evansf90016a2015-01-19 14:26:37 +0000169 polarssl_printf( " . Verify the client certificate with CA certificate..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000170 fflush( stdout );
171
Paul Bakkerddf26b42013-09-18 13:46:23 +0200172 ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
173 NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000174 if( ret != 0 )
175 {
Paul Bakker40ea7de2009-05-03 10:18:48 +0000176 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
177 {
Paul Bakker860d36b2009-05-03 17:29:56 +0000178 if( flags & BADCERT_CN_MISMATCH )
Rich Evansf90016a2015-01-19 14:26:37 +0000179 polarssl_printf( " CN_MISMATCH " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000180 if( flags & BADCERT_EXPIRED )
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_printf( " EXPIRED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000182 if( flags & BADCERT_REVOKED )
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " REVOKED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000184 if( flags & BADCERT_NOT_TRUSTED )
Rich Evansf90016a2015-01-19 14:26:37 +0000185 polarssl_printf( " NOT_TRUSTED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000186 if( flags & BADCRL_NOT_TRUSTED )
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " CRL_NOT_TRUSTED " );
Paul Bakker860d36b2009-05-03 17:29:56 +0000188 if( flags & BADCRL_EXPIRED )
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " CRL_EXPIRED " );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000190 } else {
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " failed\n ! x509_crt_verify returned %d\n\n", ret );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000192 goto exit;
193 }
Paul Bakkerd98030e2009-05-02 15:13:40 +0000194 }
195
Rich Evansf90016a2015-01-19 14:26:37 +0000196 polarssl_printf( " ok\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000197
198 /*
199 * 1.5. Load own private key
200 */
201 snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000202
Rich Evansf90016a2015-01-19 14:26:37 +0000203 polarssl_printf( " . Loading the client private key %s...", name );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000204 fflush( stdout );
205
Paul Bakker36713e82013-09-17 13:25:29 +0200206 ret = pk_parse_keyfile( &pk, name, NULL );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000207 if( ret != 0 )
208 {
Rich Evansf90016a2015-01-19 14:26:37 +0000209 polarssl_printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000210 goto exit;
211 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000212
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000214
Paul Bakkerd98030e2009-05-02 15:13:40 +0000215 /*
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200216 * 1.6. Verify certificate validity with private key
Paul Bakkerd98030e2009-05-02 15:13:40 +0000217 */
Rich Evansf90016a2015-01-19 14:26:37 +0000218 polarssl_printf( " . Verify the client certificate with private key..." );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000219 fflush( stdout );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000220
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200221
222 /* EC NOT IMPLEMENTED YET */
Manuel Pégourié-Gonnard7e56de12013-08-14 21:15:53 +0200223 if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) )
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200224 {
Rich Evansf90016a2015-01-19 14:26:37 +0000225 polarssl_printf( " failed\n ! certificate's key is not RSA\n\n" );
Manuel Pégourié-Gonnardbe506802013-07-11 13:17:21 +0200226 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
227 goto exit;
228 }
229
Paul Bakker36713e82013-09-17 13:25:29 +0200230 ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000231 if( ret != 0 )
232 {
Rich Evansf90016a2015-01-19 14:26:37 +0000233 polarssl_printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000234 goto exit;
235 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000236
Paul Bakker36713e82013-09-17 13:25:29 +0200237 ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E);
Paul Bakkerd98030e2009-05-02 15:13:40 +0000238 if( ret != 0 )
239 {
Rich Evansf90016a2015-01-19 14:26:37 +0000240 polarssl_printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000241 goto exit;
242 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000243
Paul Bakker36713e82013-09-17 13:25:29 +0200244 ret = rsa_check_privkey( pk_rsa( pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000245 if( ret != 0 )
246 {
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000248 goto exit;
249 }
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000250
Rich Evansf90016a2015-01-19 14:26:37 +0000251 polarssl_printf( " ok\n" );
Paul Bakkera1d3e5f2009-03-28 17:30:26 +0000252
Paul Bakker36713e82013-09-17 13:25:29 +0200253 x509_crt_free( &clicert );
254 pk_free( &pk );
Paul Bakker4593aea2009-02-09 22:32:35 +0000255 }
256
257exit:
Paul Bakker36713e82013-09-17 13:25:29 +0200258 x509_crt_free( &cacert );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000259 x509_crl_free( &crl );
Paul Bakker4593aea2009-02-09 22:32:35 +0000260
Paul Bakkercce9d772011-11-18 14:26:47 +0000261#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000262 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4593aea2009-02-09 22:32:35 +0000263 fflush( stdout ); getchar();
264#endif
265
266 return( ret );
267}
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200268#endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO &&
269 POLARSSL_X509_CRL_PARSE_C */