1524bc
From 01c0b0ac77baae946d270d44d1c40dbdf17a8ee3 Mon Sep 17 00:00:00 2001
1524bc
From: Andreas Schneider <asn@samba.org>
1524bc
Date: Thu, 25 Jul 2019 15:15:46 +1200
1524bc
Subject: [PATCH 024/187] s4:libnet: Use GnuTLS RC4 in
1524bc
 libnet_ChangePassword_samr()
1524bc
1524bc
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14031
1524bc
1524bc
Signed-off-by: Andreas Schneider <asn@samba.org>
1524bc
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
1524bc
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
1524bc
(cherry picked from commit 9ea736590d9b22a7518f86b18e8c55b0d0e213d5)
1524bc
---
1524bc
 source4/libnet/libnet_passwd.c | 110 +++++++++++++++++++++++++++++++--
1524bc
 1 file changed, 104 insertions(+), 6 deletions(-)
1524bc
1524bc
diff --git a/source4/libnet/libnet_passwd.c b/source4/libnet/libnet_passwd.c
1524bc
index dce3813de38..704a94a5864 100644
1524bc
--- a/source4/libnet/libnet_passwd.c
1524bc
+++ b/source4/libnet/libnet_passwd.c
1524bc
@@ -20,7 +20,6 @@
1524bc
 
1524bc
 #include "includes.h"
1524bc
 #include "libnet/libnet.h"
1524bc
-#include "../lib/crypto/crypto.h"
1524bc
 #include "libcli/auth/libcli_auth.h"
1524bc
 #include "librpc/gen_ndr/ndr_samr_c.h"
1524bc
 #include "source4/librpc/rpc/dcerpc.h"
1524bc
@@ -57,6 +56,16 @@ static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CT
1524bc
 	uint8_t old_lm_hash[16], new_lm_hash[16];
1524bc
 	struct samr_DomInfo1 *dominfo = NULL;
1524bc
 	struct userPwdChangeFailureInformation *reject = NULL;
1524bc
+	gnutls_cipher_hd_t cipher_hnd = NULL;
1524bc
+	gnutls_datum_t nt_session_key = {
1524bc
+		.data = old_nt_hash,
1524bc
+		.size = sizeof(old_nt_hash),
1524bc
+	};
1524bc
+	gnutls_datum_t lm_session_key = {
1524bc
+		.data = old_lm_hash,
1524bc
+		.size = sizeof(old_lm_hash),
1524bc
+	};
1524bc
+	int rc;
1524bc
 
1524bc
 	ZERO_STRUCT(c);
1524bc
 
1524bc
@@ -87,11 +96,47 @@ static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CT
1524bc
 
1524bc
 	/* prepare samr_ChangePasswordUser3 */
1524bc
 	encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_UNICODE);
1524bc
-	arcfour_crypt(lm_pass.data, old_nt_hash, 516);
1524bc
+
1524bc
+	rc = gnutls_cipher_init(&cipher_hnd,
1524bc
+				GNUTLS_CIPHER_ARCFOUR_128,
1524bc
+				&nt_session_key,
1524bc
+				NULL);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
+	rc = gnutls_cipher_encrypt(cipher_hnd,
1524bc
+				   lm_pass.data,
1524bc
+				   516);
1524bc
+	gnutls_cipher_deinit(cipher_hnd);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
 	E_old_pw_hash(new_lm_hash, old_lm_hash, lm_verifier.hash);
1524bc
 
1524bc
 	encode_pw_buffer(nt_pass.data,  r->samr.in.newpassword, STR_UNICODE);
1524bc
-	arcfour_crypt(nt_pass.data, old_nt_hash, 516);
1524bc
+
1524bc
+	rc = gnutls_cipher_init(&cipher_hnd,
1524bc
+				GNUTLS_CIPHER_ARCFOUR_128,
1524bc
+				&nt_session_key,
1524bc
+				NULL);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
+	rc = gnutls_cipher_encrypt(cipher_hnd,
1524bc
+				   nt_pass.data,
1524bc
+				   516);
1524bc
+	gnutls_cipher_deinit(cipher_hnd);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
 	E_old_pw_hash(new_nt_hash, old_nt_hash, nt_verifier.hash);
1524bc
 
1524bc
 	pw3.in.server = &server;
1524bc
@@ -125,11 +170,46 @@ static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CT
1524bc
 
1524bc
 	/* prepare samr_ChangePasswordUser2 */
1524bc
 	encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_ASCII|STR_TERMINATE);
1524bc
-	arcfour_crypt(lm_pass.data, old_lm_hash, 516);
1524bc
+
1524bc
+	rc = gnutls_cipher_init(&cipher_hnd,
1524bc
+				GNUTLS_CIPHER_ARCFOUR_128,
1524bc
+				&lm_session_key,
1524bc
+				NULL);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
+	rc = gnutls_cipher_encrypt(cipher_hnd,
1524bc
+				   lm_pass.data,
1524bc
+				   516);
1524bc
+	gnutls_cipher_deinit(cipher_hnd);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
 	E_old_pw_hash(new_lm_hash, old_lm_hash, lm_verifier.hash);
1524bc
 
1524bc
 	encode_pw_buffer(nt_pass.data, r->samr.in.newpassword, STR_UNICODE);
1524bc
-	arcfour_crypt(nt_pass.data, old_nt_hash, 516);
1524bc
+
1524bc
+	rc = gnutls_cipher_init(&cipher_hnd,
1524bc
+				GNUTLS_CIPHER_ARCFOUR_128,
1524bc
+				&nt_session_key,
1524bc
+				NULL);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+	rc = gnutls_cipher_encrypt(cipher_hnd,
1524bc
+				   nt_pass.data,
1524bc
+				   516);
1524bc
+	gnutls_cipher_deinit(cipher_hnd);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
 	E_old_pw_hash(new_nt_hash, old_nt_hash, nt_verifier.hash);
1524bc
 
1524bc
 	pw2.in.server = &server;
1524bc
@@ -161,7 +241,25 @@ static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CT
1524bc
 	a_account.string = r->samr.in.account_name;
1524bc
 
1524bc
 	encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_ASCII);
1524bc
-	arcfour_crypt(lm_pass.data, old_lm_hash, 516);
1524bc
+
1524bc
+	rc = gnutls_cipher_init(&cipher_hnd,
1524bc
+				GNUTLS_CIPHER_ARCFOUR_128,
1524bc
+				&lm_session_key,
1524bc
+				NULL);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
+	rc = gnutls_cipher_encrypt(cipher_hnd,
1524bc
+				   lm_pass.data,
1524bc
+				   516);
1524bc
+	gnutls_cipher_deinit(cipher_hnd);
1524bc
+	if (rc < 0) {
1524bc
+		status = gnutls_error_to_ntstatus(rc, NT_STATUS_CRYPTO_SYSTEM_INVALID);
1524bc
+		goto disconnect;
1524bc
+	}
1524bc
+
1524bc
 	E_old_pw_hash(new_lm_hash, old_lm_hash, lm_verifier.hash);
1524bc
 
1524bc
 	oe2.in.server = &a_server;
1524bc
-- 
1524bc
2.23.0
1524bc