blob: cbcb5b09e4524c309f481533af2c59e72626ecdf [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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 Bakker8e831ed2009-01-03 21:24:11 +000030#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker37ca75d2011-01-06 12:28:03 +000032/**
33 * @addtogroup x509_module
34 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000035 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000036
37/**
38 * @name ASN1 Error codes
39 * These error codes are OR'ed to X509 error codes for
40 * higher error granularity.
41 * ASN1 is a standard to specify data structures.
42 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000043 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000044#define POLARSSL_ERR_ASN1_OUT_OF_DATA 0x0014 /**< Out of data when parsing an ASN1 data structure. */
45#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG 0x0016 /**< ASN1 tag was of an unexpected value. */
46#define POLARSSL_ERR_ASN1_INVALID_LENGTH 0x0018 /**< Error when trying to determine the length or invalid length. */
47#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH 0x001A /**< Actual length differs from expected length. */
48#define POLARSSL_ERR_ASN1_INVALID_DATA 0x001C /**< Data is invalid. (not used) */
49/* @} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakker37ca75d2011-01-06 12:28:03 +000051/**
52 * @name X509 Error codes
53 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000054 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000055#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
56#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
57#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060 /**< The certificate format is invalid, e.g. different type expected. */
58#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080 /**< The certificate version element is invalid. */
59#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0 /**< The serial tag or value is invalid. */
60#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0 /**< The algorithm tag or value is invalid. */
61#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0 /**< The name tag or value is invalid. */
62#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100 /**< The date tag or value is invalid. */
63#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120 /**< The pubkey tag or value is invalid (only RSA is supported). */
64#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140 /**< The signature tag or value invalid. */
65#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160 /**< The extension tag or value is invalid. */
66#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180 /**< Certificate or CRL has an unsupported version number. */
67#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0 /**< Signature algorithm (oid) is unsupported. */
68#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0 /**< Public key algorithm is unsupported (only RSA is supported). */
69#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
70#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
71#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220 /**< PEM key string is not as expected. */
72#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240 /**< Unsupported RSA key version */
73#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260 /**< Invalid RSA key tag or value. */
74#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280 /**< RSA IV is not in hex-format. */
75#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0 /**< Unsupported key encryption algorithm. */
76#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0 /**< Private key password can't be empty. */
77#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0 /**< Given private key password does not allow for correct decryption. */
78#define POLARSSL_ERR_X509_POINT_ERROR -0x0300 /**< Not used. */
79#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320 /**< Not used. */
80/* @} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Paul Bakker37ca75d2011-01-06 12:28:03 +000082
83/**
84 * @name X509 Verify codes
85 * @{
86 */
87#define BADCERT_EXPIRED 1 /**< The certificate validity has expired. */
88#define BADCERT_REVOKED 2 /**< The certificate has been revoked (is on a CRL). */
89#define BADCERT_CN_MISMATCH 4 /**< The certificate Common Name (CN) does not match with the expected CN. */
90#define BADCERT_NOT_TRUSTED 8 /**< The certificate is not correctly signed by the trusted CA. */
91#define BADCRL_NOT_TRUSTED 16 /**< CRL is not correctly signed by the trusted CA. */
92#define BADCRL_EXPIRED 32 /**< CRL is expired. */
93/* @} name */
94
95
96/**
97 * @name DER constants
98 * These constants comply with DER encoded the ANS1 type tags.
99 * DER encoding uses hexadecimal representation.
100 * An example DER sequence is:\n
101 * - 0x02 -- tag indicating INTEGER
102 * - 0x01 -- length in octets
103 * - 0x05 -- value
104 * Such sequences are typically read into \c ::x509_buf.
105 * @{
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
107#define ASN1_BOOLEAN 0x01
108#define ASN1_INTEGER 0x02
109#define ASN1_BIT_STRING 0x03
110#define ASN1_OCTET_STRING 0x04
111#define ASN1_NULL 0x05
112#define ASN1_OID 0x06
113#define ASN1_UTF8_STRING 0x0C
114#define ASN1_SEQUENCE 0x10
115#define ASN1_SET 0x11
116#define ASN1_PRINTABLE_STRING 0x13
117#define ASN1_T61_STRING 0x14
118#define ASN1_IA5_STRING 0x16
119#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +0000120#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +0000121#define ASN1_UNIVERSAL_STRING 0x1C
122#define ASN1_BMP_STRING 0x1E
123#define ASN1_PRIMITIVE 0x00
124#define ASN1_CONSTRUCTED 0x20
125#define ASN1_CONTEXT_SPECIFIC 0x80
Paul Bakker37ca75d2011-01-06 12:28:03 +0000126/* @} name */
127/* @} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/*
130 * various object identifiers
131 */
132#define X520_COMMON_NAME 3
133#define X520_COUNTRY 6
134#define X520_LOCALITY 7
135#define X520_STATE 8
136#define X520_ORGANIZATION 10
137#define X520_ORG_UNIT 11
138#define PKCS9_EMAIL 1
139
140#define X509_OUTPUT_DER 0x01
141#define X509_OUTPUT_PEM 0x02
142#define PEM_LINE_LENGTH 72
143#define X509_ISSUER 0x01
144#define X509_SUBJECT 0x02
145
146#define OID_X520 "\x55\x04"
147#define OID_CN "\x55\x04\x03"
148#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
149#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
150#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
151#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
152#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
153
Paul Bakker37ca75d2011-01-06 12:28:03 +0000154/**
155 * @addtogroup x509_module
156 * @{ */
157
158/**
159 * @name Structures for parsing X.509 certificates and CRLs
160 * @{
161 */
162
163/**
164 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
166typedef struct _x509_buf
167{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000168 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
169 int len; /**< ASN1 length, e.g. in octets. */
170 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171}
172x509_buf;
173
Paul Bakker37ca75d2011-01-06 12:28:03 +0000174/**
175 * Container for ASN1 named information objects.
176 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
177 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178typedef struct _x509_name
179{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000180 x509_buf oid; /**< The object identifier. */
181 x509_buf val; /**< The named value. */
182 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000183}
184x509_name;
185
Paul Bakker37ca75d2011-01-06 12:28:03 +0000186/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000187typedef struct _x509_time
188{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000189 int year, mon, day; /**< Date. */
190 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000191}
192x509_time;
193
Paul Bakker37ca75d2011-01-06 12:28:03 +0000194/**
195 * Container for an X.509 certificate. The certificate may be chained.
196 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000197typedef struct _x509_cert
198{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000199 x509_buf raw; /**< The raw certificate data (DER). */
200 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker37ca75d2011-01-06 12:28:03 +0000202 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
203 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
204 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Paul Bakker37ca75d2011-01-06 12:28:03 +0000206 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
207 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
Paul Bakker37ca75d2011-01-06 12:28:03 +0000209 x509_name issuer; /**< The parsed issuer data (named information object). */
210 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Paul Bakker37ca75d2011-01-06 12:28:03 +0000212 x509_time valid_from; /**< Start time of certificate validity. */
213 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Paul Bakker37ca75d2011-01-06 12:28:03 +0000215 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
216 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 +0000217
Paul Bakker37ca75d2011-01-06 12:28:03 +0000218 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
219 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
220 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakker37ca75d2011-01-06 12:28:03 +0000222 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
223 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Paul Bakker37ca75d2011-01-06 12:28:03 +0000225 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
226 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
227 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Paul Bakker37ca75d2011-01-06 12:28:03 +0000229 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230}
231x509_cert;
232
Paul Bakker37ca75d2011-01-06 12:28:03 +0000233/**
234 * Certificate revocation list entry.
235 * Contains the CA-specific serial numbers and revocation dates.
236 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000237typedef struct _x509_crl_entry
238{
239 x509_buf raw;
240
241 x509_buf serial;
242
243 x509_time revocation_date;
244
245 x509_buf entry_ext;
246
247 struct _x509_crl_entry *next;
248}
249x509_crl_entry;
250
Paul Bakker37ca75d2011-01-06 12:28:03 +0000251/**
252 * Certificate revocation list structure.
253 * Every CRL may have multiple entries.
254 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000255typedef struct _x509_crl
256{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000257 x509_buf raw; /**< The raw certificate data (DER). */
258 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000259
260 int version;
261 x509_buf sig_oid1;
262
Paul Bakker37ca75d2011-01-06 12:28:03 +0000263 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000264
Paul Bakker37ca75d2011-01-06 12:28:03 +0000265 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000266
Paul Bakker37ca75d2011-01-06 12:28:03 +0000267 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000268 x509_time next_update;
269
Paul Bakker37ca75d2011-01-06 12:28:03 +0000270 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000271
272 x509_buf crl_ext;
273
274 x509_buf sig_oid2;
275 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000276 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000277
278 struct _x509_crl *next;
279}
280x509_crl;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000281/** @} name Structures for parsing X.509 certificates and CRLs */
282/** @} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000283
Paul Bakker37ca75d2011-01-06 12:28:03 +0000284/**
285 * @name Structures for writing X.509 certificates.
286 * XvP: commented out as they are not used.
287 * - <tt>typedef struct _x509_node x509_node;</tt>
288 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000290/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000291typedef struct _x509_node
292{
293 unsigned char *data;
294 unsigned char *p;
295 unsigned char *end;
296
297 size_t len;
298}
299x509_node;
300
301typedef struct _x509_raw
302{
303 x509_node raw;
304 x509_node tbs;
305
306 x509_node version;
307 x509_node serial;
308 x509_node tbs_signalg;
309 x509_node issuer;
310 x509_node validity;
311 x509_node subject;
312 x509_node subpubkey;
313
314 x509_node signalg;
315 x509_node sign;
316}
317x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000318*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320#ifdef __cplusplus
321extern "C" {
322#endif
323
324/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000325 * @name Functions to read in a certificate, CRL or private RSA key
326 * @{
327 */
328
329/** @ingroup x509_module */
330/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 * \brief Parse one or more certificates and add them
332 * to the chained list
333 *
334 * \param chain points to the start of the chain
335 * \param buf buffer holding the certificate data
336 * \param buflen size of the buffer
337 *
338 * \return 0 if successful, or a specific X509 error code
339 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000340int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Paul Bakker37ca75d2011-01-06 12:28:03 +0000342/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000343/**
344 * \brief Load one or more certificates and add them
345 * to the chained list
346 *
347 * \param chain points to the start of the chain
348 * \param path filename to read the certificates from
349 *
350 * \return 0 if successful, or a specific X509 error code
351 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000352int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Paul Bakker37ca75d2011-01-06 12:28:03 +0000354/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000356 * \brief Parse one or more CRLs and add them
357 * to the chained list
358 *
359 * \param chain points to the start of the chain
360 * \param buf buffer holding the CRL data
361 * \param buflen size of the buffer
362 *
363 * \return 0 if successful, or a specific X509 error code
364 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000365int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000366
Paul Bakker37ca75d2011-01-06 12:28:03 +0000367/** @ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368/**
369 * \brief Load one or more CRLs and add them
370 * to the chained list
371 *
372 * \param chain points to the start of the chain
373 * \param path filename to read the CRLs from
374 *
375 * \return 0 if successful, or a specific X509 error code
376 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000378
Paul Bakker37ca75d2011-01-06 12:28:03 +0000379/** @ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000380/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 * \brief Parse a private RSA key
382 *
383 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000384 * \param key input buffer
385 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 * \param pwd password for decryption (optional)
387 * \param pwdlen size of the password
388 *
389 * \return 0 if successful, or a specific X509 error code
390 */
391int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000392 const unsigned char *key, int keylen,
393 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
Paul Bakker37ca75d2011-01-06 12:28:03 +0000395/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000396/**
397 * \brief Load and parse a private RSA key
398 *
399 * \param rsa RSA context to be initialized
400 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000401 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 *
403 * \return 0 if successful, or a specific X509 error code
404 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000405int x509parse_keyfile( rsa_context *rsa, const char *path,
406 const char *password );
Paul Bakker37ca75d2011-01-06 12:28:03 +0000407/** @} name Functions to read in a certificate, CRL or private RSA key */
408
409
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411/**
412 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000413 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000414 *
415 * \param buf Buffer to write to
416 * \param size Maximum size of buffer
417 * \param dn The X509 name to represent
418 *
419 * \return The amount of data written to the buffer, or -1 in
420 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000422int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424/**
425 * \brief Returns an informational string about the
426 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000427 *
428 * \param buf Buffer to write to
429 * \param size Maximum size of buffer
430 * \param prefix A line prefix
431 * \param crt The X509 certificate to represent
432 *
433 * \return The amount of data written to the buffer, or -1 in
434 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000436int x509parse_cert_info( char *buf, size_t size, const char *prefix,
437 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000438
439/**
440 * \brief Returns an informational string about the
441 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000442 *
443 * \param buf Buffer to write to
444 * \param size Maximum size of buffer
445 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000446 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000447 *
448 * \return The amount of data written to the buffer, or -1 in
449 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000450 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000451int x509parse_crl_info( char *buf, size_t size, const char *prefix,
452 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \brief Check a given x509_time against the system time and check
456 * if it is valid.
457 *
458 * \param time x509_time to check
459 *
460 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000461 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000463int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000466 * @name Functions to verify a certificate
467 * @{
468 */
469/** @ingroup x509_module */
470/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * \brief Verify the certificate signature
472 *
473 * \param crt a certificate to be verified
474 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000475 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * \param cn expected Common Name (can be set to
477 * NULL if the CN must not be verified)
478 * \param flags result of the verification
479 *
Paul Bakker40e46942009-01-03 21:51:57 +0000480 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 * in which case *flags will have one or more of
482 * the following values set:
483 * BADCERT_EXPIRED --
484 * BADCERT_REVOKED --
485 * BADCERT_CN_MISMATCH --
486 * BADCERT_NOT_TRUSTED
487 *
488 * \note TODO: add two arguments, depth and crl
489 */
490int x509parse_verify( x509_cert *crt,
491 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000492 x509_crl *ca_crl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000493 const char *cn, int *flags );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
Paul Bakker37ca75d2011-01-06 12:28:03 +0000495/** @} name Functions to verify a certificate */
496
497
498
499/**
500 * @name Functions to clear a certificate, CRL or private RSA key
501 * @{
502 */
503/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000504/**
505 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000506 *
507 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 */
509void x509_free( x509_cert *crt );
510
Paul Bakker37ca75d2011-01-06 12:28:03 +0000511/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000512/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000513 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000514 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000515 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000516 */
517void x509_crl_free( x509_crl *crl );
518
Paul Bakker37ca75d2011-01-06 12:28:03 +0000519/** @} name Functions to clear a certificate, CRL or private RSA key */
520
521
Paul Bakkerd98030e2009-05-02 15:13:40 +0000522/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 * \brief Checkup routine
524 *
525 * \return 0 if successful, or 1 if the test failed
526 */
527int x509_self_test( int verbose );
528
529#ifdef __cplusplus
530}
531#endif
532
533#endif /* x509.h */