blob: 9e1d5d6ac2a083cd3809932c795a2b77bfe2587e [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;
Paul Bakker27d66162010-03-17 06:56:01 +0000183 int sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 struct _x509_cert *next;
186}
187x509_cert;
188
Paul Bakkerd98030e2009-05-02 15:13:40 +0000189typedef struct _x509_crl_entry
190{
191 x509_buf raw;
192
193 x509_buf serial;
194
195 x509_time revocation_date;
196
197 x509_buf entry_ext;
198
199 struct _x509_crl_entry *next;
200}
201x509_crl_entry;
202
203typedef struct _x509_crl
204{
205 x509_buf raw;
206 x509_buf tbs;
207
208 int version;
209 x509_buf sig_oid1;
210
211 x509_buf issuer_raw;
212
213 x509_name issuer;
214
215 x509_time this_update;
216 x509_time next_update;
217
218 x509_crl_entry entry;
219
220 x509_buf crl_ext;
221
222 x509_buf sig_oid2;
223 x509_buf sig;
Paul Bakker27d66162010-03-17 06:56:01 +0000224 int sig_alg;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000225
226 struct _x509_crl *next;
227}
228x509_crl;
229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230/*
231 * Structures for writing X.509 certificates
232 */
233typedef struct _x509_node
234{
235 unsigned char *data;
236 unsigned char *p;
237 unsigned char *end;
238
239 size_t len;
240}
241x509_node;
242
243typedef struct _x509_raw
244{
245 x509_node raw;
246 x509_node tbs;
247
248 x509_node version;
249 x509_node serial;
250 x509_node tbs_signalg;
251 x509_node issuer;
252 x509_node validity;
253 x509_node subject;
254 x509_node subpubkey;
255
256 x509_node signalg;
257 x509_node sign;
258}
259x509_raw;
260
261#ifdef __cplusplus
262extern "C" {
263#endif
264
265/**
266 * \brief Parse one or more certificates and add them
267 * to the chained list
268 *
269 * \param chain points to the start of the chain
270 * \param buf buffer holding the certificate data
271 * \param buflen size of the buffer
272 *
273 * \return 0 if successful, or a specific X509 error code
274 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000275int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277/**
278 * \brief Load one or more certificates and add them
279 * to the chained list
280 *
281 * \param chain points to the start of the chain
282 * \param path filename to read the certificates from
283 *
284 * \return 0 if successful, or a specific X509 error code
285 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000286int x509parse_crtfile( x509_cert *chain, const char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000289 * \brief Parse one or more CRLs and add them
290 * to the chained list
291 *
292 * \param chain points to the start of the chain
293 * \param buf buffer holding the CRL data
294 * \param buflen size of the buffer
295 *
296 * \return 0 if successful, or a specific X509 error code
297 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000298int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000299
300/**
301 * \brief Load one or more CRLs and add them
302 * to the chained list
303 *
304 * \param chain points to the start of the chain
305 * \param path filename to read the CRLs from
306 *
307 * \return 0 if successful, or a specific X509 error code
308 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000309int x509parse_crlfile( x509_crl *chain, const char *path );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000310
311/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * \brief Parse a private RSA key
313 *
314 * \param rsa RSA context to be initialized
Paul Bakkerff60ee62010-03-16 21:09:09 +0000315 * \param key input buffer
316 * \param keylen size of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 * \param pwd password for decryption (optional)
318 * \param pwdlen size of the password
319 *
320 * \return 0 if successful, or a specific X509 error code
321 */
322int x509parse_key( rsa_context *rsa,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000323 const unsigned char *key, int keylen,
324 const unsigned char *pwd, int pwdlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326/**
327 * \brief Load and parse a private RSA key
328 *
329 * \param rsa RSA context to be initialized
330 * \param path filename to read the private key from
331 * \param pwd password to decrypt the file (can be NULL)
332 *
333 * \return 0 if successful, or a specific X509 error code
334 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000335int x509parse_keyfile( rsa_context *rsa, const char *path,
336 const char *password );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338/**
339 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000340 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000341 *
342 * \param buf Buffer to write to
343 * \param size Maximum size of buffer
344 * \param dn The X509 name to represent
345 *
346 * \return The amount of data written to the buffer, or -1 in
347 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000349int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351/**
352 * \brief Returns an informational string about the
353 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000354 *
355 * \param buf Buffer to write to
356 * \param size Maximum size of buffer
357 * \param prefix A line prefix
358 * \param crt The X509 certificate to represent
359 *
360 * \return The amount of data written to the buffer, or -1 in
361 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000363int x509parse_cert_info( char *buf, size_t size, const char *prefix,
364 const x509_cert *crt );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000365
366/**
367 * \brief Returns an informational string about the
368 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000369 *
370 * \param buf Buffer to write to
371 * \param size Maximum size of buffer
372 * \param prefix A line prefix
373 * \param crt The X509 CRL to represent
374 *
375 * \return The amount of data written to the buffer, or -1 in
376 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000377 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000378int x509parse_crl_info( char *buf, size_t size, const char *prefix,
379 const x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000382 * \brief Check a given x509_time against the system time and check
383 * if it is valid.
384 *
385 * \param time x509_time to check
386 *
387 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000388 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000390int x509parse_time_expired( const x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392/**
393 * \brief Verify the certificate signature
394 *
395 * \param crt a certificate to be verified
396 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000397 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 * \param cn expected Common Name (can be set to
399 * NULL if the CN must not be verified)
400 * \param flags result of the verification
401 *
Paul Bakker40e46942009-01-03 21:51:57 +0000402 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 * in which case *flags will have one or more of
404 * the following values set:
405 * BADCERT_EXPIRED --
406 * BADCERT_REVOKED --
407 * BADCERT_CN_MISMATCH --
408 * BADCERT_NOT_TRUSTED
409 *
410 * \note TODO: add two arguments, depth and crl
411 */
412int x509parse_verify( x509_cert *crt,
413 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000414 x509_crl *ca_crl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000415 const char *cn, int *flags );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417/**
418 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000419 *
420 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 */
422void x509_free( x509_cert *crt );
423
424/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000425 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000426 *
427 * \param crt CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000428 */
429void x509_crl_free( x509_crl *crl );
430
431/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 * \brief Checkup routine
433 *
434 * \return 0 if successful, or 1 if the test failed
435 */
436int x509_self_test( int verbose );
437
438#ifdef __cplusplus
439}
440#endif
441
442#endif /* x509.h */