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