44 const char *label,
size_t label_size,
45 void *ekm,
size_t ekm_size)
47 memset(ekm, 0xba, ekm_size);
52#define TESTBUF_SIZE 128
55#define PATH1 "/s p a c e"
56#define PATH2 "/foo bar/baz"
57#define PARAM1 "param1"
58#define PARAM2 "param two"
61 "-----BEGIN OpenVPN tls-crypt-v2 server key-----\n"
62 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
63 "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
64 "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8=\n"
65 "-----END OpenVPN tls-crypt-v2 server key-----\n";
68 "-----BEGIN OpenVPN tls-crypt-v2 client key-----\n"
69 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
70 "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
71 "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
72 "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
73 "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
74 "8PHy8/T19vf4+fr7/P3+/xd9pcB0qUYZsWvkrLcfGmzPJPM8a7r0mEWdXwbDadSV\n"
75 "LHg5bv2TwlmPR3HgaMr8o9LTh9hxUTkrH3S0PfKRNwcso86ua/dBFTyXsM9tg4aw\n"
76 "3dS6ogH9AkaT+kRRDgNcKWkQCbwmJK2JlfkXHBwbAtmn78AkNuho6QCFqCdqGab3\n"
77 "zh2vheFqGMPdGpukbFrT3rcO3VLxUeG+RdzXiMTCpJSovFBP1lDkYwYJPn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o+1lcsjDCJVQA6XUyltas4BKQ==\n"
99 "-----END OpenVPN tls-crypt-v2 client key-----\n";
103 const int line_num,
int msglevel,
struct gc_arena *
gc)
114 const char *pem =
BSTR(buf);
115 check_expected(filename);
118 return mock_type(
bool);
124 check_expected(filename);
126 const char *pem_str = mock_ptr_type(
const char *);
138 for (
int i = 0;
i <
len;
i++)
178 const char *plaintext =
"1234567890";
182 const char *ciphertext =
"012345678";
259 uint8_t expected_ciphertext[] = {
260 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xe3, 0x19, 0x27, 0x7f, 0x1c, 0x8d, 0x6e, 0x6a,
261 0x77, 0x96, 0xa8, 0x55, 0x33, 0x7b, 0x9c, 0xfb, 0x56, 0xe1, 0xf1, 0x3a, 0x87, 0x0e, 0x66, 0x47,
262 0xdf, 0xa1, 0x95, 0xc9, 0x2c, 0x17, 0xa0, 0x15, 0xba, 0x49, 0x67, 0xa1, 0x1d, 0x55, 0xea, 0x1a,
271 memset(&
session.tls_wrap.original_wrap_keydata.keys, 0x00,
sizeof(
session.tls_wrap.original_wrap_keydata.keys));
272 session.tls_wrap.original_wrap_keydata.n = 2;
282 memset(&
session.tls_wrap.original_wrap_keydata.keys, 0x42,
sizeof(
session.tls_wrap.original_wrap_keydata.keys));
289 assert_memory_equal(
BPTR(&rctx->
work), expected_ciphertext, 8);
451 "tls-crypt-v2 server key");
495 struct key2 unwrapped_client_key2 = { 0 };
525 struct key2 unwrapped_client_key2 = { 0 };
527 &unwrap_metadata, ctx->
wkc,
580 "wrong tls-crypt-v2 server key");
583 struct key2 unwrapped_client_key2 = { 0 };
589 const struct key2 zero = { 0 };
590 assert_true(0 == memcmp(&unwrapped_client_key2, &zero,
sizeof(zero)));
612 struct key2 unwrapped_client_key2 = { 0 };
613 struct buffer unwrapped_metadata =
616 &unwrapped_metadata, ctx->
wkc,
619 const struct key2 zero = { 0 };
620 assert_true(0 == memcmp(&unwrapped_client_key2, &zero,
sizeof(zero)));
627 const char *filename =
"testfilename.key";
640 const char *filename =
"testfilename.key";
658 const char *filename =
"testfilename.key";
659 const char *b64metadata =
"AABBCCDD";
679 const struct CMUnitTest tests[] = {
724#if defined(ENABLE_CRYPTO_OPENSSL)
725 OpenSSL_add_all_algorithms();
728 int ret = cmocka_run_group_tests_name(
"tls-crypt tests", tests, NULL, NULL);
730#if defined(ENABLE_CRYPTO_OPENSSL)
void free_buf(struct buffer *buf)
void buf_clear(struct buffer *buf)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
struct buffer alloc_buf(size_t size)
static bool buf_inc_len(struct buffer *buf, int inc)
static bool buf_equal(const struct buffer *a, const struct buffer *b)
Return true if buffer contents are equal.
static bool buf_advance(struct buffer *buf, int size)
static int buf_len(const struct buffer *buf)
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
static void gc_free(struct gc_arena *a)
static struct gc_arena gc_new(void)
void free_key_ctx_bi(struct key_ctx_bi *ctx)
void init_key_ctx(struct key_ctx *ctx, const struct key_parameters *key, const struct key_type *kt, int enc, const char *prefix)
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)
void free_key_ctx(struct key_ctx *ctx)
#define CO_IGNORE_PACKET_ID
Bit-flag indicating whether to ignore the packet ID of a received packet.
#define KEY_DIRECTION_BIDIRECTIONAL
#define MAX_CIPHER_KEY_LENGTH
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
#define MAX_HMAC_KEY_LENGTH
#define TLS_CRYPT_V2_MAX_WKC_LEN
bool tls_crypt_v2_extract_client_key(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct tls_options *opt)
Extract a tls-crypt-v2 client key from a P_CONTROL_HARD_RESET_CLIENT_V3 message, and load the key int...
void tls_crypt_v2_write_client_key_file(const char *filename, const char *b64_metadata, const char *server_key_file, bool server_key_inline)
Generate a tls-crypt-v2 client key, and write to file.
bool tls_crypt_unwrap(const struct buffer *src, struct buffer *dst, struct crypto_options *opt)
Unwrap a control channel packet (decrypts, authenticates and performs replay checks).
bool tls_session_generate_dynamic_tls_crypt_key(struct tls_session *session)
Generates a TLS-Crypt key to be used with dynamic tls-crypt using the TLS EKM exporter function.
void tls_crypt_v2_write_server_key_file(const char *filename)
Generate a tls-crypt-v2 server key, and write to file.
int tls_crypt_buf_overhead(void)
Returns the maximum overhead (in bytes) added to the destination buffer by tls_crypt_wrap().
bool tls_crypt_wrap(const struct buffer *src, struct buffer *dst, struct crypto_options *opt)
Wrap a control channel packet (both authenticates and encrypts the data).
#define TLS_CRYPT_V2_MAX_METADATA_LEN
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
static void tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
Free the elements of a tls_wrap_ctx structure.
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
Security parameter state for processing data channel packets.
unsigned int flags
Bit-flags determining behavior of security operation functions.
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
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.
internal structure similar to struct key that holds key information but is not represented on wire an...
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.
struct key_ctx_bi client_key
struct buffer unwrapped_metadata
struct key_ctx_bi server_keys
Security parameter state of a single session within a VPN tunnel.
Control channel wrapping (–tls-auth/–tls-crypt) context.
@ TLS_WRAP_CRYPT
Control channel encryption and authentication.
struct crypto_options opt
Crypto state.
struct buffer work
Work buffer (only for –tls-crypt)
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
enum tls_wrap_ctx::@23 mode
Control channel wrapping mode.
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
static void tls_crypt_loopback(void **state)
Check that short messages are successfully wrapped-and-unwrapped.
static void tls_crypt_fail_invalid_key(void **state)
Check that packets that were wrapped (or unwrapped) with a different key are not accepted.
static void tls_crypt_loopback_zero_len(void **state)
Check that zero-byte messages are successfully wrapped-and-unwrapped.
static void test_tls_crypt_v2_write_server_key_file(void **state)
static const char * test_server_key
static void tls_crypt_v2_wrap_unwrap_max_metadata(void **state)
Check wrapping and unwrapping a tls-crypt-v2 client key with maximum length metadata.
static int test_tls_crypt_v2_teardown(void **state)
static int test_tls_crypt_teardown(void **state)
bool key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size, void *ekm, size_t ekm_size)
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
static void test_tls_crypt_v2_write_client_key_file_metadata(void **state)
static int test_tls_crypt_setup(void **state)
static void tls_crypt_v2_wrap_unwrap_no_metadata(void **state)
Check wrapping and unwrapping a tls-crypt-v2 client key without metadata.
static void skip_if_tls_crypt_not_supported(struct test_tls_crypt_context *ctx)
static int test_tls_crypt_v2_setup(void **state)
static void tls_crypt_v2_wrap_unwrap_dst_too_small(void **state)
Check that unwrapping a tls-crypt-v2 client key to a too small metadata buffer fails as expected.
static void test_tls_crypt_v2_write_client_key_file(void **state)
static void tls_crypt_ignore_replay(void **state)
Check that packet replays are accepted when CO_IGNORE_PACKET_ID is set.
static void tls_crypt_v2_wrap_unwrap_wrong_key(void **state)
Check that unwrapping a tls-crypt-v2 client key with the wrong server key fails as expected.
struct buffer __wrap_buffer_read_from_file(const char *filename, struct gc_arena *gc)
static void tls_crypt_fail_replay(void **state)
Check that replayed packets are not accepted.
static const char * test_client_key
static const char * test_client_key_metadata
int __wrap_rand_bytes(uint8_t *output, int len)
Predictable random for tests.
static void tls_crypt_loopback_max_len(void **state)
Check that max-length messages are successfully wrapped-and-unwrapped.
int __wrap_parse_line(const char *line, char **p, const int n, const char *file, const int line_num, int msglevel, struct gc_arena *gc)
static void test_tls_crypt_secure_reneg_key(void **state)
Test generating dynamic tls-crypt key.
bool __wrap_buffer_write_file(const char *filename, const struct buffer *buf)
static void tls_crypt_v2_wrap_too_long_metadata(void **state)
Check that wrapping a tls-crypt-v2 client key with too long metadata fails as expected.
static void tls_crypt_fail_msg_too_long(void **state)
Check that too-long messages are gracefully rejected.
static bool tls_crypt_v2_unwrap_client_key(struct key2 *client_key, struct buffer *metadata, struct buffer wrapped_client_key, struct key_ctx *server_key)
static bool tls_crypt_v2_wrap_client_key(struct buffer *wkc, const struct key2 *src_key, const struct buffer *src_metadata, struct key_ctx *server_key, struct gc_arena *gc)
static struct key_type tls_crypt_kt(void)