Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / cifs / cifsencrypt.c
index 8fdf3d7..e7d6373 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  *   fs/cifs/cifsencrypt.c
  *
- *   Copyright (C) International Business Machines  Corp., 2003
+ *   Copyright (C) International Business Machines  Corp., 2005,2006
  *   Author(s): Steve French (sfrench@us.ibm.com)
  *
  *   This library is free software; you can redistribute it and/or modify
@@ -25,6 +25,7 @@
 #include "cifs_debug.h"
 #include "md5.h"
 #include "cifs_unicode.h"
+#include "cifsproto.h"
 
 /* Calculate and return the CIFS signature based on the mac key and the smb pdu */
 /* the 16 byte signature must be allocated by the caller  */
@@ -35,7 +36,8 @@
 extern void mdfour(unsigned char *out, unsigned char *in, int n);
 extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
        
-static int cifs_calculate_signature(const struct smb_hdr * cifs_pdu, const char * key, char * signature)
+static int cifs_calculate_signature(const struct smb_hdr * cifs_pdu, 
+                                   const char * key, char * signature)
 {
        struct  MD5Context context;
 
@@ -49,30 +51,27 @@ static int cifs_calculate_signature(const struct smb_hdr * cifs_pdu, const char
        return 0;
 }
 
-int cifs_sign_smb(struct smb_hdr * cifs_pdu, struct cifsSesInfo * ses,
+int cifs_sign_smb(struct smb_hdr * cifs_pdu, struct TCP_Server_Info * server,
        __u32 * pexpected_response_sequence_number)
 {
        int rc = 0;
        char smb_signature[20];
 
-       /* BB remember to initialize sequence number elsewhere and initialize mac_signing key elsewhere BB */
-       /* BB remember to add code to save expected sequence number in midQ entry BB */
-
-       if((cifs_pdu == NULL) || (ses == NULL))
+       if((cifs_pdu == NULL) || (server == NULL))
                return -EINVAL;
 
-       if((le32_to_cpu(cifs_pdu->Flags2) & SMBFLG2_SECURITY_SIGNATURE) == 0) 
+       if((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) 
                return rc;
 
        spin_lock(&GlobalMid_Lock);
-       cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(ses->sequence_number);
+       cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(server->sequence_number);
        cifs_pdu->Signature.Sequence.Reserved = 0;
        
-       *pexpected_response_sequence_number = ses->sequence_number++;
-       ses->sequence_number++;
+       *pexpected_response_sequence_number = server->sequence_number++;
+       server->sequence_number++;
        spin_unlock(&GlobalMid_Lock);
 
-       rc = cifs_calculate_signature(cifs_pdu, ses->mac_signing_key,smb_signature);
+       rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key,smb_signature);
        if(rc)
                memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
        else
@@ -81,6 +80,72 @@ int cifs_sign_smb(struct smb_hdr * cifs_pdu, struct cifsSesInfo * ses,
        return rc;
 }
 
+static int cifs_calc_signature2(const struct kvec * iov, int n_vec,
+                               const char * key, char * signature)
+{
+       struct  MD5Context context;
+       int i;
+
+       if((iov == NULL) || (signature == NULL))
+               return -EINVAL;
+
+       MD5Init(&context);
+       MD5Update(&context,key,CIFS_SESSION_KEY_SIZE+16);
+       for(i=0;i<n_vec;i++) {
+               if(iov[i].iov_base == NULL) {
+                       cERROR(1,("null iovec entry"));
+                       return -EIO;
+               } else if(iov[i].iov_len == 0)
+                       break; /* bail out if we are sent nothing to sign */
+               /* The first entry includes a length field (which does not get 
+                  signed that occupies the first 4 bytes before the header */
+               if(i==0) {
+                       if (iov[0].iov_len <= 8 ) /* cmd field at offset 9 */
+                               break; /* nothing to sign or corrupt header */
+                       MD5Update(&context,iov[0].iov_base+4, iov[0].iov_len-4);
+               } else
+                       MD5Update(&context,iov[i].iov_base, iov[i].iov_len);
+       }
+
+       MD5Final(signature,&context);
+
+       return 0;
+}
+
+
+int cifs_sign_smb2(struct kvec * iov, int n_vec, struct TCP_Server_Info *server,
+                  __u32 * pexpected_response_sequence_number)
+{
+       int rc = 0;
+       char smb_signature[20];
+       struct smb_hdr * cifs_pdu = iov[0].iov_base;
+
+       if((cifs_pdu == NULL) || (server == NULL))
+               return -EINVAL;
+
+       if((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
+               return rc;
+
+        spin_lock(&GlobalMid_Lock);
+        cifs_pdu->Signature.Sequence.SequenceNumber = 
+                               cpu_to_le32(server->sequence_number);
+        cifs_pdu->Signature.Sequence.Reserved = 0;
+
+        *pexpected_response_sequence_number = server->sequence_number++;
+        server->sequence_number++;
+        spin_unlock(&GlobalMid_Lock);
+
+        rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key,
+                                     smb_signature);
+        if(rc)
+                memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
+        else
+                memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
+
+        return rc;
+
+}
+
 int cifs_verify_signature(struct smb_hdr * cifs_pdu, const char * mac_key,
        __u32 expected_sequence_number)
 {
@@ -107,13 +172,11 @@ int cifs_verify_signature(struct smb_hdr * cifs_pdu, const char * mac_key,
        if(memcmp(cifs_pdu->Signature.SecuritySignature,"BSRSPYL ",8)==0)
                cFYI(1,("dummy signature received for smb command 0x%x",cifs_pdu->Command));
 
-       expected_sequence_number = cpu_to_le32(expected_sequence_number);
-
        /* save off the origiginal signature so we can modify the smb and check
                its signature against what the server sent */
        memcpy(server_response_sig,cifs_pdu->Signature.SecuritySignature,8);
 
-       cifs_pdu->Signature.Sequence.SequenceNumber = expected_sequence_number;
+       cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(expected_sequence_number);
        cifs_pdu->Signature.Sequence.Reserved = 0;
 
        rc = cifs_calculate_signature(cifs_pdu, mac_key,
@@ -150,10 +213,10 @@ int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, struct nls_table * nls_
        char temp_hash[16];
        struct HMACMD5Context ctx;
        char * ucase_buf;
-       wchar_t * unicode_buf;
+       __le16 * unicode_buf;
        unsigned int i,user_name_len,dom_name_len;
 
-       if(ses)
+       if(ses == NULL)
                return -EINVAL;
 
        E_md4hash(ses->password, temp_hash);
@@ -167,7 +230,13 @@ int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, struct nls_table * nls_
                return -EINVAL;
   
        ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
+       if(ucase_buf == NULL)
+               return -ENOMEM;
        unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
+       if(unicode_buf == NULL) {
+               kfree(ucase_buf);
+               return -ENOMEM;
+       }
    
        for(i=0;i<user_name_len;i++)
                ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
@@ -185,7 +254,7 @@ int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, struct nls_table * nls_
        hmac_md5_update((const unsigned char *) unicode_buf,
                (user_name_len+dom_name_len)*2,&ctx);
 
-       hmac_md5_final(ses->mac_signing_key,&ctx);
+       hmac_md5_final(ses->server->mac_signing_key,&ctx);
        kfree(ucase_buf);
        kfree(unicode_buf);
        return 0;
@@ -195,10 +264,11 @@ void CalcNTLMv2_response(const struct cifsSesInfo * ses,char * v2_session_respon
        struct HMACMD5Context context;
        memcpy(v2_session_response + 8, ses->server->cryptKey,8);
        /* gen_blob(v2_session_response + 16); */
-       hmac_md5_init_limK_to_64(ses->mac_signing_key, 16, &context);
+       hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context);
 
        hmac_md5_update(ses->server->cryptKey,8,&context);
 /*     hmac_md5_update(v2_session_response+16)client thing,8,&context); */ /* BB fix */
 
        hmac_md5_final(v2_session_response,&context);
+       cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); /* BB removeme BB */
 }