mbed TLS v1.3.19
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 /* \} name */
80 
85 /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */
86 #define BADCERT_EXPIRED 0x01
87 #define BADCERT_REVOKED 0x02
88 #define BADCERT_CN_MISMATCH 0x04
89 #define BADCERT_NOT_TRUSTED 0x08
90 #define BADCRL_NOT_TRUSTED 0x10
91 #define BADCRL_EXPIRED 0x20
92 #define BADCERT_MISSING 0x40
93 #define BADCERT_SKIP_VERIFY 0x80
94 #define BADCERT_OTHER 0x0100
95 #define BADCERT_FUTURE 0x0200
96 #define BADCRL_FUTURE 0x0400
97 #define BADCERT_KEY_USAGE 0x0800
98 #define BADCERT_EXT_KEY_USAGE 0x1000
99 #define BADCERT_NS_CERT_TYPE 0x2000
100 /* \} name */
101 /* \} addtogroup x509_module */
102 
103 /*
104  * X.509 v3 Key Usage Extension flags
105  */
106 #define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
107 #define KU_NON_REPUDIATION (0x40) /* bit 1 */
108 #define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
109 #define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
110 #define KU_KEY_AGREEMENT (0x08) /* bit 4 */
111 #define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
112 #define KU_CRL_SIGN (0x02) /* bit 6 */
113 
114 /*
115  * Netscape certificate types
116  * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
117  */
118 
119 #define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
120 #define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
121 #define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
122 #define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
123 #define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
124 #define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
125 #define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
126 #define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
127 
128 /*
129  * X.509 extension types
130  *
131  * Comments refer to the status for using certificates. Status can be
132  * different for writing certificates or reading CRLs or CSRs.
133  */
134 #define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
135 #define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
136 #define EXT_KEY_USAGE (1 << 2) /* Parsed but not used */
137 #define EXT_CERTIFICATE_POLICIES (1 << 3)
138 #define EXT_POLICY_MAPPINGS (1 << 4)
139 #define EXT_SUBJECT_ALT_NAME (1 << 5) /* Supported (DNS) */
140 #define EXT_ISSUER_ALT_NAME (1 << 6)
141 #define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
142 #define EXT_BASIC_CONSTRAINTS (1 << 8) /* Supported */
143 #define EXT_NAME_CONSTRAINTS (1 << 9)
144 #define EXT_POLICY_CONSTRAINTS (1 << 10)
145 #define EXT_EXTENDED_KEY_USAGE (1 << 11) /* Parsed but not used */
146 #define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
147 #define EXT_INIHIBIT_ANYPOLICY (1 << 13)
148 #define EXT_FRESHEST_CRL (1 << 14)
149 
150 #define EXT_NS_CERT_TYPE (1 << 16) /* Parsed (and then ?) */
151 
152 /*
153  * Storage format identifiers
154  * Recognized formats: PEM and DER
155  */
156 #define X509_FORMAT_DER 1
157 #define X509_FORMAT_PEM 2
158 
159 #define X509_MAX_DN_NAME_SIZE 256
161 #ifdef __cplusplus
162 extern "C" {
163 #endif
164 
178 
183 
189 
194 
196 typedef struct _x509_time
197 {
198  int year, mon, day;
199  int hour, min, sec;
200 }
201 x509_time;
202 
217 int x509_dn_gets( char *buf, size_t size, const x509_name *dn );
218 
230 int x509_serial_gets( char *buf, size_t size, const x509_buf *serial );
231 
232 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
233 #if defined(POLARSSL_DEPRECATED_WARNING)
234 #define DEPRECATED __attribute__((deprecated))
235 #else
236 #define DEPRECATED
237 #endif
238 
250 const char *x509_oid_get_description( x509_buf *oid ) DEPRECATED;
251 
264 int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid ) DEPRECATED;
265 #undef DEPRECATED
266 #endif /* POLARSSL_DEPRECATED_REMOVED */
267 
277 int x509_time_expired( const x509_time *time );
278 
288 int x509_time_future( const x509_time *time );
289 
295 int x509_self_test( int verbose );
296 
297 /*
298  * Internal module functions. You probably do not want to use these unless you
299  * know you do.
300  */
301 int x509_get_name( unsigned char **p, const unsigned char *end,
302  x509_name *cur );
303 int x509_get_alg_null( unsigned char **p, const unsigned char *end,
304  x509_buf *alg );
305 int x509_get_alg( unsigned char **p, const unsigned char *end,
306  x509_buf *alg, x509_buf *params );
307 #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
308 int x509_get_rsassa_pss_params( const x509_buf *params,
309  md_type_t *md_alg, md_type_t *mgf_md,
310  int *salt_len );
311 #endif
312 int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig );
313 int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
314  md_type_t *md_alg, pk_type_t *pk_alg,
315  void **sig_opts );
316 int x509_get_time( unsigned char **p, const unsigned char *end,
317  x509_time *time );
318 int x509_get_serial( unsigned char **p, const unsigned char *end,
319  x509_buf *serial );
320 int x509_get_ext( unsigned char **p, const unsigned char *end,
321  x509_buf *ext, int tag );
322 int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
323  pk_type_t pk_alg, md_type_t md_alg,
324  const void *sig_opts );
325 int x509_key_size_helper( char *buf, size_t size, const char *name );
326 int x509_string_to_names( asn1_named_data **head, const char *name );
327 int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len,
328  int critical, const unsigned char *val,
329  size_t val_len );
330 int x509_write_extensions( unsigned char **p, unsigned char *start,
331  asn1_named_data *first );
332 int x509_write_names( unsigned char **p, unsigned char *start,
333  asn1_named_data *first );
334 int x509_write_sig( unsigned char **p, unsigned char *start,
335  const char *oid, size_t oid_len,
336  unsigned char *sig, size_t size );
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 
342 #endif /* x509.h */
int x509_time_expired(const x509_time *time)
Check a given x509_time against the system time and check if it is not expired.
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:199
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:196
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:188
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:199
int mon
Definition: x509.h:198
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)
int x509_get_time(unsigned char **p, const unsigned char *end, x509_time *time)
Generic ASN.1 parsing.
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:198
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:198
const char * x509_oid_get_description(x509_buf *oid) DEPRECATED
Give an known OID, return its descriptive string.
int x509_get_ext(unsigned char **p, const unsigned char *end, x509_buf *ext, int tag)
int min
Definition: x509.h:199
int x509_write_sig(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, unsigned char *sig, size_t size)
int x509_time_future(const x509_time *time)
Check a given x509_time against the system time and check if it is not from the future.
asn1_buf x509_buf
Type-length-value structure that allows for ASN1 using DER.
Definition: x509.h:177
int x509_self_test(int verbose)
Checkup routine.
asn1_sequence x509_sequence
Container for a sequence of ASN.1 items.
Definition: x509.h:193
asn1_bitstring x509_bitstring
Container for ASN1 bit strings.
Definition: x509.h:182
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:236