Skip to contentSkip to conversation

ntlmv2/ntlmssp generate secondary session key and ciphertext and send it if signing enabled

PATCH In review [email protected] 1 participant 2 messages Sep 8, 2010 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 7/8 ntlmv2/ntlmssp generate secondary session key and ciphertext and send it if signing enabled

From: Shirish Pargaonkar <[email protected]>

A key is exchanged with the server if client indicates so in flags in type 1 messsage and server agrees in flag in type 2 message of ntlmssp negotiation. If both client and agree, a key sent by client in type 3 message of ntlmssp negotiation in the session key field. The key is a ciphertext generated off of secondary key, a nonce, using ntlmv2 hash via rc4/arc4.

If authentication is successful, the secondary key is the that client uses to calculate cifs/smb signature in the messages that client sends and to verify the messages sent by server.
This key stays with the smb connection for its life and is the key used to generate (and verify) signatures for all the subsequent smb sessions for this smb connection.

So only for the first smb session on any smb connection, type 1 message of ntlmssp negotiation selects the flag NTLMSSP_NEGOTIATE_KEY_XCH. It is not used for subsequent smb sessions on this smb connection i.e. no need to generate secondary key, create ciphertext and send it etc.

After the very first successful smb session, sequence number gets set to 0 and variable cphready is used to mark that the key calculations are done.

---
 fs/cifs/cifsencrypt.c |   41 +++++++++++++++++++++++++++++++++++++++++
 fs/cifs/cifsproto.h   |    1 +
 fs/cifs/connect.c     |    1 +
 fs/cifs/sess.c        |   24 ++++++++++++++++++------
 4 files changed, 61 insertions(+), 6 deletions(-)
