Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file x509_crt.h |
| 3 | * |
| 4 | * \brief X.509 certificate parsing and writing |
| 5 | * |
| 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 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_CRT_H |
| 28 | #define POLARSSL_X509_CRT_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 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 38 | #include "x509_crl.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * \addtogroup x509_module |
| 42 | * \{ |
| 43 | */ |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | /** |
| 50 | * \name Structures and functions for parsing and writing X.509 certificates |
| 51 | * \{ |
| 52 | */ |
| 53 | |
| 54 | /** |
| 55 | * Container for an X.509 certificate. The certificate may be chained. |
| 56 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 57 | typedef struct _x509_crt |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 58 | { |
| 59 | x509_buf raw; /**< The raw certificate data (DER). */ |
| 60 | x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */ |
| 61 | |
Manuel Pégourié-Gonnard | f4e1b64 | 2014-06-19 11:39:46 +0200 | [diff] [blame^] | 62 | int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 63 | x509_buf serial; /**< Unique id for certificate issued by a specific CA. */ |
| 64 | x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */ |
| 65 | |
| 66 | x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */ |
| 67 | x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */ |
| 68 | |
| 69 | x509_name issuer; /**< The parsed issuer data (named information object). */ |
| 70 | x509_name subject; /**< The parsed subject data (named information object). */ |
| 71 | |
| 72 | x509_time valid_from; /**< Start time of certificate validity. */ |
| 73 | x509_time valid_to; /**< End time of certificate validity. */ |
| 74 | |
| 75 | pk_context pk; /**< Container for the public key context. */ |
| 76 | |
| 77 | x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */ |
| 78 | x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */ |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 79 | x509_buf v3_ext; /**< Optional X.509 v3 extensions. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 80 | x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */ |
| 81 | |
| 82 | int ext_types; /**< Bit string containing detected and parsed extensions */ |
| 83 | int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */ |
| 84 | int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */ |
| 85 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 86 | unsigned char key_usage; /**< Optional key usage extension value: See the values in x509.h */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 87 | |
| 88 | x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */ |
| 89 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 90 | unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 91 | |
| 92 | x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */ |
| 93 | x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */ |
| 94 | 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] | 95 | 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] | 96 | 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] | 97 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 98 | struct _x509_crt *next; /**< Next certificate in the CA-chain. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 99 | } |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 100 | x509_crt; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 101 | |
| 102 | #define X509_CRT_VERSION_1 0 |
| 103 | #define X509_CRT_VERSION_2 1 |
| 104 | #define X509_CRT_VERSION_3 2 |
| 105 | |
| 106 | #define X509_RFC5280_MAX_SERIAL_LEN 32 |
| 107 | #define X509_RFC5280_UTC_TIME_LEN 15 |
| 108 | |
| 109 | /** |
| 110 | * Container for writing a certificate (CRT) |
| 111 | */ |
| 112 | typedef struct _x509write_cert |
| 113 | { |
| 114 | int version; |
| 115 | mpi serial; |
| 116 | pk_context *subject_key; |
| 117 | pk_context *issuer_key; |
| 118 | asn1_named_data *subject; |
| 119 | asn1_named_data *issuer; |
| 120 | md_type_t md_alg; |
| 121 | char not_before[X509_RFC5280_UTC_TIME_LEN + 1]; |
| 122 | char not_after[X509_RFC5280_UTC_TIME_LEN + 1]; |
| 123 | asn1_named_data *extensions; |
| 124 | } |
| 125 | x509write_cert; |
| 126 | |
| 127 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 128 | /** |
| 129 | * \brief Parse a single DER formatted certificate and add it |
| 130 | * to the chained list. |
| 131 | * |
| 132 | * \param chain points to the start of the chain |
| 133 | * \param buf buffer holding the certificate DER data |
| 134 | * \param buflen size of the buffer |
| 135 | * |
| 136 | * \return 0 if successful, or a specific X509 or PEM error code |
| 137 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 138 | int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 139 | size_t buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * \brief Parse one or more certificates and add them |
| 143 | * to the chained list. Parses permissively. If some |
| 144 | * certificates can be parsed, the result is the number |
| 145 | * of failed certificates it encountered. If none complete |
| 146 | * correctly, the first error is returned. |
| 147 | * |
| 148 | * \param chain points to the start of the chain |
| 149 | * \param buf buffer holding the certificate data |
| 150 | * \param buflen size of the buffer |
| 151 | * |
| 152 | * \return 0 if all certificates parsed successfully, a positive number |
| 153 | * if partly successful or a specific X509 or PEM error code |
| 154 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 155 | int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 156 | |
| 157 | #if defined(POLARSSL_FS_IO) |
| 158 | /** |
| 159 | * \brief Load one or more certificates and add them |
| 160 | * to the chained list. Parses permissively. If some |
| 161 | * certificates can be parsed, the result is the number |
| 162 | * of failed certificates it encountered. If none complete |
| 163 | * correctly, the first error is returned. |
| 164 | * |
| 165 | * \param chain points to the start of the chain |
| 166 | * \param path filename to read the certificates from |
| 167 | * |
| 168 | * \return 0 if all certificates parsed successfully, a positive number |
| 169 | * if partly successful or a specific X509 or PEM error code |
| 170 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 171 | int x509_crt_parse_file( x509_crt *chain, const char *path ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * \brief Load one or more certificate files from a path and add them |
| 175 | * to the chained list. Parses permissively. If some |
| 176 | * certificates can be parsed, the result is the number |
| 177 | * of failed certificates it encountered. If none complete |
| 178 | * correctly, the first error is returned. |
| 179 | * |
Manuel Pégourié-Gonnard | e3339ce | 2013-11-28 17:16:41 +0100 | [diff] [blame] | 180 | * \warning This function is NOT thread-safe unless |
| 181 | * POLARSSL_THREADING_PTHREADS is defined. If you're using an |
| 182 | * alternative threading implementation, you should either use |
| 183 | * this function only in the main thread, or mutex it. |
| 184 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 185 | * \param chain points to the start of the chain |
| 186 | * \param path directory / folder to read the certificate files from |
| 187 | * |
| 188 | * \return 0 if all certificates parsed successfully, a positive number |
| 189 | * if partly successful or a specific X509 or PEM error code |
| 190 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 191 | int x509_crt_parse_path( x509_crt *chain, const char *path ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 192 | #endif /* POLARSSL_FS_IO */ |
| 193 | |
| 194 | /** |
| 195 | * \brief Returns an informational string about the |
| 196 | * certificate. |
| 197 | * |
| 198 | * \param buf Buffer to write to |
| 199 | * \param size Maximum size of buffer |
| 200 | * \param prefix A line prefix |
| 201 | * \param crt The X509 certificate to represent |
| 202 | * |
| 203 | * \return The amount of data written to the buffer, or -1 in |
| 204 | * case of an error. |
| 205 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 206 | int x509_crt_info( char *buf, size_t size, const char *prefix, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 207 | const x509_crt *crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * \brief Verify the certificate signature |
| 211 | * |
| 212 | * The verify callback is a user-supplied callback that |
| 213 | * can clear / modify / add flags for a certificate. If set, |
| 214 | * the verification callback is called for each |
| 215 | * certificate in the chain (from the trust-ca down to the |
| 216 | * presented crt). The parameters for the callback are: |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 217 | * (void *parameter, x509_crt *crt, int certificate_depth, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 218 | * int *flags). With the flags representing current flags for |
| 219 | * that specific certificate and the certificate depth from |
| 220 | * the bottom (Peer cert depth = 0). |
| 221 | * |
| 222 | * All flags left after returning from the callback |
| 223 | * are also returned to the application. The function should |
| 224 | * return 0 for anything but a fatal error. |
| 225 | * |
| 226 | * \param crt a certificate to be verified |
| 227 | * \param trust_ca the trusted CA chain |
| 228 | * \param ca_crl the CRL chain for trusted CA's |
| 229 | * \param cn expected Common Name (can be set to |
| 230 | * NULL if the CN must not be verified) |
| 231 | * \param flags result of the verification |
| 232 | * \param f_vrfy verification function |
| 233 | * \param p_vrfy verification parameter |
| 234 | * |
| 235 | * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED, |
| 236 | * in which case *flags will have one or more of |
| 237 | * the following values set: |
| 238 | * BADCERT_EXPIRED -- |
| 239 | * BADCERT_REVOKED -- |
| 240 | * BADCERT_CN_MISMATCH -- |
| 241 | * BADCERT_NOT_TRUSTED |
| 242 | * or another error in case of a fatal error encountered |
| 243 | * during the verification process. |
| 244 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 245 | int x509_crt_verify( x509_crt *crt, |
| 246 | x509_crt *trust_ca, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 247 | x509_crl *ca_crl, |
| 248 | const char *cn, int *flags, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 249 | int (*f_vrfy)(void *, x509_crt *, int, int *), |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 250 | void *p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 251 | |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 252 | #if defined(POLARSSL_X509_CHECK_KEY_USAGE) |
| 253 | /** |
| 254 | * \brief Check usage of certificate against keyUsage extension. |
| 255 | * |
| 256 | * \param crt Leaf certificate used. |
| 257 | * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the |
| 258 | * certificate to perform an RSA key exchange). |
| 259 | * |
| 260 | * \return 0 is these uses of the certificate are allowed, |
Paul Bakker | 02ff5ce | 2014-04-09 15:53:09 +0200 | [diff] [blame] | 261 | * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extension |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 262 | * is present but does not contain all the bits set in the |
| 263 | * usage argument. |
| 264 | * |
| 265 | * \note You should only call this function on leaf certificates, on |
| 266 | * (intermediate) CAs the keyUsage extension is automatically |
| 267 | * checked by \c x509_crt_verify(). |
| 268 | */ |
| 269 | int x509_crt_check_key_usage( const x509_crt *crt, int usage ); |
| 270 | #endif /* POLARSSL_X509_CHECK_KEY_USAGE) */ |
| 271 | |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 272 | #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) |
| 273 | /** |
| 274 | * \brief Check usage of certificate against extentedJeyUsage. |
| 275 | * |
| 276 | * \param crt Leaf certificate used. |
| 277 | * \param usage_oid Intended usage (eg OID_SERVER_AUTH or OID_CLIENT_AUTH). |
| 278 | * \param usage_len Length of usage_oid (eg given by OID_SIZE()). |
| 279 | * |
| 280 | * \return 0 is this use of the certificate is allowed, |
| 281 | * POLARSSL_ERR_X509_BAD_INPUT_DATA if not. |
| 282 | * |
| 283 | * \note Usually only makes sense on leaf certificates. |
| 284 | */ |
| 285 | int x509_crt_check_extended_key_usage( const x509_crt *crt, |
| 286 | const char *usage_oid, |
| 287 | size_t usage_len ); |
| 288 | #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) */ |
| 289 | |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 290 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 291 | /** |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 292 | * \brief Verify the certificate revocation status |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 293 | * |
| 294 | * \param crt a certificate to be verified |
| 295 | * \param crl the CRL to verify against |
| 296 | * |
| 297 | * \return 1 if the certificate is revoked, 0 otherwise |
| 298 | * |
| 299 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 300 | int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 301 | #endif /* POLARSSL_X509_CRL_PARSE_C */ |
| 302 | |
| 303 | /** |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 304 | * \brief Initialize a certificate (chain) |
| 305 | * |
| 306 | * \param crt Certificate chain to initialize |
| 307 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 308 | void x509_crt_init( x509_crt *crt ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 309 | |
| 310 | /** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 311 | * \brief Unallocate all certificate data |
| 312 | * |
| 313 | * \param crt Certificate chain to free |
| 314 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 315 | void x509_crt_free( x509_crt *crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 316 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
| 317 | |
| 318 | /* \} name */ |
| 319 | /* \} addtogroup x509_module */ |
| 320 | |
| 321 | #if defined(POLARSSL_X509_CRT_WRITE_C) |
| 322 | /** |
| 323 | * \brief Initialize a CRT writing context |
| 324 | * |
| 325 | * \param ctx CRT context to initialize |
| 326 | */ |
| 327 | void x509write_crt_init( x509write_cert *ctx ); |
| 328 | |
| 329 | /** |
| 330 | * \brief Set the verion for a Certificate |
| 331 | * Default: X509_CRT_VERSION_3 |
| 332 | * |
| 333 | * \param ctx CRT context to use |
| 334 | * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or |
| 335 | * X509_CRT_VERSION_3) |
| 336 | */ |
| 337 | void x509write_crt_set_version( x509write_cert *ctx, int version ); |
| 338 | |
| 339 | /** |
| 340 | * \brief Set the serial number for a Certificate. |
| 341 | * |
| 342 | * \param ctx CRT context to use |
| 343 | * \param serial serial number to set |
| 344 | * |
| 345 | * \return 0 if successful |
| 346 | */ |
| 347 | int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial ); |
| 348 | |
| 349 | /** |
| 350 | * \brief Set the validity period for a Certificate |
| 351 | * Timestamps should be in string format for UTC timezone |
| 352 | * i.e. "YYYYMMDDhhmmss" |
| 353 | * e.g. "20131231235959" for December 31st 2013 |
| 354 | * at 23:59:59 |
| 355 | * |
| 356 | * \param ctx CRT context to use |
| 357 | * \param not_before not_before timestamp |
| 358 | * \param not_after not_after timestamp |
| 359 | * |
| 360 | * \return 0 if timestamp was parsed successfully, or |
| 361 | * a specific error code |
| 362 | */ |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 363 | int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before, |
| 364 | const char *not_after ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * \brief Set the issuer name for a Certificate |
| 368 | * Issuer names should contain a comma-separated list |
| 369 | * of OID types and values: |
| 370 | * e.g. "C=NL,O=Offspark,CN=PolarSSL CA" |
| 371 | * |
| 372 | * \param ctx CRT context to use |
| 373 | * \param issuer_name issuer name to set |
| 374 | * |
| 375 | * \return 0 if issuer name was parsed successfully, or |
| 376 | * a specific error code |
| 377 | */ |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 378 | int x509write_crt_set_issuer_name( x509write_cert *ctx, |
| 379 | const char *issuer_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 380 | |
| 381 | /** |
| 382 | * \brief Set the subject name for a Certificate |
| 383 | * Subject names should contain a comma-separated list |
| 384 | * of OID types and values: |
| 385 | * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1" |
| 386 | * |
| 387 | * \param ctx CRT context to use |
| 388 | * \param subject_name subject name to set |
| 389 | * |
| 390 | * \return 0 if subject name was parsed successfully, or |
| 391 | * a specific error code |
| 392 | */ |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 393 | int x509write_crt_set_subject_name( x509write_cert *ctx, |
| 394 | const char *subject_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 395 | |
| 396 | /** |
| 397 | * \brief Set the subject public key for the certificate |
| 398 | * |
| 399 | * \param ctx CRT context to use |
| 400 | * \param key public key to include |
| 401 | */ |
| 402 | void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key ); |
| 403 | |
| 404 | /** |
| 405 | * \brief Set the issuer key used for signing the certificate |
| 406 | * |
| 407 | * \param ctx CRT context to use |
| 408 | * \param key private key to sign with |
| 409 | */ |
| 410 | void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key ); |
| 411 | |
| 412 | /** |
| 413 | * \brief Set the MD algorithm to use for the signature |
| 414 | * (e.g. POLARSSL_MD_SHA1) |
| 415 | * |
| 416 | * \param ctx CRT context to use |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 417 | * \param md_alg MD algorithm to use |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 418 | */ |
| 419 | void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg ); |
| 420 | |
| 421 | /** |
| 422 | * \brief Generic function to add to or replace an extension in the |
| 423 | * CRT |
| 424 | * |
| 425 | * \param ctx CRT context to use |
| 426 | * \param oid OID of the extension |
| 427 | * \param oid_len length of the OID |
| 428 | * \param critical if the extension is critical (per the RFC's definition) |
| 429 | * \param val value of the extension OCTET STRING |
| 430 | * \param val_len length of the value data |
| 431 | * |
| 432 | * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 433 | */ |
| 434 | int x509write_crt_set_extension( x509write_cert *ctx, |
| 435 | const char *oid, size_t oid_len, |
| 436 | int critical, |
| 437 | const unsigned char *val, size_t val_len ); |
| 438 | |
| 439 | /** |
| 440 | * \brief Set the basicConstraints extension for a CRT |
| 441 | * |
| 442 | * \param ctx CRT context to use |
| 443 | * \param is_ca is this a CA certificate |
| 444 | * \param max_pathlen maximum length of certificate chains below this |
| 445 | * certificate (only for CA certificates, -1 is |
| 446 | * inlimited) |
| 447 | * |
| 448 | * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 449 | */ |
| 450 | int x509write_crt_set_basic_constraints( x509write_cert *ctx, |
| 451 | int is_ca, int max_pathlen ); |
| 452 | |
Manuel Pégourié-Gonnard | 3daaf3d | 2013-10-27 14:22:02 +0100 | [diff] [blame] | 453 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 454 | /** |
| 455 | * \brief Set the subjectKeyIdentifier extension for a CRT |
| 456 | * Requires that x509write_crt_set_subject_key() has been |
| 457 | * called before |
| 458 | * |
| 459 | * \param ctx CRT context to use |
| 460 | * |
| 461 | * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 462 | */ |
| 463 | int x509write_crt_set_subject_key_identifier( x509write_cert *ctx ); |
| 464 | |
| 465 | /** |
| 466 | * \brief Set the authorityKeyIdentifier extension for a CRT |
| 467 | * Requires that x509write_crt_set_issuer_key() has been |
| 468 | * called before |
| 469 | * |
| 470 | * \param ctx CRT context to use |
| 471 | * |
| 472 | * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 473 | */ |
| 474 | int x509write_crt_set_authority_key_identifier( x509write_cert *ctx ); |
Manuel Pégourié-Gonnard | 3daaf3d | 2013-10-27 14:22:02 +0100 | [diff] [blame] | 475 | #endif /* POLARSSL_SHA1_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 476 | |
| 477 | /** |
| 478 | * \brief Set the Key Usage Extension flags |
| 479 | * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN) |
| 480 | * |
| 481 | * \param ctx CRT context to use |
| 482 | * \param key_usage key usage flags to set |
| 483 | * |
| 484 | * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 485 | */ |
| 486 | int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage ); |
| 487 | |
| 488 | /** |
| 489 | * \brief Set the Netscape Cert Type flags |
| 490 | * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL) |
| 491 | * |
| 492 | * \param ctx CRT context to use |
| 493 | * \param ns_cert_type Netscape Cert Type flags to set |
| 494 | * |
| 495 | * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
| 496 | */ |
| 497 | int x509write_crt_set_ns_cert_type( x509write_cert *ctx, |
| 498 | unsigned char ns_cert_type ); |
| 499 | |
| 500 | /** |
| 501 | * \brief Free the contents of a CRT write context |
| 502 | * |
| 503 | * \param ctx CRT context to free |
| 504 | */ |
| 505 | void x509write_crt_free( x509write_cert *ctx ); |
| 506 | |
| 507 | /** |
| 508 | * \brief Write a built up certificate to a X509 DER structure |
| 509 | * Note: data is written at the end of the buffer! Use the |
| 510 | * return value to determine where you should start |
| 511 | * using the buffer |
| 512 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 513 | * \param ctx certificate to write away |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 514 | * \param buf buffer to write to |
| 515 | * \param size size of the buffer |
| 516 | * \param f_rng RNG function (for signature, see note) |
| 517 | * \param p_rng RNG parameter |
| 518 | * |
| 519 | * \return length of data written if successful, or a specific |
| 520 | * error code |
| 521 | * |
| 522 | * \note f_rng may be NULL if RSA is used for signature and the |
| 523 | * signature is made offline (otherwise f_rng is desirable |
| 524 | * for countermeasures against timing attacks). |
| 525 | * ECDSA signatures always require a non-NULL f_rng. |
| 526 | */ |
| 527 | int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size, |
| 528 | int (*f_rng)(void *, unsigned char *, size_t), |
| 529 | void *p_rng ); |
| 530 | |
| 531 | #if defined(POLARSSL_PEM_WRITE_C) |
| 532 | /** |
| 533 | * \brief Write a built up certificate to a X509 PEM string |
| 534 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 535 | * \param ctx certificate to write away |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 536 | * \param buf buffer to write to |
| 537 | * \param size size of the buffer |
| 538 | * \param f_rng RNG function (for signature, see note) |
| 539 | * \param p_rng RNG parameter |
| 540 | * |
| 541 | * \return 0 successful, or a specific error code |
| 542 | * |
| 543 | * \note f_rng may be NULL if RSA is used for signature and the |
| 544 | * signature is made offline (otherwise f_rng is desirable |
| 545 | * for countermeasures against timing attacks). |
| 546 | * ECDSA signatures always require a non-NULL f_rng. |
| 547 | */ |
| 548 | int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size, |
| 549 | int (*f_rng)(void *, unsigned char *, size_t), |
| 550 | void *p_rng ); |
| 551 | #endif /* POLARSSL_PEM_WRITE_C */ |
| 552 | #endif /* POLARSSL_X509_CRT_WRITE_C */ |
| 553 | |
| 554 | #ifdef __cplusplus |
| 555 | } |
| 556 | #endif |
| 557 | |
| 558 | #endif /* x509_crt.h */ |