Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL certificate functionality tests |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 27 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 28 | #endif |
| 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 33 | #include "polarssl/config.h" |
| 34 | |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame^] | 35 | #if !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \ |
| 36 | !defined(POLARSSL_FS_IO) |
| 37 | int main( int argc, char *argv[] ) |
| 38 | { |
| 39 | ((void) argc); |
| 40 | ((void) argv); |
| 41 | |
| 42 | printf("POLARSSL_RSA_C and/or POLARSSL_X509_CRT_PARSE_C " |
| 43 | "not defined.\n"); |
| 44 | return( 0 ); |
| 45 | } |
| 46 | #else |
| 47 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 48 | #include "polarssl/certs.h" |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 49 | #include "polarssl/x509_crt.h" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 50 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 51 | #if defined _MSC_VER && !defined snprintf |
| 52 | #define snprintf _snprintf |
| 53 | #endif |
| 54 | |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame^] | 55 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 56 | #define MAX_CLIENT_CERTS 8 |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 57 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 58 | const char *client_certificates[MAX_CLIENT_CERTS] = |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 59 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 60 | "client1.crt", |
| 61 | "client2.crt", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 62 | "server1.crt", |
| 63 | "server2.crt", |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 64 | "cert_sha224.crt", |
| 65 | "cert_sha256.crt", |
| 66 | "cert_sha384.crt", |
| 67 | "cert_sha512.crt" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 70 | const char *client_private_keys[MAX_CLIENT_CERTS] = |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 71 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 72 | "client1.key", |
| 73 | "client2.key", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 74 | "server1.key", |
| 75 | "server2.key", |
Paul Bakker | f17ed28 | 2011-02-09 17:10:48 +0000 | [diff] [blame] | 76 | "cert_digest.key", |
| 77 | "cert_digest.key", |
| 78 | "cert_digest.key", |
| 79 | "cert_digest.key" |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 82 | int main( int argc, char *argv[] ) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 83 | { |
| 84 | int ret, i; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 85 | x509_crt cacert; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 86 | x509_crl crl; |
| 87 | char buf[10240]; |
| 88 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 89 | ((void) argc); |
| 90 | ((void) argv); |
| 91 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 92 | x509_crt_init( &cacert ); |
| 93 | x509_crl_init( &crl ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * 1.1. Load the trusted CA |
| 97 | */ |
| 98 | printf( "\n . Loading the CA root certificate ..." ); |
| 99 | fflush( stdout ); |
| 100 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Alternatively, you may load the CA certificates from a .pem or |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 103 | * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ). |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 104 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 105 | ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 106 | if( ret != 0 ) |
| 107 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 108 | printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 109 | goto exit; |
| 110 | } |
| 111 | |
| 112 | printf( " ok\n" ); |
| 113 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 114 | x509_crt_info( buf, 1024, "CRT: ", &cacert ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 115 | printf("%s\n", buf ); |
| 116 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 117 | /* |
| 118 | * 1.2. Load the CRL |
| 119 | */ |
| 120 | printf( " . Loading the CRL ..." ); |
| 121 | fflush( stdout ); |
| 122 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 123 | ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 124 | if( ret != 0 ) |
| 125 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 126 | printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 127 | goto exit; |
| 128 | } |
| 129 | |
| 130 | printf( " ok\n" ); |
| 131 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 132 | x509_crl_info( buf, 1024, "CRL: ", &crl ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 133 | printf("%s\n", buf ); |
| 134 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 135 | for( i = 0; i < MAX_CLIENT_CERTS; i++ ) |
| 136 | { |
| 137 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 138 | * 1.3. Load own certificate |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 139 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 140 | char name[512]; |
| 141 | int flags; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 142 | x509_crt clicert; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 143 | pk_context pk; |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 144 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 145 | x509_crt_init( &clicert ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 146 | pk_init( &pk ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 147 | |
| 148 | snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); |
| 149 | |
| 150 | printf( " . Loading the client certificate %s...", name ); |
| 151 | fflush( stdout ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 152 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 153 | ret = x509_crt_parse_file( &clicert, name ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 154 | if( ret != 0 ) |
| 155 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 156 | printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 157 | goto exit; |
| 158 | } |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 159 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 160 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 163 | * 1.4. Verify certificate validity with CA certificate |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 164 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 165 | printf( " . Verify the client certificate with CA certificate..." ); |
| 166 | fflush( stdout ); |
| 167 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 168 | ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, |
| 169 | NULL ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 170 | if( ret != 0 ) |
| 171 | { |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 172 | if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ) |
| 173 | { |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 174 | if( flags & BADCERT_CN_MISMATCH ) |
| 175 | printf( " CN_MISMATCH " ); |
| 176 | if( flags & BADCERT_EXPIRED ) |
| 177 | printf( " EXPIRED " ); |
| 178 | if( flags & BADCERT_REVOKED ) |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 179 | printf( " REVOKED " ); |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 180 | if( flags & BADCERT_NOT_TRUSTED ) |
| 181 | printf( " NOT_TRUSTED " ); |
| 182 | if( flags & BADCRL_NOT_TRUSTED ) |
| 183 | printf( " CRL_NOT_TRUSTED " ); |
| 184 | if( flags & BADCRL_EXPIRED ) |
| 185 | printf( " CRL_EXPIRED " ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 186 | } else { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 187 | printf( " failed\n ! x509_crt_verify returned %d\n\n", ret ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 188 | goto exit; |
| 189 | } |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | printf( " ok\n" ); |
| 193 | |
| 194 | /* |
| 195 | * 1.5. Load own private key |
| 196 | */ |
| 197 | snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 198 | |
| 199 | printf( " . Loading the client private key %s...", name ); |
| 200 | fflush( stdout ); |
| 201 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 202 | ret = pk_parse_keyfile( &pk, name, NULL ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 203 | if( ret != 0 ) |
| 204 | { |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 205 | printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 206 | goto exit; |
| 207 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 208 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 209 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 210 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 211 | /* |
Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 212 | * 1.6. Verify certificate validity with private key |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 213 | */ |
| 214 | printf( " . Verify the client certificate with private key..." ); |
| 215 | fflush( stdout ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 216 | |
Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 217 | |
| 218 | /* EC NOT IMPLEMENTED YET */ |
Manuel Pégourié-Gonnard | 7e56de1 | 2013-08-14 21:15:53 +0200 | [diff] [blame] | 219 | if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) ) |
Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 220 | { |
| 221 | printf( " failed\n ! certificate's key is not RSA\n\n" ); |
| 222 | ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE; |
| 223 | goto exit; |
| 224 | } |
| 225 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 226 | ret = mpi_cmp_mpi(&pk_rsa( pk )->N, &pk_rsa( clicert.pk )->N); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 227 | if( ret != 0 ) |
| 228 | { |
| 229 | printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret ); |
| 230 | goto exit; |
| 231 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 232 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 233 | ret = mpi_cmp_mpi(&pk_rsa( pk )->E, &pk_rsa( clicert.pk )->E); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 234 | if( ret != 0 ) |
| 235 | { |
| 236 | printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret ); |
| 237 | goto exit; |
| 238 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 239 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 240 | ret = rsa_check_privkey( pk_rsa( pk ) ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 241 | if( ret != 0 ) |
| 242 | { |
| 243 | printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret ); |
| 244 | goto exit; |
| 245 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 246 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 247 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 248 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 249 | x509_crt_free( &clicert ); |
| 250 | pk_free( &pk ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | exit: |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 254 | x509_crt_free( &cacert ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 255 | x509_crl_free( &crl ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 256 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 257 | #if defined(_WIN32) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 258 | printf( " + Press Enter to exit this program.\n" ); |
| 259 | fflush( stdout ); getchar(); |
| 260 | #endif |
| 261 | |
| 262 | return( ret ); |
| 263 | } |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame^] | 264 | #endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO */ |