blob: 2fa00f6c66d26f81c1da635008f4666daed84910 [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
Paul Bakkerefc30292011-11-10 14:43:23 +000032#include "asn1.h"
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "dhm.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020034#include "md.h"
Paul Bakkered27a042013-04-18 22:46:23 +020035#include "pk.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker37ca75d2011-01-06 12:28:03 +000037/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000038 * \addtogroup x509_module
39 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000040 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000041
Paul Bakker37ca75d2011-01-06 12:28:03 +000042/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000043 * \name X509 Error codes
44 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000045 */
Paul Bakker9d781402011-05-09 16:17:09 +000046#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
47#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
48#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
49#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
50#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
51#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
52#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
53#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2480 /**< The signature tag or value invalid. */
55#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2500 /**< The extension tag or value is invalid. */
56#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2580 /**< Certificate or CRL has an unsupported version number. */
57#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2600 /**< Signature algorithm (oid) is unsupported. */
58#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2680 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
59#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2700 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
60#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780 /**< Format not recognized as DER or PEM. */
61#define POLARSSL_ERR_X509_INVALID_INPUT -0x2800 /**< Input invalid. */
62#define POLARSSL_ERR_X509_MALLOC_FAILED -0x2880 /**< Allocation of memory failed. */
63#define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2900 /**< Read/write of file failed. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000064/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Paul Bakker37ca75d2011-01-06 12:28:03 +000066/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000067 * \name X509 Verify codes
68 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000069 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000070#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
71#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
72#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
73#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
74#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
75#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
76#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
77#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker915275b2012-09-28 07:10:55 +000078#define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000079/* \} name */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000080/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82/*
Paul Bakker74111d32011-01-15 16:57:55 +000083 * X.509 v3 Key Usage Extension flags
84 */
85#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
86#define KU_NON_REPUDIATION (0x40) /* bit 1 */
87#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
88#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
89#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
90#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
91#define KU_CRL_SIGN (0x02) /* bit 6 */
92
93/*
Paul Bakker74111d32011-01-15 16:57:55 +000094 * Netscape certificate types
95 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
96 */
97
98#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
99#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
100#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
101#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
102#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
103#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
104#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
105#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
106
Paul Bakkerc70b9822013-04-07 22:00:46 +0200107/*
108 * X.509 extension types
109 */
Paul Bakker74111d32011-01-15 16:57:55 +0000110#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
111#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
112#define EXT_KEY_USAGE (1 << 2)
113#define EXT_CERTIFICATE_POLICIES (1 << 3)
114#define EXT_POLICY_MAPPINGS (1 << 4)
115#define EXT_SUBJECT_ALT_NAME (1 << 5)
116#define EXT_ISSUER_ALT_NAME (1 << 6)
117#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
118#define EXT_BASIC_CONSTRAINTS (1 << 8)
119#define EXT_NAME_CONSTRAINTS (1 << 9)
120#define EXT_POLICY_CONSTRAINTS (1 << 10)
121#define EXT_EXTENDED_KEY_USAGE (1 << 11)
122#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
123#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
124#define EXT_FRESHEST_CRL (1 << 14)
125
126#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000128/*
129 * Storage format identifiers
130 * Recognized formats: PEM and DER
131 */
132#define X509_FORMAT_DER 1
133#define X509_FORMAT_PEM 2
134
Paul Bakker407a0da2013-06-27 14:29:21 +0200135#ifdef __cplusplus
136extern "C" {
137#endif
138
Paul Bakkere5eae762013-08-26 12:05:14 +0200139/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000140 * \addtogroup x509_module
141 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000142
143/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200144 * \name Structures for parsing X.509 certificates, CRLs and CSRs
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000145 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000146 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200147
148/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000149 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000151typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakker37ca75d2011-01-06 12:28:03 +0000153/**
Paul Bakker74111d32011-01-15 16:57:55 +0000154 * Container for ASN1 bit strings.
155 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000156typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000157
158/**
Paul Bakkere5eae762013-08-26 12:05:14 +0200159 * Container for ASN1 named information objects.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000160 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
161 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200162typedef asn1_named_data x509_name;
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakker74111d32011-01-15 16:57:55 +0000164/**
165 * Container for a sequence of ASN.1 items
166 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000167typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000168
Paul Bakker37ca75d2011-01-06 12:28:03 +0000169/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000170typedef struct _x509_time
171{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000172 int year, mon, day; /**< Date. */
173 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
175x509_time;
176
Paul Bakkere5eae762013-08-26 12:05:14 +0200177/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000178 * Container for an X.509 certificate. The certificate may be chained.
179 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000180typedef struct _x509_cert
181{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000182 x509_buf raw; /**< The raw certificate data (DER). */
183 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Paul Bakker37ca75d2011-01-06 12:28:03 +0000185 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
186 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
187 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Paul Bakker37ca75d2011-01-06 12:28:03 +0000189 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
190 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Paul Bakker37ca75d2011-01-06 12:28:03 +0000192 x509_name issuer; /**< The parsed issuer data (named information object). */
193 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Paul Bakker37ca75d2011-01-06 12:28:03 +0000195 x509_time valid_from; /**< Start time of certificate validity. */
196 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +0200198 pk_context pk; /**< Container for the public key context. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Paul Bakker37ca75d2011-01-06 12:28:03 +0000200 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
201 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
202 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakkera8cd2392012-02-11 16:09:32 +0000203 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakker74111d32011-01-15 16:57:55 +0000205 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000206 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 +0000207 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 +0000208
Paul Bakker74111d32011-01-15 16:57:55 +0000209 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
210
211 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
212
213 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
214
Paul Bakker37ca75d2011-01-06 12:28:03 +0000215 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
216 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200217 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
218 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 +0000219
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200220 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000221}
222x509_cert;
223
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200224/**
225 * Certificate revocation list entry.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000226 * Contains the CA-specific serial numbers and revocation dates.
227 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000228typedef struct _x509_crl_entry
229{
230 x509_buf raw;
231
232 x509_buf serial;
233
234 x509_time revocation_date;
235
236 x509_buf entry_ext;
237
238 struct _x509_crl_entry *next;
239}
240x509_crl_entry;
241
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200242/**
243 * Certificate revocation list structure.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000244 * Every CRL may have multiple entries.
245 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000246typedef struct _x509_crl
247{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000248 x509_buf raw; /**< The raw certificate data (DER). */
249 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000250
251 int version;
252 x509_buf sig_oid1;
253
Paul Bakker37ca75d2011-01-06 12:28:03 +0000254 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000255
Paul Bakker37ca75d2011-01-06 12:28:03 +0000256 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000257
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200258 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000259 x509_time next_update;
260
Paul Bakker37ca75d2011-01-06 12:28:03 +0000261 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000262
263 x509_buf crl_ext;
264
265 x509_buf sig_oid2;
266 x509_buf sig;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200267 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
268 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 +0000269
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200270 struct _x509_crl *next;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000271}
272x509_crl;
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200273
274/**
275 * Certificate Signing Request (CSR) structure.
276 */
277typedef struct _x509_csr
278{
279 x509_buf raw; /**< The raw CSR data (DER). */
280 x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
281
282 int version;
283
284 x509_buf subject_raw; /**< The raw subject data (DER). */
285 x509_name subject; /**< The parsed subject data (named information object). */
286
287 pk_context pk; /**< Container for the public key context. */
288
289 x509_buf sig_oid;
290 x509_buf sig;
291 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
292 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
293}
294x509_csr;
295/** \} name Structures for parsing X.509 certificates, CRLs and CSRs */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000296/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000297
Paul Bakker37ca75d2011-01-06 12:28:03 +0000298/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000299 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
300 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000301 */
302
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000303/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000304/**
Paul Bakker89ecb2d2013-06-24 19:06:15 +0200305 * \brief Parse a single DER formatted certificate and add it
306 * to the chained list.
307 *
308 * \param chain points to the start of the chain
309 * \param buf buffer holding the certificate DER data
310 * \param buflen size of the buffer
311 *
312 * \return 0 if successful, or a specific X509 or PEM error code
313 */
314int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen );
315
316/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 * \brief Parse one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000318 * to the chained list. Parses permissively. If some
319 * certificates can be parsed, the result is the number
320 * of failed certificates it encountered. If none complete
321 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 *
323 * \param chain points to the start of the chain
324 * \param buf buffer holding the certificate data
325 * \param buflen size of the buffer
326 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000327 * \return 0 if all certificates parsed successfully, a positive number
328 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000330int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200332#if defined(POLARSSL_FS_IO)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000333/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000334/**
335 * \brief Load one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000336 * to the chained list. Parses permissively. If some
337 * certificates can be parsed, the result is the number
338 * of failed certificates it encountered. If none complete
339 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 *
341 * \param chain points to the start of the chain
342 * \param path filename to read the certificates from
343 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000344 * \return 0 if all certificates parsed successfully, a positive number
345 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000347int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000349/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000350/**
Paul Bakker8d914582012-06-04 12:46:42 +0000351 * \brief Load one or more certificate files from a path and add them
352 * to the chained list. Parses permissively. If some
353 * certificates can be parsed, the result is the number
354 * of failed certificates it encountered. If none complete
355 * correctly, the first error is returned.
356 *
357 * \param chain points to the start of the chain
358 * \param path directory / folder to read the certificate files from
359 *
360 * \return 0 if all certificates parsed successfully, a positive number
361 * if partly successful or a specific X509 or PEM error code
362 */
363int x509parse_crtpath( x509_cert *chain, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200364#endif /* POLARSSL_FS_IO */
Paul Bakker8d914582012-06-04 12:46:42 +0000365
366/** \ingroup x509_module */
367/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368 * \brief Parse one or more CRLs and add them
369 * to the chained list
370 *
371 * \param chain points to the start of the chain
372 * \param buf buffer holding the CRL data
373 * \param buflen size of the buffer
374 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000375 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000376 */
Paul Bakker23986e52011-04-24 08:57:21 +0000377int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000378
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000379/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000380/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200381 * \brief Load a Certificate Signing Request (CSR)
382 *
383 * \param csr CSR context to fill
384 * \param buf buffer holding the CRL data
385 * \param buflen size of the buffer
386 *
387 * \return 0 if successful, or a specific X509 or PEM error code
388 */
389int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen );
390
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200391#if defined(POLARSSL_FS_IO)
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200392/** \ingroup x509_module */
393/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000394 * \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 Bakkerf9f377e2013-09-09 15:35:10 +0200404/** \ingroup x509_module */
405/**
406 * \brief Load a Certificate Signing Request (CSR)
407 *
408 * \param csr CSR context to fill
409 * \param path filename to read the CSR from
410 *
411 * \return 0 if successful, or a specific X509 or PEM error code
412 */
413int x509parse_csrfile( x509_csr *csr, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200414#endif /* POLARSSL_FS_IO */
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200415
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200416#if defined(POLARSSL_RSA_C)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000417/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 * \brief Parse a private RSA key
420 *
421 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000422 * \param key input buffer
423 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 * \param pwd password for decryption (optional)
425 * \param pwdlen size of the password
426 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000427 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200429int x509parse_key_rsa( rsa_context *rsa,
430 const unsigned char *key, size_t keylen,
431 const unsigned char *pwd, size_t pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200433#if defined(POLARSSL_FS_IO)
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000434/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000435/**
436 * \brief Load and parse a private RSA key
437 *
438 * \param rsa RSA context to be initialized
439 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000440 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000442 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200444int x509parse_keyfile_rsa( rsa_context *rsa, const char *path,
445 const char *password );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200446#endif /* POLARSSL_FS_IO */
Paul Bakker1b57b062011-01-06 15:48:19 +0000447
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000448/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000449/**
Paul Bakker917e7542011-03-25 14:23:36 +0000450 * \brief Parse a public RSA key
451 *
452 * \param rsa RSA context to be initialized
453 * \param key input buffer
454 * \param keylen size of the buffer
455 *
456 * \return 0 if successful, or a specific X509 or PEM error code
457 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200458int x509parse_public_key_rsa( rsa_context *rsa,
459 const unsigned char *key, size_t keylen );
Paul Bakker917e7542011-03-25 14:23:36 +0000460
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200461#if defined(POLARSSL_FS_IO)
Paul Bakker917e7542011-03-25 14:23:36 +0000462/** \ingroup x509_module */
463/**
464 * \brief Load and parse a public RSA key
465 *
466 * \param rsa RSA context to be initialized
467 * \param path filename to read the private key from
468 *
469 * \return 0 if successful, or a specific X509 or PEM error code
470 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200471int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path );
Manuel Pégourié-Gonnard02376202013-09-12 07:01:31 +0200472#endif /* POLARSSL_FS_IO */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200473#endif /* POLARSSL_RSA_C */
Paul Bakker917e7542011-03-25 14:23:36 +0000474
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000475/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000476
Paul Bakker5121ce52009-01-03 21:22:43 +0000477/**
478 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000479 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000480 *
481 * \param buf Buffer to write to
482 * \param size Maximum size of buffer
483 * \param dn The X509 name to represent
484 *
485 * \return The amount of data written to the buffer, or -1 in
486 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000488int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000491 * \brief Store the certificate serial in printable form into buf;
492 * no more than size characters will be written.
493 *
494 * \param buf Buffer to write to
495 * \param size Maximum size of buffer
496 * \param serial The X509 serial to represent
497 *
498 * \return The amount of data written to the buffer, or -1 in
499 * case of an error.
500 */
501int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
502
503/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 * \brief Returns an informational string about the
505 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000506 *
507 * \param buf Buffer to write to
508 * \param size Maximum size of buffer
509 * \param prefix A line prefix
510 * \param crt The X509 certificate to represent
511 *
512 * \return The amount of data written to the buffer, or -1 in
513 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000515int x509parse_cert_info( char *buf, size_t size, const char *prefix,
516 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000517
518/**
519 * \brief Returns an informational string about the
520 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000521 *
522 * \param buf Buffer to write to
523 * \param size Maximum size of buffer
524 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000525 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000526 *
527 * \return The amount of data written to the buffer, or -1 in
528 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000529 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000530int x509parse_crl_info( char *buf, size_t size, const char *prefix,
531 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533/**
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200534 * \brief Returns an informational string about the
535 * CSR.
536 *
537 * \param buf Buffer to write to
538 * \param size Maximum size of buffer
539 * \param prefix A line prefix
540 * \param csr The X509 CSR to represent
541 *
542 * \return The amount of data written to the buffer, or -1 in
543 * case of an error.
544 */
545int x509parse_csr_info( char *buf, size_t size, const char *prefix,
546 const x509_csr *csr );
547
548/**
Paul Bakker74111d32011-01-15 16:57:55 +0000549 * \brief Give an known OID, return its descriptive string.
550 *
551 * \param oid buffer containing the oid
552 *
553 * \return Return a string if the OID is known,
554 * or NULL otherwise.
555 */
556const char *x509_oid_get_description( x509_buf *oid );
557
Paul Bakker9a736322012-11-14 12:39:52 +0000558/**
Paul Bakker74111d32011-01-15 16:57:55 +0000559 * \brief Give an OID, return a string version of its OID number.
Paul Bakkerd5c2b542013-04-07 22:34:26 +0200560 * (Deprecated. Use oid_get_numeric_string() instead)
Paul Bakker74111d32011-01-15 16:57:55 +0000561 *
562 * \param buf Buffer to write to
563 * \param size Maximum size of buffer
564 * \param oid Buffer containing the OID
565 *
566 * \return The amount of data written to the buffer, or -1 in
567 * case of an error.
568 */
569int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
570
571/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000572 * \brief Check a given x509_time against the system time and check
573 * if it is valid.
574 *
575 * \param time x509_time to check
576 *
577 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000578 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000580int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000583 * \name Functions to verify a certificate
584 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000585 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000586/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000587/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 * \brief Verify the certificate signature
589 *
Paul Bakker915275b2012-09-28 07:10:55 +0000590 * The verify callback is a user-supplied callback that
591 * can clear / modify / add flags for a certificate. If set,
592 * the verification callback is called for each
593 * certificate in the chain (from the trust-ca down to the
594 * presented crt). The parameters for the callback are:
595 * (void *parameter, x509_cert *crt, int certificate_depth,
596 * int *flags). With the flags representing current flags for
597 * that specific certificate and the certificate depth from
Paul Bakker9a736322012-11-14 12:39:52 +0000598 * the bottom (Peer cert depth = 0).
Paul Bakker915275b2012-09-28 07:10:55 +0000599 *
600 * All flags left after returning from the callback
601 * are also returned to the application. The function should
602 * return 0 for anything but a fatal error.
603 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * \param crt a certificate to be verified
605 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000606 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 * \param cn expected Common Name (can be set to
608 * NULL if the CN must not be verified)
609 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000610 * \param f_vrfy verification function
611 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 *
Paul Bakker40e46942009-01-03 21:51:57 +0000613 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 * in which case *flags will have one or more of
615 * the following values set:
616 * BADCERT_EXPIRED --
617 * BADCERT_REVOKED --
618 * BADCERT_CN_MISMATCH --
619 * BADCERT_NOT_TRUSTED
Paul Bakker915275b2012-09-28 07:10:55 +0000620 * or another error in case of a fatal error encountered
621 * during the verification process.
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 */
623int x509parse_verify( x509_cert *crt,
624 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000625 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000626 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +0000627 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000628 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Paul Bakker74111d32011-01-15 16:57:55 +0000630/**
631 * \brief Verify the certificate signature
632 *
633 * \param crt a certificate to be verified
634 * \param crl the CRL to verify against
635 *
636 * \return 1 if the certificate is revoked, 0 otherwise
637 *
638 */
639int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
640
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000641/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000642
643
644
645/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000646 * \name Functions to clear a certificate, CRL or private RSA key
647 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000648 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000649/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000650/**
651 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000652 *
653 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 */
655void x509_free( x509_cert *crt );
656
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000657/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000658/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000659 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000660 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000661 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000662 */
663void x509_crl_free( x509_crl *crl );
664
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200665/**
666 * \brief Unallocate all CSR data
667 *
668 * \param csr CSR to free
669 */
670void x509_csr_free( x509_csr *csr );
671
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000672/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000673
674
Paul Bakkerd98030e2009-05-02 15:13:40 +0000675/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 * \brief Checkup routine
677 *
678 * \return 0 if successful, or 1 if the test failed
679 */
680int x509_self_test( int verbose );
681
682#ifdef __cplusplus
683}
684#endif
685
Paul Bakker5121ce52009-01-03 21:22:43 +0000686#endif /* x509.h */