b6b438
From 806c921c8be6d76bb8d01cf290112bceca513b42 Mon Sep 17 00:00:00 2001
b6b438
From: Isaac Boukris <iboukris@gmail.com>
b6b438
Date: Sat, 19 Oct 2019 23:48:19 +0300
b6b438
Subject: [PATCH 174/187] smbdes: add des_crypt56_gnutls() using DES-CBC with
b6b438
 zeroed IV
b6b438
b6b438
Signed-off-by: Isaac Boukris <iboukris@samba.org>
b6b438
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
b6b438
(cherry picked from commit 0f855f1ab955e3ecf47689c5e4578eb67ebe8f27)
b6b438
---
b6b438
 libcli/auth/proto.h              |  4 +++
b6b438
 libcli/auth/smbdes.c             | 57 ++++++++++++++++++++++++++++++++
b6b438
 libcli/auth/tests/test_gnutls.c  |  9 +++++
b6b438
 libcli/auth/wscript_build        |  2 +-
b6b438
 source3/passdb/wscript_build     |  2 +-
b6b438
 source3/rpc_server/wscript_build |  3 +-
b6b438
 6 files changed, 74 insertions(+), 3 deletions(-)
b6b438
b6b438
diff --git a/libcli/auth/proto.h b/libcli/auth/proto.h
b6b438
index eb725c83d15..e7c9923abf3 100644
b6b438
--- a/libcli/auth/proto.h
b6b438
+++ b/libcli/auth/proto.h
b6b438
@@ -4,6 +4,8 @@
b6b438
 #undef _PRINTF_ATTRIBUTE
b6b438
 #define _PRINTF_ATTRIBUTE(a1, a2) PRINTF_ATTRIBUTE(a1, a2)
b6b438
 
b6b438
+#include "lib/crypto/gnutls_helpers.h"
b6b438
+
b6b438
 /* this file contains prototypes for functions that are private 
b6b438
  * to this subsystem or library. These functions should not be 
b6b438
  * used outside this particular subsystem! */
b6b438
@@ -217,6 +219,8 @@ WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
b6b438
 /* The following definitions come from /home/jeremy/src/samba/git/master/source3/../source4/../libcli/auth/smbdes.c  */
b6b438
 
b6b438
 void des_crypt56(uint8_t out[8], const uint8_t in[8], const uint8_t key[7], int forw);
b6b438
+int des_crypt56_gnutls(uint8_t out[8], const uint8_t in[8], const uint8_t key[7],
b6b438
+		       enum samba_gnutls_direction encrypt);
b6b438
 void E_P16(const uint8_t *p14,uint8_t *p16);
b6b438
 void E_P24(const uint8_t *p21, const uint8_t *c8, uint8_t *p24);
b6b438
 void D_P16(const uint8_t *p14, const uint8_t *in, uint8_t *out);
b6b438
diff --git a/libcli/auth/smbdes.c b/libcli/auth/smbdes.c
b6b438
index 59cb45d81f0..f384ef132a7 100644
b6b438
--- a/libcli/auth/smbdes.c
b6b438
+++ b/libcli/auth/smbdes.c
b6b438
@@ -23,6 +23,9 @@
b6b438
 #include "includes.h"
b6b438
 #include "libcli/auth/libcli_auth.h"
b6b438
 
b6b438
+#include <gnutls/gnutls.h>
b6b438
+#include <gnutls/crypto.h>
b6b438
+
b6b438
 /* NOTES: 
b6b438
 
b6b438
    This code makes no attempt to be fast! In fact, it is a very
b6b438
@@ -273,6 +276,60 @@ static void str_to_key(const uint8_t *str,uint8_t *key)
b6b438
 	}
b6b438
 }
b6b438
 
b6b438
+int des_crypt56_gnutls(uint8_t out[8], const uint8_t in[8],
b6b438
+		       const uint8_t key_in[7],
b6b438
+		       enum samba_gnutls_direction encrypt)
b6b438
+{
b6b438
+	/*
b6b438
+	 * A single block DES-CBC op, with an all-zero IV is the same as DES
b6b438
+	 * because the IV is combined with the data using XOR.
b6b438
+	 * This allows us to use GNUTLS_CIPHER_DES_CBC from GnuTLS and not
b6b438
+	 * implement single-DES in Samba.
b6b438
+	 *
b6b438
+	 * In turn this is used to build DES-ECB, which is used
b6b438
+	 * for example in the NTLM challenge/response calculation.
b6b438
+	 */
b6b438
+	static const uint8_t iv8[8];
b6b438
+	gnutls_datum_t iv = { discard_const(iv8), 8 };
b6b438
+	gnutls_datum_t key;
b6b438
+	gnutls_cipher_hd_t ctx;
b6b438
+	uint8_t key2[8];
b6b438
+	uint8_t outb[8];
b6b438
+	int ret;
b6b438
+
b6b438
+	memset(out, 0, 8);
b6b438
+
b6b438
+	str_to_key(key_in, key2);
b6b438
+
b6b438
+	key.data = key2;
b6b438
+	key.size = 8;
b6b438
+
b6b438
+	ret = gnutls_global_init();
b6b438
+	if (ret != 0) {
b6b438
+		return ret;
b6b438
+	}
b6b438
+
b6b438
+	ret = gnutls_cipher_init(&ctx, GNUTLS_CIPHER_DES_CBC, &key, &iv;;
b6b438
+	if (ret != 0) {
b6b438
+		return ret;
b6b438
+	}
b6b438
+
b6b438
+	memcpy(outb, in, 8);
b6b438
+	if (encrypt == SAMBA_GNUTLS_ENCRYPT) {
b6b438
+		ret = gnutls_cipher_encrypt(ctx, outb, 8);
b6b438
+	} else {
b6b438
+		ret = gnutls_cipher_decrypt(ctx, outb, 8);
b6b438
+	}
b6b438
+
b6b438
+	if (ret == 0) {
b6b438
+		memcpy(out, outb, 8);
b6b438
+	}
b6b438
+
b6b438
+	gnutls_cipher_deinit(ctx);
b6b438
+
b6b438
+	return ret;
b6b438
+}
b6b438
+
b6b438
 /*
b6b438
   basic des crypt using a 56 bit (7 byte) key
b6b438
 */
