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-2017 OpenVPN Technologies, Inc. <sales@openvpn.net>
9  * Copyright (C) 2010-2017 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 #if defined(ENABLE_CRYPTO)
33 
34 #include "basic.h"
35 #include "common.h"
36 #include "crypto.h"
37 #include "packet_id.h"
38 #include "session_id.h"
39 #include "reliable.h"
40 #include "socket.h"
41 #include "mtu.h"
42 #include "options.h"
43 #include "plugin.h"
44 
45 #include "ssl_common.h"
46 #include "ssl_backend.h"
47 
48 /* Used in the TLS PRF function */
49 #define KEY_EXPANSION_ID "OpenVPN"
50 
51 /* packet opcode (high 5 bits) and key-id (low 3 bits) are combined in one byte */
52 #define P_KEY_ID_MASK 0x07
53 #define P_OPCODE_SHIFT 3
54 
55 /* packet opcodes -- the V1 is intended to allow protocol changes in the future */
56 #define P_CONTROL_HARD_RESET_CLIENT_V1 1 /* initial key from client, forget previous state */
57 #define P_CONTROL_HARD_RESET_SERVER_V1 2 /* initial key from server, forget previous state */
58 #define P_CONTROL_SOFT_RESET_V1 3 /* new key, graceful transition from old to new key */
59 #define P_CONTROL_V1 4 /* control channel packet (usually TLS ciphertext) */
60 #define P_ACK_V1 5 /* acknowledgement for packets received */
61 #define P_DATA_V1 6 /* data channel packet */
62 #define P_DATA_V2 9 /* data channel packet with peer-id */
63 
64 /* indicates key_method >= 2 */
65 #define P_CONTROL_HARD_RESET_CLIENT_V2 7 /* initial key from client, forget previous state */
66 #define P_CONTROL_HARD_RESET_SERVER_V2 8 /* initial key from server, forget previous state */
67 
68 /* define the range of legal opcodes */
69 #define P_FIRST_OPCODE 1
70 #define P_LAST_OPCODE 9
71 
72 /*
73  * Set the max number of acknowledgments that can "hitch a ride" on an outgoing
74  * non-P_ACK_V1 control packet.
75  */
76 #define CONTROL_SEND_ACK_MAX 4
77 
78 /*
79  * Define number of buffers for send and receive in the reliability layer.
80  */
81 #define TLS_RELIABLE_N_SEND_BUFFERS 4 /* also window size for reliablity layer */
82 #define TLS_RELIABLE_N_REC_BUFFERS 8
83 
84 /*
85  * Various timeouts
86  */
87 #define TLS_MULTI_REFRESH 15 /* call tls_multi_process once every n seconds */
88 #define TLS_MULTI_HORIZON 2 /* call tls_multi_process frequently for n seconds after
89  * every packet sent/received action */
90 
91 /*
92  * The SSL/TLS worker thread will wait at most this many seconds for the
93  * interprocess communication pipe to the main thread to be ready to accept
94  * writes.
95  */
96 #define TLS_MULTI_THREAD_SEND_TIMEOUT 5
97 
98 /* Interval that tls_multi_process should call tls_authentication_status */
99 #define TLS_MULTI_AUTH_STATUS_INTERVAL 10
100 
101 /*
102  * Buffer sizes (also see mtu.h).
103  */
104 
105 /* Maximum length of OCC options string passed as part of auth handshake */
106 #define TLS_OPTIONS_LEN 512
107 
108 /* Default field in X509 to be username */
109 #define X509_USERNAME_FIELD_DEFAULT "CN"
110 
111 /*
112  * Range of key exchange methods
113  */
114 #define KEY_METHOD_MIN 1
115 #define KEY_METHOD_MAX 2
116 
117 /* key method taken from lower 4 bits */
118 #define KEY_METHOD_MASK 0x0F
119 
120 /*
121  * Measure success rate of TLS handshakes, for debugging only
122  */
123 /* #define MEASURE_TLS_HANDSHAKE_STATS */
124 
125 /*
126  * Used in --mode server mode to check tls-auth signature on initial
127  * packets received from new clients.
128  */
130 {
132  struct frame frame;
133 };
134 
135 /*
136  * Prepare the SSL library for use
137  */
138 void init_ssl_lib(void);
139 
140 /*
141  * Free any internal state that the SSL library might have
142  */
143 void free_ssl_lib(void);
144 
149 void init_ssl(const struct options *options, struct tls_root_ctx *ctx);
150 
172 
187 void tls_multi_init_finalize(struct tls_multi *multi,
188  const struct frame *frame);
189 
190 /*
191  * Initialize a standalone tls-auth verification object.
192  */
194  struct gc_arena *gc);
195 
196 /*
197  * Finalize a standalone tls-auth verification object.
198  */
200  const struct frame *frame);
201 
202 /*
203  * Set local and remote option compatibility strings.
204  * Used to verify compatibility of local and remote option
205  * sets.
206  */
207 void tls_multi_init_set_options(struct tls_multi *multi,
208  const char *local,
209  const char *remote);
210 
223 void 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 
233 /*
234  * Called by the top-level event loop.
235  *
236  * Basically decides if we should call tls_process for
237  * the active or untrusted sessions.
238  */
239 int tls_multi_process(struct tls_multi *multi,
240  struct buffer *to_link,
241  struct link_socket_actual **to_link_addr,
242  struct link_socket_info *to_link_socket_info,
243  interval_t *wakeup);
244 
245 
246 /**************************************************************************/
298 bool tls_pre_decrypt(struct tls_multi *multi,
299  const struct link_socket_actual *from,
300  struct buffer *buf,
301  struct crypto_options **opt,
302  bool floated,
303  const uint8_t **ad_start);
304 
305 
306 /**************************************************************************/
341 bool tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,
342  const struct link_socket_actual *from,
343  const struct buffer *buf);
344 
345 
359 void tls_pre_encrypt(struct tls_multi *multi,
360  struct buffer *buf, struct crypto_options **opt);
361 
362 
375 void
376 tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf);
377 
394 void
395 tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf);
396 
404 void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf);
405 
408 /*
409  * Setup private key file password. If auth_file is given, use the
410  * credentials stored in the file.
411  */
412 void pem_password_setup(const char *auth_file);
413 
414 /*
415  * Setup authentication username and password. If auth_file is given, use the
416  * credentials stored in the file.
417  */
418 void auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sc_info);
419 
420 /*
421  * Ensure that no caching is performed on authentication information
422  */
423 void ssl_set_auth_nocache(void);
424 
425 /*
426  * Purge any stored authentication information, both for key files and tunnel
427  * authentication. If PCKS #11 is enabled, purge authentication for that too.
428  */
429 void ssl_purge_auth(const bool auth_user_pass_only);
430 
431 void ssl_set_auth_token(const char *token);
432 
433 #ifdef ENABLE_CLIENT_CR
434 /*
435  * ssl_get_auth_challenge will parse the server-pushed auth-failed
436  * reason string and return a dynamically allocated
437  * auth_challenge_info struct.
438  */
439 void ssl_purge_auth_challenge(void);
440 
441 void ssl_put_auth_challenge(const char *cr_str);
442 
443 #endif
444 
445 /*
446  * Reserve any extra space required on frames.
447  */
448 void tls_adjust_frame_parameters(struct frame *frame);
449 
450 /*
451  * Send a payload over the TLS control channel
452  */
453 bool tls_send_payload(struct tls_multi *multi,
454  const uint8_t *data,
455  int size);
456 
457 /*
458  * Receive a payload through the TLS control channel
459  */
460 bool tls_rec_payload(struct tls_multi *multi,
461  struct buffer *buf);
462 
469 void tls_update_remote_addr(struct tls_multi *multi,
470  const struct link_socket_actual *addr);
471 
484  struct options *options, struct frame *frame);
485 
493 void tls_poor_mans_ncp(struct options *o, const char *remote_ciphername);
494 
495 #ifdef MANAGEMENT_DEF_AUTH
496 static inline char *
497 tls_get_peer_info(const struct tls_multi *multi)
498 {
499  return multi->peer_info;
500 }
501 #endif
502 
507 int tls_peer_info_ncp_ver(const char *peer_info);
508 
516 bool tls_check_ncp_cipher_list(const char *list);
517 
522 bool tls_item_in_cipher_list(const char *item, const char *list);
523 
524 
525 /*
526  * inline functions
527  */
528 
529 static inline bool
531 {
532  return multi->n_sessions > 0;
533 }
534 
535 static inline bool
537 {
538  if (multi)
539  {
540  const struct key_state *ks = &multi->session[TM_ACTIVE].key[KS_PRIMARY];
541  return now < ks->auth_deferred_expire;
542  }
543  return false;
544 }
545 
546 static inline int
547 tls_test_payload_len(const struct tls_multi *multi)
548 {
549  if (multi)
550  {
551  const struct key_state *ks = &multi->session[TM_ACTIVE].key[KS_PRIMARY];
552  if (ks->state >= S_ACTIVE)
553  {
554  return BLEN(&ks->plaintext_read_buf);
555  }
556  }
557  return 0;
558 }
559 
560 static inline void
562 {
563  if (multi)
564  {
565  multi->opt.single_session = true;
566  }
567 }
568 
569 /*
570  * protocol_dump() flags
571  */
572 #define PD_TLS_AUTH_HMAC_SIZE_MASK 0xFF
573 #define PD_SHOW_DATA (1<<8)
574 #define PD_TLS (1<<9)
575 #define PD_VERBOSE (1<<10)
576 
577 const char *protocol_dump(struct buffer *buffer,
578  unsigned int flags,
579  struct gc_arena *gc);
580 
581 /*
582  * debugging code
583  */
584 
585 #ifdef MEASURE_TLS_HANDSHAKE_STATS
586 void show_tls_performance_stats(void);
587 
588 #endif
589 
590 /*#define EXTRACT_X509_FIELD_TEST*/
591 void extract_x509_field_test(void);
592 
599 bool is_hard_reset(int op, int key_method);
600 
601 void delayed_auth_pass_purge(void);
602 
603 #endif /* ENABLE_CRYPTO */
604 
605 #endif /* ifndef OPENVPN_SSL_H */
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:1949
Security parameter state for processing data channel packets.
Definition: crypto.h:234
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:458
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:161
struct key_state key[KS_SIZE]
Definition: ssl_common.h:438
struct buffer plaintext_read_buf
Definition: ssl_common.h:185
int n_sessions
Number of sessions negotiated thus far.
Definition: ssl_common.h:521
void tls_auth_standalone_finalize(struct tls_auth_standalone *tas, const struct frame *frame)
Definition: ssl.c:1309
void ssl_put_auth_challenge(const char *cr_str)
Definition: ssl.c:504
void ssl_set_auth_token(const char *token)
Definition: ssl.c:462
void delayed_auth_pass_purge(void)
Definition: ssl.c:4240
bool tls_send_payload(struct tls_multi *multi, const uint8_t *data, int size)
Definition: ssl.c:3981
Packet geometry parameters.
Definition: mtu.h:93
char * peer_info
Definition: ssl_common.h:552
void pem_password_setup(const char *auth_file)
Definition: ssl.c:374
void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
Perform some accounting for the key state used.
Definition: ssl.c:3961
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:496
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:1880
static int tls_test_payload_len(const struct tls_multi *multi)
Definition: ssl.h:547
void tls_multi_free(struct tls_multi *multi, bool clear)
Cleanup a tls_multi structure and free associated memory allocations.
Definition: ssl.c:1336
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:847
static bool tls_test_auth_deferred_interval(const struct tls_multi *multi)
Definition: ssl.h:536
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:3946
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:3783
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer...
Definition: ssl_common.h:571
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:3880
void tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
Updates remote address in TLS sessions.
Definition: ssl.c:4049
void ssl_purge_auth(const bool auth_user_pass_only)
Definition: ssl.c:479
#define S_ACTIVE
Operational key_state state immediately after negotiation has completed while still within the handsh...
Definition: ssl_common.h:102
void extract_x509_field_test(void)
static void tls_set_single_session(struct tls_multi *multi)
Definition: ssl.h:561
bool tls_check_ncp_cipher_list(const char *list)
Check whether the ciphers in the supplied list are supported.
Definition: ssl.c:4095
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition: ssl.c:4020
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition: ssl.c:1286
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:3932
void auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sc_info)
Definition: ssl.c:409
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:4079
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:370
bool single_session
Definition: ssl_common.h:259
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:1245
void init_ssl_lib(void)
Definition: ssl.c:350
static bool tls_initial_packet_received(const struct tls_multi *multi)
Definition: ssl.h:530
struct tls_wrap_ctx tls_wrap
Definition: ssl.h:131
struct tls_options opt
Definition: ssl_common.h:502
void tls_multi_init_set_options(struct tls_multi *multi, const char *local, const char *remote)
Definition: ssl.c:1321
void free_ssl_lib(void)
Definition: ssl.c:358
#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:3305
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:66
const char * protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
Definition: ssl.c:4122
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:400
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:3110
void tls_multi_init_finalize(struct tls_multi *multi, const struct frame *frame)
Finalize initialization of a tls_multi structure.
Definition: ssl.c:1267
void ssl_purge_auth_challenge(void)
Definition: ssl.c:497
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:450
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:1900
void tls_adjust_frame_parameters(struct frame *frame)
Definition: ssl.c:317
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:598