blob: 2f6d9c9b3b1d8befa0bea2956296ed579d4c69dc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file x509.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
8 * 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 Bakkerb5bf1762009-07-19 20:28:35 +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
94#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;
181
182 struct _x509_cert *next;
183}
184x509_cert;
185
Paul Bakkerd98030e2009-05-02 15:13:40 +0000186typedef struct _x509_crl_entry
187{
188 x509_buf raw;
189
190 x509_buf serial;
191
192 x509_time revocation_date;
193
194 x509_buf entry_ext;
195
196 struct _x509_crl_entry *next;
197}
198x509_crl_entry;
199
200typedef struct _x509_crl
201{
202 x509_buf raw;
203 x509_buf tbs;
204
205 int version;
206 x509_buf sig_oid1;
207
208 x509_buf issuer_raw;
209
210 x509_name issuer;
211
212 x509_time this_update;
213 x509_time next_update;
214
215 x509_crl_entry entry;
216
217 x509_buf crl_ext;
218
219 x509_buf sig_oid2;
220 x509_buf sig;
221
222 struct _x509_crl *next;
223}
224x509_crl;
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226/*
227 * Structures for writing X.509 certificates
228 */
229typedef struct _x509_node
230{
231 unsigned char *data;
232 unsigned char *p;
233 unsigned char *end;
234
235 size_t len;
236}
237x509_node;
238
239typedef struct _x509_raw
240{
241 x509_node raw;
242 x509_node tbs;
243
244 x509_node version;
245 x509_node serial;
246 x509_node tbs_signalg;
247 x509_node issuer;
248 x509_node validity;
249 x509_node subject;
250 x509_node subpubkey;
251
252 x509_node signalg;
253 x509_node sign;
254}
255x509_raw;
256
257#ifdef __cplusplus
258extern "C" {
259#endif
260
261/**
262 * \brief Parse one or more certificates and add them
263 * to the chained list
264 *
265 * \param chain points to the start of the chain
266 * \param buf buffer holding the certificate data
267 * \param buflen size of the buffer
268 *
269 * \return 0 if successful, or a specific X509 error code
270 */
Paul Bakker592457c2009-04-01 19:01:43 +0000271int x509parse_crt( x509_cert *chain, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
273/**
274 * \brief Load one or more certificates and add them
275 * to the chained list
276 *
277 * \param chain points to the start of the chain
278 * \param path filename to read the certificates from
279 *
280 * \return 0 if successful, or a specific X509 error code
281 */
Paul Bakker592457c2009-04-01 19:01:43 +0000282int x509parse_crtfile( x509_cert *chain, char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000285 * \brief Parse one or more CRLs and add them
286 * to the chained list
287 *
288 * \param chain points to the start of the chain
289 * \param buf buffer holding the CRL data
290 * \param buflen size of the buffer
291 *
292 * \return 0 if successful, or a specific X509 error code
293 */
294int x509parse_crl( x509_crl *chain, unsigned char *buf, int buflen );
295
296/**
297 * \brief Load one or more CRLs and add them
298 * to the chained list
299 *
300 * \param chain points to the start of the chain
301 * \param path filename to read the CRLs from
302 *
303 * \return 0 if successful, or a specific X509 error code
304 */
305int x509parse_crlfile( x509_crl *chain, char *path );
306
307/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 * \brief Parse a private RSA key
309 *
310 * \param rsa RSA context to be initialized
311 * \param buf input buffer
312 * \param buflen size of the buffer
313 * \param pwd password for decryption (optional)
314 * \param pwdlen size of the password
315 *
316 * \return 0 if successful, or a specific X509 error code
317 */
318int x509parse_key( rsa_context *rsa,
319 unsigned char *buf, int buflen,
320 unsigned char *pwd, int pwdlen );
321
322/**
323 * \brief Load and parse a private RSA key
324 *
325 * \param rsa RSA context to be initialized
326 * \param path filename to read the private key from
327 * \param pwd password to decrypt the file (can be NULL)
328 *
329 * \return 0 if successful, or a specific X509 error code
330 */
331int x509parse_keyfile( rsa_context *rsa, char *path, char *password );
332
333/**
334 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000335 * no more than size characters will be written.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000336 *
337 * \param buf Buffer to write to
338 * \param size Maximum size of buffer
339 * \param dn The X509 name to represent
340 *
341 * \return The amount of data written to the buffer, or -1 in
342 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344int x509parse_dn_gets( char *buf, size_t size, x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346/**
347 * \brief Returns an informational string about the
348 * certificate.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000349 *
350 * \param buf Buffer to write to
351 * \param size Maximum size of buffer
352 * \param prefix A line prefix
353 * \param crt The X509 certificate to represent
354 *
355 * \return The amount of data written to the buffer, or -1 in
356 * case of an error.
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000358int x509parse_cert_info( char *buf, size_t size, char *prefix, x509_cert *crt );
359
360/**
361 * \brief Returns an informational string about the
362 * CRL.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000363 *
364 * \param buf Buffer to write to
365 * \param size Maximum size of buffer
366 * \param prefix A line prefix
367 * \param crt The X509 CRL to represent
368 *
369 * \return The amount of data written to the buffer, or -1 in
370 * case of an error.
Paul Bakkerd98030e2009-05-02 15:13:40 +0000371 */
372int x509parse_crl_info( char *buf, size_t size, char *prefix, x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000375 * \brief Check a given x509_time against the system time and check
376 * if it is valid.
377 *
378 * \param time x509_time to check
379 *
380 * \return Return 0 if the x509_time is still valid,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000381 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000383int x509parse_time_expired( x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385/**
386 * \brief Verify the certificate signature
387 *
388 * \param crt a certificate to be verified
389 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000390 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * \param cn expected Common Name (can be set to
392 * NULL if the CN must not be verified)
393 * \param flags result of the verification
394 *
Paul Bakker40e46942009-01-03 21:51:57 +0000395 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 * in which case *flags will have one or more of
397 * the following values set:
398 * BADCERT_EXPIRED --
399 * BADCERT_REVOKED --
400 * BADCERT_CN_MISMATCH --
401 * BADCERT_NOT_TRUSTED
402 *
403 * \note TODO: add two arguments, depth and crl
404 */
405int x509parse_verify( x509_cert *crt,
406 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000407 x509_crl *ca_crl,
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 char *cn, int *flags );
409
410/**
411 * \brief Unallocate all certificate data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000412 *
413 * \param crt Certificate chain to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 */
415void x509_free( x509_cert *crt );
416
417/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418 * \brief Unallocate all CRL data
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000419 *
420 * \param crt CRL chain to free
Paul Bakkerd98030e2009-05-02 15:13:40 +0000421 */
422void x509_crl_free( x509_crl *crl );
423
424/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \brief Checkup routine
426 *
427 * \return 0 if successful, or 1 if the test failed
428 */
429int x509_self_test( int verbose );
430
431#ifdef __cplusplus
432}
433#endif
434
435#endif /* x509.h */