2 * Copyright (c) 2000-2001, Boris Popov
5 * Copyright (c) 2003, 2004 Tim J. Robbins.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Boris Popov.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/sys/netsmb/smb_crypt.c,v 1.1.2.3 2001/09/03 08:55:11 bp Exp $
36 * $DragonFly: src/sys/netproto/smb/smb_crypt.c,v 1.5 2008/01/05 14:02:40 swildner Exp $
38 #include <sys/param.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/endian.h>
51 #include <sys/mchain.h>
55 #include <sys/iconv.h>
63 #include "opt_netsmb.h"
67 #include <crypto/des/des.h>
69 static u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
73 smb_E(const u_char *key, u_char *data, u_char *dest)
75 des_key_schedule *ksp;
78 kk[0] = key[0] & 0xfe;
79 kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
80 kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
81 kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
82 kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
83 kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
84 kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
86 ksp = kmalloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
87 des_set_key((des_cblock *)kk, *ksp);
88 des_ecb_encrypt((des_cblock *)data, (des_cblock *)dest, *ksp, 1);
89 kfree(ksp, M_SMBTEMP);
95 smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
98 u_char *p, *P14, *S21;
100 p = kmalloc(14 + 21, M_SMBTEMP, M_WAITOK | M_ZERO);
103 bcopy(apwd, P14, min(14, strlen(apwd)));
105 * S21 = concat(Ex(P14, N8), zeros(5));
108 smb_E(P14 + 7, N8, S21 + 8);
111 smb_E(S21 + 7, C8, RN + 8);
112 smb_E(S21 + 14, C8, RN + 16);
116 SMBERROR("password encryption is not available\n");
123 smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
132 unipwd = kmalloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
134 * S21 = concat(MD4(U(apwd)), zeros(5));
136 smb_strtouni(unipwd, apwd);
137 ctxp = kmalloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
139 MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
140 kfree(unipwd, M_SMBTEMP);
143 kfree(ctxp, M_SMBTEMP);
146 smb_E(S21 + 7, C8, RN + 8);
147 smb_E(S21 + 14, C8, RN + 16);
150 SMBERROR("password encryption is not available\n");
157 * Calculate message authentication code (MAC) key for virtual circuit.
160 smb_calcmackey(struct smb_vc *vcp)
167 u_char S16[16], S21[21];
169 KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
170 ("signatures not enabled"));
172 if (vcp->vc_mackey != NULL) {
173 kfree(vcp->vc_mackey, M_SMBTEMP);
174 vcp->vc_mackey = NULL;
175 vcp->vc_mackeylen = 0;
180 * The partial MAC key is the concatenation of the 16 byte session
181 * key and the 24 byte challenge response.
183 vcp->vc_mackeylen = 16 + 24;
184 vcp->vc_mackey = kmalloc(vcp->vc_mackeylen, M_SMBTEMP, M_WAITOK);
187 * Calculate session key:
190 pwd = smb_vc_getpass(vcp);
192 unipwd = kmalloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
193 smb_strtouni(unipwd, pwd);
195 MD4Update(&md4, (u_char *)unipwd, len * sizeof(u_int16_t));
198 MD4Update(&md4, S16, 16);
199 MD4Final(vcp->vc_mackey, &md4);
200 kfree(unipwd, M_SMBTEMP);
203 * Calculate response to challenge:
204 * Ex(concat(MD4(U(pass)), zeros(5)), C8)
208 smb_E(S21, vcp->vc_ch, vcp->vc_mackey + 16);
209 smb_E(S21 + 7, vcp->vc_ch, vcp->vc_mackey + 24);
210 smb_E(S21 + 14, vcp->vc_ch, vcp->vc_mackey + 32);
214 panic("smb_calcmackey: encryption not available");
216 #endif /* NETSMBCRYPTO */
220 * Sign request with MAC.
223 smb_rq_sign(struct smb_rq *rqp)
226 struct smb_vc *vcp = rqp->sr_vc;
232 KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
233 ("signatures not enabled"));
235 if (vcp->vc_mackey == NULL)
236 /* XXX Should assert that cmd == SMB_COM_NEGOTIATE. */
240 * This is a bit of a kludge. If the request is non-TRANSACTION,
241 * or it is the first request of a transaction, give it the next
242 * sequence number, and expect the reply to have the sequence number
243 * following that one. Otherwise, it is a secondary request in
244 * a transaction, and it gets the same sequence numbers as the
247 if (rqp->sr_t2 == NULL ||
248 (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0) {
249 rqp->sr_seqno = vcp->vc_seqno++;
250 rqp->sr_rseqno = vcp->vc_seqno++;
253 * Sequence numbers are already in the struct because
254 * smb_t2_request_int() uses the same one for all the
255 * requests in the transaction.
256 * (At least we hope so.)
258 KASSERT(rqp->sr_t2 == NULL ||
259 (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0 ||
260 rqp->sr_t2->t2_rq == rqp,
261 ("sec t2 rq not using same smb_rq"));
264 /* Initialize sec. signature field to sequence number + zeros. */
265 *(u_int32_t *)rqp->sr_rqsig = htole32(rqp->sr_seqno);
266 *(u_int32_t *)(rqp->sr_rqsig + 4) = 0;
269 * Compute HMAC-MD5 of packet data, keyed by MAC key.
270 * Store the first 8 bytes in the sec. signature field.
272 smb_rq_getrequest(rqp, &mbp);
274 MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
275 for (mb = mbp->mb_top; mb != NULL; mb = mb->m_next)
276 MD5Update(&md5, mtod(mb, void *), mb->m_len);
277 MD5Final(digest, &md5);
278 bcopy(digest, rqp->sr_rqsig, 8);
282 panic("smb_rq_sign: encryption not available");
284 #endif /* NETSMBCRYPTO */
288 * Verify reply signature.
291 smb_rq_verify(struct smb_rq *rqp)
294 struct smb_vc *vcp = rqp->sr_vc;
301 KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
302 ("signatures not enabled"));
304 if (vcp->vc_mackey == NULL)
305 /* XXX Should check that this is a SMB_COM_NEGOTIATE reply. */
309 * Compute HMAC-MD5 of packet data, keyed by MAC key.
310 * We play games to pretend the security signature field
311 * contains their sequence number, to avoid modifying
314 smb_rq_getreply(rqp, &mdp);
316 KASSERT(mb->m_len >= SMB_HDRLEN, ("forgot to m_pullup"));
318 MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
319 MD5Update(&md5, mtod(mb, void *), 14);
320 *(u_int32_t *)sigbuf = htole32(rqp->sr_rseqno);
321 *(u_int32_t *)(sigbuf + 4) = 0;
322 MD5Update(&md5, sigbuf, 8);
323 MD5Update(&md5, mtod(mb, u_char *) + 22, mb->m_len - 22);
324 for (mb = mb->m_next; mb != NULL; mb = mb->m_next)
325 MD5Update(&md5, mtod(mb, void *), mb->m_len);
326 MD5Final(digest, &md5);
329 * Now verify the signature.
331 if (bcmp(mtod(mdp->md_top, u_char *) + 14, digest, 8) != 0)
336 panic("smb_rq_verify: encryption not available");
338 #endif /* NETSMBCRYPTO */