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