]> git.ipfire.org Git - thirdparty/openvpn.git/blame - src/openvpn/ssl_common.h
Fix a null-pointer dereference in establish_http_proxy_passthru()
[thirdparty/openvpn.git] / src / openvpn / ssl_common.h
CommitLineData
9a160b79
AJ
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 *
58716979
DS
8 * Copyright (C) 2002-2017 OpenVPN Technologies, Inc. <sales@openvpn.net>
9 * Copyright (C) 2010-2017 Fox Crypto B.V. <openvpn@fox-it.com>
9a160b79
AJ
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
21 * along with this program (see the file COPYING included with this
22 * distribution); if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26/**
27 * @file Control Channel Common Data Structures
28 */
29
30#ifndef SSL_COMMON_H_
31#define SSL_COMMON_H_
32
33#include "session_id.h"
34#include "socket.h"
35#include "packet_id.h"
36#include "crypto.h"
37#include "options.h"
38
39#include "ssl_backend.h"
40
289a8bb8
AJ
41/* passwords */
42#define UP_TYPE_AUTH "Auth"
43#define UP_TYPE_PRIVATE_KEY "Private Key"
44
d0811e64
AJ
45/** @addtogroup control_processor
46 * @{ */
47/**
48 * @name Control channel negotiation states
49 *
50 * These states represent the different phases of control channel
51 * negotiation between OpenVPN peers. OpenVPN servers and clients
52 * progress through the states in a different order, because of their
53 * different roles during exchange of random material. The references to
54 * the \c key_source2 structure in the list below is only valid if %key
55 * method 2 is being used. See the \link key_generation data channel key
56 * generation\endlink related page for more information.
57 *
58 * Clients follow this order:
59 * -# \c S_INITIAL, ready to begin three-way handshake and control
60 * channel negotiation.
61 * -# \c S_PRE_START, have started three-way handshake, waiting for
62 * acknowledgment from remote.
63 * -# \c S_START, initial three-way handshake complete.
64 * -# \c S_SENT_KEY, have sent local part of \c key_source2 random
65 * material.
66 * -# \c S_GOT_KEY, have received remote part of \c key_source2 random
67 * material.
68 * -# \c S_ACTIVE, normal operation during remaining handshake window.
69 * -# \c S_NORMAL_OP, normal operation.
70 *
71 * Servers follow the same order, except for \c S_SENT_KEY and \c
72 * S_GOT_KEY being reversed, because the server first receives the
73 * client's \c key_source2 random material before generating and sending
74 * its own.
75 *
76 * @{
77 */
78#define S_ERROR -1 /**< Error state. */
79#define S_UNDEF 0 /**< Undefined state, used after a \c
80 * key_state is cleaned up. */
81#define S_INITIAL 1 /**< Initial \c key_state state after
82 * initialization by \c key_state_init()
83 * before start of three-way handshake. */
84#define S_PRE_START 2 /**< Waiting for the remote OpenVPN peer
85 * to acknowledge during the initial
86 * three-way handshake. */
87#define S_START 3 /**< Three-way handshake is complete,
88 * start of key exchange. */
89#define S_SENT_KEY 4 /**< Local OpenVPN process has sent its
90 * part of the key material. */
91#define S_GOT_KEY 5 /**< Local OpenVPN process has received
92 * the remote's part of the key
93 * material. */
94#define S_ACTIVE 6 /**< Operational \c key_state state
95 * immediately after negotiation has
96 * completed while still within the
97 * handshake window. */
98/* ready to exchange data channel packets */
99#define S_NORMAL_OP 7 /**< Normal operational \c key_state
100 * state. */
101/** @} name Control channel negotiation states */
102/** @} addtogroup control_processor */
103
67d8a0d4
AJ
104/**
105 * Container for one half of random material to be used in %key method 2
106 * \ref key_generation "data channel key generation".
107 * @ingroup control_processor
108 */
109struct key_source {
81d882d5 110 uint8_t pre_master[48]; /**< Random used for master secret
67d8a0d4
AJ
111 * generation, provided only by client
112 * OpenVPN peer. */
81d882d5 113 uint8_t random1[32]; /**< Seed used for master secret
67d8a0d4
AJ
114 * generation, provided by both client
115 * and server. */
81d882d5 116 uint8_t random2[32]; /**< Seed used for key expansion, provided
67d8a0d4
AJ
117 * by both client and server. */
118};
119
120
121/**
122 * Container for both halves of random material to be used in %key method
123 * 2 \ref key_generation "data channel key generation".
124 * @ingroup control_processor
125 */
126struct key_source2 {
81d882d5
DS
127 struct key_source client; /**< Random provided by client. */
128 struct key_source server; /**< Random provided by server. */
67d8a0d4
AJ
129};
130
131/**
132 * Security parameter state of one TLS and data channel %key session.
133 * @ingroup control_processor
134 *
135 * This structure represents one security parameter session between
136 * OpenVPN peers. It includes the control channel TLS state and the data
137 * channel crypto state. It also contains the reliability layer
138 * structures used for control channel messages.
139 *
140 * A new \c key_state structure is initialized for each hard or soft
141 * reset.
142 *
143 * @see
144 * - This structure should be initialized using the \c key_state_init()
145 * function.
146 * - This structure should be cleaned up using the \c key_state_free()
147 * function.
148 */
149struct key_state
150{
81d882d5 151 int state;
97894360 152
81d882d5
DS
153 /**
154 * Key id for this key_state, inherited from struct tls_session.
155 * @see tls_session::key_id.
156 */
157 int key_id;
67d8a0d4 158
81d882d5 159 struct key_state_ssl ks_ssl; /* contains SSL object and BIOs for the control channel */
67d8a0d4 160
81d882d5
DS
161 time_t established; /* when our state went S_ACTIVE */
162 time_t must_negotiate; /* key negotiation times out if not finished before this time */
163 time_t must_die; /* this object is destroyed at this time */
67d8a0d4 164
81d882d5
DS
165 int initial_opcode; /* our initial P_ opcode */
166 struct session_id session_id_remote; /* peer's random session ID */
167 struct link_socket_actual remote_addr; /* peer's IP addr */
67d8a0d4 168
81d882d5 169 struct crypto_options crypto_options;/* data channel crypto options */
67d8a0d4 170
81d882d5 171 struct key_source2 *key_src; /* source entropy for key expansion */
67d8a0d4 172
81d882d5
DS
173 struct buffer plaintext_read_buf;
174 struct buffer plaintext_write_buf;
175 struct buffer ack_write_buf;
67d8a0d4 176
81d882d5
DS
177 struct reliable *send_reliable; /* holds a copy of outgoing packets until ACK received */
178 struct reliable *rec_reliable; /* order incoming ciphertext packets before we pass to TLS */
179 struct reliable_ack *rec_ack; /* buffers all packet IDs we want to ACK back to sender */
67d8a0d4 180
81d882d5 181 struct buffer_list *paybuf;
67d8a0d4 182
81d882d5
DS
183 counter_type n_bytes; /* how many bytes sent/recvd since last key exchange */
184 counter_type n_packets; /* how many packets sent/recvd since last key exchange */
67d8a0d4 185
81d882d5
DS
186 /*
187 * If bad username/password, TLS connection will come up but 'authenticated' will be false.
188 */
189 bool authenticated;
190 time_t auth_deferred_expire;
67d8a0d4
AJ
191
192#ifdef ENABLE_DEF_AUTH
81d882d5
DS
193 /* If auth_deferred is true, authentication is being deferred */
194 bool auth_deferred;
67d8a0d4 195#ifdef MANAGEMENT_DEF_AUTH
81d882d5
DS
196 unsigned int mda_key_id;
197 unsigned int mda_status;
67d8a0d4
AJ
198#endif
199#ifdef PLUGIN_DEF_AUTH
81d882d5
DS
200 unsigned int auth_control_status;
201 time_t acf_last_mod;
202 char *auth_control_file;
67d8a0d4
AJ
203#endif
204#endif
205};
206
c6e24fa3
SK
207/** Control channel wrapping (--tls-auth/--tls-crypt) context */
208struct tls_wrap_ctx
209{
81d882d5
DS
210 enum {
211 TLS_WRAP_NONE = 0, /**< No control channel wrapping */
212 TLS_WRAP_AUTH, /**< Control channel authentication */
213 TLS_WRAP_CRYPT, /**< Control channel encryption and authentication */
214 } mode; /**< Control channel wrapping mode */
215 struct crypto_options opt; /**< Crypto state */
216 struct buffer work; /**< Work buffer (only for --tls-crypt) */
c6e24fa3
SK
217};
218
67d8a0d4
AJ
219/*
220 * Our const options, obtained directly or derived from
221 * command line options.
222 */
223struct tls_options
224{
81d882d5
DS
225 /* our master TLS context from which all SSL objects derived */
226 struct tls_root_ctx ssl_ctx;
67d8a0d4 227
81d882d5
DS
228 /* data channel cipher, hmac, and key lengths */
229 struct key_type key_type;
67d8a0d4 230
81d882d5
DS
231 /* true if we are a TLS server, client otherwise */
232 bool server;
67d8a0d4 233
81d882d5
DS
234 /* if true, don't xmit until first packet from peer is received */
235 bool xmit_hold;
67d8a0d4
AJ
236
237#ifdef ENABLE_OCC
81d882d5
DS
238 /* local and remote options strings
239 * that must match between client and server */
240 const char *local_options;
241 const char *remote_options;
67d8a0d4
AJ
242#endif
243
81d882d5
DS
244 /* from command line */
245 int key_method;
246 bool replay;
247 bool single_session;
67d8a0d4 248#ifdef ENABLE_OCC
81d882d5 249 bool disable_occ;
67d8a0d4 250#endif
81d882d5
DS
251 int mode;
252 bool pull;
67d8a0d4 253#ifdef ENABLE_PUSH_PEER_INFO
81d882d5 254 int push_peer_info_detail;
67d8a0d4 255#endif
81d882d5
DS
256 int transition_window;
257 int handshake_window;
258 interval_t packet_timeout;
259 int renegotiate_bytes;
260 int renegotiate_packets;
261 interval_t renegotiate_seconds;
262
263 /* cert verification parms */
264 const char *verify_command;
265 const char *verify_export_cert;
266 int verify_x509_type;
267 const char *verify_x509_name;
268 const char *crl_file;
269 const char *crl_file_inline;
270 int ns_cert_type;
271 unsigned remote_cert_ku[MAX_PARMS];
272 const char *remote_cert_eku;
273 uint8_t *verify_hash;
2193d7c0 274 hash_algo_type verify_hash_algo;
81d882d5
DS
275 char *x509_username_field;
276
277 /* allow openvpn config info to be
278 * passed over control channel */
279 bool pass_config_info;
280
281 /* struct crypto_option flags */
282 unsigned int crypto_flags;
283
284 int replay_window; /* --replay-window parm */
285 int replay_time; /* --replay-window parm */
286 bool tcp_mode;
287
288 const char *config_ciphername;
289 const char *config_authname;
290 bool ncp_enabled;
291
292 /** TLS handshake wrapping state */
293 struct tls_wrap_ctx tls_wrap;
294
295 /* frame parameters for TLS control channel */
296 struct frame frame;
297
298 /* used for username/password authentication */
299 const char *auth_user_pass_verify_script;
300 bool auth_user_pass_verify_script_via_file;
301 const char *tmp_dir;
302 const char *auth_user_pass_file;
303 bool auth_token_generate; /**< Generate auth-tokens on successful user/pass auth,
58066d04 304 * set via options->auth_token_generate. */
81d882d5 305 unsigned int auth_token_lifetime;
67d8a0d4 306
81d882d5
DS
307 /* use the client-config-dir as a positive authenticator */
308 const char *client_config_dir_exclusive;
67d8a0d4 309
81d882d5
DS
310 /* instance-wide environment variable set */
311 struct env_set *es;
312 const struct plugin_list *plugins;
67d8a0d4 313
81d882d5 314 /* compression parms */
38d96bd7 315#ifdef USE_COMP
81d882d5 316 struct compress_options comp_options;
38d96bd7
JY
317#endif
318
81d882d5
DS
319 /* configuration file SSL-related boolean and low-permutation options */
320#define SSLF_CLIENT_CERT_NOT_REQUIRED (1<<0)
321#define SSLF_CLIENT_CERT_OPTIONAL (1<<1)
322#define SSLF_USERNAME_AS_COMMON_NAME (1<<2)
323#define SSLF_AUTH_USER_PASS_OPTIONAL (1<<3)
324#define SSLF_OPT_VERIFY (1<<4)
325#define SSLF_CRL_VERIFY_DIR (1<<5)
326#define SSLF_TLS_VERSION_MIN_SHIFT 6
327#define SSLF_TLS_VERSION_MIN_MASK 0xF /* (uses bit positions 6 to 9) */
328#define SSLF_TLS_VERSION_MAX_SHIFT 10
329#define SSLF_TLS_VERSION_MAX_MASK 0xF /* (uses bit positions 10 to 13) */
330 unsigned int ssl_flags;
67d8a0d4
AJ
331
332#ifdef MANAGEMENT_DEF_AUTH
81d882d5 333 struct man_def_auth_context *mda_context;
67d8a0d4
AJ
334#endif
335
81d882d5 336 const struct x509_track *x509_track;
67d8a0d4
AJ
337
338#ifdef ENABLE_CLIENT_CR
81d882d5 339 const struct static_challenge_info *sci;
67d8a0d4
AJ
340#endif
341
81d882d5
DS
342 /* --gremlin bits */
343 int gremlin;
685e486e 344
81d882d5
DS
345 /* Keying Material Exporter [RFC 5705] parameters */
346 const char *ekm_label;
347 size_t ekm_label_size;
348 size_t ekm_size;
67d8a0d4
AJ
349};
350
49620510
AJ
351/** @addtogroup control_processor
352 * @{ */
353/** @name Index of key_state objects within a tls_session structure
354 *
355 * This is the index of \c tls_session.key
356 *
357 * @{ */
358#define KS_PRIMARY 0 /**< Primary %key state index. */
359#define KS_LAME_DUCK 1 /**< %Key state index that will retire
360 * soon. */
361#define KS_SIZE 2 /**< Size of the \c tls_session.key array. */
362/** @} name Index of key_state objects within a tls_session structure */
363/** @} addtogroup control_processor */
364
270dc911
DS
365#define AUTH_TOKEN_SIZE 32 /**< Size of server side generated auth tokens.
366 * 32 bytes == 256 bits
367 */
49620510
AJ
368
369/**
370 * Security parameter state of a single session within a VPN tunnel.
371 * @ingroup control_processor
372 *
373 * This structure represents an OpenVPN peer-to-peer control channel
374 * session.
375 *
376 * A \c tls_session remains over soft resets, but a new instance is
377 * initialized for each hard reset.
378 *
379 * @see
380 * - This structure should be initialized using the \c tls_session_init()
381 * function.
382 * - This structure should be cleaned up using the \c tls_session_free()
383 * function.
384 */
385struct tls_session
386{
81d882d5
DS
387 /* const options and config info */
388 struct tls_options *opt;
49620510 389
81d882d5
DS
390 /* during hard reset used to control burst retransmit */
391 bool burst;
49620510 392
81d882d5
DS
393 /* authenticate control packets */
394 struct tls_wrap_ctx tls_wrap;
49620510 395
81d882d5
DS
396 int initial_opcode; /* our initial P_ opcode */
397 struct session_id session_id; /* our random session ID */
97894360 398
81d882d5
DS
399 /**
400 * The current active key id, used to keep track of renegotiations.
401 * key_id increments with each soft reset to KEY_ID_MASK then recycles back
402 * to 1. This way you know that if key_id is 0, it is the first key.
403 */
404 int key_id;
49620510 405
81d882d5 406 int limit_next; /* used for traffic shaping on the control channel */
49620510 407
81d882d5 408 int verify_maxlevel;
49620510 409
81d882d5 410 char *common_name;
49620510 411
81d882d5 412 struct cert_hash_set *cert_hash_set;
49620510
AJ
413
414#ifdef ENABLE_PF
81d882d5 415 uint32_t common_name_hashval;
49620510
AJ
416#endif
417
81d882d5 418 bool verified; /* true if peer certificate was verified against CA */
49620510 419
81d882d5
DS
420 /* not-yet-authenticated incoming client */
421 struct link_socket_actual untrusted_addr;
49620510 422
81d882d5 423 struct key_state key[KS_SIZE];
49620510
AJ
424};
425
426/** @addtogroup control_processor
427 * @{ */
428/** @name Index of tls_session objects within a tls_multi structure
429 *
430 * This is the index of \c tls_multi.session
431 *
432 * Normally three tls_session objects are maintained by an active openvpn
433 * session. The first is the current, TLS authenticated session, the
434 * second is used to process connection requests from a new client that
435 * would usurp the current session if successfully authenticated, and the
436 * third is used as a repository for a "lame-duck" %key in the event that
437 * the primary session resets due to error while the lame-duck %key still
438 * has time left before its expiration. Lame duck keys are used to
439 * maintain the continuity of the data channel connection while a new %key
440 * is being negotiated.
441 *
442 * @{ */
443#define TM_ACTIVE 0 /**< Active \c tls_session. */
444#define TM_UNTRUSTED 1 /**< As yet un-trusted \c tls_session
445 * being negotiated. */
446#define TM_LAME_DUCK 2 /**< Old \c tls_session. */
447#define TM_SIZE 3 /**< Size of the \c tls_multi.session
448 * array. */
449/** @} name Index of tls_session objects within a tls_multi structure */
450/** @} addtogroup control_processor */
451
452
453/*
454 * The number of keys we will scan on encrypt or decrypt. The first
455 * is the "active" key. The second is the lame_duck or retiring key
456 * associated with the active key's session ID. The third is a detached
457 * lame duck session that only occurs in situations where a key renegotiate
458 * failed on the active key, but a lame duck key was still valid. By
459 * preserving the lame duck session, we can be assured of having a data
460 * channel key available even when network conditions are so bad that
461 * we can't negotiate a new key within the time allotted.
462 */
463#define KEY_SCAN_SIZE 3
464
465
466/**
467 * Security parameter state for a single VPN tunnel.
468 * @ingroup control_processor
469 *
470 * An active VPN tunnel running with TLS enabled has one \c tls_multi
471 * object, in which it stores all control channel and data channel
472 * security parameter state. This structure can contain multiple,
473 * possibly simultaneously active, \c tls_context objects to allow for
474 * interruption-less transitions during session renegotiations. Each \c
475 * tls_context represents one control channel session, which can span
476 * multiple data channel security parameter sessions stored in \c
477 * key_state structures.
478 */
479struct tls_multi
480{
81d882d5
DS
481 /* used to coordinate access between main thread and TLS thread */
482 /*MUTEX_PTR_DEFINE (mutex);*/
483
484 /* const options and config info */
485 struct tls_options opt;
486
487 struct key_state *key_scan[KEY_SCAN_SIZE];
488 /**< List of \c key_state objects in the
489 * order they should be scanned by data
490 * channel modules. */
491
492 /*
493 * used by tls_pre_encrypt to communicate the encrypt key
494 * to tls_post_encrypt()
495 */
496 struct key_state *save_ks; /* temporary pointer used between pre/post routines */
497
498 /*
499 * Used to return outgoing address from
500 * tls_multi_process.
501 */
502 struct link_socket_actual to_link_addr;
503
504 int n_sessions; /**< Number of sessions negotiated thus
49620510
AJ
505 * far. */
506
81d882d5
DS
507 /*
508 * Number of errors.
509 */
510 int n_hard_errors; /* errors due to TLS negotiation failure */
511 int n_soft_errors; /* errors due to unrecognized or failed-to-authenticate incoming packets */
49620510 512
81d882d5
DS
513 /*
514 * Our locked common name, username, and cert hashes (cannot change during the life of this tls_multi object)
515 */
516 char *locked_cn;
517 char *locked_username;
518 struct cert_hash_set *locked_cert_hash_set;
49620510
AJ
519
520#ifdef ENABLE_DEF_AUTH
81d882d5
DS
521 /*
522 * An error message to send to client on AUTH_FAILED
523 */
524 char *client_reason;
49620510 525
81d882d5
DS
526 /* Time of last call to tls_authentication_status */
527 time_t tas_last;
a8be7379
GD
528#endif
529
1eb9a127 530#if P2MP_SERVER
81d882d5
DS
531 /*
532 * A multi-line string of general-purpose info received from peer
533 * over control channel.
534 */
535 char *peer_info;
49620510
AJ
536#endif
537
81d882d5
DS
538 /* For P_DATA_V2 */
539 uint32_t peer_id;
540 bool use_peer_id;
65eedc35 541
81d882d5 542 char *remote_ciphername; /**< cipher specified in peer's config file */
6e5ad2fa 543
81d882d5 544 char *auth_token; /**< If server sends a generated auth-token,
270dc911
DS
545 * this is the token to use for future
546 * user/pass authentications in this session.
547 */
81d882d5
DS
548 time_t auth_token_tstamp; /**< timestamp of the generated token */
549 bool auth_token_sent; /**< If server uses --auth-gen-token and
550 * token has been sent to client */
551 /*
552 * Our session objects.
553 */
554 struct tls_session session[TM_SIZE];
555 /**< Array of \c tls_session objects
556 * representing control channel
557 * sessions with the remote peer. */
49620510
AJ
558};
559
560
5f66f907 561#define SHOW_TLS_CIPHER_LIST_WARNING \
81d882d5
DS
562 "Be aware that that whether a cipher suite in this list can actually work\n" \
563 "depends on the specific setup of both peers. See the man page entries of\n" \
564 "--tls-cipher and --show-tls for more details.\n\n"
5f66f907 565
9a160b79 566#endif /* SSL_COMMON_H_ */