Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifsencrypt.c |
| 3 | * |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2005,2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Lesser General Public License as published |
| 9 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "cifspdu.h" |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 25 | #include "cifsglob.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "cifs_debug.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "cifs_unicode.h" |
| 28 | #include "cifsproto.h" |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 29 | #include "ntlmssp.h" |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 30 | #include <linux/ctype.h> |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 31 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Jeff Layton | 157c249 | 2011-04-02 07:34:30 -0400 | [diff] [blame] | 33 | /* |
| 34 | * Calculate and return the CIFS signature based on the mac key and SMB PDU. |
| 35 | * The 16 byte signature must be allocated by the caller. Note we only use the |
| 36 | * 1st eight bytes and that the smb header signature field on input contains |
| 37 | * the sequence number before this function is called. Also, this function |
| 38 | * should be called with the server->srv_mutex held. |
| 39 | */ |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 40 | static int cifs_calc_signature(struct smb_rqst *rqst, |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 41 | struct TCP_Server_Info *server, char *signature) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 42 | { |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 43 | int i; |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 44 | int rc; |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 45 | struct kvec *iov = rqst->rq_iov; |
| 46 | int n_vec = rqst->rq_nvec; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 47 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 48 | if (iov == NULL || signature == NULL || server == NULL) |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 49 | return -EINVAL; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 50 | |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 51 | if (!server->secmech.sdescmd5) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 52 | cERROR(1, "%s: Can't generate signature", __func__); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | rc = crypto_shash_init(&server->secmech.sdescmd5->shash); |
| 57 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 58 | cERROR(1, "%s: Could not init md5", __func__); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 59 | return rc; |
| 60 | } |
| 61 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 62 | rc = crypto_shash_update(&server->secmech.sdescmd5->shash, |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 63 | server->session_key.response, server->session_key.len); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 64 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 65 | cERROR(1, "%s: Could not update with response", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 66 | return rc; |
| 67 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 68 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 69 | for (i = 0; i < n_vec; i++) { |
Jeff Layton | 745542e | 2007-11-03 04:34:04 +0000 | [diff] [blame] | 70 | if (iov[i].iov_len == 0) |
| 71 | continue; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 72 | if (iov[i].iov_base == NULL) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame] | 73 | cERROR(1, "null iovec entry"); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 74 | return -EIO; |
Jeff Layton | 745542e | 2007-11-03 04:34:04 +0000 | [diff] [blame] | 75 | } |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 76 | /* The first entry includes a length field (which does not get |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 77 | signed that occupies the first 4 bytes before the header */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 78 | if (i == 0) { |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 79 | if (iov[0].iov_len <= 8) /* cmd field at offset 9 */ |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 80 | break; /* nothing to sign or corrupt header */ |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 81 | rc = |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 82 | crypto_shash_update(&server->secmech.sdescmd5->shash, |
| 83 | iov[i].iov_base + 4, iov[i].iov_len - 4); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 84 | } else { |
| 85 | rc = |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 86 | crypto_shash_update(&server->secmech.sdescmd5->shash, |
| 87 | iov[i].iov_base, iov[i].iov_len); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 88 | } |
| 89 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 90 | cERROR(1, "%s: Could not update with payload", |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 91 | __func__); |
| 92 | return rc; |
| 93 | } |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 94 | } |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 95 | |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 96 | rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 97 | if (rc) |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 98 | cERROR(1, "%s: Could not generate md5 hash", __func__); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 99 | |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 100 | return rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Layton | a0f8b4f | 2011-01-07 11:30:28 -0500 | [diff] [blame] | 103 | /* must be called with server->srv_mutex held */ |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 104 | int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server, |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 105 | __u32 *pexpected_response_sequence_number) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 106 | { |
| 107 | int rc = 0; |
| 108 | char smb_signature[20]; |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 109 | struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 110 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 111 | if ((cifs_pdu == NULL) || (server == NULL)) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | |
Jeff Layton | 998d6fc | 2011-07-26 12:21:17 -0400 | [diff] [blame] | 114 | if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) || |
| 115 | server->tcpStatus == CifsNeedNegotiate) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 116 | return rc; |
| 117 | |
Jeff Layton | 998d6fc | 2011-07-26 12:21:17 -0400 | [diff] [blame] | 118 | if (!server->session_estab) { |
Jeff Layton | b4dacbc | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 119 | memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8); |
Jeff Layton | 998d6fc | 2011-07-26 12:21:17 -0400 | [diff] [blame] | 120 | return rc; |
| 121 | } |
| 122 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 123 | cifs_pdu->Signature.Sequence.SequenceNumber = |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 124 | cpu_to_le32(server->sequence_number); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 125 | cifs_pdu->Signature.Sequence.Reserved = 0; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 126 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 127 | *pexpected_response_sequence_number = server->sequence_number++; |
| 128 | server->sequence_number++; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 129 | |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 130 | rc = cifs_calc_signature(rqst, server, smb_signature); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 131 | if (rc) |
| 132 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
| 133 | else |
| 134 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 135 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 136 | return rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 139 | int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, |
| 140 | __u32 *pexpected_response_sequence) |
| 141 | { |
| 142 | struct smb_rqst rqst = { .rq_iov = iov, |
| 143 | .rq_nvec = n_vec }; |
| 144 | |
| 145 | return cifs_sign_rqst(&rqst, server, pexpected_response_sequence); |
| 146 | } |
| 147 | |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 148 | /* must be called with server->srv_mutex held */ |
| 149 | int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, |
| 150 | __u32 *pexpected_response_sequence_number) |
| 151 | { |
| 152 | struct kvec iov; |
| 153 | |
| 154 | iov.iov_base = cifs_pdu; |
| 155 | iov.iov_len = be32_to_cpu(cifs_pdu->smb_buf_length) + 4; |
| 156 | |
Jeff Layton | 762a420 | 2012-07-23 13:28:38 -0400 | [diff] [blame] | 157 | return cifs_sign_smbv(&iov, 1, server, |
Jeff Layton | 826a95e | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 158 | pexpected_response_sequence_number); |
| 159 | } |
| 160 | |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 161 | int cifs_verify_signature(struct smb_rqst *rqst, |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 162 | struct TCP_Server_Info *server, |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 163 | __u32 expected_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Steve French | c8e56f1 | 2010-09-08 21:10:58 +0000 | [diff] [blame] | 165 | unsigned int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | char server_response_sig[8]; |
| 167 | char what_we_think_sig_should_be[20]; |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 168 | struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 170 | if (cifs_pdu == NULL || server == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return -EINVAL; |
| 172 | |
Jeff Layton | 9c4843e | 2011-06-06 15:40:23 -0400 | [diff] [blame] | 173 | if (!server->session_estab) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | |
| 176 | if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 177 | struct smb_com_lock_req *pSMB = |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 178 | (struct smb_com_lock_req *)cifs_pdu; |
| 179 | if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 183 | /* BB what if signatures are supposed to be on for session but |
| 184 | server does not send one? BB */ |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 187 | if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 188 | cFYI(1, "dummy signature received for smb command 0x%x", |
| 189 | cifs_pdu->Command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | /* save off the origiginal signature so we can modify the smb and check |
| 192 | its signature against what the server sent */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 193 | memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 195 | cifs_pdu->Signature.Sequence.SequenceNumber = |
| 196 | cpu_to_le32(expected_sequence_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | cifs_pdu->Signature.Sequence.Reserved = 0; |
| 198 | |
Jeff Layton | 157c249 | 2011-04-02 07:34:30 -0400 | [diff] [blame] | 199 | mutex_lock(&server->srv_mutex); |
Jeff Layton | bf5ea0e | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 200 | rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be); |
Jeff Layton | 157c249 | 2011-04-02 07:34:30 -0400 | [diff] [blame] | 201 | mutex_unlock(&server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 203 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return rc; |
| 205 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 206 | /* cifs_dump_mem("what we think it should be: ", |
| 207 | what_we_think_sig_should_be, 16); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 209 | if (memcmp(server_response_sig, what_we_think_sig_should_be, 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | return -EACCES; |
| 211 | else |
| 212 | return 0; |
| 213 | |
| 214 | } |
| 215 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 216 | /* first calculate 24 bytes ntlm response and then 16 byte session key */ |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 217 | int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 219 | int rc = 0; |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 220 | unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE; |
| 221 | char temp_key[CIFS_SESS_KEY_SIZE]; |
| 222 | |
| 223 | if (!ses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return -EINVAL; |
| 225 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 226 | ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL); |
| 227 | if (!ses->auth_key.response) { |
| 228 | cERROR(1, "NTLM can't allocate (%u bytes) memory", temp_len); |
| 229 | return -ENOMEM; |
| 230 | } |
| 231 | ses->auth_key.len = temp_len; |
| 232 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 233 | rc = SMBNTencrypt(ses->password, ses->server->cryptkey, |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 234 | ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 235 | if (rc) { |
| 236 | cFYI(1, "%s Can't generate NTLM response, error: %d", |
| 237 | __func__, rc); |
| 238 | return rc; |
| 239 | } |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 240 | |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 241 | rc = E_md4hash(ses->password, temp_key, nls_cp); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 242 | if (rc) { |
| 243 | cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc); |
| 244 | return rc; |
| 245 | } |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 246 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 247 | rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE); |
| 248 | if (rc) |
| 249 | cFYI(1, "%s Can't generate NTLM session key, error: %d", |
| 250 | __func__, rc); |
| 251 | |
| 252 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 255 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 256 | int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt, |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 257 | char *lnm_session_key) |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 258 | { |
| 259 | int i; |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 260 | int rc; |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 261 | char password_with_pad[CIFS_ENCPWD_SIZE]; |
| 262 | |
| 263 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 264 | if (password) |
| 265 | strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 266 | |
Jeff Layton | 04912d6 | 2010-04-24 07:57:45 -0400 | [diff] [blame] | 267 | if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) { |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 268 | memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE); |
| 269 | memcpy(lnm_session_key, password_with_pad, |
| 270 | CIFS_ENCPWD_SIZE); |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 271 | return 0; |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 272 | } |
Steve French | bdc4bf6e | 2006-06-02 22:57:13 +0000 | [diff] [blame] | 273 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 274 | /* calculate old style session key */ |
| 275 | /* calling toupper is less broken than repeatedly |
| 276 | calling nls_toupper would be since that will never |
| 277 | work for UTF8, but neither handles multibyte code pages |
| 278 | but the only alternative would be converting to UCS-16 (Unicode) |
| 279 | (using a routine something like UniStrupr) then |
| 280 | uppercasing and then converting back from Unicode - which |
| 281 | would only worth doing it if we knew it were utf8. Basically |
| 282 | utf8 and other multibyte codepages each need their own strupper |
| 283 | function since a byte at a time will ont work. */ |
| 284 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 285 | for (i = 0; i < CIFS_ENCPWD_SIZE; i++) |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 286 | password_with_pad[i] = toupper(password_with_pad[i]); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 287 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 288 | rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key); |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 289 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 290 | return rc; |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 291 | } |
| 292 | #endif /* CIFS_WEAK_PW_HASH */ |
| 293 | |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 294 | /* Build a proper attribute value/target info pairs blob. |
| 295 | * Fill in netbios and dns domain name and workstation name |
| 296 | * and client time (total five av pairs and + one end of fields indicator. |
| 297 | * Allocate domain name which gets freed when session struct is deallocated. |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 298 | */ |
| 299 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 300 | build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp) |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 301 | { |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 302 | unsigned int dlen; |
Shirish Pargaonkar | cfbd6f8 | 2011-08-24 23:05:46 -0500 | [diff] [blame] | 303 | unsigned int size = 2 * sizeof(struct ntlmssp2_name); |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 304 | char *defdmname = "WORKGROUP"; |
| 305 | unsigned char *blobptr; |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 306 | struct ntlmssp2_name *attrptr; |
| 307 | |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 308 | if (!ses->domainName) { |
| 309 | ses->domainName = kstrdup(defdmname, GFP_KERNEL); |
| 310 | if (!ses->domainName) |
| 311 | return -ENOMEM; |
| 312 | } |
| 313 | |
| 314 | dlen = strlen(ses->domainName); |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 315 | |
Shirish Pargaonkar | cfbd6f8 | 2011-08-24 23:05:46 -0500 | [diff] [blame] | 316 | /* |
| 317 | * The length of this blob is two times the size of a |
| 318 | * structure (av pair) which holds name/size |
| 319 | * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) + |
| 320 | * unicode length of a netbios domain name |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 321 | */ |
Shirish Pargaonkar | cfbd6f8 | 2011-08-24 23:05:46 -0500 | [diff] [blame] | 322 | ses->auth_key.len = size + 2 * dlen; |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 323 | ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL); |
| 324 | if (!ses->auth_key.response) { |
| 325 | ses->auth_key.len = 0; |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 326 | cERROR(1, "Challenge target info allocation failure"); |
| 327 | return -ENOMEM; |
| 328 | } |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 329 | |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 330 | blobptr = ses->auth_key.response; |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 331 | attrptr = (struct ntlmssp2_name *) blobptr; |
| 332 | |
Shirish Pargaonkar | cfbd6f8 | 2011-08-24 23:05:46 -0500 | [diff] [blame] | 333 | /* |
| 334 | * As defined in MS-NTLM 3.3.2, just this av pair field |
| 335 | * is sufficient as part of the temp |
| 336 | */ |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 337 | attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME); |
| 338 | attrptr->length = cpu_to_le16(2 * dlen); |
| 339 | blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 340 | cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp); |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 341 | |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* Server has provided av pairs/target info in the type 2 challenge |
| 346 | * packet and we have plucked it and stored within smb session. |
| 347 | * We parse that blob here to find netbios domain name to be used |
| 348 | * as part of ntlmv2 authentication (in Target String), if not already |
| 349 | * specified on the command line. |
| 350 | * If this function returns without any error but without fetching |
| 351 | * domain name, authentication may fail against some server but |
| 352 | * may not fail against other (those who are not very particular |
| 353 | * about target string i.e. for some, just user name might suffice. |
| 354 | */ |
| 355 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 356 | find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp) |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 357 | { |
| 358 | unsigned int attrsize; |
| 359 | unsigned int type; |
| 360 | unsigned int onesize = sizeof(struct ntlmssp2_name); |
| 361 | unsigned char *blobptr; |
| 362 | unsigned char *blobend; |
| 363 | struct ntlmssp2_name *attrptr; |
| 364 | |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 365 | if (!ses->auth_key.len || !ses->auth_key.response) |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 366 | return 0; |
| 367 | |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 368 | blobptr = ses->auth_key.response; |
| 369 | blobend = blobptr + ses->auth_key.len; |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 370 | |
| 371 | while (blobptr + onesize < blobend) { |
| 372 | attrptr = (struct ntlmssp2_name *) blobptr; |
| 373 | type = le16_to_cpu(attrptr->type); |
| 374 | if (type == NTLMSSP_AV_EOL) |
| 375 | break; |
| 376 | blobptr += 2; /* advance attr type */ |
| 377 | attrsize = le16_to_cpu(attrptr->length); |
| 378 | blobptr += 2; /* advance attr size */ |
| 379 | if (blobptr + attrsize > blobend) |
| 380 | break; |
| 381 | if (type == NTLMSSP_AV_NB_DOMAIN_NAME) { |
| 382 | if (!attrsize) |
| 383 | break; |
| 384 | if (!ses->domainName) { |
| 385 | ses->domainName = |
| 386 | kmalloc(attrsize + 1, GFP_KERNEL); |
| 387 | if (!ses->domainName) |
| 388 | return -ENOMEM; |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 389 | cifs_from_utf16(ses->domainName, |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 390 | (__le16 *)blobptr, attrsize, attrsize, |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 391 | nls_cp, false); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | } |
| 395 | blobptr += attrsize; /* advance attr value */ |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 401 | static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 402 | const struct nls_table *nls_cp) |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 403 | { |
| 404 | int rc = 0; |
| 405 | int len; |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 406 | char nt_hash[CIFS_NTHASH_SIZE]; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 407 | wchar_t *user; |
| 408 | wchar_t *domain; |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 409 | wchar_t *server; |
Steve French | c8e56f1 | 2010-09-08 21:10:58 +0000 | [diff] [blame] | 410 | |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 411 | if (!ses->server->secmech.sdeschmacmd5) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 412 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash"); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 413 | return -1; |
| 414 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 415 | |
| 416 | /* calculate md4 hash of password */ |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 417 | E_md4hash(ses->password, nt_hash, nls_cp); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 418 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 419 | rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash, |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 420 | CIFS_NTHASH_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 421 | if (rc) { |
| 422 | cERROR(1, "%s: Could not set NT Hash as a key", __func__); |
| 423 | return rc; |
| 424 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 425 | |
| 426 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash); |
| 427 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 428 | cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5"); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 429 | return rc; |
| 430 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 431 | |
Steve French | 8727c8a | 2011-02-25 01:11:56 -0600 | [diff] [blame] | 432 | /* convert ses->user_name to unicode and uppercase */ |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 433 | len = ses->user_name ? strlen(ses->user_name) : 0; |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 434 | user = kmalloc(2 + (len * 2), GFP_KERNEL); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 435 | if (user == NULL) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 436 | cERROR(1, "calc_ntlmv2_hash: user mem alloc failure"); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 437 | rc = -ENOMEM; |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 438 | return rc; |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 439 | } |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 440 | |
| 441 | if (len) { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 442 | len = cifs_strtoUTF16((__le16 *)user, ses->user_name, len, nls_cp); |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 443 | UniStrupr(user); |
| 444 | } else { |
| 445 | memset(user, '\0', 2); |
| 446 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 447 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 448 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 449 | (char *)user, 2 * len); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 450 | kfree(user); |
| 451 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 452 | cERROR(1, "%s: Could not update with user", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 453 | return rc; |
| 454 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 455 | |
| 456 | /* convert ses->domainName to unicode and uppercase */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 457 | if (ses->domainName) { |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 458 | len = strlen(ses->domainName); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 459 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 460 | domain = kmalloc(2 + (len * 2), GFP_KERNEL); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 461 | if (domain == NULL) { |
| 462 | cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure"); |
| 463 | rc = -ENOMEM; |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 464 | return rc; |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 465 | } |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 466 | len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len, |
| 467 | nls_cp); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 468 | rc = |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 469 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
| 470 | (char *)domain, 2 * len); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 471 | kfree(domain); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 472 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 473 | cERROR(1, "%s: Could not update with domain", |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 474 | __func__); |
| 475 | return rc; |
| 476 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 477 | } else if (ses->serverName) { |
| 478 | len = strlen(ses->serverName); |
| 479 | |
| 480 | server = kmalloc(2 + (len * 2), GFP_KERNEL); |
| 481 | if (server == NULL) { |
| 482 | cERROR(1, "calc_ntlmv2_hash: server mem alloc failure"); |
| 483 | rc = -ENOMEM; |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 484 | return rc; |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 485 | } |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 486 | len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len, |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 487 | nls_cp); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 488 | rc = |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 489 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
| 490 | (char *)server, 2 * len); |
| 491 | kfree(server); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 492 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 493 | cERROR(1, "%s: Could not update with server", |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 494 | __func__); |
| 495 | return rc; |
| 496 | } |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 497 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 498 | |
| 499 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash, |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 500 | ntlmv2_hash); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 501 | if (rc) |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 502 | cERROR(1, "%s: Could not generate md5 hash", __func__); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 503 | |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 504 | return rc; |
| 505 | } |
| 506 | |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 507 | static int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 508 | CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash) |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 509 | { |
| 510 | int rc; |
| 511 | unsigned int offset = CIFS_SESS_KEY_SIZE + 8; |
| 512 | |
| 513 | if (!ses->server->secmech.sdeschmacmd5) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 514 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash"); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 515 | return -1; |
| 516 | } |
| 517 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 518 | rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 519 | ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 520 | if (rc) { |
| 521 | cERROR(1, "%s: Could not set NTLMV2 Hash as a key", __func__); |
| 522 | return rc; |
| 523 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 524 | |
| 525 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash); |
| 526 | if (rc) { |
| 527 | cERROR(1, "CalcNTLMv2_response: could not init hmacmd5"); |
| 528 | return rc; |
| 529 | } |
| 530 | |
Shirish Pargaonkar | d3ba50b | 2010-10-27 15:20:36 -0500 | [diff] [blame] | 531 | if (ses->server->secType == RawNTLMSSP) |
| 532 | memcpy(ses->auth_key.response + offset, |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 533 | ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); |
Shirish Pargaonkar | d3ba50b | 2010-10-27 15:20:36 -0500 | [diff] [blame] | 534 | else |
| 535 | memcpy(ses->auth_key.response + offset, |
| 536 | ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 537 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 538 | ses->auth_key.response + offset, ses->auth_key.len - offset); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 539 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 540 | cERROR(1, "%s: Could not update with response", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 541 | return rc; |
| 542 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 543 | |
| 544 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash, |
| 545 | ses->auth_key.response + CIFS_SESS_KEY_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 546 | if (rc) |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 547 | cERROR(1, "%s: Could not generate md5 hash", __func__); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 548 | |
| 549 | return rc; |
| 550 | } |
| 551 | |
| 552 | |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 553 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 554 | setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp) |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 555 | { |
Steve French | c8e56f1 | 2010-09-08 21:10:58 +0000 | [diff] [blame] | 556 | int rc; |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 557 | int baselen; |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 558 | unsigned int tilen; |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 559 | struct ntlmv2_resp *buf; |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 560 | char ntlmv2_hash[16]; |
| 561 | unsigned char *tiblob = NULL; /* target info blob */ |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 562 | |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 563 | if (ses->server->secType == RawNTLMSSP) { |
| 564 | if (!ses->domainName) { |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 565 | rc = find_domain_name(ses, nls_cp); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 566 | if (rc) { |
| 567 | cERROR(1, "error %d finding domain name", rc); |
| 568 | goto setup_ntlmv2_rsp_ret; |
| 569 | } |
| 570 | } |
| 571 | } else { |
Shirish Pargaonkar | 9daa42e | 2010-10-10 13:21:05 -0500 | [diff] [blame] | 572 | rc = build_avpair_blob(ses, nls_cp); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 573 | if (rc) { |
| 574 | cERROR(1, "error %d building av pair blob", rc); |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 575 | goto setup_ntlmv2_rsp_ret; |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 576 | } |
| 577 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 578 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 579 | baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp); |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 580 | tilen = ses->auth_key.len; |
| 581 | tiblob = ses->auth_key.response; |
| 582 | |
| 583 | ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL); |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 584 | if (!ses->auth_key.response) { |
| 585 | rc = ENOMEM; |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 586 | ses->auth_key.len = 0; |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 587 | cERROR(1, "%s: Can't allocate auth blob", __func__); |
| 588 | goto setup_ntlmv2_rsp_ret; |
| 589 | } |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 590 | ses->auth_key.len += baselen; |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 591 | |
| 592 | buf = (struct ntlmv2_resp *) |
| 593 | (ses->auth_key.response + CIFS_SESS_KEY_SIZE); |
| 594 | buf->blob_signature = cpu_to_le32(0x00000101); |
| 595 | buf->reserved = 0; |
| 596 | buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
| 597 | get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); |
| 598 | buf->reserved2 = 0; |
| 599 | |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 600 | memcpy(ses->auth_key.response + baselen, tiblob, tilen); |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 601 | |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 602 | /* calculate ntlmv2_hash */ |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 603 | rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 604 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 605 | cERROR(1, "could not get v2 hash rc %d", rc); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 606 | goto setup_ntlmv2_rsp_ret; |
| 607 | } |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 608 | |
| 609 | /* calculate first part of the client response (CR1) */ |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 610 | rc = CalcNTLMv2_response(ses, ntlmv2_hash); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 611 | if (rc) { |
| 612 | cERROR(1, "Could not calculate CR1 rc: %d", rc); |
| 613 | goto setup_ntlmv2_rsp_ret; |
| 614 | } |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 615 | |
Shirish Pargaonkar | 5d0d288 | 2010-10-13 18:15:00 -0500 | [diff] [blame] | 616 | /* now calculate the session key for NTLMv2 */ |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 617 | rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 618 | ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 619 | if (rc) { |
| 620 | cERROR(1, "%s: Could not set NTLMV2 Hash as a key", __func__); |
| 621 | goto setup_ntlmv2_rsp_ret; |
| 622 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 623 | |
| 624 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash); |
| 625 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 626 | cERROR(1, "%s: Could not init hmacmd5", __func__); |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 627 | goto setup_ntlmv2_rsp_ret; |
| 628 | } |
| 629 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 630 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 631 | ses->auth_key.response + CIFS_SESS_KEY_SIZE, |
| 632 | CIFS_HMAC_MD5_HASH_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 633 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 634 | cERROR(1, "%s: Could not update with response", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 635 | goto setup_ntlmv2_rsp_ret; |
| 636 | } |
Shirish Pargaonkar | 307fbd3 | 2010-10-21 14:25:17 -0500 | [diff] [blame] | 637 | |
| 638 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash, |
| 639 | ses->auth_key.response); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 640 | if (rc) |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 641 | cERROR(1, "%s: Could not generate md5 hash", __func__); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 642 | |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 643 | setup_ntlmv2_rsp_ret: |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 644 | kfree(tiblob); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 645 | |
| 646 | return rc; |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 649 | int |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 650 | calc_seckey(struct cifs_ses *ses) |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 651 | { |
| 652 | int rc; |
| 653 | struct crypto_blkcipher *tfm_arc4; |
| 654 | struct scatterlist sgin, sgout; |
| 655 | struct blkcipher_desc desc; |
| 656 | unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */ |
| 657 | |
| 658 | get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE); |
| 659 | |
| 660 | tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC); |
Shirish Pargaonkar | 7a8587e | 2011-01-29 13:54:58 -0600 | [diff] [blame] | 661 | if (IS_ERR(tfm_arc4)) { |
| 662 | rc = PTR_ERR(tfm_arc4); |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 663 | cERROR(1, "could not allocate crypto API arc4"); |
Shirish Pargaonkar | 7a8587e | 2011-01-29 13:54:58 -0600 | [diff] [blame] | 664 | return rc; |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | desc.tfm = tfm_arc4; |
| 668 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 669 | rc = crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response, |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 670 | CIFS_SESS_KEY_SIZE); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 671 | if (rc) { |
| 672 | cERROR(1, "%s: Could not set response as a key", __func__); |
| 673 | return rc; |
| 674 | } |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 675 | |
| 676 | sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE); |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 677 | sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 678 | |
| 679 | rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE); |
| 680 | if (rc) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 681 | cERROR(1, "could not encrypt session key rc: %d", rc); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 682 | crypto_free_blkcipher(tfm_arc4); |
| 683 | return rc; |
| 684 | } |
| 685 | |
| 686 | /* make secondary_key/nonce as session key */ |
| 687 | memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE); |
| 688 | /* and make len as that of session key only */ |
| 689 | ses->auth_key.len = CIFS_SESS_KEY_SIZE; |
| 690 | |
| 691 | crypto_free_blkcipher(tfm_arc4); |
| 692 | |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 693 | return rc; |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void |
| 697 | cifs_crypto_shash_release(struct TCP_Server_Info *server) |
| 698 | { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 699 | if (server->secmech.hmacsha256) |
| 700 | crypto_free_shash(server->secmech.hmacsha256); |
| 701 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 702 | if (server->secmech.md5) |
| 703 | crypto_free_shash(server->secmech.md5); |
| 704 | |
| 705 | if (server->secmech.hmacmd5) |
| 706 | crypto_free_shash(server->secmech.hmacmd5); |
| 707 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 708 | kfree(server->secmech.sdeschmacsha256); |
| 709 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 710 | kfree(server->secmech.sdeschmacmd5); |
| 711 | |
| 712 | kfree(server->secmech.sdescmd5); |
| 713 | } |
| 714 | |
| 715 | int |
| 716 | cifs_crypto_shash_allocate(struct TCP_Server_Info *server) |
| 717 | { |
| 718 | int rc; |
| 719 | unsigned int size; |
| 720 | |
| 721 | server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 722 | if (IS_ERR(server->secmech.hmacmd5)) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 723 | cERROR(1, "could not allocate crypto hmacmd5"); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 724 | return PTR_ERR(server->secmech.hmacmd5); |
| 725 | } |
| 726 | |
| 727 | server->secmech.md5 = crypto_alloc_shash("md5", 0, 0); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 728 | if (IS_ERR(server->secmech.md5)) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 729 | cERROR(1, "could not allocate crypto md5"); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 730 | rc = PTR_ERR(server->secmech.md5); |
| 731 | goto crypto_allocate_md5_fail; |
| 732 | } |
| 733 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 734 | server->secmech.hmacsha256 = crypto_alloc_shash("hmac(sha256)", 0, 0); |
| 735 | if (IS_ERR(server->secmech.hmacsha256)) { |
| 736 | cERROR(1, "could not allocate crypto hmacsha256\n"); |
| 737 | rc = PTR_ERR(server->secmech.hmacsha256); |
| 738 | goto crypto_allocate_hmacsha256_fail; |
| 739 | } |
| 740 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 741 | size = sizeof(struct shash_desc) + |
| 742 | crypto_shash_descsize(server->secmech.hmacmd5); |
| 743 | server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL); |
| 744 | if (!server->secmech.sdeschmacmd5) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 745 | cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5"); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 746 | rc = -ENOMEM; |
| 747 | goto crypto_allocate_hmacmd5_sdesc_fail; |
| 748 | } |
| 749 | server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5; |
| 750 | server->secmech.sdeschmacmd5->shash.flags = 0x0; |
| 751 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 752 | size = sizeof(struct shash_desc) + |
| 753 | crypto_shash_descsize(server->secmech.md5); |
| 754 | server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL); |
| 755 | if (!server->secmech.sdescmd5) { |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 756 | cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5"); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 757 | rc = -ENOMEM; |
| 758 | goto crypto_allocate_md5_sdesc_fail; |
| 759 | } |
| 760 | server->secmech.sdescmd5->shash.tfm = server->secmech.md5; |
| 761 | server->secmech.sdescmd5->shash.flags = 0x0; |
| 762 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 763 | size = sizeof(struct shash_desc) + |
| 764 | crypto_shash_descsize(server->secmech.hmacsha256); |
| 765 | server->secmech.sdeschmacsha256 = kmalloc(size, GFP_KERNEL); |
| 766 | if (!server->secmech.sdeschmacsha256) { |
| 767 | cERROR(1, "%s: Can't alloc hmacsha256\n", __func__); |
| 768 | rc = -ENOMEM; |
| 769 | goto crypto_allocate_hmacsha256_sdesc_fail; |
| 770 | } |
| 771 | server->secmech.sdeschmacsha256->shash.tfm = server->secmech.hmacsha256; |
| 772 | server->secmech.sdeschmacsha256->shash.flags = 0x0; |
| 773 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 774 | return 0; |
| 775 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 776 | crypto_allocate_hmacsha256_sdesc_fail: |
| 777 | kfree(server->secmech.sdescmd5); |
| 778 | |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 779 | crypto_allocate_md5_sdesc_fail: |
| 780 | kfree(server->secmech.sdeschmacmd5); |
| 781 | |
| 782 | crypto_allocate_hmacmd5_sdesc_fail: |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 783 | crypto_free_shash(server->secmech.hmacsha256); |
| 784 | |
| 785 | crypto_allocate_hmacsha256_fail: |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 786 | crypto_free_shash(server->secmech.md5); |
| 787 | |
| 788 | crypto_allocate_md5_fail: |
| 789 | crypto_free_shash(server->secmech.hmacmd5); |
| 790 | |
| 791 | return rc; |
| 792 | } |