OpenVPN
ssl_verify_backend.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2021 Fox Crypto B.V. <openvpn@foxcrypto.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
30#ifndef SSL_VERIFY_BACKEND_H_
31#define SSL_VERIFY_BACKEND_H_
32
36typedef enum { SUCCESS = 0, FAILURE = 1 } result_t;
37
38/*
39 * Backend support functions.
40 *
41 * The following functions are needed by the backend, but defined in the main
42 * file.
43 */
44
45/*
46 * Verify certificate for the given session. Performs OpenVPN-specific
47 * verification.
48 *
49 * This function must be called for every certificate in the certificate
50 * chain during the certificate verification stage of the handshake.
51 *
52 * @param session TLS Session associated with this tunnel
53 * @param cert Certificate to process
54 * @param cert_depth Depth of the current certificate
55 *
56 * @return \c SUCCESS if verification was successful, \c FAILURE on failure.
57 */
58result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth);
59
60/*
61 * Remember the given certificate hash, allowing the certificate chain to be
62 * locked between sessions.
63 *
64 * Must be called for every certificate in the verification chain, whether it
65 * is valid or not.
66 *
67 * @param session TLS Session associated with this tunnel
68 * @param cert_depth Depth of the current certificate
69 * @param cert_hash Hash of the current certificate
70 */
71void cert_hash_remember(struct tls_session *session, const int cert_depth,
72 const struct buffer *cert_hash);
73
74/*
75 * Library-specific functions.
76 *
77 * The following functions must be implemented on a library-specific basis.
78 */
79
80/*
81 * Retrieve certificate's subject name.
82 *
83 * @param cert Certificate to retrieve the subject from.
84 * @param gc Garbage collection arena to use when allocating string.
85 *
86 * @return a string containing the subject
87 */
89
99 struct gc_arena *gc);
100
110 struct gc_arena *gc);
111
112/*
113 * Retrieve the certificate's username from the specified field.
114 *
115 * If the field is prepended with ext: and ENABLE_X509ALTUSERNAME is enabled,
116 * it will be loaded from an X.509 extension
117 *
118 * @param cn Buffer to return the common name in.
119 * @param cn_len Length of the cn buffer.
120 * @param x509_username_field Name of the field to load from
121 * @param cert Certificate to retrieve the common name from.
122 *
123 * @return \c FAILURE, \c or SUCCESS
124 */
125result_t backend_x509_get_username(char *common_name, int cn_len,
126 char *x509_username_field, openvpn_x509_cert_t *peer_cert);
127
128#ifdef ENABLE_X509ALTUSERNAME
133bool x509_username_field_ext_supported(const char *extname);
134
135#endif
136
137/*
138 * Return the certificate's serial number in decimal string representation.
139 *
140 * The serial number is returned as a string, since it might be a bignum.
141 *
142 * @param cert Certificate to retrieve the serial number from.
143 * @param gc Garbage collection arena to use when allocating string.
144 *
145 * @return String representation of the certificate's serial number
146 * in decimal notation, or NULL on error.
147 */
149
150/*
151 * Return the certificate's serial number in hex string representation.
152 *
153 * The serial number is returned as a string, since it might be a bignum.
154 *
155 * @param cert Certificate to retrieve the serial number from.
156 * @param gc Garbage collection arena to use when allocating string.
157 *
158 * @return String representation of the certificate's serial number
159 * in hex notation, or NULL on error.
160 */
162 struct gc_arena *gc);
163
164/*
165 * Write the certificate to the file in PEM format.
166 *
167 *
168 * @param cert Certificate to serialise.
169 *
170 * @return \c FAILURE, \c or SUCCESS
171 */
173 const char *filename);
174
175/*
176 * Save X509 fields to environment, using the naming convention:
177 *
178 * X509_{cert_depth}_{name}={value}
179 *
180 * @param es Environment set to save variables in
181 * @param cert_depth Depth of the certificate
182 * @param cert Certificate to set the environment for
183 */
184void x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert);
185
186/*
187 * Start tracking the given attribute.
188 *
189 * The tracked attributes are stored in ll_head.
190 *
191 * @param ll_head The x509_track to store tracked attributes in
192 * @param name Name of the attribute to track
193 * @param msglevel Message level for errors
194 * @param gc Garbage collection arena for temp data
195 *
196 */
197void x509_track_add(const struct x509_track **ll_head, const char *name,
198 int msglevel, struct gc_arena *gc);
199
200/*
201 * Save X509 fields to environment, using the naming convention:
202 *
203 * X509_{cert_depth}_{name}={value}
204 *
205 * This function differs from setenv_x509 below in the following ways:
206 *
207 * (1) Only explicitly named attributes in xt are saved, per usage
208 * of --x509-track program options.
209 * (2) Only the level 0 cert info is saved unless the XT_FULL_CHAIN
210 * flag is set in xt->flags (corresponds with prepending a '+'
211 * to the name when specified by --x509-track program option).
212 * (3) This function supports both X509 subject name fields as
213 * well as X509 V3 extensions.
214 *
215 * @param xt
216 * @param es Environment set to save variables in
217 * @param cert_depth Depth of the certificate
218 * @param cert Certificate to set the environment for
219 */
220void x509_setenv_track(const struct x509_track *xt, struct env_set *es,
221 const int depth, openvpn_x509_cert_t *x509);
222
223/*
224 * Check X.509 Netscape certificate type field, if available.
225 *
226 * @param cert Certificate to check.
227 * @param usage One of \c NS_CERT_CHECK_CLIENT, \c NS_CERT_CHECK_SERVER,
228 * or \c NS_CERT_CHECK_NONE.
229 *
230 * @return \c SUCCESS if NS_CERT_CHECK_NONE or if the certificate has
231 * the expected bit set. \c FAILURE if the certificate does
232 * not have NS cert type verification or the wrong bit set.
233 */
235
236/*
237 * Verify X.509 key usage extension field.
238 *
239 * @param cert Certificate to check.
240 * @param expected_ku Array of valid key usage values
241 * @param expected_len Length of the key usage array
242 *
243 * @return \c SUCCESS if one of the key usage values matches, \c FAILURE
244 * if key usage is not enabled, or the values do not match.
245 */
246result_t x509_verify_cert_ku(openvpn_x509_cert_t *x509, const unsigned *const expected_ku,
247 int expected_len);
248
249/*
250 * Verify X.509 extended key usage extension field.
251 *
252 * @param cert Certificate to check.
253 * @param expected_oid String representation of the expected Object ID. May be
254 * either the string representation of the numeric OID
255 * (e.g. \c "1.2.3.4", or the descriptive string matching
256 * the OID.
257 *
258 * @return \c SUCCESS if one of the expected OID matches one of the
259 * extended key usage fields, \c FAILURE if extended key
260 * usage is not enabled, or the values do not match.
261 */
262result_t x509_verify_cert_eku(openvpn_x509_cert_t *x509, const char *const expected_oid);
263
270bool tls_verify_crl_missing(const struct tls_options *opt);
271
272#endif /* SSL_VERIFY_BACKEND_H_ */
static void usage(void)
Definition options.c:4934
struct buffer x509_get_sha256_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate's SHA256 fingerprint.
void cert_hash_remember(struct tls_session *session, const int cert_depth, const struct buffer *cert_hash)
Definition ssl_verify.c:197
void x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, openvpn_x509_cert_t *x509)
void x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert)
bool tls_verify_crl_missing(const struct tls_options *opt)
Return true iff a CRL is configured, but is not loaded.
result_t backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename)
result_t x509_verify_ns_cert_type(openvpn_x509_cert_t *cert, const int usage)
result_t backend_x509_get_username(char *common_name, int cn_len, char *x509_username_field, openvpn_x509_cert_t *peer_cert)
char * backend_x509_get_serial_hex(openvpn_x509_cert_t *cert, struct gc_arena *gc)
result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
Definition ssl_verify.c:590
result_t x509_verify_cert_ku(openvpn_x509_cert_t *x509, const unsigned *const expected_ku, int expected_len)
struct buffer x509_get_sha1_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate's SHA1 fingerprint.
char * x509_get_subject(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Definition test_pkcs11.c:69
char * backend_x509_get_serial(openvpn_x509_cert_t *cert, struct gc_arena *gc)
void x509_track_add(const struct x509_track **ll_head, const char *name, int msglevel, struct gc_arena *gc)
result_t
Result of verification function.
@ FAILURE
@ SUCCESS
result_t x509_verify_cert_eku(openvpn_x509_cert_t *x509, const char *const expected_oid)
mbedtls_x509_crt openvpn_x509_cert_t
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
Structure containing the hash for a single certificate.
Definition ssl_verify.h:55
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:480
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:155