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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame^] | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame^] | 28 | #else |
| 29 | #include POLARSSL_CONFIG_FILE |
| 30 | #endif |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 31 | |
| 32 | #include <string.h> |
| 33 | #include <stdio.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) || \ |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 36 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_X509_CRL_PARSE_C) |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 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 " |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 43 | "POLARSSL_FS_IO and/or POLARSSL_X509_CRL_PARSE_C " |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 44 | "not defined.\n"); |
| 45 | return( 0 ); |
| 46 | } |
| 47 | #else |
| 48 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 49 | #include "polarssl/certs.h" |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 50 | #include "polarssl/x509_crt.h" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 51 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 52 | #if defined _MSC_VER && !defined snprintf |
| 53 | #define snprintf _snprintf |
| 54 | #endif |
| 55 | |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 56 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 57 | #define MAX_CLIENT_CERTS 8 |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 58 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 59 | const char *client_certificates[MAX_CLIENT_CERTS] = |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 60 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 61 | "client1.crt", |
| 62 | "client2.crt", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 63 | "server1.crt", |
| 64 | "server2.crt", |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 65 | "cert_sha224.crt", |
| 66 | "cert_sha256.crt", |
| 67 | "cert_sha384.crt", |
| 68 | "cert_sha512.crt" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 71 | const char *client_private_keys[MAX_CLIENT_CERTS] = |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 72 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 73 | "client1.key", |
| 74 | "client2.key", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 75 | "server1.key", |
| 76 | "server2.key", |
Paul Bakker | f17ed28 | 2011-02-09 17:10:48 +0000 | [diff] [blame] | 77 | "cert_digest.key", |
| 78 | "cert_digest.key", |
| 79 | "cert_digest.key", |
| 80 | "cert_digest.key" |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 83 | int main( int argc, char *argv[] ) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 84 | { |
| 85 | int ret, i; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 86 | x509_crt cacert; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 87 | x509_crl crl; |
| 88 | char buf[10240]; |
| 89 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 90 | ((void) argc); |
| 91 | ((void) argv); |
| 92 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 93 | x509_crt_init( &cacert ); |
| 94 | x509_crl_init( &crl ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * 1.1. Load the trusted CA |
| 98 | */ |
| 99 | printf( "\n . Loading the CA root certificate ..." ); |
| 100 | fflush( stdout ); |
| 101 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 102 | /* |
| 103 | * Alternatively, you may load the CA certificates from a .pem or |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 104 | * .crt file by calling x509_crt_parse_file( &cacert, "myca.crt" ). |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 105 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 106 | ret = x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 107 | if( ret != 0 ) |
| 108 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 109 | printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | printf( " ok\n" ); |
| 114 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 115 | x509_crt_info( buf, 1024, "CRT: ", &cacert ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 116 | printf("%s\n", buf ); |
| 117 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 118 | /* |
| 119 | * 1.2. Load the CRL |
| 120 | */ |
| 121 | printf( " . Loading the CRL ..." ); |
| 122 | fflush( stdout ); |
| 123 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 124 | ret = x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 125 | if( ret != 0 ) |
| 126 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 127 | printf( " failed\n ! x509_crl_parse_file returned %d\n\n", ret ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 128 | goto exit; |
| 129 | } |
| 130 | |
| 131 | printf( " ok\n" ); |
| 132 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 133 | x509_crl_info( buf, 1024, "CRL: ", &crl ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 134 | printf("%s\n", buf ); |
| 135 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 136 | for( i = 0; i < MAX_CLIENT_CERTS; i++ ) |
| 137 | { |
| 138 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 139 | * 1.3. Load own certificate |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 140 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 141 | char name[512]; |
| 142 | int flags; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 143 | x509_crt clicert; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 144 | pk_context pk; |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 145 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 146 | x509_crt_init( &clicert ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 147 | pk_init( &pk ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 148 | |
| 149 | snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); |
| 150 | |
| 151 | printf( " . Loading the client certificate %s...", name ); |
| 152 | fflush( stdout ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 153 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 154 | ret = x509_crt_parse_file( &clicert, name ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 155 | if( ret != 0 ) |
| 156 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 157 | printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 158 | goto exit; |
| 159 | } |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 160 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 161 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 164 | * 1.4. Verify certificate validity with CA certificate |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 165 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 166 | printf( " . Verify the client certificate with CA certificate..." ); |
| 167 | fflush( stdout ); |
| 168 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 169 | ret = x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, |
| 170 | NULL ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 171 | if( ret != 0 ) |
| 172 | { |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 173 | if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ) |
| 174 | { |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 175 | if( flags & BADCERT_CN_MISMATCH ) |
| 176 | printf( " CN_MISMATCH " ); |
| 177 | if( flags & BADCERT_EXPIRED ) |
| 178 | printf( " EXPIRED " ); |
| 179 | if( flags & BADCERT_REVOKED ) |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 180 | printf( " REVOKED " ); |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 181 | if( flags & BADCERT_NOT_TRUSTED ) |
| 182 | printf( " NOT_TRUSTED " ); |
| 183 | if( flags & BADCRL_NOT_TRUSTED ) |
| 184 | printf( " CRL_NOT_TRUSTED " ); |
| 185 | if( flags & BADCRL_EXPIRED ) |
| 186 | printf( " CRL_EXPIRED " ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 187 | } else { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 188 | printf( " failed\n ! x509_crt_verify returned %d\n\n", ret ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 189 | goto exit; |
| 190 | } |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | printf( " ok\n" ); |
| 194 | |
| 195 | /* |
| 196 | * 1.5. Load own private key |
| 197 | */ |
| 198 | snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 199 | |
| 200 | printf( " . Loading the client private key %s...", name ); |
| 201 | fflush( stdout ); |
| 202 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 203 | ret = pk_parse_keyfile( &pk, name, NULL ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 204 | if( ret != 0 ) |
| 205 | { |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 206 | printf( " failed\n ! pk_parse_keyfile returned %d\n\n", ret ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 207 | goto exit; |
| 208 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 209 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 210 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 211 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 212 | /* |
Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 213 | * 1.6. Verify certificate validity with private key |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 214 | */ |
| 215 | printf( " . Verify the client certificate with private key..." ); |
| 216 | fflush( stdout ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 217 | |
Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 218 | |
| 219 | /* EC NOT IMPLEMENTED YET */ |
Manuel Pégourié-Gonnard | 7e56de1 | 2013-08-14 21:15:53 +0200 | [diff] [blame] | 220 | if( ! pk_can_do( &clicert.pk, POLARSSL_PK_RSA ) ) |
Manuel Pégourié-Gonnard | be50680 | 2013-07-11 13:17:21 +0200 | [diff] [blame] | 221 | { |
| 222 | printf( " failed\n ! certificate's key is not RSA\n\n" ); |
| 223 | ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE; |
| 224 | goto exit; |
| 225 | } |
| 226 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 227 | 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] | 228 | if( ret != 0 ) |
| 229 | { |
| 230 | printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret ); |
| 231 | goto exit; |
| 232 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 233 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 234 | 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] | 235 | if( ret != 0 ) |
| 236 | { |
| 237 | printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret ); |
| 238 | goto exit; |
| 239 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 240 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 241 | ret = rsa_check_privkey( pk_rsa( pk ) ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 242 | if( ret != 0 ) |
| 243 | { |
| 244 | printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret ); |
| 245 | goto exit; |
| 246 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 247 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 248 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 249 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 250 | x509_crt_free( &clicert ); |
| 251 | pk_free( &pk ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | exit: |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 255 | x509_crt_free( &cacert ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 256 | x509_crl_free( &crl ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 257 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 258 | #if defined(_WIN32) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 259 | printf( " + Press Enter to exit this program.\n" ); |
| 260 | fflush( stdout ); getchar(); |
| 261 | #endif |
| 262 | |
| 263 | return( ret ); |
| 264 | } |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 265 | #endif /* POLARSSL_RSA_C && POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && |
| 266 | POLARSSL_X509_CRL_PARSE_C */ |