cert_req.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifdef PRINTF_STDLIB
  25. #include <stdio.h>
  26. #endif
  27. #ifdef PRINTF_CUSTOM
  28. #include "tinystdio.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #define DFL_FILENAME 0 //"keyfile.key"
  33. #define DFL_DEBUG_LEVEL 0
  34. #define DFL_OUTPUT_FILENAME 0 //"cert.req"
  35. #define DFL_SUBJECT_NAME 0 //"CN=Cert,O=mbed TLS,C=UK" // Надо CN - ip, O - VimpelCom, C=RU
  36. #define DFL_KEY_USAGE 0
  37. #define DFL_NS_CERT_TYPE 0
  38. extern SETTINGS_t sSettings;
  39. struct options
  40. {
  41. const char *filename; /* filename of the key file */
  42. int debug_level; /* level of debugging */
  43. const char *output_file; /* where to store the constructed key file */
  44. const char *subject_name; /* subject name for certificate request */
  45. unsigned char key_usage; /* key usage flags */
  46. unsigned char ns_cert_type; /* NS cert type */
  47. } opt;
  48. unsigned char req_cert[500];
  49. void SSL_CreateReqCert()
  50. {
  51. int ret = 0;
  52. mbedtls_pk_context key;
  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. char subject_name[40];
  58. // Set to sane values
  59. mbedtls_x509write_csr_init( &req );
  60. mbedtls_x509write_csr_set_md_alg( &req, MBEDTLS_MD_SHA256 );
  61. mbedtls_pk_init( &key );
  62. mbedtls_ctr_drbg_init( &ctr_drbg );
  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. memset(subject_name, 0, 40);
  72. strcpy(subject_name, "CN=");
  73. strcat(subject_name, sSettings.sWebParams.ip);
  74. strcat(subject_name, ",O=VimpelCom,C=RU");
  75. opt.subject_name = subject_name;
  76. //opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  77. //opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
  78. if( opt.key_usage )
  79. mbedtls_x509write_csr_set_key_usage( &req, opt.key_usage );
  80. if( opt.ns_cert_type )
  81. mbedtls_x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
  82. // 0. Seed the PRNG
  83. mbedtls_printf( " . Seeding the random number generator..." );
  84. // fflush( stdout );
  85. mbedtls_entropy_init( &entropy );
  86. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  87. (const unsigned char *) pers,
  88. strlen( pers ) ) ) != 0 )
  89. {
  90. mbedtls_printf( " failed\r\n ! mbedtls_ctr_drbg_seed returned %d", ret );
  91. goto exit;
  92. }
  93. mbedtls_printf( " ok\r\n" );
  94. // 1.0. Check the subject name for validity
  95. mbedtls_printf( " . Checking subject name..." );
  96. // fflush( stdout );
  97. if( ( ret = mbedtls_x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
  98. {
  99. mbedtls_printf( " failed\r\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret );
  100. goto exit;
  101. }
  102. mbedtls_printf( " ok\r\n" );
  103. // 1.1. Load the key
  104. mbedtls_printf( " . Loading the private key ..." );
  105. // fflush( stdout );
  106. ret = mbedtls_pk_parse_key( &key, (const unsigned char *) mbedtls_test_srv_key, mbedtls_test_srv_key_len, NULL, 0 );
  107. if( ret != 0 )
  108. {
  109. mbedtls_printf( " failed\r\n ! mbedtls_pk_parse_keyfile returned %d", ret );
  110. goto exit;
  111. }
  112. mbedtls_x509write_csr_set_key( &req, &key );
  113. mbedtls_printf( " ok\r\n" );
  114. // 1.2. Writing the request
  115. mbedtls_printf( " . Writing the certificate request ..." );
  116. ret = mbedtls_x509write_csr_pem( &req, req_cert, 4096, mbedtls_ctr_drbg_random, &ctr_drbg );
  117. if (ret != 0)
  118. {
  119. mbedtls_printf( " failed\r\n ! write_certifcate_request %d", ret );
  120. goto exit;
  121. }
  122. mbedtls_printf( " ok\r\n" );
  123. //mbedtls_printf(req_cert);
  124. mbedtls_printf("\r\n");
  125. exit:
  126. if( ret != 0 && ret != 1)
  127. {
  128. mbedtls_printf("\r\n");
  129. }
  130. mbedtls_x509write_csr_free( &req );
  131. mbedtls_pk_free( &key );
  132. mbedtls_ctr_drbg_free( &ctr_drbg );
  133. mbedtls_entropy_free( &entropy );
  134. }
  135. /********************************* (C) РОТЕК **********************************/