1524bc
From 40a13974c5279d43fa2e13b8a274ba41ec051533 Mon Sep 17 00:00:00 2001
1524bc
From: Andrew Bartlett <abartlet@samba.org>
1524bc
Date: Thu, 15 Aug 2019 15:27:30 +1200
1524bc
Subject: [PATCH 084/187] encrypted_secrets: Add known and expected value test
1524bc
1524bc
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
1524bc
Reviewed-by: Andreas Schneider <asn@samba.org>
1524bc
(cherry picked from commit 92b9cdf99da1f8657c166d413c5136c8db938a9e)
1524bc
---
1524bc
 .../tests/test_encrypted_secrets.c            | 51 +++++++++++++++++++
1524bc
 1 file changed, 51 insertions(+)
1524bc
1524bc
diff --git a/source4/dsdb/samdb/ldb_modules/tests/test_encrypted_secrets.c b/source4/dsdb/samdb/ldb_modules/tests/test_encrypted_secrets.c
1524bc
index 258e1ba829f..cfea95ae544 100644
1524bc
--- a/source4/dsdb/samdb/ldb_modules/tests/test_encrypted_secrets.c
1524bc
+++ b/source4/dsdb/samdb/ldb_modules/tests/test_encrypted_secrets.c
1524bc
@@ -1101,6 +1101,53 @@ static void test_unencrypted_secret(void **state)
1524bc
 	assert_int_equal(LDB_ERR_OPERATIONS_ERROR, ret);
1524bc
 }
1524bc
 
1524bc
+/*
1524bc
+ *  Test full decryption of a static value with static key
1524bc
+ */
1524bc
+static void test_record_decryption(void **state)
1524bc
+{
1524bc
+	struct ldbtest_ctx *test_ctx =
1524bc
+		talloc_get_type_abort(*state, struct ldbtest_ctx);
1524bc
+	unsigned char plain_data[] = {
1524bc
+		0xe6, 0xa6, 0xb8, 0xff, 0xdf, 0x06, 0x6c, 0xe3,
1524bc
+		0xea, 0xd0, 0x94, 0xbb, 0x79, 0xbd, 0x0a, 0x24
1524bc
+	};
1524bc
+	unsigned char encrypted_data[] = {
1524bc
+		0x0c, 0x00, 0x00, 0x00, 0x33, 0x91, 0x74, 0x25,
1524bc
+		0x26, 0xcc, 0x0b, 0x8c, 0x21, 0xc1, 0x13, 0xe2,
1524bc
+		0xed, 0xad, 0x5c, 0xca, 0x01, 0x00, 0x00, 0x00,
1524bc
+		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1524bc
+		0x1a, 0xdc, 0xc9, 0x11, 0x08, 0xca, 0x2c, 0xfb,
1524bc
+		0xc8, 0x32, 0x6b, 0x1b, 0x25, 0x7f, 0x52, 0xbb,
1524bc
+		0xae, 0x9b, 0x88, 0x52, 0xb0, 0x18, 0x6d, 0x9d,
1524bc
+		0x9b, 0xdd, 0xcd, 0x1b, 0x5f, 0x4a, 0x5c, 0x29,
1524bc
+		0xca, 0x0b, 0x36, 0xaa
1524bc
+	};
1524bc
+	struct ldb_val cipher_text
1524bc
+		= data_blob_const(encrypted_data,
1524bc
+				  sizeof(encrypted_data));
1524bc
+	unsigned char es_keys_blob[] = {
1524bc
+		0x1d, 0xae, 0xf5, 0xaa, 0xa3, 0x85, 0x0d, 0x0a,
1524bc
+		0x8c, 0x24, 0x5c, 0x4c, 0xa7, 0x0f, 0x81, 0x79
1524bc
+	};
1524bc
+	struct es_data data = {
1524bc
+		.encrypt_secrets = true,
1524bc
+		.keys[0] = {
1524bc
+			.data = es_keys_blob,
1524bc
+			.length = sizeof(es_keys_blob),
1524bc
+		},
1524bc
+#ifdef HAVE_GNUTLS_AEAD
1524bc
+		.encryption_algorithm = GNUTLS_CIPHER_AES_128_GCM,
1524bc
+#endif
1524bc
+	};
1524bc
+	int err = LDB_SUCCESS;
1524bc
+	struct ldb_val dec = decrypt_value(&err, test_ctx, test_ctx->ldb, cipher_text,
1524bc
+					   &data);
1524bc
+	assert_int_equal(LDB_SUCCESS, err);
1524bc
+	assert_int_equal(sizeof(plain_data), dec.length);
1524bc
+	assert_memory_equal(dec.data, plain_data, sizeof(plain_data));
1524bc
+}
1524bc
+
1524bc
 
1524bc
 int main(void) {
1524bc
 	const struct CMUnitTest tests[] = {
1524bc
@@ -1166,6 +1213,10 @@ int main(void) {
1524bc
 			test_unencrypted_secret,
1524bc
 			setup_with_key,
1524bc
 			teardown),
1524bc
+		cmocka_unit_test_setup_teardown(
1524bc
+			test_record_decryption,
1524bc
+			setup_with_key,
1524bc
+			teardown),
1524bc
 	};
1524bc
 
1524bc
 	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
1524bc
-- 
1524bc
2.23.0
1524bc