initial commit; it works
[psslcertgen.git] / src / libpolarssl / x509.h
blobb4ebf64d3fbcc3eca20b23841e1f723cafb8a059
1 /**
2 * \file x509.h
4 * \brief X.509 certificate and private key decoding
6 * Copyright (C) 2006-2011, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #ifndef POLARSSL_X509_H
28 #define POLARSSL_X509_H
30 #include "asn1.h"
31 #include "rsa.h"
32 #include "dhm.h"
34 /**
35 * \addtogroup x509_module
36 * \{
39 /**
40 * \name X509 Error codes
41 * \{
43 #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
44 #define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
45 #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
46 #define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
47 #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
48 #define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
49 #define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
50 #define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
51 #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */
52 #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */
53 #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */
54 #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */
55 #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */
56 #define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA is supported). */
57 #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
58 #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
59 #define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */
60 #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */
61 #define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */
62 #define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */
63 #define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */
64 #define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */
65 #define POLARSSL_ERR_X509_PASSWORD_REQUIRED -0x2B80 /**< Private key password can't be empty. */
66 #define POLARSSL_ERR_X509_PASSWORD_MISMATCH -0x2C00 /**< Given private key password does not allow for correct decryption. */
67 /* \} name */
70 /**
71 * \name X509 Verify codes
72 * \{
74 #define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
75 #define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
76 #define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
77 #define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
78 #define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
79 #define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
80 #define BADCERT_MISSING 0x40 /**< Certificate was missing. */
81 #define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
82 #define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
83 /* \} name */
84 /* \} addtogroup x509_module */
87 * various object identifiers
89 #define X520_COMMON_NAME 3
90 #define X520_COUNTRY 6
91 #define X520_LOCALITY 7
92 #define X520_STATE 8
93 #define X520_ORGANIZATION 10
94 #define X520_ORG_UNIT 11
95 #define PKCS9_EMAIL 1
97 #define X509_OUTPUT_DER 0x01
98 #define X509_OUTPUT_PEM 0x02
99 #define PEM_LINE_LENGTH 72
100 #define X509_ISSUER 0x01
101 #define X509_SUBJECT 0x02
103 #define OID_X520 "\x55\x04"
104 #define OID_CN OID_X520 "\x03"
105 #define OID_COUNTRY OID_X520 "\x06"
106 #define OID_LOCALITY OID_X520 "\x07"
107 #define OID_STATE OID_X520 "\x08"
108 #define OID_ORGANIZATION OID_X520 "\x0A"
109 #define OID_ORG_UNIT OID_X520 "\x0B"
111 #define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
112 #define OID_PKCS1_RSA OID_PKCS1 "\x01"
113 #define OID_PKCS1_SHA1 OID_PKCS1 "\x05"
115 #define OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
117 #define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
118 #define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
120 /** ISO arc for standard certificate and CRL extensions */
121 #define OID_ID_CE "\x55\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
124 * Private Internet Extensions
125 * { iso(1) identified-organization(3) dod(6) internet(1)
126 * security(5) mechanisms(5) pkix(7) }
128 #define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
131 * OIDs for standard certificate extensions
133 #define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
134 #define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
135 #define OID_KEY_USAGE OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
136 #define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
137 #define OID_POLICY_MAPPINGS OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
138 #define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
139 #define OID_ISSUER_ALT_NAME OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
140 #define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
141 #define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
142 #define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
143 #define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
144 #define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
145 #define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
146 #define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
147 #define OID_FRESHEST_CRL OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
150 * X.509 v3 Key Usage Extension flags
152 #define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
153 #define KU_NON_REPUDIATION (0x40) /* bit 1 */
154 #define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
155 #define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
156 #define KU_KEY_AGREEMENT (0x08) /* bit 4 */
157 #define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
158 #define KU_CRL_SIGN (0x02) /* bit 6 */
161 * X.509 v3 Extended key usage OIDs
163 #define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
165 #define OID_KP OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
166 #define OID_SERVER_AUTH OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
167 #define OID_CLIENT_AUTH OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
168 #define OID_CODE_SIGNING OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
169 #define OID_EMAIL_PROTECTION OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
170 #define OID_TIME_STAMPING OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
171 #define OID_OCSP_SIGNING OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
173 #define STRING_SERVER_AUTH "TLS Web Server Authentication"
174 #define STRING_CLIENT_AUTH "TLS Web Client Authentication"
175 #define STRING_CODE_SIGNING "Code Signing"
176 #define STRING_EMAIL_PROTECTION "E-mail Protection"
177 #define STRING_TIME_STAMPING "Time Stamping"
178 #define STRING_OCSP_SIGNING "OCSP Signing"
181 * OIDs for CRL extensions
183 #define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
184 #define OID_CRL_NUMBER OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
187 * Netscape certificate extensions
189 #define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
190 #define OID_NS_CERT OID_NETSCAPE "\x01"
191 #define OID_NS_CERT_TYPE OID_NS_CERT "\x01"
192 #define OID_NS_BASE_URL OID_NS_CERT "\x02"
193 #define OID_NS_REVOCATION_URL OID_NS_CERT "\x03"
194 #define OID_NS_CA_REVOCATION_URL OID_NS_CERT "\x04"
195 #define OID_NS_RENEWAL_URL OID_NS_CERT "\x07"
196 #define OID_NS_CA_POLICY_URL OID_NS_CERT "\x08"
197 #define OID_NS_SSL_SERVER_NAME OID_NS_CERT "\x0C"
198 #define OID_NS_COMMENT OID_NS_CERT "\x0D"
199 #define OID_NS_DATA_TYPE OID_NETSCAPE "\x02"
200 #define OID_NS_CERT_SEQUENCE OID_NS_DATA_TYPE "\x05"
203 * Netscape certificate types
204 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
207 #define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
208 #define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
209 #define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
210 #define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
211 #define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
212 #define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
213 #define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
214 #define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
216 #define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
217 #define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
218 #define EXT_KEY_USAGE (1 << 2)
219 #define EXT_CERTIFICATE_POLICIES (1 << 3)
220 #define EXT_POLICY_MAPPINGS (1 << 4)
221 #define EXT_SUBJECT_ALT_NAME (1 << 5)
222 #define EXT_ISSUER_ALT_NAME (1 << 6)
223 #define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
224 #define EXT_BASIC_CONSTRAINTS (1 << 8)
225 #define EXT_NAME_CONSTRAINTS (1 << 9)
226 #define EXT_POLICY_CONSTRAINTS (1 << 10)
227 #define EXT_EXTENDED_KEY_USAGE (1 << 11)
228 #define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
229 #define EXT_INIHIBIT_ANYPOLICY (1 << 13)
230 #define EXT_FRESHEST_CRL (1 << 14)
232 #define EXT_NS_CERT_TYPE (1 << 16)
235 * Storage format identifiers
236 * Recognized formats: PEM and DER
238 #define X509_FORMAT_DER 1
239 #define X509_FORMAT_PEM 2
241 /**
242 * \addtogroup x509_module
243 * \{ */
246 * \name Structures for parsing X.509 certificates and CRLs
247 * \{
250 /**
251 * Type-length-value structure that allows for ASN1 using DER.
253 typedef asn1_buf x509_buf;
256 * Container for ASN1 bit strings.
258 typedef asn1_bitstring x509_bitstring;
261 * Container for ASN1 named information objects.
262 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
264 typedef struct _x509_name
266 x509_buf oid; /**< The object identifier. */
267 x509_buf val; /**< The named value. */
268 struct _x509_name *next; /**< The next named information object. */
270 x509_name;
273 * Container for a sequence of ASN.1 items
275 typedef asn1_sequence x509_sequence;
277 /** Container for date and time (precision in seconds). */
278 typedef struct _x509_time
280 int year, mon, day; /**< Date. */
281 int hour, min, sec; /**< Time. */
283 x509_time;
285 /**
286 * Container for an X.509 certificate. The certificate may be chained.
288 typedef struct _x509_cert
290 x509_buf raw; /**< The raw certificate data (DER). */
291 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
293 int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
294 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
295 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
297 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
298 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
300 x509_name issuer; /**< The parsed issuer data (named information object). */
301 x509_name subject; /**< The parsed subject data (named information object). */
303 x509_time valid_from; /**< Start time of certificate validity. */
304 x509_time valid_to; /**< End time of certificate validity. */
306 x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
307 rsa_context rsa; /**< Container for the RSA context. Only RSA is supported for public keys at this time. */
309 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
310 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
311 x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
312 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
314 int ext_types; /**< Bit string containing detected and parsed extensions */
315 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
316 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
318 unsigned char key_usage; /**< Optional key usage extension value: See the values below */
320 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
322 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
324 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
325 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
326 int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
328 struct _x509_cert *next; /**< Next certificate in the CA-chain. */
330 x509_cert;
332 /**
333 * Certificate revocation list entry.
334 * Contains the CA-specific serial numbers and revocation dates.
336 typedef struct _x509_crl_entry
338 x509_buf raw;
340 x509_buf serial;
342 x509_time revocation_date;
344 x509_buf entry_ext;
346 struct _x509_crl_entry *next;
348 x509_crl_entry;
350 /**
351 * Certificate revocation list structure.
352 * Every CRL may have multiple entries.
354 typedef struct _x509_crl
356 x509_buf raw; /**< The raw certificate data (DER). */
357 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
359 int version;
360 x509_buf sig_oid1;
362 x509_buf issuer_raw; /**< The raw issuer data (DER). */
364 x509_name issuer; /**< The parsed issuer data (named information object). */
366 x509_time this_update;
367 x509_time next_update;
369 x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
371 x509_buf crl_ext;
373 x509_buf sig_oid2;
374 x509_buf sig;
375 int sig_alg;
377 struct _x509_crl *next;
379 x509_crl;
380 /** \} name Structures for parsing X.509 certificates and CRLs */
381 /** \} addtogroup x509_module */
384 * \name Structures for writing X.509 certificates.
385 * XvP: commented out as they are not used.
386 * - <tt>typedef struct _x509_node x509_node;</tt>
387 * - <tt>typedef struct _x509_raw x509_raw;</tt>
389 typedef struct _x509_node
391 unsigned char *data;
392 unsigned char *p;
393 unsigned char *end;
395 size_t len;
397 x509_node;
399 typedef struct _x509_raw
401 x509_node raw;
402 x509_node tbs;
404 x509_node version;
405 x509_node serial;
406 x509_node tbs_signalg;
407 x509_node issuer;
408 x509_node validity;
409 x509_node subject;
410 x509_node subpubkey;
412 x509_node signalg;
413 x509_node sign;
415 x509_raw;
417 #ifdef __cplusplus
418 extern "C" {
419 #endif
422 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
423 * \{
426 /** \ingroup x509_module */
428 * \brief Parse a single DER formatted certificate and add it
429 * to the chained list.
431 * \param chain points to the start of the chain
432 * \param buf buffer holding the certificate DER data
433 * \param buflen size of the buffer
435 * \return 0 if successful, or a specific X509 or PEM error code
437 int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen );
440 * \brief Parse one or more certificates and add them
441 * to the chained list. Parses permissively. If some
442 * certificates can be parsed, the result is the number
443 * of failed certificates it encountered. If none complete
444 * correctly, the first error is returned.
446 * \param chain points to the start of the chain
447 * \param buf buffer holding the certificate data
448 * \param buflen size of the buffer
450 * \return 0 if all certificates parsed successfully, a positive number
451 * if partly successful or a specific X509 or PEM error code
453 int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
455 /** \ingroup x509_module */
457 * \brief Load one or more certificates and add them
458 * to the chained list. Parses permissively. If some
459 * certificates can be parsed, the result is the number
460 * of failed certificates it encountered. If none complete
461 * correctly, the first error is returned.
463 * \param chain points to the start of the chain
464 * \param path filename to read the certificates from
466 * \return 0 if all certificates parsed successfully, a positive number
467 * if partly successful or a specific X509 or PEM error code
469 int x509parse_crtfile( x509_cert *chain, const char *path );
471 /** \ingroup x509_module */
473 * \brief Load one or more certificate files from a path and add them
474 * to the chained list. Parses permissively. If some
475 * certificates can be parsed, the result is the number
476 * of failed certificates it encountered. If none complete
477 * correctly, the first error is returned.
479 * \param chain points to the start of the chain
480 * \param path directory / folder to read the certificate files from
482 * \return 0 if all certificates parsed successfully, a positive number
483 * if partly successful or a specific X509 or PEM error code
485 int x509parse_crtpath( x509_cert *chain, const char *path );
487 /** \ingroup x509_module */
489 * \brief Parse one or more CRLs and add them
490 * to the chained list
492 * \param chain points to the start of the chain
493 * \param buf buffer holding the CRL data
494 * \param buflen size of the buffer
496 * \return 0 if successful, or a specific X509 or PEM error code
498 int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
500 /** \ingroup x509_module */
502 * \brief Load one or more CRLs and add them
503 * to the chained list
505 * \param chain points to the start of the chain
506 * \param path filename to read the CRLs from
508 * \return 0 if successful, or a specific X509 or PEM error code
510 int x509parse_crlfile( x509_crl *chain, const char *path );
512 /** \ingroup x509_module */
514 * \brief Parse a private RSA key
516 * \param rsa RSA context to be initialized
517 * \param key input buffer
518 * \param keylen size of the buffer
519 * \param pwd password for decryption (optional)
520 * \param pwdlen size of the password
522 * \return 0 if successful, or a specific X509 or PEM error code
524 int x509parse_key( rsa_context *rsa,
525 const unsigned char *key, size_t keylen,
526 const unsigned char *pwd, size_t pwdlen );
528 /** \ingroup x509_module */
530 * \brief Load and parse a private RSA key
532 * \param rsa RSA context to be initialized
533 * \param path filename to read the private key from
534 * \param password password to decrypt the file (can be NULL)
536 * \return 0 if successful, or a specific X509 or PEM error code
538 int x509parse_keyfile( rsa_context *rsa, const char *path,
539 const char *password );
541 /** \ingroup x509_module */
543 * \brief Parse a public RSA key
545 * \param rsa RSA context to be initialized
546 * \param key input buffer
547 * \param keylen size of the buffer
549 * \return 0 if successful, or a specific X509 or PEM error code
551 int x509parse_public_key( rsa_context *rsa,
552 const unsigned char *key, size_t keylen );
554 /** \ingroup x509_module */
556 * \brief Load and parse a public RSA key
558 * \param rsa RSA context to be initialized
559 * \param path filename to read the private key from
561 * \return 0 if successful, or a specific X509 or PEM error code
563 int x509parse_public_keyfile( rsa_context *rsa, const char *path );
565 /** \ingroup x509_module */
567 * \brief Parse DHM parameters
569 * \param dhm DHM context to be initialized
570 * \param dhmin input buffer
571 * \param dhminlen size of the buffer
573 * \return 0 if successful, or a specific X509 or PEM error code
575 int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
577 /** \ingroup x509_module */
579 * \brief Load and parse DHM parameters
581 * \param dhm DHM context to be initialized
582 * \param path filename to read the DHM Parameters from
584 * \return 0 if successful, or a specific X509 or PEM error code
586 int x509parse_dhmfile( dhm_context *dhm, const char *path );
588 /** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
591 * \brief Store the certificate DN in printable form into buf;
592 * no more than size characters will be written.
594 * \param buf Buffer to write to
595 * \param size Maximum size of buffer
596 * \param dn The X509 name to represent
598 * \return The amount of data written to the buffer, or -1 in
599 * case of an error.
601 int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
604 * \brief Store the certificate serial in printable form into buf;
605 * no more than size characters will be written.
607 * \param buf Buffer to write to
608 * \param size Maximum size of buffer
609 * \param serial The X509 serial to represent
611 * \return The amount of data written to the buffer, or -1 in
612 * case of an error.
614 int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
617 * \brief Returns an informational string about the
618 * certificate.
620 * \param buf Buffer to write to
621 * \param size Maximum size of buffer
622 * \param prefix A line prefix
623 * \param crt The X509 certificate to represent
625 * \return The amount of data written to the buffer, or -1 in
626 * case of an error.
628 int x509parse_cert_info( char *buf, size_t size, const char *prefix,
629 const x509_cert *crt );
632 * \brief Returns an informational string about the
633 * CRL.
635 * \param buf Buffer to write to
636 * \param size Maximum size of buffer
637 * \param prefix A line prefix
638 * \param crl The X509 CRL to represent
640 * \return The amount of data written to the buffer, or -1 in
641 * case of an error.
643 int x509parse_crl_info( char *buf, size_t size, const char *prefix,
644 const x509_crl *crl );
647 * \brief Give an known OID, return its descriptive string.
649 * \param oid buffer containing the oid
651 * \return Return a string if the OID is known,
652 * or NULL otherwise.
654 const char *x509_oid_get_description( x509_buf *oid );
657 * \brief Give an OID, return a string version of its OID number.
659 * \param buf Buffer to write to
660 * \param size Maximum size of buffer
661 * \param oid Buffer containing the OID
663 * \return The amount of data written to the buffer, or -1 in
664 * case of an error.
666 int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
669 * \brief Check a given x509_time against the system time and check
670 * if it is valid.
672 * \param time x509_time to check
674 * \return Return 0 if the x509_time is still valid,
675 * or 1 otherwise.
677 int x509parse_time_expired( const x509_time *time );
680 * \name Functions to verify a certificate
681 * \{
683 /** \ingroup x509_module */
685 * \brief Verify the certificate signature
687 * The verify callback is a user-supplied callback that
688 * can clear / modify / add flags for a certificate. If set,
689 * the verification callback is called for each
690 * certificate in the chain (from the trust-ca down to the
691 * presented crt). The parameters for the callback are:
692 * (void *parameter, x509_cert *crt, int certificate_depth,
693 * int *flags). With the flags representing current flags for
694 * that specific certificate and the certificate depth from
695 * the bottom (Peer cert depth = 0).
697 * All flags left after returning from the callback
698 * are also returned to the application. The function should
699 * return 0 for anything but a fatal error.
701 * \param crt a certificate to be verified
702 * \param trust_ca the trusted CA chain
703 * \param ca_crl the CRL chain for trusted CA's
704 * \param cn expected Common Name (can be set to
705 * NULL if the CN must not be verified)
706 * \param flags result of the verification
707 * \param f_vrfy verification function
708 * \param p_vrfy verification parameter
710 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
711 * in which case *flags will have one or more of
712 * the following values set:
713 * BADCERT_EXPIRED --
714 * BADCERT_REVOKED --
715 * BADCERT_CN_MISMATCH --
716 * BADCERT_NOT_TRUSTED
717 * or another error in case of a fatal error encountered
718 * during the verification process.
720 int x509parse_verify( x509_cert *crt,
721 x509_cert *trust_ca,
722 x509_crl *ca_crl,
723 const char *cn, int *flags,
724 int (*f_vrfy)(void *, x509_cert *, int, int *),
725 void *p_vrfy );
728 * \brief Verify the certificate signature
730 * \param crt a certificate to be verified
731 * \param crl the CRL to verify against
733 * \return 1 if the certificate is revoked, 0 otherwise
736 int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
738 /** \} name Functions to verify a certificate */
743 * \name Functions to clear a certificate, CRL or private RSA key
744 * \{
746 /** \ingroup x509_module */
748 * \brief Unallocate all certificate data
750 * \param crt Certificate chain to free
752 void x509_free( x509_cert *crt );
754 /** \ingroup x509_module */
756 * \brief Unallocate all CRL data
758 * \param crl CRL chain to free
760 void x509_crl_free( x509_crl *crl );
762 /** \} name Functions to clear a certificate, CRL or private RSA key */
766 * \brief Checkup routine
768 * \return 0 if successful, or 1 if the test failed
770 int x509_self_test( int verbose );
772 #ifdef __cplusplus
774 #endif
776 #endif /* x509.h */