cert_req.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /********************************* (C) РОТЕК ***********************************
  2. * @module cert_req
  3. * @file cert_req.c
  4. * @version 1.0.0
  5. * @date XX.XX.XXXX
  6. *******************************************************************************
  7. * @history Version Author Comment
  8. * XX.XX.XXXX 1.0.0 Telenkov D.A. First release.
  9. *******************************************************************************
  10. */
  11. #include "cert_req.h"
  12. #if !defined(MBEDTLS_CONFIG_FILE)
  13. #include "mbedtls/config.h"
  14. #else
  15. #include MBEDTLS_CONFIG_FILE
  16. #endif
  17. #include "mbedtls/platform.h"
  18. #include "mbedtls/x509_csr.h"
  19. #include "mbedtls/entropy.h"
  20. #include "mbedtls/ctr_drbg.h"
  21. #include "mbedtls/error.h"
  22. #include "mbedtls/certs.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define DFL_FILENAME "keyfile.key"
  27. #define DFL_DEBUG_LEVEL 0
  28. #define DFL_OUTPUT_FILENAME "cert.req"
  29. #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK" // Надо CN - ip, O - VimpelCom, C=RU
  30. #define DFL_KEY_USAGE 0
  31. #define DFL_NS_CERT_TYPE 0
  32. /*
  33. * global options
  34. */
  35. struct options
  36. {
  37. const char *filename; /* filename of the key file */
  38. int debug_level; /* level of debugging */
  39. const char *output_file; /* where to store the constructed key file */
  40. const char *subject_name; /* subject name for certificate request */
  41. unsigned char key_usage; /* key usage flags */
  42. unsigned char ns_cert_type; /* NS cert type */
  43. } opt;
  44. static int write_certificate_request( mbedtls_x509write_csr *req, const char *output_file,
  45. int (*f_rng)(void *, unsigned char *, size_t),
  46. void *p_rng );
  47. unsigned char output_buf[4096];
  48. void SSL_Test()
  49. {
  50. int ret = 0;
  51. mbedtls_pk_context key;
  52. char buf[1024];
  53. mbedtls_x509write_csr req;
  54. mbedtls_entropy_context entropy;
  55. mbedtls_ctr_drbg_context ctr_drbg;
  56. const char *pers = "csr example app";
  57. // Set to sane values
  58. mbedtls_x509write_csr_init( &req );
  59. mbedtls_x509write_csr_set_md_alg( &req, MBEDTLS_MD_SHA256 );
  60. mbedtls_pk_init( &key );
  61. mbedtls_ctr_drbg_init( &ctr_drbg );
  62. memset( buf, 0, sizeof( buf ) );
  63. // default
  64. opt.filename = DFL_FILENAME;
  65. opt.debug_level = DFL_DEBUG_LEVEL;
  66. opt.output_file = DFL_OUTPUT_FILENAME;
  67. opt.subject_name = DFL_SUBJECT_NAME;
  68. opt.key_usage = DFL_KEY_USAGE;
  69. opt.ns_cert_type = DFL_NS_CERT_TYPE;
  70. // user
  71. opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  72. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
  73. if( opt.key_usage )
  74. mbedtls_x509write_csr_set_key_usage( &req, opt.key_usage );
  75. if( opt.ns_cert_type )
  76. mbedtls_x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
  77. // 0. Seed the PRNG
  78. mbedtls_printf( " . Seeding the random number generator..." );
  79. fflush( stdout );
  80. mbedtls_entropy_init( &entropy );
  81. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  82. (const unsigned char *) pers,
  83. strlen( pers ) ) ) != 0 )
  84. {
  85. mbedtls_printf( " failed\r\n ! mbedtls_ctr_drbg_seed returned %d", ret );
  86. goto exit;
  87. }
  88. mbedtls_printf( " ok\r\n" );
  89. // 1.0. Check the subject name for validity
  90. mbedtls_printf( " . Checking subject name..." );
  91. fflush( stdout );
  92. if( ( ret = mbedtls_x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
  93. {
  94. mbedtls_printf( " failed\r\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret );
  95. goto exit;
  96. }
  97. mbedtls_printf( " ok\r\n" );
  98. // 1.1. Load the key
  99. mbedtls_printf( " . Loading the private key ..." );
  100. fflush( stdout );
  101. //ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
  102. ret = mbedtls_pk_parse_key( &key, (const unsigned char *) mbedtls_test_srv_key, mbedtls_test_srv_key_len, NULL, 0 );
  103. if( ret != 0 )
  104. {
  105. mbedtls_printf( " failed\r\n ! mbedtls_pk_parse_keyfile returned %d", ret );
  106. goto exit;
  107. }
  108. mbedtls_x509write_csr_set_key( &req, &key );
  109. mbedtls_printf( " ok\r\n" );
  110. // 1.2. Writing the request
  111. mbedtls_printf( " . Writing the certificate request ..." );
  112. //if( ( ret = write_certificate_request( &req, opt.output_file, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  113. ret = mbedtls_x509write_csr_pem( &req, output_buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg );
  114. if (ret != 0)
  115. {
  116. mbedtls_printf( " failed\r\n ! write_certifcate_request %d", ret );
  117. goto exit;
  118. }
  119. mbedtls_printf( " ok\r\n" );
  120. exit:
  121. if( ret != 0 && ret != 1)
  122. {
  123. mbedtls_printf("\r\n");
  124. }
  125. mbedtls_x509write_csr_free( &req );
  126. mbedtls_pk_free( &key );
  127. mbedtls_ctr_drbg_free( &ctr_drbg );
  128. mbedtls_entropy_free( &entropy );
  129. }
  130. //
  131. static int write_certificate_request( mbedtls_x509write_csr *req, const char *output_file,
  132. int (*f_rng)(void *, unsigned char *, size_t),
  133. void *p_rng )
  134. {
  135. /*
  136. int ret;
  137. FILE *f;
  138. unsigned char output_buf[4096];
  139. size_t len = 0;
  140. memset( output_buf, 0, 4096 );
  141. if( ( ret = mbedtls_x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
  142. return( ret );
  143. len = strlen( (char *) output_buf );
  144. if( ( f = fopen( output_file, "w" ) ) == NULL )
  145. return( -1 );
  146. if( fwrite( output_buf, 1, len, f ) != len )
  147. {
  148. fclose( f );
  149. return( -1 );
  150. }
  151. fclose( f );
  152. */
  153. return( 0 );
  154. }
  155. /********************************* (C) РОТЕК **********************************/