blob: fe43795128b34d79820fe17768d0acfc37e11002 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief X.509 certificate and private key decoding
5 *
Paul Bakkerefc30292011-11-10 14:43:23 +00006 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_X509_H
28#define POLARSSL_X509_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakkerefc30292011-11-10 14:43:23 +000030#include "asn1.h"
Paul Bakker314052f2011-08-15 09:07:52 +000031#include "rsa.h"
32#include "dhm.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker37ca75d2011-01-06 12:28:03 +000035/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000036 * \addtogroup x509_module
37 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000038 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000039
Paul Bakker37ca75d2011-01-06 12:28:03 +000040/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000041 * \name X509 Error codes
42 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000043 */
Paul Bakker9d781402011-05-09 16:17:09 +000044#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
45#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
46#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
47#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
48#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
49#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
50#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
51#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
52#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */
53#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */
54#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */
55#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */
56#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */
Paul Bakkered56b222011-07-13 11:26:43 +000057#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA is supported). */
Paul Bakker9d781402011-05-09 16:17:09 +000058#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
59#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
60#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */
61#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000062#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */
Paul Bakker69e095c2011-12-10 21:55:01 +000063#define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */
64#define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */
65#define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000066/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker37ca75d2011-01-06 12:28:03 +000068/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000069 * \name X509 Verify codes
70 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000071 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000072#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
73#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
74#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
75#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
76#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
77#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
78#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
79#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker915275b2012-09-28 07:10:55 +000080#define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000081/* \} name */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000082/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84/*
Paul Bakker74111d32011-01-15 16:57:55 +000085 * X.509 v3 Key Usage Extension flags
86 */
87#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
88#define KU_NON_REPUDIATION (0x40) /* bit 1 */
89#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
90#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
91#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
92#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
93#define KU_CRL_SIGN (0x02) /* bit 6 */
94
95/*
Paul Bakker74111d32011-01-15 16:57:55 +000096 * Netscape certificate types
97 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
98 */
99
100#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
101#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
102#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
103#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
104#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
105#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
106#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
107#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
108
Paul Bakkerc70b9822013-04-07 22:00:46 +0200109/*
110 * X.509 extension types
111 */
Paul Bakker74111d32011-01-15 16:57:55 +0000112#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
113#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
114#define EXT_KEY_USAGE (1 << 2)
115#define EXT_CERTIFICATE_POLICIES (1 << 3)
116#define EXT_POLICY_MAPPINGS (1 << 4)
117#define EXT_SUBJECT_ALT_NAME (1 << 5)
118#define EXT_ISSUER_ALT_NAME (1 << 6)
119#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
120#define EXT_BASIC_CONSTRAINTS (1 << 8)
121#define EXT_NAME_CONSTRAINTS (1 << 9)
122#define EXT_POLICY_CONSTRAINTS (1 << 10)
123#define EXT_EXTENDED_KEY_USAGE (1 << 11)
124#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
125#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
126#define EXT_FRESHEST_CRL (1 << 14)
127
128#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000130/*
131 * Storage format identifiers
132 * Recognized formats: PEM and DER
133 */
134#define X509_FORMAT_DER 1
135#define X509_FORMAT_PEM 2
136
Paul Bakkerc70b9822013-04-07 22:00:46 +0200137typedef enum {
138 POLARSSL_PK_NONE=0,
139 POLARSSL_PK_RSA,
140 POLARSSL_PK_ECDSA,
141} pk_type_t;
142
Paul Bakker37ca75d2011-01-06 12:28:03 +0000143/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000144 * \addtogroup x509_module
145 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000146
147/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000148 * \name Structures for parsing X.509 certificates and CRLs
149 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000150 */
151
152/**
153 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000155typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakker37ca75d2011-01-06 12:28:03 +0000157/**
Paul Bakker74111d32011-01-15 16:57:55 +0000158 * Container for ASN1 bit strings.
159 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000160typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000161
162/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000163 * Container for ASN1 named information objects.
164 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
165 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166typedef struct _x509_name
167{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000168 x509_buf oid; /**< The object identifier. */
169 x509_buf val; /**< The named value. */
170 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171}
172x509_name;
173
Paul Bakker74111d32011-01-15 16:57:55 +0000174/**
175 * Container for a sequence of ASN.1 items
176 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000177typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000178
Paul Bakker37ca75d2011-01-06 12:28:03 +0000179/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000180typedef struct _x509_time
181{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000182 int year, mon, day; /**< Date. */
183 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
185x509_time;
186
Paul Bakker37ca75d2011-01-06 12:28:03 +0000187/**
188 * Container for an X.509 certificate. The certificate may be chained.
189 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000190typedef struct _x509_cert
191{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000192 x509_buf raw; /**< The raw certificate data (DER). */
193 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Paul Bakker37ca75d2011-01-06 12:28:03 +0000195 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
196 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
197 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker37ca75d2011-01-06 12:28:03 +0000199 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
200 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker37ca75d2011-01-06 12:28:03 +0000202 x509_name issuer; /**< The parsed issuer data (named information object). */
203 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakker37ca75d2011-01-06 12:28:03 +0000205 x509_time valid_from; /**< Start time of certificate validity. */
206 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Paul Bakker37ca75d2011-01-06 12:28:03 +0000208 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
209 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 +0000210
Paul Bakker37ca75d2011-01-06 12:28:03 +0000211 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
212 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
213 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakkera8cd2392012-02-11 16:09:32 +0000214 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Paul Bakker74111d32011-01-15 16:57:55 +0000216 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000217 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 +0000218 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 +0000219
Paul Bakker74111d32011-01-15 16:57:55 +0000220 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
221
222 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
223
224 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
225
Paul Bakker37ca75d2011-01-06 12:28:03 +0000226 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
227 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200228 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
229 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 +0000230
Paul Bakker37ca75d2011-01-06 12:28:03 +0000231 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232}
233x509_cert;
234
Paul Bakker37ca75d2011-01-06 12:28:03 +0000235/**
236 * Certificate revocation list entry.
237 * Contains the CA-specific serial numbers and revocation dates.
238 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000239typedef struct _x509_crl_entry
240{
241 x509_buf raw;
242
243 x509_buf serial;
244
245 x509_time revocation_date;
246
247 x509_buf entry_ext;
248
249 struct _x509_crl_entry *next;
250}
251x509_crl_entry;
252
Paul Bakker37ca75d2011-01-06 12:28:03 +0000253/**
254 * Certificate revocation list structure.
255 * Every CRL may have multiple entries.
256 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000257typedef struct _x509_crl
258{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000259 x509_buf raw; /**< The raw certificate data (DER). */
260 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000261
262 int version;
263 x509_buf sig_oid1;
264
Paul Bakker37ca75d2011-01-06 12:28:03 +0000265 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000266
Paul Bakker37ca75d2011-01-06 12:28:03 +0000267 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000268
Paul Bakker37ca75d2011-01-06 12:28:03 +0000269 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000270 x509_time next_update;
271
Paul Bakker37ca75d2011-01-06 12:28:03 +0000272 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000273
274 x509_buf crl_ext;
275
276 x509_buf sig_oid2;
277 x509_buf sig;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200278 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
279 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 +0000280
281 struct _x509_crl *next;
282}
283x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000284/** \} name Structures for parsing X.509 certificates and CRLs */
285/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000286
Paul Bakker37ca75d2011-01-06 12:28:03 +0000287/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000288 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000289 * XvP: commented out as they are not used.
290 * - <tt>typedef struct _x509_node x509_node;</tt>
291 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000293/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000294typedef struct _x509_node
295{
296 unsigned char *data;
297 unsigned char *p;
298 unsigned char *end;
299
300 size_t len;
301}
302x509_node;
303
304typedef struct _x509_raw
305{
306 x509_node raw;
307 x509_node tbs;
308
309 x509_node version;
310 x509_node serial;
311 x509_node tbs_signalg;
312 x509_node issuer;
313 x509_node validity;
314 x509_node subject;
315 x509_node subpubkey;
316
317 x509_node signalg;
318 x509_node sign;
319}
320x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000321*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323#ifdef __cplusplus
324extern "C" {
325#endif
326
327/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000328 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
329 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000330 */
331
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000332/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000333/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 * \brief Parse one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000335 * to the chained list. Parses permissively. If some
336 * certificates can be parsed, the result is the number
337 * of failed certificates it encountered. If none complete
338 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 *
340 * \param chain points to the start of the chain
341 * \param buf buffer holding the certificate data
342 * \param buflen size of the buffer
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_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
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/**
351 * \brief Load one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000352 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 *
357 * \param chain points to the start of the chain
358 * \param path filename to read the certificates from
359 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000360 * \return 0 if all certificates parsed successfully, a positive number
361 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000363int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000365/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000366/**
Paul Bakker8d914582012-06-04 12:46:42 +0000367 * \brief Load one or more certificate files from a path and add them
368 * to the chained list. Parses permissively. If some
369 * certificates can be parsed, the result is the number
370 * of failed certificates it encountered. If none complete
371 * correctly, the first error is returned.
372 *
373 * \param chain points to the start of the chain
374 * \param path directory / folder to read the certificate files from
375 *
376 * \return 0 if all certificates parsed successfully, a positive number
377 * if partly successful or a specific X509 or PEM error code
378 */
379int x509parse_crtpath( x509_cert *chain, const char *path );
380
381/** \ingroup x509_module */
382/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000383 * \brief Parse one or more CRLs and add them
384 * to the chained list
385 *
386 * \param chain points to the start of the chain
387 * \param buf buffer holding the CRL data
388 * \param buflen size of the buffer
389 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000390 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000391 */
Paul Bakker23986e52011-04-24 08:57:21 +0000392int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000393
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000394/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000395/**
396 * \brief Load one or more CRLs and add them
397 * to the chained list
398 *
399 * \param chain points to the start of the chain
400 * \param path filename to read the CRLs from
401 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000402 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000404int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000405
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000406/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000407/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 * \brief Parse a private RSA key
409 *
410 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000411 * \param key input buffer
412 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 * \param pwd password for decryption (optional)
414 * \param pwdlen size of the password
415 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000416 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 */
418int x509parse_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000419 const unsigned char *key, size_t keylen,
420 const unsigned char *pwd, size_t pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000422/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000423/**
424 * \brief Load and parse a private RSA key
425 *
426 * \param rsa RSA context to be initialized
427 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000428 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000430 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000432int x509parse_keyfile( rsa_context *rsa, const char *path,
433 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000434
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000435/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000436/**
Paul Bakker917e7542011-03-25 14:23:36 +0000437 * \brief Parse a public RSA key
438 *
439 * \param rsa RSA context to be initialized
440 * \param key input buffer
441 * \param keylen size of the buffer
442 *
443 * \return 0 if successful, or a specific X509 or PEM error code
444 */
445int x509parse_public_key( rsa_context *rsa,
Paul Bakker23986e52011-04-24 08:57:21 +0000446 const unsigned char *key, size_t keylen );
Paul Bakker917e7542011-03-25 14:23:36 +0000447
448/** \ingroup x509_module */
449/**
450 * \brief Load and parse a public RSA key
451 *
452 * \param rsa RSA context to be initialized
453 * \param path filename to read the private key from
454 *
455 * \return 0 if successful, or a specific X509 or PEM error code
456 */
457int x509parse_public_keyfile( rsa_context *rsa, const char *path );
458
459/** \ingroup x509_module */
460/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000461 * \brief Parse DHM parameters
462 *
463 * \param dhm DHM context to be initialized
464 * \param dhmin input buffer
465 * \param dhminlen size of the buffer
466 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000467 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000468 */
Paul Bakker23986e52011-04-24 08:57:21 +0000469int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
Paul Bakker1b57b062011-01-06 15:48:19 +0000470
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000471/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000472/**
473 * \brief Load and parse DHM parameters
474 *
475 * \param dhm DHM context to be initialized
476 * \param path filename to read the DHM Parameters from
477 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000478 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000479 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000480int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000481
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000482/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000483
Paul Bakker5121ce52009-01-03 21:22:43 +0000484/**
485 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000486 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000487 *
488 * \param buf Buffer to write to
489 * \param size Maximum size of buffer
490 * \param dn The X509 name to represent
491 *
492 * \return The amount of data written to the buffer, or -1 in
493 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000495int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000498 * \brief Store the certificate serial in printable form into buf;
499 * no more than size characters will be written.
500 *
501 * \param buf Buffer to write to
502 * \param size Maximum size of buffer
503 * \param serial The X509 serial to represent
504 *
505 * \return The amount of data written to the buffer, or -1 in
506 * case of an error.
507 */
508int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
509
510/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 * \brief Returns an informational string about the
512 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000513 *
514 * \param buf Buffer to write to
515 * \param size Maximum size of buffer
516 * \param prefix A line prefix
517 * \param crt The X509 certificate to represent
518 *
519 * \return The amount of data written to the buffer, or -1 in
520 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000522int x509parse_cert_info( char *buf, size_t size, const char *prefix,
523 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000524
525/**
526 * \brief Returns an informational string about the
527 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000528 *
529 * \param buf Buffer to write to
530 * \param size Maximum size of buffer
531 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000532 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000533 *
534 * \return The amount of data written to the buffer, or -1 in
535 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000536 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000537int x509parse_crl_info( char *buf, size_t size, const char *prefix,
538 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
Paul Bakker74111d32011-01-15 16:57:55 +0000541 * \brief Give an known OID, return its descriptive string.
542 *
543 * \param oid buffer containing the oid
544 *
545 * \return Return a string if the OID is known,
546 * or NULL otherwise.
547 */
548const char *x509_oid_get_description( x509_buf *oid );
549
Paul Bakker9a736322012-11-14 12:39:52 +0000550/**
Paul Bakker74111d32011-01-15 16:57:55 +0000551 * \brief Give an OID, return a string version of its OID number.
552 *
553 * \param buf Buffer to write to
554 * \param size Maximum size of buffer
555 * \param oid Buffer containing the OID
556 *
557 * \return The amount of data written to the buffer, or -1 in
558 * case of an error.
559 */
560int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
561
562/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000563 * \brief Check a given x509_time against the system time and check
564 * if it is valid.
565 *
566 * \param time x509_time to check
567 *
568 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000569 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000571int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000574 * \name Functions to verify a certificate
575 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000576 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000577/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000578/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 * \brief Verify the certificate signature
580 *
Paul Bakker915275b2012-09-28 07:10:55 +0000581 * The verify callback is a user-supplied callback that
582 * can clear / modify / add flags for a certificate. If set,
583 * the verification callback is called for each
584 * certificate in the chain (from the trust-ca down to the
585 * presented crt). The parameters for the callback are:
586 * (void *parameter, x509_cert *crt, int certificate_depth,
587 * int *flags). With the flags representing current flags for
588 * that specific certificate and the certificate depth from
Paul Bakker9a736322012-11-14 12:39:52 +0000589 * the bottom (Peer cert depth = 0).
Paul Bakker915275b2012-09-28 07:10:55 +0000590 *
591 * All flags left after returning from the callback
592 * are also returned to the application. The function should
593 * return 0 for anything but a fatal error.
594 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 * \param crt a certificate to be verified
596 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000597 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 * \param cn expected Common Name (can be set to
599 * NULL if the CN must not be verified)
600 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000601 * \param f_vrfy verification function
602 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 *
Paul Bakker40e46942009-01-03 21:51:57 +0000604 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 * in which case *flags will have one or more of
606 * the following values set:
607 * BADCERT_EXPIRED --
608 * BADCERT_REVOKED --
609 * BADCERT_CN_MISMATCH --
610 * BADCERT_NOT_TRUSTED
Paul Bakker915275b2012-09-28 07:10:55 +0000611 * or another error in case of a fatal error encountered
612 * during the verification process.
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 */
614int x509parse_verify( x509_cert *crt,
615 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000616 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000617 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +0000618 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000619 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Paul Bakker74111d32011-01-15 16:57:55 +0000621/**
622 * \brief Verify the certificate signature
623 *
624 * \param crt a certificate to be verified
625 * \param crl the CRL to verify against
626 *
627 * \return 1 if the certificate is revoked, 0 otherwise
628 *
629 */
630int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
631
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000632/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000633
634
635
636/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000637 * \name Functions to clear a certificate, CRL or private RSA key
638 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000639 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000640/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000641/**
642 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000643 *
644 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 */
646void x509_free( x509_cert *crt );
647
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000648/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000649/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000650 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000651 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000652 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000653 */
654void x509_crl_free( x509_crl *crl );
655
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000656/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000657
658
Paul Bakkerd98030e2009-05-02 15:13:40 +0000659/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 * \brief Checkup routine
661 *
662 * \return 0 if successful, or 1 if the test failed
663 */
664int x509_self_test( int verbose );
665
666#ifdef __cplusplus
667}
668#endif
669
670#endif /* x509.h */