ssl_cert_test.c 6.7 KB

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