blob: 31b112287810bcfcc4aac856aec852a064f26431 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerfc8c4362010-03-21 17:37:16 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_X509_H
22#define POLARSSL_X509_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Paul Bakker8e831ed2009-01-03 21:24:11 +000024#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker13e2dfe2009-07-28 07:18:38 +000026/*
27 * ASN1 Error codes
28 *
29 * These error codes will be OR'ed to X509 error codes for
30 * higher error granularity.
31 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000032#define POLARSSL_ERR_ASN1_OUT_OF_DATA 0x0014
33#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG 0x0016
34#define POLARSSL_ERR_ASN1_INVALID_LENGTH 0x0018
35#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH 0x001A
36#define POLARSSL_ERR_ASN1_INVALID_DATA 0x001C
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker13e2dfe2009-07-28 07:18:38 +000038/*
39 * X509 Error codes
40 */
Paul Bakker3391b122009-07-28 20:11:54 +000041#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020
42#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040
43#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060
44#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080
45#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0
46#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0
47#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0
48#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100
49#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120
50#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140
51#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160
52#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180
53#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0
54#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0
55#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0
56#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200
57#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220
58#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240
59#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260
60#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280
61#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0
62#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0
63#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0
64#define POLARSSL_ERR_X509_POINT_ERROR -0x0300
65#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakker13e2dfe2009-07-28 07:18:38 +000067/*
68 * X509 Verify codes
69 */
Paul Bakker5121ce52009-01-03 21:22:43 +000070#define BADCERT_EXPIRED 1
71#define BADCERT_REVOKED 2
72#define BADCERT_CN_MISMATCH 4
73#define BADCERT_NOT_TRUSTED 8
Paul Bakker40ea7de2009-05-03 10:18:48 +000074#define BADCRL_NOT_TRUSTED 16
75#define BADCRL_EXPIRED 32
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77/*
78 * DER constants
79 */
80#define ASN1_BOOLEAN 0x01
81#define ASN1_INTEGER 0x02
82#define ASN1_BIT_STRING 0x03
83#define ASN1_OCTET_STRING 0x04
84#define ASN1_NULL 0x05
85#define ASN1_OID 0x06
86#define ASN1_UTF8_STRING 0x0C
87#define ASN1_SEQUENCE 0x10
88#define ASN1_SET 0x11
89#define ASN1_PRINTABLE_STRING 0x13
90#define ASN1_T61_STRING 0x14
91#define ASN1_IA5_STRING 0x16
92#define ASN1_UTC_TIME 0x17
Paul Bakker91200182010-02-18 21:26:15 +000093#define ASN1_GENERALIZED_TIME 0x18
Paul Bakker5121ce52009-01-03 21:22:43 +000094#define ASN1_UNIVERSAL_STRING 0x1C
95#define ASN1_BMP_STRING 0x1E
96#define ASN1_PRIMITIVE 0x00
97#define ASN1_CONSTRUCTED 0x20
98#define ASN1_CONTEXT_SPECIFIC 0x80
99
100/*
101 * various object identifiers
102 */
103#define X520_COMMON_NAME 3
104#define X520_COUNTRY 6
105#define X520_LOCALITY 7
106#define X520_STATE 8
107#define X520_ORGANIZATION 10
108#define X520_ORG_UNIT 11
109#define PKCS9_EMAIL 1
110
111#define X509_OUTPUT_DER 0x01
112#define X509_OUTPUT_PEM 0x02
113#define PEM_LINE_LENGTH 72
114#define X509_ISSUER 0x01
115#define X509_SUBJECT 0x02
116
117#define OID_X520 "\x55\x04"
118#define OID_CN "\x55\x04\x03"
119#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
120#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
121#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
122#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
123#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
124
125/*
126 * Structures for parsing X.509 certificates
127 */
128typedef struct _x509_buf
129{
130 int tag;
131 int len;
132 unsigned char *p;
133}
134x509_buf;
135
136typedef struct _x509_name
137{
138 x509_buf oid;
139 x509_buf val;
140 struct _x509_name *next;
141}
142x509_name;
143
144typedef struct _x509_time
145{
146 int year, mon, day;
147 int hour, min, sec;
148}
149x509_time;
150
151typedef struct _x509_cert
152{
153 x509_buf raw;
154 x509_buf tbs;
155
156 int version;
157 x509_buf serial;
158 x509_buf sig_oid1;
159
160 x509_buf issuer_raw;
161 x509_buf subject_raw;
162
163 x509_name issuer;
164 x509_name subject;
165
166 x509_time valid_from;
167 x509_time valid_to;
168
169 x509_buf pk_oid;
170 rsa_context rsa;
171
172 x509_buf issuer_id;
173 x509_buf subject_id;
174 x509_buf v3_ext;
175
176 int ca_istrue;
177 int max_pathlen;
178
179 x509_buf sig_oid2;
180 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000181 int sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 struct _x509_cert *next;
184}
185x509_cert;
186
Paul Bakkerd98030e2009-05-02 15:13:40 +0000187typedef struct _x509_crl_entry
188{
189 x509_buf raw;
190
191 x509_buf serial;
192
193 x509_time revocation_date;
194
195 x509_buf entry_ext;
196
197 struct _x509_crl_entry *next;
198}
199x509_crl_entry;
200
201typedef struct _x509_crl
202{
203 x509_buf raw;
204 x509_buf tbs;
205
206 int version;
207 x509_buf sig_oid1;
208
209 x509_buf issuer_raw;
210
211 x509_name issuer;
212
213 x509_time this_update;
214 x509_time next_update;
215
216 x509_crl_entry entry;
217
218 x509_buf crl_ext;
219
220 x509_buf sig_oid2;
221 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000222 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000223
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 */