Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file x509.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief X.509 certificate and private key decoding |
| 5 | * |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 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. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_X509_H |
| 28 | #define POLARSSL_X509_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
| 32 | #if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C) |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 33 | #include "asn1.h" |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 34 | #include "rsa.h" |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 35 | #include "ecp.h" |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 36 | #include "dhm.h" |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 37 | #include "md.h" |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 38 | #include "pk.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 40 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 41 | * \addtogroup x509_module |
| 42 | * \{ |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 43 | */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 45 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 46 | * \name X509 Error codes |
| 47 | * \{ |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 48 | */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 49 | #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */ |
| 50 | #define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */ |
| 51 | #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */ |
| 52 | #define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */ |
| 53 | #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */ |
| 54 | #define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */ |
| 55 | #define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */ |
| 56 | #define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */ |
| 57 | #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */ |
| 58 | #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */ |
| 59 | #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */ |
| 60 | #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */ |
| 61 | #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */ |
Manuel Pégourié-Gonnard | 5a9b82e | 2013-07-01 16:57:44 +0200 | [diff] [blame] | 62 | #define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA and EC are supported). */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 63 | #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */ |
| 64 | #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */ |
| 65 | #define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */ |
| 66 | #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */ |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 67 | #define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 68 | #define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */ |
| 69 | #define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */ |
| 70 | #define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */ |
Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 71 | #define POLARSSL_ERR_X509_PASSWORD_REQUIRED -0x2B80 /**< Private key password can't be empty. */ |
| 72 | #define POLARSSL_ERR_X509_PASSWORD_MISMATCH -0x2C00 /**< Given private key password does not allow for correct decryption. */ |
Manuel Pégourié-Gonnard | f838eed | 2013-07-02 14:56:43 +0200 | [diff] [blame] | 73 | #define POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE -0x2C80 /**< Elliptic curve is unsupported (only NIST curves are supported). */ |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 74 | /* \} name */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 75 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 76 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 77 | * \name X509 Verify codes |
| 78 | * \{ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 79 | */ |
Paul Bakker | cdf07e9 | 2011-01-30 17:05:13 +0000 | [diff] [blame] | 80 | #define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */ |
| 81 | #define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */ |
| 82 | #define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */ |
| 83 | #define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */ |
| 84 | #define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */ |
| 85 | #define BADCRL_EXPIRED 0x20 /**< CRL is expired. */ |
| 86 | #define BADCERT_MISSING 0x40 /**< Certificate was missing. */ |
| 87 | #define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */ |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 88 | #define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */ |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 89 | /* \} name */ |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 90 | /* \} addtogroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 93 | * X.509 v3 Key Usage Extension flags |
| 94 | */ |
| 95 | #define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */ |
| 96 | #define KU_NON_REPUDIATION (0x40) /* bit 1 */ |
| 97 | #define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */ |
| 98 | #define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */ |
| 99 | #define KU_KEY_AGREEMENT (0x08) /* bit 4 */ |
| 100 | #define KU_KEY_CERT_SIGN (0x04) /* bit 5 */ |
| 101 | #define KU_CRL_SIGN (0x02) /* bit 6 */ |
| 102 | |
| 103 | /* |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 104 | * Netscape certificate types |
| 105 | * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html) |
| 106 | */ |
| 107 | |
| 108 | #define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */ |
| 109 | #define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */ |
| 110 | #define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */ |
| 111 | #define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */ |
| 112 | #define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */ |
| 113 | #define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */ |
| 114 | #define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */ |
| 115 | #define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */ |
| 116 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 117 | /* |
| 118 | * X.509 extension types |
| 119 | */ |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 120 | #define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0) |
| 121 | #define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1) |
| 122 | #define EXT_KEY_USAGE (1 << 2) |
| 123 | #define EXT_CERTIFICATE_POLICIES (1 << 3) |
| 124 | #define EXT_POLICY_MAPPINGS (1 << 4) |
| 125 | #define EXT_SUBJECT_ALT_NAME (1 << 5) |
| 126 | #define EXT_ISSUER_ALT_NAME (1 << 6) |
| 127 | #define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7) |
| 128 | #define EXT_BASIC_CONSTRAINTS (1 << 8) |
| 129 | #define EXT_NAME_CONSTRAINTS (1 << 9) |
| 130 | #define EXT_POLICY_CONSTRAINTS (1 << 10) |
| 131 | #define EXT_EXTENDED_KEY_USAGE (1 << 11) |
| 132 | #define EXT_CRL_DISTRIBUTION_POINTS (1 << 12) |
| 133 | #define EXT_INIHIBIT_ANYPOLICY (1 << 13) |
| 134 | #define EXT_FRESHEST_CRL (1 << 14) |
| 135 | |
| 136 | #define EXT_NS_CERT_TYPE (1 << 16) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 138 | /* |
| 139 | * Storage format identifiers |
| 140 | * Recognized formats: PEM and DER |
| 141 | */ |
| 142 | #define X509_FORMAT_DER 1 |
| 143 | #define X509_FORMAT_PEM 2 |
| 144 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 145 | #ifdef __cplusplus |
| 146 | extern "C" { |
| 147 | #endif |
| 148 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 149 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 150 | * \addtogroup x509_module |
| 151 | * \{ */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 152 | |
| 153 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 154 | * \name Structures for parsing X.509 certificates and CRLs |
| 155 | * \{ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 156 | */ |
| 157 | |
| 158 | /** |
| 159 | * Type-length-value structure that allows for ASN1 using DER. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | */ |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 161 | typedef asn1_buf x509_buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 163 | /** |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 164 | * Container for ASN1 bit strings. |
| 165 | */ |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 166 | typedef asn1_bitstring x509_bitstring; |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 169 | * Container for ASN1 named information objects. |
| 170 | * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.). |
| 171 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | typedef struct _x509_name |
| 173 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 174 | x509_buf oid; /**< The object identifier. */ |
| 175 | x509_buf val; /**< The named value. */ |
| 176 | struct _x509_name *next; /**< The next named information object. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | } |
| 178 | x509_name; |
| 179 | |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Container for a sequence of ASN.1 items |
| 182 | */ |
Paul Bakker | efc3029 | 2011-11-10 14:43:23 +0000 | [diff] [blame] | 183 | typedef asn1_sequence x509_sequence; |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 184 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 185 | /** Container for date and time (precision in seconds). */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | typedef struct _x509_time |
| 187 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 188 | int year, mon, day; /**< Date. */ |
| 189 | int hour, min, sec; /**< Time. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | } |
| 191 | x509_time; |
| 192 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 193 | /** |
| 194 | * Container for an X.509 certificate. The certificate may be chained. |
| 195 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 | typedef struct _x509_cert |
| 197 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 198 | x509_buf raw; /**< The raw certificate data (DER). */ |
| 199 | x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 201 | int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */ |
| 202 | x509_buf serial; /**< Unique id for certificate issued by a specific CA. */ |
| 203 | x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 205 | x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */ |
| 206 | x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 207 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 208 | x509_name issuer; /**< The parsed issuer data (named information object). */ |
| 209 | x509_name subject; /**< The parsed subject data (named information object). */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 211 | x509_time valid_from; /**< Start time of certificate validity. */ |
| 212 | x509_time valid_to; /**< End time of certificate validity. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | |
Manuel Pégourié-Gonnard | 674b224 | 2013-07-10 14:32:58 +0200 | [diff] [blame] | 214 | pk_context pk; /**< Container for the public key context. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 216 | x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */ |
| 217 | x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */ |
| 218 | x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */ |
Paul Bakker | a8cd239 | 2012-02-11 16:09:32 +0000 | [diff] [blame] | 219 | x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 221 | int ext_types; /**< Bit string containing detected and parsed extensions */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 222 | int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */ |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 223 | 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+ */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 225 | unsigned char key_usage; /**< Optional key usage extension value: See the values below */ |
| 226 | |
| 227 | x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */ |
| 228 | |
| 229 | unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */ |
| 230 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 231 | x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */ |
| 232 | x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */ |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 233 | md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */ |
| 234 | pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 236 | struct _x509_cert *next; /**< Next certificate in the CA-chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 237 | } |
| 238 | x509_cert; |
| 239 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 240 | /** |
| 241 | * Certificate revocation list entry. |
| 242 | * Contains the CA-specific serial numbers and revocation dates. |
| 243 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 244 | typedef struct _x509_crl_entry |
| 245 | { |
| 246 | x509_buf raw; |
| 247 | |
| 248 | x509_buf serial; |
| 249 | |
| 250 | x509_time revocation_date; |
| 251 | |
| 252 | x509_buf entry_ext; |
| 253 | |
| 254 | struct _x509_crl_entry *next; |
| 255 | } |
| 256 | x509_crl_entry; |
| 257 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 258 | /** |
| 259 | * Certificate revocation list structure. |
| 260 | * Every CRL may have multiple entries. |
| 261 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 262 | typedef struct _x509_crl |
| 263 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 264 | x509_buf raw; /**< The raw certificate data (DER). */ |
| 265 | x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 266 | |
| 267 | int version; |
| 268 | x509_buf sig_oid1; |
| 269 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 270 | x509_buf issuer_raw; /**< The raw issuer data (DER). */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 271 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 272 | x509_name issuer; /**< The parsed issuer data (named information object). */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 273 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 274 | x509_time this_update; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 275 | x509_time next_update; |
| 276 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 277 | x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 278 | |
| 279 | x509_buf crl_ext; |
| 280 | |
| 281 | x509_buf sig_oid2; |
| 282 | x509_buf sig; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 283 | md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */ |
| 284 | pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 285 | |
| 286 | struct _x509_crl *next; |
| 287 | } |
| 288 | x509_crl; |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 289 | /** \} name Structures for parsing X.509 certificates and CRLs */ |
| 290 | /** \} addtogroup x509_module */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 291 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 292 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 293 | * \name Structures for writing X.509 certificates. |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 294 | * XvP: commented out as they are not used. |
| 295 | * - <tt>typedef struct _x509_node x509_node;</tt> |
| 296 | * - <tt>typedef struct _x509_raw x509_raw;</tt> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 298 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | typedef struct _x509_node |
| 300 | { |
| 301 | unsigned char *data; |
| 302 | unsigned char *p; |
| 303 | unsigned char *end; |
| 304 | |
| 305 | size_t len; |
| 306 | } |
| 307 | x509_node; |
| 308 | |
| 309 | typedef struct _x509_raw |
| 310 | { |
| 311 | x509_node raw; |
| 312 | x509_node tbs; |
| 313 | |
| 314 | x509_node version; |
| 315 | x509_node serial; |
| 316 | x509_node tbs_signalg; |
| 317 | x509_node issuer; |
| 318 | x509_node validity; |
| 319 | x509_node subject; |
| 320 | x509_node subpubkey; |
| 321 | |
| 322 | x509_node signalg; |
| 323 | x509_node sign; |
| 324 | } |
| 325 | x509_raw; |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 326 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 327 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 328 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 329 | * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key |
| 330 | * \{ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 331 | */ |
| 332 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 333 | /** \ingroup x509_module */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 334 | /** |
Paul Bakker | 89ecb2d | 2013-06-24 19:06:15 +0200 | [diff] [blame] | 335 | * \brief Parse a single DER formatted certificate and add it |
| 336 | * to the chained list. |
| 337 | * |
| 338 | * \param chain points to the start of the chain |
| 339 | * \param buf buffer holding the certificate DER data |
| 340 | * \param buflen size of the buffer |
| 341 | * |
| 342 | * \return 0 if successful, or a specific X509 or PEM error code |
| 343 | */ |
| 344 | int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen ); |
| 345 | |
| 346 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 347 | * \brief Parse one or more certificates and add them |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 348 | * to the chained list. Parses permissively. If some |
| 349 | * certificates can be parsed, the result is the number |
| 350 | * of failed certificates it encountered. If none complete |
| 351 | * correctly, the first error is returned. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | * |
| 353 | * \param chain points to the start of the chain |
| 354 | * \param buf buffer holding the certificate data |
| 355 | * \param buflen size of the buffer |
| 356 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 357 | * \return 0 if all certificates parsed successfully, a positive number |
| 358 | * if partly successful or a specific X509 or PEM error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 359 | */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 360 | int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 362 | /** \ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | /** |
| 364 | * \brief Load one or more certificates and add them |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 365 | * to the chained list. Parses permissively. If some |
| 366 | * certificates can be parsed, the result is the number |
| 367 | * of failed certificates it encountered. If none complete |
| 368 | * correctly, the first error is returned. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 369 | * |
| 370 | * \param chain points to the start of the chain |
| 371 | * \param path filename to read the certificates from |
| 372 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 373 | * \return 0 if all certificates parsed successfully, a positive number |
| 374 | * if partly successful or a specific X509 or PEM error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 375 | */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 376 | int x509parse_crtfile( x509_cert *chain, const char *path ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 378 | /** \ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 379 | /** |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 380 | * \brief Load one or more certificate files from a path and add them |
| 381 | * to the chained list. Parses permissively. If some |
| 382 | * certificates can be parsed, the result is the number |
| 383 | * of failed certificates it encountered. If none complete |
| 384 | * correctly, the first error is returned. |
| 385 | * |
| 386 | * \param chain points to the start of the chain |
| 387 | * \param path directory / folder to read the certificate files from |
| 388 | * |
| 389 | * \return 0 if all certificates parsed successfully, a positive number |
| 390 | * if partly successful or a specific X509 or PEM error code |
| 391 | */ |
| 392 | int x509parse_crtpath( x509_cert *chain, const char *path ); |
| 393 | |
| 394 | /** \ingroup x509_module */ |
| 395 | /** |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 396 | * \brief Parse one or more CRLs and add them |
| 397 | * to the chained list |
| 398 | * |
| 399 | * \param chain points to the start of the chain |
| 400 | * \param buf buffer holding the CRL data |
| 401 | * \param buflen size of the buffer |
| 402 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 403 | * \return 0 if successful, or a specific X509 or PEM error code |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 404 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 405 | int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 406 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 407 | /** \ingroup x509_module */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 408 | /** |
| 409 | * \brief Load one or more CRLs and add them |
| 410 | * to the chained list |
| 411 | * |
| 412 | * \param chain points to the start of the chain |
| 413 | * \param path filename to read the CRLs from |
| 414 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 415 | * \return 0 if successful, or a specific X509 or PEM error code |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 416 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 417 | int x509parse_crlfile( x509_crl *chain, const char *path ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 418 | |
Manuel Pégourié-Gonnard | ab2d983 | 2013-07-11 16:17:23 +0200 | [diff] [blame^] | 419 | #if defined(POLARSSL_RSA_C) |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 420 | /** \ingroup x509_module */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 421 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 422 | * \brief Parse a private RSA key |
| 423 | * |
| 424 | * \param rsa RSA context to be initialized |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 425 | * \param key input buffer |
| 426 | * \param keylen size of the buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 427 | * \param pwd password for decryption (optional) |
| 428 | * \param pwdlen size of the password |
| 429 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 430 | * \return 0 if successful, or a specific X509 or PEM error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 431 | */ |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 432 | int x509parse_key_rsa( rsa_context *rsa, |
| 433 | const unsigned char *key, size_t keylen, |
| 434 | const unsigned char *pwd, size_t pwdlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 435 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 436 | /** \ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | /** |
| 438 | * \brief Load and parse a private RSA key |
| 439 | * |
| 440 | * \param rsa RSA context to be initialized |
| 441 | * \param path filename to read the private key from |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 442 | * \param password password to decrypt the file (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 443 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 444 | * \return 0 if successful, or a specific X509 or PEM error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 445 | */ |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 446 | int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, |
| 447 | const char *password ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 448 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 449 | /** \ingroup x509_module */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 450 | /** |
Paul Bakker | 917e754 | 2011-03-25 14:23:36 +0000 | [diff] [blame] | 451 | * \brief Parse a public RSA key |
| 452 | * |
| 453 | * \param rsa RSA context to be initialized |
| 454 | * \param key input buffer |
| 455 | * \param keylen size of the buffer |
| 456 | * |
| 457 | * \return 0 if successful, or a specific X509 or PEM error code |
| 458 | */ |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 459 | int x509parse_public_key_rsa( rsa_context *rsa, |
| 460 | const unsigned char *key, size_t keylen ); |
Paul Bakker | 917e754 | 2011-03-25 14:23:36 +0000 | [diff] [blame] | 461 | |
| 462 | /** \ingroup x509_module */ |
| 463 | /** |
| 464 | * \brief Load and parse a public RSA key |
| 465 | * |
| 466 | * \param rsa RSA context to be initialized |
| 467 | * \param path filename to read the private key from |
| 468 | * |
| 469 | * \return 0 if successful, or a specific X509 or PEM error code |
| 470 | */ |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 471 | int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path ); |
Manuel Pégourié-Gonnard | ab2d983 | 2013-07-11 16:17:23 +0200 | [diff] [blame^] | 472 | #endif /* POLARSSL_RSA_C */ |
Paul Bakker | 917e754 | 2011-03-25 14:23:36 +0000 | [diff] [blame] | 473 | |
| 474 | /** \ingroup x509_module */ |
| 475 | /** |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 476 | * \brief Parse a private key |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 477 | * |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 478 | * \param ctx key to be initialized |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 479 | * \param key input buffer |
| 480 | * \param keylen size of the buffer |
| 481 | * \param pwd password for decryption (optional) |
| 482 | * \param pwdlen size of the password |
| 483 | * |
| 484 | * \return 0 if successful, or a specific X509 or PEM error code |
| 485 | */ |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 486 | int x509parse_key( pk_context *ctx, |
| 487 | const unsigned char *key, size_t keylen, |
| 488 | const unsigned char *pwd, size_t pwdlen ); |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 489 | |
| 490 | /** \ingroup x509_module */ |
| 491 | /** |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 492 | * \brief Load and parse a private key |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 493 | * |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 494 | * \param ctx key to be initialized |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 495 | * \param path filename to read the private key from |
| 496 | * \param password password to decrypt the file (can be NULL) |
| 497 | * |
| 498 | * \return 0 if successful, or a specific X509 or PEM error code |
| 499 | */ |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 500 | int x509parse_keyfile( pk_context *ctx, |
| 501 | const char *path, const char *password ); |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 502 | |
| 503 | /** \ingroup x509_module */ |
| 504 | /** |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 505 | * \brief Parse a public key |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 506 | * |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 507 | * \param ctx key to be initialized |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 508 | * \param key input buffer |
| 509 | * \param keylen size of the buffer |
| 510 | * |
| 511 | * \return 0 if successful, or a specific X509 or PEM error code |
| 512 | */ |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 513 | int x509parse_public_key( pk_context *ctx, |
| 514 | const unsigned char *key, size_t keylen ); |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 515 | |
| 516 | /** \ingroup x509_module */ |
| 517 | /** |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 518 | * \brief Load and parse a public key |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 519 | * |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 520 | * \param ctx key to be initialized |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 521 | * \param path filename to read the private key from |
| 522 | * |
| 523 | * \return 0 if successful, or a specific X509 or PEM error code |
| 524 | */ |
Manuel Pégourié-Gonnard | 8838099 | 2013-07-04 14:09:57 +0200 | [diff] [blame] | 525 | int x509parse_public_keyfile( pk_context *ctx, const char *path ); |
Manuel Pégourié-Gonnard | 26833c2 | 2013-06-27 11:27:58 +0200 | [diff] [blame] | 526 | |
| 527 | /** \ingroup x509_module */ |
| 528 | /** |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 529 | * \brief Parse DHM parameters |
| 530 | * |
| 531 | * \param dhm DHM context to be initialized |
| 532 | * \param dhmin input buffer |
| 533 | * \param dhminlen size of the buffer |
| 534 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 535 | * \return 0 if successful, or a specific X509 or PEM error code |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 536 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 537 | int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 538 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 539 | /** \ingroup x509_module */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 540 | /** |
| 541 | * \brief Load and parse DHM parameters |
| 542 | * |
| 543 | * \param dhm DHM context to be initialized |
| 544 | * \param path filename to read the DHM Parameters from |
| 545 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 546 | * \return 0 if successful, or a specific X509 or PEM error code |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 547 | */ |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 548 | int x509parse_dhmfile( dhm_context *dhm, const char *path ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 549 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 550 | /** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 551 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | /** |
| 553 | * \brief Store the certificate DN in printable form into buf; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 554 | * no more than size characters will be written. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 555 | * |
| 556 | * \param buf Buffer to write to |
| 557 | * \param size Maximum size of buffer |
| 558 | * \param dn The X509 name to represent |
| 559 | * |
| 560 | * \return The amount of data written to the buffer, or -1 in |
| 561 | * case of an error. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 562 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 563 | int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 564 | |
| 565 | /** |
Paul Bakker | dd47699 | 2011-01-16 21:34:59 +0000 | [diff] [blame] | 566 | * \brief Store the certificate serial in printable form into buf; |
| 567 | * no more than size characters will be written. |
| 568 | * |
| 569 | * \param buf Buffer to write to |
| 570 | * \param size Maximum size of buffer |
| 571 | * \param serial The X509 serial to represent |
| 572 | * |
| 573 | * \return The amount of data written to the buffer, or -1 in |
| 574 | * case of an error. |
| 575 | */ |
| 576 | int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ); |
| 577 | |
| 578 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | * \brief Returns an informational string about the |
| 580 | * certificate. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 581 | * |
| 582 | * \param buf Buffer to write to |
| 583 | * \param size Maximum size of buffer |
| 584 | * \param prefix A line prefix |
| 585 | * \param crt The X509 certificate to represent |
| 586 | * |
| 587 | * \return The amount of data written to the buffer, or -1 in |
| 588 | * case of an error. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 589 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 590 | int x509parse_cert_info( char *buf, size_t size, const char *prefix, |
| 591 | const x509_cert *crt ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 592 | |
| 593 | /** |
| 594 | * \brief Returns an informational string about the |
| 595 | * CRL. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 596 | * |
| 597 | * \param buf Buffer to write to |
| 598 | * \param size Maximum size of buffer |
| 599 | * \param prefix A line prefix |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 600 | * \param crl The X509 CRL to represent |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 601 | * |
| 602 | * \return The amount of data written to the buffer, or -1 in |
| 603 | * case of an error. |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 604 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 605 | int x509parse_crl_info( char *buf, size_t size, const char *prefix, |
| 606 | const x509_crl *crl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 607 | |
| 608 | /** |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 609 | * \brief Give an known OID, return its descriptive string. |
| 610 | * |
| 611 | * \param oid buffer containing the oid |
| 612 | * |
| 613 | * \return Return a string if the OID is known, |
| 614 | * or NULL otherwise. |
| 615 | */ |
| 616 | const char *x509_oid_get_description( x509_buf *oid ); |
| 617 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 618 | /** |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 619 | * \brief Give an OID, return a string version of its OID number. |
Paul Bakker | d5c2b54 | 2013-04-07 22:34:26 +0200 | [diff] [blame] | 620 | * (Deprecated. Use oid_get_numeric_string() instead) |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 621 | * |
| 622 | * \param buf Buffer to write to |
| 623 | * \param size Maximum size of buffer |
| 624 | * \param oid Buffer containing the OID |
| 625 | * |
| 626 | * \return The amount of data written to the buffer, or -1 in |
| 627 | * case of an error. |
| 628 | */ |
| 629 | int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid ); |
| 630 | |
| 631 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 632 | * \brief Check a given x509_time against the system time and check |
| 633 | * if it is valid. |
| 634 | * |
| 635 | * \param time x509_time to check |
| 636 | * |
| 637 | * \return Return 0 if the x509_time is still valid, |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 638 | * or 1 otherwise. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 639 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 640 | int x509parse_time_expired( const x509_time *time ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 641 | |
| 642 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 643 | * \name Functions to verify a certificate |
| 644 | * \{ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 645 | */ |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 646 | /** \ingroup x509_module */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 647 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 648 | * \brief Verify the certificate signature |
| 649 | * |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 650 | * The verify callback is a user-supplied callback that |
| 651 | * can clear / modify / add flags for a certificate. If set, |
| 652 | * the verification callback is called for each |
| 653 | * certificate in the chain (from the trust-ca down to the |
| 654 | * presented crt). The parameters for the callback are: |
| 655 | * (void *parameter, x509_cert *crt, int certificate_depth, |
| 656 | * int *flags). With the flags representing current flags for |
| 657 | * that specific certificate and the certificate depth from |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 658 | * the bottom (Peer cert depth = 0). |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 659 | * |
| 660 | * All flags left after returning from the callback |
| 661 | * are also returned to the application. The function should |
| 662 | * return 0 for anything but a fatal error. |
| 663 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 664 | * \param crt a certificate to be verified |
| 665 | * \param trust_ca the trusted CA chain |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 666 | * \param ca_crl the CRL chain for trusted CA's |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 667 | * \param cn expected Common Name (can be set to |
| 668 | * NULL if the CN must not be verified) |
| 669 | * \param flags result of the verification |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 670 | * \param f_vrfy verification function |
| 671 | * \param p_vrfy verification parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 673 | * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 674 | * in which case *flags will have one or more of |
| 675 | * the following values set: |
| 676 | * BADCERT_EXPIRED -- |
| 677 | * BADCERT_REVOKED -- |
| 678 | * BADCERT_CN_MISMATCH -- |
| 679 | * BADCERT_NOT_TRUSTED |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 680 | * or another error in case of a fatal error encountered |
| 681 | * during the verification process. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 682 | */ |
| 683 | int x509parse_verify( x509_cert *crt, |
| 684 | x509_cert *trust_ca, |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 685 | x509_crl *ca_crl, |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 686 | const char *cn, int *flags, |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 687 | int (*f_vrfy)(void *, x509_cert *, int, int *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 688 | void *p_vrfy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 689 | |
Paul Bakker | 74111d3 | 2011-01-15 16:57:55 +0000 | [diff] [blame] | 690 | /** |
| 691 | * \brief Verify the certificate signature |
| 692 | * |
| 693 | * \param crt a certificate to be verified |
| 694 | * \param crl the CRL to verify against |
| 695 | * |
| 696 | * \return 1 if the certificate is revoked, 0 otherwise |
| 697 | * |
| 698 | */ |
| 699 | int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ); |
| 700 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 701 | /** \} name Functions to verify a certificate */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 702 | |
| 703 | |
| 704 | |
| 705 | /** |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 706 | * \name Functions to clear a certificate, CRL or private RSA key |
| 707 | * \{ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 708 | */ |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 709 | /** \ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 710 | /** |
| 711 | * \brief Unallocate all certificate data |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 712 | * |
| 713 | * \param crt Certificate chain to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 714 | */ |
| 715 | void x509_free( x509_cert *crt ); |
| 716 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 717 | /** \ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 718 | /** |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 719 | * \brief Unallocate all CRL data |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 720 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 721 | * \param crl CRL chain to free |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 722 | */ |
| 723 | void x509_crl_free( x509_crl *crl ); |
| 724 | |
Paul Bakker | 0f5f72e | 2011-01-18 14:58:55 +0000 | [diff] [blame] | 725 | /** \} name Functions to clear a certificate, CRL or private RSA key */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 726 | |
| 727 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 728 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 729 | * \brief Checkup routine |
| 730 | * |
| 731 | * \return 0 if successful, or 1 if the test failed |
| 732 | */ |
| 733 | int x509_self_test( int verbose ); |
| 734 | |
| 735 | #ifdef __cplusplus |
| 736 | } |
| 737 | #endif |
| 738 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 739 | #endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 740 | #endif /* x509.h */ |