blob: a5ea52d6bf6921b60e09a8f6fdb33fdea2d4618b [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 Bakker732e1a82011-12-11 16:35:09 +000062#define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */
63#define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */
64#define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000065/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakker37ca75d2011-01-06 12:28:03 +000067
68/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000069 * \name X509 Verify codes
70 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000071 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000072#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
73#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
74#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
75#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
76#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
77#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
78#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
79#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000080/* \} name */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000081/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83/*
84 * various object identifiers
85 */
86#define X520_COMMON_NAME 3
87#define X520_COUNTRY 6
88#define X520_LOCALITY 7
89#define X520_STATE 8
90#define X520_ORGANIZATION 10
91#define X520_ORG_UNIT 11
92#define PKCS9_EMAIL 1
93
94#define X509_OUTPUT_DER 0x01
95#define X509_OUTPUT_PEM 0x02
96#define PEM_LINE_LENGTH 72
97#define X509_ISSUER 0x01
98#define X509_SUBJECT 0x02
99
100#define OID_X520 "\x55\x04"
Paul Bakker74111d32011-01-15 16:57:55 +0000101#define OID_CN OID_X520 "\x03"
102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
Paul Bakker74111d32011-01-15 16:57:55 +0000104#define OID_PKCS1_RSA OID_PKCS1 "\x01"
Paul Bakker400ff6f2011-02-20 10:40:16 +0000105
106#define OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
Paul Bakker74111d32011-01-15 16:57:55 +0000107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
Paul Bakker74111d32011-01-15 16:57:55 +0000109#define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
110
111/** ISO arc for standard certificate and CRL extensions */
112#define OID_ID_CE "\x55\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
113
114/**
115 * Private Internet Extensions
116 * { iso(1) identified-organization(3) dod(6) internet(1)
117 * security(5) mechanisms(5) pkix(7) }
118 */
119#define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
120
121/*
122 * OIDs for standard certificate extensions
123 */
124#define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
125#define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
126#define OID_KEY_USAGE OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
127#define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
128#define OID_POLICY_MAPPINGS OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
129#define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
130#define OID_ISSUER_ALT_NAME OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
131#define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
132#define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
133#define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
134#define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
135#define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
136#define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
137#define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
138#define OID_FRESHEST_CRL OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
139
140/*
141 * X.509 v3 Key Usage Extension flags
142 */
143#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
144#define KU_NON_REPUDIATION (0x40) /* bit 1 */
145#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
146#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
147#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
148#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
149#define KU_CRL_SIGN (0x02) /* bit 6 */
150
151/*
152 * X.509 v3 Extended key usage OIDs
153 */
154#define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
155
156#define OID_KP OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
157#define OID_SERVER_AUTH OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
158#define OID_CLIENT_AUTH OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
159#define OID_CODE_SIGNING OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
160#define OID_EMAIL_PROTECTION OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
161#define OID_TIME_STAMPING OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
162#define OID_OCSP_SIGNING OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
163
164#define STRING_SERVER_AUTH "TLS Web Server Authentication"
165#define STRING_CLIENT_AUTH "TLS Web Client Authentication"
166#define STRING_CODE_SIGNING "Code Signing"
167#define STRING_EMAIL_PROTECTION "E-mail Protection"
168#define STRING_TIME_STAMPING "Time Stamping"
169#define STRING_OCSP_SIGNING "OCSP Signing"
170
171/*
172 * OIDs for CRL extensions
173 */
174#define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
175#define OID_CRL_NUMBER OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
176
177/*
178 * Netscape certificate extensions
179 */
180#define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
181#define OID_NS_CERT OID_NETSCAPE "\x01"
182#define OID_NS_CERT_TYPE OID_NS_CERT "\x01"
183#define OID_NS_BASE_URL OID_NS_CERT "\x02"
184#define OID_NS_REVOCATION_URL OID_NS_CERT "\x03"
185#define OID_NS_CA_REVOCATION_URL OID_NS_CERT "\x04"
186#define OID_NS_RENEWAL_URL OID_NS_CERT "\x07"
187#define OID_NS_CA_POLICY_URL OID_NS_CERT "\x08"
188#define OID_NS_SSL_SERVER_NAME OID_NS_CERT "\x0C"
189#define OID_NS_COMMENT OID_NS_CERT "\x0D"
190#define OID_NS_DATA_TYPE OID_NETSCAPE "\x02"
191#define OID_NS_CERT_SEQUENCE OID_NS_DATA_TYPE "\x05"
192
193/*
194 * Netscape certificate types
195 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
196 */
197
198#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
199#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
200#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
201#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
202#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
203#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
204#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
205#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
206
207#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
208#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
209#define EXT_KEY_USAGE (1 << 2)
210#define EXT_CERTIFICATE_POLICIES (1 << 3)
211#define EXT_POLICY_MAPPINGS (1 << 4)
212#define EXT_SUBJECT_ALT_NAME (1 << 5)
213#define EXT_ISSUER_ALT_NAME (1 << 6)
214#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
215#define EXT_BASIC_CONSTRAINTS (1 << 8)
216#define EXT_NAME_CONSTRAINTS (1 << 9)
217#define EXT_POLICY_CONSTRAINTS (1 << 10)
218#define EXT_EXTENDED_KEY_USAGE (1 << 11)
219#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
220#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
221#define EXT_FRESHEST_CRL (1 << 14)
222
223#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000225/*
226 * Storage format identifiers
227 * Recognized formats: PEM and DER
228 */
229#define X509_FORMAT_DER 1
230#define X509_FORMAT_PEM 2
231
Paul Bakker37ca75d2011-01-06 12:28:03 +0000232/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000233 * \addtogroup x509_module
234 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000235
236/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000237 * \name Structures for parsing X.509 certificates and CRLs
238 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000239 */
240
241/**
242 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000244typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Paul Bakker37ca75d2011-01-06 12:28:03 +0000246/**
Paul Bakker74111d32011-01-15 16:57:55 +0000247 * Container for ASN1 bit strings.
248 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000249typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000250
251/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000252 * Container for ASN1 named information objects.
253 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
254 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255typedef struct _x509_name
256{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000257 x509_buf oid; /**< The object identifier. */
258 x509_buf val; /**< The named value. */
259 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260}
261x509_name;
262
Paul Bakker74111d32011-01-15 16:57:55 +0000263/**
264 * Container for a sequence of ASN.1 items
265 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000266typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000267
Paul Bakker37ca75d2011-01-06 12:28:03 +0000268/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000269typedef struct _x509_time
270{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000271 int year, mon, day; /**< Date. */
272 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000273}
274x509_time;
275
Paul Bakker37ca75d2011-01-06 12:28:03 +0000276/**
277 * Container for an X.509 certificate. The certificate may be chained.
278 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000279typedef struct _x509_cert
280{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000281 x509_buf raw; /**< The raw certificate data (DER). */
282 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
Paul Bakker37ca75d2011-01-06 12:28:03 +0000284 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
285 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
286 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Paul Bakker37ca75d2011-01-06 12:28:03 +0000288 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
289 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Paul Bakker37ca75d2011-01-06 12:28:03 +0000291 x509_name issuer; /**< The parsed issuer data (named information object). */
292 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Paul Bakker37ca75d2011-01-06 12:28:03 +0000294 x509_time valid_from; /**< Start time of certificate validity. */
295 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Paul Bakker37ca75d2011-01-06 12:28:03 +0000297 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
298 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 +0000299
Paul Bakker37ca75d2011-01-06 12:28:03 +0000300 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
301 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
302 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
Paul Bakker74111d32011-01-15 16:57:55 +0000304 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000305 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
306 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
Paul Bakker74111d32011-01-15 16:57:55 +0000308 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
309
310 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
311
312 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
313
Paul Bakker37ca75d2011-01-06 12:28:03 +0000314 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
315 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
316 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Paul Bakker37ca75d2011-01-06 12:28:03 +0000318 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000319}
320x509_cert;
321
Paul Bakker37ca75d2011-01-06 12:28:03 +0000322/**
323 * Certificate revocation list entry.
324 * Contains the CA-specific serial numbers and revocation dates.
325 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326typedef struct _x509_crl_entry
327{
328 x509_buf raw;
329
330 x509_buf serial;
331
332 x509_time revocation_date;
333
334 x509_buf entry_ext;
335
336 struct _x509_crl_entry *next;
337}
338x509_crl_entry;
339
Paul Bakker37ca75d2011-01-06 12:28:03 +0000340/**
341 * Certificate revocation list structure.
342 * Every CRL may have multiple entries.
343 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344typedef struct _x509_crl
345{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000346 x509_buf raw; /**< The raw certificate data (DER). */
347 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348
349 int version;
350 x509_buf sig_oid1;
351
Paul Bakker37ca75d2011-01-06 12:28:03 +0000352 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000353
Paul Bakker37ca75d2011-01-06 12:28:03 +0000354 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker37ca75d2011-01-06 12:28:03 +0000356 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357 x509_time next_update;
358
Paul Bakker37ca75d2011-01-06 12:28:03 +0000359 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000360
361 x509_buf crl_ext;
362
363 x509_buf sig_oid2;
364 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000365 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000366
367 struct _x509_crl *next;
368}
369x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000370/** \} name Structures for parsing X.509 certificates and CRLs */
371/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000372
Paul Bakker37ca75d2011-01-06 12:28:03 +0000373/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000374 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000375 * XvP: commented out as they are not used.
376 * - <tt>typedef struct _x509_node x509_node;</tt>
377 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000379/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000380typedef struct _x509_node
381{
382 unsigned char *data;
383 unsigned char *p;
384 unsigned char *end;
385
386 size_t len;
387}
388x509_node;
389
390typedef struct _x509_raw
391{
392 x509_node raw;
393 x509_node tbs;
394
395 x509_node version;
396 x509_node serial;
397 x509_node tbs_signalg;
398 x509_node issuer;
399 x509_node validity;
400 x509_node subject;
401 x509_node subpubkey;
402
403 x509_node signalg;
404 x509_node sign;
405}
406x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000407*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
409#ifdef __cplusplus
410extern "C" {
411#endif
412
413/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000414 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
415 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000416 */
417
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000418/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000419/**
Paul Bakkerb5df3bf2013-06-19 12:08:47 +0200420 * \brief Parse a single DER formatted certificate and add it
421 * to the chained list.
422 *
423 * \param chain points to the start of the chain
424 * \param buf buffer holding the certificate DER data
425 * \param buflen size of the buffer
426 *
427 * \return 0 if successful, or a specific X509 or PEM error code
428 */
429int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen );
430
431/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 * \brief Parse one or more certificates and add them
Paul Bakker732e1a82011-12-11 16:35:09 +0000433 * to the chained list. Parses permissively. If some
434 * certificates can be parsed, the result is the number
435 * of failed certificates it encountered. If none complete
436 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 *
438 * \param chain points to the start of the chain
439 * \param buf buffer holding the certificate data
440 * \param buflen size of the buffer
441 *
Paul Bakker732e1a82011-12-11 16:35:09 +0000442 * \return 0 if all certificates parsed successfully, a positive number
443 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Paul Bakker732e1a82011-12-11 16:35:09 +0000445int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000447/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000448/**
449 * \brief Load one or more certificates and add them
Paul Bakker732e1a82011-12-11 16:35:09 +0000450 * to the chained list. Parses permissively. If some
451 * certificates can be parsed, the result is the number
452 * of failed certificates it encountered. If none complete
453 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 *
455 * \param chain points to the start of the chain
456 * \param path filename to read the certificates from
457 *
Paul Bakker732e1a82011-12-11 16:35:09 +0000458 * \return 0 if all certificates parsed successfully, a positive number
459 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 */
Paul Bakker732e1a82011-12-11 16:35:09 +0000461int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000463/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000464/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000465 * \brief Parse one or more CRLs and add them
466 * to the chained list
467 *
468 * \param chain points to the start of the chain
469 * \param buf buffer holding the CRL data
470 * \param buflen size of the buffer
471 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000472 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000473 */
Paul Bakker23986e52011-04-24 08:57:21 +0000474int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000475
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000476/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000477/**
478 * \brief Load one or more CRLs and add them
479 * to the chained list
480 *
481 * \param chain points to the start of the chain
482 * \param path filename to read the CRLs from
483 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000484 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000485 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000486int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000487
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000488/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000489/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 * \brief Parse a private RSA key
491 *
492 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000493 * \param key input buffer
494 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 * \param pwd password for decryption (optional)
496 * \param pwdlen size of the password
497 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000498 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 */
500int x509parse_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000501 const unsigned char *key, size_t keylen,
502 const unsigned char *pwd, size_t pwdlen );
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/**
506 * \brief Load and parse a private RSA key
507 *
508 * \param rsa RSA context to be initialized
509 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000510 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000512 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000514int x509parse_keyfile( rsa_context *rsa, const char *path,
515 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000516
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000517/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000518/**
Paul Bakker917e7542011-03-25 14:23:36 +0000519 * \brief Parse a public RSA key
520 *
521 * \param rsa RSA context to be initialized
522 * \param key input buffer
523 * \param keylen size of the buffer
524 *
525 * \return 0 if successful, or a specific X509 or PEM error code
526 */
527int x509parse_public_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000528 const unsigned char *key, size_t keylen );
Paul Bakker917e7542011-03-25 14:23:36 +0000529
530/** \ingroup x509_module */
531/**
532 * \brief Load and parse a public RSA key
533 *
534 * \param rsa RSA context to be initialized
535 * \param path filename to read the private key from
536 *
537 * \return 0 if successful, or a specific X509 or PEM error code
538 */
539int x509parse_public_keyfile( rsa_context *rsa, const char *path );
540
541/** \ingroup x509_module */
542/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000543 * \brief Parse DHM parameters
544 *
545 * \param dhm DHM context to be initialized
546 * \param dhmin input buffer
547 * \param dhminlen size of the buffer
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 Bakker23986e52011-04-24 08:57:21 +0000551int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
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 Load and parse DHM parameters
556 *
557 * \param dhm DHM context to be initialized
558 * \param path filename to read the DHM Parameters from
559 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000560 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000561 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000562int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000563
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000564/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000565
Paul Bakker5121ce52009-01-03 21:22:43 +0000566/**
567 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000568 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000569 *
570 * \param buf Buffer to write to
571 * \param size Maximum size of buffer
572 * \param dn The X509 name to represent
573 *
574 * \return The amount of data written to the buffer, or -1 in
575 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000577int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000580 * \brief Store the certificate serial in printable form into buf;
581 * no more than size characters will be written.
582 *
583 * \param buf Buffer to write to
584 * \param size Maximum size of buffer
585 * \param serial The X509 serial to represent
586 *
587 * \return The amount of data written to the buffer, or -1 in
588 * case of an error.
589 */
590int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
591
592/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 * \brief Returns an informational string about the
594 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000595 *
596 * \param buf Buffer to write to
597 * \param size Maximum size of buffer
598 * \param prefix A line prefix
599 * \param crt The X509 certificate to represent
600 *
601 * \return The amount of data written to the buffer, or -1 in
602 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604int x509parse_cert_info( char *buf, size_t size, const char *prefix,
605 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000606
607/**
608 * \brief Returns an informational string about the
609 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000610 *
611 * \param buf Buffer to write to
612 * \param size Maximum size of buffer
613 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000614 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000615 *
616 * \return The amount of data written to the buffer, or -1 in
617 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000618 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000619int x509parse_crl_info( char *buf, size_t size, const char *prefix,
620 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622/**
Paul Bakker74111d32011-01-15 16:57:55 +0000623 * \brief Give an known OID, return its descriptive string.
624 *
625 * \param oid buffer containing the oid
626 *
627 * \return Return a string if the OID is known,
628 * or NULL otherwise.
629 */
630const char *x509_oid_get_description( x509_buf *oid );
631
Paul Bakker089b70d2012-11-07 19:57:39 +0000632/**
Paul Bakker74111d32011-01-15 16:57:55 +0000633 * \brief Give an OID, return a string version of its OID number.
634 *
635 * \param buf Buffer to write to
636 * \param size Maximum size of buffer
637 * \param oid Buffer containing the OID
638 *
639 * \return The amount of data written to the buffer, or -1 in
640 * case of an error.
641 */
642int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
643
644/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000645 * \brief Check a given x509_time against the system time and check
646 * if it is valid.
647 *
648 * \param time x509_time to check
649 *
650 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000651 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000653int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000656 * \name Functions to verify a certificate
657 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000658 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000659/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000660/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * \brief Verify the certificate signature
662 *
663 * \param crt a certificate to be verified
664 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000665 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * \param cn expected Common Name (can be set to
667 * NULL if the CN must not be verified)
668 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000669 * \param f_vrfy verification function
670 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 *
Paul Bakker40e46942009-01-03 21:51:57 +0000672 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 * in which case *flags will have one or more of
674 * the following values set:
675 * BADCERT_EXPIRED --
676 * BADCERT_REVOKED --
677 * BADCERT_CN_MISMATCH --
678 * BADCERT_NOT_TRUSTED
679 *
680 * \note TODO: add two arguments, depth and crl
681 */
682int x509parse_verify( x509_cert *crt,
683 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000684 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000685 const char *cn, int *flags,
686 int (*f_vrfy)(void *, x509_cert *, int, int),
687 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Paul Bakker74111d32011-01-15 16:57:55 +0000689/**
690 * \brief Verify the certificate signature
691 *
692 * \param crt a certificate to be verified
693 * \param crl the CRL to verify against
694 *
695 * \return 1 if the certificate is revoked, 0 otherwise
696 *
697 */
698int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
699
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000700/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000701
702
703
704/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000705 * \name Functions to clear a certificate, CRL or private RSA key
706 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000707 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000708/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000709/**
710 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000711 *
712 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 */
714void x509_free( x509_cert *crt );
715
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000716/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000717/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000718 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000719 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000720 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000721 */
722void x509_crl_free( x509_crl *crl );
723
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000724/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000725
726
Paul Bakkerd98030e2009-05-02 15:13:40 +0000727/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 * \brief Checkup routine
729 *
730 * \return 0 if successful, or 1 if the test failed
731 */
732int x509_self_test( int verbose );
733
734#ifdef __cplusplus
735}
736#endif
737
738#endif /* x509.h */