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