Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
x509_csr.h
00001 /** 00002 * \file x509_csr.h 00003 * 00004 * \brief X.509 certificate signing request parsing and writing 00005 */ 00006 /* 00007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00008 * SPDX-License-Identifier: Apache-2.0 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00011 * not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00018 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 * 00022 * This file is part of mbed TLS (https://tls.mbed.org) 00023 */ 00024 #ifndef MBEDTLS_X509_CSR_H 00025 #define MBEDTLS_X509_CSR_H 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "mbedtls/config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include "mbedtls/x509.h" 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 /** 00040 * \addtogroup x509_module 00041 * \{ */ 00042 00043 /** 00044 * \name Structures and functions for X.509 Certificate Signing Requests (CSR) 00045 * \{ 00046 */ 00047 00048 /** 00049 * Certificate Signing Request (CSR) structure. 00050 */ 00051 typedef struct mbedtls_x509_csr 00052 { 00053 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */ 00054 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */ 00055 00056 int version; /**< CSR version (1=v1). */ 00057 00058 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */ 00059 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */ 00060 00061 mbedtls_pk_context pk; /**< Container for the public key context. */ 00062 00063 mbedtls_x509_buf sig_oid; 00064 mbedtls_x509_buf sig; 00065 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */ 00066 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */ 00067 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */ 00068 } 00069 mbedtls_x509_csr; 00070 00071 /** 00072 * Container for writing a CSR 00073 */ 00074 typedef struct mbedtls_x509write_csr 00075 { 00076 mbedtls_pk_context *key; 00077 mbedtls_asn1_named_data *subject; 00078 mbedtls_md_type_t md_alg; 00079 mbedtls_asn1_named_data *extensions; 00080 } 00081 mbedtls_x509write_csr; 00082 00083 #if defined(MBEDTLS_X509_CSR_PARSE_C) 00084 /** 00085 * \brief Load a Certificate Signing Request (CSR) in DER format 00086 * 00087 * \note CSR attributes (if any) are currently silently ignored. 00088 * 00089 * \param csr CSR context to fill 00090 * \param buf buffer holding the CRL data 00091 * \param buflen size of the buffer 00092 * 00093 * \return 0 if successful, or a specific X509 error code 00094 */ 00095 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr, 00096 const unsigned char *buf, size_t buflen ); 00097 00098 /** 00099 * \brief Load a Certificate Signing Request (CSR), DER or PEM format 00100 * 00101 * \note See notes for \c mbedtls_x509_csr_parse_der() 00102 * 00103 * \param csr CSR context to fill 00104 * \param buf buffer holding the CRL data 00105 * \param buflen size of the buffer 00106 * (including the terminating null byte for PEM data) 00107 * 00108 * \return 0 if successful, or a specific X509 or PEM error code 00109 */ 00110 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen ); 00111 00112 #if defined(MBEDTLS_FS_IO) 00113 /** 00114 * \brief Load a Certificate Signing Request (CSR) 00115 * 00116 * \note See notes for \c mbedtls_x509_csr_parse() 00117 * 00118 * \param csr CSR context to fill 00119 * \param path filename to read the CSR from 00120 * 00121 * \return 0 if successful, or a specific X509 or PEM error code 00122 */ 00123 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path ); 00124 #endif /* MBEDTLS_FS_IO */ 00125 00126 /** 00127 * \brief Returns an informational string about the 00128 * CSR. 00129 * 00130 * \param buf Buffer to write to 00131 * \param size Maximum size of buffer 00132 * \param prefix A line prefix 00133 * \param csr The X509 CSR to represent 00134 * 00135 * \return The length of the string written (not including the 00136 * terminated nul byte), or a negative error code. 00137 */ 00138 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix, 00139 const mbedtls_x509_csr *csr ); 00140 00141 /** 00142 * \brief Initialize a CSR 00143 * 00144 * \param csr CSR to initialize 00145 */ 00146 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr ); 00147 00148 /** 00149 * \brief Unallocate all CSR data 00150 * 00151 * \param csr CSR to free 00152 */ 00153 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr ); 00154 #endif /* MBEDTLS_X509_CSR_PARSE_C */ 00155 00156 /* \} name */ 00157 /* \} addtogroup x509_module */ 00158 00159 #if defined(MBEDTLS_X509_CSR_WRITE_C) 00160 /** 00161 * \brief Initialize a CSR context 00162 * 00163 * \param ctx CSR context to initialize 00164 */ 00165 void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx ); 00166 00167 /** 00168 * \brief Set the subject name for a CSR 00169 * Subject names should contain a comma-separated list 00170 * of OID types and values: 00171 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1" 00172 * 00173 * \param ctx CSR context to use 00174 * \param subject_name subject name to set 00175 * 00176 * \return 0 if subject name was parsed successfully, or 00177 * a specific error code 00178 */ 00179 int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx, 00180 const char *subject_name ); 00181 00182 /** 00183 * \brief Set the key for a CSR (public key will be included, 00184 * private key used to sign the CSR when writing it) 00185 * 00186 * \param ctx CSR context to use 00187 * \param key Asymetric key to include 00188 */ 00189 void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key ); 00190 00191 /** 00192 * \brief Set the MD algorithm to use for the signature 00193 * (e.g. MBEDTLS_MD_SHA1) 00194 * 00195 * \param ctx CSR context to use 00196 * \param md_alg MD algorithm to use 00197 */ 00198 void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg ); 00199 00200 /** 00201 * \brief Set the Key Usage Extension flags 00202 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN) 00203 * 00204 * \param ctx CSR context to use 00205 * \param key_usage key usage flags to set 00206 * 00207 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED 00208 * 00209 * \note The <code>decipherOnly</code> flag from the Key Usage 00210 * extension is represented by bit 8 (i.e. 00211 * <code>0x8000</code>), which cannot typically be represented 00212 * in an unsigned char. Therefore, the flag 00213 * <code>decipherOnly</code> (i.e. 00214 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this 00215 * function. 00216 */ 00217 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage ); 00218 00219 /** 00220 * \brief Set the Netscape Cert Type flags 00221 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL) 00222 * 00223 * \param ctx CSR context to use 00224 * \param ns_cert_type Netscape Cert Type flags to set 00225 * 00226 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED 00227 */ 00228 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx, 00229 unsigned char ns_cert_type ); 00230 00231 /** 00232 * \brief Generic function to add to or replace an extension in the 00233 * CSR 00234 * 00235 * \param ctx CSR context to use 00236 * \param oid OID of the extension 00237 * \param oid_len length of the OID 00238 * \param val value of the extension OCTET STRING 00239 * \param val_len length of the value data 00240 * 00241 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED 00242 */ 00243 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx, 00244 const char *oid, size_t oid_len, 00245 const unsigned char *val, size_t val_len ); 00246 00247 /** 00248 * \brief Free the contents of a CSR context 00249 * 00250 * \param ctx CSR context to free 00251 */ 00252 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx ); 00253 00254 /** 00255 * \brief Write a CSR (Certificate Signing Request) to a 00256 * DER structure 00257 * Note: data is written at the end of the buffer! Use the 00258 * return value to determine where you should start 00259 * using the buffer 00260 * 00261 * \param ctx CSR to write away 00262 * \param buf buffer to write to 00263 * \param size size of the buffer 00264 * \param f_rng RNG function (for signature, see note) 00265 * \param p_rng RNG parameter 00266 * 00267 * \return length of data written if successful, or a specific 00268 * error code 00269 * 00270 * \note f_rng may be NULL if RSA is used for signature and the 00271 * signature is made offline (otherwise f_rng is desirable 00272 * for countermeasures against timing attacks). 00273 * ECDSA signatures always require a non-NULL f_rng. 00274 */ 00275 int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, 00276 int (*f_rng)(void *, unsigned char *, size_t), 00277 void *p_rng ); 00278 00279 #if defined(MBEDTLS_PEM_WRITE_C) 00280 /** 00281 * \brief Write a CSR (Certificate Signing Request) to a 00282 * PEM string 00283 * 00284 * \param ctx CSR to write away 00285 * \param buf buffer to write to 00286 * \param size size of the buffer 00287 * \param f_rng RNG function (for signature, see note) 00288 * \param p_rng RNG parameter 00289 * 00290 * \return 0 if successful, or a specific error code 00291 * 00292 * \note f_rng may be NULL if RSA is used for signature and the 00293 * signature is made offline (otherwise f_rng is desirable 00294 * for countermeasures against timing attacks). 00295 * ECDSA signatures always require a non-NULL f_rng. 00296 */ 00297 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, 00298 int (*f_rng)(void *, unsigned char *, size_t), 00299 void *p_rng ); 00300 #endif /* MBEDTLS_PEM_WRITE_C */ 00301 #endif /* MBEDTLS_X509_CSR_WRITE_C */ 00302 00303 #ifdef __cplusplus 00304 } 00305 #endif 00306 00307 #endif /* mbedtls_x509_csr.h */
Generated on Tue Jul 12 2022 13:55:05 by
