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