blob: 15e097a15ab61bc988a5fbe70e30b7855ab34452 [file] [log] [blame]
Valerio Setti25b282e2024-01-17 10:55:32 +01001/**
2 * \file x509.h
3 *
4 * \brief Internal part of the public "x509.h".
5 */
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 */
10#ifndef MBEDTLS_X509_INTERNAL_H
11#define MBEDTLS_X509_INTERNAL_H
12#include "mbedtls/private_access.h"
13
14#include "mbedtls/build_info.h"
15
16#include "mbedtls/x509.h"
17#include "mbedtls/asn1.h"
Valerio Setti639d5672024-01-17 11:04:56 +010018#include "pk_internal.h"
Valerio Setti25b282e2024-01-17 10:55:32 +010019
20#if defined(MBEDTLS_RSA_C)
21#include "mbedtls/rsa.h"
22#endif
23
24/**
25 * \brief Return the next relative DN in an X509 name.
26 *
27 * \note Intended use is to compare function result to dn->next
28 * in order to detect boundaries of multi-valued RDNs.
29 *
30 * \param dn Current node in the X509 name
31 *
32 * \return Pointer to the first attribute-value pair of the
33 * next RDN in sequence, or NULL if end is reached.
34 */
35static inline mbedtls_x509_name *mbedtls_x509_dn_get_next(
36 mbedtls_x509_name *dn)
37{
38 while (dn->MBEDTLS_PRIVATE(next_merged) && dn->next != NULL) {
39 dn = dn->next;
40 }
41 return dn->next;
42}
43
44/**
45 * \brief Store the certificate serial in printable form into buf;
46 * no more than size characters will be written.
47 *
48 * \param buf Buffer to write to
49 * \param size Maximum size of buffer
50 * \param serial The X509 serial to represent
51 *
52 * \return The length of the string written (not including the
53 * terminated nul byte), or a negative error code.
54 */
55int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial);
56
57/**
58 * \brief Compare pair of mbedtls_x509_time.
59 *
60 * \param t1 mbedtls_x509_time to compare
61 * \param t2 mbedtls_x509_time to compare
62 *
63 * \return < 0 if t1 is before t2
64 * 0 if t1 equals t2
65 * > 0 if t1 is after t2
66 */
67int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2);
68
69#if defined(MBEDTLS_HAVE_TIME_DATE)
70/**
71 * \brief Fill mbedtls_x509_time with provided mbedtls_time_t.
72 *
73 * \param tt mbedtls_time_t to convert
74 * \param now mbedtls_x509_time to fill with converted mbedtls_time_t
75 *
76 * \return \c 0 on success
77 * \return A non-zero return value on failure.
78 */
79int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now);
80#endif /* MBEDTLS_HAVE_TIME_DATE */
81
82/**
83 * \brief Check a given mbedtls_x509_time against the system time
84 * and tell if it's in the past.
85 *
86 * \note Intended usage is "if( is_past( valid_to ) ) ERROR".
87 * Hence the return value of 1 if on internal errors.
88 *
89 * \param to mbedtls_x509_time to check
90 *
91 * \return 1 if the given time is in the past or an error occurred,
92 * 0 otherwise.
93 */
94int mbedtls_x509_time_is_past(const mbedtls_x509_time *to);
95
96/**
97 * \brief Check a given mbedtls_x509_time against the system time
98 * and tell if it's in the future.
99 *
100 * \note Intended usage is "if( is_future( valid_from ) ) ERROR".
101 * Hence the return value of 1 if on internal errors.
102 *
103 * \param from mbedtls_x509_time to check
104 *
105 * \return 1 if the given time is in the future or an error occurred,
106 * 0 otherwise.
107 */
108int mbedtls_x509_time_is_future(const mbedtls_x509_time *from);
109
110/**
111 * \brief This function parses an item in the SubjectAlternativeNames
112 * extension. Please note that this function might allocate
113 * additional memory for a subject alternative name, thus
114 * mbedtls_x509_free_subject_alt_name has to be called
115 * to dispose of this additional memory afterwards.
116 *
117 * \param san_buf The buffer holding the raw data item of the subject
118 * alternative name.
119 * \param san The target structure to populate with the parsed presentation
120 * of the subject alternative name encoded in \p san_buf.
121 *
122 * \note Supported GeneralName types, as defined in RFC 5280:
123 * "rfc822Name", "dnsName", "directoryName",
124 * "uniformResourceIdentifier" and "hardware_module_name"
125 * of type "otherName", as defined in RFC 4108.
126 *
127 * \note This function should be called on a single raw data of
128 * subject alternative name. For example, after successful
129 * certificate parsing, one must iterate on every item in the
130 * \c crt->subject_alt_names sequence, and pass it to
131 * this function.
132 *
133 * \warning The target structure contains pointers to the raw data of the
134 * parsed certificate, and its lifetime is restricted by the
135 * lifetime of the certificate.
136 *
137 * \return \c 0 on success
138 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
139 * SAN type.
140 * \return Another negative value for any other failure.
141 */
142int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
143 mbedtls_x509_subject_alternative_name *san);
144/**
145 * \brief Unallocate all data related to subject alternative name
146 *
147 * \param san SAN structure - extra memory owned by this structure will be freed
148 */
149void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san);
150
151int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
152 mbedtls_x509_name *cur);
153int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
154 mbedtls_x509_buf *alg);
155int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
156 mbedtls_x509_buf *alg, mbedtls_x509_buf *params);
157#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
158int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
159 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
160 int *salt_len);
161#endif
162int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig);
163int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
164 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
165 void **sig_opts);
166int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
167 mbedtls_x509_time *t);
168int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
169 mbedtls_x509_buf *serial);
170int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
171 mbedtls_x509_buf *ext, int tag);
172#if !defined(MBEDTLS_X509_REMOVE_INFO)
173int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
174 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
175 const void *sig_opts);
176#endif
177int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name);
178int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
179 int critical, const unsigned char *val,
180 size_t val_len);
181int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
182 mbedtls_asn1_named_data *first);
183int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
184 mbedtls_asn1_named_data *first);
185int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
186 const char *oid, size_t oid_len,
187 unsigned char *sig, size_t size,
188 mbedtls_pk_type_t pk_alg);
189int mbedtls_x509_get_ns_cert_type(unsigned char **p,
190 const unsigned char *end,
191 unsigned char *ns_cert_type);
192int mbedtls_x509_get_key_usage(unsigned char **p,
193 const unsigned char *end,
194 unsigned int *key_usage);
195int mbedtls_x509_get_subject_alt_name(unsigned char **p,
196 const unsigned char *end,
197 mbedtls_x509_sequence *subject_alt_name);
198int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
199 const unsigned char *end,
200 mbedtls_x509_sequence *subject_alt_name);
201int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
202 const mbedtls_x509_sequence
203 *subject_alt_name,
204 const char *prefix);
205int mbedtls_x509_info_cert_type(char **buf, size_t *size,
206 unsigned char ns_cert_type);
207int mbedtls_x509_info_key_usage(char **buf, size_t *size,
208 unsigned int key_usage);
209
210int mbedtls_x509_write_set_san_common(mbedtls_asn1_named_data **extensions,
211 const mbedtls_x509_san_list *san_list);
212
213#endif /* MBEDTLS_X509_INTERNAL_H */