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B6kL8c5iPPigoWW0uLA9zteDRFes\n"
103 "ez+Z8wBY6g8VQ0tFPURDooUg5011GZPDcuw7/PsI4+I2J9q6LHEp+6Oo4faSn/kl\n"
104 "yWYCLjM4FZdGXbOijDacQJiN6HcRv0UdodBrEVRf7YHJJmMCbCI7ZUGW2zef/+rO\n"
105 "e4Lkxh0MLYqCkNKH5ZfoGTC4Oeb0xKykswAanqgR60r+upaLU8PFuI2L9M3vc6KU\n"
106 "F6MgVGSxl6eylJgDYckvJiAbmcp2PD/LRQQOxQA0yqeAMg2cbdvclETuYD6zoFfu\n"
108 "-----END CERTIFICATE-----\n";
111 "-----BEGIN PRIVATE KEY-----\n"
112 "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC7xFoR6fmoyfsJ\n"
113 "IQDKKgbYgFw0MzVuDAmpRx6KTEihgTchkQx9fHddWbKiOUbcEnQi3LNux7P4QVl/\n"
114 "4dRR3skisBug6Vd5LXeBGZqmpu5XZiF4DgLz1lX21G0aOogFWkie2qGEcso40159\n"
115 "x9FBDl5A3sLP18ubeex0pd/BzDFv6SLOTyVWO/GCNc8IX/i0uN4mLvoVU00SeqwT\n"
116 "PnS+CRXrSq4JjGDJLsXl0/PlxkjsgU0yOOA0Z2d8Fzk3wClwP6Hc49BOMWKstUIh\n"
117 "LbG2DcIv8l29EuEj2w3ju/7gkewol96XQ2twpPvpoVAaiVh/m7hQUcQORQCD6eJc\n"
118 "DjOZVCArAgMBAAECggEACqkuWAAJ3cyCBVWrXs8eDmLTWV9i9DmYvtS75ixIn2rf\n"
119 "v3cl12YevN0f6FgKLuqZT3Vqdqq+DCVhuIIQ9QkKMH8BQpSdE9NCCsFyZ23o8Gtr\n"
120 "EQ7ymfecb+RFwYx7NpqWrvZI32VJGArgPZH/zorLTTGYrAZbmBtHEqRsXOuEDw97\n"
121 "slwwcWaa9ztaYC8/N/7fgsnydaCFSaOByRlWuyvSmHvn6ZwLv8ANOshY6fstC0Jb\n"
122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
137 "ZbX6nKcJ7jciJVKJds59Jg==\n"
138 "-----END PRIVATE KEY-----\n";
149 assert_non_null(ret);
170 if (certfd < 0 || keyfd < 0)
172 fail_msg(
"make tmpfile for certificate or key data failed (error = %d)", errno);
204#ifdef ENABLE_CRYPTO_OPENSSL
205 cert = SSL_CTX_get0_certificate(
ctx.ctx);
206#elif defined(ENABLE_CRYPTO_MBEDTLS)
207 cert =
ctx.crt_chain;
248#if !defined(HAVE_OPENSSL_STORE)
261 const char *
lead =
"";
469 struct key2 *statickey)
525 bool ischacha = !strcmp(cipher,
"ChaCha20-Poly1305");
536 bool ischacha = !strcmp(cipher,
"ChaCha20-Poly1305");
639 const uint8_t
key[] =
640 {
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'A',
'B',
'C',
'D',
'E',
'F',
641 'G',
'H',
'j',
'k',
'u',
'c',
'h',
'e',
'n',
'l'};
643 static_assert(
sizeof(
key) == 32,
"Size of key should be 32 bytes");
649 for (
int i = 0; i <
sizeof(
key2.
keys); i++)
651 keydata[i] = (uint8_t) (
key[i %
sizeof(
key)] ^ i);
692 const char *
plaintext =
"The quick little fox jumps over the bureaucratic hurdles";
733 {0x0f, 0xff, 0xf5, 0x91, 0x3d, 0x39, 0xd7, 0x5b,
734 0x18, 0x57, 0x3b, 0x57, 0x48, 0x58, 0x9a, 0x7d};
742 {0x1f, 0xdd, 0x90, 0x8f, 0x0e, 0x9d, 0xc2, 0x5e, 0x79, 0xd8, 0x32, 0x02, 0x0d, 0x58, 0xe7, 0x3f};
787 const struct CMUnitTest tests[] = {
807#if defined(ENABLE_CRYPTO_OPENSSL)
811 int ret = cmocka_run_group_tests_name(
"ssl tests", tests,
init,
cleanup);
813#if defined(ENABLE_CRYPTO_OPENSSL)
bool buf_printf(struct buffer *buf, const char *format,...)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
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
static struct buffer clear_buf(void)
Return an empty struct buffer.
#define CC_ANY
any character
static bool buf_advance(struct buffer *buf, int size)
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)
#define CC_BACKSLASH
backslash
static bool buf_write_u8(struct buffer *dest, uint8_t data)
static void gc_free(struct gc_arena *a)
#define buf_init(buf, offset)
static struct gc_arena gc_new(void)
void free_key_ctx_bi(struct key_ctx_bi *ctx)
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)
Data Channel Cryptography Module.
#define CO_EPOCH_DATA_KEY_FORMAT
Bit-flag indicating the epoch the data format.
#define KEY_DIRECTION_BIDIRECTIONAL
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.
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.
static bool cipher_valid(const char *ciphername)
Returns if the cipher is valid, based on the given cipher name.
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
#define OPENVPN_AEAD_TAG_LENGTH
void free_epoch_key_ctx(struct crypto_options *co)
Frees the extra data structures used by epoch keys in crypto_options.
void epoch_init_key_ctx(struct crypto_options *co, const struct key_type *key_type, const struct epoch_key *e1_send, const struct epoch_key *e1_recv, uint16_t future_key_count)
Initialises data channel keys and internal structures for epoch data keys using the provided E0 epoch...
void epoch_iterate_send_key(struct crypto_options *co)
Updates the send key and send_epoch_key in cryptio_options->key_ctx_bi to use the next epoch.
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.
#define RELIABLE_ACK_SIZE
The maximum number of packet IDs waiting to be acknowledged which can be stored in one reliable_ack s...
static void update_time(void)
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
void packet_id_free(struct packet_id *p)
#define PACKET_ID_EPOCH_MAX
static char * auth_challenge
Control Channel SSL/Data channel negotiation module.
Control Channel SSL library backend module.
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline)
Load private key file into the given TLS context.
void tls_free_lib(void)
Free any global SSL library-specific data structures.
void tls_init_lib(void)
Perform any static initialisation necessary by the library.
void tls_ctx_client_new(struct tls_root_ctx *ctx)
Initialises a library-specific TLS context for a client.
void tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
Load certificate file into the given TLS context.
Control Channel Verification Module library-specific backend interface.
result_t backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename)
mbedtls_x509_crt openvpn_x509_cert_t
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.
uint8_t epoch_key[SHA256_DIGEST_LENGTH]
Packet geometry parameters.
int tun_mtu
the (user) configured tun-mtu.
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...
int tailroom
the tailroom in the buffer.
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.
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
uint16_t epoch
OpenVPN data channel epoch, this variable holds the epoch number this key belongs to.
uint64_t plaintext_blocks
Counter for the number of plaintext block encrypted using this cipher with the current key in number ...
const char * cipher
const name of the cipher
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.
struct packet_id_send send
Structure that wraps the TLS context.
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 test_data_channel_roundtrip_aes_192_cbc(void **state)
static void test_load_certificate_and_key_uri(void **state)
bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *auth_challenge)
Retrieves the user credentials from various sources depending on the flags.
static void test_data_channel_roundtrip_aes_192_gcm_epoch(void **state)
static void encrypt_one_packet(struct crypto_options *co, int len)
static void test_data_channel_roundtrip_aes_256_cbc(void **state)
static void test_load_certificate_and_key(void **state)
static void test_data_channel_known_vectors_run(bool epoch)
static struct key2 create_key(void)
void purge_user_pass(struct user_pass *up, bool force)
static void check_aead_limits(struct crypto_options *co, bool chachapoly)
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
static const char *const unittest_cert
static void run_data_channel_with_cipher_epoch(const char *cipher)
static int cleanup(void **state)
static struct @27 global_state
static void do_data_channel_round_trip(struct crypto_options *co)
static void test_data_channel_roundtrip_aes_128_gcm_epoch(void **state)
static void crypto_pem_encode_certificate(void **state)
static void test_data_channel_roundtrip_aes_128_gcm(void **state)
static void test_data_channel_roundtrip_aes_192_gcm(void **state)
void throw_signal(const int signum)
Throw a hard signal.
static void init_frame_parameters(struct frame *frame)
static void test_data_channel_roundtrip_bf_cbc(void **state)
static void test_data_channel_roundtrip_chacha20_poly1305(void **state)
struct signal_info siginfo_static
static const char * get_tmp_dir(void)
static void uninit_crypto_options(struct crypto_options *co)
static void test_data_channel_roundtrip_aes_256_gcm(void **state)
static void test_data_channel_known_vectors_epoch(void **state)
static void test_data_channel_roundtrip_chacha20_poly1305_epoch(void **state)
static const char *const unittest_key
static void test_data_channel_roundtrip_aes_256_gcm_epoch(void **state)
static int init(void **state)
static void test_data_channel_roundtrip_aes_128_cbc(void **state)
static struct crypto_options init_crypto_options(const char *cipher, const char *auth, bool epoch, struct key2 *statickey)
static void run_data_channel_with_cipher(const char *cipher, const char *auth)
static void test_data_channel_known_vectors_shortpktid(void **state)
const char * win_get_tempdir(void)