![]() |
|
|||
File indexing completed on 2025-08-27 09:37:35
0001 /** 0002 * \file x509_csr.h 0003 * 0004 * \brief X.509 certificate signing request parsing and writing 0005 */ 0006 /* 0007 * Copyright The Mbed TLS Contributors 0008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0009 */ 0010 #ifndef MBEDTLS_X509_CSR_H 0011 #define MBEDTLS_X509_CSR_H 0012 #include "mbedtls/private_access.h" 0013 0014 #include "mbedtls/build_info.h" 0015 0016 #include "mbedtls/x509.h" 0017 0018 #ifdef __cplusplus 0019 extern "C" { 0020 #endif 0021 0022 /** 0023 * \addtogroup x509_module 0024 * \{ */ 0025 0026 /** 0027 * \name Structures and functions for X.509 Certificate Signing Requests (CSR) 0028 * \{ 0029 */ 0030 0031 /** 0032 * Certificate Signing Request (CSR) structure. 0033 * 0034 * Some fields of this structure are publicly readable. Do not modify 0035 * them except via Mbed TLS library functions: the effect of modifying 0036 * those fields or the data that those fields point to is unspecified. 0037 */ 0038 typedef struct mbedtls_x509_csr { 0039 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */ 0040 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */ 0041 0042 int version; /**< CSR version (1=v1). */ 0043 0044 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */ 0045 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */ 0046 0047 mbedtls_pk_context pk; /**< Container for the public key context. */ 0048 0049 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */ 0050 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */ 0051 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. */ 0052 0053 int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */ 0054 0055 mbedtls_x509_buf sig_oid; 0056 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); 0057 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */ 0058 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */ 0059 void *MBEDTLS_PRIVATE(sig_opts); /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */ 0060 } 0061 mbedtls_x509_csr; 0062 0063 /** 0064 * Container for writing a CSR 0065 */ 0066 typedef struct mbedtls_x509write_csr { 0067 mbedtls_pk_context *MBEDTLS_PRIVATE(key); 0068 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject); 0069 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg); 0070 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions); 0071 } 0072 mbedtls_x509write_csr; 0073 0074 #if defined(MBEDTLS_X509_CSR_PARSE_C) 0075 /** 0076 * \brief Load a Certificate Signing Request (CSR) in DER format 0077 * 0078 * \note Any unsupported requested extensions are silently 0079 * ignored, unless the critical flag is set, in which case 0080 * the CSR is rejected. 0081 * 0082 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto 0083 * subsystem must have been initialized by calling 0084 * psa_crypto_init() before calling this function. 0085 * 0086 * \param csr CSR context to fill 0087 * \param buf buffer holding the CRL data 0088 * \param buflen size of the buffer 0089 * 0090 * \return 0 if successful, or a specific X509 error code 0091 */ 0092 int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr, 0093 const unsigned char *buf, size_t buflen); 0094 0095 /** 0096 * \brief The type of certificate extension callbacks. 0097 * 0098 * Callbacks of this type are passed to and used by the 0099 * mbedtls_x509_csr_parse_der_with_ext_cb() routine when 0100 * it encounters either an unsupported extension. 0101 * Future versions of the library may invoke the callback 0102 * in other cases, if and when the need arises. 0103 * 0104 * \param p_ctx An opaque context passed to the callback. 0105 * \param csr The CSR being parsed. 0106 * \param oid The OID of the extension. 0107 * \param critical Whether the extension is critical. 0108 * \param p Pointer to the start of the extension value 0109 * (the content of the OCTET STRING). 0110 * \param end End of extension value. 0111 * 0112 * \note The callback must fail and return a negative error code 0113 * if it can not parse or does not support the extension. 0114 * When the callback fails to parse a critical extension 0115 * mbedtls_x509_csr_parse_der_with_ext_cb() also fails. 0116 * When the callback fails to parse a non critical extension 0117 * mbedtls_x509_csr_parse_der_with_ext_cb() simply skips 0118 * the extension and continues parsing. 0119 * 0120 * \return \c 0 on success. 0121 * \return A negative error code on failure. 0122 */ 0123 typedef int (*mbedtls_x509_csr_ext_cb_t)(void *p_ctx, 0124 mbedtls_x509_csr const *csr, 0125 mbedtls_x509_buf const *oid, 0126 int critical, 0127 const unsigned char *p, 0128 const unsigned char *end); 0129 0130 /** 0131 * \brief Load a Certificate Signing Request (CSR) in DER format 0132 * 0133 * \note Any unsupported requested extensions are silently 0134 * ignored, unless the critical flag is set, in which case 0135 * the result of the callback function decides whether 0136 * CSR is rejected. 0137 * 0138 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto 0139 * subsystem must have been initialized by calling 0140 * psa_crypto_init() before calling this function. 0141 * 0142 * \param csr CSR context to fill 0143 * \param buf buffer holding the CRL data 0144 * \param buflen size of the buffer 0145 * \param cb A callback invoked for every unsupported certificate 0146 * extension. 0147 * \param p_ctx An opaque context passed to the callback. 0148 * 0149 * \return 0 if successful, or a specific X509 error code 0150 */ 0151 int mbedtls_x509_csr_parse_der_with_ext_cb(mbedtls_x509_csr *csr, 0152 const unsigned char *buf, size_t buflen, 0153 mbedtls_x509_csr_ext_cb_t cb, 0154 void *p_ctx); 0155 0156 /** 0157 * \brief Load a Certificate Signing Request (CSR), DER or PEM format 0158 * 0159 * \note See notes for \c mbedtls_x509_csr_parse_der() 0160 * 0161 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto 0162 * subsystem must have been initialized by calling 0163 * psa_crypto_init() before calling this function. 0164 * 0165 * \param csr CSR context to fill 0166 * \param buf buffer holding the CRL data 0167 * \param buflen size of the buffer 0168 * (including the terminating null byte for PEM data) 0169 * 0170 * \return 0 if successful, or a specific X509 or PEM error code 0171 */ 0172 int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen); 0173 0174 #if defined(MBEDTLS_FS_IO) 0175 /** 0176 * \brief Load a Certificate Signing Request (CSR) 0177 * 0178 * \note See notes for \c mbedtls_x509_csr_parse() 0179 * 0180 * \param csr CSR context to fill 0181 * \param path filename to read the CSR from 0182 * 0183 * \return 0 if successful, or a specific X509 or PEM error code 0184 */ 0185 int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path); 0186 #endif /* MBEDTLS_FS_IO */ 0187 0188 #if !defined(MBEDTLS_X509_REMOVE_INFO) 0189 /** 0190 * \brief Returns an informational string about the 0191 * CSR. 0192 * 0193 * \param buf Buffer to write to 0194 * \param size Maximum size of buffer 0195 * \param prefix A line prefix 0196 * \param csr The X509 CSR to represent 0197 * 0198 * \return The length of the string written (not including the 0199 * terminated nul byte), or a negative error code. 0200 */ 0201 int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix, 0202 const mbedtls_x509_csr *csr); 0203 #endif /* !MBEDTLS_X509_REMOVE_INFO */ 0204 0205 /** 0206 * \brief Initialize a CSR 0207 * 0208 * \param csr CSR to initialize 0209 */ 0210 void mbedtls_x509_csr_init(mbedtls_x509_csr *csr); 0211 0212 /** 0213 * \brief Unallocate all CSR data 0214 * 0215 * \param csr CSR to free 0216 */ 0217 void mbedtls_x509_csr_free(mbedtls_x509_csr *csr); 0218 #endif /* MBEDTLS_X509_CSR_PARSE_C */ 0219 0220 /** \} name Structures and functions for X.509 Certificate Signing Requests (CSR) */ 0221 0222 #if defined(MBEDTLS_X509_CSR_WRITE_C) 0223 /** 0224 * \brief Initialize a CSR context 0225 * 0226 * \param ctx CSR context to initialize 0227 */ 0228 void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx); 0229 0230 /** 0231 * \brief Set the subject name for a CSR 0232 * Subject names should contain a comma-separated list 0233 * of OID types and values: 0234 * e.g. "C=UK,O=ARM,CN=Mbed TLS Server 1" 0235 * 0236 * \param ctx CSR context to use 0237 * \param subject_name subject name to set 0238 * 0239 * \return 0 if subject name was parsed successfully, or 0240 * a specific error code 0241 */ 0242 int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx, 0243 const char *subject_name); 0244 0245 /** 0246 * \brief Set the key for a CSR (public key will be included, 0247 * private key used to sign the CSR when writing it) 0248 * 0249 * \param ctx CSR context to use 0250 * \param key Asymmetric key to include 0251 */ 0252 void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key); 0253 0254 /** 0255 * \brief Set the MD algorithm to use for the signature 0256 * (e.g. MBEDTLS_MD_SHA1) 0257 * 0258 * \param ctx CSR context to use 0259 * \param md_alg MD algorithm to use 0260 */ 0261 void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg); 0262 0263 /** 0264 * \brief Set the Key Usage Extension flags 0265 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN) 0266 * 0267 * \param ctx CSR context to use 0268 * \param key_usage key usage flags to set 0269 * 0270 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED 0271 * 0272 * \note The <code>decipherOnly</code> flag from the Key Usage 0273 * extension is represented by bit 8 (i.e. 0274 * <code>0x8000</code>), which cannot typically be represented 0275 * in an unsigned char. Therefore, the flag 0276 * <code>decipherOnly</code> (i.e. 0277 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this 0278 * function. 0279 */ 0280 int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage); 0281 0282 /** 0283 * \brief Set Subject Alternative Name 0284 * 0285 * \param ctx CSR context to use 0286 * \param san_list List of SAN values 0287 * 0288 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED 0289 * 0290 * \note Only "dnsName", "uniformResourceIdentifier" and "otherName", 0291 * as defined in RFC 5280, are supported. 0292 */ 0293 int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx, 0294 const mbedtls_x509_san_list *san_list); 0295 0296 /** 0297 * \brief Set the Netscape Cert Type flags 0298 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL) 0299 * 0300 * \param ctx CSR context to use 0301 * \param ns_cert_type Netscape Cert Type flags to set 0302 * 0303 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED 0304 */ 0305 int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx, 0306 unsigned char ns_cert_type); 0307 0308 /** 0309 * \brief Generic function to add to or replace an extension in the 0310 * CSR 0311 * 0312 * \param ctx CSR context to use 0313 * \param oid OID of the extension 0314 * \param oid_len length of the OID 0315 * \param critical Set to 1 to mark the extension as critical, 0 otherwise. 0316 * \param val value of the extension OCTET STRING 0317 * \param val_len length of the value data 0318 * 0319 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED 0320 */ 0321 int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx, 0322 const char *oid, size_t oid_len, 0323 int critical, 0324 const unsigned char *val, size_t val_len); 0325 0326 /** 0327 * \brief Free the contents of a CSR context 0328 * 0329 * \param ctx CSR context to free 0330 */ 0331 void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx); 0332 0333 /** 0334 * \brief Write a CSR (Certificate Signing Request) to a 0335 * DER structure 0336 * Note: data is written at the end of the buffer! Use the 0337 * return value to determine where you should start 0338 * using the buffer 0339 * 0340 * \param ctx CSR to write away 0341 * \param buf buffer to write to 0342 * \param size size of the buffer 0343 * \param f_rng RNG function. This must not be \c NULL. 0344 * \param p_rng RNG parameter 0345 * 0346 * \return length of data written if successful, or a specific 0347 * error code 0348 * 0349 * \note \p f_rng is used for the signature operation. 0350 */ 0351 int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, 0352 int (*f_rng)(void *, unsigned char *, size_t), 0353 void *p_rng); 0354 0355 #if defined(MBEDTLS_PEM_WRITE_C) 0356 /** 0357 * \brief Write a CSR (Certificate Signing Request) to a 0358 * PEM string 0359 * 0360 * \param ctx CSR to write away 0361 * \param buf buffer to write to 0362 * \param size size of the buffer 0363 * \param f_rng RNG function. This must not be \c NULL. 0364 * \param p_rng RNG parameter 0365 * 0366 * \return 0 if successful, or a specific error code 0367 * 0368 * \note \p f_rng is used for the signature operation. 0369 */ 0370 int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, 0371 int (*f_rng)(void *, unsigned char *, size_t), 0372 void *p_rng); 0373 #endif /* MBEDTLS_PEM_WRITE_C */ 0374 #endif /* MBEDTLS_X509_CSR_WRITE_C */ 0375 0376 /** \} addtogroup x509_module */ 0377 0378 #ifdef __cplusplus 0379 } 0380 #endif 0381 0382 #endif /* mbedtls_x509_csr.h */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |