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