blob: eda522161c097044b2bcda40b716d4c7a524d662 [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. */
72#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220 /**< PEM key string is not as expected. */
73#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240 /**< Unsupported RSA key version */
74#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260 /**< Invalid RSA key tag or value. */
75#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280 /**< RSA IV is not in hex-format. */
76#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0 /**< Unsupported key encryption algorithm. */
77#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0 /**< Private key password can't be empty. */
78#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0 /**< Given private key password does not allow for correct decryption. */
79#define POLARSSL_ERR_X509_POINT_ERROR -0x0300 /**< Not used. */
80#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320 /**< Not used. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000081/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakker37ca75d2011-01-06 12:28:03 +000083
84/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000085 * \name X509 Verify codes
86 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000087 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000088#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
89#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
90#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
91#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
92#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
93#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
94#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
95#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000096/* \} name */
Paul Bakker37ca75d2011-01-06 12:28:03 +000097
98
99/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000100 * \name DER constants
Paul Bakker37ca75d2011-01-06 12:28:03 +0000101 * These constants comply with DER encoded the ANS1 type tags.
102 * DER encoding uses hexadecimal representation.
103 * An example DER sequence is:\n
104 * - 0x02 -- tag indicating INTEGER
105 * - 0x01 -- length in octets
106 * - 0x05 -- value
107 * Such sequences are typically read into \c ::x509_buf.
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000108 * \{
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
110#define ASN1_BOOLEAN 0x01
111#define ASN1_INTEGER 0x02
112#define ASN1_BIT_STRING 0x03
113#define ASN1_OCTET_STRING 0x04
114#define ASN1_NULL 0x05
115#define ASN1_OID 0x06
116#define ASN1_UTF8_STRING 0x0C
117#define ASN1_SEQUENCE 0x10
118#define ASN1_SET 0x11
119#define ASN1_PRINTABLE_STRING 0x13
120#define ASN1_T61_STRING 0x14
121#define ASN1_IA5_STRING 0x16
122#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +0000123#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +0000124#define ASN1_UNIVERSAL_STRING 0x1C
125#define ASN1_BMP_STRING 0x1E
126#define ASN1_PRIMITIVE 0x00
127#define ASN1_CONSTRUCTED 0x20
128#define ASN1_CONTEXT_SPECIFIC 0x80
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000129/* \} name */
130/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132/*
133 * various object identifiers
134 */
135#define X520_COMMON_NAME 3
136#define X520_COUNTRY 6
137#define X520_LOCALITY 7
138#define X520_STATE 8
139#define X520_ORGANIZATION 10
140#define X520_ORG_UNIT 11
141#define PKCS9_EMAIL 1
142
143#define X509_OUTPUT_DER 0x01
144#define X509_OUTPUT_PEM 0x02
145#define PEM_LINE_LENGTH 72
146#define X509_ISSUER 0x01
147#define X509_SUBJECT 0x02
148
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000149/** Returns the size of the binary string, without the trailing \\0 */
Paul Bakker74111d32011-01-15 16:57:55 +0000150#define OID_SIZE(x) (sizeof(x) - 1)
151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152#define OID_X520 "\x55\x04"
Paul Bakker74111d32011-01-15 16:57:55 +0000153#define OID_CN OID_X520 "\x03"
154
Paul Bakker5121ce52009-01-03 21:22:43 +0000155#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
Paul Bakker74111d32011-01-15 16:57:55 +0000156#define OID_PKCS1_RSA OID_PKCS1 "\x01"
157#define OID_PKCS1_RSA_SHA OID_PKCS1 "\x05"
158
Paul Bakker5121ce52009-01-03 21:22:43 +0000159#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
Paul Bakker74111d32011-01-15 16:57:55 +0000160#define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
161
162/** ISO arc for standard certificate and CRL extensions */
163#define OID_ID_CE "\x55\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
164
165/**
166 * Private Internet Extensions
167 * { iso(1) identified-organization(3) dod(6) internet(1)
168 * security(5) mechanisms(5) pkix(7) }
169 */
170#define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
171
172/*
173 * OIDs for standard certificate extensions
174 */
175#define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
176#define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
177#define OID_KEY_USAGE OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
178#define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
179#define OID_POLICY_MAPPINGS OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
180#define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
181#define OID_ISSUER_ALT_NAME OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
182#define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
183#define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
184#define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
185#define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
186#define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
187#define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
188#define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
189#define OID_FRESHEST_CRL OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
190
191/*
192 * X.509 v3 Key Usage Extension flags
193 */
194#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
195#define KU_NON_REPUDIATION (0x40) /* bit 1 */
196#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
197#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
198#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
199#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
200#define KU_CRL_SIGN (0x02) /* bit 6 */
201
202/*
203 * X.509 v3 Extended key usage OIDs
204 */
205#define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
206
207#define OID_KP OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
208#define OID_SERVER_AUTH OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
209#define OID_CLIENT_AUTH OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
210#define OID_CODE_SIGNING OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
211#define OID_EMAIL_PROTECTION OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
212#define OID_TIME_STAMPING OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
213#define OID_OCSP_SIGNING OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
214
215#define STRING_SERVER_AUTH "TLS Web Server Authentication"
216#define STRING_CLIENT_AUTH "TLS Web Client Authentication"
217#define STRING_CODE_SIGNING "Code Signing"
218#define STRING_EMAIL_PROTECTION "E-mail Protection"
219#define STRING_TIME_STAMPING "Time Stamping"
220#define STRING_OCSP_SIGNING "OCSP Signing"
221
222/*
223 * OIDs for CRL extensions
224 */
225#define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
226#define OID_CRL_NUMBER OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
227
228/*
229 * Netscape certificate extensions
230 */
231#define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
232#define OID_NS_CERT OID_NETSCAPE "\x01"
233#define OID_NS_CERT_TYPE OID_NS_CERT "\x01"
234#define OID_NS_BASE_URL OID_NS_CERT "\x02"
235#define OID_NS_REVOCATION_URL OID_NS_CERT "\x03"
236#define OID_NS_CA_REVOCATION_URL OID_NS_CERT "\x04"
237#define OID_NS_RENEWAL_URL OID_NS_CERT "\x07"
238#define OID_NS_CA_POLICY_URL OID_NS_CERT "\x08"
239#define OID_NS_SSL_SERVER_NAME OID_NS_CERT "\x0C"
240#define OID_NS_COMMENT OID_NS_CERT "\x0D"
241#define OID_NS_DATA_TYPE OID_NETSCAPE "\x02"
242#define OID_NS_CERT_SEQUENCE OID_NS_DATA_TYPE "\x05"
243
244/*
245 * Netscape certificate types
246 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
247 */
248
249#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
250#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
251#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
252#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
253#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
254#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
255#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
256#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
257
258#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
259#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
260#define EXT_KEY_USAGE (1 << 2)
261#define EXT_CERTIFICATE_POLICIES (1 << 3)
262#define EXT_POLICY_MAPPINGS (1 << 4)
263#define EXT_SUBJECT_ALT_NAME (1 << 5)
264#define EXT_ISSUER_ALT_NAME (1 << 6)
265#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
266#define EXT_BASIC_CONSTRAINTS (1 << 8)
267#define EXT_NAME_CONSTRAINTS (1 << 9)
268#define EXT_POLICY_CONSTRAINTS (1 << 10)
269#define EXT_EXTENDED_KEY_USAGE (1 << 11)
270#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
271#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
272#define EXT_FRESHEST_CRL (1 << 14)
273
274#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Paul Bakker37ca75d2011-01-06 12:28:03 +0000276/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000277 * \addtogroup x509_module
278 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000279
280/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000281 * \name Structures for parsing X.509 certificates and CRLs
282 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000283 */
284
285/**
286 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 */
288typedef struct _x509_buf
289{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000290 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
291 int len; /**< ASN1 length, e.g. in octets. */
292 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000293}
294x509_buf;
295
Paul Bakker37ca75d2011-01-06 12:28:03 +0000296/**
Paul Bakker74111d32011-01-15 16:57:55 +0000297 * Container for ASN1 bit strings.
298 */
299typedef struct _x509_bitstring
300{
301 int len; /**< ASN1 length, e.g. in octets. */
302 unsigned char unused_bits; /**< Number of unused bits at the end of the string */
303 unsigned char *p; /**< Raw ASN1 data for the bit string */
304}
305x509_bitstring;
306
307/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000308 * Container for ASN1 named information objects.
309 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
310 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000311typedef struct _x509_name
312{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000313 x509_buf oid; /**< The object identifier. */
314 x509_buf val; /**< The named value. */
315 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000316}
317x509_name;
318
Paul Bakker74111d32011-01-15 16:57:55 +0000319/**
320 * Container for a sequence of ASN.1 items
321 */
322typedef struct _x509_sequence
323{
324 x509_buf buf; /**< Buffer containing the given ASN.1 item. */
325 struct _x509_sequence *next; /**< The next entry in the sequence. */
326}
327x509_sequence;
328
Paul Bakker37ca75d2011-01-06 12:28:03 +0000329/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330typedef struct _x509_time
331{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000332 int year, mon, day; /**< Date. */
333 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000334}
335x509_time;
336
Paul Bakker37ca75d2011-01-06 12:28:03 +0000337/**
338 * Container for an X.509 certificate. The certificate may be chained.
339 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000340typedef struct _x509_cert
341{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000342 x509_buf raw; /**< The raw certificate data (DER). */
343 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Paul Bakker37ca75d2011-01-06 12:28:03 +0000345 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
346 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
347 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Paul Bakker37ca75d2011-01-06 12:28:03 +0000349 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
350 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
Paul Bakker37ca75d2011-01-06 12:28:03 +0000352 x509_name issuer; /**< The parsed issuer data (named information object). */
353 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Paul Bakker37ca75d2011-01-06 12:28:03 +0000355 x509_time valid_from; /**< Start time of certificate validity. */
356 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
Paul Bakker37ca75d2011-01-06 12:28:03 +0000358 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
359 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 +0000360
Paul Bakker37ca75d2011-01-06 12:28:03 +0000361 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
362 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
363 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
Paul Bakker74111d32011-01-15 16:57:55 +0000365 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000366 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
367 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
Paul Bakker74111d32011-01-15 16:57:55 +0000369 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
370
371 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
372
373 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
374
Paul Bakker37ca75d2011-01-06 12:28:03 +0000375 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
376 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
377 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
Paul Bakker37ca75d2011-01-06 12:28:03 +0000379 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000380}
381x509_cert;
382
Paul Bakker37ca75d2011-01-06 12:28:03 +0000383/**
384 * Certificate revocation list entry.
385 * Contains the CA-specific serial numbers and revocation dates.
386 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000387typedef struct _x509_crl_entry
388{
389 x509_buf raw;
390
391 x509_buf serial;
392
393 x509_time revocation_date;
394
395 x509_buf entry_ext;
396
397 struct _x509_crl_entry *next;
398}
399x509_crl_entry;
400
Paul Bakker37ca75d2011-01-06 12:28:03 +0000401/**
402 * Certificate revocation list structure.
403 * Every CRL may have multiple entries.
404 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000405typedef struct _x509_crl
406{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000407 x509_buf raw; /**< The raw certificate data (DER). */
408 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000409
410 int version;
411 x509_buf sig_oid1;
412
Paul Bakker37ca75d2011-01-06 12:28:03 +0000413 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000414
Paul Bakker37ca75d2011-01-06 12:28:03 +0000415 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000416
Paul Bakker37ca75d2011-01-06 12:28:03 +0000417 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418 x509_time next_update;
419
Paul Bakker37ca75d2011-01-06 12:28:03 +0000420 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000421
422 x509_buf crl_ext;
423
424 x509_buf sig_oid2;
425 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000426 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427
428 struct _x509_crl *next;
429}
430x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000431/** \} name Structures for parsing X.509 certificates and CRLs */
432/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000433
Paul Bakker37ca75d2011-01-06 12:28:03 +0000434/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000435 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000436 * XvP: commented out as they are not used.
437 * - <tt>typedef struct _x509_node x509_node;</tt>
438 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000440/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000441typedef struct _x509_node
442{
443 unsigned char *data;
444 unsigned char *p;
445 unsigned char *end;
446
447 size_t len;
448}
449x509_node;
450
451typedef struct _x509_raw
452{
453 x509_node raw;
454 x509_node tbs;
455
456 x509_node version;
457 x509_node serial;
458 x509_node tbs_signalg;
459 x509_node issuer;
460 x509_node validity;
461 x509_node subject;
462 x509_node subpubkey;
463
464 x509_node signalg;
465 x509_node sign;
466}
467x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000468*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470#ifdef __cplusplus
471extern "C" {
472#endif
473
474/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000475 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
476 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000477 */
478
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000479/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000480/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 * \brief Parse one or more certificates and add them
482 * to the chained list
483 *
484 * \param chain points to the start of the chain
485 * \param buf buffer holding the certificate data
486 * \param buflen size of the buffer
487 *
488 * \return 0 if successful, or a specific X509 error code
489 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000490int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000492/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000493/**
494 * \brief Load one or more certificates and add them
495 * to the chained list
496 *
497 * \param chain points to the start of the chain
498 * \param path filename to read the certificates from
499 *
500 * \return 0 if successful, or a specific X509 error code
501 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000504/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000505/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000506 * \brief Parse one or more CRLs and add them
507 * to the chained list
508 *
509 * \param chain points to the start of the chain
510 * \param buf buffer holding the CRL data
511 * \param buflen size of the buffer
512 *
513 * \return 0 if successful, or a specific X509 error code
514 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000515int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000516
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000517/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000518/**
519 * \brief Load one or more CRLs and add them
520 * to the chained list
521 *
522 * \param chain points to the start of the chain
523 * \param path filename to read the CRLs from
524 *
525 * \return 0 if successful, or a specific X509 error code
526 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000527int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000528
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000529/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000530/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * \brief Parse a private RSA key
532 *
533 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000534 * \param key input buffer
535 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * \param pwd password for decryption (optional)
537 * \param pwdlen size of the password
538 *
539 * \return 0 if successful, or a specific X509 error code
540 */
541int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000542 const unsigned char *key, int keylen,
543 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000545/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000546/**
547 * \brief Load and parse a private RSA key
548 *
549 * \param rsa RSA context to be initialized
550 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000551 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 *
553 * \return 0 if successful, or a specific X509 error code
554 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555int x509parse_keyfile( rsa_context *rsa, const char *path,
556 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000557
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000558/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000559/**
560 * \brief Parse DHM parameters
561 *
562 * \param dhm DHM context to be initialized
563 * \param dhmin input buffer
564 * \param dhminlen size of the buffer
565 *
566 * \return 0 if successful, or a specific X509 error code
567 */
568int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, int dhminlen );
569
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000570/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000571/**
572 * \brief Load and parse DHM parameters
573 *
574 * \param dhm DHM context to be initialized
575 * \param path filename to read the DHM Parameters from
576 *
577 * \return 0 if successful, or a specific X509 error code
578 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000579int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000580
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000581/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000582
583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585/**
586 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000587 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000588 *
589 * \param buf Buffer to write to
590 * \param size Maximum size of buffer
591 * \param dn The X509 name to represent
592 *
593 * \return The amount of data written to the buffer, or -1 in
594 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000596int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000599 * \brief Store the certificate serial in printable form into buf;
600 * no more than size characters will be written.
601 *
602 * \param buf Buffer to write to
603 * \param size Maximum size of buffer
604 * \param serial The X509 serial to represent
605 *
606 * \return The amount of data written to the buffer, or -1 in
607 * case of an error.
608 */
609int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
610
611/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 * \brief Returns an informational string about the
613 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000614 *
615 * \param buf Buffer to write to
616 * \param size Maximum size of buffer
617 * \param prefix A line prefix
618 * \param crt The X509 certificate to represent
619 *
620 * \return The amount of data written to the buffer, or -1 in
621 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000623int x509parse_cert_info( char *buf, size_t size, const char *prefix,
624 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000625
626/**
627 * \brief Returns an informational string about the
628 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000629 *
630 * \param buf Buffer to write to
631 * \param size Maximum size of buffer
632 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000633 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000634 *
635 * \return The amount of data written to the buffer, or -1 in
636 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000637 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000638int x509parse_crl_info( char *buf, size_t size, const char *prefix,
639 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641/**
Paul Bakker74111d32011-01-15 16:57:55 +0000642 * \brief Give an known OID, return its descriptive string.
643 *
644 * \param oid buffer containing the oid
645 *
646 * \return Return a string if the OID is known,
647 * or NULL otherwise.
648 */
649const char *x509_oid_get_description( x509_buf *oid );
650
651/*
652 * \brief Give an OID, return a string version of its OID number.
653 *
654 * \param buf Buffer to write to
655 * \param size Maximum size of buffer
656 * \param oid Buffer containing the OID
657 *
658 * \return The amount of data written to the buffer, or -1 in
659 * case of an error.
660 */
661int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
662
663/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000664 * \brief Check a given x509_time against the system time and check
665 * if it is valid.
666 *
667 * \param time x509_time to check
668 *
669 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000670 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000672int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000675 * \name Functions to verify a certificate
676 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000677 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000678/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000679/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 * \brief Verify the certificate signature
681 *
682 * \param crt a certificate to be verified
683 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000684 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 * \param cn expected Common Name (can be set to
686 * NULL if the CN must not be verified)
687 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000688 * \param f_vrfy verification function
689 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 *
Paul Bakker40e46942009-01-03 21:51:57 +0000691 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 * in which case *flags will have one or more of
693 * the following values set:
694 * BADCERT_EXPIRED --
695 * BADCERT_REVOKED --
696 * BADCERT_CN_MISMATCH --
697 * BADCERT_NOT_TRUSTED
698 *
699 * \note TODO: add two arguments, depth and crl
700 */
701int x509parse_verify( x509_cert *crt,
702 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000703 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000704 const char *cn, int *flags,
705 int (*f_vrfy)(void *, x509_cert *, int, int),
706 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Paul Bakker74111d32011-01-15 16:57:55 +0000708/**
709 * \brief Verify the certificate signature
710 *
711 * \param crt a certificate to be verified
712 * \param crl the CRL to verify against
713 *
714 * \return 1 if the certificate is revoked, 0 otherwise
715 *
716 */
717int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
718
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000719/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000720
721
722
723/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000724 * \name Functions to clear a certificate, CRL or private RSA key
725 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000726 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000727/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000728/**
729 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000730 *
731 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 */
733void x509_free( x509_cert *crt );
734
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000735/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000736/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000737 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000738 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000739 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000740 */
741void x509_crl_free( x509_crl *crl );
742
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000743/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000744
745
Paul Bakkerd98030e2009-05-02 15:13:40 +0000746/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 * \brief Checkup routine
748 *
749 * \return 0 if successful, or 1 if the test failed
750 */
751int x509_self_test( int verbose );
752
753#ifdef __cplusplus
754}
755#endif
756
757#endif /* x509.h */