OpenVPN
ssl_common.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 SSL_COMMON_H_
30 #define SSL_COMMON_H_
31 
32 #include "session_id.h"
33 #include "socket.h"
34 #include "packet_id.h"
35 #include "crypto.h"
36 #include "options.h"
37 
38 #include "ssl_backend.h"
39 
40 /* passwords */
41 #define UP_TYPE_AUTH "Auth"
42 #define UP_TYPE_PRIVATE_KEY "Private Key"
43 
77 #define S_ERROR -1
78 #define S_UNDEF 0
80 #define S_INITIAL 1
83 #define S_PRE_START 2
86 #define S_START 3
88 #define S_SENT_KEY 4
90 #define S_GOT_KEY 5
93 #define S_ACTIVE 6
97 /* ready to exchange data channel packets */
98 #define S_NORMAL_OP 7
108 struct key_source {
109  uint8_t pre_master[48];
112  uint8_t random1[32];
115  uint8_t random2[32];
117 };
118 
119 
125 struct key_source2 {
126  struct key_source client;
127  struct key_source server;
128 };
129 
148 struct key_state
149 {
150  int state;
151 
156  int key_id;
157 
158  struct key_state_ssl ks_ssl; /* contains SSL object and BIOs for the control channel */
159 
160  time_t established; /* when our state went S_ACTIVE */
161  time_t must_negotiate; /* key negotiation times out if not finished before this time */
162  time_t must_die; /* this object is destroyed at this time */
164  int initial_opcode; /* our initial P_ opcode */
165  struct session_id session_id_remote; /* peer's random session ID */
166  struct link_socket_actual remote_addr; /* peer's IP addr */
167 
168  struct crypto_options crypto_options;/* data channel crypto options */
170  struct key_source2 *key_src; /* source entropy for key expansion */
172  struct buffer plaintext_read_buf;
173  struct buffer plaintext_write_buf;
174  struct buffer ack_write_buf;
176  struct reliable *send_reliable; /* holds a copy of outgoing packets until ACK received */
177  struct reliable *rec_reliable; /* order incoming ciphertext packets before we pass to TLS */
178  struct reliable_ack *rec_ack; /* buffers all packet IDs we want to ACK back to sender */
180  struct buffer_list *paybuf;
182  counter_type n_bytes; /* how many bytes sent/recvd since last key exchange */
183  counter_type n_packets; /* how many packets sent/recvd since last key exchange */
184 
185  /*
186  * If bad username/password, TLS connection will come up but 'authenticated' will be false.
187  */
188  bool authenticated;
189  time_t auth_deferred_expire;
191 #ifdef ENABLE_DEF_AUTH
192  /* If auth_deferred is true, authentication is being deferred */
193  bool auth_deferred;
194 #ifdef MANAGEMENT_DEF_AUTH
195  unsigned int mda_key_id;
196  unsigned int mda_status;
197 #endif
198 #ifdef PLUGIN_DEF_AUTH
199  unsigned int auth_control_status;
200  time_t acf_last_mod;
201  char *auth_control_file;
202 #endif
203 #endif
204 };
205 
207 struct tls_wrap_ctx
208 {
209  enum {
210  TLS_WRAP_NONE = 0,
211  TLS_WRAP_AUTH,
212  TLS_WRAP_CRYPT,
213  } mode;
214  struct crypto_options opt;
215  struct buffer work;
216 };
217 
218 /*
219  * Our const options, obtained directly or derived from
220  * command line options.
221  */
222 struct tls_options
223 {
224  /* our master TLS context from which all SSL objects derived */
225  struct tls_root_ctx ssl_ctx;
226 
227  /* data channel cipher, hmac, and key lengths */
229 
230  /* true if we are a TLS server, client otherwise */
231  bool server;
232 
233  /* if true, don't xmit until first packet from peer is received */
234  bool xmit_hold;
236 #ifdef ENABLE_OCC
237  /* local and remote options strings
238  * that must match between client and server */
239  const char *local_options;
240  const char *remote_options;
241 #endif
242 
243  /* from command line */
244  int key_method;
245  bool replay;
246  bool single_session;
247 #ifdef ENABLE_OCC
248  bool disable_occ;
249 #endif
250  int mode;
251  bool pull;
252 #ifdef ENABLE_PUSH_PEER_INFO
253  int push_peer_info_detail;
254 #endif
255  int transition_window;
256  int handshake_window;
257  interval_t packet_timeout;
258  int renegotiate_bytes;
259  int renegotiate_packets;
260  interval_t renegotiate_seconds;
262  /* cert verification parms */
263  const char *verify_command;
264  const char *verify_export_cert;
265  int verify_x509_type;
266  const char *verify_x509_name;
267  const char *crl_file;
268  const char *crl_file_inline;
269  int ns_cert_type;
270  unsigned remote_cert_ku[MAX_PARMS];
271  const char *remote_cert_eku;
272  uint8_t *verify_hash;
273  hash_algo_type verify_hash_algo;
274  char *x509_username_field;
275 
276  /* allow openvpn config info to be
277  * passed over control channel */
278  bool pass_config_info;
280  /* struct crypto_option flags */
281  unsigned int crypto_flags;
283  int replay_window; /* --replay-window parm */
284  int replay_time; /* --replay-window parm */
285  bool tcp_mode;
287  const char *config_ciphername;
288  const char *config_authname;
289  bool ncp_enabled;
290 
292  struct tls_wrap_ctx tls_wrap;
293 
294  /* frame parameters for TLS control channel */
295  struct frame frame;
297  /* used for username/password authentication */
298  const char *auth_user_pass_verify_script;
299  bool auth_user_pass_verify_script_via_file;
300  const char *tmp_dir;
301  const char *auth_user_pass_file;
302  bool auth_token_generate;
304  unsigned int auth_token_lifetime;
306  /* use the client-config-dir as a positive authenticator */
307  const char *client_config_dir_exclusive;
309  /* instance-wide environment variable set */
310  struct env_set *es;
311  const struct plugin_list *plugins;
313  /* compression parms */
314 #ifdef USE_COMP
315  struct compress_options comp_options;
316 #endif
318  /* configuration file SSL-related boolean and low-permutation options */
319 #define SSLF_CLIENT_CERT_NOT_REQUIRED (1<<0)
320 #define SSLF_CLIENT_CERT_OPTIONAL (1<<1)
321 #define SSLF_USERNAME_AS_COMMON_NAME (1<<2)
322 #define SSLF_AUTH_USER_PASS_OPTIONAL (1<<3)
323 #define SSLF_OPT_VERIFY (1<<4)
324 #define SSLF_CRL_VERIFY_DIR (1<<5)
325 #define SSLF_TLS_VERSION_MIN_SHIFT 6
326 #define SSLF_TLS_VERSION_MIN_MASK 0xF /* (uses bit positions 6 to 9) */
327 #define SSLF_TLS_VERSION_MAX_SHIFT 10
328 #define SSLF_TLS_VERSION_MAX_MASK 0xF /* (uses bit positions 10 to 13) */
329  unsigned int ssl_flags;
330 
331 #ifdef MANAGEMENT_DEF_AUTH
332  struct man_def_auth_context *mda_context;
333 #endif
335  const struct x509_track *x509_track;
337 #ifdef ENABLE_CLIENT_CR
338  const struct static_challenge_info *sci;
339 #endif
341  /* --gremlin bits */
342  int gremlin;
343 
344  /* Keying Material Exporter [RFC 5705] parameters */
345  const char *ekm_label;
346  size_t ekm_label_size;
347  size_t ekm_size;
348 };
349 
357 #define KS_PRIMARY 0
358 #define KS_LAME_DUCK 1
360 #define KS_SIZE 2
364 #define AUTH_TOKEN_SIZE 32
384 struct tls_session
385 {
386  /* const options and config info */
387  struct tls_options *opt;
388 
389  /* during hard reset used to control burst retransmit */
390  bool burst;
391 
392  /* authenticate control packets */
393  struct tls_wrap_ctx tls_wrap;
394 
395  int initial_opcode; /* our initial P_ opcode */
396  struct session_id session_id; /* our random session ID */
397 
403  int key_id;
404 
405  int limit_next; /* used for traffic shaping on the control channel */
407  int verify_maxlevel;
408 
409  char *common_name;
410 
413 #ifdef ENABLE_PF
414  uint32_t common_name_hashval;
415 #endif
416 
417  bool verified; /* true if peer certificate was verified against CA */
418 
419  /* not-yet-authenticated incoming client */
420  struct link_socket_actual untrusted_addr;
422  struct key_state key[KS_SIZE];
423 };
424 
442 #define TM_ACTIVE 0
443 #define TM_UNTRUSTED 1
445 #define TM_LAME_DUCK 2
446 #define TM_SIZE 3
452 /*
453  * The number of keys we will scan on encrypt or decrypt. The first
454  * is the "active" key. The second is the lame_duck or retiring key
455  * associated with the active key's session ID. The third is a detached
456  * lame duck session that only occurs in situations where a key renegotiate
457  * failed on the active key, but a lame duck key was still valid. By
458  * preserving the lame duck session, we can be assured of having a data
459  * channel key available even when network conditions are so bad that
460  * we can't negotiate a new key within the time allotted.
461  */
462 #define KEY_SCAN_SIZE 3
464 
478 struct tls_multi
479 {
480  /* used to coordinate access between main thread and TLS thread */
481  /*MUTEX_PTR_DEFINE (mutex);*/
482 
483  /* const options and config info */
484  struct tls_options opt;
485 
486  struct key_state *key_scan[KEY_SCAN_SIZE];
491  /*
492  * used by tls_pre_encrypt to communicate the encrypt key
493  * to tls_post_encrypt()
494  */
495  struct key_state *save_ks; /* temporary pointer used between pre/post routines */
497  /*
498  * Used to return outgoing address from
499  * tls_multi_process.
500  */
501  struct link_socket_actual to_link_addr;
503  int n_sessions;
506  /*
507  * Number of errors.
508  */
509  int n_hard_errors; /* errors due to TLS negotiation failure */
510  int n_soft_errors; /* errors due to unrecognized or failed-to-authenticate incoming packets */
511 
512  /*
513  * Our locked common name, username, and cert hashes (cannot change during the life of this tls_multi object)
514  */
515  char *locked_cn;
516  char *locked_username;
517  struct cert_hash_set *locked_cert_hash_set;
518 
519 #ifdef ENABLE_DEF_AUTH
520  /*
521  * An error message to send to client on AUTH_FAILED
522  */
523  char *client_reason;
524 
525  /* Time of last call to tls_authentication_status */
526  time_t tas_last;
527 #endif
529 #if P2MP_SERVER
530  /*
531  * A multi-line string of general-purpose info received from peer
532  * over control channel.
533  */
534  char *peer_info;
535 #endif
536 
537  /* For P_DATA_V2 */
538  uint32_t peer_id;
539  bool use_peer_id;
540 
541  char *remote_ciphername;
543  char *auth_token;
547  time_t auth_token_tstamp;
548  bool auth_token_sent;
550  /*
551  * Our session objects.
552  */
553  struct tls_session session[TM_SIZE];
557 };
558 
560 #define SHOW_TLS_CIPHER_LIST_WARNING \
561  "Be aware that that whether a cipher suite in this list can actually work\n" \
562  "depends on the specific setup of both peers. See the man page entries of\n" \
563  "--tls-cipher and --show-tls for more details.\n\n"
564 
565 #endif /* SSL_COMMON_H_ */
Security parameter state for processing data channel packets.
Definition: crypto.h:234
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:161
Container for both halves of random material to be used in key method 2 data channel key generation...
Definition: ssl_common.h:138
Packet geometry parameters.
Definition: mtu.h:93
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:496
hash_algo_type
Types referencing specific message digest hashing algorithms.
#define MAX_PARMS
Definition: options.h:53
Structure containing the hashes for a full certificate chain.
Definition: ssl_verify.h:61
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition: ssl_common.h:220
#define KEY_SCAN_SIZE
Definition: ssl_common.h:480
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:374
Container for one half of random material to be used in key method 2 data channel key generation...
Definition: ssl_common.h:121
unsigned __int32 uint32_t
Definition: config-msvc.h:121
unsigned int counter_type
Definition: common.h:38
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:463
struct key_source server
Random provided by server.
Definition: ssl_common.h:140
unsigned __int8 uint8_t
Definition: config-msvc.h:123
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:66
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:400
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition: reliable.h:66
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
The reliability layer storage structure for one VPN tunnel&#39;s control channel in one direction...
Definition: reliable.h:90
Definition: misc.h:49
int interval_t
Definition: common.h:45
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:153