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 | /** |
| 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 | /** |
| 39 | * @name ASN1 Error codes |
| 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. |
| 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) */ |
| 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 | /** |
| 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. */ |
| 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 | /** |
| 85 | * @name X509 Verify codes |
| 86 | * @{ |
| 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. */ |
| 94 | /* @} name */ |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * @name DER constants |
| 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. |
| 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 | 37ca75d | 2011-01-06 12:28:03 +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 | |
| 147 | #define OID_X520 "\x55\x04" |
| 148 | #define OID_CN "\x55\x04\x03" |
| 149 | #define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01" |
| 150 | #define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01" |
| 151 | #define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05" |
| 152 | #define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09" |
| 153 | #define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01" |
| 154 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 155 | /** |
| 156 | * @addtogroup x509_module |
| 157 | * @{ */ |
| 158 | |
| 159 | /** |
| 160 | * @name Structures for parsing X.509 certificates and CRLs |
| 161 | * @{ |
| 162 | */ |
| 163 | |
| 164 | /** |
| 165 | * Type-length-value structure that allows for ASN1 using DER. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | */ |
| 167 | typedef struct _x509_buf |
| 168 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 169 | int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */ |
| 170 | int len; /**< ASN1 length, e.g. in octets. */ |
| 171 | unsigned char *p; /**< ASN1 data, e.g. in ASCII. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | } |
| 173 | x509_buf; |
| 174 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 175 | /** |
| 176 | * Container for ASN1 named information objects. |
| 177 | * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.). |
| 178 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | typedef struct _x509_name |
| 180 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 181 | x509_buf oid; /**< The object identifier. */ |
| 182 | x509_buf val; /**< The named value. */ |
| 183 | struct _x509_name *next; /**< The next named information object. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | } |
| 185 | x509_name; |
| 186 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 187 | /** Container for date and time (precision in seconds). */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | typedef struct _x509_time |
| 189 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 190 | int year, mon, day; /**< Date. */ |
| 191 | int hour, min, sec; /**< Time. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | } |
| 193 | x509_time; |
| 194 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 195 | /** |
| 196 | * Container for an X.509 certificate. The certificate may be chained. |
| 197 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | typedef struct _x509_cert |
| 199 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 200 | x509_buf raw; /**< The raw certificate data (DER). */ |
| 201 | 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] | 202 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 203 | int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */ |
| 204 | x509_buf serial; /**< Unique id for certificate issued by a specific CA. */ |
| 205 | x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 207 | x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */ |
| 208 | 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] | 209 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 210 | x509_name issuer; /**< The parsed issuer data (named information object). */ |
| 211 | x509_name subject; /**< The parsed subject data (named information object). */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 213 | x509_time valid_from; /**< Start time of certificate validity. */ |
| 214 | x509_time valid_to; /**< End time of certificate validity. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 216 | x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */ |
| 217 | 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] | 218 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 219 | x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */ |
| 220 | x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */ |
| 221 | 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] | 222 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 223 | int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */ |
| 224 | 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] | 225 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 226 | x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */ |
| 227 | x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */ |
| 228 | 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] | 229 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 230 | struct _x509_cert *next; /**< Next certificate in the CA-chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | } |
| 232 | x509_cert; |
| 233 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 234 | /** |
| 235 | * Certificate revocation list entry. |
| 236 | * Contains the CA-specific serial numbers and revocation dates. |
| 237 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 238 | typedef struct _x509_crl_entry |
| 239 | { |
| 240 | x509_buf raw; |
| 241 | |
| 242 | x509_buf serial; |
| 243 | |
| 244 | x509_time revocation_date; |
| 245 | |
| 246 | x509_buf entry_ext; |
| 247 | |
| 248 | struct _x509_crl_entry *next; |
| 249 | } |
| 250 | x509_crl_entry; |
| 251 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 252 | /** |
| 253 | * Certificate revocation list structure. |
| 254 | * Every CRL may have multiple entries. |
| 255 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 256 | typedef struct _x509_crl |
| 257 | { |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 258 | x509_buf raw; /**< The raw certificate data (DER). */ |
| 259 | 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] | 260 | |
| 261 | int version; |
| 262 | x509_buf sig_oid1; |
| 263 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 264 | x509_buf issuer_raw; /**< The raw issuer data (DER). */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 265 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 266 | x509_name issuer; /**< The parsed issuer data (named information object). */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 267 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 268 | x509_time this_update; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 269 | x509_time next_update; |
| 270 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 271 | 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] | 272 | |
| 273 | x509_buf crl_ext; |
| 274 | |
| 275 | x509_buf sig_oid2; |
| 276 | x509_buf sig; |
Paul Bakker | 27d6616 | 2010-03-17 06:56:01 +0000 | [diff] [blame] | 277 | int sig_alg; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 278 | |
| 279 | struct _x509_crl *next; |
| 280 | } |
| 281 | x509_crl; |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 282 | /** @} name Structures for parsing X.509 certificates and CRLs */ |
| 283 | /** @} addtogroup x509_module */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 284 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 285 | /** |
| 286 | * @name Structures for writing X.509 certificates. |
| 287 | * XvP: commented out as they are not used. |
| 288 | * - <tt>typedef struct _x509_node x509_node;</tt> |
| 289 | * - <tt>typedef struct _x509_raw x509_raw;</tt> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 291 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | typedef struct _x509_node |
| 293 | { |
| 294 | unsigned char *data; |
| 295 | unsigned char *p; |
| 296 | unsigned char *end; |
| 297 | |
| 298 | size_t len; |
| 299 | } |
| 300 | x509_node; |
| 301 | |
| 302 | typedef struct _x509_raw |
| 303 | { |
| 304 | x509_node raw; |
| 305 | x509_node tbs; |
| 306 | |
| 307 | x509_node version; |
| 308 | x509_node serial; |
| 309 | x509_node tbs_signalg; |
| 310 | x509_node issuer; |
| 311 | x509_node validity; |
| 312 | x509_node subject; |
| 313 | x509_node subpubkey; |
| 314 | |
| 315 | x509_node signalg; |
| 316 | x509_node sign; |
| 317 | } |
| 318 | x509_raw; |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 319 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | |
| 321 | #ifdef __cplusplus |
| 322 | extern "C" { |
| 323 | #endif |
| 324 | |
| 325 | /** |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 326 | * @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] | 327 | * @{ |
| 328 | */ |
| 329 | |
| 330 | /** @ingroup x509_module */ |
| 331 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 332 | * \brief Parse one or more certificates and add them |
| 333 | * to the chained list |
| 334 | * |
| 335 | * \param chain points to the start of the chain |
| 336 | * \param buf buffer holding the certificate data |
| 337 | * \param buflen size of the buffer |
| 338 | * |
| 339 | * \return 0 if successful, or a specific X509 error code |
| 340 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 341 | int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen ); |
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 | /** @ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | /** |
| 345 | * \brief Load one or more certificates and add them |
| 346 | * to the chained list |
| 347 | * |
| 348 | * \param chain points to the start of the chain |
| 349 | * \param path filename to read the certificates from |
| 350 | * |
| 351 | * \return 0 if successful, or a specific X509 error code |
| 352 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 353 | int x509parse_crtfile( x509_cert *chain, const char *path ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 354 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 355 | /** @ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | /** |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 357 | * \brief Parse one or more CRLs and add them |
| 358 | * to the chained list |
| 359 | * |
| 360 | * \param chain points to the start of the chain |
| 361 | * \param buf buffer holding the CRL data |
| 362 | * \param buflen size of the buffer |
| 363 | * |
| 364 | * \return 0 if successful, or a specific X509 error code |
| 365 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 366 | int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 367 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 368 | /** @ingroup x509_module */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 369 | /** |
| 370 | * \brief Load one or more CRLs and add them |
| 371 | * to the chained list |
| 372 | * |
| 373 | * \param chain points to the start of the chain |
| 374 | * \param path filename to read the CRLs from |
| 375 | * |
| 376 | * \return 0 if successful, or a specific X509 error code |
| 377 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 378 | int x509parse_crlfile( x509_crl *chain, const char *path ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 379 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 380 | /** @ingroup x509_module */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 381 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 382 | * \brief Parse a private RSA key |
| 383 | * |
| 384 | * \param rsa RSA context to be initialized |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 385 | * \param key input buffer |
| 386 | * \param keylen size of the buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 387 | * \param pwd password for decryption (optional) |
| 388 | * \param pwdlen size of the password |
| 389 | * |
| 390 | * \return 0 if successful, or a specific X509 error code |
| 391 | */ |
| 392 | int x509parse_key( rsa_context *rsa, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 393 | const unsigned char *key, int keylen, |
| 394 | const unsigned char *pwd, int pwdlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 395 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 396 | /** @ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 397 | /** |
| 398 | * \brief Load and parse a private RSA key |
| 399 | * |
| 400 | * \param rsa RSA context to be initialized |
| 401 | * \param path filename to read the private key from |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 402 | * \param password password to decrypt the file (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 403 | * |
| 404 | * \return 0 if successful, or a specific X509 error code |
| 405 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 406 | int x509parse_keyfile( rsa_context *rsa, const char *path, |
| 407 | const char *password ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 408 | |
| 409 | /** @ingroup x509_module */ |
| 410 | /** |
| 411 | * \brief Parse DHM parameters |
| 412 | * |
| 413 | * \param dhm DHM context to be initialized |
| 414 | * \param dhmin input buffer |
| 415 | * \param dhminlen size of the buffer |
| 416 | * |
| 417 | * \return 0 if successful, or a specific X509 error code |
| 418 | */ |
| 419 | int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, int dhminlen ); |
| 420 | |
| 421 | /** @ingroup x509_module */ |
| 422 | /** |
| 423 | * \brief Load and parse DHM parameters |
| 424 | * |
| 425 | * \param dhm DHM context to be initialized |
| 426 | * \param path filename to read the DHM Parameters from |
| 427 | * |
| 428 | * \return 0 if successful, or a specific X509 error code |
| 429 | */ |
| 430 | int x509parse_dhmfile( dhm_context *rsa, const char *path ); |
| 431 | |
| 432 | /** @} 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] | 433 | |
| 434 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * \brief Store the certificate DN in printable form into buf; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 438 | * no more than size characters will be written. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 439 | * |
| 440 | * \param buf Buffer to write to |
| 441 | * \param size Maximum size of buffer |
| 442 | * \param dn The X509 name to represent |
| 443 | * |
| 444 | * \return The amount of data written to the buffer, or -1 in |
| 445 | * case of an error. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 446 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 447 | 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] | 448 | |
| 449 | /** |
| 450 | * \brief Returns an informational string about the |
| 451 | * certificate. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 452 | * |
| 453 | * \param buf Buffer to write to |
| 454 | * \param size Maximum size of buffer |
| 455 | * \param prefix A line prefix |
| 456 | * \param crt The X509 certificate to represent |
| 457 | * |
| 458 | * \return The amount of data written to the buffer, or -1 in |
| 459 | * case of an error. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 460 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 461 | int x509parse_cert_info( char *buf, size_t size, const char *prefix, |
| 462 | const x509_cert *crt ); |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * \brief Returns an informational string about the |
| 466 | * CRL. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 467 | * |
| 468 | * \param buf Buffer to write to |
| 469 | * \param size Maximum size of buffer |
| 470 | * \param prefix A line prefix |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 471 | * \param crl The X509 CRL to represent |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 472 | * |
| 473 | * \return The amount of data written to the buffer, or -1 in |
| 474 | * case of an error. |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 475 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 476 | int x509parse_crl_info( char *buf, size_t size, const char *prefix, |
| 477 | const x509_crl *crl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 478 | |
| 479 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 480 | * \brief Check a given x509_time against the system time and check |
| 481 | * if it is valid. |
| 482 | * |
| 483 | * \param time x509_time to check |
| 484 | * |
| 485 | * \return Return 0 if the x509_time is still valid, |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 486 | * or 1 otherwise. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 488 | int x509parse_time_expired( const x509_time *time ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 489 | |
| 490 | /** |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 491 | * @name Functions to verify a certificate |
| 492 | * @{ |
| 493 | */ |
| 494 | /** @ingroup x509_module */ |
| 495 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 496 | * \brief Verify the certificate signature |
| 497 | * |
| 498 | * \param crt a certificate to be verified |
| 499 | * \param trust_ca the trusted CA chain |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 500 | * \param ca_crl the CRL chain for trusted CA's |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 501 | * \param cn expected Common Name (can be set to |
| 502 | * NULL if the CN must not be verified) |
| 503 | * \param flags result of the verification |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame^] | 504 | * \param f_vrfy verification function |
| 505 | * \param p_vrfy verification parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 506 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 507 | * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 508 | * in which case *flags will have one or more of |
| 509 | * the following values set: |
| 510 | * BADCERT_EXPIRED -- |
| 511 | * BADCERT_REVOKED -- |
| 512 | * BADCERT_CN_MISMATCH -- |
| 513 | * BADCERT_NOT_TRUSTED |
| 514 | * |
| 515 | * \note TODO: add two arguments, depth and crl |
| 516 | */ |
| 517 | int x509parse_verify( x509_cert *crt, |
| 518 | x509_cert *trust_ca, |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 519 | x509_crl *ca_crl, |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame^] | 520 | const char *cn, int *flags, |
| 521 | int (*f_vrfy)(void *, x509_cert *, int, int), |
| 522 | void *p_vrfy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 523 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 524 | /** @} name Functions to verify a certificate */ |
| 525 | |
| 526 | |
| 527 | |
| 528 | /** |
| 529 | * @name Functions to clear a certificate, CRL or private RSA key |
| 530 | * @{ |
| 531 | */ |
| 532 | /** @ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 | /** |
| 534 | * \brief Unallocate all certificate data |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 535 | * |
| 536 | * \param crt Certificate chain to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 537 | */ |
| 538 | void x509_free( x509_cert *crt ); |
| 539 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 540 | /** @ingroup x509_module */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 541 | /** |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 542 | * \brief Unallocate all CRL data |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 543 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 544 | * \param crl CRL chain to free |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 545 | */ |
| 546 | void x509_crl_free( x509_crl *crl ); |
| 547 | |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 548 | /** @} name Functions to clear a certificate, CRL or private RSA key */ |
| 549 | |
| 550 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 551 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | * \brief Checkup routine |
| 553 | * |
| 554 | * \return 0 if successful, or 1 if the test failed |
| 555 | */ |
| 556 | int x509_self_test( int verbose ); |
| 557 | |
| 558 | #ifdef __cplusplus |
| 559 | } |
| 560 | #endif |
| 561 | |
| 562 | #endif /* x509.h */ |