x509_csr.h 10 KB

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