x509write_csr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * X.509 Certificate Signing Request writing
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * References:
  9. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  10. * - attributes: PKCS#9 v2.0 aka RFC 2985
  11. */
  12. #include "common.h"
  13. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  14. #include "x509_internal.h"
  15. #include "mbedtls/x509_csr.h"
  16. #include "mbedtls/asn1write.h"
  17. #include "mbedtls/error.h"
  18. #include "mbedtls/oid.h"
  19. #include "mbedtls/platform_util.h"
  20. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  21. #include "psa/crypto.h"
  22. #include "psa_util_internal.h"
  23. #include "mbedtls/psa_util.h"
  24. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #if defined(MBEDTLS_PEM_WRITE_C)
  28. #include "mbedtls/pem.h"
  29. #endif
  30. #include "mbedtls/platform.h"
  31. void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
  32. {
  33. memset(ctx, 0, sizeof(mbedtls_x509write_csr));
  34. }
  35. void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
  36. {
  37. if (ctx == NULL) {
  38. return;
  39. }
  40. mbedtls_asn1_free_named_data_list(&ctx->subject);
  41. mbedtls_asn1_free_named_data_list(&ctx->extensions);
  42. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
  43. }
  44. void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
  45. {
  46. ctx->md_alg = md_alg;
  47. }
  48. void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
  49. {
  50. ctx->key = key;
  51. }
  52. int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
  53. const char *subject_name)
  54. {
  55. return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
  56. }
  57. int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
  58. const char *oid, size_t oid_len,
  59. int critical,
  60. const unsigned char *val, size_t val_len)
  61. {
  62. return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
  63. critical, val, val_len);
  64. }
  65. int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
  66. const mbedtls_x509_san_list *san_list)
  67. {
  68. return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
  69. }
  70. int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
  71. {
  72. unsigned char buf[4] = { 0 };
  73. unsigned char *c;
  74. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  75. c = buf + 4;
  76. ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
  77. if (ret < 3 || ret > 4) {
  78. return ret;
  79. }
  80. ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
  81. MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
  82. 0, c, (size_t) ret);
  83. if (ret != 0) {
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
  89. unsigned char ns_cert_type)
  90. {
  91. unsigned char buf[4] = { 0 };
  92. unsigned char *c;
  93. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  94. c = buf + 4;
  95. ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
  96. if (ret < 3 || ret > 4) {
  97. return ret;
  98. }
  99. ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
  100. MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
  101. 0, c, (size_t) ret);
  102. if (ret != 0) {
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
  108. unsigned char *buf,
  109. size_t size,
  110. unsigned char *sig, size_t sig_size,
  111. int (*f_rng)(void *, unsigned char *, size_t),
  112. void *p_rng)
  113. {
  114. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  115. const char *sig_oid;
  116. size_t sig_oid_len = 0;
  117. unsigned char *c, *c2;
  118. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  119. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  120. size_t len = 0;
  121. mbedtls_pk_type_t pk_alg;
  122. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  123. size_t hash_len;
  124. psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
  125. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  126. /* Write the CSR backwards starting from the end of buf */
  127. c = buf + size;
  128. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
  129. ctx->extensions));
  130. if (len) {
  131. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  132. MBEDTLS_ASN1_CHK_ADD(len,
  133. mbedtls_asn1_write_tag(
  134. &c, buf,
  135. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  136. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  137. MBEDTLS_ASN1_CHK_ADD(len,
  138. mbedtls_asn1_write_tag(
  139. &c, buf,
  140. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
  141. MBEDTLS_ASN1_CHK_ADD(len,
  142. mbedtls_asn1_write_oid(
  143. &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  144. MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
  145. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  146. MBEDTLS_ASN1_CHK_ADD(len,
  147. mbedtls_asn1_write_tag(
  148. &c, buf,
  149. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  150. }
  151. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  152. MBEDTLS_ASN1_CHK_ADD(len,
  153. mbedtls_asn1_write_tag(
  154. &c, buf,
  155. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
  156. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
  157. buf, (size_t) (c - buf)));
  158. c -= pub_len;
  159. len += pub_len;
  160. /*
  161. * Subject ::= Name
  162. */
  163. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
  164. ctx->subject));
  165. /*
  166. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  167. */
  168. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
  169. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  170. MBEDTLS_ASN1_CHK_ADD(len,
  171. mbedtls_asn1_write_tag(
  172. &c, buf,
  173. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  174. /*
  175. * Sign the written CSR data into the sig buffer
  176. * Note: hash errors can happen only after an internal error
  177. */
  178. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  179. if (psa_hash_compute(hash_alg,
  180. c,
  181. len,
  182. hash,
  183. sizeof(hash),
  184. &hash_len) != PSA_SUCCESS) {
  185. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  186. }
  187. #else /* MBEDTLS_USE_PSA_CRYPTO */
  188. ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
  189. if (ret != 0) {
  190. return ret;
  191. }
  192. #endif
  193. if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
  194. sig, sig_size, &sig_len,
  195. f_rng, p_rng)) != 0) {
  196. return ret;
  197. }
  198. if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
  199. pk_alg = MBEDTLS_PK_RSA;
  200. } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
  201. pk_alg = MBEDTLS_PK_ECDSA;
  202. } else {
  203. return MBEDTLS_ERR_X509_INVALID_ALG;
  204. }
  205. if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
  206. &sig_oid, &sig_oid_len)) != 0) {
  207. return ret;
  208. }
  209. /*
  210. * Move the written CSR data to the start of buf to create space for
  211. * writing the signature into buf.
  212. */
  213. memmove(buf, c, len);
  214. /*
  215. * Write sig and its OID into buf backwards from the end of buf.
  216. * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
  217. * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
  218. */
  219. c2 = buf + size;
  220. MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
  221. mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
  222. sig, sig_len, pk_alg));
  223. /*
  224. * Compact the space between the CSR data and signature by moving the
  225. * CSR data to the start of the signature.
  226. */
  227. c2 -= len;
  228. memmove(c2, buf, len);
  229. /* ASN encode the total size and tag the CSR data with it. */
  230. len += sig_and_oid_len;
  231. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
  232. MBEDTLS_ASN1_CHK_ADD(len,
  233. mbedtls_asn1_write_tag(
  234. &c2, buf,
  235. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  236. /* Zero the unused bytes at the start of buf */
  237. memset(buf, 0, (size_t) (c2 - buf));
  238. return (int) len;
  239. }
  240. int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
  241. size_t size,
  242. int (*f_rng)(void *, unsigned char *, size_t),
  243. void *p_rng)
  244. {
  245. int ret;
  246. unsigned char *sig;
  247. if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
  248. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  249. }
  250. ret = x509write_csr_der_internal(ctx, buf, size,
  251. sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
  252. f_rng, p_rng);
  253. mbedtls_free(sig);
  254. return ret;
  255. }
  256. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  257. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  258. #if defined(MBEDTLS_PEM_WRITE_C)
  259. int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  260. int (*f_rng)(void *, unsigned char *, size_t),
  261. void *p_rng)
  262. {
  263. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  264. size_t olen = 0;
  265. if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
  266. f_rng, p_rng)) < 0) {
  267. return ret;
  268. }
  269. if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
  270. buf + size - ret,
  271. ret, buf, size, &olen)) != 0) {
  272. return ret;
  273. }
  274. return 0;
  275. }
  276. #endif /* MBEDTLS_PEM_WRITE_C */
  277. #endif /* MBEDTLS_X509_CSR_WRITE_C */