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