4 * \brief X.509 certificate signing request parsing and writing
\r
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
\r
8 * SPDX-License-Identifier: Apache-2.0
\r
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
\r
11 * not use this file except in compliance with the License.
\r
12 * You may obtain a copy of the License at
\r
14 * http://www.apache.org/licenses/LICENSE-2.0
\r
16 * Unless required by applicable law or agreed to in writing, software
\r
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\r
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
19 * See the License for the specific language governing permissions and
\r
20 * limitations under the License.
\r
22 * This file is part of mbed TLS (https://tls.mbed.org)
\r
24 #ifndef MBEDTLS_X509_CSR_H
\r
25 #define MBEDTLS_X509_CSR_H
\r
27 #if !defined(MBEDTLS_CONFIG_FILE)
\r
30 #include MBEDTLS_CONFIG_FILE
\r
40 * \addtogroup x509_module
\r
44 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
\r
49 * Certificate Signing Request (CSR) structure.
\r
51 typedef struct mbedtls_x509_csr
\r
53 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
\r
54 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
\r
56 int version; /**< CSR version (1=v1). */
\r
58 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
\r
59 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
\r
61 mbedtls_pk_context pk; /**< Container for the public key context. */
\r
63 mbedtls_x509_buf sig_oid;
\r
64 mbedtls_x509_buf sig;
\r
65 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
\r
66 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
\r
67 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
\r
72 * Container for writing a CSR
\r
74 typedef struct mbedtls_x509write_csr
\r
76 mbedtls_pk_context *key;
\r
77 mbedtls_asn1_named_data *subject;
\r
78 mbedtls_md_type_t md_alg;
\r
79 mbedtls_asn1_named_data *extensions;
\r
81 mbedtls_x509write_csr;
\r
83 #if defined(MBEDTLS_X509_CSR_PARSE_C)
\r
85 * \brief Load a Certificate Signing Request (CSR) in DER format
\r
87 * \note CSR attributes (if any) are currently silently ignored.
\r
89 * \param csr CSR context to fill
\r
90 * \param buf buffer holding the CRL data
\r
91 * \param buflen size of the buffer
\r
93 * \return 0 if successful, or a specific X509 error code
\r
95 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
\r
96 const unsigned char *buf, size_t buflen );
\r
99 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
\r
101 * \note See notes for \c mbedtls_x509_csr_parse_der()
\r
103 * \param csr CSR context to fill
\r
104 * \param buf buffer holding the CRL data
\r
105 * \param buflen size of the buffer
\r
106 * (including the terminating null byte for PEM data)
\r
108 * \return 0 if successful, or a specific X509 or PEM error code
\r
110 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
\r
112 #if defined(MBEDTLS_FS_IO)
\r
114 * \brief Load a Certificate Signing Request (CSR)
\r
116 * \note See notes for \c mbedtls_x509_csr_parse()
\r
118 * \param csr CSR context to fill
\r
119 * \param path filename to read the CSR from
\r
121 * \return 0 if successful, or a specific X509 or PEM error code
\r
123 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
\r
124 #endif /* MBEDTLS_FS_IO */
\r
127 * \brief Returns an informational string about the
\r
130 * \param buf Buffer to write to
\r
131 * \param size Maximum size of buffer
\r
132 * \param prefix A line prefix
\r
133 * \param csr The X509 CSR to represent
\r
135 * \return The length of the string written (not including the
\r
136 * terminated nul byte), or a negative error code.
\r
138 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
\r
139 const mbedtls_x509_csr *csr );
\r
142 * \brief Initialize a CSR
\r
144 * \param csr CSR to initialize
\r
146 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
\r
149 * \brief Unallocate all CSR data
\r
151 * \param csr CSR to free
\r
153 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
\r
154 #endif /* MBEDTLS_X509_CSR_PARSE_C */
\r
157 /* \} addtogroup x509_module */
\r
159 #if defined(MBEDTLS_X509_CSR_WRITE_C)
\r
161 * \brief Initialize a CSR context
\r
163 * \param ctx CSR context to initialize
\r
165 void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
\r
168 * \brief Set the subject name for a CSR
\r
169 * Subject names should contain a comma-separated list
\r
170 * of OID types and values:
\r
171 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
\r
173 * \param ctx CSR context to use
\r
174 * \param subject_name subject name to set
\r
176 * \return 0 if subject name was parsed successfully, or
\r
177 * a specific error code
\r
179 int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
\r
180 const char *subject_name );
\r
183 * \brief Set the key for a CSR (public key will be included,
\r
184 * private key used to sign the CSR when writing it)
\r
186 * \param ctx CSR context to use
\r
187 * \param key Asymetric key to include
\r
189 void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
\r
192 * \brief Set the MD algorithm to use for the signature
\r
193 * (e.g. MBEDTLS_MD_SHA1)
\r
195 * \param ctx CSR context to use
\r
196 * \param md_alg MD algorithm to use
\r
198 void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
\r
201 * \brief Set the Key Usage Extension flags
\r
202 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
\r
204 * \param ctx CSR context to use
\r
205 * \param key_usage key usage flags to set
\r
207 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
\r
209 * \note The <code>decipherOnly</code> flag from the Key Usage
\r
210 * extension is represented by bit 8 (i.e.
\r
211 * <code>0x8000</code>), which cannot typically be represented
\r
212 * in an unsigned char. Therefore, the flag
\r
213 * <code>decipherOnly</code> (i.e.
\r
214 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
\r
217 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
\r
220 * \brief Set the Netscape Cert Type flags
\r
221 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
\r
223 * \param ctx CSR context to use
\r
224 * \param ns_cert_type Netscape Cert Type flags to set
\r
226 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
\r
228 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
\r
229 unsigned char ns_cert_type );
\r
232 * \brief Generic function to add to or replace an extension in the
\r
235 * \param ctx CSR context to use
\r
236 * \param oid OID of the extension
\r
237 * \param oid_len length of the OID
\r
238 * \param val value of the extension OCTET STRING
\r
239 * \param val_len length of the value data
\r
241 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
\r
243 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
\r
244 const char *oid, size_t oid_len,
\r
245 const unsigned char *val, size_t val_len );
\r
248 * \brief Free the contents of a CSR context
\r
250 * \param ctx CSR context to free
\r
252 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
\r
255 * \brief Write a CSR (Certificate Signing Request) to a
\r
257 * Note: data is written at the end of the buffer! Use the
\r
258 * return value to determine where you should start
\r
261 * \param ctx CSR to write away
\r
262 * \param buf buffer to write to
\r
263 * \param size size of the buffer
\r
264 * \param f_rng RNG function (for signature, see note)
\r
265 * \param p_rng RNG parameter
\r
267 * \return length of data written if successful, or a specific
\r
270 * \note f_rng may be NULL if RSA is used for signature and the
\r
271 * signature is made offline (otherwise f_rng is desirable
\r
272 * for countermeasures against timing attacks).
\r
273 * ECDSA signatures always require a non-NULL f_rng.
\r
275 int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
\r
276 int (*f_rng)(void *, unsigned char *, size_t),
\r
279 #if defined(MBEDTLS_PEM_WRITE_C)
\r
281 * \brief Write a CSR (Certificate Signing Request) to a
\r
284 * \param ctx CSR to write away
\r
285 * \param buf buffer to write to
\r
286 * \param size size of the buffer
\r
287 * \param f_rng RNG function (for signature, see note)
\r
288 * \param p_rng RNG parameter
\r
290 * \return 0 if successful, or a specific error code
\r
292 * \note f_rng may be NULL if RSA is used for signature and the
\r
293 * signature is made offline (otherwise f_rng is desirable
\r
294 * for countermeasures against timing attacks).
\r
295 * ECDSA signatures always require a non-NULL f_rng.
\r
297 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
\r
298 int (*f_rng)(void *, unsigned char *, size_t),
\r
300 #endif /* MBEDTLS_PEM_WRITE_C */
\r
301 #endif /* MBEDTLS_X509_CSR_WRITE_C */
\r
307 #endif /* mbedtls_x509_csr.h */
\r