Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL certificate functionality tests |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame^] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 7 | * |
| 8 | * 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 | |
| 23 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 24 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 30 | #include "polarssl/config.h" |
| 31 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 32 | #include "polarssl/certs.h" |
| 33 | #include "polarssl/x509.h" |
| 34 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 35 | #if defined _MSC_VER && !defined snprintf |
| 36 | #define snprintf _snprintf |
| 37 | #endif |
| 38 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 39 | #define MAX_CLIENT_CERTS 8 |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 40 | |
Paul Bakker | e0225e4 | 2013-06-06 12:52:24 +0200 | [diff] [blame] | 41 | const char *client_certificates[MAX_CLIENT_CERTS] = |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 42 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 43 | "client1.crt", |
| 44 | "client2.crt", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 45 | "server1.crt", |
| 46 | "server2.crt", |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 47 | "cert_sha224.crt", |
| 48 | "cert_sha256.crt", |
| 49 | "cert_sha384.crt", |
| 50 | "cert_sha512.crt" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Paul Bakker | e0225e4 | 2013-06-06 12:52:24 +0200 | [diff] [blame] | 53 | const char *client_private_keys[MAX_CLIENT_CERTS] = |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 54 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 55 | "client1.key", |
| 56 | "client2.key", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 57 | "server1.key", |
| 58 | "server2.key", |
Paul Bakker | f17ed28 | 2011-02-09 17:10:48 +0000 | [diff] [blame] | 59 | "cert_digest.key", |
| 60 | "cert_digest.key", |
| 61 | "cert_digest.key", |
| 62 | "cert_digest.key" |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 65 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 66 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 67 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 68 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 69 | ((void) argc); |
| 70 | ((void) argv); |
| 71 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 72 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 73 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 74 | return( 0 ); |
| 75 | } |
| 76 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 77 | int main( int argc, char *argv[] ) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 78 | { |
| 79 | int ret, i; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 80 | x509_cert cacert; |
| 81 | x509_crl crl; |
| 82 | char buf[10240]; |
| 83 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 84 | ((void) argc); |
| 85 | ((void) argv); |
| 86 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 87 | memset( &cacert, 0, sizeof( x509_cert ) ); |
| 88 | memset( &crl, 0, sizeof( x509_crl ) ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * 1.1. Load the trusted CA |
| 92 | */ |
| 93 | printf( "\n . Loading the CA root certificate ..." ); |
| 94 | fflush( stdout ); |
| 95 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 96 | /* |
| 97 | * Alternatively, you may load the CA certificates from a .pem or |
| 98 | * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ). |
| 99 | */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 100 | ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 101 | if( ret != 0 ) |
| 102 | { |
| 103 | printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret ); |
| 104 | goto exit; |
| 105 | } |
| 106 | |
| 107 | printf( " ok\n" ); |
| 108 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 109 | x509parse_cert_info( buf, 1024, "CRT: ", &cacert ); |
| 110 | printf("%s\n", buf ); |
| 111 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 112 | /* |
| 113 | * 1.2. Load the CRL |
| 114 | */ |
| 115 | printf( " . Loading the CRL ..." ); |
| 116 | fflush( stdout ); |
| 117 | |
| 118 | ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" ); |
| 119 | if( ret != 0 ) |
| 120 | { |
| 121 | printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret ); |
| 122 | goto exit; |
| 123 | } |
| 124 | |
| 125 | printf( " ok\n" ); |
| 126 | |
| 127 | x509parse_crl_info( buf, 1024, "CRL: ", &crl ); |
| 128 | printf("%s\n", buf ); |
| 129 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 130 | for( i = 0; i < MAX_CLIENT_CERTS; i++ ) |
| 131 | { |
| 132 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 133 | * 1.3. Load own certificate |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 134 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 135 | char name[512]; |
| 136 | int flags; |
| 137 | x509_cert clicert; |
| 138 | rsa_context rsa; |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 139 | |
| 140 | memset( &clicert, 0, sizeof( x509_cert ) ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 141 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 142 | |
| 143 | snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); |
| 144 | |
| 145 | printf( " . Loading the client certificate %s...", name ); |
| 146 | fflush( stdout ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 147 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 148 | ret = x509parse_crtfile( &clicert, name ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 149 | if( ret != 0 ) |
| 150 | { |
| 151 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 152 | goto exit; |
| 153 | } |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 154 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 155 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 158 | * 1.4. Verify certificate validity with CA certificate |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 159 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 160 | printf( " . Verify the client certificate with CA certificate..." ); |
| 161 | fflush( stdout ); |
| 162 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 163 | ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags, NULL, NULL ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 164 | if( ret != 0 ) |
| 165 | { |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 166 | if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ) |
| 167 | { |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 168 | if( flags & BADCERT_CN_MISMATCH ) |
| 169 | printf( " CN_MISMATCH " ); |
| 170 | if( flags & BADCERT_EXPIRED ) |
| 171 | printf( " EXPIRED " ); |
| 172 | if( flags & BADCERT_REVOKED ) |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 173 | printf( " REVOKED " ); |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 174 | if( flags & BADCERT_NOT_TRUSTED ) |
| 175 | printf( " NOT_TRUSTED " ); |
| 176 | if( flags & BADCRL_NOT_TRUSTED ) |
| 177 | printf( " CRL_NOT_TRUSTED " ); |
| 178 | if( flags & BADCRL_EXPIRED ) |
| 179 | printf( " CRL_EXPIRED " ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 180 | } else { |
| 181 | printf( " failed\n ! x509parse_verify returned %d\n\n", ret ); |
| 182 | goto exit; |
| 183 | } |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | printf( " ok\n" ); |
| 187 | |
| 188 | /* |
| 189 | * 1.5. Load own private key |
| 190 | */ |
| 191 | snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 192 | |
| 193 | printf( " . Loading the client private key %s...", name ); |
| 194 | fflush( stdout ); |
| 195 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 196 | ret = x509parse_keyfile( &rsa, name, NULL ); |
| 197 | if( ret != 0 ) |
| 198 | { |
| 199 | printf( " failed\n ! x509parse_key returned %d\n\n", ret ); |
| 200 | goto exit; |
| 201 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 202 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 203 | printf( " ok\n" ); |
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 | /* |
| 206 | * 1.5. Verify certificate validity with private key |
| 207 | */ |
| 208 | printf( " . Verify the client certificate with private key..." ); |
| 209 | fflush( stdout ); |
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 | ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N); |
| 212 | if( ret != 0 ) |
| 213 | { |
| 214 | printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret ); |
| 215 | goto exit; |
| 216 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 217 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 218 | ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E); |
| 219 | if( ret != 0 ) |
| 220 | { |
| 221 | printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret ); |
| 222 | goto exit; |
| 223 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 224 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 225 | ret = rsa_check_privkey( &rsa ); |
| 226 | if( ret != 0 ) |
| 227 | { |
| 228 | printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret ); |
| 229 | goto exit; |
| 230 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 231 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 232 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 233 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 234 | x509_free( &clicert ); |
| 235 | rsa_free( &rsa ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | exit: |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 239 | x509_free( &cacert ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 240 | x509_crl_free( &crl ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 241 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 242 | #if defined(_WIN32) |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 243 | printf( " + Press Enter to exit this program.\n" ); |
| 244 | fflush( stdout ); getchar(); |
| 245 | #endif |
| 246 | |
| 247 | return( ret ); |
| 248 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 249 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C && |
| 250 | POLARSSL_FS_IO */ |