231#define KEY_DIRECTION_BIDIRECTIONAL 0
232#define KEY_DIRECTION_NORMAL 1
233#define KEY_DIRECTION_INVERSE 2
347#define CO_PACKET_ID_LONG_FORM (1u << 0)
350#define CO_IGNORE_PACKET_ID (1u << 1)
356#define CO_MUTE_REPLAY_WARNINGS (1u << 2)
359#define CO_USE_TLS_KEY_MATERIAL_EXPORT (1u << 3)
363#define CO_RESEND_WKC (1u << 4)
367#define CO_FORCE_TLSCRYPTV2_COOKIE (1u << 5)
371#define CO_USE_CC_EXIT_NOTIFY (1u << 6)
375#define CO_USE_DYNAMIC_TLS_CRYPT (1u << 7)
379#define CO_EPOCH_DATA_KEY_FORMAT (1u << 8)
390#define CRYPT_ERROR_EXIT(flags, format) \
393 msg(flags, "%s: " format, error_prefix); \
397#define CRYPT_ERROR(format) CRYPT_ERROR_EXIT(D_CRYPT_ERRORS, format)
398#define CRYPT_DROP(format) CRYPT_ERROR_EXIT(D_MULTI_DROPPED, format)
404#define OPENVPN_AEAD_MIN_IV_LEN (sizeof(packet_id_type) + 8)
406#define RKF_MUST_SUCCEED (1 << 0)
407#define RKF_INLINE (1 << 1)
437 int enc,
const char *prefix);
440 const struct key_type *kt,
const char *name);
443 const struct key_type *kt,
const char *name);
448 const struct key_type *kt,
const char *name);
520 const struct frame *
frame,
const uint8_t *ad_start);
536 uint16_t epoch,
const char *error_prefix,
struct gc_arena *
gc);
634 const char *prefix1);
637 const char *key_file,
bool key_inline,
const int key_direction,
638 const char *key_name,
const char *opt_name,
struct key2 *keydata);
645 const char *key_name);
748#define AEAD_LIMIT_BLOCKSIZE 16
Buffer management functions and garbage collection.
void free_key_ctx_bi(struct key_ctx_bi *ctx)
const char * print_key_filename(const char *str, bool is_inline)
To be used when printing a string that may contain inline data.
void prng_bytes(uint8_t *output, int len)
size_t 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.
void init_key_ctx(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, int enc, const char *prefix)
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.
static bool cipher_decrypt_verify_fail_exceeded(const struct key_ctx *ctx)
Check if the number of failed decryption is over the acceptable limit.
void verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared_secret_file)
void read_key_file(struct key2 *key2, const char *file, const unsigned int flags)
void test_crypto(struct crypto_options *co, struct frame *f)
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.
void init_key_bi_ctx_recv(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, const char *name)
bool generate_ephemeral_key(struct buffer *key, const char *pem_name)
Generate ephermal key material into the key structure.
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.
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.
int ascii2keydirection(msglvl_t msglevel, const char *str)
void must_have_n_keys(const char *filename, const char *option, const struct key2 *key2, int n)
const char * keydirection2ascii(int kd, bool remote, bool humanreadable)
int write_key_file(const int nkeys, const char *filename)
Write nkeys 1024-bits keys to file.
bool check_key(struct key *key, const struct key_type *kt)
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.
void print_cipher(const char *cipher)
Print a cipher list entry.
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...
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)
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 ...
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...
void key_direction_state_init(struct key_direction_state *kds, int key_direction)
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).
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
void init_key_bi_ctx_send(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, const char *name)
void generate_test_crypto_random_key(const struct key_type *key_type, struct key_ctx_bi *ctx, const char *key_name)
Generate a random key and initialise ctx to be used the in the crypto random test.
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)
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.
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.
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.
void free_key_ctx(struct key_ctx *ctx)
static bool key_ctx_bi_defined(const struct key_ctx_bi *key)
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.
struct hmac_ctx hmac_ctx_t
#define SHA256_DIGEST_LENGTH
struct cipher_ctx cipher_ctx_t
#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...
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.
Wrapper structure for dynamically allocated memory.
Security parameter state for processing data channel packets.
struct epoch_key epoch_key_send
last epoch_key used for generation of the current send data keys.
struct key_type epoch_key_type
the key_type that is used to generate the epoch keys
struct key_ctx epoch_retiring_data_receive_key
The old key before the sender switched to a new epoch data key.
uint64_t aead_usage_limit
The limit for AEAD cipher, this is the sum of packets + blocks that are allowed to be used.
unsigned int flags
Bit-flags determining behavior of security operation functions.
struct key_ctx * epoch_data_keys_future
Keeps the future epoch data keys for decryption.
struct packet_id_persist * pid_persist
Persistent packet ID state for keeping state between successive OpenVPN process startups.
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
uint16_t epoch_data_keys_future_count
number of keys stored in epoch_data_keys_future
struct packet_id_rec epoch_retiring_key_pid_recv
struct epoch_key epoch_key_recv
epoch_key used for the highest receive epoch keys
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
uint8_t epoch_key[SHA256_DIGEST_LENGTH]
Packet geometry parameters.
Garbage collection arena used to keep track of dynamically allocated memory.
Container for bidirectional cipher and HMAC key material.
int n
The number of key objects stored in the key2.keys array.
struct key keys[2]
Two unidirectional sets of key material.
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Container for one set of cipher and/or HMAC contexts.
size_t implicit_iv_len
The length of implicit_iv.
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.
uint16_t epoch
OpenVPN data channel epoch, this variable holds the epoch number this key belongs to.
uint64_t failed_verifications
number of failed verification using this cipher
cipher_ctx_t * cipher
Generic cipher context.
hmac_ctx_t * hmac
Generic HMAC context.
uint64_t plaintext_blocks
Counter for the number of plaintext block encrypted using this cipher with the current key in number ...
Key ordering of the key2.keys array.
int need_keys
The number of key objects necessary to support both sending and receiving.
int in_key
Index into the key2.keys array for the receiving direction.
int out_key
Index into the key2.keys array for the sending direction.
internal structure similar to struct key that holds key information but is not represented on wire an...
unsigned int hmac_size
Number of bytes set in the HMac key material.
uint16_t epoch
the epoch of the key.
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
unsigned int cipher_size
Number of bytes set in the cipher key material.
const char * cipher
const name of the cipher
const char * digest
Message digest static parameters.
Container for unidirectional cipher and HMAC key material.
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Data structure for describing the packet id that is received/send to the network.
Wrapper struct to pass around SHA256 digests.
uint8_t digest[SHA256_DIGEST_LENGTH]