Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL certificate functionality tests |
| 3 | * |
Paul Bakker | fc8c436 | 2010-03-21 17:37:16 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 23 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 24 | #endif |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | #include "polarssl/certs.h" |
| 30 | #include "polarssl/x509.h" |
| 31 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 32 | #if defined _MSC_VER && !defined snprintf |
| 33 | #define snprintf _snprintf |
| 34 | #endif |
| 35 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 36 | #define MAX_CLIENT_CERTS 8 |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 37 | |
| 38 | char *client_certificates[MAX_CLIENT_CERTS] = |
| 39 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 40 | "client1.crt", |
| 41 | "client2.crt", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 42 | "server1.crt", |
| 43 | "server2.crt", |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 44 | "cert_sha224.crt", |
| 45 | "cert_sha256.crt", |
| 46 | "cert_sha384.crt", |
| 47 | "cert_sha512.crt" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 50 | char *client_private_keys[MAX_CLIENT_CERTS] = |
| 51 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 52 | "client1.key", |
| 53 | "client2.key", |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 54 | "server1.key", |
| 55 | "server2.key", |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 56 | "cert_sha224.key", |
| 57 | "cert_sha256.key", |
| 58 | "cert_sha384.key", |
| 59 | "cert_sha512.key" |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 62 | int main( void ) |
| 63 | { |
| 64 | int ret, i; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 65 | x509_cert cacert; |
| 66 | x509_crl crl; |
| 67 | char buf[10240]; |
| 68 | |
| 69 | memset( &cacert, 0, sizeof( x509_cert ) ); |
| 70 | memset( &crl, 0, sizeof( x509_crl ) ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * 1.1. Load the trusted CA |
| 74 | */ |
| 75 | printf( "\n . Loading the CA root certificate ..." ); |
| 76 | fflush( stdout ); |
| 77 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 78 | /* |
| 79 | * Alternatively, you may load the CA certificates from a .pem or |
| 80 | * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ). |
| 81 | */ |
| 82 | ret = x509parse_crtfile( &cacert, "ssl/test-ca/test-ca.crt" ); |
| 83 | if( ret != 0 ) |
| 84 | { |
| 85 | printf( " failed\n ! x509parse_crtfile returned %d\n\n", ret ); |
| 86 | goto exit; |
| 87 | } |
| 88 | |
| 89 | printf( " ok\n" ); |
| 90 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 91 | x509parse_cert_info( buf, 1024, "CRT: ", &cacert ); |
| 92 | printf("%s\n", buf ); |
| 93 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 94 | /* |
| 95 | * 1.2. Load the CRL |
| 96 | */ |
| 97 | printf( " . Loading the CRL ..." ); |
| 98 | fflush( stdout ); |
| 99 | |
| 100 | ret = x509parse_crlfile( &crl, "ssl/test-ca/crl.pem" ); |
| 101 | if( ret != 0 ) |
| 102 | { |
| 103 | printf( " failed\n ! x509parse_crlfile returned %d\n\n", ret ); |
| 104 | goto exit; |
| 105 | } |
| 106 | |
| 107 | printf( " ok\n" ); |
| 108 | |
| 109 | x509parse_crl_info( buf, 1024, "CRL: ", &crl ); |
| 110 | printf("%s\n", buf ); |
| 111 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 112 | for( i = 0; i < MAX_CLIENT_CERTS; i++ ) |
| 113 | { |
| 114 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 115 | * 1.3. Load own certificate |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 116 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 117 | char name[512]; |
| 118 | int flags; |
| 119 | x509_cert clicert; |
| 120 | rsa_context rsa; |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 121 | |
| 122 | memset( &clicert, 0, sizeof( x509_cert ) ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 123 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 124 | |
| 125 | snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); |
| 126 | |
| 127 | printf( " . Loading the client certificate %s...", name ); |
| 128 | fflush( stdout ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 129 | |
| 130 | ret = x509parse_crtfile( &clicert, name ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 131 | if( ret != 0 ) |
| 132 | { |
| 133 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 134 | goto exit; |
| 135 | } |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 136 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 137 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 140 | * 1.4. Verify certificate validity with CA certificate |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 141 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 142 | printf( " . Verify the client certificate with CA certificate..." ); |
| 143 | fflush( stdout ); |
| 144 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 145 | ret = x509parse_verify( &clicert, &cacert, &crl, NULL, &flags ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 146 | if( ret != 0 ) |
| 147 | { |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 148 | if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ) |
| 149 | { |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 150 | if( flags & BADCERT_CN_MISMATCH ) |
| 151 | printf( " CN_MISMATCH " ); |
| 152 | if( flags & BADCERT_EXPIRED ) |
| 153 | printf( " EXPIRED " ); |
| 154 | if( flags & BADCERT_REVOKED ) |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 155 | printf( " REVOKED " ); |
Paul Bakker | 860d36b | 2009-05-03 17:29:56 +0000 | [diff] [blame] | 156 | if( flags & BADCERT_NOT_TRUSTED ) |
| 157 | printf( " NOT_TRUSTED " ); |
| 158 | if( flags & BADCRL_NOT_TRUSTED ) |
| 159 | printf( " CRL_NOT_TRUSTED " ); |
| 160 | if( flags & BADCRL_EXPIRED ) |
| 161 | printf( " CRL_EXPIRED " ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 162 | } else { |
| 163 | printf( " failed\n ! x509parse_verify returned %d\n\n", ret ); |
| 164 | goto exit; |
| 165 | } |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | printf( " ok\n" ); |
| 169 | |
| 170 | /* |
| 171 | * 1.5. Load own private key |
| 172 | */ |
| 173 | snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 174 | |
| 175 | printf( " . Loading the client private key %s...", name ); |
| 176 | fflush( stdout ); |
| 177 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 178 | ret = x509parse_keyfile( &rsa, name, NULL ); |
| 179 | if( ret != 0 ) |
| 180 | { |
| 181 | printf( " failed\n ! x509parse_key returned %d\n\n", ret ); |
| 182 | goto exit; |
| 183 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 184 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 185 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 186 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 187 | /* |
| 188 | * 1.5. Verify certificate validity with private key |
| 189 | */ |
| 190 | printf( " . Verify the client certificate with private key..." ); |
| 191 | fflush( stdout ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 192 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 193 | ret = mpi_cmp_mpi(&rsa.N, &clicert.rsa.N); |
| 194 | if( ret != 0 ) |
| 195 | { |
| 196 | printf( " failed\n ! mpi_cmp_mpi for N returned %d\n\n", ret ); |
| 197 | goto exit; |
| 198 | } |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 199 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 200 | ret = mpi_cmp_mpi(&rsa.E, &clicert.rsa.E); |
| 201 | if( ret != 0 ) |
| 202 | { |
| 203 | printf( " failed\n ! mpi_cmp_mpi for E returned %d\n\n", ret ); |
| 204 | goto exit; |
| 205 | } |
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 | ret = rsa_check_privkey( &rsa ); |
| 208 | if( ret != 0 ) |
| 209 | { |
| 210 | printf( " failed\n ! rsa_check_privkey returned %d\n\n", ret ); |
| 211 | goto exit; |
| 212 | } |
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 | printf( " ok\n" ); |
Paul Bakker | a1d3e5f | 2009-03-28 17:30:26 +0000 | [diff] [blame] | 215 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 216 | x509_free( &clicert ); |
| 217 | rsa_free( &rsa ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | exit: |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 221 | x509_free( &cacert ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 222 | x509_crl_free( &crl ); |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 223 | |
| 224 | #ifdef WIN32 |
| 225 | printf( " + Press Enter to exit this program.\n" ); |
| 226 | fflush( stdout ); getchar(); |
| 227 | #endif |
| 228 | |
| 229 | return( ret ); |
| 230 | } |