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