x509_csr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * \file x509_csr.h
  3. *
  4. * \brief X.509 certificate signing request parsing and writing
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_X509_CSR_H
  11. #define MBEDTLS_X509_CSR_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include "mbedtls/x509.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * \addtogroup x509_module
  20. * \{ */
  21. /**
  22. * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
  23. * \{
  24. */
  25. /**
  26. * Certificate Signing Request (CSR) structure.
  27. *
  28. * Some fields of this structure are publicly readable. Do not modify
  29. * them except via Mbed TLS library functions: the effect of modifying
  30. * those fields or the data that those fields point to is unspecified.
  31. */
  32. typedef struct mbedtls_x509_csr {
  33. mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
  34. mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
  35. int version; /**< CSR version (1=v1). */
  36. mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
  37. mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
  38. mbedtls_pk_context pk; /**< Container for the public key context. */
  39. unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
  40. unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
  41. mbedtls_x509_sequence subject_alt_names; /**< Optional list of raw entries of Subject Alternative Names extension. These can be later parsed by mbedtls_x509_parse_subject_alt_name. */
  42. int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */
  43. mbedtls_x509_buf sig_oid;
  44. mbedtls_x509_buf MBEDTLS_PRIVATE(sig);
  45. mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  46. mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  47. void *MBEDTLS_PRIVATE(sig_opts); /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  48. }
  49. mbedtls_x509_csr;
  50. /**
  51. * Container for writing a CSR
  52. */
  53. typedef struct mbedtls_x509write_csr {
  54. mbedtls_pk_context *MBEDTLS_PRIVATE(key);
  55. mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
  56. mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
  57. mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
  58. }
  59. mbedtls_x509write_csr;
  60. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  61. /**
  62. * \brief Load a Certificate Signing Request (CSR) in DER format
  63. *
  64. * \note Any unsupported requested extensions are silently
  65. * ignored, unless the critical flag is set, in which case
  66. * the CSR is rejected.
  67. *
  68. * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
  69. * subsystem must have been initialized by calling
  70. * psa_crypto_init() before calling this function.
  71. *
  72. * \param csr CSR context to fill
  73. * \param buf buffer holding the CRL data
  74. * \param buflen size of the buffer
  75. *
  76. * \return 0 if successful, or a specific X509 error code
  77. */
  78. int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
  79. const unsigned char *buf, size_t buflen);
  80. /**
  81. * \brief The type of certificate extension callbacks.
  82. *
  83. * Callbacks of this type are passed to and used by the
  84. * mbedtls_x509_csr_parse_der_with_ext_cb() routine when
  85. * it encounters either an unsupported extension.
  86. * Future versions of the library may invoke the callback
  87. * in other cases, if and when the need arises.
  88. *
  89. * \param p_ctx An opaque context passed to the callback.
  90. * \param csr The CSR being parsed.
  91. * \param oid The OID of the extension.
  92. * \param critical Whether the extension is critical.
  93. * \param p Pointer to the start of the extension value
  94. * (the content of the OCTET STRING).
  95. * \param end End of extension value.
  96. *
  97. * \note The callback must fail and return a negative error code
  98. * if it can not parse or does not support the extension.
  99. * When the callback fails to parse a critical extension
  100. * mbedtls_x509_csr_parse_der_with_ext_cb() also fails.
  101. * When the callback fails to parse a non critical extension
  102. * mbedtls_x509_csr_parse_der_with_ext_cb() simply skips
  103. * the extension and continues parsing.
  104. *
  105. * \return \c 0 on success.
  106. * \return A negative error code on failure.
  107. */
  108. typedef int (*mbedtls_x509_csr_ext_cb_t)(void *p_ctx,
  109. mbedtls_x509_csr const *csr,
  110. mbedtls_x509_buf const *oid,
  111. int critical,
  112. const unsigned char *p,
  113. const unsigned char *end);
  114. /**
  115. * \brief Load a Certificate Signing Request (CSR) in DER format
  116. *
  117. * \note Any unsupported requested extensions are silently
  118. * ignored, unless the critical flag is set, in which case
  119. * the result of the callback function decides whether
  120. * CSR is rejected.
  121. *
  122. * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
  123. * subsystem must have been initialized by calling
  124. * psa_crypto_init() before calling this function.
  125. *
  126. * \param csr CSR context to fill
  127. * \param buf buffer holding the CRL data
  128. * \param buflen size of the buffer
  129. * \param cb A callback invoked for every unsupported certificate
  130. * extension.
  131. * \param p_ctx An opaque context passed to the callback.
  132. *
  133. * \return 0 if successful, or a specific X509 error code
  134. */
  135. int mbedtls_x509_csr_parse_der_with_ext_cb(mbedtls_x509_csr *csr,
  136. const unsigned char *buf, size_t buflen,
  137. mbedtls_x509_csr_ext_cb_t cb,
  138. void *p_ctx);
  139. /**
  140. * \brief Load a Certificate Signing Request (CSR), DER or PEM format
  141. *
  142. * \note See notes for \c mbedtls_x509_csr_parse_der()
  143. *
  144. * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
  145. * subsystem must have been initialized by calling
  146. * psa_crypto_init() before calling this function.
  147. *
  148. * \param csr CSR context to fill
  149. * \param buf buffer holding the CRL data
  150. * \param buflen size of the buffer
  151. * (including the terminating null byte for PEM data)
  152. *
  153. * \return 0 if successful, or a specific X509 or PEM error code
  154. */
  155. int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen);
  156. #if defined(MBEDTLS_FS_IO)
  157. /**
  158. * \brief Load a Certificate Signing Request (CSR)
  159. *
  160. * \note See notes for \c mbedtls_x509_csr_parse()
  161. *
  162. * \param csr CSR context to fill
  163. * \param path filename to read the CSR from
  164. *
  165. * \return 0 if successful, or a specific X509 or PEM error code
  166. */
  167. int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path);
  168. #endif /* MBEDTLS_FS_IO */
  169. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  170. /**
  171. * \brief Returns an informational string about the
  172. * CSR.
  173. *
  174. * \param buf Buffer to write to
  175. * \param size Maximum size of buffer
  176. * \param prefix A line prefix
  177. * \param csr The X509 CSR to represent
  178. *
  179. * \return The length of the string written (not including the
  180. * terminated nul byte), or a negative error code.
  181. */
  182. int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
  183. const mbedtls_x509_csr *csr);
  184. #endif /* !MBEDTLS_X509_REMOVE_INFO */
  185. /**
  186. * \brief Initialize a CSR
  187. *
  188. * \param csr CSR to initialize
  189. */
  190. void mbedtls_x509_csr_init(mbedtls_x509_csr *csr);
  191. /**
  192. * \brief Unallocate all CSR data
  193. *
  194. * \param csr CSR to free
  195. */
  196. void mbedtls_x509_csr_free(mbedtls_x509_csr *csr);
  197. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  198. /** \} name Structures and functions for X.509 Certificate Signing Requests (CSR) */
  199. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  200. /**
  201. * \brief Initialize a CSR context
  202. *
  203. * \param ctx CSR context to initialize
  204. */
  205. void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx);
  206. /**
  207. * \brief Set the subject name for a CSR
  208. * Subject names should contain a comma-separated list
  209. * of OID types and values:
  210. * e.g. "C=UK,O=ARM,CN=Mbed TLS Server 1"
  211. *
  212. * \param ctx CSR context to use
  213. * \param subject_name subject name to set
  214. *
  215. * \return 0 if subject name was parsed successfully, or
  216. * a specific error code
  217. */
  218. int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
  219. const char *subject_name);
  220. /**
  221. * \brief Set the key for a CSR (public key will be included,
  222. * private key used to sign the CSR when writing it)
  223. *
  224. * \param ctx CSR context to use
  225. * \param key Asymmetric key to include
  226. */
  227. void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key);
  228. /**
  229. * \brief Set the MD algorithm to use for the signature
  230. * (e.g. MBEDTLS_MD_SHA1)
  231. *
  232. * \param ctx CSR context to use
  233. * \param md_alg MD algorithm to use
  234. */
  235. void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg);
  236. /**
  237. * \brief Set the Key Usage Extension flags
  238. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  239. *
  240. * \param ctx CSR context to use
  241. * \param key_usage key usage flags to set
  242. *
  243. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  244. *
  245. * \note The <code>decipherOnly</code> flag from the Key Usage
  246. * extension is represented by bit 8 (i.e.
  247. * <code>0x8000</code>), which cannot typically be represented
  248. * in an unsigned char. Therefore, the flag
  249. * <code>decipherOnly</code> (i.e.
  250. * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
  251. * function.
  252. */
  253. int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage);
  254. /**
  255. * \brief Set Subject Alternative Name
  256. *
  257. * \param ctx CSR context to use
  258. * \param san_list List of SAN values
  259. *
  260. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  261. *
  262. * \note Only "dnsName", "uniformResourceIdentifier" and "otherName",
  263. * as defined in RFC 5280, are supported.
  264. */
  265. int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
  266. const mbedtls_x509_san_list *san_list);
  267. /**
  268. * \brief Set the Netscape Cert Type flags
  269. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  270. *
  271. * \param ctx CSR context to use
  272. * \param ns_cert_type Netscape Cert Type flags to set
  273. *
  274. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  275. */
  276. int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
  277. unsigned char ns_cert_type);
  278. /**
  279. * \brief Generic function to add to or replace an extension in the
  280. * CSR
  281. *
  282. * \param ctx CSR context to use
  283. * \param oid OID of the extension
  284. * \param oid_len length of the OID
  285. * \param critical Set to 1 to mark the extension as critical, 0 otherwise.
  286. * \param val value of the extension OCTET STRING
  287. * \param val_len length of the value data
  288. *
  289. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  290. */
  291. int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
  292. const char *oid, size_t oid_len,
  293. int critical,
  294. const unsigned char *val, size_t val_len);
  295. /**
  296. * \brief Free the contents of a CSR context
  297. *
  298. * \param ctx CSR context to free
  299. */
  300. void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx);
  301. /**
  302. * \brief Write a CSR (Certificate Signing Request) to a
  303. * DER structure
  304. * Note: data is written at the end of the buffer! Use the
  305. * return value to determine where you should start
  306. * using the buffer
  307. *
  308. * \param ctx CSR to write away
  309. * \param buf buffer to write to
  310. * \param size size of the buffer
  311. * \param f_rng RNG function. This must not be \c NULL.
  312. * \param p_rng RNG parameter
  313. *
  314. * \return length of data written if successful, or a specific
  315. * error code
  316. *
  317. * \note \p f_rng is used for the signature operation.
  318. */
  319. int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  320. mbedtls_f_rng_t *f_rng,
  321. void *p_rng);
  322. #if defined(MBEDTLS_PEM_WRITE_C)
  323. /**
  324. * \brief Write a CSR (Certificate Signing Request) to a
  325. * PEM string
  326. *
  327. * \param ctx CSR to write away
  328. * \param buf buffer to write to
  329. * \param size size of the buffer
  330. * \param f_rng RNG function. This must not be \c NULL.
  331. * \param p_rng RNG parameter
  332. *
  333. * \return 0 if successful, or a specific error code
  334. *
  335. * \note \p f_rng is used for the signature operation.
  336. */
  337. int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  338. mbedtls_f_rng_t *f_rng,
  339. void *p_rng);
  340. #endif /* MBEDTLS_PEM_WRITE_C */
  341. #endif /* MBEDTLS_X509_CSR_WRITE_C */
  342. /** \} addtogroup x509_module */
  343. #ifdef __cplusplus
  344. }
  345. #endif
  346. #endif /* mbedtls_x509_csr.h */