cert_req.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Certificate request generation
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. ******************************************************************************
  20. * Modifications copyright (C) 2023, Rockwell Automation, Inc.
  21. * All rights reserved.
  22. ******************************************************************************/
  23. /** @file
  24. * @brief Certificate signing request (CSR) generation
  25. *
  26. * ********************************************************************
  27. * include files
  28. */
  29. #include "mbedtls/build_info.h"
  30. #include "mbedtls/platform.h"
  31. /* md.h is included this early since MD_CAN_XXX macros are defined there. */
  32. #include "mbedtls/md.h"
  33. #include "mbedtls/ctr_drbg.h"
  34. #include "mbedtls/entropy.h"
  35. #include "mbedtls/error.h"
  36. #include "mbedtls/x509_csr.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "cert_req.h"
  41. #include "ciptypes.h"
  42. #include "trace.h"
  43. #define DFL_FILENAME RSA_KEY_FILE_LOCATION // config in CMakeLists.txt
  44. #define DFL_PASSWORD NULL
  45. #define DFL_DEBUG_LEVEL 0
  46. #define DFL_OUTPUT_FILENAME FILE_OBJECT_CSR_FILE_LOCATION // config in CMakeLists.txt
  47. #define SUBJECT_NAME_TEMPLATE "CN=%s,O=%s,OU=%s,L=%s,ST=%s,C=%s,R=%s"
  48. #define DFL_KEY_USAGE 0
  49. #define DFL_FORCE_KEY_USAGE 0
  50. #define DFL_NS_CERT_TYPE 0
  51. #define DFL_FORCE_NS_CERT_TYPE 0
  52. #define DFL_MD_ALG MBEDTLS_MD_SHA256
  53. /*
  54. * global options
  55. */
  56. struct options {
  57. const char *filename; /* filename of the key file */
  58. const char *password; /* password for the key file */
  59. int debug_level; /* level of debugging */
  60. const char *output_file; /* where to store the constructed key file */
  61. const char *subject_name; /* subject name for certificate request */
  62. mbedtls_x509_san_list *san_list; /* subjectAltName for certificate request */
  63. unsigned char key_usage; /* key usage flags */
  64. int force_key_usage; /* Force adding the KeyUsage extension */
  65. unsigned char ns_cert_type; /* NS cert type */
  66. int force_ns_cert_type; /* Force adding NsCertType extension */
  67. mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
  68. } options;
  69. /** @brief Write certificate request
  70. *
  71. * @param req structure containing CSR Parameters
  72. * @param output_file where to store the constructed key file
  73. * @param f_rng random number generator function
  74. * @param p_rng random number generator param
  75. * @return status
  76. */
  77. int write_certificate_request(mbedtls_x509write_csr *req,
  78. const char *output_file,
  79. int (*f_rng)(void *, unsigned char *, size_t),
  80. void *p_rng) {
  81. int ret; //return value
  82. unsigned char output_buf[4096];
  83. memset(output_buf, 0, 4096);
  84. if ( ( ret =
  85. mbedtls_x509write_csr_pem(req, output_buf, 4096, f_rng, p_rng) ) <
  86. 0 ) {
  87. return ret;
  88. }
  89. size_t len = strlen( (char *)output_buf );
  90. FILE *file;
  91. if ( ( file = fopen(output_file, "w") ) == NULL ) {
  92. return -1;
  93. }
  94. if (fwrite(output_buf, 1, len, file) != len) {
  95. fclose(file);
  96. return -1;
  97. }
  98. fclose(file);
  99. return 0;
  100. }
  101. /* function called in OpENer certificatemanagement */
  102. int MbedtlsWriteCSR(CipShortString *short_strings) {
  103. int ret = 1;
  104. int exit_code = MBEDTLS_EXIT_FAILURE;
  105. mbedtls_pk_context key;
  106. char buf[1024];
  107. mbedtls_x509write_csr req;
  108. mbedtls_entropy_context entropy;
  109. mbedtls_ctr_drbg_context ctr_drbg; //ctr_drbg - Counter Mode Deterministic Random Bit Generator
  110. mbedtls_x509_san_list *cur, *prev; //Subject Alternative Name List
  111. /*
  112. * Set to sane values
  113. */
  114. mbedtls_x509write_csr_init(&req);
  115. mbedtls_pk_init(&key);
  116. mbedtls_ctr_drbg_init(&ctr_drbg);
  117. memset( buf, 0, sizeof(buf) );
  118. mbedtls_entropy_init(&entropy);
  119. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  120. psa_status_t status = psa_crypto_init();
  121. if (status != PSA_SUCCESS) {
  122. mbedtls_fprintf(stderr,
  123. "Failed to initialize PSA Crypto implementation: %d\n",
  124. (int)status);
  125. goto exit;
  126. }
  127. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  128. /* create subjectName string from input params */
  129. char subjectName[500];
  130. snprintf(subjectName,
  131. sizeof(subjectName),
  132. SUBJECT_NAME_TEMPLATE,
  133. short_strings[0].string,
  134. short_strings[1].string,
  135. short_strings[2].string,
  136. short_strings[3].string,
  137. short_strings[4].string,
  138. short_strings[5].string,
  139. short_strings[6].string);
  140. options.filename = DFL_FILENAME;
  141. options.password = DFL_PASSWORD;
  142. options.debug_level = DFL_DEBUG_LEVEL;
  143. options.output_file = DFL_OUTPUT_FILENAME;
  144. options.subject_name = subjectName;
  145. options.key_usage = DFL_KEY_USAGE;
  146. options.force_key_usage = DFL_FORCE_KEY_USAGE;
  147. options.ns_cert_type = DFL_NS_CERT_TYPE;
  148. options.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
  149. options.md_alg = DFL_MD_ALG;
  150. options.san_list = NULL;
  151. /* Set the MD algorithm to use for the signature in the CSR */
  152. mbedtls_x509write_csr_set_md_alg(&req, options.md_alg);
  153. /* Set the Key Usage Extension flags in the CSR */
  154. if (options.key_usage || options.force_key_usage == 1) {
  155. ret = mbedtls_x509write_csr_set_key_usage(&req, options.key_usage);
  156. if (ret != 0) {
  157. OPENER_TRACE_INFO(
  158. " failed\n ! mbedtls_x509write_csr_set_key_usage returned %d", ret);
  159. goto exit;
  160. }
  161. }
  162. /* Set the Cert Type flags in the CSR */
  163. if (options.ns_cert_type || options.force_ns_cert_type == 1) {
  164. ret = mbedtls_x509write_csr_set_ns_cert_type(&req, options.ns_cert_type);
  165. if (ret != 0) {
  166. OPENER_TRACE_INFO(
  167. " failed\n ! mbedtls_x509write_csr_set_ns_cert_type returned %d",
  168. ret);
  169. goto exit;
  170. }
  171. }
  172. /*
  173. * 0. Seed the PRNG (Pseudorandom number generator)
  174. */
  175. OPENER_TRACE_INFO(" . Seeding the random number generator...");
  176. fflush(stdout);
  177. if ( ( ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  178. NULL,
  179. 0) ) != 0 ) {
  180. OPENER_TRACE_INFO(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
  181. goto exit;
  182. }
  183. OPENER_TRACE_INFO(" ok\n");
  184. /*
  185. * 1.0. Check the subject name for validity
  186. */
  187. OPENER_TRACE_INFO(" . Checking subject name...");
  188. fflush(stdout);
  189. if ( ( ret =
  190. mbedtls_x509write_csr_set_subject_name(&req,
  191. options.subject_name) ) !=
  192. 0 ) {
  193. OPENER_TRACE_INFO(
  194. " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d",
  195. ret);
  196. goto exit;
  197. }
  198. OPENER_TRACE_INFO(" ok\n");
  199. /*
  200. * 1.1. Load the key
  201. */
  202. OPENER_TRACE_INFO(" . Loading the private key ...");
  203. fflush(stdout);
  204. ret = mbedtls_pk_parse_keyfile(&key, options.filename, options.password,
  205. mbedtls_ctr_drbg_random, &ctr_drbg);
  206. if (ret != 0) {
  207. OPENER_TRACE_INFO(" failed\n ! mbedtls_pk_parse_keyfile returned %d",
  208. ret);
  209. goto exit;
  210. }
  211. mbedtls_x509write_csr_set_key(&req, &key);
  212. OPENER_TRACE_INFO(" ok\n");
  213. /*
  214. * 1.2. Writing the request
  215. */
  216. OPENER_TRACE_INFO(" . Writing the certificate request ...");
  217. fflush(stdout);
  218. if ( ( ret = write_certificate_request(
  219. &req, options.output_file, mbedtls_ctr_drbg_random,
  220. &ctr_drbg) ) != 0 ) {
  221. OPENER_TRACE_INFO(" failed\n ! write_certificate_request %d", ret);
  222. goto exit;
  223. }
  224. OPENER_TRACE_INFO(" ok\n\n");
  225. exit_code = MBEDTLS_EXIT_SUCCESS;
  226. exit:
  227. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  228. #ifdef MBEDTLS_ERROR_C
  229. mbedtls_strerror( ret, buf, sizeof(buf) );
  230. OPENER_TRACE_INFO(" - %s\n", buf);
  231. #else
  232. OPENER_TRACE_INFO("\n");
  233. #endif
  234. }
  235. mbedtls_x509write_csr_free(&req);
  236. mbedtls_pk_free(&key);
  237. mbedtls_ctr_drbg_free(&ctr_drbg);
  238. mbedtls_entropy_free(&entropy);
  239. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  240. mbedtls_psa_crypto_free();
  241. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  242. cur = options.san_list;
  243. while (cur != NULL) {
  244. prev = cur;
  245. cur = cur->next;
  246. mbedtls_free(prev);
  247. }
  248. return exit_code;
  249. }