OpenVPN
crypto.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-2024 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2021 Fox Crypto B.V. <openvpn@foxcrypto.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 
122 #ifndef CRYPTO_H
123 #define CRYPTO_H
124 
125 #include "crypto_backend.h"
126 #include "basic.h"
127 #include "buffer.h"
128 #include "packet_id.h"
129 #include "mtu.h"
130 
134 };
135 
136 /*
137  * Defines a key type and key length for both cipher and HMAC.
138  */
139 struct key_type
140 {
141  const char *cipher;
142  const char *digest;
143 };
144 
149 struct key
150 {
155 };
156 
157 
162 struct key_ctx
163 {
169 };
170 
171 #define KEY_DIRECTION_BIDIRECTIONAL 0 /* same keys for both directions */
172 #define KEY_DIRECTION_NORMAL 1 /* encrypt with keys[0], decrypt with keys[1] */
173 #define KEY_DIRECTION_INVERSE 2 /* encrypt with keys[1], decrypt with keys[0] */
174 
179 struct key2
180 {
181  int n;
183  struct key keys[2];
185 };
186 
197 {
198  int out_key;
200  int in_key;
202  int need_keys;
210 };
211 
218 {
219  struct key_ctx encrypt;
221  struct key_ctx decrypt;
224 };
225 
231 {
250 #define CO_PACKET_ID_LONG_FORM (1<<0)
251 
253 #define CO_IGNORE_PACKET_ID (1<<1)
254 
259 #define CO_MUTE_REPLAY_WARNINGS (1<<2)
260 
262 #define CO_USE_TLS_KEY_MATERIAL_EXPORT (1<<3)
263 
266 #define CO_RESEND_WKC (1<<4)
267 
270 #define CO_FORCE_TLSCRYPTV2_COOKIE (1<<5)
271 
274 #define CO_USE_CC_EXIT_NOTIFY (1<<6)
275 
278 #define CO_USE_DYNAMIC_TLS_CRYPT (1<<7)
279 
283  unsigned int flags;
285 };
286 
287 #define CRYPT_ERROR(format) \
288  do { msg(D_CRYPT_ERRORS, "%s: " format, error_prefix); goto error_exit; } while (false)
289 
294 #define OPENVPN_AEAD_MIN_IV_LEN (sizeof(packet_id_type) + 8)
295 
296 #define RKF_MUST_SUCCEED (1<<0)
297 #define RKF_INLINE (1<<1)
298 void read_key_file(struct key2 *key2, const char *file, const unsigned int flags);
299 
305 int write_key_file(const int nkeys, const char *filename);
306 
307 bool check_key(struct key *key, const struct key_type *kt);
308 
309 bool write_key(const struct key *key, const struct key_type *kt,
310  struct buffer *buf);
311 
312 int read_key(struct key *key, const struct key_type *kt, struct buffer *buf);
313 
324 void init_key_type(struct key_type *kt, const char *ciphername,
325  const char *authname, bool tls_mode, bool warn);
326 
327 /*
328  * Key context functions
329  */
330 
331 void init_key_ctx(struct key_ctx *ctx, const struct key *key,
332  const struct key_type *kt, int enc,
333  const char *prefix);
334 
335 void free_key_ctx(struct key_ctx *ctx);
336 
337 void init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2,
338  int key_direction, const struct key_type *kt,
339  const char *name);
340 
341 void free_key_ctx_bi(struct key_ctx_bi *ctx);
342 
343 
344 /**************************************************************************/
374 void openvpn_encrypt(struct buffer *buf, struct buffer work,
375  struct crypto_options *opt);
376 
377 
411 bool openvpn_decrypt(struct buffer *buf, struct buffer work,
412  struct crypto_options *opt, const struct frame *frame,
413  const uint8_t *ad_start);
414 
427 bool crypto_check_replay(struct crypto_options *opt,
428  const struct packet_id_net *pin, const char *error_prefix,
429  struct gc_arena *gc);
430 
431 
445 unsigned int
446 calculate_crypto_overhead(const struct key_type *kt,
447  unsigned int pkt_id_size,
448  bool occ);
449 
451 unsigned int crypto_max_overhead(void);
452 
460 void
461 write_pem_key_file(const char *filename, const char *key_name);
462 
470 bool
471 generate_ephemeral_key(struct buffer *key, const char *pem_name);
472 
483 bool
484 read_pem_key_file(struct buffer *key, const char *pem_name,
485  const char *key_file, bool key_inline);
486 
487 /*
488  * Message digest-based pseudo random number generator.
489  *
490  * If the PRNG was initialised with a certain message digest, uses the digest
491  * to calculate the next random number, and prevent depletion of the entropy
492  * pool.
493  *
494  * This PRNG is aimed at IV generation and similar miscellaneous tasks. Use
495  * \c rand_bytes() for higher-assurance functionality.
496  *
497  * Retrieves len bytes of pseudo random data, and places it in output.
498  *
499  * @param output Output buffer
500  * @param len Length of the output buffer
501  */
502 void prng_bytes(uint8_t *output, int len);
503 
504 /* an analogue to the random() function, but use prng_bytes */
505 long int get_random(void);
506 
508 void print_cipher(const char *cipher);
509 
510 void test_crypto(struct crypto_options *co, struct frame *f);
511 
512 
513 /* key direction functions */
514 
515 void key_direction_state_init(struct key_direction_state *kds, int key_direction);
516 
517 void verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared_secret_file);
518 
519 void must_have_n_keys(const char *filename, const char *option, const struct key2 *key2, int n);
520 
521 int ascii2keydirection(int msglevel, const char *str);
522 
523 const char *keydirection2ascii(int kd, bool remote, bool humanreadable);
524 
525 /* print keys */
526 void key2_print(const struct key2 *k,
527  const struct key_type *kt,
528  const char *prefix0,
529  const char *prefix1);
530 
531 void crypto_read_openvpn_key(const struct key_type *key_type,
532  struct key_ctx_bi *ctx, const char *key_file,
533  bool key_inline, const int key_direction,
534  const char *key_name, const char *opt_name,
535  struct key2 *keydata);
536 
537 /*
538  * Inline functions
539  */
540 
545 int memcmp_constant_time(const void *a, const void *b, size_t size);
546 
547 static inline bool
549 {
550  return key->encrypt.cipher || key->encrypt.hmac || key->decrypt.cipher || key->decrypt.hmac;
551 }
552 
563 const char *print_key_filename(const char *str, bool is_inline);
564 
575 static inline struct key_type
576 create_kt(const char *cipher, const char *md, const char *optname)
577 {
578  struct key_type kt;
579  kt.cipher = cipher;
580  kt.digest = md;
581 
582  if (cipher_defined(kt.cipher) && !cipher_valid(kt.cipher))
583  {
584  msg(M_WARN, "ERROR: --%s requires %s support.", optname, kt.cipher);
585  return (struct key_type) { 0 };
586  }
587  if (md_defined(kt.digest) && !md_valid(kt.digest))
588  {
589  msg(M_WARN, "ERROR: --%s requires %s support.", optname, kt.digest);
590  return (struct key_type) { 0 };
591  }
592 
593  return kt;
594 }
595 
602 bool check_tls_prf_working(void);
603 
604 #endif /* CRYPTO_H */
key_direction_state::out_key
int out_key
Index into the key2.keys array for the sending direction.
Definition: crypto.h:198
cipher_valid
static bool cipher_valid(const char *ciphername)
Returns if the cipher is valid, based on the given cipher name.
Definition: crypto_backend.h:204
key2::n
int n
The number of key objects stored in the key2.keys array.
Definition: crypto.h:181
generate_ephemeral_key
bool generate_ephemeral_key(struct buffer *key, const char *pem_name)
Generate ephermal key material into the key structure.
Definition: crypto.c:1738
packet_id_persist::filename
const char * filename
Definition: packet_id.h:147
hmac_ctx_t
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
Definition: crypto_mbedtls.h:46
key2_print
void key2_print(const struct key2 *k, const struct key_type *kt, const char *prefix0, const char *prefix1)
Prints the keys in a key2 structure.
Definition: crypto.c:988
key_ctx_bi_defined
static bool key_ctx_bi_defined(const struct key_ctx_bi *key)
Definition: crypto.h:548
cipher_defined
static bool cipher_defined(const char *ciphername)
Checks if the cipher is defined and is not the null (none) cipher.
Definition: crypto_backend.h:218
key_ctx::cipher
cipher_ctx_t * cipher
Generic cipher context.
Definition: crypto.h:164
key_ctx_bi::encrypt
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Definition: crypto.h:219
key_ctx_bi::initialized
bool initialized
Definition: crypto.h:223
init_key_ctx_bi
void init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2, int key_direction, const struct key_type *kt, const char *name)
Definition: crypto.c:869
key::hmac
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Definition: crypto.h:153
frame
Packet geometry parameters.
Definition: mtu.h:98
MAX_CIPHER_KEY_LENGTH
#define MAX_CIPHER_KEY_LENGTH
Definition: crypto_backend.h:177
write_pem_key_file
void write_pem_key_file(const char *filename, const char *key_name)
Generate a server key with enough randomness to fill a key struct and write to file.
Definition: crypto.c:1700
get_random
long int get_random(void)
Definition: crypto.c:1611
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:149
free_key_ctx
void free_key_ctx(struct key_ctx *ctx)
Definition: crypto.c:889
keydirection2ascii
const char * keydirection2ascii(int kd, bool remote, bool humanreadable)
Definition: crypto.c:1449
packet_id
Definition: packet_id.h:200
mtu.h
print_key_filename
const char * print_key_filename(const char *str, bool is_inline)
To be used when printing a string that may contain inline data.
Definition: crypto.c:1083
key_ctx_bi
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition: crypto.h:217
MAX_HMAC_KEY_LENGTH
#define MAX_HMAC_KEY_LENGTH
Definition: crypto_backend.h:495
packet_id.h
key_direction_state
Key ordering of the key2.keys array.
Definition: crypto.h:196
init_key_ctx
void init_key_ctx(struct key_ctx *ctx, const struct key *key, const struct key_type *kt, int enc, const char *prefix)
Definition: crypto.c:824
key_type::digest
const char * digest
Message digest static parameters.
Definition: crypto.h:142
cipher_ctx_t
mbedtls_cipher_context_t cipher_ctx_t
Generic cipher context.
Definition: crypto_mbedtls.h:40
M_WARN
#define M_WARN
Definition: error.h:91
ascii2keydirection
int ascii2keydirection(int msglevel, const char *str)
Definition: crypto.c:1426
openvpn_encrypt
void openvpn_encrypt(struct buffer *buf, struct buffer work, struct crypto_options *opt)
Encrypt and HMAC sign a packet so that it can be sent as a data channel VPN tunnel packet to a remote...
Definition: crypto.c:295
key_ctx
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:162
crypto_max_overhead
unsigned int crypto_max_overhead(void)
Return the worst-case OpenVPN crypto overhead (in bytes)
Definition: crypto.c:719
key_direction_state::in_key
int in_key
Index into the key2.keys array for the receiving direction.
Definition: crypto.h:200
crypto_read_openvpn_key
void crypto_read_openvpn_key(const struct key_type *key_type, struct key_ctx_bi *ctx, const char *key_file, bool key_inline, const int key_direction, const char *key_name, const char *opt_name, struct key2 *keydata)
Definition: crypto.c:1094
openvpn_decrypt
bool openvpn_decrypt(struct buffer *buf, struct buffer work, struct crypto_options *opt, const struct frame *frame, const uint8_t *ad_start)
HMAC verify and decrypt a data channel packet received from a remote OpenVPN peer.
Definition: crypto.c:645
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
OPENVPN_MAX_IV_LENGTH
#define OPENVPN_MAX_IV_LENGTH
Maximum length of an IV.
Definition: crypto_mbedtls.h:52
key_ctx::implicit_iv_len
size_t implicit_iv_len
The length of implicit_iv.
Definition: crypto.h:168
test_crypto
void test_crypto(struct crypto_options *co, struct frame *f)
Definition: crypto.c:999
key_type
Definition: crypto.h:139
must_have_n_keys
void must_have_n_keys(const char *filename, const char *option, const struct key2 *key2, int n)
Definition: crypto.c:1413
key_direction_state::need_keys
int need_keys
The number of key objects necessary to support both sending and receiving.
Definition: crypto.h:202
read_key
int read_key(struct key *key, const struct key_type *kt, struct buffer *buf)
Definition: crypto.c:1561
prng_bytes
void prng_bytes(uint8_t *output, int len)
Definition: crypto.c:1604
buffer.h
key_ctx::implicit_iv
uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH]
The implicit part of the IV.
Definition: crypto.h:166
create_kt
static struct key_type create_kt(const char *cipher, const char *md, const char *optname)
Creates and validates an instance of struct key_type with the provided algs.
Definition: crypto.h:576
print_cipher
void print_cipher(const char *cipher)
Print a cipher list entry.
Definition: crypto.c:1623
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
key_direction_state_init
void key_direction_state_init(struct key_direction_state *kds, int key_direction)
Definition: crypto.c:1478
sha256_digest::digest
uint8_t digest[SHA256_DIGEST_LENGTH]
Definition: crypto.h:133
key_type::cipher
const char * cipher
const name of the cipher
Definition: crypto.h:141
verify_fix_key2
void verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared_secret_file)
Definition: crypto.c:1507
memcmp_constant_time
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
Definition: crypto_openssl.c:1345
basic.h
packet_id_persist
Definition: openvpn.h:81
SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH
Definition: crypto_mbedtls.h:77
free_key_ctx_bi
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition: crypto.c:906
crypto_check_replay
bool crypto_check_replay(struct crypto_options *opt, const struct packet_id_net *pin, const char *error_prefix, struct gc_arena *gc)
Check packet ID for replay, and perform replay administration.
Definition: crypto.c:312
check_key
bool check_key(struct key *key, const struct key_type *kt)
Definition: crypto.c:931
md_defined
static bool md_defined(const char *mdname)
Checks if the cipher is defined and is not the null (none) cipher.
Definition: crypto_backend.h:504
calculate_crypto_overhead
unsigned int calculate_crypto_overhead(const struct key_type *kt, unsigned int pkt_id_size, bool occ)
Calculate the maximum overhead that our encryption has on a packet.
Definition: crypto.c:670
key::cipher
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
Definition: crypto.h:151
init_key_type
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, bool tls_mode, bool warn)
Initialize a key_type structure with.
Definition: crypto.c:744
write_key
bool write_key(const struct key *key, const struct key_type *kt, struct buffer *buf)
Definition: crypto.c:1524
packet_id_net
Definition: packet_id.h:194
key2
Container for bidirectional cipher and HMAC key material.
Definition: crypto.h:179
md_valid
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
Definition: crypto_openssl.c:1038
sha256_digest
Wrapper struct to pass around SHA256 digests.
Definition: crypto.h:132
write_key_file
int write_key_file(const int nkeys, const char *filename)
Write nkeys 1024-bits keys to file.
Definition: crypto.c:1350
read_pem_key_file
bool read_pem_key_file(struct buffer *key, const char *pem_name, const char *key_file, bool key_inline)
Read key material from a PEM encoded files into the key structure.
Definition: crypto.c:1756
key_ctx::hmac
hmac_ctx_t * hmac
Generic HMAC context.
Definition: crypto.h:165
crypto_options::flags
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition: crypto.h:283
key2::keys
struct key keys[2]
Two unidirectional sets of key material.
Definition: crypto.h:183
crypto_options::pid_persist
struct packet_id_persist * pid_persist
Persistent packet ID state for keeping state between successive OpenVPN process startups.
Definition: crypto.h:245
msg
#define msg(flags,...)
Definition: error.h:144
key_ctx_bi::decrypt
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
Definition: crypto.h:221
check_tls_prf_working
bool check_tls_prf_working(void)
Checks if the current TLS library supports the TLS 1.0 PRF with MD5+SHA1 that OpenVPN uses when TLS K...
Definition: crypto.c:1795
http-client.f
string f
Definition: http-client.py:6
crypto_backend.h
read_key_file
void read_key_file(struct key2 *key2, const char *file, const unsigned int flags)
Definition: crypto.c:1146
crypto_options
Security parameter state for processing data channel packets.
Definition: crypto.h:230