x509_csr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef MBEDTLS_X509_CSR_H
  23. #define MBEDTLS_X509_CSR_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #include "mbedtls/x509.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * \addtogroup x509_module
  35. * \{ */
  36. /**
  37. * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
  38. * \{
  39. */
  40. /**
  41. * Certificate Signing Request (CSR) structure.
  42. */
  43. typedef struct mbedtls_x509_csr
  44. {
  45. mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
  46. mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
  47. int version; /**< CSR version (1=v1). */
  48. mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
  49. mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
  50. mbedtls_pk_context pk; /**< Container for the public key context. */
  51. mbedtls_x509_buf sig_oid;
  52. mbedtls_x509_buf sig;
  53. mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  54. mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  55. void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  56. }
  57. mbedtls_x509_csr;
  58. /**
  59. * Container for writing a CSR
  60. */
  61. typedef struct mbedtls_x509write_csr
  62. {
  63. mbedtls_pk_context *key;
  64. mbedtls_asn1_named_data *subject;
  65. mbedtls_md_type_t md_alg;
  66. mbedtls_asn1_named_data *extensions;
  67. }
  68. mbedtls_x509write_csr;
  69. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  70. /**
  71. * \brief Load a Certificate Signing Request (CSR) in DER format
  72. *
  73. * \note CSR attributes (if any) are currently silently ignored.
  74. *
  75. * \param csr CSR context to fill
  76. * \param buf buffer holding the CRL data
  77. * \param buflen size of the buffer
  78. *
  79. * \return 0 if successful, or a specific X509 error code
  80. */
  81. int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
  82. const unsigned char *buf, size_t buflen );
  83. /**
  84. * \brief Load a Certificate Signing Request (CSR), DER or PEM format
  85. *
  86. * \note See notes for \c mbedtls_x509_csr_parse_der()
  87. *
  88. * \param csr CSR context to fill
  89. * \param buf buffer holding the CRL data
  90. * \param buflen size of the buffer
  91. * (including the terminating null byte for PEM data)
  92. *
  93. * \return 0 if successful, or a specific X509 or PEM error code
  94. */
  95. int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
  96. #if defined(MBEDTLS_FS_IO)
  97. /**
  98. * \brief Load a Certificate Signing Request (CSR)
  99. *
  100. * \note See notes for \c mbedtls_x509_csr_parse()
  101. *
  102. * \param csr CSR context to fill
  103. * \param path filename to read the CSR from
  104. *
  105. * \return 0 if successful, or a specific X509 or PEM error code
  106. */
  107. int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
  108. #endif /* MBEDTLS_FS_IO */
  109. /**
  110. * \brief Returns an informational string about the
  111. * CSR.
  112. *
  113. * \param buf Buffer to write to
  114. * \param size Maximum size of buffer
  115. * \param prefix A line prefix
  116. * \param csr The X509 CSR to represent
  117. *
  118. * \return The length of the string written (not including the
  119. * terminated nul byte), or a negative error code.
  120. */
  121. int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
  122. const mbedtls_x509_csr *csr );
  123. /**
  124. * \brief Initialize a CSR
  125. *
  126. * \param csr CSR to initialize
  127. */
  128. void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
  129. /**
  130. * \brief Unallocate all CSR data
  131. *
  132. * \param csr CSR to free
  133. */
  134. void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
  135. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  136. /* \} name */
  137. /* \} addtogroup x509_module */
  138. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  139. /**
  140. * \brief Initialize a CSR context
  141. *
  142. * \param ctx CSR context to initialize
  143. */
  144. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
  145. /**
  146. * \brief Set the subject name for a CSR
  147. * Subject names should contain a comma-separated list
  148. * of OID types and values:
  149. * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  150. *
  151. * \param ctx CSR context to use
  152. * \param subject_name subject name to set
  153. *
  154. * \return 0 if subject name was parsed successfully, or
  155. * a specific error code
  156. */
  157. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  158. const char *subject_name );
  159. /**
  160. * \brief Set the key for a CSR (public key will be included,
  161. * private key used to sign the CSR when writing it)
  162. *
  163. * \param ctx CSR context to use
  164. * \param key Asymetric key to include
  165. */
  166. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
  167. /**
  168. * \brief Set the MD algorithm to use for the signature
  169. * (e.g. MBEDTLS_MD_SHA1)
  170. *
  171. * \param ctx CSR context to use
  172. * \param md_alg MD algorithm to use
  173. */
  174. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
  175. /**
  176. * \brief Set the Key Usage Extension flags
  177. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  178. *
  179. * \param ctx CSR context to use
  180. * \param key_usage key usage flags to set
  181. *
  182. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  183. *
  184. * \note The <code>decipherOnly</code> flag from the Key Usage
  185. * extension is represented by bit 8 (i.e.
  186. * <code>0x8000</code>), which cannot typically be represented
  187. * in an unsigned char. Therefore, the flag
  188. * <code>decipherOnly</code> (i.e.
  189. * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
  190. * function.
  191. */
  192. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
  193. /**
  194. * \brief Set the Netscape Cert Type flags
  195. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  196. *
  197. * \param ctx CSR context to use
  198. * \param ns_cert_type Netscape Cert Type flags to set
  199. *
  200. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  201. */
  202. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  203. unsigned char ns_cert_type );
  204. /**
  205. * \brief Generic function to add to or replace an extension in the
  206. * CSR
  207. *
  208. * \param ctx CSR context to use
  209. * \param oid OID of the extension
  210. * \param oid_len length of the OID
  211. * \param val value of the extension OCTET STRING
  212. * \param val_len length of the value data
  213. *
  214. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  215. */
  216. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  217. const char *oid, size_t oid_len,
  218. const unsigned char *val, size_t val_len );
  219. /**
  220. * \brief Free the contents of a CSR context
  221. *
  222. * \param ctx CSR context to free
  223. */
  224. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
  225. /**
  226. * \brief Write a CSR (Certificate Signing Request) to a
  227. * DER structure
  228. * Note: data is written at the end of the buffer! Use the
  229. * return value to determine where you should start
  230. * using the buffer
  231. *
  232. * \param ctx CSR to write away
  233. * \param buf buffer to write to
  234. * \param size size of the buffer
  235. * \param f_rng RNG function (for signature, see note)
  236. * \param p_rng RNG parameter
  237. *
  238. * \return length of data written if successful, or a specific
  239. * error code
  240. *
  241. * \note f_rng may be NULL if RSA is used for signature and the
  242. * signature is made offline (otherwise f_rng is desirable
  243. * for countermeasures against timing attacks).
  244. * ECDSA signatures always require a non-NULL f_rng.
  245. */
  246. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  247. int (*f_rng)(void *, unsigned char *, size_t),
  248. void *p_rng );
  249. #if defined(MBEDTLS_PEM_WRITE_C)
  250. /**
  251. * \brief Write a CSR (Certificate Signing Request) to a
  252. * PEM string
  253. *
  254. * \param ctx CSR to write away
  255. * \param buf buffer to write to
  256. * \param size size of the buffer
  257. * \param f_rng RNG function (for signature, see note)
  258. * \param p_rng RNG parameter
  259. *
  260. * \return 0 if successful, or a specific error code
  261. *
  262. * \note f_rng may be NULL if RSA is used for signature and the
  263. * signature is made offline (otherwise f_rng is desirable
  264. * for countermeasures against timing attacks).
  265. * ECDSA signatures always require a non-NULL f_rng.
  266. */
  267. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  268. int (*f_rng)(void *, unsigned char *, size_t),
  269. void *p_rng );
  270. #endif /* MBEDTLS_PEM_WRITE_C */
  271. #endif /* MBEDTLS_X509_CSR_WRITE_C */
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif /* mbedtls_x509_csr.h */