blob: 1f062e9b74c0d7738a2aa2630f71e1070070f122 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief X.509 certificate and private key decoding
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * 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 Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_X509_H
28#define POLARSSL_X509_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker8e831ed2009-01-03 21:24:11 +000030#include "polarssl/rsa.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000031#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker37ca75d2011-01-06 12:28:03 +000033/**
34 * @addtogroup x509_module
35 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000036 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000037
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 Bakker13e2dfe2009-07-28 07:18:38 +000044 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000045#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 Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakker37ca75d2011-01-06 12:28:03 +000052/**
53 * @name X509 Error codes
54 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000055 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000056#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 Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakker37ca75d2011-01-06 12:28:03 +000083
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 Bakker5121ce52009-01-03 21:22:43 +0000107 */
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 Bakker91200182010-02-18 21:26:15 +0000121#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +0000122#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 Bakker37ca75d2011-01-06 12:28:03 +0000127/* @} name */
128/* @} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
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 Bakker74111d32011-01-15 16:57:55 +0000147/** Returns the size of the binary string, without the trailing \0 */
148#define OID_SIZE(x) (sizeof(x) - 1)
149
Paul Bakker5121ce52009-01-03 21:22:43 +0000150#define OID_X520 "\x55\x04"
Paul Bakker74111d32011-01-15 16:57:55 +0000151#define OID_CN OID_X520 "\x03"
152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
Paul Bakker74111d32011-01-15 16:57:55 +0000154#define OID_PKCS1_RSA OID_PKCS1 "\x01"
155#define OID_PKCS1_RSA_SHA OID_PKCS1 "\x05"
156
Paul Bakker5121ce52009-01-03 21:22:43 +0000157#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
Paul Bakker74111d32011-01-15 16:57:55 +0000158#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 Bakker5121ce52009-01-03 21:22:43 +0000273
Paul Bakker37ca75d2011-01-06 12:28:03 +0000274/**
275 * @addtogroup x509_module
276 * @{ */
277
278/**
279 * @name Structures for parsing X.509 certificates and CRLs
280 * @{
281 */
282
283/**
284 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 */
286typedef struct _x509_buf
287{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000288 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 Bakker5121ce52009-01-03 21:22:43 +0000291}
292x509_buf;
293
Paul Bakker37ca75d2011-01-06 12:28:03 +0000294/**
Paul Bakker74111d32011-01-15 16:57:55 +0000295 * Container for ASN1 bit strings.
296 */
297typedef 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}
303x509_bitstring;
304
305/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000306 * Container for ASN1 named information objects.
307 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
308 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000309typedef struct _x509_name
310{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000311 x509_buf oid; /**< The object identifier. */
312 x509_buf val; /**< The named value. */
313 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000314}
315x509_name;
316
Paul Bakker74111d32011-01-15 16:57:55 +0000317/**
318 * Container for a sequence of ASN.1 items
319 */
320typedef 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}
325x509_sequence;
326
Paul Bakker37ca75d2011-01-06 12:28:03 +0000327/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000328typedef struct _x509_time
329{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000330 int year, mon, day; /**< Date. */
331 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332}
333x509_time;
334
Paul Bakker37ca75d2011-01-06 12:28:03 +0000335/**
336 * Container for an X.509 certificate. The certificate may be chained.
337 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000338typedef struct _x509_cert
339{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000340 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 Bakker5121ce52009-01-03 21:22:43 +0000342
Paul Bakker37ca75d2011-01-06 12:28:03 +0000343 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 Bakker5121ce52009-01-03 21:22:43 +0000346
Paul Bakker37ca75d2011-01-06 12:28:03 +0000347 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 Bakker5121ce52009-01-03 21:22:43 +0000349
Paul Bakker37ca75d2011-01-06 12:28:03 +0000350 x509_name issuer; /**< The parsed issuer data (named information object). */
351 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Paul Bakker37ca75d2011-01-06 12:28:03 +0000353 x509_time valid_from; /**< Start time of certificate validity. */
354 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Paul Bakker37ca75d2011-01-06 12:28:03 +0000356 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 Bakker5121ce52009-01-03 21:22:43 +0000358
Paul Bakker37ca75d2011-01-06 12:28:03 +0000359 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 Bakker5121ce52009-01-03 21:22:43 +0000362
Paul Bakker74111d32011-01-15 16:57:55 +0000363 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000364 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 Bakker5121ce52009-01-03 21:22:43 +0000366
Paul Bakker74111d32011-01-15 16:57:55 +0000367 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 Bakker37ca75d2011-01-06 12:28:03 +0000373 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 Bakker5121ce52009-01-03 21:22:43 +0000376
Paul Bakker37ca75d2011-01-06 12:28:03 +0000377 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000378}
379x509_cert;
380
Paul Bakker37ca75d2011-01-06 12:28:03 +0000381/**
382 * Certificate revocation list entry.
383 * Contains the CA-specific serial numbers and revocation dates.
384 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000385typedef 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}
397x509_crl_entry;
398
Paul Bakker37ca75d2011-01-06 12:28:03 +0000399/**
400 * Certificate revocation list structure.
401 * Every CRL may have multiple entries.
402 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403typedef struct _x509_crl
404{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000405 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 Bakkerd98030e2009-05-02 15:13:40 +0000407
408 int version;
409 x509_buf sig_oid1;
410
Paul Bakker37ca75d2011-01-06 12:28:03 +0000411 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412
Paul Bakker37ca75d2011-01-06 12:28:03 +0000413 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000414
Paul Bakker37ca75d2011-01-06 12:28:03 +0000415 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000416 x509_time next_update;
417
Paul Bakker37ca75d2011-01-06 12:28:03 +0000418 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000419
420 x509_buf crl_ext;
421
422 x509_buf sig_oid2;
423 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000424 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000425
426 struct _x509_crl *next;
427}
428x509_crl;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000429/** @} name Structures for parsing X.509 certificates and CRLs */
430/** @} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000431
Paul Bakker37ca75d2011-01-06 12:28:03 +0000432/**
433 * @name Structures for writing X.509 certificates.
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 Bakker5121ce52009-01-03 21:22:43 +0000437 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000438/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000439typedef struct _x509_node
440{
441 unsigned char *data;
442 unsigned char *p;
443 unsigned char *end;
444
445 size_t len;
446}
447x509_node;
448
449typedef 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}
465x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000466*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468#ifdef __cplusplus
469extern "C" {
470#endif
471
472/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000473 * @name Functions to read in DHM parameters, a certificate, CRL or private RSA key
Paul Bakker37ca75d2011-01-06 12:28:03 +0000474 * @{
475 */
476
477/** @ingroup x509_module */
478/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 * \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 Bakkerff60ee62010-03-16 21:09:09 +0000488int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Paul Bakker37ca75d2011-01-06 12:28:03 +0000490/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000491/**
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 Bakkerff60ee62010-03-16 21:09:09 +0000500int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker37ca75d2011-01-06 12:28:03 +0000502/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000503/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000504 * \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 Bakkerff60ee62010-03-16 21:09:09 +0000513int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000514
Paul Bakker37ca75d2011-01-06 12:28:03 +0000515/** @ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000516/**
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 Bakkerff60ee62010-03-16 21:09:09 +0000525int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000526
Paul Bakker37ca75d2011-01-06 12:28:03 +0000527/** @ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000528/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 * \brief Parse a private RSA key
530 *
531 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000532 * \param key input buffer
533 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 * \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 */
539int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000540 const unsigned char *key, int keylen,
541 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Paul Bakker37ca75d2011-01-06 12:28:03 +0000543/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000544/**
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 Bakker37ca75d2011-01-06 12:28:03 +0000549 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 *
551 * \return 0 if successful, or a specific X509 error code
552 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000553int x509parse_keyfile( rsa_context *rsa, const char *path,
554 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000555
556/** @ingroup x509_module */
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 */
566int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, int dhminlen );
567
568/** @ingroup x509_module */
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 */
577int x509parse_dhmfile( dhm_context *rsa, const char *path );
578
579/** @} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000580
581
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583/**
584 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000585 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000586 *
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 Bakker5121ce52009-01-03 21:22:43 +0000593 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000594int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000597 * \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 */
607int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
608
609/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 * \brief Returns an informational string about the
611 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000612 *
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 Bakker5121ce52009-01-03 21:22:43 +0000620 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000621int x509parse_cert_info( char *buf, size_t size, const char *prefix,
622 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000623
624/**
625 * \brief Returns an informational string about the
626 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000627 *
628 * \param buf Buffer to write to
629 * \param size Maximum size of buffer
630 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000631 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000632 *
633 * \return The amount of data written to the buffer, or -1 in
634 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000635 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000636int x509parse_crl_info( char *buf, size_t size, const char *prefix,
637 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639/**
Paul Bakker74111d32011-01-15 16:57:55 +0000640 * \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 */
647const 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 */
659int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
660
661/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000662 * \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 Bakker40ea7de2009-05-03 10:18:48 +0000668 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000670int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000673 * @name Functions to verify a certificate
674 * @{
675 */
676/** @ingroup x509_module */
677/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 * \brief Verify the certificate signature
679 *
680 * \param crt a certificate to be verified
681 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000682 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 * \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 Bakkerb63b0af2011-01-13 17:54:59 +0000686 * \param f_vrfy verification function
687 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 *
Paul Bakker40e46942009-01-03 21:51:57 +0000689 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 * 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 */
699int x509parse_verify( x509_cert *crt,
700 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000701 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000702 const char *cn, int *flags,
703 int (*f_vrfy)(void *, x509_cert *, int, int),
704 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Paul Bakker74111d32011-01-15 16:57:55 +0000706/**
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 */
715int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
716
Paul Bakker37ca75d2011-01-06 12:28:03 +0000717/** @} name Functions to verify a certificate */
718
719
720
721/**
722 * @name Functions to clear a certificate, CRL or private RSA key
723 * @{
724 */
725/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000726/**
727 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000728 *
729 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 */
731void x509_free( x509_cert *crt );
732
Paul Bakker37ca75d2011-01-06 12:28:03 +0000733/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000734/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000735 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000736 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000737 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000738 */
739void x509_crl_free( x509_crl *crl );
740
Paul Bakker37ca75d2011-01-06 12:28:03 +0000741/** @} name Functions to clear a certificate, CRL or private RSA key */
742
743
Paul Bakkerd98030e2009-05-02 15:13:40 +0000744/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 * \brief Checkup routine
746 *
747 * \return 0 if successful, or 1 if the test failed
748 */
749int x509_self_test( int verbose );
750
751#ifdef __cplusplus
752}
753#endif
754
755#endif /* x509.h */