blob: 829adaf2a4e0b2a9723ae86557bb1f64c201d941 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_X509_H
26#define POLARSSL_X509_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker8e831ed2009-01-03 21:24:11 +000028#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker13e2dfe2009-07-28 07:18:38 +000030/*
31 * ASN1 Error codes
32 *
33 * These error codes will be OR'ed to X509 error codes for
34 * higher error granularity.
35 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000036#define POLARSSL_ERR_ASN1_OUT_OF_DATA 0x0014
37#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG 0x0016
38#define POLARSSL_ERR_ASN1_INVALID_LENGTH 0x0018
39#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH 0x001A
40#define POLARSSL_ERR_ASN1_INVALID_DATA 0x001C
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker13e2dfe2009-07-28 07:18:38 +000042/*
43 * X509 Error codes
44 */
Paul Bakker3391b122009-07-28 20:11:54 +000045#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020
46#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040
47#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060
48#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080
49#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0
50#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0
51#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0
52#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100
53#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120
54#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140
55#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160
56#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180
57#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0
58#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0
59#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0
60#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200
61#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220
62#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240
63#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260
64#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280
65#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0
66#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0
67#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0
68#define POLARSSL_ERR_X509_POINT_ERROR -0x0300
69#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker13e2dfe2009-07-28 07:18:38 +000071/*
72 * X509 Verify codes
73 */
Paul Bakker5121ce52009-01-03 21:22:43 +000074#define BADCERT_EXPIRED 1
75#define BADCERT_REVOKED 2
76#define BADCERT_CN_MISMATCH 4
77#define BADCERT_NOT_TRUSTED 8
Paul Bakker40ea7de2009-05-03 10:18:48 +000078#define BADCRL_NOT_TRUSTED 16
79#define BADCRL_EXPIRED 32
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/*
82 * DER constants
83 */
84#define ASN1_BOOLEAN 0x01
85#define ASN1_INTEGER 0x02
86#define ASN1_BIT_STRING 0x03
87#define ASN1_OCTET_STRING 0x04
88#define ASN1_NULL 0x05
89#define ASN1_OID 0x06
90#define ASN1_UTF8_STRING 0x0C
91#define ASN1_SEQUENCE 0x10
92#define ASN1_SET 0x11
93#define ASN1_PRINTABLE_STRING 0x13
94#define ASN1_T61_STRING 0x14
95#define ASN1_IA5_STRING 0x16
96#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +000097#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +000098#define ASN1_UNIVERSAL_STRING 0x1C
99#define ASN1_BMP_STRING 0x1E
100#define ASN1_PRIMITIVE 0x00
101#define ASN1_CONSTRUCTED 0x20
102#define ASN1_CONTEXT_SPECIFIC 0x80
103
104/*
105 * various object identifiers
106 */
107#define X520_COMMON_NAME 3
108#define X520_COUNTRY 6
109#define X520_LOCALITY 7
110#define X520_STATE 8
111#define X520_ORGANIZATION 10
112#define X520_ORG_UNIT 11
113#define PKCS9_EMAIL 1
114
115#define X509_OUTPUT_DER 0x01
116#define X509_OUTPUT_PEM 0x02
117#define PEM_LINE_LENGTH 72
118#define X509_ISSUER 0x01
119#define X509_SUBJECT 0x02
120
121#define OID_X520 "\x55\x04"
122#define OID_CN "\x55\x04\x03"
123#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
124#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
125#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
126#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
127#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
128
129/*
130 * Structures for parsing X.509 certificates
131 */
132typedef struct _x509_buf
133{
134 int tag;
135 int len;
136 unsigned char *p;
137}
138x509_buf;
139
140typedef struct _x509_name
141{
142 x509_buf oid;
143 x509_buf val;
144 struct _x509_name *next;
145}
146x509_name;
147
148typedef struct _x509_time
149{
150 int year, mon, day;
151 int hour, min, sec;
152}
153x509_time;
154
155typedef struct _x509_cert
156{
157 x509_buf raw;
158 x509_buf tbs;
159
160 int version;
161 x509_buf serial;
162 x509_buf sig_oid1;
163
164 x509_buf issuer_raw;
165 x509_buf subject_raw;
166
167 x509_name issuer;
168 x509_name subject;
169
170 x509_time valid_from;
171 x509_time valid_to;
172
173 x509_buf pk_oid;
174 rsa_context rsa;
175
176 x509_buf issuer_id;
177 x509_buf subject_id;
178 x509_buf v3_ext;
179
180 int ca_istrue;
181 int max_pathlen;
182
183 x509_buf sig_oid2;
184 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000185 int sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187 struct _x509_cert *next;
188}
189x509_cert;
190
Paul Bakkerd98030e2009-05-02 15:13:40 +0000191typedef struct _x509_crl_entry
192{
193 x509_buf raw;
194
195 x509_buf serial;
196
197 x509_time revocation_date;
198
199 x509_buf entry_ext;
200
201 struct _x509_crl_entry *next;
202}
203x509_crl_entry;
204
205typedef struct _x509_crl
206{
207 x509_buf raw;
208 x509_buf tbs;
209
210 int version;
211 x509_buf sig_oid1;
212
213 x509_buf issuer_raw;
214
215 x509_name issuer;
216
217 x509_time this_update;
218 x509_time next_update;
219
220 x509_crl_entry entry;
221
222 x509_buf crl_ext;
223
224 x509_buf sig_oid2;
225 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000226 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000227
228 struct _x509_crl *next;
229}
230x509_crl;
231
Paul Bakker5121ce52009-01-03 21:22:43 +0000232/*
233 * Structures for writing X.509 certificates
234 */
235typedef struct _x509_node
236{
237 unsigned char *data;
238 unsigned char *p;
239 unsigned char *end;
240
241 size_t len;
242}
243x509_node;
244
245typedef struct _x509_raw
246{
247 x509_node raw;
248 x509_node tbs;
249
250 x509_node version;
251 x509_node serial;
252 x509_node tbs_signalg;
253 x509_node issuer;
254 x509_node validity;
255 x509_node subject;
256 x509_node subpubkey;
257
258 x509_node signalg;
259 x509_node sign;
260}
261x509_raw;
262
263#ifdef __cplusplus
264extern "C" {
265#endif
266
267/**
268 * \brief Parse one or more certificates and add them
269 * to the chained list
270 *
271 * \param chain points to the start of the chain
272 * \param buf buffer holding the certificate data
273 * \param buflen size of the buffer
274 *
275 * \return 0 if successful, or a specific X509 error code
276 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000277int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
279/**
280 * \brief Load one or more certificates and add them
281 * to the chained list
282 *
283 * \param chain points to the start of the chain
284 * \param path filename to read the certificates from
285 *
286 * \return 0 if successful, or a specific X509 error code
287 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000288int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000291 * \brief Parse one or more CRLs and add them
292 * to the chained list
293 *
294 * \param chain points to the start of the chain
295 * \param buf buffer holding the CRL data
296 * \param buflen size of the buffer
297 *
298 * \return 0 if successful, or a specific X509 error code
299 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000300int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000301
302/**
303 * \brief Load one or more CRLs and add them
304 * to the chained list
305 *
306 * \param chain points to the start of the chain
307 * \param path filename to read the CRLs from
308 *
309 * \return 0 if successful, or a specific X509 error code
310 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000311int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000312
313/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * \brief Parse a private RSA key
315 *
316 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000317 * \param key input buffer
318 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * \param pwd password for decryption (optional)
320 * \param pwdlen size of the password
321 *
322 * \return 0 if successful, or a specific X509 error code
323 */
324int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000325 const unsigned char *key, int keylen,
326 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328/**
329 * \brief Load and parse a private RSA key
330 *
331 * \param rsa RSA context to be initialized
332 * \param path filename to read the private key from
333 * \param pwd password to decrypt the file (can be NULL)
334 *
335 * \return 0 if successful, or a specific X509 error code
336 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000337int x509parse_keyfile( rsa_context *rsa, const char *path,
338 const char *password );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340/**
341 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000343 *
344 * \param buf Buffer to write to
345 * \param size Maximum size of buffer
346 * \param dn The X509 name to represent
347 *
348 * \return The amount of data written to the buffer, or -1 in
349 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000351int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
354 * \brief Returns an informational string about the
355 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 *
357 * \param buf Buffer to write to
358 * \param size Maximum size of buffer
359 * \param prefix A line prefix
360 * \param crt The X509 certificate to represent
361 *
362 * \return The amount of data written to the buffer, or -1 in
363 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000365int x509parse_cert_info( char *buf, size_t size, const char *prefix,
366 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000367
368/**
369 * \brief Returns an informational string about the
370 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000371 *
372 * \param buf Buffer to write to
373 * \param size Maximum size of buffer
374 * \param prefix A line prefix
375 * \param crt The X509 CRL to represent
376 *
377 * \return The amount of data written to the buffer, or -1 in
378 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000379 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000380int x509parse_crl_info( char *buf, size_t size, const char *prefix,
381 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
383/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000384 * \brief Check a given x509_time against the system time and check
385 * if it is valid.
386 *
387 * \param time x509_time to check
388 *
389 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000390 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000392int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394/**
395 * \brief Verify the certificate signature
396 *
397 * \param crt a certificate to be verified
398 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000399 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 * \param cn expected Common Name (can be set to
401 * NULL if the CN must not be verified)
402 * \param flags result of the verification
403 *
Paul Bakker40e46942009-01-03 21:51:57 +0000404 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 * in which case *flags will have one or more of
406 * the following values set:
407 * BADCERT_EXPIRED --
408 * BADCERT_REVOKED --
409 * BADCERT_CN_MISMATCH --
410 * BADCERT_NOT_TRUSTED
411 *
412 * \note TODO: add two arguments, depth and crl
413 */
414int x509parse_verify( x509_cert *crt,
415 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000416 x509_crl *ca_crl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000417 const char *cn, int *flags );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/**
420 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000421 *
422 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 */
424void x509_free( x509_cert *crt );
425
426/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000428 *
429 * \param crt CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000430 */
431void x509_crl_free( x509_crl *crl );
432
433/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 * \brief Checkup routine
435 *
436 * \return 0 if successful, or 1 if the test failed
437 */
438int x509_self_test( int verbose );
439
440#ifdef __cplusplus
441}
442#endif
443
444#endif /* x509.h */