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