mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file x509_csr.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief X.509 certificate signing request parsing and writing
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 7 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 8 *
markrad 0:cdf462088d13 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 10 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 11 * You may obtain a copy of the License at
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 18 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 19 * limitations under the License.
markrad 0:cdf462088d13 20 *
markrad 0:cdf462088d13 21 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 22 */
markrad 0:cdf462088d13 23 #ifndef MBEDTLS_X509_CSR_H
markrad 0:cdf462088d13 24 #define MBEDTLS_X509_CSR_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 27 #include "config.h"
markrad 0:cdf462088d13 28 #else
markrad 0:cdf462088d13 29 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 30 #endif
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #include "x509.h"
markrad 0:cdf462088d13 33
markrad 0:cdf462088d13 34 #ifdef __cplusplus
markrad 0:cdf462088d13 35 extern "C" {
markrad 0:cdf462088d13 36 #endif
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 /**
markrad 0:cdf462088d13 39 * \addtogroup x509_module
markrad 0:cdf462088d13 40 * \{ */
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 /**
markrad 0:cdf462088d13 43 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
markrad 0:cdf462088d13 44 * \{
markrad 0:cdf462088d13 45 */
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 /**
markrad 0:cdf462088d13 48 * Certificate Signing Request (CSR) structure.
markrad 0:cdf462088d13 49 */
markrad 0:cdf462088d13 50 typedef struct mbedtls_x509_csr
markrad 0:cdf462088d13 51 {
markrad 0:cdf462088d13 52 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
markrad 0:cdf462088d13 53 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
markrad 0:cdf462088d13 54
markrad 0:cdf462088d13 55 int version; /**< CSR version (1=v1). */
markrad 0:cdf462088d13 56
markrad 0:cdf462088d13 57 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
markrad 0:cdf462088d13 58 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
markrad 0:cdf462088d13 59
markrad 0:cdf462088d13 60 mbedtls_pk_context pk; /**< Container for the public key context. */
markrad 0:cdf462088d13 61
markrad 0:cdf462088d13 62 mbedtls_x509_buf sig_oid;
markrad 0:cdf462088d13 63 mbedtls_x509_buf sig;
markrad 0:cdf462088d13 64 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
markrad 0:cdf462088d13 65 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
markrad 0:cdf462088d13 66 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
markrad 0:cdf462088d13 67 }
markrad 0:cdf462088d13 68 mbedtls_x509_csr;
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 /**
markrad 0:cdf462088d13 71 * Container for writing a CSR
markrad 0:cdf462088d13 72 */
markrad 0:cdf462088d13 73 typedef struct mbedtls_x509write_csr
markrad 0:cdf462088d13 74 {
markrad 0:cdf462088d13 75 mbedtls_pk_context *key;
markrad 0:cdf462088d13 76 mbedtls_asn1_named_data *subject;
markrad 0:cdf462088d13 77 mbedtls_md_type_t md_alg;
markrad 0:cdf462088d13 78 mbedtls_asn1_named_data *extensions;
markrad 0:cdf462088d13 79 }
markrad 0:cdf462088d13 80 mbedtls_x509write_csr;
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 #if defined(MBEDTLS_X509_CSR_PARSE_C)
markrad 0:cdf462088d13 83 /**
markrad 0:cdf462088d13 84 * \brief Load a Certificate Signing Request (CSR) in DER format
markrad 0:cdf462088d13 85 *
markrad 0:cdf462088d13 86 * \note CSR attributes (if any) are currently silently ignored.
markrad 0:cdf462088d13 87 *
markrad 0:cdf462088d13 88 * \param csr CSR context to fill
markrad 0:cdf462088d13 89 * \param buf buffer holding the CRL data
markrad 0:cdf462088d13 90 * \param buflen size of the buffer
markrad 0:cdf462088d13 91 *
markrad 0:cdf462088d13 92 * \return 0 if successful, or a specific X509 error code
markrad 0:cdf462088d13 93 */
markrad 0:cdf462088d13 94 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
markrad 0:cdf462088d13 95 const unsigned char *buf, size_t buflen );
markrad 0:cdf462088d13 96
markrad 0:cdf462088d13 97 /**
markrad 0:cdf462088d13 98 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
markrad 0:cdf462088d13 99 *
markrad 0:cdf462088d13 100 * \note See notes for \c mbedtls_x509_csr_parse_der()
markrad 0:cdf462088d13 101 *
markrad 0:cdf462088d13 102 * \param csr CSR context to fill
markrad 0:cdf462088d13 103 * \param buf buffer holding the CRL data
markrad 0:cdf462088d13 104 * \param buflen size of the buffer
markrad 0:cdf462088d13 105 * (including the terminating null byte for PEM data)
markrad 0:cdf462088d13 106 *
markrad 0:cdf462088d13 107 * \return 0 if successful, or a specific X509 or PEM error code
markrad 0:cdf462088d13 108 */
markrad 0:cdf462088d13 109 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
markrad 0:cdf462088d13 110
markrad 0:cdf462088d13 111 #if defined(MBEDTLS_FS_IO)
markrad 0:cdf462088d13 112 /**
markrad 0:cdf462088d13 113 * \brief Load a Certificate Signing Request (CSR)
markrad 0:cdf462088d13 114 *
markrad 0:cdf462088d13 115 * \note See notes for \c mbedtls_x509_csr_parse()
markrad 0:cdf462088d13 116 *
markrad 0:cdf462088d13 117 * \param csr CSR context to fill
markrad 0:cdf462088d13 118 * \param path filename to read the CSR from
markrad 0:cdf462088d13 119 *
markrad 0:cdf462088d13 120 * \return 0 if successful, or a specific X509 or PEM error code
markrad 0:cdf462088d13 121 */
markrad 0:cdf462088d13 122 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
markrad 0:cdf462088d13 123 #endif /* MBEDTLS_FS_IO */
markrad 0:cdf462088d13 124
markrad 0:cdf462088d13 125 /**
markrad 0:cdf462088d13 126 * \brief Returns an informational string about the
markrad 0:cdf462088d13 127 * CSR.
markrad 0:cdf462088d13 128 *
markrad 0:cdf462088d13 129 * \param buf Buffer to write to
markrad 0:cdf462088d13 130 * \param size Maximum size of buffer
markrad 0:cdf462088d13 131 * \param prefix A line prefix
markrad 0:cdf462088d13 132 * \param csr The X509 CSR to represent
markrad 0:cdf462088d13 133 *
markrad 0:cdf462088d13 134 * \return The length of the string written (not including the
markrad 0:cdf462088d13 135 * terminated nul byte), or a negative error code.
markrad 0:cdf462088d13 136 */
markrad 0:cdf462088d13 137 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
markrad 0:cdf462088d13 138 const mbedtls_x509_csr *csr );
markrad 0:cdf462088d13 139
markrad 0:cdf462088d13 140 /**
markrad 0:cdf462088d13 141 * \brief Initialize a CSR
markrad 0:cdf462088d13 142 *
markrad 0:cdf462088d13 143 * \param csr CSR to initialize
markrad 0:cdf462088d13 144 */
markrad 0:cdf462088d13 145 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
markrad 0:cdf462088d13 146
markrad 0:cdf462088d13 147 /**
markrad 0:cdf462088d13 148 * \brief Unallocate all CSR data
markrad 0:cdf462088d13 149 *
markrad 0:cdf462088d13 150 * \param csr CSR to free
markrad 0:cdf462088d13 151 */
markrad 0:cdf462088d13 152 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
markrad 0:cdf462088d13 153 #endif /* MBEDTLS_X509_CSR_PARSE_C */
markrad 0:cdf462088d13 154
markrad 0:cdf462088d13 155 /* \} name */
markrad 0:cdf462088d13 156 /* \} addtogroup x509_module */
markrad 0:cdf462088d13 157
markrad 0:cdf462088d13 158 #if defined(MBEDTLS_X509_CSR_WRITE_C)
markrad 0:cdf462088d13 159 /**
markrad 0:cdf462088d13 160 * \brief Initialize a CSR context
markrad 0:cdf462088d13 161 *
markrad 0:cdf462088d13 162 * \param ctx CSR context to initialize
markrad 0:cdf462088d13 163 */
markrad 0:cdf462088d13 164 void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
markrad 0:cdf462088d13 165
markrad 0:cdf462088d13 166 /**
markrad 0:cdf462088d13 167 * \brief Set the subject name for a CSR
markrad 0:cdf462088d13 168 * Subject names should contain a comma-separated list
markrad 0:cdf462088d13 169 * of OID types and values:
markrad 0:cdf462088d13 170 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
markrad 0:cdf462088d13 171 *
markrad 0:cdf462088d13 172 * \param ctx CSR context to use
markrad 0:cdf462088d13 173 * \param subject_name subject name to set
markrad 0:cdf462088d13 174 *
markrad 0:cdf462088d13 175 * \return 0 if subject name was parsed successfully, or
markrad 0:cdf462088d13 176 * a specific error code
markrad 0:cdf462088d13 177 */
markrad 0:cdf462088d13 178 int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
markrad 0:cdf462088d13 179 const char *subject_name );
markrad 0:cdf462088d13 180
markrad 0:cdf462088d13 181 /**
markrad 0:cdf462088d13 182 * \brief Set the key for a CSR (public key will be included,
markrad 0:cdf462088d13 183 * private key used to sign the CSR when writing it)
markrad 0:cdf462088d13 184 *
markrad 0:cdf462088d13 185 * \param ctx CSR context to use
markrad 0:cdf462088d13 186 * \param key Asymetric key to include
markrad 0:cdf462088d13 187 */
markrad 0:cdf462088d13 188 void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
markrad 0:cdf462088d13 189
markrad 0:cdf462088d13 190 /**
markrad 0:cdf462088d13 191 * \brief Set the MD algorithm to use for the signature
markrad 0:cdf462088d13 192 * (e.g. MBEDTLS_MD_SHA1)
markrad 0:cdf462088d13 193 *
markrad 0:cdf462088d13 194 * \param ctx CSR context to use
markrad 0:cdf462088d13 195 * \param md_alg MD algorithm to use
markrad 0:cdf462088d13 196 */
markrad 0:cdf462088d13 197 void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
markrad 0:cdf462088d13 198
markrad 0:cdf462088d13 199 /**
markrad 0:cdf462088d13 200 * \brief Set the Key Usage Extension flags
markrad 0:cdf462088d13 201 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
markrad 0:cdf462088d13 202 *
markrad 0:cdf462088d13 203 * \param ctx CSR context to use
markrad 0:cdf462088d13 204 * \param key_usage key usage flags to set
markrad 0:cdf462088d13 205 *
markrad 0:cdf462088d13 206 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
markrad 0:cdf462088d13 207 */
markrad 0:cdf462088d13 208 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
markrad 0:cdf462088d13 209
markrad 0:cdf462088d13 210 /**
markrad 0:cdf462088d13 211 * \brief Set the Netscape Cert Type flags
markrad 0:cdf462088d13 212 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
markrad 0:cdf462088d13 213 *
markrad 0:cdf462088d13 214 * \param ctx CSR context to use
markrad 0:cdf462088d13 215 * \param ns_cert_type Netscape Cert Type flags to set
markrad 0:cdf462088d13 216 *
markrad 0:cdf462088d13 217 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
markrad 0:cdf462088d13 218 */
markrad 0:cdf462088d13 219 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
markrad 0:cdf462088d13 220 unsigned char ns_cert_type );
markrad 0:cdf462088d13 221
markrad 0:cdf462088d13 222 /**
markrad 0:cdf462088d13 223 * \brief Generic function to add to or replace an extension in the
markrad 0:cdf462088d13 224 * CSR
markrad 0:cdf462088d13 225 *
markrad 0:cdf462088d13 226 * \param ctx CSR context to use
markrad 0:cdf462088d13 227 * \param oid OID of the extension
markrad 0:cdf462088d13 228 * \param oid_len length of the OID
markrad 0:cdf462088d13 229 * \param val value of the extension OCTET STRING
markrad 0:cdf462088d13 230 * \param val_len length of the value data
markrad 0:cdf462088d13 231 *
markrad 0:cdf462088d13 232 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
markrad 0:cdf462088d13 233 */
markrad 0:cdf462088d13 234 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
markrad 0:cdf462088d13 235 const char *oid, size_t oid_len,
markrad 0:cdf462088d13 236 const unsigned char *val, size_t val_len );
markrad 0:cdf462088d13 237
markrad 0:cdf462088d13 238 /**
markrad 0:cdf462088d13 239 * \brief Free the contents of a CSR context
markrad 0:cdf462088d13 240 *
markrad 0:cdf462088d13 241 * \param ctx CSR context to free
markrad 0:cdf462088d13 242 */
markrad 0:cdf462088d13 243 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
markrad 0:cdf462088d13 244
markrad 0:cdf462088d13 245 /**
markrad 0:cdf462088d13 246 * \brief Write a CSR (Certificate Signing Request) to a
markrad 0:cdf462088d13 247 * DER structure
markrad 0:cdf462088d13 248 * Note: data is written at the end of the buffer! Use the
markrad 0:cdf462088d13 249 * return value to determine where you should start
markrad 0:cdf462088d13 250 * using the buffer
markrad 0:cdf462088d13 251 *
markrad 0:cdf462088d13 252 * \param ctx CSR to write away
markrad 0:cdf462088d13 253 * \param buf buffer to write to
markrad 0:cdf462088d13 254 * \param size size of the buffer
markrad 0:cdf462088d13 255 * \param f_rng RNG function (for signature, see note)
markrad 0:cdf462088d13 256 * \param p_rng RNG parameter
markrad 0:cdf462088d13 257 *
markrad 0:cdf462088d13 258 * \return length of data written if successful, or a specific
markrad 0:cdf462088d13 259 * error code
markrad 0:cdf462088d13 260 *
markrad 0:cdf462088d13 261 * \note f_rng may be NULL if RSA is used for signature and the
markrad 0:cdf462088d13 262 * signature is made offline (otherwise f_rng is desirable
markrad 0:cdf462088d13 263 * for countermeasures against timing attacks).
markrad 0:cdf462088d13 264 * ECDSA signatures always require a non-NULL f_rng.
markrad 0:cdf462088d13 265 */
markrad 0:cdf462088d13 266 int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
markrad 0:cdf462088d13 267 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 268 void *p_rng );
markrad 0:cdf462088d13 269
markrad 0:cdf462088d13 270 #if defined(MBEDTLS_PEM_WRITE_C)
markrad 0:cdf462088d13 271 /**
markrad 0:cdf462088d13 272 * \brief Write a CSR (Certificate Signing Request) to a
markrad 0:cdf462088d13 273 * PEM string
markrad 0:cdf462088d13 274 *
markrad 0:cdf462088d13 275 * \param ctx CSR to write away
markrad 0:cdf462088d13 276 * \param buf buffer to write to
markrad 0:cdf462088d13 277 * \param size size of the buffer
markrad 0:cdf462088d13 278 * \param f_rng RNG function (for signature, see note)
markrad 0:cdf462088d13 279 * \param p_rng RNG parameter
markrad 0:cdf462088d13 280 *
markrad 0:cdf462088d13 281 * \return 0 if successful, or a specific error code
markrad 0:cdf462088d13 282 *
markrad 0:cdf462088d13 283 * \note f_rng may be NULL if RSA is used for signature and the
markrad 0:cdf462088d13 284 * signature is made offline (otherwise f_rng is desirable
markrad 0:cdf462088d13 285 * for countermeasures against timing attacks).
markrad 0:cdf462088d13 286 * ECDSA signatures always require a non-NULL f_rng.
markrad 0:cdf462088d13 287 */
markrad 0:cdf462088d13 288 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
markrad 0:cdf462088d13 289 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 290 void *p_rng );
markrad 0:cdf462088d13 291 #endif /* MBEDTLS_PEM_WRITE_C */
markrad 0:cdf462088d13 292 #endif /* MBEDTLS_X509_CSR_WRITE_C */
markrad 0:cdf462088d13 293
markrad 0:cdf462088d13 294 #ifdef __cplusplus
markrad 0:cdf462088d13 295 }
markrad 0:cdf462088d13 296 #endif
markrad 0:cdf462088d13 297
markrad 0:cdf462088d13 298 #endif /* mbedtls_x509_csr.h */