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