blob: b2ca75e1c458d3f91f395124076e57b791f68aa9 [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/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000034 * \addtogroup x509_module
35 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000036 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000037
38/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000039 * \name ASN1 Error codes
Paul Bakker37ca75d2011-01-06 12:28:03 +000040 * 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 Bakker0f5f72e2011-01-18 14:58:55 +000043 * \{
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) */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000050/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakker37ca75d2011-01-06 12:28:03 +000052/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000053 * \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. */
Paul Bakker37ca75d2011-01-06 12:28:03 +000072#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240 /**< Unsupported RSA key version */
73#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260 /**< Invalid RSA key tag or value. */
Paul Bakker37ca75d2011-01-06 12:28:03 +000074#define POLARSSL_ERR_X509_POINT_ERROR -0x0300 /**< Not used. */
75#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320 /**< Not used. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000076/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Paul Bakker37ca75d2011-01-06 12:28:03 +000078
79/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000080 * \name X509 Verify codes
81 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000082 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000083#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
84#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
85#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
86#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
87#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
88#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
89#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
90#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000091/* \} name */
Paul Bakker37ca75d2011-01-06 12:28:03 +000092
93
94/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000095 * \name DER constants
Paul Bakker37ca75d2011-01-06 12:28:03 +000096 * These constants comply with DER encoded the ANS1 type tags.
97 * DER encoding uses hexadecimal representation.
98 * An example DER sequence is:\n
99 * - 0x02 -- tag indicating INTEGER
100 * - 0x01 -- length in octets
101 * - 0x05 -- value
102 * Such sequences are typically read into \c ::x509_buf.
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000103 * \{
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
105#define ASN1_BOOLEAN 0x01
106#define ASN1_INTEGER 0x02
107#define ASN1_BIT_STRING 0x03
108#define ASN1_OCTET_STRING 0x04
109#define ASN1_NULL 0x05
110#define ASN1_OID 0x06
111#define ASN1_UTF8_STRING 0x0C
112#define ASN1_SEQUENCE 0x10
113#define ASN1_SET 0x11
114#define ASN1_PRINTABLE_STRING 0x13
115#define ASN1_T61_STRING 0x14
116#define ASN1_IA5_STRING 0x16
117#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +0000118#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +0000119#define ASN1_UNIVERSAL_STRING 0x1C
120#define ASN1_BMP_STRING 0x1E
121#define ASN1_PRIMITIVE 0x00
122#define ASN1_CONSTRUCTED 0x20
123#define ASN1_CONTEXT_SPECIFIC 0x80
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000124/* \} name */
125/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/*
128 * various object identifiers
129 */
130#define X520_COMMON_NAME 3
131#define X520_COUNTRY 6
132#define X520_LOCALITY 7
133#define X520_STATE 8
134#define X520_ORGANIZATION 10
135#define X520_ORG_UNIT 11
136#define PKCS9_EMAIL 1
137
138#define X509_OUTPUT_DER 0x01
139#define X509_OUTPUT_PEM 0x02
140#define PEM_LINE_LENGTH 72
141#define X509_ISSUER 0x01
142#define X509_SUBJECT 0x02
143
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000144/** Returns the size of the binary string, without the trailing \\0 */
Paul Bakker74111d32011-01-15 16:57:55 +0000145#define OID_SIZE(x) (sizeof(x) - 1)
146
Paul Bakker5121ce52009-01-03 21:22:43 +0000147#define OID_X520 "\x55\x04"
Paul Bakker74111d32011-01-15 16:57:55 +0000148#define OID_CN OID_X520 "\x03"
149
Paul Bakker5121ce52009-01-03 21:22:43 +0000150#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
Paul Bakker74111d32011-01-15 16:57:55 +0000151#define OID_PKCS1_RSA OID_PKCS1 "\x01"
152#define OID_PKCS1_RSA_SHA OID_PKCS1 "\x05"
153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
Paul Bakker74111d32011-01-15 16:57:55 +0000155#define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
156
157/** ISO arc for standard certificate and CRL extensions */
158#define OID_ID_CE "\x55\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
159
160/**
161 * Private Internet Extensions
162 * { iso(1) identified-organization(3) dod(6) internet(1)
163 * security(5) mechanisms(5) pkix(7) }
164 */
165#define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
166
167/*
168 * OIDs for standard certificate extensions
169 */
170#define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
171#define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
172#define OID_KEY_USAGE OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
173#define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
174#define OID_POLICY_MAPPINGS OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
175#define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
176#define OID_ISSUER_ALT_NAME OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
177#define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
178#define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
179#define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
180#define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
181#define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
182#define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
183#define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
184#define OID_FRESHEST_CRL OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
185
186/*
187 * X.509 v3 Key Usage Extension flags
188 */
189#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
190#define KU_NON_REPUDIATION (0x40) /* bit 1 */
191#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
192#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
193#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
194#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
195#define KU_CRL_SIGN (0x02) /* bit 6 */
196
197/*
198 * X.509 v3 Extended key usage OIDs
199 */
200#define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
201
202#define OID_KP OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
203#define OID_SERVER_AUTH OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
204#define OID_CLIENT_AUTH OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
205#define OID_CODE_SIGNING OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
206#define OID_EMAIL_PROTECTION OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
207#define OID_TIME_STAMPING OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
208#define OID_OCSP_SIGNING OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
209
210#define STRING_SERVER_AUTH "TLS Web Server Authentication"
211#define STRING_CLIENT_AUTH "TLS Web Client Authentication"
212#define STRING_CODE_SIGNING "Code Signing"
213#define STRING_EMAIL_PROTECTION "E-mail Protection"
214#define STRING_TIME_STAMPING "Time Stamping"
215#define STRING_OCSP_SIGNING "OCSP Signing"
216
217/*
218 * OIDs for CRL extensions
219 */
220#define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
221#define OID_CRL_NUMBER OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
222
223/*
224 * Netscape certificate extensions
225 */
226#define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
227#define OID_NS_CERT OID_NETSCAPE "\x01"
228#define OID_NS_CERT_TYPE OID_NS_CERT "\x01"
229#define OID_NS_BASE_URL OID_NS_CERT "\x02"
230#define OID_NS_REVOCATION_URL OID_NS_CERT "\x03"
231#define OID_NS_CA_REVOCATION_URL OID_NS_CERT "\x04"
232#define OID_NS_RENEWAL_URL OID_NS_CERT "\x07"
233#define OID_NS_CA_POLICY_URL OID_NS_CERT "\x08"
234#define OID_NS_SSL_SERVER_NAME OID_NS_CERT "\x0C"
235#define OID_NS_COMMENT OID_NS_CERT "\x0D"
236#define OID_NS_DATA_TYPE OID_NETSCAPE "\x02"
237#define OID_NS_CERT_SEQUENCE OID_NS_DATA_TYPE "\x05"
238
239/*
240 * Netscape certificate types
241 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
242 */
243
244#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
245#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
246#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
247#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
248#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
249#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
250#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
251#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
252
253#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
254#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
255#define EXT_KEY_USAGE (1 << 2)
256#define EXT_CERTIFICATE_POLICIES (1 << 3)
257#define EXT_POLICY_MAPPINGS (1 << 4)
258#define EXT_SUBJECT_ALT_NAME (1 << 5)
259#define EXT_ISSUER_ALT_NAME (1 << 6)
260#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
261#define EXT_BASIC_CONSTRAINTS (1 << 8)
262#define EXT_NAME_CONSTRAINTS (1 << 9)
263#define EXT_POLICY_CONSTRAINTS (1 << 10)
264#define EXT_EXTENDED_KEY_USAGE (1 << 11)
265#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
266#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
267#define EXT_FRESHEST_CRL (1 << 14)
268
269#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Paul Bakker37ca75d2011-01-06 12:28:03 +0000271/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000272 * \addtogroup x509_module
273 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000274
275/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000276 * \name Structures for parsing X.509 certificates and CRLs
277 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000278 */
279
280/**
281 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 */
283typedef struct _x509_buf
284{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000285 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
286 int len; /**< ASN1 length, e.g. in octets. */
287 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000288}
289x509_buf;
290
Paul Bakker37ca75d2011-01-06 12:28:03 +0000291/**
Paul Bakker74111d32011-01-15 16:57:55 +0000292 * Container for ASN1 bit strings.
293 */
294typedef struct _x509_bitstring
295{
296 int len; /**< ASN1 length, e.g. in octets. */
297 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
298 unsigned char *p; /**< Raw ASN1 data for the bit string */
299}
300x509_bitstring;
301
302/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000303 * Container for ASN1 named information objects.
304 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
305 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000306typedef struct _x509_name
307{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000308 x509_buf oid; /**< The object identifier. */
309 x509_buf val; /**< The named value. */
310 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000311}
312x509_name;
313
Paul Bakker74111d32011-01-15 16:57:55 +0000314/**
315 * Container for a sequence of ASN.1 items
316 */
317typedef struct _x509_sequence
318{
319 x509_buf buf; /**< Buffer containing the given ASN.1 item. */
320 struct _x509_sequence *next; /**< The next entry in the sequence. */
321}
322x509_sequence;
323
Paul Bakker37ca75d2011-01-06 12:28:03 +0000324/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000325typedef struct _x509_time
326{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000327 int year, mon, day; /**< Date. */
328 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329}
330x509_time;
331
Paul Bakker37ca75d2011-01-06 12:28:03 +0000332/**
333 * Container for an X.509 certificate. The certificate may be chained.
334 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000335typedef struct _x509_cert
336{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000337 x509_buf raw; /**< The raw certificate data (DER). */
338 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Paul Bakker37ca75d2011-01-06 12:28:03 +0000340 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
341 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
342 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
Paul Bakker37ca75d2011-01-06 12:28:03 +0000344 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
345 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
Paul Bakker37ca75d2011-01-06 12:28:03 +0000347 x509_name issuer; /**< The parsed issuer data (named information object). */
348 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Paul Bakker37ca75d2011-01-06 12:28:03 +0000350 x509_time valid_from; /**< Start time of certificate validity. */
351 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Paul Bakker37ca75d2011-01-06 12:28:03 +0000353 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
354 rsa_context rsa; /**< Container for the RSA context. Only RSA is supported for public keys at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Paul Bakker37ca75d2011-01-06 12:28:03 +0000356 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
357 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
358 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
Paul Bakker74111d32011-01-15 16:57:55 +0000360 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000361 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
362 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Paul Bakker74111d32011-01-15 16:57:55 +0000364 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
365
366 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
367
368 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
369
Paul Bakker37ca75d2011-01-06 12:28:03 +0000370 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
371 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
372 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Paul Bakker37ca75d2011-01-06 12:28:03 +0000374 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000375}
376x509_cert;
377
Paul Bakker37ca75d2011-01-06 12:28:03 +0000378/**
379 * Certificate revocation list entry.
380 * Contains the CA-specific serial numbers and revocation dates.
381 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000382typedef struct _x509_crl_entry
383{
384 x509_buf raw;
385
386 x509_buf serial;
387
388 x509_time revocation_date;
389
390 x509_buf entry_ext;
391
392 struct _x509_crl_entry *next;
393}
394x509_crl_entry;
395
Paul Bakker37ca75d2011-01-06 12:28:03 +0000396/**
397 * Certificate revocation list structure.
398 * Every CRL may have multiple entries.
399 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000400typedef struct _x509_crl
401{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000402 x509_buf raw; /**< The raw certificate data (DER). */
403 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404
405 int version;
406 x509_buf sig_oid1;
407
Paul Bakker37ca75d2011-01-06 12:28:03 +0000408 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000409
Paul Bakker37ca75d2011-01-06 12:28:03 +0000410 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000411
Paul Bakker37ca75d2011-01-06 12:28:03 +0000412 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000413 x509_time next_update;
414
Paul Bakker37ca75d2011-01-06 12:28:03 +0000415 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000416
417 x509_buf crl_ext;
418
419 x509_buf sig_oid2;
420 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000421 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422
423 struct _x509_crl *next;
424}
425x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000426/** \} name Structures for parsing X.509 certificates and CRLs */
427/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000428
Paul Bakker37ca75d2011-01-06 12:28:03 +0000429/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000430 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000431 * XvP: commented out as they are not used.
432 * - <tt>typedef struct _x509_node x509_node;</tt>
433 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000435/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000436typedef struct _x509_node
437{
438 unsigned char *data;
439 unsigned char *p;
440 unsigned char *end;
441
442 size_t len;
443}
444x509_node;
445
446typedef struct _x509_raw
447{
448 x509_node raw;
449 x509_node tbs;
450
451 x509_node version;
452 x509_node serial;
453 x509_node tbs_signalg;
454 x509_node issuer;
455 x509_node validity;
456 x509_node subject;
457 x509_node subpubkey;
458
459 x509_node signalg;
460 x509_node sign;
461}
462x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000463*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465#ifdef __cplusplus
466extern "C" {
467#endif
468
469/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000470 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
471 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000472 */
473
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000474/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000475/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * \brief Parse one or more certificates and add them
477 * to the chained list
478 *
479 * \param chain points to the start of the chain
480 * \param buf buffer holding the certificate data
481 * \param buflen size of the buffer
482 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000483 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000485int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000487/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000488/**
489 * \brief Load one or more certificates and add them
490 * to the chained list
491 *
492 * \param chain points to the start of the chain
493 * \param path filename to read the certificates from
494 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000495 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000497int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000499/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000500/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000501 * \brief Parse one or more CRLs and add them
502 * to the chained list
503 *
504 * \param chain points to the start of the chain
505 * \param buf buffer holding the CRL data
506 * \param buflen size of the buffer
507 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000508 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000509 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000510int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000511
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000512/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000513/**
514 * \brief Load one or more CRLs and add them
515 * to the chained list
516 *
517 * \param chain points to the start of the chain
518 * \param path filename to read the CRLs from
519 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000520 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000521 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000522int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000523
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000524/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000525/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 * \brief Parse a private RSA key
527 *
528 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000529 * \param key input buffer
530 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * \param pwd password for decryption (optional)
532 * \param pwdlen size of the password
533 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000534 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 */
536int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000537 const unsigned char *key, int keylen,
538 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000540/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000541/**
542 * \brief Load and parse a private RSA key
543 *
544 * \param rsa RSA context to be initialized
545 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000546 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000548 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000550int x509parse_keyfile( rsa_context *rsa, const char *path,
551 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000552
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000553/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000554/**
555 * \brief Parse DHM parameters
556 *
557 * \param dhm DHM context to be initialized
558 * \param dhmin input buffer
559 * \param dhminlen size of the buffer
560 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000561 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000562 */
563int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, int dhminlen );
564
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000565/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000566/**
567 * \brief Load and parse DHM parameters
568 *
569 * \param dhm DHM context to be initialized
570 * \param path filename to read the DHM Parameters from
571 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000572 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000573 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000574int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000575
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000576/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000577
578
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580/**
581 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000582 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000583 *
584 * \param buf Buffer to write to
585 * \param size Maximum size of buffer
586 * \param dn The X509 name to represent
587 *
588 * \return The amount of data written to the buffer, or -1 in
589 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000591int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000594 * \brief Store the certificate serial in printable form into buf;
595 * no more than size characters will be written.
596 *
597 * \param buf Buffer to write to
598 * \param size Maximum size of buffer
599 * \param serial The X509 serial to represent
600 *
601 * \return The amount of data written to the buffer, or -1 in
602 * case of an error.
603 */
604int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
605
606/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 * \brief Returns an informational string about the
608 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000609 *
610 * \param buf Buffer to write to
611 * \param size Maximum size of buffer
612 * \param prefix A line prefix
613 * \param crt The X509 certificate to represent
614 *
615 * \return The amount of data written to the buffer, or -1 in
616 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000618int x509parse_cert_info( char *buf, size_t size, const char *prefix,
619 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000620
621/**
622 * \brief Returns an informational string about the
623 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000624 *
625 * \param buf Buffer to write to
626 * \param size Maximum size of buffer
627 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000628 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000629 *
630 * \return The amount of data written to the buffer, or -1 in
631 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000632 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000633int x509parse_crl_info( char *buf, size_t size, const char *prefix,
634 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636/**
Paul Bakker74111d32011-01-15 16:57:55 +0000637 * \brief Give an known OID, return its descriptive string.
638 *
639 * \param oid buffer containing the oid
640 *
641 * \return Return a string if the OID is known,
642 * or NULL otherwise.
643 */
644const char *x509_oid_get_description( x509_buf *oid );
645
646/*
647 * \brief Give an OID, return a string version of its OID number.
648 *
649 * \param buf Buffer to write to
650 * \param size Maximum size of buffer
651 * \param oid Buffer containing the OID
652 *
653 * \return The amount of data written to the buffer, or -1 in
654 * case of an error.
655 */
656int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
657
658/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000659 * \brief Check a given x509_time against the system time and check
660 * if it is valid.
661 *
662 * \param time x509_time to check
663 *
664 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000665 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000667int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000670 * \name Functions to verify a certificate
671 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000672 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000673/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000674/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 * \brief Verify the certificate signature
676 *
677 * \param crt a certificate to be verified
678 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000679 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 * \param cn expected Common Name (can be set to
681 * NULL if the CN must not be verified)
682 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000683 * \param f_vrfy verification function
684 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 *
Paul Bakker40e46942009-01-03 21:51:57 +0000686 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 * in which case *flags will have one or more of
688 * the following values set:
689 * BADCERT_EXPIRED --
690 * BADCERT_REVOKED --
691 * BADCERT_CN_MISMATCH --
692 * BADCERT_NOT_TRUSTED
693 *
694 * \note TODO: add two arguments, depth and crl
695 */
696int x509parse_verify( x509_cert *crt,
697 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000698 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000699 const char *cn, int *flags,
700 int (*f_vrfy)(void *, x509_cert *, int, int),
701 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Paul Bakker74111d32011-01-15 16:57:55 +0000703/**
704 * \brief Verify the certificate signature
705 *
706 * \param crt a certificate to be verified
707 * \param crl the CRL to verify against
708 *
709 * \return 1 if the certificate is revoked, 0 otherwise
710 *
711 */
712int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
713
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000714/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000715
716
717
718/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000719 * \name Functions to clear a certificate, CRL or private RSA key
720 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000721 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000722/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000723/**
724 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000725 *
726 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 */
728void x509_free( x509_cert *crt );
729
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000730/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000731/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000732 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000733 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000734 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000735 */
736void x509_crl_free( x509_crl *crl );
737
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000738/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000739
740
Paul Bakkerd98030e2009-05-02 15:13:40 +0000741/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 * \brief Checkup routine
743 *
744 * \return 0 if successful, or 1 if the test failed
745 */
746int x509_self_test( int verbose );
747
748#ifdef __cplusplus
749}
750#endif
751
752#endif /* x509.h */