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-2018 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2018 Fox Crypto B.V. <openvpn@fox-it.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 
29 #ifndef OPENVPN_SSL_H
30 #define OPENVPN_SSL_H
31 
32 #include "basic.h"
33 #include "common.h"
34 #include "crypto.h"
35 #include "packet_id.h"
36 #include "session_id.h"
37 #include "reliable.h"
38 #include "socket.h"
39 #include "mtu.h"
40 #include "options.h"
41 #include "plugin.h"
42 
43 #include "ssl_common.h"
44 #include "ssl_backend.h"
45 
46 /* Used in the TLS PRF function */
47 #define KEY_EXPANSION_ID "OpenVPN"
48 
49 /* packet opcode (high 5 bits) and key-id (low 3 bits) are combined in one byte */
50 #define P_KEY_ID_MASK 0x07
51 #define P_OPCODE_SHIFT 3
52 
53 /* packet opcodes -- the V1 is intended to allow protocol changes in the future */
54 #define P_CONTROL_HARD_RESET_CLIENT_V1 1 /* initial key from client, forget previous state */
55 #define P_CONTROL_HARD_RESET_SERVER_V1 2 /* initial key from server, forget previous state */
56 #define P_CONTROL_SOFT_RESET_V1 3 /* new key, graceful transition from old to new key */
57 #define P_CONTROL_V1 4 /* control channel packet (usually TLS ciphertext) */
58 #define P_ACK_V1 5 /* acknowledgement for packets received */
59 #define P_DATA_V1 6 /* data channel packet */
60 #define P_DATA_V2 9 /* data channel packet with peer-id */
61 
62 /* indicates key_method >= 2 */
63 #define P_CONTROL_HARD_RESET_CLIENT_V2 7 /* initial key from client, forget previous state */
64 #define P_CONTROL_HARD_RESET_SERVER_V2 8 /* initial key from server, forget previous state */
65 
66 /* indicates key_method >= 2 and client-specific tls-crypt key */
67 #define P_CONTROL_HARD_RESET_CLIENT_V3 10 /* initial key from client, forget previous state */
68 
69 /* define the range of legal opcodes */
70 #define P_FIRST_OPCODE 1
71 #define P_LAST_OPCODE 10
72 
73 /*
74  * Set the max number of acknowledgments that can "hitch a ride" on an outgoing
75  * non-P_ACK_V1 control packet.
76  */
77 #define CONTROL_SEND_ACK_MAX 4
78 
79 /*
80  * Define number of buffers for send and receive in the reliability layer.
81  */
82 #define TLS_RELIABLE_N_SEND_BUFFERS 4 /* also window size for reliability layer */
83 #define TLS_RELIABLE_N_REC_BUFFERS 8
84 
85 /*
86  * Various timeouts
87  */
88 #define TLS_MULTI_REFRESH 15 /* call tls_multi_process once every n seconds */
89 #define TLS_MULTI_HORIZON 2 /* call tls_multi_process frequently for n seconds after
90  * every packet sent/received action */
91 
92 /*
93  * The SSL/TLS worker thread will wait at most this many seconds for the
94  * interprocess communication pipe to the main thread to be ready to accept
95  * writes.
96  */
97 #define TLS_MULTI_THREAD_SEND_TIMEOUT 5
98 
99 /* Interval that tls_multi_process should call tls_authentication_status */
100 #define TLS_MULTI_AUTH_STATUS_INTERVAL 10
101 
102 /*
103  * Buffer sizes (also see mtu.h).
104  */
105 
106 /* Maximum length of OCC options string passed as part of auth handshake */
107 #define TLS_OPTIONS_LEN 512
108 
109 /* Default field in X509 to be username */
110 #define X509_USERNAME_FIELD_DEFAULT "CN"
111 
112 /*
113  * Range of key exchange methods
114  */
115 #define KEY_METHOD_MIN 1
116 #define KEY_METHOD_MAX 2
117 
118 /* key method taken from lower 4 bits */
119 #define KEY_METHOD_MASK 0x0F
120 
121 /*
122  * Measure success rate of TLS handshakes, for debugging only
123  */
124 /* #define MEASURE_TLS_HANDSHAKE_STATS */
125 
126 /*
127  * Used in --mode server mode to check tls-auth signature on initial
128  * packets received from new clients.
129  */
131 {
133  struct frame frame;
134 };
135 
136 /*
137  * Prepare the SSL library for use
138  */
139 void init_ssl_lib(void);
140 
141 /*
142  * Free any internal state that the SSL library might have
143  */
144 void free_ssl_lib(void);
145 
150 void init_ssl(const struct options *options, struct tls_root_ctx *ctx);
151 
173 
188 void tls_multi_init_finalize(struct tls_multi *multi,
189  const struct frame *frame);
190 
191 /*
192  * Initialize a standalone tls-auth verification object.
193  */
195  struct gc_arena *gc);
196 
197 /*
198  * Finalize a standalone tls-auth verification object.
199  */
201  const struct frame *frame);
202 
203 /*
204  * Set local and remote option compatibility strings.
205  * Used to verify compatibility of local and remote option
206  * sets.
207  */
208 void tls_multi_init_set_options(struct tls_multi *multi,
209  const char *local,
210  const char *remote);
211 
224 void tls_multi_free(struct tls_multi *multi, bool clear);
225 
230 #define TLSMP_INACTIVE 0
231 #define TLSMP_ACTIVE 1
232 #define TLSMP_KILL 2
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  */
240 int 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 /**************************************************************************/
299 bool 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 /**************************************************************************/
342 bool tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,
343  const struct link_socket_actual *from,
344  const struct buffer *buf);
345 
346 
360 void tls_pre_encrypt(struct tls_multi *multi,
361  struct buffer *buf, struct crypto_options **opt);
362 
363 
376 void
377 tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf);
378 
395 void
396 tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf);
397 
405 void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf);
406 
409 /*
410  * Setup private key file password. If auth_file is given, use the
411  * credentials stored in the file.
412  */
413 void pem_password_setup(const char *auth_file);
414 
415 /*
416  * Setup authentication username and password. If auth_file is given, use the
417  * credentials stored in the file.
418  */
419 void auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sc_info);
420 
421 /*
422  * Ensure that no caching is performed on authentication information
423  */
424 void ssl_set_auth_nocache(void);
425 
426 /*
427  * Purge any stored authentication information, both for key files and tunnel
428  * authentication. If PCKS #11 is enabled, purge authentication for that too.
429  */
430 void ssl_purge_auth(const bool auth_user_pass_only);
431 
432 void ssl_set_auth_token(const char *token);
433 
434 #ifdef ENABLE_MANAGEMENT
435 /*
436  * ssl_get_auth_challenge will parse the server-pushed auth-failed
437  * reason string and return a dynamically allocated
438  * auth_challenge_info struct.
439  */
440 void ssl_purge_auth_challenge(void);
441 
442 bool ssl_clean_auth_token(void);
443 
444 void ssl_put_auth_challenge(const char *cr_str);
445 
446 #endif
447 
448 /*
449  * Reserve any extra space required on frames.
450  */
451 void tls_adjust_frame_parameters(struct frame *frame);
452 
453 /*
454  * Send a payload over the TLS control channel
455  */
456 bool tls_send_payload(struct tls_multi *multi,
457  const uint8_t *data,
458  int size);
459 
460 /*
461  * Receive a payload through the TLS control channel
462  */
463 bool tls_rec_payload(struct tls_multi *multi,
464  struct buffer *buf);
465 
472 void tls_update_remote_addr(struct tls_multi *multi,
473  const struct link_socket_actual *addr);
474 
487  struct options *options, struct frame *frame);
488 
496 void tls_poor_mans_ncp(struct options *o, const char *remote_ciphername);
497 
498 #ifdef MANAGEMENT_DEF_AUTH
499 static inline char *
500 tls_get_peer_info(const struct tls_multi *multi)
501 {
502  return multi->peer_info;
503 }
504 #endif
505 
510 int tls_peer_info_ncp_ver(const char *peer_info);
511 
519 bool tls_check_ncp_cipher_list(const char *list);
520 
525 bool tls_item_in_cipher_list(const char *item, const char *list);
526 
527 
528 /*
529  * inline functions
530  */
531 
533 static inline void
535 {
536  if (packet_id_initialized(&tls_wrap->opt.packet_id))
537  {
538  packet_id_free(&tls_wrap->opt.packet_id);
539  }
540 
541  if (tls_wrap->cleanup_key_ctx)
542  {
543  free_key_ctx_bi(&tls_wrap->opt.key_ctx_bi);
544  }
545 
546  free_buf(&tls_wrap->tls_crypt_v2_metadata);
547  free_buf(&tls_wrap->work);
548 }
549 
550 static inline bool
552 {
553  return multi->n_sessions > 0;
554 }
555 
556 static inline bool
558 {
559  if (multi)
560  {
561  const struct key_state *ks = &multi->session[TM_ACTIVE].key[KS_PRIMARY];
562  return now < ks->auth_deferred_expire;
563  }
564  return false;
565 }
566 
567 static inline int
568 tls_test_payload_len(const struct tls_multi *multi)
569 {
570  if (multi)
571  {
572  const struct key_state *ks = &multi->session[TM_ACTIVE].key[KS_PRIMARY];
573  if (ks->state >= S_ACTIVE)
574  {
575  return BLEN(&ks->plaintext_read_buf);
576  }
577  }
578  return 0;
579 }
580 
581 static inline void
583 {
584  if (multi)
585  {
586  multi->opt.single_session = true;
587  }
588 }
589 
590 /*
591  * protocol_dump() flags
592  */
593 #define PD_TLS_AUTH_HMAC_SIZE_MASK 0xFF
594 #define PD_SHOW_DATA (1<<8)
595 #define PD_TLS (1<<9)
596 #define PD_VERBOSE (1<<10)
597 
598 const char *protocol_dump(struct buffer *buffer,
599  unsigned int flags,
600  struct gc_arena *gc);
601 
602 /*
603  * debugging code
604  */
605 
606 #ifdef MEASURE_TLS_HANDSHAKE_STATS
607 void show_tls_performance_stats(void);
608 
609 #endif
610 
611 /*#define EXTRACT_X509_FIELD_TEST*/
612 void extract_x509_field_test(void);
613 
620 bool is_hard_reset(int op, int key_method);
621 
622 void delayed_auth_pass_purge(void);
623 
624 
625 /*
626  * Show the TLS ciphers that are available for us to use in the SSL
627  * library with headers hinting their usage and warnings about usage.
628  *
629  * @param cipher_list list of allowed TLS cipher, or NULL.
630  * @param cipher_list_tls13 list of allowed TLS 1.3+ cipher, or NULL
631  * @param tls_cert_profile TLS certificate crypto profile name.
632  */
633 void
634 show_available_tls_ciphers(const char *cipher_list,
635  const char *cipher_list_tls13,
636  const char *tls_cert_profile);
637 
638 #endif /* ifndef OPENVPN_SSL_H */
void ssl_put_auth_challenge(const char *cr_str)
Definition: ssl.c:502
bool tls_session_update_crypto_params(struct tls_session *session, struct options *options, struct frame *frame)
Update TLS session crypto parameters (cipher and auth) and derive data channel keys based on the supp...
Definition: ssl.c:1992
bool ssl_clean_auth_token(void)
Definition: ssl.c:466
Security parameter state for processing data channel packets.
Definition: crypto.h:232
static void tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
Free the elements of a tls_wrap_ctx structure.
Definition: ssl.h:534
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:465
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:161
struct buffer work
Work buffer (only for –tls-crypt)
Definition: ssl_common.h:228
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition: crypto.h:238
struct key_state key[KS_SIZE]
Definition: ssl_common.h:445
struct buffer plaintext_read_buf
Definition: ssl_common.h:185
int n_sessions
Number of sessions negotiated thus far.
Definition: ssl_common.h:528
void tls_auth_standalone_finalize(struct tls_auth_standalone *tas, const struct frame *frame)
Definition: ssl.c:1318
void free_buf(struct buffer *buf)
Definition: buffer.c:185
void ssl_set_auth_token(const char *token)
Definition: ssl.c:457
struct buffer tls_crypt_v2_metadata
Received from client.
Definition: ssl_common.h:232
void delayed_auth_pass_purge(void)
Definition: ssl.c:4325
bool tls_send_payload(struct tls_multi *multi, const uint8_t *data, int size)
Definition: ssl.c:4042
Packet geometry parameters.
Definition: mtu.h:93
char * peer_info
Definition: ssl_common.h:559
void pem_password_setup(const char *auth_file)
Definition: ssl.c:372
void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
Perform some accounting for the key state used.
Definition: ssl.c:4022
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:503
bool tls_item_in_cipher_list(const char *item, const char *list)
Return true iff item is present in the colon-separated zero-terminated cipher list.
Definition: ssl.c:1923
static int tls_test_payload_len(const struct tls_multi *multi)
Definition: ssl.h:568
void tls_multi_free(struct tls_multi *multi, bool clear)
Cleanup a tls_multi structure and free associated memory allocations.
Definition: ssl.c:1345
bool is_hard_reset(int op, int key_method)
Given a key_method, return true if opcode represents the required form of hard_reset.
Definition: ssl.c:855
static bool tls_test_auth_deferred_interval(const struct tls_multi *multi)
Definition: ssl.h:557
struct crypto_options opt
Crypto state.
Definition: ssl_common.h:227
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:4007
void show_available_tls_ciphers(const char *cipher_list, const char *cipher_list_tls13, const char *tls_cert_profile)
Definition: ssl.c:4179
bool tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, const struct link_socket_actual *from, const struct buffer *buf)
Inspect an incoming packet for which no VPN tunnel is active, and determine whether a new VPN tunnel ...
Definition: ssl.c:3838
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer...
Definition: ssl_common.h:578
static bool packet_id_initialized(const struct packet_id *pid)
Is this struct packet_id initialized?
Definition: packet_id.h:274
void packet_id_free(struct packet_id *p)
Definition: packet_id.c:101
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:3941
void tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
Updates remote address in TLS sessions.
Definition: ssl.c:4110
void ssl_purge_auth(const bool auth_user_pass_only)
Definition: ssl.c:477
list flags
#define S_ACTIVE
Operational key_state state immediately after negotiation has completed while still within the handsh...
Definition: ssl_common.h:102
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
Definition: ssl_common.h:233
void extract_x509_field_test(void)
static void tls_set_single_session(struct tls_multi *multi)
Definition: ssl.h:582
bool tls_check_ncp_cipher_list(const char *list)
Check whether the ciphers in the supplied list are supported.
Definition: ssl.c:4156
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition: ssl.c:4081
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition: ssl.c:1295
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition: ssl_common.h:220
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:3993
void auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sc_info)
Definition: ssl.c:408
Reliability Layer module header file.
int tls_peer_info_ncp_ver(const char *peer_info)
Return the Negotiable Crypto Parameters version advertised in the peer info string, or 0 if none specified.
Definition: ssl.c:4140
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:377
bool single_session
Definition: ssl_common.h:265
time_t now
Definition: otime.c:36
struct tls_multi * tls_multi_init(struct tls_options *tls_options)
Allocate and initialize a tls_multi structure.
Definition: ssl.c:1254
void init_ssl_lib(void)
Definition: ssl.c:348
static bool tls_initial_packet_received(const struct tls_multi *multi)
Definition: ssl.h:551
struct tls_wrap_ctx tls_wrap
Definition: ssl.h:132
struct tls_options opt
Definition: ssl_common.h:509
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition: crypto.c:912
void tls_multi_init_set_options(struct tls_multi *multi, const char *local, const char *remote)
Definition: ssl.c:1330
void free_ssl_lib(void)
Definition: ssl.c:356
#define BLEN(buf)
Definition: buffer.h:127
time_t auth_deferred_expire
Definition: ssl_common.h:202
unsigned __int8 uint8_t
Definition: config-msvc.h:123
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:3356
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:90
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition: crypto.h:234
const char * protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
Definition: ssl.c:4207
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:407
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
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:3161
void tls_multi_init_finalize(struct tls_multi *multi, const struct frame *frame)
Finalize initialization of a tls_multi structure.
Definition: ssl.c:1276
void ssl_purge_auth_challenge(void)
Definition: ssl.c:495
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
int interval_t
Definition: common.h:45
void ssl_set_auth_nocache(void)
Definition: ssl.c:445
void tls_poor_mans_ncp(struct options *o, const char *remote_ciphername)
"Poor man&#39;s NCP": Use peer cipher if it is an allowed (NCP) cipher.
Definition: ssl.c:1943
void tls_adjust_frame_parameters(struct frame *frame)
Definition: ssl.c:315
void init_ssl(const struct options *options, struct tls_root_ctx *ctx)
Build master SSL context object that serves for the whole of OpenVPN instantiation.
Definition: ssl.c:600