blob: c54fc2dd380b2fa712b4262791a550e588beb891 [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 Bakker1b57b062011-01-06 15:48:19 +000031#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker37ca75d2011-01-06 12:28:03 +000033/**
34 * @addtogroup x509_module
35 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000036 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000037
38/**
39 * @name ASN1 Error codes
40 * These error codes are OR'ed to X509 error codes for
41 * higher error granularity.
42 * ASN1 is a standard to specify data structures.
43 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000045#define POLARSSL_ERR_ASN1_OUT_OF_DATA 0x0014 /**< Out of data when parsing an ASN1 data structure. */
46#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG 0x0016 /**< ASN1 tag was of an unexpected value. */
47#define POLARSSL_ERR_ASN1_INVALID_LENGTH 0x0018 /**< Error when trying to determine the length or invalid length. */
48#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH 0x001A /**< Actual length differs from expected length. */
49#define POLARSSL_ERR_ASN1_INVALID_DATA 0x001C /**< Data is invalid. (not used) */
50/* @} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakker37ca75d2011-01-06 12:28:03 +000052/**
53 * @name X509 Error codes
54 * @{
Paul Bakker13e2dfe2009-07-28 07:18:38 +000055 */
Paul Bakker37ca75d2011-01-06 12:28:03 +000056#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
57#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
58#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060 /**< The certificate format is invalid, e.g. different type expected. */
59#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080 /**< The certificate version element is invalid. */
60#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0 /**< The serial tag or value is invalid. */
61#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0 /**< The algorithm tag or value is invalid. */
62#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0 /**< The name tag or value is invalid. */
63#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100 /**< The date tag or value is invalid. */
64#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120 /**< The pubkey tag or value is invalid (only RSA is supported). */
65#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140 /**< The signature tag or value invalid. */
66#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160 /**< The extension tag or value is invalid. */
67#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180 /**< Certificate or CRL has an unsupported version number. */
68#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0 /**< Signature algorithm (oid) is unsupported. */
69#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0 /**< Public key algorithm is unsupported (only RSA is supported). */
70#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
71#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
72#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220 /**< PEM key string is not as expected. */
73#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240 /**< Unsupported RSA key version */
74#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260 /**< Invalid RSA key tag or value. */
75#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280 /**< RSA IV is not in hex-format. */
76#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0 /**< Unsupported key encryption algorithm. */
77#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0 /**< Private key password can't be empty. */
78#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0 /**< Given private key password does not allow for correct decryption. */
79#define POLARSSL_ERR_X509_POINT_ERROR -0x0300 /**< Not used. */
80#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320 /**< Not used. */
81/* @} name */
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakker37ca75d2011-01-06 12:28:03 +000083
84/**
85 * @name X509 Verify codes
86 * @{
87 */
88#define BADCERT_EXPIRED 1 /**< The certificate validity has expired. */
89#define BADCERT_REVOKED 2 /**< The certificate has been revoked (is on a CRL). */
90#define BADCERT_CN_MISMATCH 4 /**< The certificate Common Name (CN) does not match with the expected CN. */
91#define BADCERT_NOT_TRUSTED 8 /**< The certificate is not correctly signed by the trusted CA. */
92#define BADCRL_NOT_TRUSTED 16 /**< CRL is not correctly signed by the trusted CA. */
93#define BADCRL_EXPIRED 32 /**< CRL is expired. */
94/* @} name */
95
96
97/**
98 * @name DER constants
99 * These constants comply with DER encoded the ANS1 type tags.
100 * DER encoding uses hexadecimal representation.
101 * An example DER sequence is:\n
102 * - 0x02 -- tag indicating INTEGER
103 * - 0x01 -- length in octets
104 * - 0x05 -- value
105 * Such sequences are typically read into \c ::x509_buf.
106 * @{
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 */
108#define ASN1_BOOLEAN 0x01
109#define ASN1_INTEGER 0x02
110#define ASN1_BIT_STRING 0x03
111#define ASN1_OCTET_STRING 0x04
112#define ASN1_NULL 0x05
113#define ASN1_OID 0x06
114#define ASN1_UTF8_STRING 0x0C
115#define ASN1_SEQUENCE 0x10
116#define ASN1_SET 0x11
117#define ASN1_PRINTABLE_STRING 0x13
118#define ASN1_T61_STRING 0x14
119#define ASN1_IA5_STRING 0x16
120#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +0000121#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +0000122#define ASN1_UNIVERSAL_STRING 0x1C
123#define ASN1_BMP_STRING 0x1E
124#define ASN1_PRIMITIVE 0x00
125#define ASN1_CONSTRUCTED 0x20
126#define ASN1_CONTEXT_SPECIFIC 0x80
Paul Bakker37ca75d2011-01-06 12:28:03 +0000127/* @} name */
128/* @} addtogroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/*
131 * various object identifiers
132 */
133#define X520_COMMON_NAME 3
134#define X520_COUNTRY 6
135#define X520_LOCALITY 7
136#define X520_STATE 8
137#define X520_ORGANIZATION 10
138#define X520_ORG_UNIT 11
139#define PKCS9_EMAIL 1
140
141#define X509_OUTPUT_DER 0x01
142#define X509_OUTPUT_PEM 0x02
143#define PEM_LINE_LENGTH 72
144#define X509_ISSUER 0x01
145#define X509_SUBJECT 0x02
146
147#define OID_X520 "\x55\x04"
148#define OID_CN "\x55\x04\x03"
149#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
150#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
151#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
152#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
153#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
154
Paul Bakker37ca75d2011-01-06 12:28:03 +0000155/**
156 * @addtogroup x509_module
157 * @{ */
158
159/**
160 * @name Structures for parsing X.509 certificates and CRLs
161 * @{
162 */
163
164/**
165 * Type-length-value structure that allows for ASN1 using DER.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
167typedef struct _x509_buf
168{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000169 int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
170 int len; /**< ASN1 length, e.g. in octets. */
171 unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000172}
173x509_buf;
174
Paul Bakker37ca75d2011-01-06 12:28:03 +0000175/**
176 * Container for ASN1 named information objects.
177 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
178 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179typedef struct _x509_name
180{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000181 x509_buf oid; /**< The object identifier. */
182 x509_buf val; /**< The named value. */
183 struct _x509_name *next; /**< The next named information object. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
185x509_name;
186
Paul Bakker37ca75d2011-01-06 12:28:03 +0000187/** Container for date and time (precision in seconds). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000188typedef struct _x509_time
189{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000190 int year, mon, day; /**< Date. */
191 int hour, min, sec; /**< Time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000192}
193x509_time;
194
Paul Bakker37ca75d2011-01-06 12:28:03 +0000195/**
196 * Container for an X.509 certificate. The certificate may be chained.
197 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000198typedef struct _x509_cert
199{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000200 x509_buf raw; /**< The raw certificate data (DER). */
201 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Paul Bakker37ca75d2011-01-06 12:28:03 +0000203 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
204 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
205 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
Paul Bakker37ca75d2011-01-06 12:28:03 +0000207 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
208 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Paul Bakker37ca75d2011-01-06 12:28:03 +0000210 x509_name issuer; /**< The parsed issuer data (named information object). */
211 x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
Paul Bakker37ca75d2011-01-06 12:28:03 +0000213 x509_time valid_from; /**< Start time of certificate validity. */
214 x509_time valid_to; /**< End time of certificate validity. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Paul Bakker37ca75d2011-01-06 12:28:03 +0000216 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
217 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 +0000218
Paul Bakker37ca75d2011-01-06 12:28:03 +0000219 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
220 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
221 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
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. */
224 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
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. */
228 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Paul Bakker37ca75d2011-01-06 12:28:03 +0000230 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000231}
232x509_cert;
233
Paul Bakker37ca75d2011-01-06 12:28:03 +0000234/**
235 * Certificate revocation list entry.
236 * Contains the CA-specific serial numbers and revocation dates.
237 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000238typedef struct _x509_crl_entry
239{
240 x509_buf raw;
241
242 x509_buf serial;
243
244 x509_time revocation_date;
245
246 x509_buf entry_ext;
247
248 struct _x509_crl_entry *next;
249}
250x509_crl_entry;
251
Paul Bakker37ca75d2011-01-06 12:28:03 +0000252/**
253 * Certificate revocation list structure.
254 * Every CRL may have multiple entries.
255 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000256typedef struct _x509_crl
257{
Paul Bakker37ca75d2011-01-06 12:28:03 +0000258 x509_buf raw; /**< The raw certificate data (DER). */
259 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000260
261 int version;
262 x509_buf sig_oid1;
263
Paul Bakker37ca75d2011-01-06 12:28:03 +0000264 x509_buf issuer_raw; /**< The raw issuer data (DER). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000265
Paul Bakker37ca75d2011-01-06 12:28:03 +0000266 x509_name issuer; /**< The parsed issuer data (named information object). */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000267
Paul Bakker37ca75d2011-01-06 12:28:03 +0000268 x509_time this_update;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000269 x509_time next_update;
270
Paul Bakker37ca75d2011-01-06 12:28:03 +0000271 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000272
273 x509_buf crl_ext;
274
275 x509_buf sig_oid2;
276 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000277 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000278
279 struct _x509_crl *next;
280}
281x509_crl;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000282/** @} name Structures for parsing X.509 certificates and CRLs */
283/** @} addtogroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000284
Paul Bakker37ca75d2011-01-06 12:28:03 +0000285/**
286 * @name Structures for writing X.509 certificates.
287 * XvP: commented out as they are not used.
288 * - <tt>typedef struct _x509_node x509_node;</tt>
289 * - <tt>typedef struct _x509_raw x509_raw;</tt>
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000291/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000292typedef struct _x509_node
293{
294 unsigned char *data;
295 unsigned char *p;
296 unsigned char *end;
297
298 size_t len;
299}
300x509_node;
301
302typedef struct _x509_raw
303{
304 x509_node raw;
305 x509_node tbs;
306
307 x509_node version;
308 x509_node serial;
309 x509_node tbs_signalg;
310 x509_node issuer;
311 x509_node validity;
312 x509_node subject;
313 x509_node subpubkey;
314
315 x509_node signalg;
316 x509_node sign;
317}
318x509_raw;
Paul Bakker37ca75d2011-01-06 12:28:03 +0000319*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321#ifdef __cplusplus
322extern "C" {
323#endif
324
325/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000326 * @name Functions to read in DHM parameters, a certificate, CRL or private RSA key
Paul Bakker37ca75d2011-01-06 12:28:03 +0000327 * @{
328 */
329
330/** @ingroup x509_module */
331/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 * \brief Parse one or more certificates and add them
333 * to the chained list
334 *
335 * \param chain points to the start of the chain
336 * \param buf buffer holding the certificate data
337 * \param buflen size of the buffer
338 *
339 * \return 0 if successful, or a specific X509 error code
340 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000341int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Paul Bakker37ca75d2011-01-06 12:28:03 +0000343/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000344/**
345 * \brief Load one or more certificates and add them
346 * to the chained list
347 *
348 * \param chain points to the start of the chain
349 * \param path filename to read the certificates from
350 *
351 * \return 0 if successful, or a specific X509 error code
352 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000353int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Paul Bakker37ca75d2011-01-06 12:28:03 +0000355/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000356/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357 * \brief Parse one or more CRLs and add them
358 * to the chained list
359 *
360 * \param chain points to the start of the chain
361 * \param buf buffer holding the CRL data
362 * \param buflen size of the buffer
363 *
364 * \return 0 if successful, or a specific X509 error code
365 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000366int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000367
Paul Bakker37ca75d2011-01-06 12:28:03 +0000368/** @ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000369/**
370 * \brief Load one or more CRLs and add them
371 * to the chained list
372 *
373 * \param chain points to the start of the chain
374 * \param path filename to read the CRLs from
375 *
376 * \return 0 if successful, or a specific X509 error code
377 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000378int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000379
Paul Bakker37ca75d2011-01-06 12:28:03 +0000380/** @ingroup x509_module */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000381/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 * \brief Parse a private RSA key
383 *
384 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000385 * \param key input buffer
386 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 * \param pwd password for decryption (optional)
388 * \param pwdlen size of the password
389 *
390 * \return 0 if successful, or a specific X509 error code
391 */
392int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000393 const unsigned char *key, int keylen,
394 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
Paul Bakker37ca75d2011-01-06 12:28:03 +0000396/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000397/**
398 * \brief Load and parse a private RSA key
399 *
400 * \param rsa RSA context to be initialized
401 * \param path filename to read the private key from
Paul Bakker37ca75d2011-01-06 12:28:03 +0000402 * \param password password to decrypt the file (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 *
404 * \return 0 if successful, or a specific X509 error code
405 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000406int x509parse_keyfile( rsa_context *rsa, const char *path,
407 const char *password );
Paul Bakker1b57b062011-01-06 15:48:19 +0000408
409/** @ingroup x509_module */
410/**
411 * \brief Parse DHM parameters
412 *
413 * \param dhm DHM context to be initialized
414 * \param dhmin input buffer
415 * \param dhminlen size of the buffer
416 *
417 * \return 0 if successful, or a specific X509 error code
418 */
419int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, int dhminlen );
420
421/** @ingroup x509_module */
422/**
423 * \brief Load and parse DHM parameters
424 *
425 * \param dhm DHM context to be initialized
426 * \param path filename to read the DHM Parameters from
427 *
428 * \return 0 if successful, or a specific X509 error code
429 */
430int x509parse_dhmfile( dhm_context *rsa, const char *path );
431
432/** @} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000433
434
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436/**
437 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000438 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000439 *
440 * \param buf Buffer to write to
441 * \param size Maximum size of buffer
442 * \param dn The X509 name to represent
443 *
444 * \return The amount of data written to the buffer, or -1 in
445 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000447int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449/**
450 * \brief Returns an informational string about the
451 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000452 *
453 * \param buf Buffer to write to
454 * \param size Maximum size of buffer
455 * \param prefix A line prefix
456 * \param crt The X509 certificate to represent
457 *
458 * \return The amount of data written to the buffer, or -1 in
459 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000461int x509parse_cert_info( char *buf, size_t size, const char *prefix,
462 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000463
464/**
465 * \brief Returns an informational string about the
466 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000467 *
468 * \param buf Buffer to write to
469 * \param size Maximum size of buffer
470 * \param prefix A line prefix
Paul Bakker37ca75d2011-01-06 12:28:03 +0000471 * \param crl The X509 CRL to represent
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000472 *
473 * \return The amount of data written to the buffer, or -1 in
474 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000475 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000476int x509parse_crl_info( char *buf, size_t size, const char *prefix,
477 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000480 * \brief Check a given x509_time against the system time and check
481 * if it is valid.
482 *
483 * \param time x509_time to check
484 *
485 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000486 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000488int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/**
Paul Bakker37ca75d2011-01-06 12:28:03 +0000491 * @name Functions to verify a certificate
492 * @{
493 */
494/** @ingroup x509_module */
495/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 * \brief Verify the certificate signature
497 *
498 * \param crt a certificate to be verified
499 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000500 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 * \param cn expected Common Name (can be set to
502 * NULL if the CN must not be verified)
503 * \param flags result of the verification
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000504 * \param f_vrfy verification function
505 * \param p_vrfy verification parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 *
Paul Bakker40e46942009-01-03 21:51:57 +0000507 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 * in which case *flags will have one or more of
509 * the following values set:
510 * BADCERT_EXPIRED --
511 * BADCERT_REVOKED --
512 * BADCERT_CN_MISMATCH --
513 * BADCERT_NOT_TRUSTED
514 *
515 * \note TODO: add two arguments, depth and crl
516 */
517int x509parse_verify( x509_cert *crt,
518 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000519 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000520 const char *cn, int *flags,
521 int (*f_vrfy)(void *, x509_cert *, int, int),
522 void *p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Paul Bakker37ca75d2011-01-06 12:28:03 +0000524/** @} name Functions to verify a certificate */
525
526
527
528/**
529 * @name Functions to clear a certificate, CRL or private RSA key
530 * @{
531 */
532/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000533/**
534 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000535 *
536 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
538void x509_free( x509_cert *crt );
539
Paul Bakker37ca75d2011-01-06 12:28:03 +0000540/** @ingroup x509_module */
Paul Bakker5121ce52009-01-03 21:22:43 +0000541/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000542 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000543 *
Paul Bakker37ca75d2011-01-06 12:28:03 +0000544 * \param crl CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000545 */
546void x509_crl_free( x509_crl *crl );
547
Paul Bakker37ca75d2011-01-06 12:28:03 +0000548/** @} name Functions to clear a certificate, CRL or private RSA key */
549
550
Paul Bakkerd98030e2009-05-02 15:13:40 +0000551/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 * \brief Checkup routine
553 *
554 * \return 0 if successful, or 1 if the test failed
555 */
556int x509_self_test( int verbose );
557
558#ifdef __cplusplus
559}
560#endif
561
562#endif /* x509.h */