Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers x509_csr.h Source File

x509_csr.h

Go to the documentation of this file.
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 "config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #include "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 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
00210 
00211 /**
00212  * \brief           Set the Netscape Cert Type flags
00213  *                  (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
00214  *
00215  * \param ctx           CSR context to use
00216  * \param ns_cert_type  Netscape Cert Type flags to set
00217  *
00218  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
00219  */
00220 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
00221                                     unsigned char ns_cert_type );
00222 
00223 /**
00224  * \brief           Generic function to add to or replace an extension in the
00225  *                  CSR
00226  *
00227  * \param ctx       CSR context to use
00228  * \param oid       OID of the extension
00229  * \param oid_len   length of the OID
00230  * \param val       value of the extension OCTET STRING
00231  * \param val_len   length of the value data
00232  *
00233  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
00234  */
00235 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
00236                                  const char *oid, size_t oid_len,
00237                                  const unsigned char *val, size_t val_len );
00238 
00239 /**
00240  * \brief           Free the contents of a CSR context
00241  *
00242  * \param ctx       CSR context to free
00243  */
00244 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
00245 
00246 /**
00247  * \brief           Write a CSR (Certificate Signing Request) to a
00248  *                  DER structure
00249  *                  Note: data is written at the end of the buffer! Use the
00250  *                        return value to determine where you should start
00251  *                        using the buffer
00252  *
00253  * \param ctx       CSR to write away
00254  * \param buf       buffer to write to
00255  * \param size      size of the buffer
00256  * \param f_rng     RNG function (for signature, see note)
00257  * \param p_rng     RNG parameter
00258  *
00259  * \return          length of data written if successful, or a specific
00260  *                  error code
00261  *
00262  * \note            f_rng may be NULL if RSA is used for signature and the
00263  *                  signature is made offline (otherwise f_rng is desirable
00264  *                  for countermeasures against timing attacks).
00265  *                  ECDSA signatures always require a non-NULL f_rng.
00266  */
00267 int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
00268                        int (*f_rng)(void *, unsigned char *, size_t),
00269                        void *p_rng );
00270 
00271 #if defined(MBEDTLS_PEM_WRITE_C)
00272 /**
00273  * \brief           Write a CSR (Certificate Signing Request) to a
00274  *                  PEM string
00275  *
00276  * \param ctx       CSR to write away
00277  * \param buf       buffer to write to
00278  * \param size      size of the buffer
00279  * \param f_rng     RNG function (for signature, see note)
00280  * \param p_rng     RNG parameter
00281  *
00282  * \return          0 if successful, or a specific error code
00283  *
00284  * \note            f_rng may be NULL if RSA is used for signature and the
00285  *                  signature is made offline (otherwise f_rng is desirable
00286  *                  for countermeasures against timing attacks).
00287  *                  ECDSA signatures always require a non-NULL f_rng.
00288  */
00289 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
00290                        int (*f_rng)(void *, unsigned char *, size_t),
00291                        void *p_rng );
00292 #endif /* MBEDTLS_PEM_WRITE_C */
00293 #endif /* MBEDTLS_X509_CSR_WRITE_C */
00294 
00295 #ifdef __cplusplus
00296 }
00297 #endif
00298 
00299 #endif /* mbedtls_x509_csr.h */