mbed TLS v1.3.21
x509.h
Go to the documentation of this file.
1 
24 #ifndef POLARSSL_X509_H
25 #define POLARSSL_X509_H
26 
27 #if !defined(POLARSSL_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include POLARSSL_CONFIG_FILE
31 #endif
32 
33 #include "asn1.h"
34 #include "pk.h"
35 
36 #if defined(POLARSSL_RSA_C)
37 #include "rsa.h"
38 #endif
39 
45 #if !defined(POLARSSL_X509_MAX_INTERMEDIATE_CA)
46 
54 #define POLARSSL_X509_MAX_INTERMEDIATE_CA 8
55 #endif
56 
61 #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080
62 #define POLARSSL_ERR_X509_UNKNOWN_OID -0x2100
63 #define POLARSSL_ERR_X509_INVALID_FORMAT -0x2180
64 #define POLARSSL_ERR_X509_INVALID_VERSION -0x2200
65 #define POLARSSL_ERR_X509_INVALID_SERIAL -0x2280
66 #define POLARSSL_ERR_X509_INVALID_ALG -0x2300
67 #define POLARSSL_ERR_X509_INVALID_NAME -0x2380
68 #define POLARSSL_ERR_X509_INVALID_DATE -0x2400
69 #define POLARSSL_ERR_X509_INVALID_SIGNATURE -0x2480
70 #define POLARSSL_ERR_X509_INVALID_EXTENSIONS -0x2500
71 #define POLARSSL_ERR_X509_UNKNOWN_VERSION -0x2580
72 #define POLARSSL_ERR_X509_UNKNOWN_SIG_ALG -0x2600
73 #define POLARSSL_ERR_X509_SIG_MISMATCH -0x2680
74 #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2700
75 #define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780
76 #define POLARSSL_ERR_X509_BAD_INPUT_DATA -0x2800
77 #define POLARSSL_ERR_X509_MALLOC_FAILED -0x2880
78 #define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2900
79 #define POLARSSL_ERR_X509_FATAL_ERROR -0x3000
80 /* \} name */
81 
86 /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */
87 #define BADCERT_EXPIRED 0x01
88 #define BADCERT_REVOKED 0x02
89 #define BADCERT_CN_MISMATCH 0x04
90 #define BADCERT_NOT_TRUSTED 0x08
91 #define BADCRL_NOT_TRUSTED 0x10
92 #define BADCRL_EXPIRED 0x20
93 #define BADCERT_MISSING 0x40
94 #define BADCERT_SKIP_VERIFY 0x80
95 #define BADCERT_OTHER 0x0100
96 #define BADCERT_FUTURE 0x0200
97 #define BADCRL_FUTURE 0x0400
98 #define BADCERT_KEY_USAGE 0x0800
99 #define BADCERT_EXT_KEY_USAGE 0x1000
100 #define BADCERT_NS_CERT_TYPE 0x2000
101 #define BADCERT_BAD_KEY 0x10000
103 /* \} name */
104 /* \} addtogroup x509_module */
105 
106 /*
107  * X.509 v3 Key Usage Extension flags
108  */
109 #define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
110 #define KU_NON_REPUDIATION (0x40) /* bit 1 */
111 #define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
112 #define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
113 #define KU_KEY_AGREEMENT (0x08) /* bit 4 */
114 #define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
115 #define KU_CRL_SIGN (0x02) /* bit 6 */
116 
117 /*
118  * Netscape certificate types
119  * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
120  */
121 
122 #define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
123 #define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
124 #define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
125 #define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
126 #define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
127 #define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
128 #define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
129 #define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
130 
131 /*
132  * X.509 extension types
133  *
134  * Comments refer to the status for using certificates. Status can be
135  * different for writing certificates or reading CRLs or CSRs.
136  */
137 #define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
138 #define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
139 #define EXT_KEY_USAGE (1 << 2) /* Parsed but not used */
140 #define EXT_CERTIFICATE_POLICIES (1 << 3)
141 #define EXT_POLICY_MAPPINGS (1 << 4)
142 #define EXT_SUBJECT_ALT_NAME (1 << 5) /* Supported (DNS) */
143 #define EXT_ISSUER_ALT_NAME (1 << 6)
144 #define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
145 #define EXT_BASIC_CONSTRAINTS (1 << 8) /* Supported */
146 #define EXT_NAME_CONSTRAINTS (1 << 9)
147 #define EXT_POLICY_CONSTRAINTS (1 << 10)
148 #define EXT_EXTENDED_KEY_USAGE (1 << 11) /* Parsed but not used */
149 #define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
150 #define EXT_INIHIBIT_ANYPOLICY (1 << 13)
151 #define EXT_FRESHEST_CRL (1 << 14)
152 
153 #define EXT_NS_CERT_TYPE (1 << 16) /* Parsed (and then ?) */
154 
155 /*
156  * Storage format identifiers
157  * Recognized formats: PEM and DER
158  */
159 #define X509_FORMAT_DER 1
160 #define X509_FORMAT_PEM 2
161 
162 #define X509_MAX_DN_NAME_SIZE 256
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
167 
181 
186 
192 
197 
199 typedef struct _x509_time
200 {
201  int year, mon, day;
202  int hour, min, sec;
203 }
204 x509_time;
205 
220 int x509_dn_gets( char *buf, size_t size, const x509_name *dn );
221 
233 int x509_serial_gets( char *buf, size_t size, const x509_buf *serial );
234 
235 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
236 #if defined(POLARSSL_DEPRECATED_WARNING)
237 #define DEPRECATED __attribute__((deprecated))
238 #else
239 #define DEPRECATED
240 #endif
241 
253 const char *x509_oid_get_description( x509_buf *oid ) DEPRECATED;
254 
267 int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid ) DEPRECATED;
268 #undef DEPRECATED
269 #endif /* POLARSSL_DEPRECATED_REMOVED */
270 
280 int x509_time_expired( const x509_time *tm );
281 
291 int x509_time_future( const x509_time *tm );
292 
298 int x509_self_test( int verbose );
299 
300 /*
301  * Internal module functions. You probably do not want to use these unless you
302  * know you do.
303  */
304 int x509_get_name( unsigned char **p, const unsigned char *end,
305  x509_name *cur );
306 int x509_get_alg_null( unsigned char **p, const unsigned char *end,
307  x509_buf *alg );
308 int x509_get_alg( unsigned char **p, const unsigned char *end,
309  x509_buf *alg, x509_buf *params );
310 #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
311 int x509_get_rsassa_pss_params( const x509_buf *params,
312  md_type_t *md_alg, md_type_t *mgf_md,
313  int *salt_len );
314 #endif
315 int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig );
316 int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
317  md_type_t *md_alg, pk_type_t *pk_alg,
318  void **sig_opts );
319 int x509_get_time( unsigned char **p, const unsigned char *end,
320  x509_time *t );
321 int x509_get_serial( unsigned char **p, const unsigned char *end,
322  x509_buf *serial );
323 int x509_get_ext( unsigned char **p, const unsigned char *end,
324  x509_buf *ext, int tag );
325 int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
326  pk_type_t pk_alg, md_type_t md_alg,
327  const void *sig_opts );
328 int x509_key_size_helper( char *buf, size_t size, const char *name );
329 int x509_string_to_names( asn1_named_data **head, const char *name );
330 int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len,
331  int critical, const unsigned char *val,
332  size_t val_len );
333 int x509_write_extensions( unsigned char **p, unsigned char *start,
334  asn1_named_data *first );
335 int x509_write_names( unsigned char **p, unsigned char *start,
336  asn1_named_data *first );
337 int x509_write_sig( unsigned char **p, unsigned char *start,
338  const char *oid, size_t oid_len,
339  unsigned char *sig, size_t size );
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 
345 #endif /* x509.h */
int x509_set_extension(asn1_named_data **head, const char *oid, size_t oid_len, int critical, const unsigned char *val, size_t val_len)
int x509_string_to_names(asn1_named_data **head, const char *name)
struct _x509_time x509_time
Container for date and time (precision in seconds).
int sec
Time.
Definition: x509.h:202
int x509_get_name(unsigned char **p, const unsigned char *end, x509_name *cur)
int x509_get_serial(unsigned char **p, const unsigned char *end, x509_buf *serial)
int x509_get_sig_alg(const x509_buf *sig_oid, const x509_buf *sig_params, md_type_t *md_alg, pk_type_t *pk_alg, void **sig_opts)
int x509_key_size_helper(char *buf, size_t size, const char *name)
int x509_get_alg_null(unsigned char **p, const unsigned char *end, x509_buf *alg)
Container for date and time (precision in seconds).
Definition: x509.h:199
Configuration options (set of defines)
int x509_get_sig(unsigned char **p, const unsigned char *end, x509_buf *sig)
Container for ASN1 bit strings.
Definition: asn1.h:132
Public Key abstraction layer.
asn1_named_data x509_name
Container for ASN1 named information objects.
Definition: x509.h:191
int x509_get_alg(unsigned char **p, const unsigned char *end, x509_buf *alg, x509_buf *params)
md_type_t
Definition: md.h:45
int hour
Definition: x509.h:202
int mon
Definition: x509.h:201
Container for a sequence of ASN.1 items.
Definition: asn1.h:143
int x509_write_names(unsigned char **p, unsigned char *start, asn1_named_data *first)
Generic ASN.1 parsing.
int x509_time_expired(const x509_time *tm)
Check a given x509_time against the system time and check if it is not expired.
int x509_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 x509_write_extensions(unsigned char **p, unsigned char *start, asn1_named_data *first)
int x509_oid_get_numeric_string(char *buf, size_t size, x509_buf *oid) DEPRECATED
Give an OID, return a string version of its OID number.
int day
Date.
Definition: x509.h:201
pk_type_t
Public key types.
Definition: pk.h:92
int x509_sig_alg_gets(char *buf, size_t size, const x509_buf *sig_oid, pk_type_t pk_alg, md_type_t md_alg, const void *sig_opts)
int x509_get_rsassa_pss_params(const x509_buf *params, md_type_t *md_alg, md_type_t *mgf_md, int *salt_len)
Container for a sequence or list of 'named' ASN.1 data items.
Definition: asn1.h:153
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:121
The RSA public-key cryptosystem.
int year
Definition: x509.h:201
const char * x509_oid_get_description(x509_buf *oid) DEPRECATED
Give an known OID, return its descriptive string.
int x509_get_time(unsigned char **p, const unsigned char *end, x509_time *t)
int x509_get_ext(unsigned char **p, const unsigned char *end, x509_buf *ext, int tag)
int min
Definition: x509.h:202
int x509_write_sig(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, unsigned char *sig, size_t size)
asn1_buf x509_buf
Type-length-value structure that allows for ASN1 using DER.
Definition: x509.h:180
int x509_self_test(int verbose)
Checkup routine.
int x509_time_future(const x509_time *tm)
Check a given x509_time against the system time and check if it is not from the future.
asn1_sequence x509_sequence
Container for a sequence of ASN.1 items.
Definition: x509.h:196
asn1_bitstring x509_bitstring
Container for ASN1 bit strings.
Definition: x509.h:185
int x509_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...
#define DEPRECATED
Definition: x509.h:239