ssl_cert_test.c 6.8 KB

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