b6b438
diff --git a/libcli/auth/tests/test_gnutls.c b/libcli/auth/tests/test_gnutls.c
b6b438
index d9ce8a765cf..121848341e6 100644
b6b438
--- a/libcli/auth/tests/test_gnutls.c
b6b438
+++ b/libcli/auth/tests/test_gnutls.c
b6b438
@@ -242,12 +242,21 @@ static void torture_gnutls_des_crypt56(void **state)
b6b438
 
b6b438
 	uint8_t crypt[8];
b6b438
 	uint8_t decrypt[8];
b6b438
+	int rc;
b6b438
 
b6b438
 	des_crypt56(crypt, clear, key, 1);
b6b438
 	assert_memory_equal(crypt, crypt_expected, 8);
b6b438
 
b6b438
 	des_crypt56(decrypt, crypt, key, 0);
b6b438
 	assert_memory_equal(decrypt, clear, 8);
b6b438
+
b6b438
+	rc = des_crypt56_gnutls(crypt, clear, key, SAMBA_GNUTLS_ENCRYPT);
b6b438
+	assert_int_equal(rc, 0);
b6b438
+	assert_memory_equal(crypt, crypt_expected, 8);
b6b438
+
b6b438
+	rc = des_crypt56_gnutls(decrypt, crypt, key, SAMBA_GNUTLS_DECRYPT);
b6b438
+	assert_int_equal(rc, 0);
b6b438
+	assert_memory_equal(decrypt, clear, 8);
b6b438
 }
b6b438
 
b6b438
 static void torture_gnutls_E_P16(void **state)
b6b438
diff --git a/libcli/auth/wscript_build b/libcli/auth/wscript_build
b6b438
index 8e856d07ddf..0a3de9a1f7b 100644
b6b438
--- a/libcli/auth/wscript_build
b6b438
+++ b/libcli/auth/wscript_build
b6b438
@@ -13,7 +13,7 @@ bld.SAMBA_SUBSYSTEM('MSRPC_PARSE',
b6b438
 
b6b438
 bld.SAMBA_SUBSYSTEM('NTLM_CHECK',
b6b438
                     source='ntlm_check.c',
b6b438
-                    deps = 'talloc'
b6b438
+                    deps = 'talloc LIBCLI_AUTH'
b6b438
                     )
b6b438
 
b6b438
 bld.SAMBA_SUBSYSTEM('LIBCLI_AUTH',
b6b438
diff --git a/source3/passdb/wscript_build b/source3/passdb/wscript_build
b6b438
index faa0cc4b495..7facc1fed79 100644
b6b438
--- a/source3/passdb/wscript_build
b6b438
+++ b/source3/passdb/wscript_build
b6b438
@@ -10,7 +10,7 @@ bld.SAMBA3_MODULE('pdb_tdbsam',
b6b438
 
b6b438
 bld.SAMBA3_MODULE('pdb_ldapsam',
b6b438
                  subsystem='pdb',
b6b438
-                 deps='smbldap smbldaphelper',
b6b438
+                 deps='smbldap smbldaphelper LIBCLI_AUTH',
b6b438
                  source='pdb_ldap.c pdb_nds.c',
b6b438
                  init_function='',
b6b438
                  internal_module=bld.SAMBA3_IS_STATIC_MODULE('pdb_ldapsam'),
b6b438
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
b6b438
index 3dec6ee3f5b..357d9c3a29f 100644
b6b438
--- a/source3/rpc_server/wscript_build
b6b438
+++ b/source3/rpc_server/wscript_build
b6b438
@@ -86,7 +86,8 @@ bld.SAMBA3_SUBSYSTEM('RPC_NETDFS',
b6b438
 
b6b438
 bld.SAMBA3_SUBSYSTEM('RPC_NETLOGON',
b6b438
                      source='''netlogon/srv_netlog_nt.c
b6b438
-                     ../../librpc/gen_ndr/srv_netlogon.c''')
b6b438
+                     ../../librpc/gen_ndr/srv_netlogon.c''',
b6b438
+                    deps='LIBCLI_AUTH')
b6b438
 
b6b438
 bld.SAMBA3_SUBSYSTEM('RPC_NTSVCS',
b6b438
                     source='''ntsvcs/srv_ntsvcs_nt.c
b6b438
-- 
b6b438
2.23.0
b6b438