PolarSSL v1.2.9
x509.h
Go to the documentation of this file.
1 
27 #ifndef POLARSSL_X509_H
28 #define POLARSSL_X509_H
29 
30 #include "asn1.h"
31 #include "rsa.h"
32 #include "dhm.h"
33 
43 #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080
44 #define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100
45 #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180
46 #define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200
47 #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280
48 #define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300
49 #define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380
50 #define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400
51 #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480
52 #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500
53 #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580
54 #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600
55 #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680
56 #define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700
57 #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780
58 #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800
59 #define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880
60 #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900
61 #define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980
62 #define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00
63 #define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80
64 #define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00
65 #define POLARSSL_ERR_X509_PASSWORD_REQUIRED -0x2B80
66 #define POLARSSL_ERR_X509_PASSWORD_MISMATCH -0x2C00
67 /* \} name */
68 
69 
74 #define BADCERT_EXPIRED 0x01
75 #define BADCERT_REVOKED 0x02
76 #define BADCERT_CN_MISMATCH 0x04
77 #define BADCERT_NOT_TRUSTED 0x08
78 #define BADCRL_NOT_TRUSTED 0x10
79 #define BADCRL_EXPIRED 0x20
80 #define BADCERT_MISSING 0x40
81 #define BADCERT_SKIP_VERIFY 0x80
82 #define BADCERT_OTHER 0x0100
83 /* \} name */
84 /* \} addtogroup x509_module */
85 
86 /*
87  * various object identifiers
88  */
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
96 
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
102 
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"
110 
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"
114 
115 #define OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
116 
117 #define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
118 #define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
119 
121 #define OID_ID_CE "\x55\x1D"
128 #define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
129 
130 /*
131  * OIDs for standard certificate extensions
132  */
133 #define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23"
134 #define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E"
135 #define OID_KEY_USAGE OID_ID_CE "\x0F"
136 #define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20"
137 #define OID_POLICY_MAPPINGS OID_ID_CE "\x21"
138 #define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11"
139 #define OID_ISSUER_ALT_NAME OID_ID_CE "\x12"
140 #define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09"
141 #define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13"
142 #define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E"
143 #define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24"
144 #define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25"
145 #define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F"
146 #define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36"
147 #define OID_FRESHEST_CRL OID_ID_CE "\x2E"
149 /*
150  * X.509 v3 Key Usage Extension flags
151  */
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 */
159 
160 /*
161  * X.509 v3 Extended key usage OIDs
162  */
163 #define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00"
165 #define OID_KP OID_PKIX "\x03"
166 #define OID_SERVER_AUTH OID_KP "\x01"
167 #define OID_CLIENT_AUTH OID_KP "\x02"
168 #define OID_CODE_SIGNING OID_KP "\x03"
169 #define OID_EMAIL_PROTECTION OID_KP "\x04"
170 #define OID_TIME_STAMPING OID_KP "\x08"
171 #define OID_OCSP_SIGNING OID_KP "\x09"
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"
179 
180 /*
181  * OIDs for CRL extensions
182  */
183 #define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
184 #define OID_CRL_NUMBER OID_ID_CE "\x14"
186 /*
187  * Netscape certificate extensions
188  */
189 #define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42"
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"
201 
202 /*
203  * Netscape certificate types
204  * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
205  */
206 
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 */
215 
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)
231 
232 #define EXT_NS_CERT_TYPE (1 << 16)
233 
234 /*
235  * Storage format identifiers
236  * Recognized formats: PEM and DER
237  */
238 #define X509_FORMAT_DER 1
239 #define X509_FORMAT_PEM 2
240 
254 
259 
264 typedef struct _x509_name
265 {
268  struct _x509_name *next;
269 }
270 x509_name;
271 
276 
278 typedef struct _x509_time
279 {
280  int year, mon, day;
281  int hour, min, sec;
282 }
283 x509_time;
284 
288 typedef struct _x509_cert
289 {
293  int version;
314  int ext_types;
315  int ca_istrue;
318  unsigned char key_usage;
322  unsigned char ns_cert_type;
326  int sig_alg;
328  struct _x509_cert *next;
329 }
330 x509_cert;
331 
336 typedef struct _x509_crl_entry
337 {
339 
341 
343 
345 
347 }
349 
354 typedef struct _x509_crl
355 {
359  int version;
361 
368 
372 
375  int sig_alg;
376 
377  struct _x509_crl *next;
378 }
379 x509_crl;
389 /*
390 typedef struct _x509_node
391 {
392  unsigned char *data;
393  unsigned char *p;
394  unsigned char *end;
395 
396  size_t len;
397 }
398 x509_node;
399 
400 typedef struct _x509_raw
401 {
402  x509_node raw;
403  x509_node tbs;
404 
405  x509_node version;
406  x509_node serial;
407  x509_node tbs_signalg;
408  x509_node issuer;
409  x509_node validity;
410  x509_node subject;
411  x509_node subpubkey;
412 
413  x509_node signalg;
414  x509_node sign;
415 }
416 x509_raw;
417 */
418 
419 #ifdef __cplusplus
420 extern "C" {
421 #endif
422 
439 int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen );
440 
455 int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
456 
471 int x509parse_crtfile( x509_cert *chain, const char *path );
472 
487 int x509parse_crtpath( x509_cert *chain, const char *path );
488 
500 int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
501 
512 int x509parse_crlfile( x509_crl *chain, const char *path );
513 
526 int x509parse_key( rsa_context *rsa,
527  const unsigned char *key, size_t keylen,
528  const unsigned char *pwd, size_t pwdlen );
529 
540 int x509parse_keyfile( rsa_context *rsa, const char *path,
541  const char *password );
542 
554  const unsigned char *key, size_t keylen );
555 
565 int x509parse_public_keyfile( rsa_context *rsa, const char *path );
566 
577 int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
578 
588 int x509parse_dhmfile( dhm_context *dhm, const char *path );
589 
603 int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
604 
616 int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
617 
630 int x509parse_cert_info( char *buf, size_t size, const char *prefix,
631  const x509_cert *crt );
632 
645 int x509parse_crl_info( char *buf, size_t size, const char *prefix,
646  const x509_crl *crl );
647 
656 const char *x509_oid_get_description( x509_buf *oid );
657 
668 int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
669 
679 int x509parse_time_expired( const x509_time *time );
680 
722 int x509parse_verify( x509_cert *crt,
723  x509_cert *trust_ca,
724  x509_crl *ca_crl,
725  const char *cn, int *flags,
726  int (*f_vrfy)(void *, x509_cert *, int, int *),
727  void *p_vrfy );
728 
738 int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
739 
754 void x509_free( x509_cert *crt );
755 
762 void x509_crl_free( x509_crl *crl );
763 
772 int x509_self_test( int verbose );
773 
774 #ifdef __cplusplus
775 }
776 #endif
777 
778 #endif /* x509.h */
x509_time valid_to
End time of certificate validity.
Definition: x509.h:304
x509_buf sig
Definition: x509.h:374
x509_buf issuer_raw
The raw issuer data (DER).
Definition: x509.h:297
int x509parse_crt_der(x509_cert *chain, const unsigned char *buf, size_t buflen)
Parse a single DER formatted certificate and add it to the chained list.
struct _x509_name x509_name
Container for ASN1 named information objects.
x509_name issuer
The parsed issuer data (named information object).
Definition: x509.h:300
struct _x509_crl_entry x509_crl_entry
Certificate revocation list entry.
x509_buf tbs
The raw certificate body (DER).
Definition: x509.h:291
x509_buf val
The named value.
Definition: x509.h:267
int max_pathlen
Optional Basic Constraint extension value: The maximum path length to the root certificate.
Definition: x509.h:316
struct _x509_time x509_time
Container for date and time (precision in seconds).
unsigned char ns_cert_type
Optional Netscape certificate type extension value: See the values below.
Definition: x509.h:322
int sec
Time.
Definition: x509.h:281
int version
Definition: x509.h:359
x509_time next_update
Definition: x509.h:367
int x509parse_crtfile(x509_cert *chain, const char *path)
Load one or more certificates and add them to the chained list.
DHM context structure.
Definition: dhm.h:136
Certificate revocation list entry.
Definition: x509.h:336
x509_buf sig
Signature: hash of the tbs part signed with the private key.
Definition: x509.h:325
x509_name subject
The parsed subject data (named information object).
Definition: x509.h:301
struct _x509_crl * next
Definition: x509.h:377
x509_buf raw
The raw certificate data (DER).
Definition: x509.h:290
const char * x509_oid_get_description(x509_buf *oid)
Give an known OID, return its descriptive string.
Container for date and time (precision in seconds).
Definition: x509.h:278
struct _x509_cert x509_cert
Container for an X.509 certificate.
void x509_free(x509_cert *crt)
Unallocate all certificate data.
x509_sequence ext_key_usage
Optional list of extended key usage OIDs.
Definition: x509.h:320
Container for ASN1 named information objects.
Definition: x509.h:264
int x509parse_cert_info(char *buf, size_t size, const char *prefix, const x509_cert *crt)
Returns an informational string about the certificate.
x509_buf sig_oid2
Definition: x509.h:373
Container for an X.509 certificate.
Definition: x509.h:288
int x509parse_dhmfile(dhm_context *dhm, const char *path)
Load and parse DHM parameters.
Container for ASN1 bit strings.
Definition: asn1.h:119
x509_name issuer
The parsed issuer data (named information object).
Definition: x509.h:364
x509_buf serial
Definition: x509.h:340
struct _x509_crl x509_crl
Certificate revocation list structure.
x509_crl_entry entry
The CRL entries containing the certificate revocation times for this CA.
Definition: x509.h:369
x509_buf sig_oid2
Signature algorithm.
Definition: x509.h:324
int hour
Definition: x509.h:281
int mon
Definition: x509.h:280
Container for a sequence of ASN.1 items.
Definition: asn1.h:130
x509_buf sig_oid1
Definition: x509.h:360
int sig_alg
Internal representation of the signature algorithm, e.g.
Definition: x509.h:326
RSA context structure.
Definition: rsa.h:135
int x509parse_crt(x509_cert *chain, const unsigned char *buf, size_t buflen)
Parse one or more certificates and add them to the chained list.
Generic ASN.1 parsing.
int x509parse_time_expired(const x509_time *time)
Check a given x509_time against the system time and check if it is valid.
void x509_crl_free(x509_crl *crl)
Unallocate all CRL data.
int x509parse_dhm(dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen)
Parse DHM parameters.
int x509parse_crtpath(x509_cert *chain, const char *path)
Load one or more certificate files from a path and add them to the chained list.
int x509parse_crl(x509_crl *chain, const unsigned char *buf, size_t buflen)
Parse one or more CRLs and add them to the chained list.
int x509parse_dn_gets(char *buf, size_t size, const x509_name *dn)
Store the certificate DN in printable form into buf; no more than size characters will be written...
int x509parse_key(rsa_context *rsa, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen)
Parse a private RSA key.
int x509_oid_get_numeric_string(char *buf, size_t size, x509_buf *oid)
Give an OID, return a string version of its OID number.
x509_buf tbs
The raw certificate body (DER).
Definition: x509.h:357
int x509parse_public_keyfile(rsa_context *rsa, const char *path)
Load and parse a public RSA key.
x509_buf serial
Unique id for certificate issued by a specific CA.
Definition: x509.h:294
struct _x509_crl_entry * next
Definition: x509.h:346
x509_buf subject_id
Optional X.509 v2/v3 subject unique identifier.
Definition: x509.h:310
Diffie-Hellman-Merkle key exchange.
int day
Date.
Definition: x509.h:280
x509_time valid_from
Start time of certificate validity.
Definition: x509.h:303
x509_buf issuer_id
Optional X.509 v2/v3 issuer unique identifier.
Definition: x509.h:309
unsigned char key_usage
Optional key usage extension value: See the values below.
Definition: x509.h:318
int x509parse_crl_info(char *buf, size_t size, const char *prefix, const x509_crl *crl)
Returns an informational string about the CRL.
int x509parse_public_key(rsa_context *rsa, const unsigned char *key, size_t keylen)
Parse a public RSA key.
struct _x509_cert * next
Next certificate in the CA-chain.
Definition: x509.h:328
x509_time this_update
Definition: x509.h:366
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:108
The RSA public-key cryptosystem.
x509_buf entry_ext
Definition: x509.h:344
int year
Definition: x509.h:280
int x509parse_revoked(const x509_cert *crt, const x509_crl *crl)
Verify the certificate signature.
x509_buf raw
Definition: x509.h:338
x509_buf subject_raw
The raw subject data (DER).
Definition: x509.h:298
Certificate revocation list structure.
Definition: x509.h:354
int min
Definition: x509.h:281
x509_sequence subject_alt_names
Optional list of Subject Alternative Names (Only dNSName supported).
Definition: x509.h:312
x509_buf v3_ext
Optional X.509 v3 extensions.
Definition: x509.h:311
x509_buf pk_oid
Subject public key info.
Definition: x509.h:306
x509_buf raw
The raw certificate data (DER).
Definition: x509.h:356
asn1_buf x509_buf
Type-length-value structure that allows for ASN1 using DER.
Definition: x509.h:253
int x509_self_test(int verbose)
Checkup routine.
asn1_sequence x509_sequence
Container for a sequence of ASN.1 items.
Definition: x509.h:275
x509_time revocation_date
Definition: x509.h:342
int x509parse_verify(x509_cert *crt, x509_cert *trust_ca, x509_crl *ca_crl, const char *cn, int *flags, int(*f_vrfy)(void *, x509_cert *, int, int *), void *p_vrfy)
Verify the certificate signature.
asn1_bitstring x509_bitstring
Container for ASN1 bit strings.
Definition: x509.h:258
x509_buf issuer_raw
The raw issuer data (DER).
Definition: x509.h:362
int version
The X.509 version.
Definition: x509.h:293
rsa_context rsa
Container for the RSA context.
Definition: x509.h:307
x509_buf crl_ext
Definition: x509.h:371
int x509parse_keyfile(rsa_context *rsa, const char *path, const char *password)
Load and parse a private RSA key.
int x509parse_serial_gets(char *buf, size_t size, const x509_buf *serial)
Store the certificate serial in printable form into buf; no more than size characters will be written...
int x509parse_crlfile(x509_crl *chain, const char *path)
Load one or more CRLs and add them to the chained list.
int sig_alg
Definition: x509.h:375
x509_buf sig_oid1
Signature algorithm, e.g.
Definition: x509.h:295
int ca_istrue
Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise...
Definition: x509.h:315
struct _x509_name * next
The next named information object.
Definition: x509.h:268
x509_buf oid
The object identifier.
Definition: x509.h:266
int ext_types
Bit string containing detected and parsed extensions.
Definition: x509.h:314