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