From patchwork Mon Jan 22 16:04:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "plaisthos (Code Review)" X-Patchwork-Id: 3588 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7301:2802:b0:101:b91e:57cc with SMTP id hj2csp2668820dyb; Mon, 22 Jan 2024 08:04:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHw6ENnmNOO5L9xmxXc4bEYEMoQPzif1stHnM1uknXxmREV1hf9FjuSQXRUdQka5B+LtfIR X-Received: by 2002:a05:6a20:438d:b0:19a:e152:656 with SMTP id i13-20020a056a20438d00b0019ae1520656mr12025981pzl.3.1705939491013; Mon, 22 Jan 2024 08:04:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705939490; cv=none; d=google.com; s=arc-20160816; b=HCjBZPkbqqJnJvGfVfeBhC2k1y2fIhSXozXBIbUDbbj1c85qY/ca/TzWN4j04kxsWJ 0SLj67hDSsooe89Jm/HrIxWYDu+jcBp5oIByygpYdcrU9/zsXeb3eGZxe39LRdgZPvt0 OCHP+EaAHYnBE/QQGQV1XA009ZTCeLmgDSSnkmDqlxtCKkhW+DoLw5jrQDe++1vVe6lC nXh7qS93VUUQ19tkSDJLZBLWxsi+hG3KfWtUOIX3jE3EIQOA1PTAQo66sBvm9CB7h6KA 8g7PlIn6sFmi3rcjWvm42A+lNUcZh5LfWvy9sB0IYHXviHEFUK5ssRweZ7qKmwNhIcL9 Z3Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:cc:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:user-agent :mime-version:message-id:references:auto-submitted:to:date:from :dkim-signature:dkim-signature:dkim-signature; bh=SjSENSMm2UfpaUhhL6QSj7oukFkwxKW1llDP4DwJjYU=; fh=lm0MLPW7DntlrDqRECIiC9JlE1uPxhepE0URYHIf+eE=; b=jOO/S1h9HgKxTwv0L0F8r7q4xsP2/HXe3moaOWQo/GrndqZ29/cUJp8hYViqsAo9us 3RJ6IW2MqVeNcGORuMZNu9KOuu5rKtUrcP1PCAA/VUTeIiHph5qjLIvOtlJKxagFtZHb VGZBZf3nZbzXBundFIEowazK96D/TMtsB0lWqSHszU9Da2BIFxt6tR+ijVKgVtnUF5F0 1Ntq2Wc5SSVLdoUnGD8CUeP9mK1Yenf2Z0030pB2cR3aNs/1fUxQX5cPpSi0VdYPZwZB JgSZ3dTC0ytQwYzhynfkuG7GOldeFrQPURdk7XYiMLAoPvGHp2hWRChoghkWYoVORm7J GuHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=E130aE7K; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=YK4c8NDN; dkim=neutral (body hash did not verify) header.i=@openvpn.net header.s=google header.b=BnniP39p; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=openvpn.net Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id y18-20020a056a00181200b006dbd5a0a128si3397893pfa.307.2024.01.22.08.04.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Jan 2024 08:04:50 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=E130aE7K; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=YK4c8NDN; dkim=neutral (body hash did not verify) header.i=@openvpn.net header.s=google header.b=BnniP39p; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=openvpn.net Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1rRwmX-0000gj-MG; Mon, 22 Jan 2024 16:04:18 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rRwmW-0000gc-51 for openvpn-devel@lists.sourceforge.net; Mon, 22 Jan 2024 16:04:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Content-Transfer-Encoding:MIME-Version :Message-ID:Reply-To:References:Subject:List-Unsubscribe:List-Id:Cc:To:Date: From:Sender:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:List-Help: List-Subscribe:List-Post:List-Owner:List-Archive; bh=dGix51l5sZKEgnUD70hK9uZCQIgLEJ8bauYVy9AUG6s=; b=E130aE7KPC+tkPvCU7iZ7VXTRY pD3cZOKS3q6fslMLJv000TATcaI65pMpGldcT9FJy+rqHl3lRjrxfBffBoYb/+b4sxuz5bMfkH0ca d3ZsxP67eB9AF9x9luoIeRKJUrCqTgeCY4EXpLGgv4nK9o9tveHGzZQe+rFb7Z5VUPnA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Content-Transfer-Encoding:MIME-Version:Message-ID:Reply-To: References:Subject:List-Unsubscribe:List-Id:Cc:To:Date:From:Sender:Content-ID :Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To: Resent-Cc:Resent-Message-ID:In-Reply-To:List-Help:List-Subscribe:List-Post: List-Owner:List-Archive; bh=dGix51l5sZKEgnUD70hK9uZCQIgLEJ8bauYVy9AUG6s=; b=Y K4c8NDN5SvHp5PDlIZDp6TvRKkAjSJ3xGhRzevtSYOJxnUsBsba6z357GW4dnUPD3jAbLH3agTrbU im4ter/aPeIp8NeFLYjUNIHAGgsIAAOh7xiws4BLcK+yPFI9MkMMU2+wrSP/9ORgoIpxlBOPWnpMY jT2FqXrf8jNzoFgM=; Received: from mail-wm1-f49.google.com ([209.85.128.49]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1rRwmU-0007XR-Cc for openvpn-devel@lists.sourceforge.net; Mon, 22 Jan 2024 16:04:16 +0000 Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40e490c2115so26548955e9.0 for ; Mon, 22 Jan 2024 08:04:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1705939443; x=1706544243; darn=lists.sourceforge.net; h=user-agent:content-disposition:content-transfer-encoding :mime-version:message-id:reply-to:references:subject :list-unsubscribe:list-id:auto-submitted:cc:to:date:from:from:to:cc :subject:date:message-id:reply-to; bh=dGix51l5sZKEgnUD70hK9uZCQIgLEJ8bauYVy9AUG6s=; b=BnniP39pKUxdxF9WJizs+E+2ycHFez21fctRls7UfIwXfAg9kKWgT374DpR2gRfUAf zBIaT6fvGVzvPLUeY+gtiF+CWJ/UXlm0/RgJxbuyk09ylosWFJ7adKz5XrKDdqz8KjV0 +UmN9EwVdTAl0RtgNAoiGYYrGDOJaK3C1R1b1f8wOfCf3V04m9EvgXJ4bsXI3Tll69OF nR0fDDJVmJPLrhvbP9SLE8OY+zpqG+fWWMkMvqpJpdHs7aupRAkosw6lQy/vbj6GdVnv Q8lFsphIKF0jrN3lnfFKf34CydoYdtvnzPUoqLlklDzW7XbIcaIWzV3xLBTFKVebwv69 M3nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705939443; x=1706544243; h=user-agent:content-disposition:content-transfer-encoding :mime-version:message-id:reply-to:references:subject :list-unsubscribe:list-id:auto-submitted:cc:to:date:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dGix51l5sZKEgnUD70hK9uZCQIgLEJ8bauYVy9AUG6s=; b=KVnHL77Cbl67CH8HSMPvXNbaL8YiHXJlggOuvVie2VFlW0n7zXOiJ0PtBsQ3I0PMlC uVCaCIAOnRSVkG+l/3IWATSmppcuY9DVz+DeNdNLHQ3JTSo41unZw7eDDe4k2/xygox7 P1WZlsVYau1dRx+EHZKembgyVlHAtsW8sdE7fGKXJjxRN5WX2NOpMt0+OUcqVnDKbWK5 pipgfl4/bKuJ8zYNqY7SMrxOwjbVKd9/p/BnLGUoJLXe5tFUzfqEuC8YBt8tkFXsUkYh bEcv1IRvgYXJR/Y2AA4dCQj+QrwwjWZ9n179JFGKsRRIYdgJqoaQlj7TAJ9IaSAo9Xwc 1FFg== X-Gm-Message-State: AOJu0YxrfZNhsGg10XlNzuBYPtsVq0KFoKsmXoyqYZCACIBWZ03IJ+bb M7riRb3dI9PzJBcVFtftl6Rweuk9e8YRswJp6IMDWlPbbjwTR1MoiEyQyw4C6bLgGcZ0qKOxJ4U b X-Received: by 2002:a05:600c:2183:b0:40e:4794:f1d9 with SMTP id e3-20020a05600c218300b0040e4794f1d9mr2229252wme.149.1705939443062; Mon, 22 Jan 2024 08:04:03 -0800 (PST) Received: from gerrit.openvpn.in (ec2-18-159-0-78.eu-central-1.compute.amazonaws.com. [18.159.0.78]) by smtp.gmail.com with ESMTPSA id z10-20020a05600c0a0a00b0040e486bc0dfsm43948571wmp.27.2024.01.22.08.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 08:04:02 -0800 (PST) From: "plaisthos (Code Review)" X-Google-Original-From: "plaisthos (Code Review)" X-Gerrit-PatchSet: 1 Date: Mon, 22 Jan 2024 16:04:01 +0000 To: flichtenheld Auto-Submitted: auto-generated X-Gerrit-MessageType: newchange X-Gerrit-Change-Id: Ifa5ae96165d17b3cae4afc53e844bb34d1610e58 X-Gerrit-Change-Number: 505 X-Gerrit-Project: openvpn X-Gerrit-ChangeURL: X-Gerrit-Commit: 9439cc366821d5e2f88775636ed9ea48243cd634 References: Message-ID: <979ba1528b877912f2c278a805ba6c483372c97d-HTML@gerrit.openvpn.net> MIME-Version: 1.0 User-Agent: Gerrit/3.8.2 X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-2.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.128.49 listed in wl.mailspike.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.128.49 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 WEIRD_PORT URI: Uses non-standard port number for HTTP 0.0 HTML_MESSAGE BODY: HTML included in message -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.0 T_SCC_BODY_TEXT_LINE No description available. 0.0 T_KAM_HTML_FONT_INVALID Test for Invalidly Named or Formatted Colors in HTML X-Headers-End: 1rRwmU-0007XR-Cc Subject: [Openvpn-devel] [M] Change in openvpn[master]: Add unit test for encrypting/decrypting data channel X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arne-openvpn@rfc2549.org, openvpn-devel@lists.sourceforge.net, frank@lichtenheld.com Cc: openvpn-devel Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1788807207968463185?= X-GMAIL-MSGID: =?utf-8?q?1788807207968463185?= X-getmail-filter-classifier: gerrit message type newchange Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/505?usp=email to review the following change. Change subject: Add unit test for encrypting/decrypting data channel ...................................................................... Add unit test for encrypting/decrypting data channel This test is reusing code from --test-crypto but is modified to not rely on the static key functionality and also only tests the most common algorithm. So it does not yet completely replace --test-crypto Change-Id: Ifa5ae96165d17b3cae4afc53e844bb34d1610e58 --- M tests/unit_tests/openvpn/test_ssl.c 1 file changed, 223 insertions(+), 2 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/05/505/1 diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c index 18b9ec8..8c1fb5b 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -44,6 +44,9 @@ #include "ssl_verify_backend.h" #include "win32.h" #include "test_common.h" +#include "ssl.h" +#include "buffer.h" +#include "packet_id.h" /* Mock function to be allowed to include win32.c which is required for * getting the temp directory */ @@ -120,20 +123,238 @@ gc_free(&gc); } +static void +init_implicit_iv(struct crypto_options *co) +{ + cipher_ctx_t *cipher = co->key_ctx_bi.encrypt.cipher; + + if (cipher_ctx_mode_aead(cipher)) + { + size_t impl_iv_len = cipher_ctx_iv_length(cipher) - sizeof(packet_id_type); + ASSERT(cipher_ctx_iv_length(cipher) <= OPENVPN_MAX_IV_LENGTH); + ASSERT(cipher_ctx_iv_length(cipher) >= OPENVPN_AEAD_MIN_IV_LEN); + + /* Generate dummy implicit IV */ + ASSERT(rand_bytes(co->key_ctx_bi.encrypt.implicit_iv, + OPENVPN_MAX_IV_LENGTH)); + co->key_ctx_bi.encrypt.implicit_iv_len = impl_iv_len; + + memcpy(co->key_ctx_bi.decrypt.implicit_iv, + co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH); + co->key_ctx_bi.decrypt.implicit_iv_len = impl_iv_len; + } +} + +static void +init_frame_parameters(struct frame *frame) +{ + int overhead = 0; + + /* tls-auth and tls-crypt */ + overhead += 128; + + /* TCP length field and opcode */ + overhead += 3; + + /* ACK array and remote SESSION ID (part of the ACK array) */ + overhead += ACK_SIZE(RELIABLE_ACK_SIZE); + + /* Previous OpenVPN version calculated the maximum size and buffer of a + * control frame depending on the overhead of the data channel frame + * overhead and limited its maximum size to 1250. Since control frames + * also need to fit into data channel buffer we have the same + * default of 1500 + 100 as data channel buffers have. Increasing + * control channel mtu beyond this limit also increases the data channel + * buffers */ + int tls_mtu = 1500; + frame->buf.payload_size = tls_mtu + 100; + + frame->buf.headroom = overhead; + frame->buf.tailroom = overhead; + + frame->tun_mtu = tls_mtu; + +} + +static void +do_data_channel_round_trip(struct crypto_options *co) +{ + struct gc_arena gc = gc_new(); + + /* initialise frame for the test */ + struct frame frame; + init_frame_parameters(&frame); + + struct buffer src = alloc_buf_gc(frame.buf.payload_size, &gc); + struct buffer work = alloc_buf_gc(BUF_SIZE(&frame), &gc); + struct buffer encrypt_workspace = alloc_buf_gc(BUF_SIZE(&frame), &gc); + struct buffer decrypt_workspace = alloc_buf_gc(BUF_SIZE(&frame), &gc); + struct buffer buf = clear_buf(); + void *buf_p; + + /* init work */ + ASSERT(buf_init(&work, frame.buf.headroom)); + + init_implicit_iv(co); + update_time(); + + /* Test encryption, decryption for all packet sizes */ + for (int i = 1; i <= frame.buf.payload_size; ++i) + { + + /* msg(M_INFO, "TESTING ENCRYPT/DECRYPT of packet length=%d", i); */ + + /* + * Load src with random data. + */ + ASSERT(buf_init(&src, 0)); + ASSERT(i <= src.capacity); + src.len = i; + ASSERT(rand_bytes(BPTR(&src), BLEN(&src))); + + /* copy source to input buf */ + buf = work; + buf_p = buf_write_alloc(&buf, BLEN(&src)); + ASSERT(buf_p); + memcpy(buf_p, BPTR(&src), BLEN(&src)); + + /* initialize work buffer with buf.headroom bytes of prepend capacity */ + ASSERT(buf_init(&encrypt_workspace, frame.buf.headroom)); + + /* encrypt */ + openvpn_encrypt(&buf, encrypt_workspace, co); + + /* decrypt */ + openvpn_decrypt(&buf, decrypt_workspace, co, &frame, BPTR(&buf)); + + /* compare */ + assert_int_equal(buf.len, src.len); + assert_memory_equal(BPTR(&src), BPTR(&buf), i); + + } + gc_free(&gc); +} + + + +struct crypto_options +init_crypto_options(const char *cipher, const char *auth) +{ + struct key2 key2 = { .n = 2}; + + ASSERT(rand_bytes(key2.keys[0].cipher, sizeof(key2.keys[0].cipher))); + ASSERT(rand_bytes(key2.keys[0].hmac, sizeof(key2.keys[0].hmac))); + ASSERT(rand_bytes(key2.keys[1].cipher, sizeof(key2.keys[1].cipher))); + ASSERT(rand_bytes(key2.keys[1].hmac, sizeof(key2.keys)[1].hmac)); + + struct crypto_options co = { 0 }; + + struct key_type kt = create_kt(cipher, auth, "ssl-test"); + + init_key_ctx_bi(&co.key_ctx_bi, &key2, 0, &kt, "unit-test-ssl"); + packet_id_init(&co.packet_id, 5, 5, "UNITTEST", 0); + + return co; +} + +static void +uninit_crypto_options(struct crypto_options *co) +{ + packet_id_free(&co->packet_id); + free_key_ctx_bi(&co->key_ctx_bi); + +} + + +static void +run_data_channel_with_cipher(const char *cipher, const char *auth) +{ + struct crypto_options co = init_crypto_options(cipher, auth); + do_data_channel_round_trip(&co); + uninit_crypto_options(&co); +} + +static void +test_data_channel_roundtrip_aes_128_gcm(void **state) +{ + run_data_channel_with_cipher("AES-128-GCM", "none"); +} + +static void +test_data_channel_roundtrip_aes_192_gcm(void **state) +{ + run_data_channel_with_cipher("AES-192-GCM", "none"); +} + +static void +test_data_channel_roundtrip_aes_256_gcm(void **state) +{ + run_data_channel_with_cipher("AES-256-GCM", "none"); +} + +static void +test_data_channel_roundtrip_aes_128_cbc(void **state) +{ + run_data_channel_with_cipher("AES-128-CBC", "SHA256"); +} + +static void +test_data_channel_roundtrip_aes_192_cbc(void **state) +{ + run_data_channel_with_cipher("AES-192-CBC", "SHA256"); +} + +static void +test_data_channel_roundtrip_aes_256_cbc(void **state) +{ + run_data_channel_with_cipher("AES-256-CBC", "SHA256"); +} + +static void +test_data_channel_roundtrip_chacha20_poly1305(void **state) +{ + if (!cipher_valid("ChaCha20-Poly1305")) + { + skip(); + return; + } + run_data_channel_with_cipher("ChaCha20-Poly1305", "none"); +} + +static void +test_data_channel_roundtrip_bf_cbc(void **state) +{ + if (!cipher_valid("BF-CBC")) + { + skip(); + return; + } + run_data_channel_with_cipher("BF-CBC", "SHA1"); +} + + int main(void) { openvpn_unit_test_setup(); const struct CMUnitTest tests[] = { - cmocka_unit_test(crypto_pem_encode_certificate) + cmocka_unit_test(crypto_pem_encode_certificate), + cmocka_unit_test(test_data_channel_roundtrip_aes_128_gcm), + cmocka_unit_test(test_data_channel_roundtrip_aes_192_gcm), + cmocka_unit_test(test_data_channel_roundtrip_aes_256_gcm), + cmocka_unit_test(test_data_channel_roundtrip_chacha20_poly1305), + cmocka_unit_test(test_data_channel_roundtrip_aes_128_cbc), + cmocka_unit_test(test_data_channel_roundtrip_aes_192_cbc), + cmocka_unit_test(test_data_channel_roundtrip_aes_256_cbc), + cmocka_unit_test(test_data_channel_roundtrip_bf_cbc), }; #if defined(ENABLE_CRYPTO_OPENSSL) tls_init_lib(); #endif - int ret = cmocka_run_group_tests_name("crypto tests", tests, NULL, NULL); + int ret = cmocka_run_group_tests_name("ssl tests", tests, NULL, NULL); #if defined(ENABLE_CRYPTO_OPENSSL) tls_free_lib();