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