x509write_csr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.h"
  27. #include "mbedtls/x509_csr.h"
  28. #include "mbedtls/asn1write.h"
  29. #include "mbedtls/error.h"
  30. #include "mbedtls/oid.h"
  31. #include "mbedtls/platform_util.h"
  32. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  33. #include "psa/crypto.h"
  34. #include "mbedtls/psa_util.h"
  35. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  36. #include "hash_info.h"
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #if defined(MBEDTLS_PEM_WRITE_C)
  40. #include "mbedtls/pem.h"
  41. #endif
  42. #include "mbedtls/platform.h"
  43. void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
  44. {
  45. memset(ctx, 0, sizeof(mbedtls_x509write_csr));
  46. }
  47. void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
  48. {
  49. mbedtls_asn1_free_named_data_list(&ctx->subject);
  50. mbedtls_asn1_free_named_data_list(&ctx->extensions);
  51. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
  52. }
  53. void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
  54. {
  55. ctx->md_alg = md_alg;
  56. }
  57. void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
  58. {
  59. ctx->key = key;
  60. }
  61. int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
  62. const char *subject_name)
  63. {
  64. return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
  65. }
  66. int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
  67. const char *oid, size_t oid_len,
  68. int critical,
  69. const unsigned char *val, size_t val_len)
  70. {
  71. return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
  72. critical, val, val_len);
  73. }
  74. int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
  75. const mbedtls_x509_san_list *san_list)
  76. {
  77. int ret = 0;
  78. const mbedtls_x509_san_list *cur;
  79. unsigned char *buf;
  80. unsigned char *p;
  81. size_t len;
  82. size_t buflen = 0;
  83. /* Determine the maximum size of the SubjectAltName list */
  84. for (cur = san_list; cur != NULL; cur = cur->next) {
  85. /* Calculate size of the required buffer */
  86. switch (cur->node.type) {
  87. case MBEDTLS_X509_SAN_DNS_NAME:
  88. case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
  89. case MBEDTLS_X509_SAN_IP_ADDRESS:
  90. /* length of value for each name entry,
  91. * maximum 4 bytes for the length field,
  92. * 1 byte for the tag/type.
  93. */
  94. buflen += cur->node.san.unstructured_name.len + 4 + 1;
  95. break;
  96. default:
  97. /* Not supported - skip. */
  98. break;
  99. }
  100. }
  101. /* Add the extra length field and tag */
  102. buflen += 4 + 1;
  103. /* Allocate buffer */
  104. buf = mbedtls_calloc(1, buflen);
  105. if (buf == NULL) {
  106. return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
  107. }
  108. mbedtls_platform_zeroize(buf, buflen);
  109. p = buf + buflen;
  110. /* Write ASN.1-based structure */
  111. cur = san_list;
  112. len = 0;
  113. while (cur != NULL) {
  114. switch (cur->node.type) {
  115. case MBEDTLS_X509_SAN_DNS_NAME:
  116. case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
  117. case MBEDTLS_X509_SAN_IP_ADDRESS:
  118. {
  119. const unsigned char *unstructured_name =
  120. (const unsigned char *) cur->node.san.unstructured_name.p;
  121. size_t unstructured_name_len = cur->node.san.unstructured_name.len;
  122. MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
  123. mbedtls_asn1_write_raw_buffer(
  124. &p, buf,
  125. unstructured_name, unstructured_name_len));
  126. MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(
  127. &p, buf, unstructured_name_len));
  128. MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
  129. mbedtls_asn1_write_tag(
  130. &p, buf,
  131. MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
  132. }
  133. break;
  134. default:
  135. /* Skip unsupported names. */
  136. break;
  137. }
  138. cur = cur->next;
  139. }
  140. MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
  141. MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
  142. mbedtls_asn1_write_tag(&p, buf,
  143. MBEDTLS_ASN1_CONSTRUCTED |
  144. MBEDTLS_ASN1_SEQUENCE));
  145. ret = mbedtls_x509write_csr_set_extension(
  146. ctx,
  147. MBEDTLS_OID_SUBJECT_ALT_NAME,
  148. MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
  149. 0,
  150. buf + buflen - len,
  151. len);
  152. /* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list
  153. * was incorrectly calculated and memory is corrupted. */
  154. if (p < buf) {
  155. ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  156. }
  157. cleanup:
  158. mbedtls_free(buf);
  159. return ret;
  160. }
  161. int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
  162. {
  163. unsigned char buf[4] = { 0 };
  164. unsigned char *c;
  165. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  166. c = buf + 4;
  167. ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
  168. if (ret < 3 || ret > 4) {
  169. return ret;
  170. }
  171. ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
  172. MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
  173. 0, c, (size_t) ret);
  174. if (ret != 0) {
  175. return ret;
  176. }
  177. return 0;
  178. }
  179. int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
  180. unsigned char ns_cert_type)
  181. {
  182. unsigned char buf[4] = { 0 };
  183. unsigned char *c;
  184. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  185. c = buf + 4;
  186. ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
  187. if (ret < 3 || ret > 4) {
  188. return ret;
  189. }
  190. ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
  191. MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
  192. 0, c, (size_t) ret);
  193. if (ret != 0) {
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
  199. unsigned char *buf,
  200. size_t size,
  201. unsigned char *sig, size_t sig_size,
  202. int (*f_rng)(void *, unsigned char *, size_t),
  203. void *p_rng)
  204. {
  205. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  206. const char *sig_oid;
  207. size_t sig_oid_len = 0;
  208. unsigned char *c, *c2;
  209. unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
  210. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  211. size_t len = 0;
  212. mbedtls_pk_type_t pk_alg;
  213. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  214. size_t hash_len;
  215. psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(ctx->md_alg);
  216. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  217. /* Write the CSR backwards starting from the end of buf */
  218. c = buf + size;
  219. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
  220. ctx->extensions));
  221. if (len) {
  222. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  223. MBEDTLS_ASN1_CHK_ADD(len,
  224. mbedtls_asn1_write_tag(
  225. &c, buf,
  226. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  227. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  228. MBEDTLS_ASN1_CHK_ADD(len,
  229. mbedtls_asn1_write_tag(
  230. &c, buf,
  231. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
  232. MBEDTLS_ASN1_CHK_ADD(len,
  233. mbedtls_asn1_write_oid(
  234. &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  235. MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
  236. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  237. MBEDTLS_ASN1_CHK_ADD(len,
  238. mbedtls_asn1_write_tag(
  239. &c, buf,
  240. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  241. }
  242. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  243. MBEDTLS_ASN1_CHK_ADD(len,
  244. mbedtls_asn1_write_tag(
  245. &c, buf,
  246. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
  247. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
  248. buf, c - buf));
  249. c -= pub_len;
  250. len += pub_len;
  251. /*
  252. * Subject ::= Name
  253. */
  254. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
  255. ctx->subject));
  256. /*
  257. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  258. */
  259. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
  260. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  261. MBEDTLS_ASN1_CHK_ADD(len,
  262. mbedtls_asn1_write_tag(
  263. &c, buf,
  264. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  265. /*
  266. * Sign the written CSR data into the sig buffer
  267. * Note: hash errors can happen only after an internal error
  268. */
  269. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  270. if (psa_hash_compute(hash_alg,
  271. c,
  272. len,
  273. hash,
  274. sizeof(hash),
  275. &hash_len) != PSA_SUCCESS) {
  276. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  277. }
  278. #else /* MBEDTLS_USE_PSA_CRYPTO */
  279. ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
  280. if (ret != 0) {
  281. return ret;
  282. }
  283. #endif
  284. if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
  285. sig, sig_size, &sig_len,
  286. f_rng, p_rng)) != 0) {
  287. return ret;
  288. }
  289. if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
  290. pk_alg = MBEDTLS_PK_RSA;
  291. } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
  292. pk_alg = MBEDTLS_PK_ECDSA;
  293. } else {
  294. return MBEDTLS_ERR_X509_INVALID_ALG;
  295. }
  296. if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
  297. &sig_oid, &sig_oid_len)) != 0) {
  298. return ret;
  299. }
  300. /*
  301. * Move the written CSR data to the start of buf to create space for
  302. * writing the signature into buf.
  303. */
  304. memmove(buf, c, len);
  305. /*
  306. * Write sig and its OID into buf backwards from the end of buf.
  307. * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
  308. * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
  309. */
  310. c2 = buf + size;
  311. MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
  312. mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
  313. sig, sig_len));
  314. /*
  315. * Compact the space between the CSR data and signature by moving the
  316. * CSR data to the start of the signature.
  317. */
  318. c2 -= len;
  319. memmove(c2, buf, len);
  320. /* ASN encode the total size and tag the CSR data with it. */
  321. len += sig_and_oid_len;
  322. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
  323. MBEDTLS_ASN1_CHK_ADD(len,
  324. mbedtls_asn1_write_tag(
  325. &c2, buf,
  326. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  327. /* Zero the unused bytes at the start of buf */
  328. memset(buf, 0, c2 - buf);
  329. return (int) len;
  330. }
  331. int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
  332. size_t size,
  333. int (*f_rng)(void *, unsigned char *, size_t),
  334. void *p_rng)
  335. {
  336. int ret;
  337. unsigned char *sig;
  338. if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
  339. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  340. }
  341. ret = x509write_csr_der_internal(ctx, buf, size,
  342. sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
  343. f_rng, p_rng);
  344. mbedtls_free(sig);
  345. return ret;
  346. }
  347. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  348. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  349. #if defined(MBEDTLS_PEM_WRITE_C)
  350. int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  351. int (*f_rng)(void *, unsigned char *, size_t),
  352. void *p_rng)
  353. {
  354. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  355. size_t olen = 0;
  356. if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
  357. f_rng, p_rng)) < 0) {
  358. return ret;
  359. }
  360. if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
  361. buf + size - ret,
  362. ret, buf, size, &olen)) != 0) {
  363. return ret;
  364. }
  365. return 0;
  366. }
  367. #endif /* MBEDTLS_PEM_WRITE_C */
  368. #endif /* MBEDTLS_X509_CSR_WRITE_C */