blob: 11f1a3108817a29171c94a847e24b05d1a600bfc [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 Bakkerefc30292011-11-10 14:43:23 +00006 * Copyright (C) 2006-2011, 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 Bakkerefc30292011-11-10 14:43:23 +000030#include "asn1.h"
Paul Bakker314052f2011-08-15 09:07:52 +000031#include "rsa.h"
32#include "dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker37ca75d2011-01-06 12:28:03 +000034/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000035 * \addtogroup x509_module
36 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000037 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000038
Paul Bakker37ca75d2011-01-06 12:28:03 +000039/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000040 * \name X509 Error codes
41 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000042 */
Paul Bakker9d781402011-05-09 16:17:09 +000043#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
44#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
45#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
46#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
47#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
48#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
49#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
50#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
51#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */
52#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */
53#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */
54#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */
55#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */
Paul Bakkered56b222011-07-13 11:26:43 +000056#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA is supported). */
Paul Bakker9d781402011-05-09 16:17:09 +000057#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
58#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
59#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */
60#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000061#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */
Paul Bakker9d781402011-05-09 16:17:09 +000062#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x2A00 /**< Not used. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000063/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Paul Bakker37ca75d2011-01-06 12:28:03 +000065
66/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000067 * \name X509 Verify codes
68 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000069 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000070#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
71#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
72#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
73#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
74#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
75#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
76#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
77#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000078/* \} name */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000079/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/*
82 * various object identifiers
83 */
84#define X520_COMMON_NAME 3
85#define X520_COUNTRY 6
86#define X520_LOCALITY 7
87#define X520_STATE 8
88#define X520_ORGANIZATION 10
89#define X520_ORG_UNIT 11
90#define PKCS9_EMAIL 1
91
92#define X509_OUTPUT_DER 0x01
93#define X509_OUTPUT_PEM 0x02
94#define PEM_LINE_LENGTH 72
95#define X509_ISSUER 0x01
96#define X509_SUBJECT 0x02
97
98#define OID_X520 "\x55\x04"
Paul Bakker74111d32011-01-15 16:57:55 +000099#define OID_CN OID_X520 "\x03"
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
Paul Bakker74111d32011-01-15 16:57:55 +0000102#define OID_PKCS1_RSA OID_PKCS1 "\x01"
Paul Bakker400ff6f2011-02-20 10:40:16 +0000103
104#define OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
Paul Bakker74111d32011-01-15 16:57:55 +0000105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
Paul Bakker74111d32011-01-15 16:57:55 +0000107#define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
108
109/** ISO arc for standard certificate and CRL extensions */
110#define OID_ID_CE "\x55\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
111
112/**
113 * Private Internet Extensions
114 * { iso(1) identified-organization(3) dod(6) internet(1)
115 * security(5) mechanisms(5) pkix(7) }
116 */
117#define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
118
119/*
120 * OIDs for standard certificate extensions
121 */
122#define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
123#define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
124#define OID_KEY_USAGE OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
125#define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
126#define OID_POLICY_MAPPINGS OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
127#define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
128#define OID_ISSUER_ALT_NAME OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
129#define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
130#define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
131#define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
132#define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
133#define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
134#define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
135#define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
136#define OID_FRESHEST_CRL OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
137
138/*
139 * X.509 v3 Key Usage Extension flags
140 */
141#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
142#define KU_NON_REPUDIATION (0x40) /* bit 1 */
143#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
144#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
145#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
146#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
147#define KU_CRL_SIGN (0x02) /* bit 6 */
148
149/*
150 * X.509 v3 Extended key usage OIDs
151 */
152#define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
153
154#define OID_KP OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
155#define OID_SERVER_AUTH OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
156#define OID_CLIENT_AUTH OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
157#define OID_CODE_SIGNING OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
158#define OID_EMAIL_PROTECTION OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
159#define OID_TIME_STAMPING OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
160#define OID_OCSP_SIGNING OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
161
162#define STRING_SERVER_AUTH "TLS Web Server Authentication"
163#define STRING_CLIENT_AUTH "TLS Web Client Authentication"
164#define STRING_CODE_SIGNING "Code Signing"
165#define STRING_EMAIL_PROTECTION "E-mail Protection"
166#define STRING_TIME_STAMPING "Time Stamping"
167#define STRING_OCSP_SIGNING "OCSP Signing"
168
169/*
170 * OIDs for CRL extensions
171 */
172#define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
173#define OID_CRL_NUMBER OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
174
175/*
176 * Netscape certificate extensions
177 */
178#define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
179#define OID_NS_CERT OID_NETSCAPE "\x01"
180#define OID_NS_CERT_TYPE OID_NS_CERT "\x01"
181#define OID_NS_BASE_URL OID_NS_CERT "\x02"
182#define OID_NS_REVOCATION_URL OID_NS_CERT "\x03"
183#define OID_NS_CA_REVOCATION_URL OID_NS_CERT "\x04"
184#define OID_NS_RENEWAL_URL OID_NS_CERT "\x07"
185#define OID_NS_CA_POLICY_URL OID_NS_CERT "\x08"
186#define OID_NS_SSL_SERVER_NAME OID_NS_CERT "\x0C"
187#define OID_NS_COMMENT OID_NS_CERT "\x0D"
188#define OID_NS_DATA_TYPE OID_NETSCAPE "\x02"
189#define OID_NS_CERT_SEQUENCE OID_NS_DATA_TYPE "\x05"
190
191/*
192 * Netscape certificate types
193 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
194 */
195
196#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
197#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
198#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
199#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
200#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
201#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
202#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
203#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
204
205#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
206#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
207#define EXT_KEY_USAGE (1 << 2)
208#define EXT_CERTIFICATE_POLICIES (1 << 3)
209#define EXT_POLICY_MAPPINGS (1 << 4)
210#define EXT_SUBJECT_ALT_NAME (1 << 5)
211#define EXT_ISSUER_ALT_NAME (1 << 6)
212#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
213#define EXT_BASIC_CONSTRAINTS (1 << 8)
214#define EXT_NAME_CONSTRAINTS (1 << 9)
215#define EXT_POLICY_CONSTRAINTS (1 << 10)
216#define EXT_EXTENDED_KEY_USAGE (1 << 11)
217#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
218#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
219#define EXT_FRESHEST_CRL (1 << 14)
220
221#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000223/*
224 * Storage format identifiers
225 * Recognized formats: PEM and DER
226 */
227#define X509_FORMAT_DER 1
228#define X509_FORMAT_PEM 2
229
230#define X509_NON_PERMISSIVE 0
231#define X509_PERMISSIVE 1
232
233
Paul Bakker37ca75d2011-01-06 12:28:03 +0000234/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000235 * \addtogroup x509_module
236 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000237
238/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000239 * \name Structures for parsing X.509 certificates and CRLs
240 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000241 */
242
243/**
244 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000246typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Paul Bakker37ca75d2011-01-06 12:28:03 +0000248/**
Paul Bakker74111d32011-01-15 16:57:55 +0000249 * Container for ASN1 bit strings.
250 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000251typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000252
253/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000254 * Container for ASN1 named information objects.
255 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
256 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000257typedef struct _x509_name
258{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000259 x509_buf oid; /**< The object identifier. */
260 x509_buf val; /**< The named value. */
261 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000262}
263x509_name;
264
Paul Bakker74111d32011-01-15 16:57:55 +0000265/**
266 * Container for a sequence of ASN.1 items
267 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000268typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000269
Paul Bakker37ca75d2011-01-06 12:28:03 +0000270/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000271typedef struct _x509_time
272{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000273 int year, mon, day; /**< Date. */
274 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000275}
276x509_time;
277
Paul Bakker37ca75d2011-01-06 12:28:03 +0000278/**
279 * Container for an X.509 certificate. The certificate may be chained.
280 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000281typedef struct _x509_cert
282{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000283 x509_buf raw; /**< The raw certificate data (DER). */
284 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
Paul Bakker37ca75d2011-01-06 12:28:03 +0000286 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
287 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
288 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Paul Bakker37ca75d2011-01-06 12:28:03 +0000290 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
291 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Paul Bakker37ca75d2011-01-06 12:28:03 +0000293 x509_name issuer; /**< The parsed issuer data (named information object). */
294 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Paul Bakker37ca75d2011-01-06 12:28:03 +0000296 x509_time valid_from; /**< Start time of certificate validity. */
297 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Paul Bakker37ca75d2011-01-06 12:28:03 +0000299 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
300 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 +0000301
Paul Bakker37ca75d2011-01-06 12:28:03 +0000302 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
303 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
304 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
Paul Bakker74111d32011-01-15 16:57:55 +0000306 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000307 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
308 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Paul Bakker74111d32011-01-15 16:57:55 +0000310 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
311
312 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
313
314 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
315
Paul Bakker37ca75d2011-01-06 12:28:03 +0000316 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
317 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
318 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Paul Bakker37ca75d2011-01-06 12:28:03 +0000320 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000321}
322x509_cert;
323
Paul Bakker37ca75d2011-01-06 12:28:03 +0000324/**
325 * Certificate revocation list entry.
326 * Contains the CA-specific serial numbers and revocation dates.
327 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328typedef struct _x509_crl_entry
329{
330 x509_buf raw;
331
332 x509_buf serial;
333
334 x509_time revocation_date;
335
336 x509_buf entry_ext;
337
338 struct _x509_crl_entry *next;
339}
340x509_crl_entry;
341
Paul Bakker37ca75d2011-01-06 12:28:03 +0000342/**
343 * Certificate revocation list structure.
344 * Every CRL may have multiple entries.
345 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000346typedef struct _x509_crl
347{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000348 x509_buf raw; /**< The raw certificate data (DER). */
349 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000350
351 int version;
352 x509_buf sig_oid1;
353
Paul Bakker37ca75d2011-01-06 12:28:03 +0000354 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker37ca75d2011-01-06 12:28:03 +0000356 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357
Paul Bakker37ca75d2011-01-06 12:28:03 +0000358 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000359 x509_time next_update;
360
Paul Bakker37ca75d2011-01-06 12:28:03 +0000361 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000362
363 x509_buf crl_ext;
364
365 x509_buf sig_oid2;
366 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000367 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368
369 struct _x509_crl *next;
370}
371x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000372/** \} name Structures for parsing X.509 certificates and CRLs */
373/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000374
Paul Bakker37ca75d2011-01-06 12:28:03 +0000375/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000376 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000377 * XvP: commented out as they are not used.
378 * - <tt>typedef struct _x509_node x509_node;</tt>
379 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000381/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000382typedef struct _x509_node
383{
384 unsigned char *data;
385 unsigned char *p;
386 unsigned char *end;
387
388 size_t len;
389}
390x509_node;
391
392typedef struct _x509_raw
393{
394 x509_node raw;
395 x509_node tbs;
396
397 x509_node version;
398 x509_node serial;
399 x509_node tbs_signalg;
400 x509_node issuer;
401 x509_node validity;
402 x509_node subject;
403 x509_node subpubkey;
404
405 x509_node signalg;
406 x509_node sign;
407}
408x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000409*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411#ifdef __cplusplus
412extern "C" {
413#endif
414
415/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000416 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
417 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000418 */
419
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000420/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000421/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 * \brief Parse one or more certificates and add them
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000423 * to the chained list. With permissive parsing enabled
424 * all certificates that cannot be parsed are ignored.
425 * If none complete correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 *
427 * \param chain points to the start of the chain
428 * \param buf buffer holding the certificate data
429 * \param buflen size of the buffer
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000430 * \param permissive X509_PERMISSIVE or X509_NON_PERMISSIVE
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000432 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000434int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen,
435 int permissive );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000437/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000438/**
439 * \brief Load one or more certificates and add them
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000440 * to the chained list. With permissive parsing enabled
441 * all certificates that cannot be parsed are ignored.
442 * If none complete correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 *
444 * \param chain points to the start of the chain
445 * \param path filename to read the certificates from
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000446 * \param permissive X509_PERMISSIVE or X509_NON_PERMISSIVE
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000448 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000450int x509parse_crtfile( x509_cert *chain, const char *path, int permissive );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000452/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000453/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000454 * \brief Parse one or more CRLs and add them
455 * to the chained list
456 *
457 * \param chain points to the start of the chain
458 * \param buf buffer holding the CRL data
459 * \param buflen size of the buffer
460 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000461 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000462 */
Paul Bakker23986e52011-04-24 08:57:21 +0000463int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000464
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000465/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000466/**
467 * \brief Load one or more CRLs and add them
468 * to the chained list
469 *
470 * \param chain points to the start of the chain
471 * \param path filename to read the CRLs from
472 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000473 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000474 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000475int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000476
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000477/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000478/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 * \brief Parse a private RSA key
480 *
481 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000482 * \param key input buffer
483 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 * \param pwd password for decryption (optional)
485 * \param pwdlen size of the password
486 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000487 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 */
489int x509parse_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000490 const unsigned char *key, size_t keylen,
491 const unsigned char *pwd, size_t pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000493/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000494/**
495 * \brief Load and parse a private RSA key
496 *
497 * \param rsa RSA context to be initialized
498 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000499 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000501 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000503int x509parse_keyfile( rsa_context *rsa, const char *path,
504 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000505
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000506/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000507/**
Paul Bakker917e7542011-03-25 14:23:36 +0000508 * \brief Parse a public RSA key
509 *
510 * \param rsa RSA context to be initialized
511 * \param key input buffer
512 * \param keylen size of the buffer
513 *
514 * \return 0 if successful, or a specific X509 or PEM error code
515 */
516int x509parse_public_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000517 const unsigned char *key, size_t keylen );
Paul Bakker917e7542011-03-25 14:23:36 +0000518
519/** \ingroup x509_module */
520/**
521 * \brief Load and parse a public RSA key
522 *
523 * \param rsa RSA context to be initialized
524 * \param path filename to read the private key from
525 *
526 * \return 0 if successful, or a specific X509 or PEM error code
527 */
528int x509parse_public_keyfile( rsa_context *rsa, const char *path );
529
530/** \ingroup x509_module */
531/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000532 * \brief Parse DHM parameters
533 *
534 * \param dhm DHM context to be initialized
535 * \param dhmin input buffer
536 * \param dhminlen size of the buffer
537 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000538 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000539 */
Paul Bakker23986e52011-04-24 08:57:21 +0000540int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
Paul Bakker1b57b062011-01-06 15:48:19 +0000541
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000542/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000543/**
544 * \brief Load and parse DHM parameters
545 *
546 * \param dhm DHM context to be initialized
547 * \param path filename to read the DHM Parameters from
548 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000549 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000550 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000551int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000552
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000553/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000554
555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557/**
558 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000559 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000560 *
561 * \param buf Buffer to write to
562 * \param size Maximum size of buffer
563 * \param dn The X509 name to represent
564 *
565 * \return The amount of data written to the buffer, or -1 in
566 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000571 * \brief Store the certificate serial in printable form into buf;
572 * no more than size characters will be written.
573 *
574 * \param buf Buffer to write to
575 * \param size Maximum size of buffer
576 * \param serial The X509 serial to represent
577 *
578 * \return The amount of data written to the buffer, or -1 in
579 * case of an error.
580 */
581int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
582
583/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 * \brief Returns an informational string about the
585 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000586 *
587 * \param buf Buffer to write to
588 * \param size Maximum size of buffer
589 * \param prefix A line prefix
590 * \param crt The X509 certificate to represent
591 *
592 * \return The amount of data written to the buffer, or -1 in
593 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000595int x509parse_cert_info( char *buf, size_t size, const char *prefix,
596 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597
598/**
599 * \brief Returns an informational string about the
600 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000601 *
602 * \param buf Buffer to write to
603 * \param size Maximum size of buffer
604 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000605 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000606 *
607 * \return The amount of data written to the buffer, or -1 in
608 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000609 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000610int x509parse_crl_info( char *buf, size_t size, const char *prefix,
611 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613/**
Paul Bakker74111d32011-01-15 16:57:55 +0000614 * \brief Give an known OID, return its descriptive string.
615 *
616 * \param oid buffer containing the oid
617 *
618 * \return Return a string if the OID is known,
619 * or NULL otherwise.
620 */
621const char *x509_oid_get_description( x509_buf *oid );
622
623/*
624 * \brief Give an OID, return a string version of its OID number.
625 *
626 * \param buf Buffer to write to
627 * \param size Maximum size of buffer
628 * \param oid Buffer containing the OID
629 *
630 * \return The amount of data written to the buffer, or -1 in
631 * case of an error.
632 */
633int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
634
635/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000636 * \brief Check a given x509_time against the system time and check
637 * if it is valid.
638 *
639 * \param time x509_time to check
640 *
641 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000642 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000644int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000647 * \name Functions to verify a certificate
648 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000649 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000650/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000651/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 * \brief Verify the certificate signature
653 *
654 * \param crt a certificate to be verified
655 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000656 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 * \param cn expected Common Name (can be set to
658 * NULL if the CN must not be verified)
659 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000660 * \param f_vrfy verification function
661 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 *
Paul Bakker40e46942009-01-03 21:51:57 +0000663 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 * in which case *flags will have one or more of
665 * the following values set:
666 * BADCERT_EXPIRED --
667 * BADCERT_REVOKED --
668 * BADCERT_CN_MISMATCH --
669 * BADCERT_NOT_TRUSTED
670 *
671 * \note TODO: add two arguments, depth and crl
672 */
673int x509parse_verify( x509_cert *crt,
674 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000675 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000676 const char *cn, int *flags,
677 int (*f_vrfy)(void *, x509_cert *, int, int),
678 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Paul Bakker74111d32011-01-15 16:57:55 +0000680/**
681 * \brief Verify the certificate signature
682 *
683 * \param crt a certificate to be verified
684 * \param crl the CRL to verify against
685 *
686 * \return 1 if the certificate is revoked, 0 otherwise
687 *
688 */
689int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
690
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000691/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000692
693
694
695/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000696 * \name Functions to clear a certificate, CRL or private RSA key
697 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000698 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000699/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000700/**
701 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000702 *
703 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 */
705void x509_free( x509_cert *crt );
706
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000707/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000708/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000709 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000710 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000711 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000712 */
713void x509_crl_free( x509_crl *crl );
714
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000715/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000716
717
Paul Bakkerd98030e2009-05-02 15:13:40 +0000718/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 * \brief Checkup routine
720 *
721 * \return 0 if successful, or 1 if the test failed
722 */
723int x509_self_test( int verbose );
724
725#ifdef __cplusplus
726}
727#endif
728
729#endif /* x509.h */