blob: fbb1b6740eaadf938c6d935dcfc6fcf08a492fa9 [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
95#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;
182
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;
222
223 struct _x509_crl *next;
224}
225x509_crl;
226
Paul Bakker5121ce52009-01-03 21:22:43 +0000227/*
228 * Structures for writing X.509 certificates
229 */
230typedef struct _x509_node
231{
232 unsigned char *data;
233 unsigned char *p;
234 unsigned char *end;
235
236 size_t len;
237}
238x509_node;
239
240typedef struct _x509_raw
241{
242 x509_node raw;
243 x509_node tbs;
244
245 x509_node version;
246 x509_node serial;
247 x509_node tbs_signalg;
248 x509_node issuer;
249 x509_node validity;
250 x509_node subject;
251 x509_node subpubkey;
252
253 x509_node signalg;
254 x509_node sign;
255}
256x509_raw;
257
258#ifdef __cplusplus
259extern "C" {
260#endif
261
262/**
263 * \brief Parse one or more certificates and add them
264 * to the chained list
265 *
266 * \param chain points to the start of the chain
267 * \param buf buffer holding the certificate data
268 * \param buflen size of the buffer
269 *
270 * \return 0 if successful, or a specific X509 error code
271 */
Paul Bakker592457c2009-04-01 19:01:43 +0000272int x509parse_crt( x509_cert *chain, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274/**
275 * \brief Load one or more certificates and add them
276 * to the chained list
277 *
278 * \param chain points to the start of the chain
279 * \param path filename to read the certificates from
280 *
281 * \return 0 if successful, or a specific X509 error code
282 */
Paul Bakker592457c2009-04-01 19:01:43 +0000283int x509parse_crtfile( x509_cert *chain, char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000286 * \brief Parse one or more CRLs and add them
287 * to the chained list
288 *
289 * \param chain points to the start of the chain
290 * \param buf buffer holding the CRL data
291 * \param buflen size of the buffer
292 *
293 * \return 0 if successful, or a specific X509 error code
294 */
295int x509parse_crl( x509_crl *chain, unsigned char *buf, int buflen );
296
297/**
298 * \brief Load one or more CRLs and add them
299 * to the chained list
300 *
301 * \param chain points to the start of the chain
302 * \param path filename to read the CRLs from
303 *
304 * \return 0 if successful, or a specific X509 error code
305 */
306int x509parse_crlfile( x509_crl *chain, char *path );
307
308/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \brief Parse a private RSA key
310 *
311 * \param rsa RSA context to be initialized
312 * \param buf input buffer
313 * \param buflen size of the buffer
314 * \param pwd password for decryption (optional)
315 * \param pwdlen size of the password
316 *
317 * \return 0 if successful, or a specific X509 error code
318 */
319int x509parse_key( rsa_context *rsa,
320 unsigned char *buf, int buflen,
321 unsigned char *pwd, int pwdlen );
322
323/**
324 * \brief Load and parse a private RSA key
325 *
326 * \param rsa RSA context to be initialized
327 * \param path filename to read the private key from
328 * \param pwd password to decrypt the file (can be NULL)
329 *
330 * \return 0 if successful, or a specific X509 error code
331 */
332int x509parse_keyfile( rsa_context *rsa, char *path, char *password );
333
334/**
335 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000337 *
338 * \param buf Buffer to write to
339 * \param size Maximum size of buffer
340 * \param dn The X509 name to represent
341 *
342 * \return The amount of data written to the buffer, or -1 in
343 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000345int x509parse_dn_gets( char *buf, size_t size, x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347/**
348 * \brief Returns an informational string about the
349 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000350 *
351 * \param buf Buffer to write to
352 * \param size Maximum size of buffer
353 * \param prefix A line prefix
354 * \param crt The X509 certificate to represent
355 *
356 * \return The amount of data written to the buffer, or -1 in
357 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000359int x509parse_cert_info( char *buf, size_t size, char *prefix, x509_cert *crt );
360
361/**
362 * \brief Returns an informational string about the
363 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000364 *
365 * \param buf Buffer to write to
366 * \param size Maximum size of buffer
367 * \param prefix A line prefix
368 * \param crt The X509 CRL to represent
369 *
370 * \return The amount of data written to the buffer, or -1 in
371 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000372 */
373int x509parse_crl_info( char *buf, size_t size, char *prefix, x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000376 * \brief Check a given x509_time against the system time and check
377 * if it is valid.
378 *
379 * \param time x509_time to check
380 *
381 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000382 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000384int x509parse_time_expired( x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386/**
387 * \brief Verify the certificate signature
388 *
389 * \param crt a certificate to be verified
390 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000391 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 * \param cn expected Common Name (can be set to
393 * NULL if the CN must not be verified)
394 * \param flags result of the verification
395 *
Paul Bakker40e46942009-01-03 21:51:57 +0000396 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 * in which case *flags will have one or more of
398 * the following values set:
399 * BADCERT_EXPIRED --
400 * BADCERT_REVOKED --
401 * BADCERT_CN_MISMATCH --
402 * BADCERT_NOT_TRUSTED
403 *
404 * \note TODO: add two arguments, depth and crl
405 */
406int x509parse_verify( x509_cert *crt,
407 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000408 x509_crl *ca_crl,
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 char *cn, int *flags );
410
411/**
412 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000413 *
414 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 */
416void x509_free( x509_cert *crt );
417
418/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000419 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000420 *
421 * \param crt CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422 */
423void x509_crl_free( x509_crl *crl );
424
425/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 * \brief Checkup routine
427 *
428 * \return 0 if successful, or 1 if the test failed
429 */
430int x509_self_test( int verbose );
431
432#ifdef __cplusplus
433}
434#endif
435
436#endif /* x509.h */