blob: 06b979fffe94839f8da4d44f3567987a72d9c64d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_X509_H
24#define POLARSSL_X509_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker8e831ed2009-01-03 21:24:11 +000026#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker13e2dfe2009-07-28 07:18:38 +000028/*
29 * ASN1 Error codes
30 *
31 * These error codes will be OR'ed to X509 error codes for
32 * higher error granularity.
33 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000034#define POLARSSL_ERR_ASN1_OUT_OF_DATA 0x0014
35#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG 0x0016
36#define POLARSSL_ERR_ASN1_INVALID_LENGTH 0x0018
37#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH 0x001A
38#define POLARSSL_ERR_ASN1_INVALID_DATA 0x001C
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker13e2dfe2009-07-28 07:18:38 +000040/*
41 * X509 Error codes
42 */
Paul Bakker3391b122009-07-28 20:11:54 +000043#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020
44#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040
45#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060
46#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080
47#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0
48#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0
49#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0
50#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100
51#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120
52#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140
53#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160
54#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180
55#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0
56#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0
57#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0
58#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200
59#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220
60#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240
61#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260
62#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280
63#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0
64#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0
65#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0
66#define POLARSSL_ERR_X509_POINT_ERROR -0x0300
67#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Paul Bakker13e2dfe2009-07-28 07:18:38 +000069/*
70 * X509 Verify codes
71 */
Paul Bakker5121ce52009-01-03 21:22:43 +000072#define BADCERT_EXPIRED 1
73#define BADCERT_REVOKED 2
74#define BADCERT_CN_MISMATCH 4
75#define BADCERT_NOT_TRUSTED 8
Paul Bakker40ea7de2009-05-03 10:18:48 +000076#define BADCRL_NOT_TRUSTED 16
77#define BADCRL_EXPIRED 32
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79/*
80 * DER constants
81 */
82#define ASN1_BOOLEAN 0x01
83#define ASN1_INTEGER 0x02
84#define ASN1_BIT_STRING 0x03
85#define ASN1_OCTET_STRING 0x04
86#define ASN1_NULL 0x05
87#define ASN1_OID 0x06
88#define ASN1_UTF8_STRING 0x0C
89#define ASN1_SEQUENCE 0x10
90#define ASN1_SET 0x11
91#define ASN1_PRINTABLE_STRING 0x13
92#define ASN1_T61_STRING 0x14
93#define ASN1_IA5_STRING 0x16
94#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +000095#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +000096#define ASN1_UNIVERSAL_STRING 0x1C
97#define ASN1_BMP_STRING 0x1E
98#define ASN1_PRIMITIVE 0x00
99#define ASN1_CONSTRUCTED 0x20
100#define ASN1_CONTEXT_SPECIFIC 0x80
101
102/*
103 * various object identifiers
104 */
105#define X520_COMMON_NAME 3
106#define X520_COUNTRY 6
107#define X520_LOCALITY 7
108#define X520_STATE 8
109#define X520_ORGANIZATION 10
110#define X520_ORG_UNIT 11
111#define PKCS9_EMAIL 1
112
113#define X509_OUTPUT_DER 0x01
114#define X509_OUTPUT_PEM 0x02
115#define PEM_LINE_LENGTH 72
116#define X509_ISSUER 0x01
117#define X509_SUBJECT 0x02
118
119#define OID_X520 "\x55\x04"
120#define OID_CN "\x55\x04\x03"
121#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
122#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
123#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
124#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
125#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
126
127/*
128 * Structures for parsing X.509 certificates
129 */
130typedef struct _x509_buf
131{
132 int tag;
133 int len;
134 unsigned char *p;
135}
136x509_buf;
137
138typedef struct _x509_name
139{
140 x509_buf oid;
141 x509_buf val;
142 struct _x509_name *next;
143}
144x509_name;
145
146typedef struct _x509_time
147{
148 int year, mon, day;
149 int hour, min, sec;
150}
151x509_time;
152
153typedef struct _x509_cert
154{
155 x509_buf raw;
156 x509_buf tbs;
157
158 int version;
159 x509_buf serial;
160 x509_buf sig_oid1;
161
162 x509_buf issuer_raw;
163 x509_buf subject_raw;
164
165 x509_name issuer;
166 x509_name subject;
167
168 x509_time valid_from;
169 x509_time valid_to;
170
171 x509_buf pk_oid;
172 rsa_context rsa;
173
174 x509_buf issuer_id;
175 x509_buf subject_id;
176 x509_buf v3_ext;
177
178 int ca_istrue;
179 int max_pathlen;
180
181 x509_buf sig_oid2;
182 x509_buf sig;
183
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;
223
224 struct _x509_crl *next;
225}
226x509_crl;
227
Paul Bakker5121ce52009-01-03 21:22:43 +0000228/*
229 * Structures for writing X.509 certificates
230 */
231typedef struct _x509_node
232{
233 unsigned char *data;
234 unsigned char *p;
235 unsigned char *end;
236
237 size_t len;
238}
239x509_node;
240
241typedef struct _x509_raw
242{
243 x509_node raw;
244 x509_node tbs;
245
246 x509_node version;
247 x509_node serial;
248 x509_node tbs_signalg;
249 x509_node issuer;
250 x509_node validity;
251 x509_node subject;
252 x509_node subpubkey;
253
254 x509_node signalg;
255 x509_node sign;
256}
257x509_raw;
258
259#ifdef __cplusplus
260extern "C" {
261#endif
262
263/**
264 * \brief Parse one or more certificates and add them
265 * to the chained list
266 *
267 * \param chain points to the start of the chain
268 * \param buf buffer holding the certificate data
269 * \param buflen size of the buffer
270 *
271 * \return 0 if successful, or a specific X509 error code
272 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000273int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275/**
276 * \brief Load one or more certificates and add them
277 * to the chained list
278 *
279 * \param chain points to the start of the chain
280 * \param path filename to read the certificates from
281 *
282 * \return 0 if successful, or a specific X509 error code
283 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000284int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000287 * \brief Parse one or more CRLs and add them
288 * to the chained list
289 *
290 * \param chain points to the start of the chain
291 * \param buf buffer holding the CRL data
292 * \param buflen size of the buffer
293 *
294 * \return 0 if successful, or a specific X509 error code
295 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000296int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000297
298/**
299 * \brief Load one or more CRLs and add them
300 * to the chained list
301 *
302 * \param chain points to the start of the chain
303 * \param path filename to read the CRLs from
304 *
305 * \return 0 if successful, or a specific X509 error code
306 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000307int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000308
309/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 * \brief Parse a private RSA key
311 *
312 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000313 * \param key input buffer
314 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 * \param pwd password for decryption (optional)
316 * \param pwdlen size of the password
317 *
318 * \return 0 if successful, or a specific X509 error code
319 */
320int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000321 const unsigned char *key, int keylen,
322 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324/**
325 * \brief Load and parse a private RSA key
326 *
327 * \param rsa RSA context to be initialized
328 * \param path filename to read the private key from
329 * \param pwd password to decrypt the file (can be NULL)
330 *
331 * \return 0 if successful, or a specific X509 error code
332 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333int x509parse_keyfile( rsa_context *rsa, const char *path,
334 const char *password );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/**
337 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 *
340 * \param buf Buffer to write to
341 * \param size Maximum size of buffer
342 * \param dn The X509 name to represent
343 *
344 * \return The amount of data written to the buffer, or -1 in
345 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
349/**
350 * \brief Returns an informational string about the
351 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000352 *
353 * \param buf Buffer to write to
354 * \param size Maximum size of buffer
355 * \param prefix A line prefix
356 * \param crt The X509 certificate to represent
357 *
358 * \return The amount of data written to the buffer, or -1 in
359 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000361int x509parse_cert_info( char *buf, size_t size, const char *prefix,
362 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000363
364/**
365 * \brief Returns an informational string about the
366 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 *
368 * \param buf Buffer to write to
369 * \param size Maximum size of buffer
370 * \param prefix A line prefix
371 * \param crt The X509 CRL to represent
372 *
373 * \return The amount of data written to the buffer, or -1 in
374 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000375 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000376int x509parse_crl_info( char *buf, size_t size, const char *prefix,
377 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000380 * \brief Check a given x509_time against the system time and check
381 * if it is valid.
382 *
383 * \param time x509_time to check
384 *
385 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000386 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000388int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390/**
391 * \brief Verify the certificate signature
392 *
393 * \param crt a certificate to be verified
394 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000395 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 * \param cn expected Common Name (can be set to
397 * NULL if the CN must not be verified)
398 * \param flags result of the verification
399 *
Paul Bakker40e46942009-01-03 21:51:57 +0000400 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 * in which case *flags will have one or more of
402 * the following values set:
403 * BADCERT_EXPIRED --
404 * BADCERT_REVOKED --
405 * BADCERT_CN_MISMATCH --
406 * BADCERT_NOT_TRUSTED
407 *
408 * \note TODO: add two arguments, depth and crl
409 */
410int x509parse_verify( x509_cert *crt,
411 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000412 x509_crl *ca_crl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000413 const char *cn, int *flags );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415/**
416 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000417 *
418 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 */
420void x509_free( x509_cert *crt );
421
422/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000423 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000424 *
425 * \param crt CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000426 */
427void x509_crl_free( x509_crl *crl );
428
429/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 * \brief Checkup routine
431 *
432 * \return 0 if successful, or 1 if the test failed
433 */
434int x509_self_test( int verbose );
435
436#ifdef __cplusplus
437}
438#endif
439
440#endif /* x509.h */