blob: 3c66e60b7e67058bc0db722ac223612f8721e669 [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 Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, 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 "dhm.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020035#include "md.h"
Paul Bakkered27a042013-04-18 22:46:23 +020036#include "pk.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker37ca75d2011-01-06 12:28:03 +000038/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000039 * \addtogroup x509_module
40 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000041 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000042
Paul Bakker37ca75d2011-01-06 12:28:03 +000043/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000044 * \name X509 Error codes
45 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000046 */
Paul Bakker9d781402011-05-09 16:17:09 +000047#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
48#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
49#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
50#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
51#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
52#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
53#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
54#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020055#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2480 /**< The signature tag or value invalid. */
56#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2500 /**< The extension tag or value is invalid. */
57#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2580 /**< Certificate or CRL has an unsupported version number. */
58#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2600 /**< Signature algorithm (oid) is unsupported. */
59#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2680 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
60#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2700 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
61#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780 /**< Format not recognized as DER or PEM. */
62#define POLARSSL_ERR_X509_INVALID_INPUT -0x2800 /**< Input invalid. */
63#define POLARSSL_ERR_X509_MALLOC_FAILED -0x2880 /**< Allocation of memory failed. */
64#define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2900 /**< 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/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000068 * \name X509 Verify codes
69 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000070 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000071#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
72#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
73#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
74#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
75#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
76#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
77#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
78#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker915275b2012-09-28 07:10:55 +000079#define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
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/*
Paul Bakker74111d32011-01-15 16:57:55 +000084 * X.509 v3 Key Usage Extension flags
85 */
86#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
87#define KU_NON_REPUDIATION (0x40) /* bit 1 */
88#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
89#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
90#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
91#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
92#define KU_CRL_SIGN (0x02) /* bit 6 */
93
94/*
Paul Bakker74111d32011-01-15 16:57:55 +000095 * Netscape certificate types
96 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
97 */
98
99#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
100#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
101#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
102#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
103#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
104#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
105#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
106#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
107
Paul Bakkerc70b9822013-04-07 22:00:46 +0200108/*
109 * X.509 extension types
110 */
Paul Bakker74111d32011-01-15 16:57:55 +0000111#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
112#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
113#define EXT_KEY_USAGE (1 << 2)
114#define EXT_CERTIFICATE_POLICIES (1 << 3)
115#define EXT_POLICY_MAPPINGS (1 << 4)
116#define EXT_SUBJECT_ALT_NAME (1 << 5)
117#define EXT_ISSUER_ALT_NAME (1 << 6)
118#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
119#define EXT_BASIC_CONSTRAINTS (1 << 8)
120#define EXT_NAME_CONSTRAINTS (1 << 9)
121#define EXT_POLICY_CONSTRAINTS (1 << 10)
122#define EXT_EXTENDED_KEY_USAGE (1 << 11)
123#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
124#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
125#define EXT_FRESHEST_CRL (1 << 14)
126
127#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000129/*
130 * Storage format identifiers
131 * Recognized formats: PEM and DER
132 */
133#define X509_FORMAT_DER 1
134#define X509_FORMAT_PEM 2
135
Paul Bakker407a0da2013-06-27 14:29:21 +0200136#ifdef __cplusplus
137extern "C" {
138#endif
139
Paul Bakkere5eae762013-08-26 12:05:14 +0200140/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000141 * \addtogroup x509_module
142 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000143
144/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200145 * \name Structures for parsing X.509 certificates, CRLs and CSRs
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000146 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000147 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200148
149/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000150 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000152typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakker37ca75d2011-01-06 12:28:03 +0000154/**
Paul Bakker74111d32011-01-15 16:57:55 +0000155 * Container for ASN1 bit strings.
156 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000157typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000158
159/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200160 * Container for ASN1 named information objects.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000161 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
162 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200163typedef asn1_named_data x509_name;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakker74111d32011-01-15 16:57:55 +0000165/**
166 * Container for a sequence of ASN.1 items
167 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000168typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000169
Paul Bakker37ca75d2011-01-06 12:28:03 +0000170/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171typedef struct _x509_time
172{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000173 int year, mon, day; /**< Date. */
174 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000175}
176x509_time;
177
Paul Bakkere5eae762013-08-26 12:05:14 +0200178/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000179 * Container for an X.509 certificate. The certificate may be chained.
180 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000181typedef struct _x509_cert
182{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000183 x509_buf raw; /**< The raw certificate data (DER). */
184 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakker37ca75d2011-01-06 12:28:03 +0000186 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
187 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
188 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Paul Bakker37ca75d2011-01-06 12:28:03 +0000190 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
191 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Paul Bakker37ca75d2011-01-06 12:28:03 +0000193 x509_name issuer; /**< The parsed issuer data (named information object). */
194 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Paul Bakker37ca75d2011-01-06 12:28:03 +0000196 x509_time valid_from; /**< Start time of certificate validity. */
197 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +0200199 pk_context pk; /**< Container for the public key context. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Paul Bakker37ca75d2011-01-06 12:28:03 +0000201 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
202 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
203 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakkera8cd2392012-02-11 16:09:32 +0000204 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Paul Bakker74111d32011-01-15 16:57:55 +0000206 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000207 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 +0000208 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 +0000209
Paul Bakker74111d32011-01-15 16:57:55 +0000210 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
211
212 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
213
214 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
215
Paul Bakker37ca75d2011-01-06 12:28:03 +0000216 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
217 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200218 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
219 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 +0000220
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200221 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000222}
223x509_cert;
224
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200225/**
226 * Certificate revocation list entry.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000227 * Contains the CA-specific serial numbers and revocation dates.
228 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000229typedef struct _x509_crl_entry
230{
231 x509_buf raw;
232
233 x509_buf serial;
234
235 x509_time revocation_date;
236
237 x509_buf entry_ext;
238
239 struct _x509_crl_entry *next;
240}
241x509_crl_entry;
242
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200243/**
244 * Certificate revocation list structure.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000245 * Every CRL may have multiple entries.
246 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000247typedef struct _x509_crl
248{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000249 x509_buf raw; /**< The raw certificate data (DER). */
250 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000251
252 int version;
253 x509_buf sig_oid1;
254
Paul Bakker37ca75d2011-01-06 12:28:03 +0000255 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000256
Paul Bakker37ca75d2011-01-06 12:28:03 +0000257 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000258
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200259 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000260 x509_time next_update;
261
Paul Bakker37ca75d2011-01-06 12:28:03 +0000262 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000263
264 x509_buf crl_ext;
265
266 x509_buf sig_oid2;
267 x509_buf sig;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200268 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
269 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 +0000270
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200271 struct _x509_crl *next;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000272}
273x509_crl;
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200274
275/**
276 * Certificate Signing Request (CSR) structure.
277 */
278typedef struct _x509_csr
279{
280 x509_buf raw; /**< The raw CSR data (DER). */
281 x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
282
283 int version;
284
285 x509_buf subject_raw; /**< The raw subject data (DER). */
286 x509_name subject; /**< The parsed subject data (named information object). */
287
288 pk_context pk; /**< Container for the public key context. */
289
290 x509_buf sig_oid;
291 x509_buf sig;
292 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
293 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
294}
295x509_csr;
296/** \} name Structures for parsing X.509 certificates, CRLs and CSRs */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000297/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000298
Paul Bakker37ca75d2011-01-06 12:28:03 +0000299/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000300 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
301 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000302 */
303
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000304/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000305/**
Paul Bakker89ecb2d2013-06-24 19:06:15 +0200306 * \brief Parse a single DER formatted certificate and add it
307 * to the chained list.
308 *
309 * \param chain points to the start of the chain
310 * \param buf buffer holding the certificate DER data
311 * \param buflen size of the buffer
312 *
313 * \return 0 if successful, or a specific X509 or PEM error code
314 */
315int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen );
316
317/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 * \brief Parse one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000319 * to the chained list. Parses permissively. If some
320 * certificates can be parsed, the result is the number
321 * of failed certificates it encountered. If none complete
322 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 *
324 * \param chain points to the start of the chain
325 * \param buf buffer holding the certificate data
326 * \param buflen size of the buffer
327 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000328 * \return 0 if all certificates parsed successfully, a positive number
329 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000331int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200333#if defined(POLARSSL_FS_IO)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000334/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000335/**
336 * \brief Load one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000337 * to the chained list. Parses permissively. If some
338 * certificates can be parsed, the result is the number
339 * of failed certificates it encountered. If none complete
340 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 *
342 * \param chain points to the start of the chain
343 * \param path filename to read the certificates from
344 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000345 * \return 0 if all certificates parsed successfully, a positive number
346 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000348int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000350/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000351/**
Paul Bakker8d914582012-06-04 12:46:42 +0000352 * \brief Load one or more certificate files from a path and add them
353 * to the chained list. Parses permissively. If some
354 * certificates can be parsed, the result is the number
355 * of failed certificates it encountered. If none complete
356 * correctly, the first error is returned.
357 *
358 * \param chain points to the start of the chain
359 * \param path directory / folder to read the certificate files from
360 *
361 * \return 0 if all certificates parsed successfully, a positive number
362 * if partly successful or a specific X509 or PEM error code
363 */
364int x509parse_crtpath( x509_cert *chain, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200365#endif /* POLARSSL_FS_IO */
Paul Bakker8d914582012-06-04 12:46:42 +0000366
367/** \ingroup x509_module */
368/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000369 * \brief Parse one or more CRLs and add them
370 * to the chained list
371 *
372 * \param chain points to the start of the chain
373 * \param buf buffer holding the CRL data
374 * \param buflen size of the buffer
375 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000376 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000377 */
Paul Bakker23986e52011-04-24 08:57:21 +0000378int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000379
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000380/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000381/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200382 * \brief Load a Certificate Signing Request (CSR)
383 *
384 * \param csr CSR context to fill
385 * \param buf buffer holding the CRL data
386 * \param buflen size of the buffer
387 *
388 * \return 0 if successful, or a specific X509 or PEM error code
389 */
390int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen );
391
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200392#if defined(POLARSSL_FS_IO)
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200393/** \ingroup x509_module */
394/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000395 * \brief Load one or more CRLs and add them
396 * to the chained list
397 *
398 * \param chain points to the start of the chain
399 * \param path filename to read the CRLs from
400 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000401 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000402 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000403int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200405/** \ingroup x509_module */
406/**
407 * \brief Load a Certificate Signing Request (CSR)
408 *
409 * \param csr CSR context to fill
410 * \param path filename to read the CSR from
411 *
412 * \return 0 if successful, or a specific X509 or PEM error code
413 */
414int x509parse_csrfile( x509_csr *csr, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200415#endif /* POLARSSL_FS_IO */
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200416
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200417#if defined(POLARSSL_RSA_C)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000418/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000419/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 * \brief Parse a private RSA key
421 *
422 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000423 * \param key input buffer
424 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \param pwd password for decryption (optional)
426 * \param pwdlen size of the password
427 *
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 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200430int x509parse_key_rsa( rsa_context *rsa,
431 const unsigned char *key, size_t keylen,
432 const unsigned char *pwd, size_t pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200434#if defined(POLARSSL_FS_IO)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000435/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000436/**
437 * \brief Load and parse a private RSA key
438 *
439 * \param rsa RSA context to be initialized
440 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000441 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000443 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200445int x509parse_keyfile_rsa( rsa_context *rsa, const char *path,
446 const char *password );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200447#endif /* POLARSSL_FS_IO */
Paul Bakker1b57b062011-01-06 15:48:19 +0000448
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000449/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000450/**
Paul Bakker917e7542011-03-25 14:23:36 +0000451 * \brief Parse a public RSA key
452 *
453 * \param rsa RSA context to be initialized
454 * \param key input buffer
455 * \param keylen size of the buffer
456 *
457 * \return 0 if successful, or a specific X509 or PEM error code
458 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200459int x509parse_public_key_rsa( rsa_context *rsa,
460 const unsigned char *key, size_t keylen );
Paul Bakker917e7542011-03-25 14:23:36 +0000461
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200462#if defined(POLARSSL_FS_IO)
Paul Bakker917e7542011-03-25 14:23:36 +0000463/** \ingroup x509_module */
464/**
465 * \brief Load and parse a public RSA key
466 *
467 * \param rsa RSA context to be initialized
468 * \param path filename to read the private key from
469 *
470 * \return 0 if successful, or a specific X509 or PEM error code
471 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200472int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200473#endif /* POLARSSL_FS_IO */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200474#endif /* POLARSSL_RSA_C */
Paul Bakker917e7542011-03-25 14:23:36 +0000475
476/** \ingroup x509_module */
477/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000478 * \brief Parse DHM parameters
479 *
480 * \param dhm DHM context to be initialized
481 * \param dhmin input buffer
482 * \param dhminlen size of the buffer
483 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000484 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000485 */
Paul Bakker23986e52011-04-24 08:57:21 +0000486int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
Paul Bakker1b57b062011-01-06 15:48:19 +0000487
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200488#if defined(POLARSSL_FS_IO)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000489/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000490/**
491 * \brief Load and parse DHM parameters
492 *
493 * \param dhm DHM context to be initialized
494 * \param path filename to read the DHM Parameters from
495 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000496 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000497 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000498int x509parse_dhmfile( dhm_context *dhm, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200499#endif /* POLARSSL_FS_IO */
Paul Bakker1b57b062011-01-06 15:48:19 +0000500
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000501/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000502
Paul Bakker5121ce52009-01-03 21:22:43 +0000503/**
504 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000505 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000506 *
507 * \param buf Buffer to write to
508 * \param size Maximum size of buffer
509 * \param dn The X509 name to represent
510 *
511 * \return The amount of data written to the buffer, or -1 in
512 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000514int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000517 * \brief Store the certificate serial in printable form into buf;
518 * no more than size characters will be written.
519 *
520 * \param buf Buffer to write to
521 * \param size Maximum size of buffer
522 * \param serial The X509 serial to represent
523 *
524 * \return The amount of data written to the buffer, or -1 in
525 * case of an error.
526 */
527int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
528
529/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * \brief Returns an informational string about the
531 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000532 *
533 * \param buf Buffer to write to
534 * \param size Maximum size of buffer
535 * \param prefix A line prefix
536 * \param crt The X509 certificate to represent
537 *
538 * \return The amount of data written to the buffer, or -1 in
539 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000541int x509parse_cert_info( char *buf, size_t size, const char *prefix,
542 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000543
544/**
545 * \brief Returns an informational string about the
546 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000547 *
548 * \param buf Buffer to write to
549 * \param size Maximum size of buffer
550 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000551 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000552 *
553 * \return The amount of data written to the buffer, or -1 in
554 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000555 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000556int x509parse_crl_info( char *buf, size_t size, const char *prefix,
557 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200560 * \brief Returns an informational string about the
561 * CSR.
562 *
563 * \param buf Buffer to write to
564 * \param size Maximum size of buffer
565 * \param prefix A line prefix
566 * \param csr The X509 CSR to represent
567 *
568 * \return The amount of data written to the buffer, or -1 in
569 * case of an error.
570 */
571int x509parse_csr_info( char *buf, size_t size, const char *prefix,
572 const x509_csr *csr );
573
574/**
Paul Bakker74111d32011-01-15 16:57:55 +0000575 * \brief Give an known OID, return its descriptive string.
576 *
577 * \param oid buffer containing the oid
578 *
579 * \return Return a string if the OID is known,
580 * or NULL otherwise.
581 */
582const char *x509_oid_get_description( x509_buf *oid );
583
Paul Bakker9a736322012-11-14 12:39:52 +0000584/**
Paul Bakker74111d32011-01-15 16:57:55 +0000585 * \brief Give an OID, return a string version of its OID number.
Paul Bakkerd5c2b542013-04-07 22:34:26 +0200586 * (Deprecated. Use oid_get_numeric_string() instead)
Paul Bakker74111d32011-01-15 16:57:55 +0000587 *
588 * \param buf Buffer to write to
589 * \param size Maximum size of buffer
590 * \param oid Buffer containing the OID
591 *
592 * \return The amount of data written to the buffer, or -1 in
593 * case of an error.
594 */
595int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
596
597/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000598 * \brief Check a given x509_time against the system time and check
599 * if it is valid.
600 *
601 * \param time x509_time to check
602 *
603 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000604 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000606int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000609 * \name Functions to verify a certificate
610 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000611 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000612/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000613/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 * \brief Verify the certificate signature
615 *
Paul Bakker915275b2012-09-28 07:10:55 +0000616 * The verify callback is a user-supplied callback that
617 * can clear / modify / add flags for a certificate. If set,
618 * the verification callback is called for each
619 * certificate in the chain (from the trust-ca down to the
620 * presented crt). The parameters for the callback are:
621 * (void *parameter, x509_cert *crt, int certificate_depth,
622 * int *flags). With the flags representing current flags for
623 * that specific certificate and the certificate depth from
Paul Bakker9a736322012-11-14 12:39:52 +0000624 * the bottom (Peer cert depth = 0).
Paul Bakker915275b2012-09-28 07:10:55 +0000625 *
626 * All flags left after returning from the callback
627 * are also returned to the application. The function should
628 * return 0 for anything but a fatal error.
629 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 * \param crt a certificate to be verified
631 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000632 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 * \param cn expected Common Name (can be set to
634 * NULL if the CN must not be verified)
635 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000636 * \param f_vrfy verification function
637 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 *
Paul Bakker40e46942009-01-03 21:51:57 +0000639 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 * in which case *flags will have one or more of
641 * the following values set:
642 * BADCERT_EXPIRED --
643 * BADCERT_REVOKED --
644 * BADCERT_CN_MISMATCH --
645 * BADCERT_NOT_TRUSTED
Paul Bakker915275b2012-09-28 07:10:55 +0000646 * or another error in case of a fatal error encountered
647 * during the verification process.
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 */
649int x509parse_verify( x509_cert *crt,
650 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000651 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000652 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +0000653 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000654 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker74111d32011-01-15 16:57:55 +0000656/**
657 * \brief Verify the certificate signature
658 *
659 * \param crt a certificate to be verified
660 * \param crl the CRL to verify against
661 *
662 * \return 1 if the certificate is revoked, 0 otherwise
663 *
664 */
665int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
666
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000667/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000668
669
670
671/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000672 * \name Functions to clear a certificate, CRL or private RSA key
673 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000674 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000675/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000676/**
677 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000678 *
679 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 */
681void x509_free( x509_cert *crt );
682
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000683/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000684/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000685 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000686 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000687 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000688 */
689void x509_crl_free( x509_crl *crl );
690
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200691/**
692 * \brief Unallocate all CSR data
693 *
694 * \param csr CSR to free
695 */
696void x509_csr_free( x509_csr *csr );
697
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000698/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000699
700
Paul Bakkerd98030e2009-05-02 15:13:40 +0000701/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 * \brief Checkup routine
703 *
704 * \return 0 if successful, or 1 if the test failed
705 */
706int x509_self_test( int verbose );
707
708#ifdef __cplusplus
709}
710#endif
711
Paul Bakkered27a042013-04-18 22:46:23 +0200712#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000713#endif /* x509.h */