Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file x509_csr.h |
| 3 | * |
| 4 | * \brief X.509 certificate signing request parsing and writing |
| 5 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 967a2a5 | 2015-01-22 14:28:16 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (http://www.polarssl.org) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_X509_CSR_H |
| 28 | #define POLARSSL_X509_CSR_H |
| 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 31 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | |
| 36 | #include "x509.h" |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /** |
| 43 | * \addtogroup x509_module |
| 44 | * \{ */ |
| 45 | |
| 46 | /** |
| 47 | * \name Structures and functions for X.509 Certificate Signing Requests (CSR) |
| 48 | * \{ |
| 49 | */ |
| 50 | |
| 51 | /** |
| 52 | * Certificate Signing Request (CSR) structure. |
| 53 | */ |
| 54 | typedef struct _x509_csr |
| 55 | { |
| 56 | x509_buf raw; /**< The raw CSR data (DER). */ |
| 57 | x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */ |
| 58 | |
Manuel Pégourié-Gonnard | f4e1b64 | 2014-06-19 11:39:46 +0200 | [diff] [blame] | 59 | int version; /**< CSR version (1=v1). */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 60 | |
| 61 | x509_buf subject_raw; /**< The raw subject data (DER). */ |
| 62 | x509_name subject; /**< The parsed subject data (named information object). */ |
| 63 | |
| 64 | pk_context pk; /**< Container for the public key context. */ |
| 65 | |
| 66 | x509_buf sig_oid; |
| 67 | x509_buf sig; |
| 68 | md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */ |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 69 | pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */ |
Paul Bakker | 6dade7c | 2014-06-12 22:02:14 +0200 | [diff] [blame] | 70 | void *sig_opts; /**< Signature options to be passed to pk_verify_ext(), e.g. for RSASSA-PSS */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 71 | } |
| 72 | x509_csr; |
| 73 | |
| 74 | /** |
| 75 | * Container for writing a CSR |
| 76 | */ |
| 77 | typedef struct _x509write_csr |
| 78 | { |
| 79 | pk_context *key; |
| 80 | asn1_named_data *subject; |
| 81 | md_type_t md_alg; |
| 82 | asn1_named_data *extensions; |
| 83 | } |
| 84 | x509write_csr; |
| 85 | |
| 86 | #if defined(POLARSSL_X509_CSR_PARSE_C) |
| 87 | /** |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 88 | * \brief Load a Certificate Signing Request (CSR) in DER format |
| 89 | * |
| 90 | * \param csr CSR context to fill |
| 91 | * \param buf buffer holding the CRL data |
| 92 | * \param buflen size of the buffer |
| 93 | * |
| 94 | * \return 0 if successful, or a specific X509 error code |
| 95 | */ |
| 96 | int x509_csr_parse_der( x509_csr *csr, |
| 97 | const unsigned char *buf, size_t buflen ); |
| 98 | |
| 99 | /** |
| 100 | * \brief Load a Certificate Signing Request (CSR), DER or PEM format |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 101 | * |
| 102 | * \param csr CSR context to fill |
| 103 | * \param buf buffer holding the CRL data |
| 104 | * \param buflen size of the buffer |
| 105 | * |
| 106 | * \return 0 if successful, or a specific X509 or PEM error code |
| 107 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 108 | int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 109 | |
| 110 | #if defined(POLARSSL_FS_IO) |
| 111 | /** |
| 112 | * \brief Load a Certificate Signing Request (CSR) |
| 113 | * |
| 114 | * \param csr CSR context to fill |
| 115 | * \param path filename to read the CSR from |
| 116 | * |
| 117 | * \return 0 if successful, or a specific X509 or PEM error code |
| 118 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 119 | int x509_csr_parse_file( x509_csr *csr, const char *path ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 120 | #endif /* POLARSSL_FS_IO */ |
| 121 | |
| 122 | /** |
| 123 | * \brief Returns an informational string about the |
| 124 | * CSR. |
| 125 | * |
| 126 | * \param buf Buffer to write to |
| 127 | * \param size Maximum size of buffer |
| 128 | * \param prefix A line prefix |
| 129 | * \param csr The X509 CSR to represent |
| 130 | * |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 131 | * \return The length of the string written (exluding the terminating |
| 132 | * null byte), or a negative value in case of an error. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 133 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 134 | int x509_csr_info( char *buf, size_t size, const char *prefix, |
| 135 | const x509_csr *csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 136 | |
| 137 | /** |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 138 | * \brief Initialize a CSR |
| 139 | * |
| 140 | * \param csr CSR to initialize |
| 141 | */ |
| 142 | void x509_csr_init( x509_csr *csr ); |
| 143 | |
| 144 | /** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 145 | * \brief Unallocate all CSR data |
| 146 | * |
| 147 | * \param csr CSR to free |
| 148 | */ |
| 149 | void x509_csr_free( x509_csr *csr ); |
| 150 | #endif /* POLARSSL_X509_CSR_PARSE_C */ |
| 151 | |
| 152 | /* \} name */ |
| 153 | /* \} addtogroup x509_module */ |
| 154 | |
| 155 | #if defined(POLARSSL_X509_CSR_WRITE_C) |
| 156 | /** |
| 157 | * \brief Initialize a CSR context |
| 158 | * |
| 159 | * \param ctx CSR context to initialize |
| 160 | */ |
| 161 | void x509write_csr_init( x509write_csr *ctx ); |
| 162 | |
| 163 | /** |
| 164 | * \brief Set the subject name for a CSR |
| 165 | * Subject names should contain a comma-separated list |
| 166 | * of OID types and values: |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame^] | 167 | * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 168 | * |
| 169 | * \param ctx CSR context to use |
| 170 | * \param subject_name subject name to set |
| 171 | * |
| 172 | * \return 0 if subject name was parsed successfully, or |
| 173 | * a specific error code |
| 174 | */ |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 175 | int x509write_csr_set_subject_name( x509write_csr *ctx, |
| 176 | const char *subject_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * \brief Set the key for a CSR (public key will be included, |
| 180 | * private key used to sign the CSR when writing it) |
| 181 | * |
| 182 | * \param ctx CSR context to use |
| 183 | * \param key Asymetric key to include |
| 184 | */ |
| 185 | void x509write_csr_set_key( x509write_csr *ctx, pk_context *key ); |
| 186 | |
| 187 | /** |
| 188 | * \brief Set the MD algorithm to use for the signature |
| 189 | * (e.g. POLARSSL_MD_SHA1) |
| 190 | * |
| 191 | * \param ctx CSR context to use |
| 192 | * \param md_alg MD algorithm to use |
| 193 | */ |
| 194 | void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg ); |
| 195 | |
| 196 | /** |
| 197 | * \brief Set the Key Usage Extension flags |
| 198 | * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN) |
| 199 | * |
| 200 | * \param ctx CSR context to use |
| 201 | * \param key_usage key usage flags to set |
| 202 | * |
| 203 | * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 204 | */ |
| 205 | int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage ); |
| 206 | |
| 207 | /** |
| 208 | * \brief Set the Netscape Cert Type flags |
| 209 | * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL) |
| 210 | * |
| 211 | * \param ctx CSR context to use |
| 212 | * \param ns_cert_type Netscape Cert Type flags to set |
| 213 | * |
| 214 | * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 215 | */ |
| 216 | int x509write_csr_set_ns_cert_type( x509write_csr *ctx, |
| 217 | unsigned char ns_cert_type ); |
| 218 | |
| 219 | /** |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 220 | * \brief Generic function to add to or replace an extension in the |
| 221 | * CSR |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 222 | * |
| 223 | * \param ctx CSR context to use |
| 224 | * \param oid OID of the extension |
| 225 | * \param oid_len length of the OID |
| 226 | * \param val value of the extension OCTET STRING |
| 227 | * \param val_len length of the value data |
| 228 | * |
| 229 | * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 230 | */ |
| 231 | int x509write_csr_set_extension( x509write_csr *ctx, |
| 232 | const char *oid, size_t oid_len, |
| 233 | const unsigned char *val, size_t val_len ); |
| 234 | |
| 235 | /** |
| 236 | * \brief Free the contents of a CSR context |
| 237 | * |
| 238 | * \param ctx CSR context to free |
| 239 | */ |
| 240 | void x509write_csr_free( x509write_csr *ctx ); |
| 241 | |
| 242 | /** |
| 243 | * \brief Write a CSR (Certificate Signing Request) to a |
| 244 | * DER structure |
| 245 | * Note: data is written at the end of the buffer! Use the |
| 246 | * return value to determine where you should start |
| 247 | * using the buffer |
| 248 | * |
| 249 | * \param ctx CSR to write away |
| 250 | * \param buf buffer to write to |
| 251 | * \param size size of the buffer |
| 252 | * \param f_rng RNG function (for signature, see note) |
| 253 | * \param p_rng RNG parameter |
| 254 | * |
| 255 | * \return length of data written if successful, or a specific |
| 256 | * error code |
| 257 | * |
| 258 | * \note f_rng may be NULL if RSA is used for signature and the |
| 259 | * signature is made offline (otherwise f_rng is desirable |
| 260 | * for countermeasures against timing attacks). |
| 261 | * ECDSA signatures always require a non-NULL f_rng. |
| 262 | */ |
| 263 | int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size, |
| 264 | int (*f_rng)(void *, unsigned char *, size_t), |
| 265 | void *p_rng ); |
| 266 | |
| 267 | #if defined(POLARSSL_PEM_WRITE_C) |
| 268 | /** |
| 269 | * \brief Write a CSR (Certificate Signing Request) to a |
| 270 | * PEM string |
| 271 | * |
| 272 | * \param ctx CSR to write away |
| 273 | * \param buf buffer to write to |
| 274 | * \param size size of the buffer |
| 275 | * \param f_rng RNG function (for signature, see note) |
| 276 | * \param p_rng RNG parameter |
| 277 | * |
| 278 | * \return 0 successful, or a specific error code |
| 279 | * |
| 280 | * \note f_rng may be NULL if RSA is used for signature and the |
| 281 | * signature is made offline (otherwise f_rng is desirable |
| 282 | * for couermeasures against timing attacks). |
| 283 | * ECDSA signatures always require a non-NULL f_rng. |
| 284 | */ |
| 285 | int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size, |
| 286 | int (*f_rng)(void *, unsigned char *, size_t), |
| 287 | void *p_rng ); |
| 288 | #endif /* POLARSSL_PEM_WRITE_C */ |
| 289 | #endif /* POLARSSL_X509_CSR_WRITE_C */ |
| 290 | |
| 291 | #ifdef __cplusplus |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | #endif /* x509_csr.h */ |