ssl_cert_test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * SSL certificate functionality tests
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #define mbedtls_snprintf snprintf
  31. #define mbedtls_printf printf
  32. #endif
  33. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \
  34. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C)
  35. #include "mbedtls/certs.h"
  36. #include "mbedtls/x509_crt.h"
  37. #include <stdio.h>
  38. #include <string.h>
  39. #endif
  40. #define MAX_CLIENT_CERTS 8
  41. #if !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
  42. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_X509_CRL_PARSE_C)
  43. int main( void )
  44. {
  45. mbedtls_printf("MBEDTLS_RSA_C and/or MBEDTLS_X509_CRT_PARSE_C "
  46. "MBEDTLS_FS_IO and/or MBEDTLS_X509_CRL_PARSE_C "
  47. "not defined.\n");
  48. return( 0 );
  49. }
  50. #else
  51. const char *client_certificates[MAX_CLIENT_CERTS] =
  52. {
  53. "client1.crt",
  54. "client2.crt",
  55. "server1.crt",
  56. "server2.crt",
  57. "cert_sha224.crt",
  58. "cert_sha256.crt",
  59. "cert_sha384.crt",
  60. "cert_sha512.crt"
  61. };
  62. const char *client_private_keys[MAX_CLIENT_CERTS] =
  63. {
  64. "client1.key",
  65. "client2.key",
  66. "server1.key",
  67. "server2.key",
  68. "cert_digest.key",
  69. "cert_digest.key",
  70. "cert_digest.key",
  71. "cert_digest.key"
  72. };
  73. int main( void )
  74. {
  75. int ret, i;
  76. mbedtls_x509_crt cacert;
  77. mbedtls_x509_crl crl;
  78. char buf[10240];
  79. mbedtls_x509_crt_init( &cacert );
  80. mbedtls_x509_crl_init( &crl );
  81. /*
  82. * 1.1. Load the trusted CA
  83. */
  84. mbedtls_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 mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ).
  89. */
  90. ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
  91. if( ret != 0 )
  92. {
  93. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
  94. goto exit;
  95. }
  96. mbedtls_printf( " ok\n" );
  97. mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert );
  98. mbedtls_printf("%s\n", buf );
  99. /*
  100. * 1.2. Load the CRL
  101. */
  102. mbedtls_printf( " . Loading the CRL ..." );
  103. fflush( stdout );
  104. ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
  105. if( ret != 0 )
  106. {
  107. mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
  108. goto exit;
  109. }
  110. mbedtls_printf( " ok\n" );
  111. mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl );
  112. mbedtls_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. uint32_t flags;
  120. mbedtls_x509_crt clicert;
  121. mbedtls_pk_context pk;
  122. mbedtls_x509_crt_init( &clicert );
  123. mbedtls_pk_init( &pk );
  124. mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
  125. mbedtls_printf( " . Loading the client certificate %s...", name );
  126. fflush( stdout );
  127. ret = mbedtls_x509_crt_parse_file( &clicert, name );
  128. if( ret != 0 )
  129. {
  130. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
  131. goto exit;
  132. }
  133. mbedtls_printf( " ok\n" );
  134. /*
  135. * 1.4. Verify certificate validity with CA certificate
  136. */
  137. mbedtls_printf( " . Verify the client certificate with CA certificate..." );
  138. fflush( stdout );
  139. ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
  140. NULL );
  141. if( ret != 0 )
  142. {
  143. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  144. {
  145. char vrfy_buf[512];
  146. mbedtls_printf( " failed\n" );
  147. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  148. mbedtls_printf( "%s\n", vrfy_buf );
  149. }
  150. else
  151. {
  152. mbedtls_printf( " failed\n ! mbedtls_x509_crt_verify returned %d\n\n", ret );
  153. goto exit;
  154. }
  155. }
  156. mbedtls_printf( " ok\n" );
  157. /*
  158. * 1.5. Load own private key
  159. */
  160. mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
  161. mbedtls_printf( " . Loading the client private key %s...", name );
  162. fflush( stdout );
  163. ret = mbedtls_pk_parse_keyfile( &pk, name, NULL );
  164. if( ret != 0 )
  165. {
  166. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d\n\n", ret );
  167. goto exit;
  168. }
  169. mbedtls_printf( " ok\n" );
  170. /*
  171. * 1.6. Verify certificate validity with private key
  172. */
  173. mbedtls_printf( " . Verify the client certificate with private key..." );
  174. fflush( stdout );
  175. /* EC NOT IMPLEMENTED YET */
  176. if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
  177. {
  178. mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
  179. ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  180. goto exit;
  181. }
  182. ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N);
  183. if( ret != 0 )
  184. {
  185. mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret );
  186. goto exit;
  187. }
  188. ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E);
  189. if( ret != 0 )
  190. {
  191. mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret );
  192. goto exit;
  193. }
  194. ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) );
  195. if( ret != 0 )
  196. {
  197. mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey returned %d\n\n", ret );
  198. goto exit;
  199. }
  200. mbedtls_printf( " ok\n" );
  201. mbedtls_x509_crt_free( &clicert );
  202. mbedtls_pk_free( &pk );
  203. }
  204. exit:
  205. mbedtls_x509_crt_free( &cacert );
  206. mbedtls_x509_crl_free( &crl );
  207. #if defined(_WIN32)
  208. mbedtls_printf( " + Press Enter to exit this program.\n" );
  209. fflush( stdout ); getchar();
  210. #endif
  211. return( ret );
  212. }
  213. #endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
  214. MBEDTLS_X509_CRL_PARSE_C */