x509_csr.h 9.6 KB

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