Commit b609f06a authored by Steve French's avatar Steve French Committed by Steve French

[CIFS] Fix packet signatures for NTLMv2 case

Signed-off-by: default avatarYehuda Sadeh Weinraub <Yehuda.Sadeh@expand.com>
Signed-off-by: default avatarSteve French <sfrench@us.ibm.com>
parent 3870253e
Version 1.50
------------
Fix NTLMv2 signing
Version 1.49 Version 1.49
------------ ------------
IPv6 support. Enable ipv6 addresses to be passed on mount (put the ipv6 IPv6 support. Enable ipv6 addresses to be passed on mount (put the ipv6
......
...@@ -18,9 +18,9 @@ better) ...@@ -18,9 +18,9 @@ better)
d) Kerberos/SPNEGO session setup support - (started) d) Kerberos/SPNEGO session setup support - (started)
e) More testing of NTLMv2 authentication (mostly implemented - double check e) Cleanup now unneeded SessSetup code in
that NTLMv2 signing works, also need to cleanup now unneeded SessSetup code in fs/cifs/connect.c and add back in NTLMSSP code if any servers
fs/cifs/connect.c) need it
f) MD5-HMAC signing SMB PDUs when SPNEGO style SessionSetup f) MD5-HMAC signing SMB PDUs when SPNEGO style SessionSetup
used (Kerberos or NTLMSSP). Signing alreadyimplemented for NTLM used (Kerberos or NTLMSSP). Signing alreadyimplemented for NTLM
......
...@@ -41,16 +41,17 @@ extern void SMBencrypt(unsigned char *passwd, unsigned char *c8, ...@@ -41,16 +41,17 @@ extern void SMBencrypt(unsigned char *passwd, unsigned char *c8,
unsigned char *p24); unsigned char *p24);
static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu, static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
const char *key, char *signature) const struct mac_key *key, char *signature)
{ {
struct MD5Context context; struct MD5Context context;
if ((cifs_pdu == NULL) || (signature == NULL)) if ((cifs_pdu == NULL) || (signature == NULL) || (key == NULL))
return -EINVAL; return -EINVAL;
MD5Init(&context); MD5Init(&context);
MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16); MD5Update(&context, (char *)&key->data, key->len);
MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length); MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
MD5Final(signature, &context); MD5Final(signature, &context);
return 0; return 0;
} }
...@@ -76,7 +77,7 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, ...@@ -76,7 +77,7 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
server->sequence_number++; server->sequence_number++;
spin_unlock(&GlobalMid_Lock); spin_unlock(&GlobalMid_Lock);
rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key, rc = cifs_calculate_signature(cifs_pdu, &server->mac_signing_key,
smb_signature); smb_signature);
if (rc) if (rc)
memset(cifs_pdu->Signature.SecuritySignature, 0, 8); memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
...@@ -87,16 +88,16 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, ...@@ -87,16 +88,16 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
} }
static int cifs_calc_signature2(const struct kvec *iov, int n_vec, static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
const char *key, char *signature) const struct mac_key *key, char *signature)
{ {
struct MD5Context context; struct MD5Context context;
int i; int i;
if ((iov == NULL) || (signature == NULL)) if ((iov == NULL) || (signature == NULL) || (key == NULL))
return -EINVAL; return -EINVAL;
MD5Init(&context); MD5Init(&context);
MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16); MD5Update(&context, (char *)&key->data, key->len);
for (i=0;i<n_vec;i++) { for (i=0;i<n_vec;i++) {
if (iov[i].iov_base == NULL) { if (iov[i].iov_base == NULL) {
cERROR(1 ,("null iovec entry")); cERROR(1 ,("null iovec entry"));
...@@ -141,7 +142,7 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, ...@@ -141,7 +142,7 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
server->sequence_number++; server->sequence_number++;
spin_unlock(&GlobalMid_Lock); spin_unlock(&GlobalMid_Lock);
rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key, rc = cifs_calc_signature2(iov, n_vec, &server->mac_signing_key,
smb_signature); smb_signature);
if (rc) if (rc)
memset(cifs_pdu->Signature.SecuritySignature, 0, 8); memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
...@@ -151,7 +152,8 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, ...@@ -151,7 +152,8 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
return rc; return rc;
} }
int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key, int cifs_verify_signature(struct smb_hdr *cifs_pdu,
const struct mac_key *mac_key,
__u32 expected_sequence_number) __u32 expected_sequence_number)
{ {
unsigned int rc; unsigned int rc;
...@@ -202,15 +204,17 @@ int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key, ...@@ -202,15 +204,17 @@ int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key,
} }
/* We fill in key by putting in 40 byte array which was allocated by caller */ /* We fill in key by putting in 40 byte array which was allocated by caller */
int cifs_calculate_mac_key(char * key, const char * rn, const char * password) int cifs_calculate_mac_key(struct mac_key *key, const char *rn,
const char *password)
{ {
char temp_key[16]; char temp_key[16];
if ((key == NULL) || (rn == NULL)) if ((key == NULL) || (rn == NULL))
return -EINVAL; return -EINVAL;
E_md4hash(password, temp_key); E_md4hash(password, temp_key);
mdfour(key,temp_key,16); mdfour(key->data.ntlm, temp_key, 16);
memcpy(key+16,rn, CIFS_SESS_KEY_SIZE); memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
key->len = 40;
return 0; return 0;
} }
...@@ -235,35 +239,35 @@ int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, ...@@ -235,35 +239,35 @@ int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses,
if(ses->domainName == NULL) if(ses->domainName == NULL)
return -EINVAL; /* BB should we use CIFS_LINUX_DOM */ return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
dom_name_len = strlen(ses->domainName); dom_name_len = strlen(ses->domainName);
if(dom_name_len > MAX_USERNAME_SIZE) if (dom_name_len > MAX_USERNAME_SIZE)
return -EINVAL; return -EINVAL;
ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL); ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
if(ucase_buf == NULL) if (ucase_buf == NULL)
return -ENOMEM; return -ENOMEM;
unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL); unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
if(unicode_buf == NULL) { if (unicode_buf == NULL) {
kfree(ucase_buf); kfree(ucase_buf);
return -ENOMEM; return -ENOMEM;
} }
for(i=0;i<user_name_len;i++) for (i = 0;i < user_name_len; i++)
ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]]; ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
ucase_buf[i] = 0; ucase_buf[i] = 0;
user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info); user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
unicode_buf[user_name_len] = 0; unicode_buf[user_name_len] = 0;
user_name_len++; user_name_len++;
for(i=0;i<dom_name_len;i++) for (i = 0; i < dom_name_len; i++)
ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]]; ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]];
ucase_buf[i] = 0; ucase_buf[i] = 0;
dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info); dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
unicode_buf[user_name_len + dom_name_len] = 0; unicode_buf[user_name_len + dom_name_len] = 0;
hmac_md5_update((const unsigned char *) unicode_buf, hmac_md5_update((const unsigned char *) unicode_buf,
(user_name_len+dom_name_len)*2,&ctx); (user_name_len+dom_name_len)*2, &ctx);
hmac_md5_final(ses->server->mac_signing_key,&ctx); hmac_md5_final(ses->server->ntlmv2_hash, &ctx);
kfree(ucase_buf); kfree(ucase_buf);
kfree(unicode_buf); kfree(unicode_buf);
return 0; return 0;
...@@ -347,7 +351,10 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses, ...@@ -347,7 +351,10 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
if(domain == NULL) if(domain == NULL)
goto calc_exit_1; goto calc_exit_1;
len = cifs_strtoUCS(domain, ses->domainName, len, nls_cp); len = cifs_strtoUCS(domain, ses->domainName, len, nls_cp);
UniStrupr(domain); /* the following line was removed since it didn't work well
with lower cased domain name that passed as an option.
Maybe converting the domain name earlier makes sense */
/* UniStrupr(domain); */
hmac_md5_update((char *)domain, 2*len, pctxt); hmac_md5_update((char *)domain, 2*len, pctxt);
...@@ -358,7 +365,7 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses, ...@@ -358,7 +365,7 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
calc_exit_2: calc_exit_2:
/* BB FIXME what about bytes 24 through 40 of the signing key? /* BB FIXME what about bytes 24 through 40 of the signing key?
compare with the NTLM example */ compare with the NTLM example */
hmac_md5_final(ses->server->mac_signing_key, pctxt); hmac_md5_final(ses->server->ntlmv2_hash, pctxt);
return rc; return rc;
} }
...@@ -368,6 +375,7 @@ void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf, ...@@ -368,6 +375,7 @@ void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf,
{ {
int rc; int rc;
struct ntlmv2_resp * buf = (struct ntlmv2_resp *)resp_buf; struct ntlmv2_resp * buf = (struct ntlmv2_resp *)resp_buf;
struct HMACMD5Context context;
buf->blob_signature = cpu_to_le32(0x00000101); buf->blob_signature = cpu_to_le32(0x00000101);
buf->reserved = 0; buf->reserved = 0;
...@@ -384,6 +392,15 @@ void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf, ...@@ -384,6 +392,15 @@ void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf,
if(rc) if(rc)
cERROR(1,("could not get v2 hash rc %d",rc)); cERROR(1,("could not get v2 hash rc %d",rc));
CalcNTLMv2_response(ses, resp_buf); CalcNTLMv2_response(ses, resp_buf);
/* now calculate the MAC key for NTLMv2 */
hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
hmac_md5_update(resp_buf, 16, &context);
hmac_md5_final(ses->server->mac_signing_key.data.ntlmv2.key, &context);
memcpy(&ses->server->mac_signing_key.data.ntlmv2.resp, resp_buf,
sizeof(struct ntlmv2_resp));
ses->server->mac_signing_key.len = 16 + sizeof(struct ntlmv2_resp);
} }
void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response) void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response)
...@@ -391,7 +408,7 @@ void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_respo ...@@ -391,7 +408,7 @@ void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_respo
struct HMACMD5Context context; struct HMACMD5Context context;
/* rest of v2 struct already generated */ /* rest of v2 struct already generated */
memcpy(v2_session_response + 8, ses->server->cryptKey,8); memcpy(v2_session_response + 8, ses->server->cryptKey,8);
hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context); hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
hmac_md5_update(v2_session_response+8, hmac_md5_update(v2_session_response+8,
sizeof(struct ntlmv2_resp) - 8, &context); sizeof(struct ntlmv2_resp) - 8, &context);
......
...@@ -104,6 +104,17 @@ enum protocolEnum { ...@@ -104,6 +104,17 @@ enum protocolEnum {
/* Netbios frames protocol not supported at this time */ /* Netbios frames protocol not supported at this time */
}; };
struct mac_key {
unsigned int len;
union {
char ntlm[CIFS_SESS_KEY_SIZE + 16];
struct {
char key[16];
struct ntlmv2_resp resp;
} ntlmv2;
} data;
};
/* /*
***************************************************************** *****************************************************************
* Except the CIFS PDUs themselves all the * Except the CIFS PDUs themselves all the
...@@ -159,7 +170,8 @@ struct TCP_Server_Info { ...@@ -159,7 +170,8 @@ struct TCP_Server_Info {
/* 16th byte of RFC1001 workstation name is always null */ /* 16th byte of RFC1001 workstation name is always null */
char workstation_RFC1001_name[SERVER_NAME_LEN_WITH_NULL]; char workstation_RFC1001_name[SERVER_NAME_LEN_WITH_NULL];
__u32 sequence_number; /* needed for CIFS PDU signature */ __u32 sequence_number; /* needed for CIFS PDU signature */
char mac_signing_key[CIFS_SESS_KEY_SIZE + 16]; struct mac_key mac_signing_key;
char ntlmv2_hash[16];
unsigned long lstrp; /* when we got last response from this server */ unsigned long lstrp; /* when we got last response from this server */
}; };
......
...@@ -294,9 +294,11 @@ extern void tconInfoFree(struct cifsTconInfo *); ...@@ -294,9 +294,11 @@ extern void tconInfoFree(struct cifsTconInfo *);
extern int cifs_sign_smb(struct smb_hdr *, struct TCP_Server_Info *, __u32 *); extern int cifs_sign_smb(struct smb_hdr *, struct TCP_Server_Info *, __u32 *);
extern int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *, extern int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *,
__u32 *); __u32 *);
extern int cifs_verify_signature(struct smb_hdr *, const char *mac_key, extern int cifs_verify_signature(struct smb_hdr *,
const struct mac_key *mac_key,
__u32 expected_sequence_number); __u32 expected_sequence_number);
extern int cifs_calculate_mac_key(char *key, const char *rn, const char *pass); extern int cifs_calculate_mac_key(struct mac_key *key, const char *rn,
const char *pass);
extern int CalcNTLMv2_partial_mac_key(struct cifsSesInfo *, extern int CalcNTLMv2_partial_mac_key(struct cifsSesInfo *,
const struct nls_table *); const struct nls_table *);
extern void CalcNTLMv2_response(const struct cifsSesInfo *, char * ); extern void CalcNTLMv2_response(const struct cifsSesInfo *, char * );
......
...@@ -3475,7 +3475,7 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo, ...@@ -3475,7 +3475,7 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo,
if(first_time) if(first_time)
cifs_calculate_mac_key( cifs_calculate_mac_key(
pSesInfo->server->mac_signing_key, &pSesInfo->server->mac_signing_key,
ntlm_session_key, ntlm_session_key,
pSesInfo->password); pSesInfo->password);
} }
...@@ -3495,7 +3495,7 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo, ...@@ -3495,7 +3495,7 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo,
if(first_time) if(first_time)
cifs_calculate_mac_key( cifs_calculate_mac_key(
pSesInfo->server->mac_signing_key, &pSesInfo->server->mac_signing_key,
ntlm_session_key, pSesInfo->password); ntlm_session_key, pSesInfo->password);
rc = CIFSSessSetup(xid, pSesInfo, rc = CIFSSessSetup(xid, pSesInfo,
......
...@@ -419,7 +419,7 @@ CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time, ...@@ -419,7 +419,7 @@ CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time,
if (first_time) /* should this be moved into common code if (first_time) /* should this be moved into common code
with similar ntlmv2 path? */ with similar ntlmv2 path? */
cifs_calculate_mac_key(ses->server->mac_signing_key, cifs_calculate_mac_key(&ses->server->mac_signing_key,
ntlm_session_key, ses->password); ntlm_session_key, ses->password);
/* copy session key */ /* copy session key */
......
...@@ -559,7 +559,7 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses, ...@@ -559,7 +559,7 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
(ses->server->secMode & (SECMODE_SIGN_REQUIRED | (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
SECMODE_SIGN_ENABLED))) { SECMODE_SIGN_ENABLED))) {
rc = cifs_verify_signature(midQ->resp_buf, rc = cifs_verify_signature(midQ->resp_buf,
ses->server->mac_signing_key, &ses->server->mac_signing_key,
midQ->sequence_number+1); midQ->sequence_number+1);
if (rc) { if (rc) {
cERROR(1, ("Unexpected SMB signature")); cERROR(1, ("Unexpected SMB signature"));
...@@ -738,7 +738,7 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses, ...@@ -738,7 +738,7 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
(ses->server->secMode & (SECMODE_SIGN_REQUIRED | (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
SECMODE_SIGN_ENABLED))) { SECMODE_SIGN_ENABLED))) {
rc = cifs_verify_signature(out_buf, rc = cifs_verify_signature(out_buf,
ses->server->mac_signing_key, &ses->server->mac_signing_key,
midQ->sequence_number+1); midQ->sequence_number+1);
if (rc) { if (rc) {
cERROR(1, ("Unexpected SMB signature")); cERROR(1, ("Unexpected SMB signature"));
...@@ -982,7 +982,7 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon, ...@@ -982,7 +982,7 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
(ses->server->secMode & (SECMODE_SIGN_REQUIRED | (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
SECMODE_SIGN_ENABLED))) { SECMODE_SIGN_ENABLED))) {
rc = cifs_verify_signature(out_buf, rc = cifs_verify_signature(out_buf,
ses->server->mac_signing_key, &ses->server->mac_signing_key,
midQ->sequence_number+1); midQ->sequence_number+1);
if (rc) { if (rc) {
cERROR(1, ("Unexpected SMB signature")); cERROR(1, ("Unexpected SMB signature"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment