OpenVPN
ssl.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 OPENVPN_SSL_H
31#define OPENVPN_SSL_H
32
33#include "basic.h"
34#include "common.h"
35#include "crypto.h"
36#include "packet_id.h"
37#include "session_id.h"
38#include "reliable.h"
39#include "socket.h"
40#include "mtu.h"
41#include "options.h"
42#include "plugin.h"
43
44#include "ssl_common.h"
45#include "ssl_backend.h"
46#include "ssl_pkt.h"
47#include "tls_crypt.h"
48
49/* Used in the TLS PRF function */
50#define KEY_EXPANSION_ID "OpenVPN"
51
52/*
53 * Set the max number of acknowledgments that can "hitch a ride" on an outgoing
54 * non-P_ACK_V1 control packet.
55 */
56#define CONTROL_SEND_ACK_MAX 4
57
58/*
59 * Various timeouts
60 */
61#define TLS_MULTI_REFRESH 15 /* call tls_multi_process once every n seconds */
62#define TLS_MULTI_HORIZON 2 /* call tls_multi_process frequently for n seconds after
63 * every packet sent/received action */
64
65/*
66 * Buffer sizes (also see mtu.h).
67 */
68
69/* Maximum length of OCC options string passed as part of auth handshake */
70#define TLS_OPTIONS_LEN 512
71
72/* Definitions of the bits in the IV_PROTO bitfield
73 *
74 * In older OpenVPN versions this used in a comparison
75 * IV_PROTO >= 2 to determine if DATA_V2 is supported.
76 * Therefore any client announcing any of the flags must
77 * also announce IV_PROTO_DATA_V2. We also treat bit 0
78 * as reserved for this reason */
79
81#define IV_PROTO_DATA_V2 (1<<1)
82
85#define IV_PROTO_REQUEST_PUSH (1<<2)
86
88#define IV_PROTO_TLS_KEY_EXPORT (1<<3)
89
91#define IV_PROTO_AUTH_PENDING_KW (1<<4)
92
96#define IV_PROTO_NCP_P2P (1<<5)
97
99#define IV_PROTO_DNS_OPTION (1<<6)
100
103#define IV_PROTO_CC_EXIT_NOTIFY (1<<7)
104
106#define IV_PROTO_AUTH_FAIL_TEMP (1<<8)
107
109#define IV_PROTO_DYN_TLS_CRYPT (1<<9)
110
112#define IV_PROTO_DATA_EPOCH (1<<10)
113
115#define IV_PROTO_DNS_OPTION_V2 (1<<11)
116
117/* Default field in X509 to be username */
118#define X509_USERNAME_FIELD_DEFAULT "CN"
120#define KEY_METHOD_2 2
121
122/* key method taken from lower 4 bits */
123#define KEY_METHOD_MASK 0x0F
124
125/*
126 * Measure success rate of TLS handshakes, for debugging only
127 */
128/* #define MEASURE_TLS_HANDSHAKE_STATS */
129
130/*
131 * Prepare the SSL library for use
132 */
133void init_ssl_lib(void);
134
135/*
136 * Free any internal state that the SSL library might have
137 */
138void free_ssl_lib(void);
139
144void init_ssl(const struct options *options, struct tls_root_ctx *ctx, bool in_chroot);
145
167
182void tls_multi_init_finalize(struct tls_multi *multi, int tls_mtu);
183
184/*
185 * Initialize a standalone tls-auth verification object.
186 */
188 struct gc_arena *gc);
189
195
196/*
197 * Setups the control channel frame size parameters from the data channel
198 * parameters
199 */
200void tls_init_control_channel_frame_parameters(struct frame *frame, int tls_mtu);
201
202/*
203 * Set local and remote option compatibility strings.
204 * Used to verify compatibility of local and remote option
205 * sets.
206 */
207void tls_multi_init_set_options(struct tls_multi *multi,
208 const char *local,
209 const char *remote);
210
223void tls_multi_free(struct tls_multi *multi, bool clear);
224
229#define TLSMP_INACTIVE 0
230#define TLSMP_ACTIVE 1
231#define TLSMP_KILL 2
232#define TLSMP_RECONNECT 3
233
234/*
235 * Called by the top-level event loop.
236 *
237 * Basically decides if we should call tls_process for
238 * the active or untrusted sessions.
239 */
240int tls_multi_process(struct tls_multi *multi,
241 struct buffer *to_link,
242 struct link_socket_actual **to_link_addr,
243 struct link_socket_info *to_link_socket_info,
244 interval_t *wakeup);
245
246
247/**************************************************************************/
299bool tls_pre_decrypt(struct tls_multi *multi,
300 const struct link_socket_actual *from,
301 struct buffer *buf,
302 struct crypto_options **opt,
303 bool floated,
304 const uint8_t **ad_start);
305
306
307/**************************************************************************/
325void tls_pre_encrypt(struct tls_multi *multi,
326 struct buffer *buf, struct crypto_options **opt);
327
337struct key_state *tls_select_encryption_key(struct tls_multi *multi);
338
351void
352tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf);
353
370void
371tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf);
372
380void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf);
381
384/*
385 * Setup private key file password. If auth_file is given, use the
386 * credentials stored in the file.
387 */
388void pem_password_setup(const char *auth_file);
389
390/* Enables the use of user/password authentication */
391void enable_auth_user_pass(void);
392
393/*
394 * Setup authentication username and password. If auth_file is given, use the
395 * credentials stored in the file, however, if is_inline is true then auth_file
396 * contains the username/password inline.
397 */
398void auth_user_pass_setup(const char *auth_file, bool is_inline,
399 const struct static_challenge_info *sc_info);
400
401/*
402 * Ensure that no caching is performed on authentication information
403 */
404void ssl_set_auth_nocache(void);
405
406/*
407 * Getter method for retrieving the auth-nocache option.
408 */
409bool ssl_get_auth_nocache(void);
410
411/*
412 * Purge any stored authentication information, both for key files and tunnel
413 * authentication. If PCKS #11 is enabled, purge authentication for that too.
414 * Note that auth_token is not cleared.
415 */
416void ssl_purge_auth(const bool auth_user_pass_only);
417
418void ssl_set_auth_token(const char *token);
419
420void ssl_set_auth_token_user(const char *username);
421
422bool ssl_clean_auth_token(void);
423
424#ifdef ENABLE_MANAGEMENT
425
426void ssl_purge_auth_challenge(void);
427
428void ssl_put_auth_challenge(const char *cr_str);
429
430#endif
431
432/*
433 * Send a payload over the TLS control channel
434 */
435bool tls_send_payload(struct key_state *ks,
436 const uint8_t *data,
437 int size);
438
439/*
440 * Receive a payload through the TLS control channel
441 */
442bool tls_rec_payload(struct tls_multi *multi,
443 struct buffer *buf);
444
451void tls_update_remote_addr(struct tls_multi *multi,
452 const struct link_socket_actual *addr);
453
473 struct tls_session *session,
474 struct options *options,
475 struct frame *frame,
476 struct frame *frame_fragment,
477 struct link_socket_info *lsi,
478 dco_context_t *dco);
479
480/*
481 * inline functions
482 */
483
485static inline void
486tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
487{
488 if (packet_id_initialized(&tls_wrap->opt.packet_id))
489 {
490 packet_id_free(&tls_wrap->opt.packet_id);
491 }
492
493 if (tls_wrap->cleanup_key_ctx)
494 {
495 free_key_ctx_bi(&tls_wrap->opt.key_ctx_bi);
496 }
497
499 free_buf(&tls_wrap->work);
500 secure_memzero(&tls_wrap->original_wrap_keydata, sizeof(tls_wrap->original_wrap_keydata));
501}
502
503static inline bool
504tls_initial_packet_received(const struct tls_multi *multi)
505{
506 return multi->n_sessions > 0;
507}
508
509static inline int
510tls_test_payload_len(const struct tls_multi *multi)
511{
512 if (multi)
513 {
514 const struct key_state *ks = get_primary_key(multi);
515 if (ks->state >= S_ACTIVE)
516 {
517 return BLEN(&ks->plaintext_read_buf);
518 }
519 }
520 return 0;
521}
522
523static inline void
525{
526 if (multi)
527 {
528 multi->opt.single_session = true;
529 }
530}
531
532/*
533 * protocol_dump() flags
534 */
535#define PD_TLS_AUTH_HMAC_SIZE_MASK 0xFF
536#define PD_SHOW_DATA (1<<8)
537#define PD_TLS (1<<9)
538#define PD_VERBOSE (1<<10)
539#define PD_TLS_CRYPT (1<<11)
540
541const char *protocol_dump(struct buffer *buffer,
542 unsigned int flags,
543 struct gc_arena *gc);
544
545/*
546 * debugging code
547 */
548
549#ifdef MEASURE_TLS_HANDSHAKE_STATS
550void show_tls_performance_stats(void);
551
552#endif
553
559bool is_hard_reset_method2(int op);
560
561/*
562 * Show the TLS ciphers that are available for us to use in the SSL
563 * library with headers hinting their usage and warnings about usage.
564 *
565 * @param cipher_list list of allowed TLS cipher, or NULL.
566 * @param cipher_list_tls13 list of allowed TLS 1.3+ cipher, or NULL
567 * @param tls_cert_profile TLS certificate crypto profile name.
568 */
569void
570show_available_tls_ciphers(const char *cipher_list,
571 const char *cipher_list_tls13,
572 const char *tls_cert_profile);
573
574
581bool
583 struct tls_session *session);
584
585void
586tls_session_soft_reset(struct tls_multi *multi);
587
591void
593
594/* Special method to skip the three way handshake RESET stages. This is
595 * used by the HMAC code when seeing a packet that matches the previous
596 * HMAC based stateless server state */
597bool
600 struct link_socket_actual *from);
601
602#endif /* ifndef OPENVPN_SSL_H */
void free_buf(struct buffer *buf)
Definition buffer.c:183
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:414
#define BLEN(buf)
Definition buffer.h:127
int interval_t
Definition common.h:36
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition crypto.c:1125
Data Channel Cryptography Module.
void * dco_context_t
Definition dco.h:267
#define S_ACTIVE
Operational key_state state immediately after negotiation has completed while still within the handsh...
Definition ssl_common.h:97
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition ssl.c:1201
void tls_init_control_channel_frame_parameters(struct frame *frame, int tls_mtu)
Definition ssl.c:142
void tls_multi_free(struct tls_multi *multi, bool clear)
Cleanup a tls_multi structure and free associated memory allocations.
Definition ssl.c:1256
struct tls_multi * tls_multi_init(struct tls_options *tls_options)
Allocate and initialize a tls_multi structure.
Definition ssl.c:1172
void tls_multi_init_finalize(struct tls_multi *multi, int tls_mtu)
Finalize initialization of a tls_multi structure.
Definition ssl.c:1187
void tls_auth_standalone_free(struct tls_auth_standalone *tas)
Frees a standalone tls-auth verification object.
Definition ssl.c:1227
void tls_multi_init_set_options(struct tls_multi *multi, const char *local, const char *remote)
Definition ssl.c:1243
void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
Perform some accounting for the key state used.
Definition ssl.c:4136
struct key_state * tls_select_encryption_key(struct tls_multi *multi)
Selects the primary encryption that should be used to encrypt data of an outgoing packet.
Definition ssl.c:4047
void tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf)
Prepend a one-byte OpenVPN data channel P_DATA_V1 opcode to the packet.
Definition ssl.c:4107
void tls_pre_encrypt(struct tls_multi *multi, struct buffer *buf, struct crypto_options **opt)
Choose the appropriate security parameters with which to process an outgoing packet.
Definition ssl.c:4074
void tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf)
Prepend an OpenVPN data channel P_DATA_V2 header to the packet.
Definition ssl.c:4121
bool tls_pre_decrypt(struct tls_multi *multi, const struct link_socket_actual *from, struct buffer *buf, struct crypto_options **opt, bool floated, const uint8_t **ad_start)
Determine whether an incoming packet is a data channel or control channel packet, and process accordi...
Definition ssl.c:3682
void packet_id_free(struct packet_id *p)
Definition packet_id.c:127
static bool packet_id_initialized(const struct packet_id *pid)
Is this struct packet_id initialized?
Definition packet_id.h:275
Reliability Layer module header file.
void ssl_purge_auth(const bool auth_user_pass_only)
Definition ssl.c:392
void ssl_set_auth_token_user(const char *username)
Definition ssl.c:372
void tls_session_soft_reset(struct tls_multi *multi)
Definition ssl.c:1842
void load_xkey_provider(void)
Load ovpn.xkey provider used for external key signing.
void ssl_set_auth_nocache(void)
Definition ssl.c:347
static void tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
Free the elements of a tls_wrap_ctx structure.
Definition ssl.h:485
void ssl_put_auth_challenge(const char *cr_str)
Definition ssl.c:417
void auth_user_pass_setup(const char *auth_file, bool is_inline, const struct static_challenge_info *sc_info)
Definition ssl.c:295
void tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
Updates remote address in TLS sessions.
Definition ssl.c:4216
bool tls_send_payload(struct key_state *ks, const uint8_t *data, int size)
Definition ssl.c:4156
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition ssl.c:4190
bool tls_session_generate_data_channel_keys(struct tls_multi *multi, struct tls_session *session)
Generate data channel keys for the supplied TLS session.
Definition ssl.c:1597
static bool tls_initial_packet_received(const struct tls_multi *multi)
Definition ssl.h:503
bool is_hard_reset_method2(int op)
Given a key_method, return true if opcode represents the one of the hard_reset op codes for key-metho...
Definition ssl.c:788
bool session_skip_to_pre_start(struct tls_session *session, struct tls_pre_decrypt_state *state, struct link_socket_actual *from)
Definition ssl.c:2580
void ssl_set_auth_token(const char *token)
Definition ssl.c:366
int tls_multi_process(struct tls_multi *multi, struct buffer *to_link, struct link_socket_actual **to_link_addr, struct link_socket_info *to_link_socket_info, interval_t *wakeup)
Definition ssl.c:3309
static void tls_set_single_session(struct tls_multi *multi)
Definition ssl.h:523
bool ssl_get_auth_nocache(void)
Definition ssl.c:357
void pem_password_setup(const char *auth_file)
Definition ssl.c:251
void init_ssl_lib(void)
Definition ssl.c:228
bool tls_session_update_crypto_params(struct tls_multi *multi, struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment, struct link_socket_info *lsi, dco_context_t *dco)
Update TLS session crypto parameters (cipher and auth) and derive data channel keys based on the supp...
Definition ssl.c:1707
void free_ssl_lib(void)
Definition ssl.c:236
void init_ssl(const struct options *options, struct tls_root_ctx *ctx, bool in_chroot)
Build master SSL context object that serves for the whole of OpenVPN instantiation.
Definition ssl.c:523
static int tls_test_payload_len(const struct tls_multi *multi)
Definition ssl.h:509
bool ssl_clean_auth_token(void)
Definition ssl.c:381
void enable_auth_user_pass(void)
Definition ssl.c:289
void ssl_purge_auth_challenge(void)
Definition ssl.c:410
void show_available_tls_ciphers(const char *cipher_list, const char *cipher_list_tls13, const char *tls_cert_profile)
Definition ssl.c:4244
const char * protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
Definition ssl.c:4271
Control Channel SSL library backend module.
Control Channel Common Data Structures.
static const struct key_state * get_primary_key(const struct tls_multi *multi)
gets an item of key_state objects in the order they should be scanned by data channel modules.
Definition ssl_common.h:728
SSL control channel wrap/unwrap and decode functions.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
Security parameter state for processing data channel packets.
Definition crypto.h:292
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:293
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition crypto.h:330
Packet geometry parameters.
Definition mtu.h:98
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Security parameter state of one TLS and data channel key session.
Definition ssl_common.h:200
struct buffer plaintext_read_buf
Definition ssl_common.h:233
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:597
struct tls_options opt
Definition ssl_common.h:602
int n_sessions
Number of sessions negotiated thus far.
Definition ssl_common.h:616
bool single_session
Definition ssl_common.h:317
struct that stores the temporary data for the tls lite decrypt functions
Definition ssl_pkt.h:105
Structure that wraps the TLS context.
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:480
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition ssl_common.h:268
struct buffer tls_crypt_v2_metadata
Received from client.
Definition ssl_common.h:279
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
Definition ssl_common.h:280
struct crypto_options opt
Crypto state.
Definition ssl_common.h:274
struct buffer work
Work buffer (only for –tls-crypt)
Definition ssl_common.h:275
struct key2 original_wrap_keydata
original key data to be xored in to the key for dynamic tls-crypt.
Definition ssl_common.h:290
struct gc_arena gc
Definition test_ssl.c:155