1524bc
From fcbef176770dc8531ab9eb8bb091b44b3923f405 Mon Sep 17 00:00:00 2001
1524bc
From: Andreas Schneider <asn@samba.org>
1524bc
Date: Thu, 14 Mar 2019 10:53:23 +0100
1524bc
Subject: [PATCH 126/187] libcli:smb: Use smb2_signing_key in
1524bc
 smb2_signing_decrypt_pdu()
1524bc
1524bc
Signed-off-by: Andreas Schneider <asn@samba.org>
1524bc
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
1524bc
1524bc
Adaped to remove Samba AES support
1524bc
1524bc
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
1524bc
(cherry picked from commit 7f56e91dbe404bc1ee40e4843c4046336945b057)
1524bc
---
1524bc
 libcli/smb/smb2_signing.c  | 34 +++++++++++++++-------------------
1524bc
 libcli/smb/smb2_signing.h  |  2 +-
1524bc
 libcli/smb/smbXcli_base.c  |  2 +-
1524bc
 source3/smbd/smb2_server.c |  2 +-
1524bc
 4 files changed, 18 insertions(+), 22 deletions(-)
1524bc
1524bc
diff --git a/libcli/smb/smb2_signing.c b/libcli/smb/smb2_signing.c
1524bc
index 1d9c99337d8..9f40e8bbea5 100644
1524bc
--- a/libcli/smb/smb2_signing.c
1524bc
+++ b/libcli/smb/smb2_signing.c
1524bc
@@ -558,7 +558,7 @@ out:
1524bc
 	return status;
1524bc
 }
1524bc
 
1524bc
-NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
+NTSTATUS smb2_signing_decrypt_pdu(struct smb2_signing_key *decryption_key,
1524bc
 				  uint16_t cipher_id,
1524bc
 				  struct iovec *vector,
1524bc
 				  int count)
1524bc
@@ -574,7 +574,6 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 	uint32_t tag_size = 0;
1524bc
 	uint8_t _key[16] = {0};
1524bc
 	gnutls_cipher_algorithm_t algo = 0;
1524bc
-	gnutls_aead_cipher_hd_t cipher_hnd = NULL;
1524bc
 	gnutls_datum_t key;
1524bc
 	gnutls_datum_t iv;
1524bc
 	NTSTATUS status;
1524bc
@@ -590,9 +589,9 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 
1524bc
 	tf = (uint8_t *)vector[0].iov_base;
1524bc
 
1524bc
-	if (decryption_key.length == 0) {
1524bc
-		DEBUG(2,("Wrong decryption key length %u for SMB2 signing\n",
1524bc
-			 (unsigned)decryption_key.length));
1524bc
+	if (!smb2_signing_key_valid(decryption_key)) {
1524bc
+		DBG_WARNING("Wrong decryption key length %zu for SMB2 signing\n",
1524bc
+			    decryption_key->blob.length);
1524bc
 		return NT_STATUS_ACCESS_DENIED;
1524bc
 	}
1524bc
 
1524bc
@@ -640,20 +639,22 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 	};
1524bc
 
1524bc
 	memcpy(key.data,
1524bc
-	       decryption_key.data,
1524bc
-	       MIN(decryption_key.length, key.size));
1524bc
+	       decryption_key->blob.data,
1524bc
+	       MIN(decryption_key->blob.length, key.size));
1524bc
 
1524bc
 	iv = (gnutls_datum_t) {
1524bc
 		.data = tf + SMB2_TF_NONCE,
1524bc
 		.size = iv_size,
1524bc
 	};
1524bc
 
