cert_req.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "settings_api.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #define DFL_FILENAME 0 //"keyfile.key"
  28. #define DFL_DEBUG_LEVEL 0
  29. #define DFL_OUTPUT_FILENAME 0 //"cert.req"
  30. #define DFL_SUBJECT_NAME 0 //"CN=Cert,O=mbed TLS,C=UK" // Надо CN - ip, O - VimpelCom, C=RU
  31. #define DFL_KEY_USAGE 0
  32. #define DFL_NS_CERT_TYPE 0
  33. extern SETTINGS_t sSettings;
  34. struct options
  35. {
  36. const char *filename; /* filename of the key file */
  37. int debug_level; /* level of debugging */
  38. const char *output_file; /* where to store the constructed key file */
  39. const char *subject_name; /* subject name for certificate request */
  40. unsigned char key_usage; /* key usage flags */
  41. unsigned char ns_cert_type; /* NS cert type */
  42. } opt;
  43. unsigned char req_cert[4096];
  44. void SSL_Test()
  45. {
  46. int ret = 0;
  47. mbedtls_pk_context key;
  48. mbedtls_x509write_csr req;
  49. mbedtls_entropy_context entropy;
  50. mbedtls_ctr_drbg_context ctr_drbg;
  51. const char *pers = "csr example app";
  52. char subject_name[40];
  53. // Set to sane values
  54. mbedtls_x509write_csr_init( &req );
  55. mbedtls_x509write_csr_set_md_alg( &req, MBEDTLS_MD_SHA256 );
  56. mbedtls_pk_init( &key );
  57. mbedtls_ctr_drbg_init( &ctr_drbg );
  58. // default
  59. opt.filename = DFL_FILENAME;
  60. opt.debug_level = DFL_DEBUG_LEVEL;
  61. opt.output_file = DFL_OUTPUT_FILENAME;
  62. opt.subject_name = DFL_SUBJECT_NAME;
  63. opt.key_usage = DFL_KEY_USAGE;
  64. opt.ns_cert_type = DFL_NS_CERT_TYPE;
  65. // user
  66. memset(subject_name, 0, 40);
  67. strcpy(subject_name, "CN=");
  68. strcat(subject_name, sSettings.sWebParams.ip);
  69. strcat(subject_name, ",O=VimpelCom,C=RU");
  70. opt.subject_name = subject_name;
  71. opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  72. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
  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_key( &key, (const unsigned char *) mbedtls_test_srv_key, mbedtls_test_srv_key_len, NULL, 0 );
  102. if( ret != 0 )
  103. {
  104. mbedtls_printf( " failed\r\n ! mbedtls_pk_parse_keyfile returned %d", ret );
  105. goto exit;
  106. }
  107. mbedtls_x509write_csr_set_key( &req, &key );
  108. mbedtls_printf( " ok\r\n" );
  109. // 1.2. Writing the request
  110. mbedtls_printf( " . Writing the certificate request ..." );
  111. ret = mbedtls_x509write_csr_pem( &req, req_cert, 4096, mbedtls_ctr_drbg_random, &ctr_drbg );
  112. if (ret != 0)
  113. {
  114. mbedtls_printf( " failed\r\n ! write_certifcate_request %d", ret );
  115. goto exit;
  116. }
  117. mbedtls_printf( " ok\r\n" );
  118. mbedtls_printf(req_cert);
  119. mbedtls_printf("\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. /********************************* (C) РОТЕК **********************************/