blob: b7b971c70b09241fbe532ee425d4bfaed5cb1bd2 [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 Bakker40e46942009-01-03 21:51:57 +000027#define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0014
28#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0016
29#define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0018
30#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x001A
31#define POLARSSL_ERR_ASN1_INVALID_DATA -0x001C
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020
34#define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040
35#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060
36#define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080
37#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0
38#define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0
39#define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0
40#define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100
41#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120
42#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140
43#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160
44#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180
45#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0
46#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0
47#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0
48#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200
49#define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220
50#define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240
51#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260
52#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280
53#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0
54#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0
55#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0
56#define POLARSSL_ERR_X509_POINT_ERROR -0x0300
57#define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320
Paul Bakker5121ce52009-01-03 21:22:43 +000058
59#define BADCERT_EXPIRED 1
60#define BADCERT_REVOKED 2
61#define BADCERT_CN_MISMATCH 4
62#define BADCERT_NOT_TRUSTED 8
Paul Bakker40ea7de2009-05-03 10:18:48 +000063#define BADCRL_NOT_TRUSTED 16
64#define BADCRL_EXPIRED 32
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66/*
67 * DER constants
68 */
69#define ASN1_BOOLEAN 0x01
70#define ASN1_INTEGER 0x02
71#define ASN1_BIT_STRING 0x03
72#define ASN1_OCTET_STRING 0x04
73#define ASN1_NULL 0x05
74#define ASN1_OID 0x06
75#define ASN1_UTF8_STRING 0x0C
76#define ASN1_SEQUENCE 0x10
77#define ASN1_SET 0x11
78#define ASN1_PRINTABLE_STRING 0x13
79#define ASN1_T61_STRING 0x14
80#define ASN1_IA5_STRING 0x16
81#define ASN1_UTC_TIME 0x17
82#define ASN1_UNIVERSAL_STRING 0x1C
83#define ASN1_BMP_STRING 0x1E
84#define ASN1_PRIMITIVE 0x00
85#define ASN1_CONSTRUCTED 0x20
86#define ASN1_CONTEXT_SPECIFIC 0x80
87
88/*
89 * various object identifiers
90 */
91#define X520_COMMON_NAME 3
92#define X520_COUNTRY 6
93#define X520_LOCALITY 7
94#define X520_STATE 8
95#define X520_ORGANIZATION 10
96#define X520_ORG_UNIT 11
97#define PKCS9_EMAIL 1
98
99#define X509_OUTPUT_DER 0x01
100#define X509_OUTPUT_PEM 0x02
101#define PEM_LINE_LENGTH 72
102#define X509_ISSUER 0x01
103#define X509_SUBJECT 0x02
104
105#define OID_X520 "\x55\x04"
106#define OID_CN "\x55\x04\x03"
107#define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
108#define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
109#define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
110#define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
111#define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
112
113/*
114 * Structures for parsing X.509 certificates
115 */
116typedef struct _x509_buf
117{
118 int tag;
119 int len;
120 unsigned char *p;
121}
122x509_buf;
123
124typedef struct _x509_name
125{
126 x509_buf oid;
127 x509_buf val;
128 struct _x509_name *next;
129}
130x509_name;
131
132typedef struct _x509_time
133{
134 int year, mon, day;
135 int hour, min, sec;
136}
137x509_time;
138
139typedef struct _x509_cert
140{
141 x509_buf raw;
142 x509_buf tbs;
143
144 int version;
145 x509_buf serial;
146 x509_buf sig_oid1;
147
148 x509_buf issuer_raw;
149 x509_buf subject_raw;
150
151 x509_name issuer;
152 x509_name subject;
153
154 x509_time valid_from;
155 x509_time valid_to;
156
157 x509_buf pk_oid;
158 rsa_context rsa;
159
160 x509_buf issuer_id;
161 x509_buf subject_id;
162 x509_buf v3_ext;
163
164 int ca_istrue;
165 int max_pathlen;
166
167 x509_buf sig_oid2;
168 x509_buf sig;
169
170 struct _x509_cert *next;
171}
172x509_cert;
173
Paul Bakkerd98030e2009-05-02 15:13:40 +0000174typedef struct _x509_crl_entry
175{
176 x509_buf raw;
177
178 x509_buf serial;
179
180 x509_time revocation_date;
181
182 x509_buf entry_ext;
183
184 struct _x509_crl_entry *next;
185}
186x509_crl_entry;
187
188typedef struct _x509_crl
189{
190 x509_buf raw;
191 x509_buf tbs;
192
193 int version;
194 x509_buf sig_oid1;
195
196 x509_buf issuer_raw;
197
198 x509_name issuer;
199
200 x509_time this_update;
201 x509_time next_update;
202
203 x509_crl_entry entry;
204
205 x509_buf crl_ext;
206
207 x509_buf sig_oid2;
208 x509_buf sig;
209
210 struct _x509_crl *next;
211}
212x509_crl;
213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214/*
215 * Structures for writing X.509 certificates
216 */
217typedef struct _x509_node
218{
219 unsigned char *data;
220 unsigned char *p;
221 unsigned char *end;
222
223 size_t len;
224}
225x509_node;
226
227typedef struct _x509_raw
228{
229 x509_node raw;
230 x509_node tbs;
231
232 x509_node version;
233 x509_node serial;
234 x509_node tbs_signalg;
235 x509_node issuer;
236 x509_node validity;
237 x509_node subject;
238 x509_node subpubkey;
239
240 x509_node signalg;
241 x509_node sign;
242}
243x509_raw;
244
245#ifdef __cplusplus
246extern "C" {
247#endif
248
249/**
250 * \brief Parse one or more certificates and add them
251 * to the chained list
252 *
253 * \param chain points to the start of the chain
254 * \param buf buffer holding the certificate data
255 * \param buflen size of the buffer
256 *
257 * \return 0 if successful, or a specific X509 error code
258 */
Paul Bakker592457c2009-04-01 19:01:43 +0000259int x509parse_crt( x509_cert *chain, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261/**
262 * \brief Load 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 path filename to read the certificates from
267 *
268 * \return 0 if successful, or a specific X509 error code
269 */
Paul Bakker592457c2009-04-01 19:01:43 +0000270int x509parse_crtfile( x509_cert *chain, char *path );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000273 * \brief Parse one or more CRLs and add them
274 * to the chained list
275 *
276 * \param chain points to the start of the chain
277 * \param buf buffer holding the CRL data
278 * \param buflen size of the buffer
279 *
280 * \return 0 if successful, or a specific X509 error code
281 */
282int x509parse_crl( x509_crl *chain, unsigned char *buf, int buflen );
283
284/**
285 * \brief Load 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 path filename to read the CRLs from
290 *
291 * \return 0 if successful, or a specific X509 error code
292 */
293int x509parse_crlfile( x509_crl *chain, char *path );
294
295/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 * \brief Parse a private RSA key
297 *
298 * \param rsa RSA context to be initialized
299 * \param buf input buffer
300 * \param buflen size of the buffer
301 * \param pwd password for decryption (optional)
302 * \param pwdlen size of the password
303 *
304 * \return 0 if successful, or a specific X509 error code
305 */
306int x509parse_key( rsa_context *rsa,
307 unsigned char *buf, int buflen,
308 unsigned char *pwd, int pwdlen );
309
310/**
311 * \brief Load and parse a private RSA key
312 *
313 * \param rsa RSA context to be initialized
314 * \param path filename to read the private key from
315 * \param pwd password to decrypt the file (can be NULL)
316 *
317 * \return 0 if successful, or a specific X509 error code
318 */
319int x509parse_keyfile( rsa_context *rsa, char *path, char *password );
320
321/**
322 * \brief Store the certificate DN in printable form into buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000323 * no more than size characters will be written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000325int x509parse_dn_gets( char *buf, size_t size, x509_name *dn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327/**
328 * \brief Returns an informational string about the
329 * certificate.
330 */
Paul Bakkerd98030e2009-05-02 15:13:40 +0000331int x509parse_cert_info( char *buf, size_t size, char *prefix, x509_cert *crt );
332
333/**
334 * \brief Returns an informational string about the
335 * CRL.
336 */
337int x509parse_crl_info( char *buf, size_t size, char *prefix, x509_crl *crl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339/**
Paul Bakker40ea7de2009-05-03 10:18:48 +0000340 * \brief Return 0 if the x509_time is still valid,
341 * or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000343int x509parse_time_expired( x509_time *time );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345/**
346 * \brief Verify the certificate signature
347 *
348 * \param crt a certificate to be verified
349 * \param trust_ca the trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000350 * \param ca_crl the CRL chain for trusted CA's
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 * \param cn expected Common Name (can be set to
352 * NULL if the CN must not be verified)
353 * \param flags result of the verification
354 *
Paul Bakker40e46942009-01-03 21:51:57 +0000355 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 * in which case *flags will have one or more of
357 * the following values set:
358 * BADCERT_EXPIRED --
359 * BADCERT_REVOKED --
360 * BADCERT_CN_MISMATCH --
361 * BADCERT_NOT_TRUSTED
362 *
363 * \note TODO: add two arguments, depth and crl
364 */
365int x509parse_verify( x509_cert *crt,
366 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000367 x509_crl *ca_crl,
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 char *cn, int *flags );
369
370/**
371 * \brief Unallocate all certificate data
372 */
373void x509_free( x509_cert *crt );
374
375/**
Paul Bakkerd98030e2009-05-02 15:13:40 +0000376 * \brief Unallocate all CRL data
377 */
378void x509_crl_free( x509_crl *crl );
379
380/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 * \brief Checkup routine
382 *
383 * \return 0 if successful, or 1 if the test failed
384 */
385int x509_self_test( int verbose );
386
387#ifdef __cplusplus
388}
389#endif
390
391#endif /* x509.h */