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 
76 #define S_ERROR -1
77 #define S_UNDEF 0
79 #define S_INITIAL 1
82 #define S_PRE_START 2
85 #define S_START 3
87 #define S_SENT_KEY 4
89 #define S_GOT_KEY 5
92 #define S_ACTIVE 6
96 /* Note that earlier versions also had a S_OP_NORMAL state that was
97  * virtually identical with S_ACTIVE and the code still assumes everything
98  * >= S_ACTIVE to be fully operational */
99 
107 struct key_source {
108  uint8_t pre_master[48];
111  uint8_t random1[32];
114  uint8_t random2[32];
116 };
117 
118 
124 struct key_source2 {
125  struct key_source client;
126  struct key_source server;
127 };
128 
129 
141  KS_AUTH_TRUE
146 };
147 
149 {
150  char *auth_control_file;
151  char *auth_pending_file;
152  unsigned int auth_control_status;
153 };
154 
173 struct key_state
174 {
175  int state;
176 
181  int key_id;
182 
183  struct key_state_ssl ks_ssl; /* contains SSL object and BIOs for the control channel */
184 
185  time_t initial; /* when we created this session */
186  time_t established; /* when our state went S_ACTIVE */
187  time_t must_negotiate; /* key negotiation times out if not finished before this time */
188  time_t must_die; /* this object is destroyed at this time */
189  time_t peer_last_packet; /* Last time we received a packet in this control session */
190 
191  int initial_opcode; /* our initial P_ opcode */
192  struct session_id session_id_remote; /* peer's random session ID */
193  struct link_socket_actual remote_addr; /* peer's IP addr */
194 
195  struct crypto_options crypto_options;/* data channel crypto options */
196 
197  struct key_source2 *key_src; /* source entropy for key expansion */
199  struct buffer plaintext_read_buf;
200  struct buffer plaintext_write_buf;
201  struct buffer ack_write_buf;
202 
203  struct reliable *send_reliable; /* holds a copy of outgoing packets until ACK received */
204  struct reliable *rec_reliable; /* order incoming ciphertext packets before we pass to TLS */
205  struct reliable_ack *rec_ack; /* buffers all packet IDs we want to ACK back to sender */
206 
207  struct buffer_list *paybuf;
208 
209  counter_type n_bytes; /* how many bytes sent/recvd since last key exchange */
210  counter_type n_packets; /* how many packets sent/recvd since last key exchange */
212  /*
213  * If bad username/password, TLS connection will come up but 'authenticated' will be false.
214  */
215  enum ks_auth_state authenticated;
216  time_t auth_deferred_expire;
218 #ifdef ENABLE_MANAGEMENT
219  unsigned int mda_key_id;
220  unsigned int mda_status;
221 #endif
222  time_t acf_last_mod;
223 
224  struct auth_deferred_status plugin_auth;
225  struct auth_deferred_status script_auth;
226 };
229 struct tls_wrap_ctx
230 {
231  enum {
232  TLS_WRAP_NONE = 0,
233  TLS_WRAP_AUTH,
234  TLS_WRAP_CRYPT,
235  } mode;
236  struct crypto_options opt;
237  struct buffer work;
238  struct key_ctx tls_crypt_v2_server_key;
239  const struct buffer *tls_crypt_v2_wkc;
241  struct buffer tls_crypt_v2_metadata;
242  bool cleanup_key_ctx;
244 };
246 /*
247  * Our const options, obtained directly or derived from
248  * command line options.
249  */
251 {
252  /* our master TLS context from which all SSL objects derived */
253  struct tls_root_ctx ssl_ctx;
255  /* data channel cipher, hmac, and key lengths */
256  struct key_type key_type;
257 
258  /* true if we are a TLS server, client otherwise */
259  bool server;
260 
261  /* if true, don't xmit until first packet from peer is received */
262  bool xmit_hold;
263 
264  /* local and remote options strings
265  * that must match between client and server */
266  const char *local_options;
267  const char *remote_options;
269  /* from command line */
270  bool replay;
271  bool single_session;
272  bool disable_occ;
273  int mode;
274  bool pull;
275  int push_peer_info_detail;
276  int transition_window;
277  int handshake_window;
278  interval_t packet_timeout;
279  int renegotiate_bytes;
280  int renegotiate_packets;
281  interval_t renegotiate_seconds;
283  /* cert verification parms */
284  const char *verify_command;
285  const char *verify_export_cert;
286  int verify_x509_type;
287  const char *verify_x509_name;
288  const char *crl_file;
289  bool crl_file_inline;
290  int ns_cert_type;
291  unsigned remote_cert_ku[MAX_PARMS];
292  const char *remote_cert_eku;
293  struct verify_hash_list *verify_hash;
294  int verify_hash_depth;
295  bool verify_hash_no_ca;
296  hash_algo_type verify_hash_algo;
297 #ifdef ENABLE_X509ALTUSERNAME
298  char *x509_username_field[MAX_PARMS];
299 #else
300  char *x509_username_field[2];
301 #endif
303  /* struct crypto_option flags */
304  unsigned int crypto_flags;
306  int replay_window; /* --replay-window parm */
307  int replay_time; /* --replay-window parm */
308  bool tcp_mode;
309 
310  const char *config_ciphername;
311  const char *config_ncp_ciphers;
312  bool ncp_enabled;
313 
314  bool tls_crypt_v2;
315  const char *tls_crypt_v2_verify_script;
318  struct tls_wrap_ctx tls_wrap;
320  struct frame frame;
321 
322  /* used for username/password authentication */
323  const char *auth_user_pass_verify_script;
324  bool auth_user_pass_verify_script_via_file;
325  const char *tmp_dir;
326  const char *auth_user_pass_file;
328  bool auth_token_generate;
331  bool auth_token_call_auth;
332  unsigned int auth_token_lifetime;
333 
334  struct key_ctx auth_token_key;
336  /* use the client-config-dir as a positive authenticator */
337  const char *client_config_dir_exclusive;
339  /* instance-wide environment variable set */
340  struct env_set *es;
341  openvpn_net_ctx_t *net_ctx;
342  const struct plugin_list *plugins;
344  /* compression parms */
345 #ifdef USE_COMP
346  struct compress_options comp_options;
347 #endif
348 
349  /* configuration file SSL-related boolean and low-permutation options */
350 #define SSLF_CLIENT_CERT_NOT_REQUIRED (1<<0)
351 #define SSLF_CLIENT_CERT_OPTIONAL (1<<1)
352 #define SSLF_USERNAME_AS_COMMON_NAME (1<<2)
353 #define SSLF_AUTH_USER_PASS_OPTIONAL (1<<3)
354 #define SSLF_OPT_VERIFY (1<<4)
355 #define SSLF_CRL_VERIFY_DIR (1<<5)
356 #define SSLF_TLS_VERSION_MIN_SHIFT 6
357 #define SSLF_TLS_VERSION_MIN_MASK 0xF /* (uses bit positions 6 to 9) */
358 #define SSLF_TLS_VERSION_MAX_SHIFT 10
359 #define SSLF_TLS_VERSION_MAX_MASK 0xF /* (uses bit positions 10 to 13) */
360 #define SSLF_TLS_DEBUG_ENABLED (1<<14)
361  unsigned int ssl_flags;
363 #ifdef ENABLE_MANAGEMENT
364  struct man_def_auth_context *mda_context;
365 #endif
367  const struct x509_track *x509_track;
369 #ifdef ENABLE_MANAGEMENT
370  const struct static_challenge_info *sci;
371 #endif
373  /* --gremlin bits */
374  int gremlin;
375 
376  /* Keying Material Exporter [RFC 5705] parameters */
377  const char *ekm_label;
378  size_t ekm_label_size;
379  size_t ekm_size;
380 };
381 
389 #define KS_PRIMARY 0
390 #define KS_LAME_DUCK 1
392 #define KS_SIZE 2
412 struct tls_session
413 {
414  /* const options and config info */
415  struct tls_options *opt;
416 
417  /* during hard reset used to control burst retransmit */
418  bool burst;
419 
420  /* authenticate control packets */
421  struct tls_wrap_ctx tls_wrap;
422 
423  int initial_opcode; /* our initial P_ opcode */
424  struct session_id session_id; /* our random session ID */
431  int key_id;
432 
433  int limit_next; /* used for traffic shaping on the control channel */
435  int verify_maxlevel;
437  char *common_name;
438 
440 
441 #ifdef ENABLE_PF
442  uint32_t common_name_hashval;
443 #endif
445  bool verified; /* true if peer certificate was verified against CA */
447  /* not-yet-authenticated incoming client */
448  struct link_socket_actual untrusted_addr;
449 
451 };
470 #define TM_ACTIVE 0
471 #define TM_UNTRUSTED 1
473 #define TM_LAME_DUCK 2
474 #define TM_SIZE 3
480 /*
481  * The number of keys we will scan on encrypt or decrypt. The first
482  * is the "active" key. The second is the lame_duck or retiring key
483  * associated with the active key's session ID. The third is a detached
484  * lame duck session that only occurs in situations where a key renegotiate
485  * failed on the active key, but a lame duck key was still valid. By
486  * preserving the lame duck session, we can be assured of having a data
487  * channel key available even when network conditions are so bad that
488  * we can't negotiate a new key within the time allotted.
489  */
490 #define KEY_SCAN_SIZE 3
491 
492 
493 /* client authentication state, CAS_SUCCEEDED must be 0 since
494  * non multi code path still checks this variable but does not initialise it
495  * so the code depends on zero initialisation */
497  CAS_SUCCEEDED=0,
498  CAS_PENDING,
501  CAS_FAILED,
502 };
503 
504 
518 struct tls_multi
519 {
520  /* used to coordinate access between main thread and TLS thread */
521  /*MUTEX_PTR_DEFINE (mutex);*/
522 
523  /* const options and config info */
524  struct tls_options opt;
525 
526  /*
527  * used by tls_pre_encrypt to communicate the encrypt key
528  * to tls_post_encrypt()
529  */
530  struct key_state *save_ks; /* temporary pointer used between pre/post routines */
531 
532  /*
533  * Used to return outgoing address from
534  * tls_multi_process.
535  */
536  struct link_socket_actual to_link_addr;
537 
538  int n_sessions;
540  enum client_connect_status multi_state;
541 
542  /*
543  * Number of errors.
544  */
545  int n_hard_errors; /* errors due to TLS negotiation failure */
546  int n_soft_errors; /* errors due to unrecognized or failed-to-authenticate incoming packets */
547 
548  /*
549  * Our locked common name, username, and cert hashes (cannot change during the life of this tls_multi object)
550  */
551  char *locked_cn;
552  char *locked_username;
553  struct cert_hash_set *locked_cert_hash_set;
554 
555  /* Time of last call to tls_authentication_status */
556  time_t tas_last;
557 
558  /*
559  * An error message to send to client on AUTH_FAILED
560  */
561  char *client_reason;
562 
563  /*
564  * A multi-line string of general-purpose info received from peer
565  * over control channel.
566  */
567  char *peer_info;
568  char *auth_token;
572  char *auth_token_initial;
576 #define AUTH_TOKEN_HMAC_OK (1<<0)
577 
578 #define AUTH_TOKEN_EXPIRED (1<<1)
579 
580 #define AUTH_TOKEN_VALID_EMPTYUSER (1<<2)
581 
588  int auth_token_state_flags;
591  /* For P_DATA_V2 */
592  uint32_t peer_id;
593  bool use_peer_id;
594 
595  char *remote_ciphername;
596  bool remote_usescomp;
598  /*
599  * Our session objects.
600  */
601  struct tls_session session[TM_SIZE];
605 };
606 
610 static inline struct key_state *
611 get_key_scan(struct tls_multi *multi, int index)
612 {
613  switch (index)
614  {
615  case 0:
616  return &multi->session[TM_ACTIVE].key[KS_PRIMARY];
617  case 1:
618  return &multi->session[TM_ACTIVE].key[KS_LAME_DUCK];
619  case 2:
620  return &multi->session[TM_LAME_DUCK].key[KS_LAME_DUCK];
621  default:
622  ASSERT(false);
623  return NULL; /* NOTREACHED */
624  }
625 }
630 static inline const struct key_state *
631 get_primary_key(const struct tls_multi *multi)
632 {
633  return &multi->session[TM_ACTIVE].key[KS_PRIMARY];
634 }
635 
636 #endif /* SSL_COMMON_H_ */
Security parameter state for processing data channel packets.
Definition: crypto.h:232
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:483
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:185
struct key_state key[KS_SIZE]
Definition: ssl_common.h:463
Container for both halves of random material to be used in key method 2 data channel key generation...
Definition: ssl_common.h:136
Packet geometry parameters.
Definition: mtu.h:93
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:646
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:533
Key state is authenticated.
Definition: ssl_common.h:153
hash_algo_type
Types referencing specific message digest hashing algorithms.
#define MAX_PARMS
Definition: options.h:51
void * openvpn_net_ctx_t
Definition: networking.h:26
#define ASSERT(x)
Definition: error.h:200
uint8_t random1[32]
Seed used for master secret generation, provided by both client and server.
Definition: ssl_common.h:123
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer...
Definition: ssl_common.h:616
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:241
uint8_t random2[32]
Seed used for key expansion, provided by both client and server.
Definition: ssl_common.h:126
Key state authentication is being deferred, by async auth.
Definition: ssl_common.h:151
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:405
Container for one half of random material to be used in key method 2 data channel key generation...
Definition: ssl_common.h:119
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:401
Key state is not authenticated.
Definition: ssl_common.h:150
ks_auth_state
This reflects the (server side) authentication state after the TLS session has been established and k...
Definition: ssl_common.h:149
at least handler succeeded, no result yet
Definition: ssl_common.h:515
uint64_t counter_type
Definition: common.h:30
static struct user_pass auth_token
Definition: ssl.c:390
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:164
static struct key_state * get_key_scan(struct tls_multi *multi, int index)
gets an item of key_state objects in the order they should be scanned by data channel modules...
Definition: ssl_common.h:626
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:488
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:104
#define KS_LAME_DUCK
Key state index that will retire soon.
Definition: ssl_common.h:402
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:425
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition: reliable.h:70
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:97
#define TM_LAME_DUCK
Old tls_session.
Definition: ssl_common.h:487
int interval_t
Definition: common.h:36
client_connect_status
Definition: ssl_common.h:511
uint8_t pre_master[48]
Random used for master secret generation, provided only by client OpenVPN peer.
Definition: ssl_common.h:120
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151