blob: 240b9bbba0c975578fe35b372f182de879ffaa18 [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 "rsa.h"
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +020035#include "ecp.h"
Paul Bakker314052f2011-08-15 09:07:52 +000036#include "dhm.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020037#include "md.h"
Paul Bakkered27a042013-04-18 22:46:23 +020038#include "pk.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker37ca75d2011-01-06 12:28:03 +000040/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000041 * \addtogroup x509_module
42 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000043 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000044
Paul Bakker37ca75d2011-01-06 12:28:03 +000045/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000046 * \name X509 Error codes
47 * \{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000048 */
Paul Bakker9d781402011-05-09 16:17:09 +000049#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
50#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
51#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
52#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
53#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
54#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
55#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
56#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
57#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */
58#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */
59#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */
60#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */
61#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +020062#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA and EC are supported). */
Paul Bakker9d781402011-05-09 16:17:09 +000063#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
64#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
65#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */
66#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000067#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */
Paul Bakker69e095c2011-12-10 21:55:01 +000068#define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */
69#define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */
70#define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */
Paul Bakker28144de2013-06-24 19:28:55 +020071#define POLARSSL_ERR_X509_PASSWORD_REQUIRED -0x2B80 /**< Private key password can't be empty. */
72#define POLARSSL_ERR_X509_PASSWORD_MISMATCH -0x2C00 /**< Given private key password does not allow for correct decryption. */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +020073#define POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE -0x2C80 /**< Elliptic curve is unsupported (only NIST curves are supported). */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000074/* \} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakker37ca75d2011-01-06 12:28:03 +000076/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +000077 * \name X509 Verify codes
78 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +000079 */
Paul Bakkercdf07e92011-01-30 17:05:13 +000080#define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
81#define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
82#define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
83#define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
84#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
85#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
86#define BADCERT_MISSING 0x40 /**< Certificate was missing. */
87#define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
Paul Bakker915275b2012-09-28 07:10:55 +000088#define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000089/* \} name */
Paul Bakker0f5f72e2011-01-18 14:58:55 +000090/* \} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92/*
Paul Bakker74111d32011-01-15 16:57:55 +000093 * X.509 v3 Key Usage Extension flags
94 */
95#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
96#define KU_NON_REPUDIATION (0x40) /* bit 1 */
97#define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
98#define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
99#define KU_KEY_AGREEMENT (0x08) /* bit 4 */
100#define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
101#define KU_CRL_SIGN (0x02) /* bit 6 */
102
103/*
Paul Bakker74111d32011-01-15 16:57:55 +0000104 * Netscape certificate types
105 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
106 */
107
108#define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
109#define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
110#define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
111#define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
112#define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
113#define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
114#define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
115#define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
116
Paul Bakkerc70b9822013-04-07 22:00:46 +0200117/*
118 * X.509 extension types
119 */
Paul Bakker74111d32011-01-15 16:57:55 +0000120#define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
121#define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
122#define EXT_KEY_USAGE (1 << 2)
123#define EXT_CERTIFICATE_POLICIES (1 << 3)
124#define EXT_POLICY_MAPPINGS (1 << 4)
125#define EXT_SUBJECT_ALT_NAME (1 << 5)
126#define EXT_ISSUER_ALT_NAME (1 << 6)
127#define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
128#define EXT_BASIC_CONSTRAINTS (1 << 8)
129#define EXT_NAME_CONSTRAINTS (1 << 9)
130#define EXT_POLICY_CONSTRAINTS (1 << 10)
131#define EXT_EXTENDED_KEY_USAGE (1 << 11)
132#define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
133#define EXT_INIHIBIT_ANYPOLICY (1 << 13)
134#define EXT_FRESHEST_CRL (1 << 14)
135
136#define EXT_NS_CERT_TYPE (1 << 16)
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000138/*
139 * Storage format identifiers
140 * Recognized formats: PEM and DER
141 */
142#define X509_FORMAT_DER 1
143#define X509_FORMAT_PEM 2
144
Paul Bakker407a0da2013-06-27 14:29:21 +0200145#ifdef __cplusplus
146extern "C" {
147#endif
148
Paul Bakker37ca75d2011-01-06 12:28:03 +0000149/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000150 * \addtogroup x509_module
151 * \{ */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000152
153/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000154 * \name Structures for parsing X.509 certificates and CRLs
155 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000156 */
157
158/**
159 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000161typedef asn1_buf x509_buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Paul Bakker37ca75d2011-01-06 12:28:03 +0000163/**
Paul Bakker74111d32011-01-15 16:57:55 +0000164 * Container for ASN1 bit strings.
165 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000166typedef asn1_bitstring x509_bitstring;
Paul Bakker74111d32011-01-15 16:57:55 +0000167
168/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000169 * Container for ASN1 named information objects.
170 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
171 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000172typedef struct _x509_name
173{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000174 x509_buf oid; /**< The object identifier. */
175 x509_buf val; /**< The named value. */
176 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000177}
178x509_name;
179
Paul Bakker74111d32011-01-15 16:57:55 +0000180/**
181 * Container for a sequence of ASN.1 items
182 */
Paul Bakkerefc30292011-11-10 14:43:23 +0000183typedef asn1_sequence x509_sequence;
Paul Bakker74111d32011-01-15 16:57:55 +0000184
Paul Bakker37ca75d2011-01-06 12:28:03 +0000185/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000186typedef struct _x509_time
187{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000188 int year, mon, day; /**< Date. */
189 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000190}
191x509_time;
192
Paul Bakker37ca75d2011-01-06 12:28:03 +0000193/**
194 * Container for an X.509 certificate. The certificate may be chained.
195 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000196typedef struct _x509_cert
197{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000198 x509_buf raw; /**< The raw certificate data (DER). */
199 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Paul Bakker37ca75d2011-01-06 12:28:03 +0000201 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
202 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
203 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakker37ca75d2011-01-06 12:28:03 +0000205 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
206 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Paul Bakker37ca75d2011-01-06 12:28:03 +0000208 x509_name issuer; /**< The parsed issuer data (named information object). */
209 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker37ca75d2011-01-06 12:28:03 +0000211 x509_time valid_from; /**< Start time of certificate validity. */
212 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker37ca75d2011-01-06 12:28:03 +0000214 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
215 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 +0000216
Paul Bakker37ca75d2011-01-06 12:28:03 +0000217 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
218 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
219 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakkera8cd2392012-02-11 16:09:32 +0000220 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakker74111d32011-01-15 16:57:55 +0000222 int ext_types; /**< Bit string containing detected and parsed extensions */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000223 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 +0000224 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 +0000225
Paul Bakker74111d32011-01-15 16:57:55 +0000226 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
227
228 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
229
230 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
231
Paul Bakker37ca75d2011-01-06 12:28:03 +0000232 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
233 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200234 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
235 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 +0000236
Paul Bakker37ca75d2011-01-06 12:28:03 +0000237 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000238}
239x509_cert;
240
Paul Bakker37ca75d2011-01-06 12:28:03 +0000241/**
242 * Certificate revocation list entry.
243 * Contains the CA-specific serial numbers and revocation dates.
244 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000245typedef struct _x509_crl_entry
246{
247 x509_buf raw;
248
249 x509_buf serial;
250
251 x509_time revocation_date;
252
253 x509_buf entry_ext;
254
255 struct _x509_crl_entry *next;
256}
257x509_crl_entry;
258
Paul Bakker37ca75d2011-01-06 12:28:03 +0000259/**
260 * Certificate revocation list structure.
261 * Every CRL may have multiple entries.
262 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000263typedef struct _x509_crl
264{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000265 x509_buf raw; /**< The raw certificate data (DER). */
266 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000267
268 int version;
269 x509_buf sig_oid1;
270
Paul Bakker37ca75d2011-01-06 12:28:03 +0000271 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000272
Paul Bakker37ca75d2011-01-06 12:28:03 +0000273 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000274
Paul Bakker37ca75d2011-01-06 12:28:03 +0000275 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000276 x509_time next_update;
277
Paul Bakker37ca75d2011-01-06 12:28:03 +0000278 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000279
280 x509_buf crl_ext;
281
282 x509_buf sig_oid2;
283 x509_buf sig;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200284 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
285 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 +0000286
287 struct _x509_crl *next;
288}
289x509_crl;
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000290/** \} name Structures for parsing X.509 certificates and CRLs */
291/** \} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000292
Paul Bakker37ca75d2011-01-06 12:28:03 +0000293/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000294 * \name Structures for writing X.509 certificates.
Paul Bakker37ca75d2011-01-06 12:28:03 +0000295 * XvP: commented out as they are not used.
296 * - <tt>typedef struct _x509_node x509_node;</tt>
297 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000299/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000300typedef struct _x509_node
301{
302 unsigned char *data;
303 unsigned char *p;
304 unsigned char *end;
305
306 size_t len;
307}
308x509_node;
309
310typedef struct _x509_raw
311{
312 x509_node raw;
313 x509_node tbs;
314
315 x509_node version;
316 x509_node serial;
317 x509_node tbs_signalg;
318 x509_node issuer;
319 x509_node validity;
320 x509_node subject;
321 x509_node subpubkey;
322
323 x509_node signalg;
324 x509_node sign;
325}
326x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000327*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Paul Bakker5121ce52009-01-03 21:22:43 +0000329/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000330 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
331 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000332 */
333
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000334/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000335/**
Paul Bakker89ecb2d2013-06-24 19:06:15 +0200336 * \brief Parse a single DER formatted certificate and add it
337 * to the chained list.
338 *
339 * \param chain points to the start of the chain
340 * \param buf buffer holding the certificate DER data
341 * \param buflen size of the buffer
342 *
343 * \return 0 if successful, or a specific X509 or PEM error code
344 */
345int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen );
346
347/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 * \brief Parse one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000349 * to the chained list. Parses permissively. If some
350 * certificates can be parsed, the result is the number
351 * of failed certificates it encountered. If none complete
352 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 *
354 * \param chain points to the start of the chain
355 * \param buf buffer holding the certificate data
356 * \param buflen size of the buffer
357 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000358 * \return 0 if all certificates parsed successfully, a positive number
359 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000361int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000363/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000364/**
365 * \brief Load one or more certificates and add them
Paul Bakker69e095c2011-12-10 21:55:01 +0000366 * to the chained list. Parses permissively. If some
367 * certificates can be parsed, the result is the number
368 * of failed certificates it encountered. If none complete
369 * correctly, the first error is returned.
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 *
371 * \param chain points to the start of the chain
372 * \param path filename to read the certificates from
373 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000374 * \return 0 if all certificates parsed successfully, a positive number
375 * if partly successful or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 */
Paul Bakker69e095c2011-12-10 21:55:01 +0000377int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000379/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000380/**
Paul Bakker8d914582012-06-04 12:46:42 +0000381 * \brief Load one or more certificate files from a path and add them
382 * to the chained list. Parses permissively. If some
383 * certificates can be parsed, the result is the number
384 * of failed certificates it encountered. If none complete
385 * correctly, the first error is returned.
386 *
387 * \param chain points to the start of the chain
388 * \param path directory / folder to read the certificate files from
389 *
390 * \return 0 if all certificates parsed successfully, a positive number
391 * if partly successful or a specific X509 or PEM error code
392 */
393int x509parse_crtpath( x509_cert *chain, const char *path );
394
395/** \ingroup x509_module */
396/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000397 * \brief Parse one or more CRLs and add them
398 * to the chained list
399 *
400 * \param chain points to the start of the chain
401 * \param buf buffer holding the CRL data
402 * \param buflen size of the buffer
403 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000404 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000405 */
Paul Bakker23986e52011-04-24 08:57:21 +0000406int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000407
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000408/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000409/**
410 * \brief Load one or more CRLs and add them
411 * to the chained list
412 *
413 * \param chain points to the start of the chain
414 * \param path filename to read the CRLs from
415 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000416 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakkerd98030e2009-05-02 15:13:40 +0000417 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000418int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000419
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000420/** \ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000421/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 * \brief Parse a private RSA key
423 *
424 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000425 * \param key input buffer
426 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 * \param pwd password for decryption (optional)
428 * \param pwdlen size of the password
429 *
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 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200432int x509parse_key_rsa( rsa_context *rsa,
433 const unsigned char *key, size_t keylen,
434 const unsigned char *pwd, size_t pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000436/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000437/**
438 * \brief Load and parse a private RSA key
439 *
440 * \param rsa RSA context to be initialized
441 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000442 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000444 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200446int x509parse_keyfile_rsa( rsa_context *rsa, const char *path,
447 const char *password );
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
462/** \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 );
Paul Bakker917e7542011-03-25 14:23:36 +0000472
473/** \ingroup x509_module */
474/**
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +0200475 * \brief Parse a private EC key
476 *
477 * \param eckey EC key to be initialized
478 * \param key input buffer
479 * \param keylen size of the buffer
480 * \param pwd password for decryption (optional)
481 * \param pwdlen size of the password
482 *
483 * \return 0 if successful, or a specific X509 or PEM error code
484 */
485int x509parse_key_ec( ecp_keypair *eckey,
486 const unsigned char *key, size_t keylen,
487 const unsigned char *pwd, size_t pwdlen );
488
489/** \ingroup x509_module */
490/**
491 * \brief Load and parse a private EC key
492 *
493 * \param eckey EC key to be initialized
494 * \param path filename to read the private key from
495 * \param password password to decrypt the file (can be NULL)
496 *
497 * \return 0 if successful, or a specific X509 or PEM error code
498 */
499int x509parse_keyfile_ec( ecp_keypair *eckey,
500 const char *path, const char *password );
501
502/** \ingroup x509_module */
503/**
504 * \brief Parse a public EC key
505 *
506 * \param eckey EC key to be initialized
507 * \param key input buffer
508 * \param keylen size of the buffer
509 *
510 * \return 0 if successful, or a specific X509 or PEM error code
511 */
512int x509parse_public_key_ec( ecp_keypair *eckey,
513 const unsigned char *key, size_t keylen );
514
515/** \ingroup x509_module */
516/**
517 * \brief Load and parse a public EC key
518 *
519 * \param eckey EC key to be initialized
520 * \param path filename to read the private key from
521 *
522 * \return 0 if successful, or a specific X509 or PEM error code
523 */
524int x509parse_public_keyfile_ec( ecp_keypair *eckey, const char *path );
525
526/** \ingroup x509_module */
527/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000528 * \brief Parse DHM parameters
529 *
530 * \param dhm DHM context to be initialized
531 * \param dhmin input buffer
532 * \param dhminlen size of the buffer
533 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000534 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000535 */
Paul Bakker23986e52011-04-24 08:57:21 +0000536int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
Paul Bakker1b57b062011-01-06 15:48:19 +0000537
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000538/** \ingroup x509_module */
Paul Bakker1b57b062011-01-06 15:48:19 +0000539/**
540 * \brief Load and parse DHM parameters
541 *
542 * \param dhm DHM context to be initialized
543 * \param path filename to read the DHM Parameters from
544 *
Paul Bakker96743fc2011-02-12 14:30:57 +0000545 * \return 0 if successful, or a specific X509 or PEM error code
Paul Bakker1b57b062011-01-06 15:48:19 +0000546 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000547int x509parse_dhmfile( dhm_context *dhm, const char *path );
Paul Bakker1b57b062011-01-06 15:48:19 +0000548
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000549/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000550
Paul Bakker5121ce52009-01-03 21:22:43 +0000551/**
552 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000553 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000554 *
555 * \param buf Buffer to write to
556 * \param size Maximum size of buffer
557 * \param dn The X509 name to represent
558 *
559 * \return The amount of data written to the buffer, or -1 in
560 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000562int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564/**
Paul Bakkerdd476992011-01-16 21:34:59 +0000565 * \brief Store the certificate serial in printable form into buf;
566 * no more than size characters will be written.
567 *
568 * \param buf Buffer to write to
569 * \param size Maximum size of buffer
570 * \param serial The X509 serial to represent
571 *
572 * \return The amount of data written to the buffer, or -1 in
573 * case of an error.
574 */
575int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
576
577/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 * \brief Returns an informational string about the
579 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000580 *
581 * \param buf Buffer to write to
582 * \param size Maximum size of buffer
583 * \param prefix A line prefix
584 * \param crt The X509 certificate to represent
585 *
586 * \return The amount of data written to the buffer, or -1 in
587 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000589int x509parse_cert_info( char *buf, size_t size, const char *prefix,
590 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000591
592/**
593 * \brief Returns an informational string about the
594 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000595 *
596 * \param buf Buffer to write to
597 * \param size Maximum size of buffer
598 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000599 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000600 *
601 * \return The amount of data written to the buffer, or -1 in
602 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000603 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604int x509parse_crl_info( char *buf, size_t size, const char *prefix,
605 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607/**
Paul Bakker74111d32011-01-15 16:57:55 +0000608 * \brief Give an known OID, return its descriptive string.
609 *
610 * \param oid buffer containing the oid
611 *
612 * \return Return a string if the OID is known,
613 * or NULL otherwise.
614 */
615const char *x509_oid_get_description( x509_buf *oid );
616
Paul Bakker9a736322012-11-14 12:39:52 +0000617/**
Paul Bakker74111d32011-01-15 16:57:55 +0000618 * \brief Give an OID, return a string version of its OID number.
Paul Bakkerd5c2b542013-04-07 22:34:26 +0200619 * (Deprecated. Use oid_get_numeric_string() instead)
Paul Bakker74111d32011-01-15 16:57:55 +0000620 *
621 * \param buf Buffer to write to
622 * \param size Maximum size of buffer
623 * \param oid Buffer containing the OID
624 *
625 * \return The amount of data written to the buffer, or -1 in
626 * case of an error.
627 */
628int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
629
630/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000631 * \brief Check a given x509_time against the system time and check
632 * if it is valid.
633 *
634 * \param time x509_time to check
635 *
636 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000637 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000639int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000642 * \name Functions to verify a certificate
643 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000644 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000645/** \ingroup x509_module */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000646/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 * \brief Verify the certificate signature
648 *
Paul Bakker915275b2012-09-28 07:10:55 +0000649 * The verify callback is a user-supplied callback that
650 * can clear / modify / add flags for a certificate. If set,
651 * the verification callback is called for each
652 * certificate in the chain (from the trust-ca down to the
653 * presented crt). The parameters for the callback are:
654 * (void *parameter, x509_cert *crt, int certificate_depth,
655 * int *flags). With the flags representing current flags for
656 * that specific certificate and the certificate depth from
Paul Bakker9a736322012-11-14 12:39:52 +0000657 * the bottom (Peer cert depth = 0).
Paul Bakker915275b2012-09-28 07:10:55 +0000658 *
659 * All flags left after returning from the callback
660 * are also returned to the application. The function should
661 * return 0 for anything but a fatal error.
662 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 * \param crt a certificate to be verified
664 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000665 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * \param cn expected Common Name (can be set to
667 * NULL if the CN must not be verified)
668 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000669 * \param f_vrfy verification function
670 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 *
Paul Bakker40e46942009-01-03 21:51:57 +0000672 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 * in which case *flags will have one or more of
674 * the following values set:
675 * BADCERT_EXPIRED --
676 * BADCERT_REVOKED --
677 * BADCERT_CN_MISMATCH --
678 * BADCERT_NOT_TRUSTED
Paul Bakker915275b2012-09-28 07:10:55 +0000679 * or another error in case of a fatal error encountered
680 * during the verification process.
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 */
682int x509parse_verify( x509_cert *crt,
683 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000684 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000685 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +0000686 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000687 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Paul Bakker74111d32011-01-15 16:57:55 +0000689/**
690 * \brief Verify the certificate signature
691 *
692 * \param crt a certificate to be verified
693 * \param crl the CRL to verify against
694 *
695 * \return 1 if the certificate is revoked, 0 otherwise
696 *
697 */
698int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
699
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000700/** \} name Functions to verify a certificate */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000701
702
703
704/**
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000705 * \name Functions to clear a certificate, CRL or private RSA key
706 * \{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000707 */
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000708/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000709/**
710 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000711 *
712 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 */
714void x509_free( x509_cert *crt );
715
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000716/** \ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000717/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000718 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000719 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000720 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000721 */
722void x509_crl_free( x509_crl *crl );
723
Paul Bakker0f5f72e2011-01-18 14:58:55 +0000724/** \} name Functions to clear a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000725
726
Paul Bakkerd98030e2009-05-02 15:13:40 +0000727/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 * \brief Checkup routine
729 *
730 * \return 0 if successful, or 1 if the test failed
731 */
732int x509_self_test( int verbose );
733
734#ifdef __cplusplus
735}
736#endif
737
Paul Bakkered27a042013-04-18 22:46:23 +0200738#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000739#endif /* x509.h */