72 if (use_epoch_data_format)
78 uint8_t *mac_out = NULL;
98 if (use_epoch_data_format)
126 for (
unsigned int i = 0; i < iv_len; i++)
142 msg(
D_CRYPT_ERRORS,
"ENCRYPT: buffer size error, bc=%d bo=%d bl=%d wc=%d wo=%d wl=%d",
151 if (!use_epoch_data_format)
172 if (use_epoch_data_format)
202 if (buf->
len > 0 && opt)
205 uint8_t *mac_out = NULL;
206 const uint8_t *hmac_start = NULL;
219 hmac_start =
BEND(&work);
264 "ENCRYPT: buffer size error, bc=%d bo=%d bl=%d wc=%d wo=%d wl=%d cbs=%u",
293 hmac_start =
BPTR(buf);
331 if (buf->
len > 0 && opt)
370 uint64_t rs = (1ull << 36) - 1;
377 const char *error_prefix,
struct gc_arena *
gc)
395 "Received data packet with new epoch %d. Updating "
417 "%s: bad packet ID (may be a replay): %s -- "
418 "see the man page entry for --replay-window for "
419 "more info or silence this warning with --mute-replay-warnings",
436 const struct frame *
frame,
const uint8_t *ad_start)
438 static const char error_prefix[] =
"AEAD Decrypt error";
447 CRYPT_DROP(
"Decryption failed verification limit reached.");
470 if (use_epoch_data_format)
472 min_packet_len +=
sizeof(uint32_t);
475 if (buf->
len < min_packet_len)
477 CRYPT_ERROR(
"missing IV info, missing tag or no payload");
488 if (use_epoch_data_format)
491 const size_t packet_iv_len =
sizeof(uint64_t);
494 memcpy(iv,
BPTR(buf), packet_iv_len);
508 CRYPT_DROP(
"Decryption failed verification limit reached");
515 memcpy(iv,
BPTR(buf), packet_iv_len);
524 for (
unsigned int i = 0; i < iv_len; i++)
538 const int ad_size = (int)(
BPTR(buf) - ad_start);
540 uint8_t *tag_ptr = NULL;
543 if (use_epoch_data_format)
545 data_len =
BLEN(buf) - tag_size;
546 tag_ptr =
BPTR(buf) + data_len;
552 data_len =
BLEN(buf);
579 CRYPT_DROP(
"packet tag authentication failed");
619 static const char error_prefix[] =
"Authenticate/Decrypt packet error";
623 if (buf->
len > 0 && opt)
627 bool have_pin =
false;
643 if (buf->
len < hmac_len)
654 CRYPT_DROP(
"packet HMAC authentication failed");
672 if (buf->
len < (
int)iv_size)
676 memcpy(iv_buf,
BPTR(buf), iv_size);
707 CRYPT_DROP(
"packet authentication failed, dropping.");
780 const struct frame *
frame,
const uint8_t *ad_start)
784 if (buf->
len > 0 && opt)
805 size_t crypto_overhead = 0;
810 crypto_overhead += pkt_id_size;
846 return crypto_overhead;
862 "WARNING: INSECURE cipher (%s) with block size less than 128"
863 " bit (%u bit). This allows attacks like SWEET32. Mitigate by "
864 "using a --cipher with a larger block size (e.g. AES-256-CBC). "
865 "Support for these insecure ciphers will be removed in "
878 bool aead_cipher =
false;
885 if (strcmp(ciphername,
"none") != 0)
889 msg(
M_FATAL,
"Cipher %s not supported", ciphername);
895#ifdef ENABLE_OFB_CFB_MODE
900 msg(
M_FATAL,
"Cipher '%s' mode not supported", ciphername);
905 msg(
M_FATAL,
"Cipher '%s' not allowed: block size too big.", ciphername);
916 msg(
M_WARN,
"******* WARNING *******: '--cipher none' was specified. "
917 "This means NO encryption will be performed and tunnelled "
918 "data WILL be transmitted in clear text over the network! "
919 "PLEASE DO RECONSIDER THIS SETTING!");
923 if (strcmp(authname,
"none") != 0)
935 msg(
M_FATAL,
"HMAC '%s' not allowed: digest size too big.", authname);
939 else if (!aead_cipher)
943 msg(
M_WARN,
"******* WARNING *******: '--auth none' was specified. "
944 "This means no authentication will be performed on received "
945 "packets, meaning you CANNOT trust that the data received by "
946 "the remote side have NOT been manipulated. "
947 "PLEASE DO RECONSIDER THIS SETTING!");
969 size_t impl_iv_len = 0;
970 size_t impl_iv_offset = 0;
988 ASSERT(impl_iv_len <= key->hmac_size);
997 int enc,
const char *prefix)
1008 msg(
D_CIPHER_INIT,
"%s: Cipher '%s' initialized with %u bit key", prefix, ciphername,
1024 msg(
D_CIPHER_INIT,
"%s: Using %d bit message hash '%s' for HMAC authentication", prefix,
1039 const struct key_type *kt,
const char *name)
1041 char log_prefix[128] = { 0 };
1043 snprintf(log_prefix,
sizeof(log_prefix),
"Outgoing %s", name);
1051 const struct key_type *kt,
const char *name)
1053 char log_prefix[128] = { 0 };
1055 snprintf(log_prefix,
sizeof(log_prefix),
"Incoming %s", name);
1063 const struct key_type *kt,
const char *name)
1111 for (
size_t i = 0; i < cipher_length; ++i)
1155 msg(
M_FATAL,
"ERROR: Random number generator cannot obtain entropy for key generation");
1181 const char *prefix1)
1227 msg(
M_INFO,
"Entering " PACKAGE_NAME
" crypto self-test mode.");
1232 msg(
M_INFO,
"TESTING ENCRYPT/DECRYPT of packet length=%d", i);
1243 struct buffer buf = work;
1262 for (j = 0; j < i; ++j)
1264 const uint8_t in = *(
BPTR(&src) + j);
1265 const uint8_t out = *(
BPTR(&buf) + j);
1268 msg(
M_FATAL,
"SELF TEST FAILED, pos=%d in=%d out=%d", j, in, out);
1272 msg(
M_INFO, PACKAGE_NAME
" crypto self-test mode SUCCEEDED.");
1281 return "[[INLINE]]";
1289 const char *key_file,
bool key_inline,
const int key_direction,
1290 const char *key_name,
const char *opt_name,
struct key2 *keydata)
1305 "File '%s' does not have OpenVPN Static Key format. Using "
1306 "free-form passphrase file is not supported anymore.",
1328 const char *key_name)
1343 "Non-Hex character ('%c') found at line %d in key file '%s' (%d/%d/%d bytes found/min/max)";
1346 "Non-Hex, unprintable character (0x%02x) found at line %d in key file '%s' (%d/%d/%d bytes found/min/max)";
1356 uint8_t hex_byte[3] = { 0, 0, 0 };
1359 const unsigned char *cp;
1366 uint8_t *out = (uint8_t *)&
key2->
keys;
1367 const int keylen =
sizeof(
key2->
keys);
1371#define PARSE_INITIAL 0
1374#define PARSE_DATA_COMPLETE 3
1376#define PARSE_FINISHED 5
1382 const int onekeylen =
sizeof(
key2->
keys[0]);
1392 size_t buf_size = strlen(file) + 1;
1393 ASSERT(buf_size <= INT_MAX);
1394 size = (int)buf_size;
1402 msg(
M_FATAL,
"Read error on key file ('%s')", file);
1408 cp = (
unsigned char *)in.
data;
1411 const unsigned char c = *cp;
1414 msg(
M_INFO,
"char='%c'[%d] s=%d ln=%d li=%d m=%d c=%d",
1415 c, (
int)c, state, line_num, line_index, match, count);
1420 line_index = match = 0;
1446 if (++match == hlen)
1458 if (++match == flen)
1470 ASSERT(hb_index >= 0 && hb_index < 2);
1471 hex_byte[hb_index++] = c;
1475 ASSERT(sscanf((
const char *)hex_byte,
"%2x", &u) == 1);
1476 *out++ = (uint8_t)u;
1478 if (++count == keylen)
1484 else if (isspace(c))
1504 key2->
n = count / onekeylen;
1513 "Insufficient key material or header text not found in file '%s' (%d/%d/%d bytes found/min/max)",
1519 msg(
M_FATAL,
"Footer text not found in file '%s' (%d/%d/%d bytes found/min/max)",
1534 printf(
"KEY READ, n=%d\n",
key2->
n);
1544 printf(
"[%d]\n%s\n\n", i, fmt);
1558 int nbits = nkeys *
sizeof(
struct key) * 8;
1564 const int bytes_per_line = 16;
1567 buf_printf(&out,
"#\n# %d bit OpenVPN static key\n#\n", nbits);
1570 for (
int i = 0; i < nkeys; ++i)
1592 if (!filename || strcmp(filename,
"") == 0)
1594 printf(
"%.*s\n",
BLEN(&out),
BPTR(&out));
1618 "Key file '%s' used in --%s contains insufficient key material [keys found=%d required=%d]",
1619 filename, option,
key2->
n, n);
1622 "Key file '%s' used in --%s contains insufficient key material [keys found=%d required=%d] -- try generating a new key file with '" PACKAGE
1623 " --genkey secret [file]', or use the existing key file in bidirectional mode by specifying --%s without a key direction parameter",
1624 filename, option,
key2->
n, n, option);
1636 else if (!strcmp(str,
"0"))
1640 else if (!strcmp(str,
"1"))
1646 msg(msglevel,
"Unknown key direction '%s' -- must be '0' or '1'", str);
1668 return remote ?
"1" :
"0";
1672 return remote ?
"0" :
"1";
1685 switch (key_direction)
1715 for (i = 0; i <
key2->
n; ++i)
1720 msg(
M_FATAL,
"Key #%d in '%s' is bad. Try making a new key with --genkey.", i + 1,
1721 shared_secret_file);
1753 printf(
"stream cipher");
1762 printf(
", TLS client/server mode only");
1768 printf(
", %s", reason);
1782 || 0 == strcmp(cipher_name, pair->
lib_name))
1822 struct key server_key = { 0 };
1826 if (!
rand_bytes((
void *)&server_key,
sizeof(server_key)))
1831 buf_set_read(&server_key_buf, (
void *)&server_key,
sizeof(server_key));
1834 msg(
M_WARN,
"ERROR: could not PEM-encode key");
1838 if (!filename || strcmp(filename,
"") == 0)
1840 printf(
"%.*s",
BLEN(&server_key_pem),
BPTR(&server_key_pem));
1844 msg(
M_ERR,
"ERROR: could not write key file");
1860 msg(
M_INFO,
"Using random %s.", key_name);
1864 msg(
M_WARN,
"ERROR: could not generate random key");
1877 struct buffer key_pem = { 0 };
1885 msg(
M_WARN,
"ERROR: failed to read %s file (%s)", pem_name, key_file);
1891 buf_set_read(&key_pem, (
const void *)key_file, strlen(key_file) + 1);
1896 msg(
M_WARN,
"ERROR: %s pem decode failed", pem_name);
1917 const char *seed =
"tls1-prf-test";
1918 const char *secret =
"tls1-prf-test-secret";
1920 const uint8_t expected_out[] = {
'q',
'D', 0xfe,
'%',
'@',
's',
'u', 0x95 };
1922 int ret =
ssl_tls1_PRF((uint8_t *)seed, strlen(seed), (uint8_t *)secret,
1923 strlen(secret), out,
sizeof(out));
1925 return (ret && memcmp(out, expected_out,
sizeof(out)) == 0);
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc)
buffer_read_from_file - copy the content of a file into a buffer
#define BEND(buf)
Return a pointer one past the end of the buffer content.
static struct buffer clear_buf(void)
Return an empty, undefined struct buffer (all fields zero).
#define BPTR(buf)
Return a pointer to the start of the buffer content.
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
static bool buf_valid(const struct buffer *buf)
Return true iff buf is valid.
static void gc_init(struct gc_arena *a)
Initialise a garbage collection arena to an empty state.
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Initialise a buffer with an externally provided writable memory region.
static uint8_t * buf_prepend(struct buffer *buf, ssize_t size)
Make space at the front of a buffer for prepending data.
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Initialise a buffer with an externally provided read-only memory region.
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
Reserve space at the end of a buffer for writing.
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
#define BLEN(buf)
Return the length of the buffer content in bytes.
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Format a binary buffer as a hex string with spaces every 4 bytes.
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
#define buf_init(buf, offset)
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
const char * translate_cipher_name_from_openvpn(const char *cipher_name)
Translate an OpenVPN cipher name to a crypto library cipher name.
static void key_print(const struct key *key, const struct key_type *kt, const char *prefix)
static const char static_key_head[]
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.
static void warn_insecure_key_type(const char *ciphername)
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.
void verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared_secret_file)
void init_key_bi_ctx_send(struct key_ctx *ctx, const struct key_parameters *key_params, const struct key_type *kt, const char *name)
void read_key_file(struct key2 *key2, const char *file, const unsigned int flags)
static bool key_is_zero(struct key *key, const struct key_type *kt)
static bool openvpn_decrypt_aead(struct buffer *buf, struct buffer work, struct crypto_options *opt, const struct frame *frame, const uint8_t *ad_start)
Unwrap (authenticate, decrypt and check replay protection) AEAD-mode data channel packets.
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)
bool generate_ephemeral_key(struct buffer *key, const char *key_name)
Generate ephermal key material into the key structure.
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)
const char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
void write_pem_key_file(const char *filename, const char *pem_name)
Generate a server key with enough randomness to fill a key struct and write to file.
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.
#define PARSE_DATA_COMPLETE
static const char static_key_foot[]
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...
static const cipher_name_pair * get_cipher_name_pair(const char *cipher_name)
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)
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)
unsigned int crypto_max_overhead(void)
Return the worst-case OpenVPN crypto overhead (in bytes).
static void openvpn_encrypt_v1(struct buffer *buf, struct buffer work, struct crypto_options *opt)
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...
static void key_ctx_update_implicit_iv(struct key_ctx *ctx, const struct key_parameters *key)
Update the implicit IV for a key_ctx based on TLS session ids and cipher used.
static const char unprintable_char_fmt[]
void print_cipher(const char *ciphername)
Print a cipher list entry.
static void generate_key_random(struct key *key)
void test_crypto(struct crypto_options *co, struct frame *frame)
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)
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 void openvpn_encrypt_aead(struct buffer *buf, struct buffer work, struct crypto_options *opt)
static bool openvpn_decrypt_v1(struct buffer *buf, struct buffer work, struct crypto_options *opt, const struct frame *frame)
void free_key_ctx(struct key_ctx *ctx)
static const char printable_char_fmt[]
void init_key_bi_ctx_recv(struct key_ctx *ctx, const struct key_parameters *key_params, const struct key_type *kt, const char *name)
Data Channel Cryptography Module.
#define AEAD_LIMIT_BLOCKSIZE
Blocksize used for the AEAD limit caluclation.
#define KEY_DIRECTION_NORMAL
#define CO_PACKET_ID_LONG_FORM
Bit-flag indicating whether to use OpenVPN's long packet ID format.
static bool cipher_decrypt_verify_fail_exceeded(const struct key_ctx *ctx)
Check if the number of failed decryption is over the acceptable limit.
#define OPENVPN_AEAD_MIN_IV_LEN
Minimal IV length for AEAD mode ciphers (in bytes): 4-byte packet id + 8 bytes implicit IV.
#define CRYPT_DROP(format)
#define CRYPT_ERROR(format)
#define CO_IGNORE_PACKET_ID
Bit-flag indicating whether to ignore the packet ID of a received packet.
#define CO_EPOCH_DATA_KEY_FORMAT
Bit-flag indicating the epoch the data format.
#define CO_MUTE_REPLAY_WARNINGS
Bit-flag indicating not to display replay warnings.
#define KEY_DIRECTION_BIDIRECTIONAL
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
#define KEY_DIRECTION_INVERSE
bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len, uint8_t *output, size_t output_len)
Calculates the TLS 1.0-1.1 PRF function.
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
hmac_ctx_t * hmac_ctx_new(void)
void hmac_ctx_reset(hmac_ctx_t *ctx)
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
static bool cipher_defined(const char *ciphername)
Checks if the cipher is defined and is not the null (none) cipher.
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
unsigned int cipher_kt_iv_size(const char *ciphername)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
unsigned int cipher_ctx_iv_length(const cipher_ctx_t *ctx)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported CBC mode cipher.
cipher_ctx_t * cipher_ctx_new(void)
Generic cipher functions.
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
int hmac_ctx_size(hmac_ctx_t *ctx)
bool cipher_kt_insecure(const char *ciphername)
Returns true if we consider this cipher to be insecure.
#define MAX_CIPHER_KEY_LENGTH
void crypto_clear_error(void)
bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
Decode a PEM buffer to binary data.
void cipher_ctx_free(cipher_ctx_t *ctx)
Cleanup and free a cipher context.
int cipher_ctx_mode(const cipher_ctx_t *ctx)
Returns the mode that the cipher runs in.
bool cipher_ctx_mode_ofb_cfb(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported AEAD mode cipher.
static bool cipher_valid(const char *ciphername)
Returns if the cipher is valid, based on the given cipher name.
void hmac_ctx_free(hmac_ctx_t *ctx)
unsigned int cipher_kt_tag_size(const char *ciphername)
Returns the MAC tag size of the cipher, in bytes.
int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len)
Updates the given cipher context, providing additional data (AD) for authenticated encryption with ad...
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
const size_t cipher_name_translation_table_count
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
#define MAX_HMAC_KEY_LENGTH
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
#define OPENVPN_AEAD_TAG_LENGTH
void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername, crypto_operation_t enc)
Initialise a cipher context, based on the given key and key type.
int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len)
Gets the computed message authenticated code (MAC) tag for this cipher.
int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, const uint8_t *src, int src_len)
Updates the given cipher context, encrypting data in the source buffer, and placing any complete bloc...
#define OPENVPN_MAX_CIPHER_BLOCK_SIZE
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf)
Resets the given cipher context, setting the IV to the specified value.
const cipher_name_pair cipher_name_translation_table[]
Cipher name translation table.
unsigned int cipher_ctx_block_size(const cipher_ctx_t *ctx)
Returns the block size of the cipher, in bytes.
int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len)
Pads the final cipher block using PKCS padding, and output to the destination buffer.
unsigned int cipher_kt_block_size(const char *ciphername)
Returns the block size of the cipher, in bytes.
#define OPENVPN_MAX_HMAC_SIZE
unsigned char md_kt_size(const char *mdname)
Returns the size of the message digest, in bytes.
static bool md_defined(const char *mdname)
Checks if the cipher is defined and is not the null (none) cipher.
bool cipher_valid_reason(const char *ciphername, const char **reason)
Returns if the cipher is valid, based on the given cipher name and provides a reason if invalid.
int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len)
Like cipher_ctx_final, but check the computed authentication tag against the supplied (expected) tag.
bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src, struct gc_arena *gc)
Encode binary data as PEM.
void epoch_replace_update_recv_key(struct crypto_options *co, uint16_t new_epoch)
This is called when the peer uses a new send key that is not the default key.
void epoch_check_send_iterate(struct crypto_options *opt)
Checks if we need to iterate the send epoch key.
struct key_ctx * epoch_lookup_decrypt_key(struct crypto_options *opt, uint16_t epoch)
Using an epoch, this function will try to retrieve a decryption key context that matches that epoch f...
#define OPENVPN_OP_DECRYPT
Cipher should decrypt.
struct cipher_ctx cipher_ctx_t
#define OPENVPN_MODE_CBC
Cipher is in CBC mode.
#define OPENVPN_MAX_IV_LENGTH
Maximum length of an IV.
#define OPENVPN_OP_ENCRYPT
Cipher should encrypt.
#define D_SHOW_KEY_SOURCE
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.
static int max_int(int x, int y)
static const char * np(const char *str)
static void update_time(void)
uint16_t packet_id_read_epoch(struct packet_id_net *pin, struct buffer *buf)
Reads the packet ID containing both the epoch and the per-epoch counter from the buf.
bool packet_id_write_epoch(struct packet_id_send *p, uint16_t epoch, struct buffer *buf)
Writes the packet ID containing both the epoch and the packet id to the buffer specified by buf.
bool packet_id_test(struct packet_id_rec *p, const struct packet_id_net *pin)
bool packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool long_form)
const char * packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp, struct gc_arena *gc)
void packet_id_add(struct packet_id_rec *p, const struct packet_id_net *pin)
bool packet_id_write(struct packet_id_send *p, struct buffer *buf, bool long_form, bool prepend)
Write a packet ID to buf, and update the packet ID state.
static void packet_id_reap_test(struct packet_id_rec *p)
static void packet_id_persist_save_obj(struct packet_id_persist *p, const struct packet_id *pid)
static bool packet_id_initialized(const struct packet_id *pid)
Is this struct packet_id initialized?
static int packet_id_size(bool long_form)
Wrapper structure for dynamically allocated memory.
int capacity
Size in bytes of memory allocated by malloc().
uint8_t * data
Pointer to the allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
int offset
Offset in bytes of the actual content within the allocated memory.
Struct used in cipher name translation table.
const char * openvpn_name
Cipher name used by OpenVPN.
const char * lib_name
Cipher name used by crypto library.
Security parameter state for processing data channel packets.
struct key_ctx epoch_retiring_data_receive_key
The old key before the sender switched to a new epoch data key.
unsigned int flags
Bit-flags determining behavior of security operation functions.
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.
struct packet_id_rec epoch_retiring_key_pid_recv
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Packet geometry parameters.
struct frame::@074234134026241341315172337061247271261307273127 buf
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
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.
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.
struct packet_id_send send
static int cleanup(void **state)