1524bc
-	rc = gnutls_aead_cipher_init(&cipher_hnd,
1524bc
-				     algo,
1524bc
-				     &key);
1524bc
-	if (rc < 0) {
1524bc
-		status = NT_STATUS_NO_MEMORY;
1524bc
-		goto out;
1524bc
+	if (decryption_key->cipher_hnd == NULL) {
1524bc
+		rc = gnutls_aead_cipher_init(&decryption_key->cipher_hnd,
1524bc
+					     algo,
1524bc
+					     &key);
1524bc
+		if (rc < 0) {
1524bc
+			status = NT_STATUS_NO_MEMORY;
1524bc
+			goto out;
1524bc
+		}
1524bc
 	}
1524bc
 
1524bc
 	{
1524bc
@@ -667,7 +668,6 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 
1524bc
 		ptext = talloc_size(talloc_tos(), ptext_size);
1524bc
 		if (ptext == NULL) {
1524bc
-			gnutls_aead_cipher_deinit(cipher_hnd);
1524bc
 			status = NT_STATUS_NO_MEMORY;
1524bc
 			goto out;
1524bc
 		}
1524bc
@@ -675,7 +675,6 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 		ctext = talloc_size(talloc_tos(), ctext_size);
1524bc
 		if (ctext == NULL) {
1524bc
 			TALLOC_FREE(ptext);
1524bc
-			gnutls_aead_cipher_deinit(cipher_hnd);
1524bc
 			status = NT_STATUS_NO_MEMORY;
1524bc
 			goto out;
1524bc
 		}
1524bc
@@ -691,7 +690,6 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 		if (len != m_total) {
1524bc
 			TALLOC_FREE(ptext);
1524bc
 			TALLOC_FREE(ctext);
1524bc
-			gnutls_aead_cipher_deinit(cipher_hnd);
1524bc
 			status = NT_STATUS_INTERNAL_ERROR;
1524bc
 			goto out;
1524bc
 		}
1524bc
@@ -701,7 +699,7 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 		       tag_size);
1524bc
 
1524bc
 		/* This function will verify the tag */
1524bc
-		rc = gnutls_aead_cipher_decrypt(cipher_hnd,
1524bc
+		rc = gnutls_aead_cipher_decrypt(decryption_key->cipher_hnd,
1524bc
 						iv.data,
1524bc
 						iv.size,
1524bc
 						tf + SMB2_TF_NONCE,
1524bc
@@ -715,7 +713,6 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 			DBG_ERR("ERROR: %s\n", gnutls_strerror(rc));
1524bc
 			TALLOC_FREE(ptext);
1524bc
 			TALLOC_FREE(ctext);
1524bc
-			gnutls_aead_cipher_deinit(cipher_hnd);
1524bc
 			status = NT_STATUS_INTERNAL_ERROR;
1524bc
 			goto out;
1524bc
 		}
1524bc
@@ -732,7 +729,6 @@ NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
 		TALLOC_FREE(ptext);
1524bc
 		TALLOC_FREE(ctext);
1524bc
 	}
1524bc
-	gnutls_aead_cipher_deinit(cipher_hnd);
1524bc
 
1524bc
 	DBG_INFO("Decrypted SMB2 message\n");
1524bc
 
1524bc
diff --git a/libcli/smb/smb2_signing.h b/libcli/smb/smb2_signing.h
1524bc
index 13fb54e4e4e..7eefad93b3e 100644
1524bc
--- a/libcli/smb/smb2_signing.h
1524bc
+++ b/libcli/smb/smb2_signing.h
1524bc
@@ -57,7 +57,7 @@ NTSTATUS smb2_signing_encrypt_pdu(DATA_BLOB encryption_key,
1524bc
 				  uint16_t cipher_id,
1524bc
 				  struct iovec *vector,
1524bc
 				  int count);
1524bc
-NTSTATUS smb2_signing_decrypt_pdu(DATA_BLOB decryption_key,
1524bc
+NTSTATUS smb2_signing_decrypt_pdu(struct smb2_signing_key *decryption_key,
1524bc
 				  uint16_t cipher_id,
1524bc
 				  struct iovec *vector,
1524bc
 				  int count);
1524bc
diff --git a/libcli/smb/smbXcli_base.c b/libcli/smb/smbXcli_base.c
1524bc
index aa69c374d49..421fc434305 100644
1524bc
--- a/libcli/smb/smbXcli_base.c
1524bc
+++ b/libcli/smb/smbXcli_base.c
1524bc
@@ -3567,7 +3567,7 @@ static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
1524bc
 			tf_iov[1].iov_base = (void *)hdr;
1524bc
 			tf_iov[1].iov_len = enc_len;
1524bc
 
1524bc
-			status = smb2_signing_decrypt_pdu(s->smb2->decryption_key->blob,
1524bc
+			status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
1524bc
 							  conn->smb2.server.cipher,
1524bc
 							  tf_iov, 2);
1524bc
 			if (!NT_STATUS_IS_OK(status)) {
1524bc
diff --git a/source3/smbd/smb2_server.c b/source3/smbd/smb2_server.c
1524bc
index 56e7b70696b..9df22b5a6ac 100644
1524bc
--- a/source3/smbd/smb2_server.c
1524bc
+++ b/source3/smbd/smb2_server.c
1524bc
@@ -432,7 +432,7 @@ static NTSTATUS smbd_smb2_inbuf_parse_compound(struct smbXsrv_connection *xconn,
1524bc
 			tf_iov[1].iov_base = (void *)hdr;
1524bc
 			tf_iov[1].iov_len = enc_len;
1524bc
 
1524bc
-			status = smb2_signing_decrypt_pdu(s->global->decryption_key->blob,
1524bc
+			status = smb2_signing_decrypt_pdu(s->global->decryption_key,
1524bc
 							  xconn->smb2.server.cipher,
1524bc
 							  tf_iov, 2);
1524bc
 			if (!NT_STATUS_IS_OK(status)) {
1524bc
-- 
1524bc
2.23.0
1524bc