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