blob: c5bd566640d997dc442dfbdf93253477f5518811 [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.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_X509_H
23#define POLARSSL_X509_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker8e831ed2009-01-03 21:24:11 +000025#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker13e2dfe2009-07-28 07:18:38 +000027/*
28 * ASN1 Error codes
29 *
30 * These error codes will be OR'ed to X509 error codes for
31 * higher error granularity.
32 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000033#define POLARSSL_ERR_ASN1_OUT_OF_DATA 0x0014
34#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG 0x0016
35#define POLARSSL_ERR_ASN1_INVALID_LENGTH 0x0018
36#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH 0x001A
37#define POLARSSL_ERR_ASN1_INVALID_DATA 0x001C
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * X509 Error codes
41 */
Paul Bakker3391b122009-07-28 20:11:54 +000042#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020
43#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040
44#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060
45#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080
46#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0
47#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0
48#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0
49#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100
50#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120
51#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140
52#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160
53#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180
54#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0
55#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0
56#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0
57#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200
58#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220
59#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240
60#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260
61#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280
62#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0
63#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0
64#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0
65#define POLARSSL_ERR_X509_POINT_ERROR -0x0300
66#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker13e2dfe2009-07-28 07:18:38 +000068/*
69 * X509 Verify codes
70 */
Paul Bakker5121ce52009-01-03 21:22:43 +000071#define BADCERT_EXPIRED 1
72#define BADCERT_REVOKED 2
73#define BADCERT_CN_MISMATCH 4
74#define BADCERT_NOT_TRUSTED 8
Paul Bakker40ea7de2009-05-03 10:18:48 +000075#define BADCRL_NOT_TRUSTED 16
76#define BADCRL_EXPIRED 32
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/*
79 * DER constants
80 */
81#define ASN1_BOOLEAN 0x01
82#define ASN1_INTEGER 0x02
83#define ASN1_BIT_STRING 0x03
84#define ASN1_OCTET_STRING 0x04
85#define ASN1_NULL 0x05
86#define ASN1_OID 0x06
87#define ASN1_UTF8_STRING 0x0C
88#define ASN1_SEQUENCE 0x10
89#define ASN1_SET 0x11
90#define ASN1_PRINTABLE_STRING 0x13
91#define ASN1_T61_STRING 0x14
92#define ASN1_IA5_STRING 0x16
93#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +000094#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +000095#define ASN1_UNIVERSAL_STRING 0x1C
96#define ASN1_BMP_STRING 0x1E
97#define ASN1_PRIMITIVE 0x00
98#define ASN1_CONSTRUCTED 0x20
99#define ASN1_CONTEXT_SPECIFIC 0x80
100
101/*
102 * various object identifiers
103 */
104#define X520_COMMON_NAME 3
105#define X520_COUNTRY 6
106#define X520_LOCALITY 7
107#define X520_STATE 8
108#define X520_ORGANIZATION 10
109#define X520_ORG_UNIT 11
110#define PKCS9_EMAIL 1
111
112#define X509_OUTPUT_DER 0x01
113#define X509_OUTPUT_PEM 0x02
114#define PEM_LINE_LENGTH 72
115#define X509_ISSUER 0x01
116#define X509_SUBJECT 0x02
117
118#define OID_X520 "\x55\x04"
119#define OID_CN "\x55\x04\x03"
120#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
121#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
122#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
123#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
124#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
125
126/*
127 * Structures for parsing X.509 certificates
128 */
129typedef struct _x509_buf
130{
131 int tag;
132 int len;
133 unsigned char *p;
134}
135x509_buf;
136
137typedef struct _x509_name
138{
139 x509_buf oid;
140 x509_buf val;
141 struct _x509_name *next;
142}
143x509_name;
144
145typedef struct _x509_time
146{
147 int year, mon, day;
148 int hour, min, sec;
149}
150x509_time;
151
152typedef struct _x509_cert
153{
154 x509_buf raw;
155 x509_buf tbs;
156
157 int version;
158 x509_buf serial;
159 x509_buf sig_oid1;
160
161 x509_buf issuer_raw;
162 x509_buf subject_raw;
163
164 x509_name issuer;
165 x509_name subject;
166
167 x509_time valid_from;
168 x509_time valid_to;
169
170 x509_buf pk_oid;
171 rsa_context rsa;
172
173 x509_buf issuer_id;
174 x509_buf subject_id;
175 x509_buf v3_ext;
176
177 int ca_istrue;
178 int max_pathlen;
179
180 x509_buf sig_oid2;
181 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000182 int sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 struct _x509_cert *next;
185}
186x509_cert;
187
Paul Bakkerd98030e2009-05-02 15:13:40 +0000188typedef struct _x509_crl_entry
189{
190 x509_buf raw;
191
192 x509_buf serial;
193
194 x509_time revocation_date;
195
196 x509_buf entry_ext;
197
198 struct _x509_crl_entry *next;
199}
200x509_crl_entry;
201
202typedef struct _x509_crl
203{
204 x509_buf raw;
205 x509_buf tbs;
206
207 int version;
208 x509_buf sig_oid1;
209
210 x509_buf issuer_raw;
211
212 x509_name issuer;
213
214 x509_time this_update;
215 x509_time next_update;
216
217 x509_crl_entry entry;
218
219 x509_buf crl_ext;
220
221 x509_buf sig_oid2;
222 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000223 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000224
225 struct _x509_crl *next;
226}
227x509_crl;
228
Paul Bakker5121ce52009-01-03 21:22:43 +0000229/*
230 * Structures for writing X.509 certificates
231 */
232typedef struct _x509_node
233{
234 unsigned char *data;
235 unsigned char *p;
236 unsigned char *end;
237
238 size_t len;
239}
240x509_node;
241
242typedef struct _x509_raw
243{
244 x509_node raw;
245 x509_node tbs;
246
247 x509_node version;
248 x509_node serial;
249 x509_node tbs_signalg;
250 x509_node issuer;
251 x509_node validity;
252 x509_node subject;
253 x509_node subpubkey;
254
255 x509_node signalg;
256 x509_node sign;
257}
258x509_raw;
259
260#ifdef __cplusplus
261extern "C" {
262#endif
263
264/**
265 * \brief Parse one or more certificates and add them
266 * to the chained list
267 *
268 * \param chain points to the start of the chain
269 * \param buf buffer holding the certificate data
270 * \param buflen size of the buffer
271 *
272 * \return 0 if successful, or a specific X509 error code
273 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000274int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276/**
277 * \brief Load one or more certificates and add them
278 * to the chained list
279 *
280 * \param chain points to the start of the chain
281 * \param path filename to read the certificates from
282 *
283 * \return 0 if successful, or a specific X509 error code
284 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000285int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000288 * \brief Parse one or more CRLs and add them
289 * to the chained list
290 *
291 * \param chain points to the start of the chain
292 * \param buf buffer holding the CRL data
293 * \param buflen size of the buffer
294 *
295 * \return 0 if successful, or a specific X509 error code
296 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000297int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000298
299/**
300 * \brief Load one or more CRLs and add them
301 * to the chained list
302 *
303 * \param chain points to the start of the chain
304 * \param path filename to read the CRLs from
305 *
306 * \return 0 if successful, or a specific X509 error code
307 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000308int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000309
310/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \brief Parse a private RSA key
312 *
313 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000314 * \param key input buffer
315 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 * \param pwd password for decryption (optional)
317 * \param pwdlen size of the password
318 *
319 * \return 0 if successful, or a specific X509 error code
320 */
321int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000322 const unsigned char *key, int keylen,
323 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325/**
326 * \brief Load and parse a private RSA key
327 *
328 * \param rsa RSA context to be initialized
329 * \param path filename to read the private key from
330 * \param pwd password to decrypt the file (can be NULL)
331 *
332 * \return 0 if successful, or a specific X509 error code
333 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000334int x509parse_keyfile( rsa_context *rsa, const char *path,
335 const char *password );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337/**
338 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000339 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000340 *
341 * \param buf Buffer to write to
342 * \param size Maximum size of buffer
343 * \param dn The X509 name to represent
344 *
345 * \return The amount of data written to the buffer, or -1 in
346 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000348int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350/**
351 * \brief Returns an informational string about the
352 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000353 *
354 * \param buf Buffer to write to
355 * \param size Maximum size of buffer
356 * \param prefix A line prefix
357 * \param crt The X509 certificate to represent
358 *
359 * \return The amount of data written to the buffer, or -1 in
360 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362int x509parse_cert_info( char *buf, size_t size, const char *prefix,
363 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000364
365/**
366 * \brief Returns an informational string about the
367 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000368 *
369 * \param buf Buffer to write to
370 * \param size Maximum size of buffer
371 * \param prefix A line prefix
372 * \param crt The X509 CRL to represent
373 *
374 * \return The amount of data written to the buffer, or -1 in
375 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000376 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377int x509parse_crl_info( char *buf, size_t size, const char *prefix,
378 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \brief Check a given x509_time against the system time and check
382 * if it is valid.
383 *
384 * \param time x509_time to check
385 *
386 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000387 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000389int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391/**
392 * \brief Verify the certificate signature
393 *
394 * \param crt a certificate to be verified
395 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000396 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 * \param cn expected Common Name (can be set to
398 * NULL if the CN must not be verified)
399 * \param flags result of the verification
400 *
Paul Bakker40e46942009-01-03 21:51:57 +0000401 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 * in which case *flags will have one or more of
403 * the following values set:
404 * BADCERT_EXPIRED --
405 * BADCERT_REVOKED --
406 * BADCERT_CN_MISMATCH --
407 * BADCERT_NOT_TRUSTED
408 *
409 * \note TODO: add two arguments, depth and crl
410 */
411int x509parse_verify( x509_cert *crt,
412 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000413 x509_crl *ca_crl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000414 const char *cn, int *flags );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416/**
417 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000418 *
419 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
421void x509_free( x509_cert *crt );
422
423/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000425 *
426 * \param crt CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427 */
428void x509_crl_free( x509_crl *crl );
429
430/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 * \brief Checkup routine
432 *
433 * \return 0 if successful, or 1 if the test failed
434 */
435int x509_self_test( int verbose );
436
437#ifdef __cplusplus
438}
439#endif
440
441#endif /* x509.h */