fs/cifs/cifsencrypt.c +41 -0
index f9c140a..ff576a8 100644
@@ -470,6 +470,47 @@ void CalcNTLMv2_response(const struct cifsSesInfo *ses,
 /*	cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
 }
 
+int
+calc_seckey(struct TCP_Server_Info *server)
+{
+	int rc;
+	struct crypto_blkcipher *tfm_arc4;
+	struct scatterlist sgin, sgout;
+	struct blkcipher_desc desc;
+
+	if (!server) {
+		cERROR(1, "%s: Can't determine ciphertext key\n", __func__);
+		return 1;
+	}
+
+	if (server->cphready)
+		return 0;
+
+	get_random_bytes(server->ntlmssp.sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
+
+	tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)",
+						0, CRYPTO_ALG_ASYNC);
+	if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
+		cERROR(1, "could not allocate crypto API arc4\n");
+		return 1;
+	}
+
+	desc.tfm = tfm_arc4;
+
+	crypto_blkcipher_setkey(tfm_arc4,
+		server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE);
+	sg_init_one(&sgin, server->ntlmssp.sec_key, CIFS_CPHTXT_SIZE);
+	sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
+	rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
+
+	crypto_free_blkcipher(tfm_arc4);
+
+	server->sequence_number = 0;
+	server->cphready = true;
+
+	return 0;
+}
+
 void
 cifs_crypto_shash_release(struct TCP_Server_Info *server)
 {
fs/cifs/cifsproto.h +1 -0
index fbfdd8e..b78bb77 100644
@@ -370,6 +370,7 @@ extern int setup_ntlmv2_rsp(struct cifsSesInfo *, char *,
 			     const struct nls_table *);
 extern int cifs_crypto_shash_allocate(struct TCP_Server_Info *);
 extern void cifs_crypto_shash_release(struct TCP_Server_Info *);
+extern int calc_seckey(struct TCP_Server_Info *);
 #ifdef CONFIG_CIFS_WEAK_PW_HASH
 extern void calc_lanman_hash(const char *password, const char *cryptkey,
 				bool encrypt, char *lnm_session_key);
fs/cifs/connect.c +1 -0
index 0621a72..e3883f3 100644
@@ -1587,6 +1587,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
 		goto out_err2;
 	}
 
+	tcp_ses->cphready = false;
 	tcp_ses->noblocksnd = volume_info->noblocksnd;
 	tcp_ses->noautotune = volume_info->noautotune;
 	tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
fs/cifs/sess.c +18 -6
index 68ea153..7c94d7b 100644
@@ -444,10 +444,12 @@ static void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
 		NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
 		NTLMSSP_NEGOTIATE_NTLM;
 	if (ses->server->secMode &
-	   (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
+			(SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
 		flags |= NTLMSSP_NEGOTIATE_SIGN;
-	if (ses->server->secMode & SECMODE_SIGN_REQUIRED)
-		flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
+		if (!ses->server->cphready)
+			flags |= NTLMSSP_NEGOTIATE_KEY_XCH |
+				NTLMSSP_NEGOTIATE_EXTENDED_SEC;
+	}
 
 	sec_blob->NegotiateFlags |= cpu_to_le32(flags);
 
@@ -554,9 +556,19 @@ static int build_ntlmssp_auth_blob(unsigned char *pbuffer,
 	sec_blob->WorkstationName.MaximumLength = 0;
 	tmp += 2;
 
-	sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
-	sec_blob->SessionKey.Length = 0;
-	sec_blob->SessionKey.MaximumLength = 0;
+	if ((ses->server->ntlmssp.server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
+			!calc_seckey(ses->server)) {
+		memcpy(tmp, ses->server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
+		sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
+		sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
+		sec_blob->SessionKey.MaximumLength =
+			cpu_to_le16(CIFS_CPHTXT_SIZE);
+		tmp += CIFS_CPHTXT_SIZE;
+	} else {
+		sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
+		sec_blob->SessionKey.Length = 0;
+		sec_blob->SessionKey.MaximumLength = 0;
+	}
 
 setup_ntlmv2_ret:
 	return tmp - pbuffer;
Signature · 1 line
1.6.0.2

A message that is not in the archive (sent off-list or not yet received): 1283921280-13163-1-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw
Quoted 152 lines · On Tue, Sep 7, 2010 at 11:48 PM, <shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

A key is exchanged with the server if client indicates so in flags in type 1 messsage and server agrees in flag in type 2 message of ntlmssp negotiation.  If both client and agree, a key sent by client in type 3 message of ntlmssp negotiation in the session key field. The key is a ciphertext generated off of secondary key, a nonce, using ntlmv2 hash via rc4/arc4.

If authentication is successful, the secondary key is the that client uses to calculate cifs/smb signature in the messages that client sends and to verify the messages sent by server.
This key stays with the smb connection for its life and is the key used to generate (and verify) signatures for all the subsequent smb sessions for this smb connection.

So only for the first smb session on any smb connection, type 1 message of ntlmssp negotiation selects the flag NTLMSSP_NEGOTIATE_KEY_XCH. It is not used for subsequent smb sessions on this smb connection i.e. no need to generate secondary key, create ciphertext and send it etc.

After the very first successful smb session, sequence number gets set to 0 and  variable cphready is used to mark that the key calculations are done.

---
 fs/cifs/cifsencrypt.c |   41 +++++++++++++++++++++++++++++++++++++++++ fs/cifs/cifsproto.h   |    1 +
 fs/cifs/connect.c     |    1 +
 fs/cifs/sess.c        |   24 ++++++++++++++++++------

 4 files changed, 61 insertions(+), 6 deletions(-)
fs/cifs/cifsencrypt.c +41 -0
index f9c140a..ff576a8 100644
@@ -470,6 +470,47 @@ void CalcNTLMv2_response(const struct cifsSesInfo *ses,
  /*     cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
  }
 
+int
+calc_seckey(struct TCP_Server_Info *server)
+{
+       int rc;
+       struct crypto_blkcipher *tfm_arc4;
+       struct scatterlist sgin, sgout;
+       struct blkcipher_desc desc;
+
+       if (!server) {
+               cERROR(1, "%s: Can't determine ciphertext key\n", __func__);
+               return 1;
+       }
+
+       if (server->cphready)
+               return 0;
+
+       get_random_bytes(server->ntlmssp.sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
+
+       tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)",
+                                               0, CRYPTO_ALG_ASYNC);
+       if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
+               cERROR(1, "could not allocate crypto API arc4\n");
+               return 1;
+       }
+
+       desc.tfm = tfm_arc4;
+
+       crypto_blkcipher_setkey(tfm_arc4,
+               server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE);
+       sg_init_one(&sgin, server->ntlmssp.sec_key, CIFS_CPHTXT_SIZE);
+       sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
+       rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
+
+       crypto_free_blkcipher(tfm_arc4);
+
+       server->sequence_number = 0;
+       server->cphready = true;
+
+       return 0;
+}
+

 void
 cifs_crypto_shash_release(struct TCP_Server_Info *server) {

fs/cifs/cifsproto.h +1 -0
index fbfdd8e..b78bb77 100644
@@ -370,6 +370,7 @@ extern int setup_ntlmv2_rsp(struct cifsSesInfo *, char *,
                             const struct nls_table *);
  extern int cifs_crypto_shash_allocate(struct TCP_Server_Info *);
  extern void cifs_crypto_shash_release(struct TCP_Server_Info *);
+extern int calc_seckey(struct TCP_Server_Info *);
  #ifdef CONFIG_CIFS_WEAK_PW_HASH
  extern void calc_lanman_hash(const char *password, const char *cryptkey,
                                bool encrypt, char *lnm_session_key);
fs/cifs/connect.c +1 -0
index 0621a72..e3883f3 100644
@@ -1587,6 +1587,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
                goto out_err2;
        }
 
+       tcp_ses->cphready = false;
        tcp_ses->noblocksnd = volume_info->noblocksnd;
        tcp_ses->noautotune = volume_info->noautotune;
        tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
fs/cifs/sess.c +0 -0
index 68ea153..7c94d7b 100644
@@ -444,10 +444,12 @@ static void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,

               NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_NTLM;
       if (ses->server->secMode &
-          (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
+                       (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) { flags |= NTLMSSP_NEGOTIATE_SIGN;
-       if (ses->server->secMode & SECMODE_SIGN_REQUIRED)
-               flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
+               if (!ses->server->cphready)
+                       flags |= NTLMSSP_NEGOTIATE_KEY_XCH |
+                               NTLMSSP_NEGOTIATE_EXTENDED_SEC;
+       }

       sec_blob->NegotiateFlags |= cpu_to_le32(flags);

fs/cifs/sess.c +13 -3
@@ -554,9 +556,19 @@ static int build_ntlmssp_auth_blob(unsigned char *pbuffer,
        sec_blob->WorkstationName.MaximumLength = 0;
        tmp += 2;
 
-       sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
-       sec_blob->SessionKey.Length = 0;
-       sec_blob->SessionKey.MaximumLength = 0;
+       if ((ses->server->ntlmssp.server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
+                       !calc_seckey(ses->server)) {
+               memcpy(tmp, ses->server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
+               sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
+               sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
+               sec_blob->SessionKey.MaximumLength =
+                       cpu_to_le16(CIFS_CPHTXT_SIZE);
+               tmp += CIFS_CPHTXT_SIZE;
+       } else {
+               sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - pbuffer);
+               sec_blob->SessionKey.Length = 0;
+               sec_blob->SessionKey.MaximumLength = 0;
+       }
 
Quoted 6 lines

 setup_ntlmv2_ret:
       return tmp - pbuffer;
--
1.6.0.2

I probably should do the code within calc_seckey within srv_mutex to prevent race of
smb sessions within a smb connection.