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-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 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  int push_peer_info_detail;
253  int transition_window;
254  int handshake_window;
255  interval_t packet_timeout;
256  int renegotiate_bytes;
257  int renegotiate_packets;
258  interval_t renegotiate_seconds;
260  /* cert verification parms */
261  const char *verify_command;
262  const char *verify_export_cert;
263  int verify_x509_type;
264  const char *verify_x509_name;
265  const char *crl_file;
266  const char *crl_file_inline;
267  int ns_cert_type;
268  unsigned remote_cert_ku[MAX_PARMS];
269  const char *remote_cert_eku;
270  uint8_t *verify_hash;
271  hash_algo_type verify_hash_algo;
272  char *x509_username_field;
273 
274  /* allow openvpn config info to be
275  * passed over control channel */
276  bool pass_config_info;
278  /* struct crypto_option flags */
279  unsigned int crypto_flags;
281  int replay_window; /* --replay-window parm */
282  int replay_time; /* --replay-window parm */
283  bool tcp_mode;
285  const char *config_ciphername;
286  const char *config_authname;
287  bool ncp_enabled;
288 
290  struct tls_wrap_ctx tls_wrap;
291 
292  /* frame parameters for TLS control channel */
293  struct frame frame;
295  /* used for username/password authentication */
296  const char *auth_user_pass_verify_script;
297  bool auth_user_pass_verify_script_via_file;
298  const char *tmp_dir;
299  const char *auth_user_pass_file;
300  bool auth_token_generate;
302  unsigned int auth_token_lifetime;
304  /* use the client-config-dir as a positive authenticator */
305  const char *client_config_dir_exclusive;
307  /* instance-wide environment variable set */
308  struct env_set *es;
309  const struct plugin_list *plugins;
311  /* compression parms */
312 #ifdef USE_COMP
313  struct compress_options comp_options;
314 #endif
316  /* configuration file SSL-related boolean and low-permutation options */
317 #define SSLF_CLIENT_CERT_NOT_REQUIRED (1<<0)
318 #define SSLF_CLIENT_CERT_OPTIONAL (1<<1)
319 #define SSLF_USERNAME_AS_COMMON_NAME (1<<2)
320 #define SSLF_AUTH_USER_PASS_OPTIONAL (1<<3)
321 #define SSLF_OPT_VERIFY (1<<4)
322 #define SSLF_CRL_VERIFY_DIR (1<<5)
323 #define SSLF_TLS_VERSION_MIN_SHIFT 6
324 #define SSLF_TLS_VERSION_MIN_MASK 0xF /* (uses bit positions 6 to 9) */
325 #define SSLF_TLS_VERSION_MAX_SHIFT 10
326 #define SSLF_TLS_VERSION_MAX_MASK 0xF /* (uses bit positions 10 to 13) */
327  unsigned int ssl_flags;
328 
329 #ifdef MANAGEMENT_DEF_AUTH
330  struct man_def_auth_context *mda_context;
331 #endif
333  const struct x509_track *x509_track;
335 #ifdef ENABLE_CLIENT_CR
336  const struct static_challenge_info *sci;
337 #endif
339  /* --gremlin bits */
340  int gremlin;
341 
342  /* Keying Material Exporter [RFC 5705] parameters */
343  const char *ekm_label;
344  size_t ekm_label_size;
345  size_t ekm_size;
346 };
347 
355 #define KS_PRIMARY 0
356 #define KS_LAME_DUCK 1
358 #define KS_SIZE 2
362 #define AUTH_TOKEN_SIZE 32
382 struct tls_session
383 {
384  /* const options and config info */
385  struct tls_options *opt;
386 
387  /* during hard reset used to control burst retransmit */
388  bool burst;
389 
390  /* authenticate control packets */
391  struct tls_wrap_ctx tls_wrap;
392 
393  int initial_opcode; /* our initial P_ opcode */
394  struct session_id session_id; /* our random session ID */
395 
401  int key_id;
402 
403  int limit_next; /* used for traffic shaping on the control channel */
405  int verify_maxlevel;
406 
407  char *common_name;
408 
411 #ifdef ENABLE_PF
412  uint32_t common_name_hashval;
413 #endif
414 
415  bool verified; /* true if peer certificate was verified against CA */
416 
417  /* not-yet-authenticated incoming client */
418  struct link_socket_actual untrusted_addr;
420  struct key_state key[KS_SIZE];
421 };
422 
440 #define TM_ACTIVE 0
441 #define TM_UNTRUSTED 1
443 #define TM_LAME_DUCK 2
444 #define TM_SIZE 3
450 /*
451  * The number of keys we will scan on encrypt or decrypt. The first
452  * is the "active" key. The second is the lame_duck or retiring key
453  * associated with the active key's session ID. The third is a detached
454  * lame duck session that only occurs in situations where a key renegotiate
455  * failed on the active key, but a lame duck key was still valid. By
456  * preserving the lame duck session, we can be assured of having a data
457  * channel key available even when network conditions are so bad that
458  * we can't negotiate a new key within the time allotted.
459  */
460 #define KEY_SCAN_SIZE 3
462 
476 struct tls_multi
477 {
478  /* used to coordinate access between main thread and TLS thread */
479  /*MUTEX_PTR_DEFINE (mutex);*/
480 
481  /* const options and config info */
482  struct tls_options opt;
483 
484  struct key_state *key_scan[KEY_SCAN_SIZE];
489  /*
490  * used by tls_pre_encrypt to communicate the encrypt key
491  * to tls_post_encrypt()
492  */
493  struct key_state *save_ks; /* temporary pointer used between pre/post routines */
495  /*
496  * Used to return outgoing address from
497  * tls_multi_process.
498  */
499  struct link_socket_actual to_link_addr;
501  int n_sessions;
504  /*
505  * Number of errors.
506  */
507  int n_hard_errors; /* errors due to TLS negotiation failure */
508  int n_soft_errors; /* errors due to unrecognized or failed-to-authenticate incoming packets */
509 
510  /*
511  * Our locked common name, username, and cert hashes (cannot change during the life of this tls_multi object)
512  */
513  char *locked_cn;
514  char *locked_username;
515  struct cert_hash_set *locked_cert_hash_set;
516 
517 #ifdef ENABLE_DEF_AUTH
518  /*
519  * An error message to send to client on AUTH_FAILED
520  */
521  char *client_reason;
522 
523  /* Time of last call to tls_authentication_status */
524  time_t tas_last;
525 #endif
527 #if P2MP_SERVER
528  /*
529  * A multi-line string of general-purpose info received from peer
530  * over control channel.
531  */
532  char *peer_info;
533 #endif
534 
535  /* For P_DATA_V2 */
536  uint32_t peer_id;
537  bool use_peer_id;
538 
539  char *remote_ciphername;
541  char *auth_token;
545  time_t auth_token_tstamp;
546  bool auth_token_sent;
548  /*
549  * Our session objects.
550  */
551  struct tls_session session[TM_SIZE];
555 };
556 
558 #define SHOW_TLS_CIPHER_LIST_WARNING \
559  "Be aware that that whether a cipher suite in this list can actually work\n" \
560  "depends on the specific setup of both peers. See the man page entries of\n" \
561  "--tls-cipher and --show-tls for more details.\n\n"
562 
563 #endif /* SSL_COMMON_H_ */
Security parameter state for processing data channel packets.
Definition: crypto.h:232
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:494
hash_algo_type
Types referencing specific message digest hashing algorithms.
#define MAX_PARMS
Definition: options.h:51
Structure containing the hashes for a full certificate chain.
Definition: ssl_verify.h:59
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition: ssl_common.h:220
#define KEY_SCAN_SIZE
Definition: ssl_common.h:478
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:372
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:120
unsigned int counter_type
Definition: common.h:38
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:461
struct key_source server
Random provided by server.
Definition: ssl_common.h:140
unsigned __int8 uint8_t
Definition: config-msvc.h:122
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:398
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition: reliable.h:64
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:88
int interval_t
Definition: common.h:45
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151