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
123#ifndef CRYPTO_H
124#define CRYPTO_H
125
126#include "crypto_backend.h"
127#include "basic.h"
128#include "buffer.h"
129#include "packet_id.h"
130#include "mtu.h"
131
136
137/*
138 * Defines a key type and key length for both cipher and HMAC.
139 */
141{
142 const char *cipher;
143 const char *digest;
144};
145
158
165
168
171
174
178 uint16_t epoch;
179};
180
188void
189key_parameters_from_key(struct key_parameters *key_params, const struct key *key);
190
191struct epoch_key {
193 uint16_t epoch;
194};
195
229
230#define KEY_DIRECTION_BIDIRECTIONAL 0 /* same keys for both directions */
231#define KEY_DIRECTION_NORMAL 1 /* encrypt with keys[0], decrypt with keys[1] */
232#define KEY_DIRECTION_INVERSE 2 /* encrypt with keys[1], decrypt with keys[0] */
233
238struct key2
239{
240 int n;
242 struct key keys[2];
246};
247
272
279{
285};
286
292{
302
305
308
313
322
325
329
344#define CO_PACKET_ID_LONG_FORM (1<<0)
347#define CO_IGNORE_PACKET_ID (1<<1)
353#define CO_MUTE_REPLAY_WARNINGS (1<<2)
356#define CO_USE_TLS_KEY_MATERIAL_EXPORT (1<<3)
360#define CO_RESEND_WKC (1<<4)
364#define CO_FORCE_TLSCRYPTV2_COOKIE (1<<5)
368#define CO_USE_CC_EXIT_NOTIFY (1<<6)
372#define CO_USE_DYNAMIC_TLS_CRYPT (1<<7)
376#define CO_EPOCH_DATA_KEY_FORMAT (1<<8)
383 unsigned int flags;
385};
386
387#define CRYPT_ERROR_EXIT(flags, format) \
388 do { msg(flags, "%s: " format, error_prefix); goto error_exit; } while (false)
389
390#define CRYPT_ERROR(format) CRYPT_ERROR_EXIT(D_CRYPT_ERRORS, format)
391#define CRYPT_DROP(format) CRYPT_ERROR_EXIT(D_MULTI_DROPPED, format)
392
397#define OPENVPN_AEAD_MIN_IV_LEN (sizeof(packet_id_type) + 8)
398
399#define RKF_MUST_SUCCEED (1<<0)
400#define RKF_INLINE (1<<1)
401void read_key_file(struct key2 *key2, const char *file, const unsigned int flags);
402
408int write_key_file(const int nkeys, const char *filename);
409
410bool check_key(struct key *key, const struct key_type *kt);
411
422void init_key_type(struct key_type *kt, const char *ciphername,
423 const char *authname, bool tls_mode, bool warn);
424
425/*
426 * Key context functions
427 */
428
429void init_key_ctx(struct key_ctx *ctx, const struct key_parameters *key,
430 const struct key_type *kt, int enc,
431 const char *prefix);
432
433void
434init_key_bi_ctx_send(struct key_ctx *ctx, const struct key_parameters *key,
435 const struct key_type *kt, const char *name);
436
437void
438init_key_bi_ctx_recv(struct key_ctx *ctx, const struct key_parameters *key,
439 const struct key_type *kt, const char *name);
440
441void free_key_ctx(struct key_ctx *ctx);
442
443void init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2,
444 int key_direction, const struct key_type *kt,
445 const char *name);
446
447void free_key_ctx_bi(struct key_ctx_bi *ctx);
448
449
450/**************************************************************************/
480void openvpn_encrypt(struct buffer *buf, struct buffer work,
481 struct crypto_options *opt);
482
483
517bool openvpn_decrypt(struct buffer *buf, struct buffer work,
518 struct crypto_options *opt, const struct frame *frame,
519 const uint8_t *ad_start);
520
534bool crypto_check_replay(struct crypto_options *opt,
535 const struct packet_id_net *pin,
536 uint16_t epoch,
537 const char *error_prefix,
538 struct gc_arena *gc);
539
540
554unsigned int
555calculate_crypto_overhead(const struct key_type *kt,
556 unsigned int pkt_id_size,
557 bool occ);
558
560unsigned int crypto_max_overhead(void);
561
569void
570write_pem_key_file(const char *filename, const char *key_name);
571
579bool
580generate_ephemeral_key(struct buffer *key, const char *pem_name);
581
592bool
593read_pem_key_file(struct buffer *key, const char *pem_name,
594 const char *key_file, bool key_inline);
595
596/*
597 * Message digest-based pseudo random number generator.
598 *
599 * If the PRNG was initialised with a certain message digest, uses the digest
600 * to calculate the next random number, and prevent depletion of the entropy
601 * pool.
602 *
603 * This PRNG is aimed at IV generation and similar miscellaneous tasks. Use
604 * \c rand_bytes() for higher-assurance functionality.
605 *
606 * Retrieves len bytes of pseudo random data, and places it in output.
607 *
608 * @param output Output buffer
609 * @param len Length of the output buffer
610 */
611void prng_bytes(uint8_t *output, int len);
612
613/* an analogue to the random() function, but use prng_bytes */
614long int get_random(void);
615
617void print_cipher(const char *cipher);
618
619void test_crypto(struct crypto_options *co, struct frame *f);
620
621
622/* key direction functions */
623
624void key_direction_state_init(struct key_direction_state *kds, int key_direction);
625
626void verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared_secret_file);
627
628void must_have_n_keys(const char *filename, const char *option, const struct key2 *key2, int n);
629
630int ascii2keydirection(int msglevel, const char *str);
631
632const char *keydirection2ascii(int kd, bool remote, bool humanreadable);
633
634/* print keys */
635void key2_print(const struct key2 *k,
636 const struct key_type *kt,
637 const char *prefix0,
638 const char *prefix1);
639
640void crypto_read_openvpn_key(const struct key_type *key_type,
641 struct key_ctx_bi *ctx, const char *key_file,
642 bool key_inline, const int key_direction,
643 const char *key_name, const char *opt_name,
644 struct key2 *keydata);
645
646/*
647 * Inline functions
648 */
649
654int memcmp_constant_time(const void *a, const void *b, size_t size);
655
656static inline bool
658{
659 return key->encrypt.cipher || key->encrypt.hmac || key->decrypt.cipher || key->decrypt.hmac;
660}
661
672const char *print_key_filename(const char *str, bool is_inline);
673
684static inline struct key_type
685create_kt(const char *cipher, const char *md, const char *optname)
686{
687 struct key_type kt;
688 kt.cipher = cipher;
689 kt.digest = md;
690
692 {
693 msg(M_WARN, "ERROR: --%s requires %s support.", optname, kt.cipher);
694 return (struct key_type) { 0 };
695 }
696 if (md_defined(kt.digest) && !md_valid(kt.digest))
697 {
698 msg(M_WARN, "ERROR: --%s requires %s support.", optname, kt.digest);
699 return (struct key_type) { 0 };
700 }
701
702 return kt;
703}
704
714uint64_t
715cipher_get_aead_limits(const char *ciphername);
716
720static inline bool
722{
723 /* Use 2**36, same as DTLS 1.3. Strictly speaking this only guarantees
724 * the security margin for packets up to 2^10 blocks (16384 bytes)
725 * but we accept slightly lower security bound for the edge
726 * of Chacha20-Poly1305 and packets over 16k as MTUs over 16k are
727 * extremely rarely used */
728 return ctx->failed_verifications > (1ull << 36);
729}
730
735static inline bool
737{
738 /* Use 2**35, half the amount after which we refuse to decrypt */
739 return ctx->failed_verifications > (1ull << 35);
740}
741
742
748#define AEAD_LIMIT_BLOCKSIZE 16
749
756bool check_tls_prf_working(void);
757
764static inline bool
765aead_usage_limit_reached(const uint64_t limit, const struct key_ctx *key_ctx,
766 int64_t higest_pid)
767{
768 /* This is the q + s <= p^(1/2) * 2^(129/2) - 1 calculation where
769 * q is the number of protected messages (highest_pid)
770 * s Total plaintext length in all messages (in blocks) */
771 return (limit > 0 && key_ctx->plaintext_blocks + (uint64_t) higest_pid > limit);
772}
773
774#endif /* CRYPTO_H */
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition crypto.c:1125
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:1310
void prng_bytes(uint8_t *output, int len)
Definition crypto.c:1750
void init_key_ctx(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, int enc, const char *prefix)
Definition crypto.c:1015
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:819
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:1208
static bool cipher_decrypt_verify_fail_exceeded(const struct key_ctx *ctx)
Check if the number of failed decryption is over the acceptable limit.
Definition crypto.h:721
void verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared_secret_file)
Definition crypto.c:1734
void read_key_file(struct key2 *key2, const char *file, const unsigned int flags)
Definition crypto.c:1373
long int get_random(void)
Definition crypto.c:1757
void test_crypto(struct crypto_options *co, struct frame *f)
Definition crypto.c:1229
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:1846
void init_key_bi_ctx_recv(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, const char *name)
Definition crypto.c:1075
bool generate_ephemeral_key(struct buffer *key, const char *pem_name)
Generate ephermal key material into the key structure.
Definition crypto.c:1884
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:1902
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:893
int ascii2keydirection(int msglevel, const char *str)
Definition crypto.c:1653
void must_have_n_keys(const char *filename, const char *option, const struct key2 *key2, int n)
Definition crypto.c:1640
const char * keydirection2ascii(int kd, bool remote, bool humanreadable)
Definition crypto.c:1676
int write_key_file(const int nkeys, const char *filename)
Write nkeys 1024-bits keys to file.
Definition crypto.c:1577
bool check_key(struct key *key, const struct key_type *kt)
Definition crypto.c:1151
void key_parameters_from_key(struct key_parameters *key_params, const struct key *key)
Converts a struct key representation into a struct key_parameters representation.
Definition crypto.c:1219
void print_cipher(const char *cipher)
Print a cipher list entry.
Definition crypto.c:1769
uint64_t cipher_get_aead_limits(const char *ciphername)
Check if the cipher is an AEAD cipher and needs to be limited to a certain number of number of blocks...
Definition crypto.c:353
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:1087
static bool cipher_decrypt_verify_fail_warn(const struct key_ctx *ctx)
Check if the number of failed decryption is approaching the limit and we should try to move to a new ...
Definition crypto.h:736
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:1941
void key_direction_state_init(struct key_direction_state *kds, int key_direction)
Definition crypto.c:1705
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
unsigned int crypto_max_overhead(void)
Return the worst-case OpenVPN crypto overhead (in bytes)
Definition crypto.c:868
void init_key_bi_ctx_send(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, const char *name)
Definition crypto.c:1063
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:1321
static bool aead_usage_limit_reached(const uint64_t limit, const struct key_ctx *key_ctx, int64_t higest_pid)
Checks if the usage limit for an AEAD cipher is reached.
Definition crypto.h:765
bool crypto_check_replay(struct crypto_options *opt, const struct packet_id_net *pin, uint16_t epoch, const char *error_prefix, struct gc_arena *gc)
Check packet ID for replay, and perform replay administration.
Definition crypto.c:384
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:685
void free_key_ctx(struct key_ctx *ctx)
Definition crypto.c:1106
static bool key_ctx_bi_defined(const struct key_ctx_bi *key)
Definition crypto.h:657
Data Channel Cryptography SSL library-specific backend interface.
static bool cipher_defined(const char *ciphername)
Checks if the cipher is defined and is not the null (none) cipher.
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
#define MAX_CIPHER_KEY_LENGTH
static bool cipher_valid(const char *ciphername)
Returns if the cipher is valid, based on the given cipher name.
#define MAX_HMAC_KEY_LENGTH
static bool md_defined(const char *mdname)
Checks if the cipher is defined and is not the null (none) cipher.
mbedtls_cipher_context_t cipher_ctx_t
Generic cipher context.
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
#define SHA256_DIGEST_LENGTH
#define OPENVPN_MAX_IV_LENGTH
Maximum length of an IV.
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:336
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:794
#define msg(flags,...)
Definition error.h:144
#define M_WARN
Definition error.h:91
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
Security parameter state for processing data channel packets.
Definition crypto.h:292
struct epoch_key epoch_key_send
last epoch_key used for generation of the current send data keys.
Definition crypto.h:301
struct key_type epoch_key_type
the key_type that is used to generate the epoch keys
Definition crypto.h:307
struct key_ctx epoch_retiring_data_receive_key
The old key before the sender switched to a new epoch data key.
Definition crypto.h:327
uint64_t aead_usage_limit
The limit for AEAD cipher, this is the sum of packets + blocks that are allowed to be used.
Definition crypto.h:312
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition crypto.h:383
struct key_ctx * epoch_data_keys_future
Keeps the future epoch data keys for decryption.
Definition crypto.h:321
struct packet_id_persist * pid_persist
Persistent packet ID state for keeping state between successive OpenVPN process startups.
Definition crypto.h:339
uint16_t epoch_data_keys_future_count
number of keys stored in epoch_data_keys_future
Definition crypto.h:324
struct packet_id_rec epoch_retiring_key_pid_recv
Definition crypto.h:328
struct epoch_key epoch_key_recv
epoch_key used for the highest receive epoch keys
Definition crypto.h:304
uint16_t epoch
Definition crypto.h:193
Packet geometry parameters.
Definition mtu.h:98
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:239
int n
The number of key objects stored in the key2.keys array.
Definition crypto.h:240
struct key keys[2]
Two unidirectional sets of key material.
Definition crypto.h:242
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition crypto.h:279
bool initialized
Definition crypto.h:284
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
Definition crypto.h:282
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Definition crypto.h:280
Container for one set of cipher and/or HMAC contexts.
Definition crypto.h:201
size_t implicit_iv_len
The length of implicit_iv.
Definition crypto.h:217
uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH]
This implicit IV will be always XORed with the packet id that is sent on the wire to get the IV.
Definition crypto.h:215
uint16_t epoch
OpenVPN data channel epoch, this variable holds the epoch number this key belongs to.
Definition crypto.h:227
uint64_t failed_verifications
number of failed verification using this cipher
Definition crypto.h:223
cipher_ctx_t * cipher
Generic cipher context.
Definition crypto.h:202
hmac_ctx_t * hmac
Generic HMAC context.
Definition crypto.h:203
uint64_t plaintext_blocks
Counter for the number of plaintext block encrypted using this cipher with the current key in number ...
Definition crypto.h:221
Key ordering of the key2.keys array.
Definition crypto.h:258
int need_keys
The number of key objects necessary to support both sending and receiving.
Definition crypto.h:263
int in_key
Index into the key2.keys array for the receiving direction.
Definition crypto.h:261
int out_key
Index into the key2.keys array for the sending direction.
Definition crypto.h:259
internal structure similar to struct key that holds key information but is not represented on wire an...
Definition crypto.h:162
int hmac_size
Number of bytes set in the HMac key material.
Definition crypto.h:173
int cipher_size
Number of bytes set in the cipher key material.
Definition crypto.h:167
uint16_t epoch
the epoch of the key.
Definition crypto.h:178
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Definition crypto.h:170
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
Definition crypto.h:164
const char * cipher
const name of the cipher
Definition crypto.h:142
const char * digest
Message digest static parameters.
Definition crypto.h:143
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
Definition crypto.h:153
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Definition crypto.h:155
Data structure for describing the packet id that is received/send to the network.
Definition packet_id.h:192
const char * filename
Definition packet_id.h:134
Wrapper struct to pass around SHA256 digests.
Definition crypto.h:133
uint8_t digest[SHA256_DIGEST_LENGTH]
Definition crypto.h:134
struct gc_arena gc
Definition test_ssl.c:155