blob: ed02ad9cf2dfbb06938c821df0c6695d88029885 [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 Bakkered27a042013-04-18 22:46:23 +020030#include "config.h"
31
32#if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C)
Paul Bakkerefc30292011-11-10 14:43:23 +000033#include "asn1.h"
Paul Bakker314052f2011-08-15 09:07:52 +000034#include "rsa.h"
35#include "dhm.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020036#include "md.h"
Paul Bakkered27a042013-04-18 22:46:23 +020037#include "pk.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker37ca75d2011-01-06 12:28:03 +000039/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000040 * \addtogroup x509_module
41 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000042 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000043
Paul Bakker37ca75d2011-01-06 12:28:03 +000044/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000045 * \name X509 Error codes
46 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000047 */
Paul Bakker9d781402011-05-09 16:17:09 +000048#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
49#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
50#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
51#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
52#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
53#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
54#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
55#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
56#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */
57#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */
58#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */
59#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */
60#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */
Paul Bakkered56b222011-07-13 11:26:43 +000061#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA is supported). */
Paul Bakker9d781402011-05-09 16:17:09 +000062#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
63#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
64#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */
65#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000066#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */
Paul Bakker69e095c2011-12-10 21:55:01 +000067#define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */
68#define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */
69#define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000070/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Paul Bakker37ca75d2011-01-06 12:28:03 +000072/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000073 * \name X509 Verify codes
74 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000075 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000076#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
77#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
78#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
79#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
80#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
81#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
82#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
83#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker915275b2012-09-28 07:10:55 +000084#define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000085/* \} name */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000086/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88/*
Paul Bakker74111d32011-01-15 16:57:55 +000089 * X.509 v3 Key Usage Extension flags
90 */
91#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
92#define KU_NON_REPUDIATION (0x40) /* bit 1 */
93#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
94#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
95#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
96#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
97#define KU_CRL_SIGN (0x02) /* bit 6 */
98
99/*
Paul Bakker74111d32011-01-15 16:57:55 +0000100 * Netscape certificate types
101 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
102 */
103
104#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
105#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
106#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
107#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
108#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
109#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
110#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
111#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
112
Paul Bakkerc70b9822013-04-07 22:00:46 +0200113/*
114 * X.509 extension types
115 */
Paul Bakker74111d32011-01-15 16:57:55 +0000116#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
117#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
118#define EXT_KEY_USAGE (1 << 2)
119#define EXT_CERTIFICATE_POLICIES (1 << 3)
120#define EXT_POLICY_MAPPINGS (1 << 4)
121#define EXT_SUBJECT_ALT_NAME (1 << 5)
122#define EXT_ISSUER_ALT_NAME (1 << 6)
123#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
124#define EXT_BASIC_CONSTRAINTS (1 << 8)
125#define EXT_NAME_CONSTRAINTS (1 << 9)
126#define EXT_POLICY_CONSTRAINTS (1 << 10)
127#define EXT_EXTENDED_KEY_USAGE (1 << 11)
128#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
129#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
130#define EXT_FRESHEST_CRL (1 << 14)
131
132#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000134/*
135 * Storage format identifiers
136 * Recognized formats: PEM and DER
137 */
138#define X509_FORMAT_DER 1
139#define X509_FORMAT_PEM 2
140
Paul Bakker37ca75d2011-01-06 12:28:03 +0000141/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000142 * \addtogroup x509_module
143 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000144
145/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000146 * \name Structures for parsing X.509 certificates and CRLs
147 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000148 */
149
150/**
151 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000153typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakker37ca75d2011-01-06 12:28:03 +0000155/**
Paul Bakker74111d32011-01-15 16:57:55 +0000156 * Container for ASN1 bit strings.
157 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000158typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000159
160/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000161 * Container for ASN1 named information objects.
162 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
163 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000164typedef struct _x509_name
165{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000166 x509_buf oid; /**< The object identifier. */
167 x509_buf val; /**< The named value. */
168 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000169}
170x509_name;
171
Paul Bakker74111d32011-01-15 16:57:55 +0000172/**
173 * Container for a sequence of ASN.1 items
174 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000175typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000176
Paul Bakker37ca75d2011-01-06 12:28:03 +0000177/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178typedef struct _x509_time
179{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000180 int year, mon, day; /**< Date. */
181 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000182}
183x509_time;
184
Paul Bakker37ca75d2011-01-06 12:28:03 +0000185/**
186 * Container for an X.509 certificate. The certificate may be chained.
187 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000188typedef struct _x509_cert
189{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000190 x509_buf raw; /**< The raw certificate data (DER). */
191 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Paul Bakker37ca75d2011-01-06 12:28:03 +0000193 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
194 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
195 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Paul Bakker37ca75d2011-01-06 12:28:03 +0000197 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
198 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Paul Bakker37ca75d2011-01-06 12:28:03 +0000200 x509_name issuer; /**< The parsed issuer data (named information object). */
201 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Paul Bakker37ca75d2011-01-06 12:28:03 +0000203 x509_time valid_from; /**< Start time of certificate validity. */
204 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Paul Bakker37ca75d2011-01-06 12:28:03 +0000206 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
207 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 +0000208
Paul Bakker37ca75d2011-01-06 12:28:03 +0000209 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
210 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
211 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakkera8cd2392012-02-11 16:09:32 +0000212 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker74111d32011-01-15 16:57:55 +0000214 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000215 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
Paul Bakker915275b2012-09-28 07:10:55 +0000216 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakker74111d32011-01-15 16:57:55 +0000218 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
219
220 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
221
222 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
223
Paul Bakker37ca75d2011-01-06 12:28:03 +0000224 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
225 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200226 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
227 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Paul Bakker37ca75d2011-01-06 12:28:03 +0000229 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230}
231x509_cert;
232
Paul Bakker37ca75d2011-01-06 12:28:03 +0000233/**
234 * Certificate revocation list entry.
235 * Contains the CA-specific serial numbers and revocation dates.
236 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000237typedef struct _x509_crl_entry
238{
239 x509_buf raw;
240
241 x509_buf serial;
242
243 x509_time revocation_date;
244
245 x509_buf entry_ext;
246
247 struct _x509_crl_entry *next;
248}
249x509_crl_entry;
250
Paul Bakker37ca75d2011-01-06 12:28:03 +0000251/**
252 * Certificate revocation list structure.
253 * Every CRL may have multiple entries.
254 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000255typedef struct _x509_crl
256{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000257 x509_buf raw; /**< The raw certificate data (DER). */
258 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000259
260 int version;
261 x509_buf sig_oid1;
262
Paul Bakker37ca75d2011-01-06 12:28:03 +0000263 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000264
Paul Bakker37ca75d2011-01-06 12:28:03 +0000265 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000266
Paul Bakker37ca75d2011-01-06 12:28:03 +0000267 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000268 x509_time next_update;
269
Paul Bakker37ca75d2011-01-06 12:28:03 +0000270 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000271
272 x509_buf crl_ext;
273
274 x509_buf sig_oid2;
275 x509_buf sig;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200276 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
277 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000278
279 struct _x509_crl *next;
280}
281x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000282/** \} name Structures for parsing X.509 certificates and CRLs */
283/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000284
Paul Bakker37ca75d2011-01-06 12:28:03 +0000285/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000286 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000287 * XvP: commented out as they are not used.
288 * - <tt>typedef struct _x509_node x509_node;</tt>
289 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000291/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000292typedef struct _x509_node
293{
294 unsigned char *data;
295 unsigned char *p;
296 unsigned char *end;
297
298 size_t len;
299}
300x509_node;
301
302typedef struct _x509_raw
303{
304 x509_node raw;
305 x509_node tbs;
306
307 x509_node version;
308 x509_node serial;
309 x509_node tbs_signalg;
310 x509_node issuer;
311 x509_node validity;
312 x509_node subject;
313 x509_node subpubkey;
314
315 x509_node signalg;
316 x509_node sign;
317}
318x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000319*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321#ifdef __cplusplus
322extern "C" {
323#endif
324
325/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000326 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
327 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000328 */
329
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000330/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000331/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 * \brief Parse one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000333 * to the chained list. Parses permissively. If some
334 * certificates can be parsed, the result is the number
335 * of failed certificates it encountered. If none complete
336 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 *
338 * \param chain points to the start of the chain
339 * \param buf buffer holding the certificate data
340 * \param buflen size of the buffer
341 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000342 * \return 0 if all certificates parsed successfully, a positive number
343 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000345int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000347/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000348/**
349 * \brief Load one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000350 * to the chained list. Parses permissively. If some
351 * certificates can be parsed, the result is the number
352 * of failed certificates it encountered. If none complete
353 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 *
355 * \param chain points to the start of the chain
356 * \param path filename to read the certificates from
357 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000358 * \return 0 if all certificates parsed successfully, a positive number
359 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000361int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000363/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000364/**
Paul Bakker8d914582012-06-04 12:46:42 +0000365 * \brief Load one or more certificate files from a path and add them
366 * to the chained list. Parses permissively. If some
367 * certificates can be parsed, the result is the number
368 * of failed certificates it encountered. If none complete
369 * correctly, the first error is returned.
370 *
371 * \param chain points to the start of the chain
372 * \param path directory / folder to read the certificate files from
373 *
374 * \return 0 if all certificates parsed successfully, a positive number
375 * if partly successful or a specific X509 or PEM error code
376 */
377int x509parse_crtpath( x509_cert *chain, const char *path );
378
379/** \ingroup x509_module */
380/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000381 * \brief Parse one or more CRLs and add them
382 * to the chained list
383 *
384 * \param chain points to the start of the chain
385 * \param buf buffer holding the CRL data
386 * \param buflen size of the buffer
387 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000388 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000389 */
Paul Bakker23986e52011-04-24 08:57:21 +0000390int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000391
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000392/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000393/**
394 * \brief Load one or more CRLs and add them
395 * to the chained list
396 *
397 * \param chain points to the start of the chain
398 * \param path filename to read the CRLs from
399 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000400 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000401 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000402int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000404/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000405/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 * \brief Parse a private RSA key
407 *
408 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000409 * \param key input buffer
410 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 * \param pwd password for decryption (optional)
412 * \param pwdlen size of the password
413 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000414 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 */
416int x509parse_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000417 const unsigned char *key, size_t keylen,
418 const unsigned char *pwd, size_t pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000420/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000421/**
422 * \brief Load and parse a private RSA key
423 *
424 * \param rsa RSA context to be initialized
425 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000426 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000428 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000430int x509parse_keyfile( rsa_context *rsa, const char *path,
431 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000432
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000433/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000434/**
Paul Bakker917e7542011-03-25 14:23:36 +0000435 * \brief Parse a public RSA key
436 *
437 * \param rsa RSA context to be initialized
438 * \param key input buffer
439 * \param keylen size of the buffer
440 *
441 * \return 0 if successful, or a specific X509 or PEM error code
442 */
443int x509parse_public_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000444 const unsigned char *key, size_t keylen );
Paul Bakker917e7542011-03-25 14:23:36 +0000445
446/** \ingroup x509_module */
447/**
448 * \brief Load and parse a public RSA key
449 *
450 * \param rsa RSA context to be initialized
451 * \param path filename to read the private key from
452 *
453 * \return 0 if successful, or a specific X509 or PEM error code
454 */
455int x509parse_public_keyfile( rsa_context *rsa, const char *path );
456
457/** \ingroup x509_module */
458/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000459 * \brief Parse DHM parameters
460 *
461 * \param dhm DHM context to be initialized
462 * \param dhmin input buffer
463 * \param dhminlen size of the buffer
464 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000465 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000466 */
Paul Bakker23986e52011-04-24 08:57:21 +0000467int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
Paul Bakker1b57b062011-01-06 15:48:19 +0000468
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000469/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000470/**
471 * \brief Load and parse DHM parameters
472 *
473 * \param dhm DHM context to be initialized
474 * \param path filename to read the DHM Parameters from
475 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000476 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000477 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000478int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000479
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000480/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000481
Paul Bakker5121ce52009-01-03 21:22:43 +0000482/**
483 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000484 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000485 *
486 * \param buf Buffer to write to
487 * \param size Maximum size of buffer
488 * \param dn The X509 name to represent
489 *
490 * \return The amount of data written to the buffer, or -1 in
491 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000493int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000496 * \brief Store the certificate serial in printable form into buf;
497 * no more than size characters will be written.
498 *
499 * \param buf Buffer to write to
500 * \param size Maximum size of buffer
501 * \param serial The X509 serial to represent
502 *
503 * \return The amount of data written to the buffer, or -1 in
504 * case of an error.
505 */
506int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
507
508/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 * \brief Returns an informational string about the
510 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000511 *
512 * \param buf Buffer to write to
513 * \param size Maximum size of buffer
514 * \param prefix A line prefix
515 * \param crt The X509 certificate to represent
516 *
517 * \return The amount of data written to the buffer, or -1 in
518 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000520int x509parse_cert_info( char *buf, size_t size, const char *prefix,
521 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000522
523/**
524 * \brief Returns an informational string about the
525 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000526 *
527 * \param buf Buffer to write to
528 * \param size Maximum size of buffer
529 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000530 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000531 *
532 * \return The amount of data written to the buffer, or -1 in
533 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000534 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000535int x509parse_crl_info( char *buf, size_t size, const char *prefix,
536 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538/**
Paul Bakker74111d32011-01-15 16:57:55 +0000539 * \brief Give an known OID, return its descriptive string.
540 *
541 * \param oid buffer containing the oid
542 *
543 * \return Return a string if the OID is known,
544 * or NULL otherwise.
545 */
546const char *x509_oid_get_description( x509_buf *oid );
547
Paul Bakker9a736322012-11-14 12:39:52 +0000548/**
Paul Bakker74111d32011-01-15 16:57:55 +0000549 * \brief Give an OID, return a string version of its OID number.
Paul Bakkerd5c2b542013-04-07 22:34:26 +0200550 * (Deprecated. Use oid_get_numeric_string() instead)
Paul Bakker74111d32011-01-15 16:57:55 +0000551 *
552 * \param buf Buffer to write to
553 * \param size Maximum size of buffer
554 * \param oid Buffer containing the OID
555 *
556 * \return The amount of data written to the buffer, or -1 in
557 * case of an error.
558 */
559int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
560
561/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000562 * \brief Check a given x509_time against the system time and check
563 * if it is valid.
564 *
565 * \param time x509_time to check
566 *
567 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000568 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000570int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000573 * \name Functions to verify a certificate
574 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000575 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000576/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000577/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 * \brief Verify the certificate signature
579 *
Paul Bakker915275b2012-09-28 07:10:55 +0000580 * The verify callback is a user-supplied callback that
581 * can clear / modify / add flags for a certificate. If set,
582 * the verification callback is called for each
583 * certificate in the chain (from the trust-ca down to the
584 * presented crt). The parameters for the callback are:
585 * (void *parameter, x509_cert *crt, int certificate_depth,
586 * int *flags). With the flags representing current flags for
587 * that specific certificate and the certificate depth from
Paul Bakker9a736322012-11-14 12:39:52 +0000588 * the bottom (Peer cert depth = 0).
Paul Bakker915275b2012-09-28 07:10:55 +0000589 *
590 * All flags left after returning from the callback
591 * are also returned to the application. The function should
592 * return 0 for anything but a fatal error.
593 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 * \param crt a certificate to be verified
595 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000596 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 * \param cn expected Common Name (can be set to
598 * NULL if the CN must not be verified)
599 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000600 * \param f_vrfy verification function
601 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 *
Paul Bakker40e46942009-01-03 21:51:57 +0000603 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * in which case *flags will have one or more of
605 * the following values set:
606 * BADCERT_EXPIRED --
607 * BADCERT_REVOKED --
608 * BADCERT_CN_MISMATCH --
609 * BADCERT_NOT_TRUSTED
Paul Bakker915275b2012-09-28 07:10:55 +0000610 * or another error in case of a fatal error encountered
611 * during the verification process.
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 */
613int x509parse_verify( x509_cert *crt,
614 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000615 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000616 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +0000617 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000618 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Paul Bakker74111d32011-01-15 16:57:55 +0000620/**
621 * \brief Verify the certificate signature
622 *
623 * \param crt a certificate to be verified
624 * \param crl the CRL to verify against
625 *
626 * \return 1 if the certificate is revoked, 0 otherwise
627 *
628 */
629int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
630
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000631/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000632
633
634
635/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000636 * \name Functions to clear a certificate, CRL or private RSA key
637 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000638 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000639/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000640/**
641 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000642 *
643 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 */
645void x509_free( x509_cert *crt );
646
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000647/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000648/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000649 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000650 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000651 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000652 */
653void x509_crl_free( x509_crl *crl );
654
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000655/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000656
657
Paul Bakkerd98030e2009-05-02 15:13:40 +0000658/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 * \brief Checkup routine
660 *
661 * \return 0 if successful, or 1 if the test failed
662 */
663int x509_self_test( int verbose );
664
665#ifdef __cplusplus
666}
667#endif
668
Paul Bakkered27a042013-04-18 22:46:23 +0200669#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000670#endif /* x509.h */