xref: /freebsd/sys/netsmb/smb_crypt.c (revision df57947f083046d50552e99b91074927d2458708)
1c398230bSWarner Losh /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
4681a5bbeSBoris Popov  * Copyright (c) 2000-2001, Boris Popov
5681a5bbeSBoris Popov  * All rights reserved.
6681a5bbeSBoris Popov  *
7190b2c4fSTim J. Robbins  * Copyright (c) 2003, 2004 Tim J. Robbins.
8190b2c4fSTim J. Robbins  * All rights reserved.
9190b2c4fSTim J. Robbins  *
10681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
11681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
12681a5bbeSBoris Popov  * are met:
13681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
14681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
15681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
16681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
17681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
18681a5bbeSBoris Popov  * 3. All advertising materials mentioning features or use of this software
19681a5bbeSBoris Popov  *    must display the following acknowledgement:
20681a5bbeSBoris Popov  *    This product includes software developed by Boris Popov.
21681a5bbeSBoris Popov  * 4. Neither the name of the author nor the names of any co-contributors
22681a5bbeSBoris Popov  *    may be used to endorse or promote products derived from this software
23681a5bbeSBoris Popov  *    without specific prior written permission.
24681a5bbeSBoris Popov  *
25681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35681a5bbeSBoris Popov  * SUCH DAMAGE.
36681a5bbeSBoris Popov  */
37ab0de15bSDavid E. O'Brien 
38ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
39ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
40ab0de15bSDavid E. O'Brien 
41681a5bbeSBoris Popov #include <sys/param.h>
42681a5bbeSBoris Popov #include <sys/malloc.h>
43681a5bbeSBoris Popov #include <sys/kernel.h>
44681a5bbeSBoris Popov #include <sys/systm.h>
45681a5bbeSBoris Popov #include <sys/conf.h>
46681a5bbeSBoris Popov #include <sys/proc.h>
47681a5bbeSBoris Popov #include <sys/fcntl.h>
48681a5bbeSBoris Popov #include <sys/socket.h>
49681a5bbeSBoris Popov #include <sys/socketvar.h>
50681a5bbeSBoris Popov #include <sys/sysctl.h>
51190b2c4fSTim J. Robbins #include <sys/endian.h>
52190b2c4fSTim J. Robbins #include <sys/mbuf.h>
53190b2c4fSTim J. Robbins #include <sys/mchain.h>
54681a5bbeSBoris Popov #include <sys/md4.h>
55190b2c4fSTim J. Robbins #include <sys/md5.h>
56681a5bbeSBoris Popov #include <sys/iconv.h>
57681a5bbeSBoris Popov 
58681a5bbeSBoris Popov #include <netsmb/smb.h>
59681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
60681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
61190b2c4fSTim J. Robbins #include <netsmb/smb_rq.h>
62681a5bbeSBoris Popov #include <netsmb/smb_dev.h>
63681a5bbeSBoris Popov 
64681a5bbeSBoris Popov #include <crypto/des/des.h>
65681a5bbeSBoris Popov 
668d96e455SYaroslav Tykhiy #include "opt_netsmb.h"
678d96e455SYaroslav Tykhiy 
68681a5bbeSBoris Popov static u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
69681a5bbeSBoris Popov 
70681a5bbeSBoris Popov 
71681a5bbeSBoris Popov static void
72681a5bbeSBoris Popov smb_E(const u_char *key, u_char *data, u_char *dest)
73681a5bbeSBoris Popov {
74681a5bbeSBoris Popov 	des_key_schedule *ksp;
75681a5bbeSBoris Popov 	u_char kk[8];
76681a5bbeSBoris Popov 
77681a5bbeSBoris Popov 	kk[0] = key[0] & 0xfe;
78681a5bbeSBoris Popov 	kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
79681a5bbeSBoris Popov 	kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
80681a5bbeSBoris Popov 	kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
81681a5bbeSBoris Popov 	kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
82681a5bbeSBoris Popov 	kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
83681a5bbeSBoris Popov 	kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
84681a5bbeSBoris Popov 	kk[7] = key[6] << 1;
85a163d034SWarner Losh 	ksp = malloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
8633841545SHajimu UMEMOTO 	des_set_key((des_cblock *)kk, *ksp);
8733841545SHajimu UMEMOTO 	des_ecb_encrypt((des_cblock *)data, (des_cblock *)dest, *ksp, 1);
88681a5bbeSBoris Popov 	free(ksp, M_SMBTEMP);
89681a5bbeSBoris Popov }
90681a5bbeSBoris Popov 
91681a5bbeSBoris Popov 
92681a5bbeSBoris Popov int
93681a5bbeSBoris Popov smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
94681a5bbeSBoris Popov {
95681a5bbeSBoris Popov 	u_char *p, *P14, *S21;
96681a5bbeSBoris Popov 
97a163d034SWarner Losh 	p = malloc(14 + 21, M_SMBTEMP, M_WAITOK);
98681a5bbeSBoris Popov 	bzero(p, 14 + 21);
99681a5bbeSBoris Popov 	P14 = p;
100681a5bbeSBoris Popov 	S21 = p + 14;
101681a5bbeSBoris Popov 	bcopy(apwd, P14, min(14, strlen(apwd)));
102681a5bbeSBoris Popov 	/*
103681a5bbeSBoris Popov 	 * S21 = concat(Ex(P14, N8), zeros(5));
104681a5bbeSBoris Popov 	 */
105681a5bbeSBoris Popov 	smb_E(P14, N8, S21);
106681a5bbeSBoris Popov 	smb_E(P14 + 7, N8, S21 + 8);
107681a5bbeSBoris Popov 
108681a5bbeSBoris Popov 	smb_E(S21, C8, RN);
109681a5bbeSBoris Popov 	smb_E(S21 + 7, C8, RN + 8);
110681a5bbeSBoris Popov 	smb_E(S21 + 14, C8, RN + 16);
111681a5bbeSBoris Popov 	free(p, M_SMBTEMP);
112681a5bbeSBoris Popov 	return 0;
113681a5bbeSBoris Popov }
114681a5bbeSBoris Popov 
115681a5bbeSBoris Popov int
116681a5bbeSBoris Popov smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
117681a5bbeSBoris Popov {
118681a5bbeSBoris Popov 	u_char S21[21];
119681a5bbeSBoris Popov 	u_int16_t *unipwd;
120681a5bbeSBoris Popov 	MD4_CTX *ctxp;
121681a5bbeSBoris Popov 	int len;
122681a5bbeSBoris Popov 
123681a5bbeSBoris Popov 	len = strlen(apwd);
124a163d034SWarner Losh 	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
125681a5bbeSBoris Popov 	/*
126681a5bbeSBoris Popov 	 * S21 = concat(MD4(U(apwd)), zeros(5));
127681a5bbeSBoris Popov 	 */
128681a5bbeSBoris Popov 	smb_strtouni(unipwd, apwd);
129a163d034SWarner Losh 	ctxp = malloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
130681a5bbeSBoris Popov 	MD4Init(ctxp);
131681a5bbeSBoris Popov 	MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
132681a5bbeSBoris Popov 	free(unipwd, M_SMBTEMP);
133681a5bbeSBoris Popov 	bzero(S21, 21);
134681a5bbeSBoris Popov 	MD4Final(S21, ctxp);
135681a5bbeSBoris Popov 	free(ctxp, M_SMBTEMP);
136681a5bbeSBoris Popov 
137681a5bbeSBoris Popov 	smb_E(S21, C8, RN);
138681a5bbeSBoris Popov 	smb_E(S21 + 7, C8, RN + 8);
139681a5bbeSBoris Popov 	smb_E(S21 + 14, C8, RN + 16);
140681a5bbeSBoris Popov 	return 0;
141681a5bbeSBoris Popov }
142681a5bbeSBoris Popov 
143190b2c4fSTim J. Robbins /*
144190b2c4fSTim J. Robbins  * Calculate message authentication code (MAC) key for virtual circuit.
145190b2c4fSTim J. Robbins  */
146190b2c4fSTim J. Robbins int
147190b2c4fSTim J. Robbins smb_calcmackey(struct smb_vc *vcp)
148190b2c4fSTim J. Robbins {
149190b2c4fSTim J. Robbins 	const char *pwd;
150190b2c4fSTim J. Robbins 	u_int16_t *unipwd;
151190b2c4fSTim J. Robbins 	int len;
152190b2c4fSTim J. Robbins 	MD4_CTX md4;
153190b2c4fSTim J. Robbins 	u_char S16[16], S21[21];
154190b2c4fSTim J. Robbins 
155190b2c4fSTim J. Robbins 	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
156190b2c4fSTim J. Robbins 	    ("signatures not enabled"));
157190b2c4fSTim J. Robbins 
158190b2c4fSTim J. Robbins 	if (vcp->vc_mackey != NULL) {
159190b2c4fSTim J. Robbins 		free(vcp->vc_mackey, M_SMBTEMP);
160190b2c4fSTim J. Robbins 		vcp->vc_mackey = NULL;
161190b2c4fSTim J. Robbins 		vcp->vc_mackeylen = 0;
162190b2c4fSTim J. Robbins 		vcp->vc_seqno = 0;
163190b2c4fSTim J. Robbins 	}
164190b2c4fSTim J. Robbins 
165190b2c4fSTim J. Robbins 	/*
166190b2c4fSTim J. Robbins 	 * The partial MAC key is the concatenation of the 16 byte session
167190b2c4fSTim J. Robbins 	 * key and the 24 byte challenge response.
168190b2c4fSTim J. Robbins 	 */
169190b2c4fSTim J. Robbins 	vcp->vc_mackeylen = 16 + 24;
170190b2c4fSTim J. Robbins 	vcp->vc_mackey = malloc(vcp->vc_mackeylen, M_SMBTEMP, M_WAITOK);
171190b2c4fSTim J. Robbins 
172190b2c4fSTim J. Robbins 	/*
173190b2c4fSTim J. Robbins 	 * Calculate session key:
174190b2c4fSTim J. Robbins 	 *	MD4(MD4(U(PN)))
175190b2c4fSTim J. Robbins 	 */
176190b2c4fSTim J. Robbins 	pwd = smb_vc_getpass(vcp);
177190b2c4fSTim J. Robbins 	len = strlen(pwd);
178190b2c4fSTim J. Robbins 	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
179190b2c4fSTim J. Robbins 	smb_strtouni(unipwd, pwd);
180190b2c4fSTim J. Robbins 	MD4Init(&md4);
181190b2c4fSTim J. Robbins 	MD4Update(&md4, (u_char *)unipwd, len * sizeof(u_int16_t));
182190b2c4fSTim J. Robbins 	MD4Final(S16, &md4);
183190b2c4fSTim J. Robbins 	MD4Init(&md4);
184190b2c4fSTim J. Robbins 	MD4Update(&md4, S16, 16);
185190b2c4fSTim J. Robbins 	MD4Final(vcp->vc_mackey, &md4);
186190b2c4fSTim J. Robbins 	free(unipwd, M_SMBTEMP);
187190b2c4fSTim J. Robbins 
188190b2c4fSTim J. Robbins 	/*
189190b2c4fSTim J. Robbins 	 * Calculate response to challenge:
190190b2c4fSTim J. Robbins 	 *	Ex(concat(MD4(U(pass)), zeros(5)), C8)
191190b2c4fSTim J. Robbins 	 */
192190b2c4fSTim J. Robbins 	bzero(S21, 21);
193190b2c4fSTim J. Robbins 	bcopy(S16, S21, 16);
194190b2c4fSTim J. Robbins 	smb_E(S21, vcp->vc_ch, vcp->vc_mackey + 16);
195190b2c4fSTim J. Robbins 	smb_E(S21 + 7, vcp->vc_ch, vcp->vc_mackey + 24);
196190b2c4fSTim J. Robbins 	smb_E(S21 + 14, vcp->vc_ch, vcp->vc_mackey + 32);
197190b2c4fSTim J. Robbins 
198190b2c4fSTim J. Robbins 	return (0);
199190b2c4fSTim J. Robbins }
200190b2c4fSTim J. Robbins 
201190b2c4fSTim J. Robbins /*
202190b2c4fSTim J. Robbins  * Sign request with MAC.
203190b2c4fSTim J. Robbins  */
204190b2c4fSTim J. Robbins int
205190b2c4fSTim J. Robbins smb_rq_sign(struct smb_rq *rqp)
206190b2c4fSTim J. Robbins {
207190b2c4fSTim J. Robbins 	struct smb_vc *vcp = rqp->sr_vc;
208190b2c4fSTim J. Robbins 	struct mbchain *mbp;
209190b2c4fSTim J. Robbins 	struct mbuf *mb;
210190b2c4fSTim J. Robbins 	MD5_CTX md5;
211190b2c4fSTim J. Robbins 	u_char digest[16];
212190b2c4fSTim J. Robbins 
213190b2c4fSTim J. Robbins 	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
214190b2c4fSTim J. Robbins 	    ("signatures not enabled"));
215190b2c4fSTim J. Robbins 
216190b2c4fSTim J. Robbins 	if (vcp->vc_mackey == NULL)
217190b2c4fSTim J. Robbins 		/* XXX Should assert that cmd == SMB_COM_NEGOTIATE. */
218190b2c4fSTim J. Robbins 		return (0);
219190b2c4fSTim J. Robbins 
220190b2c4fSTim J. Robbins 	/*
221190b2c4fSTim J. Robbins 	 * This is a bit of a kludge. If the request is non-TRANSACTION,
222190b2c4fSTim J. Robbins 	 * or it is the first request of a transaction, give it the next
223190b2c4fSTim J. Robbins 	 * sequence number, and expect the reply to have the sequence number
224190b2c4fSTim J. Robbins 	 * following that one. Otherwise, it is a secondary request in
225190b2c4fSTim J. Robbins 	 * a transaction, and it gets the same sequence numbers as the
226190b2c4fSTim J. Robbins 	 * primary request.
227190b2c4fSTim J. Robbins 	 */
228190b2c4fSTim J. Robbins 	if (rqp->sr_t2 == NULL ||
229190b2c4fSTim J. Robbins 	    (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0) {
230190b2c4fSTim J. Robbins 		rqp->sr_seqno = vcp->vc_seqno++;
231190b2c4fSTim J. Robbins 		rqp->sr_rseqno = vcp->vc_seqno++;
232190b2c4fSTim J. Robbins 	} else {
233190b2c4fSTim J. Robbins 		/*
234190b2c4fSTim J. Robbins 		 * Sequence numbers are already in the struct because
235190b2c4fSTim J. Robbins 		 * smb_t2_request_int() uses the same one for all the
236190b2c4fSTim J. Robbins 		 * requests in the transaction.
237190b2c4fSTim J. Robbins 		 * (At least we hope so.)
238190b2c4fSTim J. Robbins 		 */
239190b2c4fSTim J. Robbins 		KASSERT(rqp->sr_t2 == NULL ||
240190b2c4fSTim J. Robbins 		    (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0 ||
241190b2c4fSTim J. Robbins 		    rqp->sr_t2->t2_rq == rqp,
242190b2c4fSTim J. Robbins 		    ("sec t2 rq not using same smb_rq"));
243190b2c4fSTim J. Robbins 	}
244190b2c4fSTim J. Robbins 
245190b2c4fSTim J. Robbins 	/* Initialize sec. signature field to sequence number + zeros. */
246a6a4232fSMarcel Moolenaar 	le32enc(rqp->sr_rqsig, rqp->sr_seqno);
247a6a4232fSMarcel Moolenaar 	le32enc(rqp->sr_rqsig + 4, 0);
248190b2c4fSTim J. Robbins 
249190b2c4fSTim J. Robbins 	/*
250190b2c4fSTim J. Robbins 	 * Compute HMAC-MD5 of packet data, keyed by MAC key.
251190b2c4fSTim J. Robbins 	 * Store the first 8 bytes in the sec. signature field.
252190b2c4fSTim J. Robbins 	 */
253190b2c4fSTim J. Robbins 	smb_rq_getrequest(rqp, &mbp);
254190b2c4fSTim J. Robbins 	MD5Init(&md5);
255190b2c4fSTim J. Robbins 	MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
256190b2c4fSTim J. Robbins 	for (mb = mbp->mb_top; mb != NULL; mb = mb->m_next)
257190b2c4fSTim J. Robbins 		MD5Update(&md5, mtod(mb, void *), mb->m_len);
258190b2c4fSTim J. Robbins 	MD5Final(digest, &md5);
259190b2c4fSTim J. Robbins 	bcopy(digest, rqp->sr_rqsig, 8);
260190b2c4fSTim J. Robbins 
261190b2c4fSTim J. Robbins 	return (0);
262190b2c4fSTim J. Robbins }
263190b2c4fSTim J. Robbins 
264190b2c4fSTim J. Robbins /*
265190b2c4fSTim J. Robbins  * Verify reply signature.
266190b2c4fSTim J. Robbins  */
267190b2c4fSTim J. Robbins int
268190b2c4fSTim J. Robbins smb_rq_verify(struct smb_rq *rqp)
269190b2c4fSTim J. Robbins {
270190b2c4fSTim J. Robbins 	struct smb_vc *vcp = rqp->sr_vc;
271190b2c4fSTim J. Robbins 	struct mdchain *mdp;
272190b2c4fSTim J. Robbins 	u_char sigbuf[8];
273190b2c4fSTim J. Robbins 	MD5_CTX md5;
274190b2c4fSTim J. Robbins 	u_char digest[16];
275190b2c4fSTim J. Robbins 	struct mbuf *mb;
276190b2c4fSTim J. Robbins 
277190b2c4fSTim J. Robbins 	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
278190b2c4fSTim J. Robbins 	    ("signatures not enabled"));
279190b2c4fSTim J. Robbins 
280190b2c4fSTim J. Robbins 	if (vcp->vc_mackey == NULL)
281190b2c4fSTim J. Robbins 		/* XXX Should check that this is a SMB_COM_NEGOTIATE reply. */
282190b2c4fSTim J. Robbins 		return (0);
283190b2c4fSTim J. Robbins 
284190b2c4fSTim J. Robbins 	/*
285190b2c4fSTim J. Robbins 	 * Compute HMAC-MD5 of packet data, keyed by MAC key.
286190b2c4fSTim J. Robbins 	 * We play games to pretend the security signature field
287190b2c4fSTim J. Robbins 	 * contains their sequence number, to avoid modifying
288190b2c4fSTim J. Robbins 	 * the packet itself.
289190b2c4fSTim J. Robbins 	 */
290190b2c4fSTim J. Robbins 	smb_rq_getreply(rqp, &mdp);
291190b2c4fSTim J. Robbins 	mb = mdp->md_top;
292190b2c4fSTim J. Robbins 	KASSERT(mb->m_len >= SMB_HDRLEN, ("forgot to m_pullup"));
293190b2c4fSTim J. Robbins 	MD5Init(&md5);
294190b2c4fSTim J. Robbins 	MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
295190b2c4fSTim J. Robbins 	MD5Update(&md5, mtod(mb, void *), 14);
296190b2c4fSTim J. Robbins 	*(u_int32_t *)sigbuf = htole32(rqp->sr_rseqno);
297190b2c4fSTim J. Robbins 	*(u_int32_t *)(sigbuf + 4) = 0;
298190b2c4fSTim J. Robbins 	MD5Update(&md5, sigbuf, 8);
299190b2c4fSTim J. Robbins 	MD5Update(&md5, mtod(mb, u_char *) + 22, mb->m_len - 22);
300190b2c4fSTim J. Robbins 	for (mb = mb->m_next; mb != NULL; mb = mb->m_next)
301190b2c4fSTim J. Robbins 		MD5Update(&md5, mtod(mb, void *), mb->m_len);
302190b2c4fSTim J. Robbins 	MD5Final(digest, &md5);
303190b2c4fSTim J. Robbins 
304190b2c4fSTim J. Robbins 	/*
305190b2c4fSTim J. Robbins 	 * Now verify the signature.
306190b2c4fSTim J. Robbins 	 */
307190b2c4fSTim J. Robbins 	if (bcmp(mtod(mdp->md_top, u_char *) + 14, digest, 8) != 0)
308190b2c4fSTim J. Robbins 		return (EAUTH);
309190b2c4fSTim J. Robbins 
310190b2c4fSTim J. Robbins 	return (0);
311190b2c4fSTim J. Robbins }
312