xref: /freebsd/sys/netsmb/smb_crypt.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2000-2001, Boris Popov
5  * All rights reserved.
6  *
7  * Copyright (c) 2003, 2004 Tim J. Robbins.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *    This product includes software developed by Boris Popov.
21  * 4. Neither the name of the author nor the names of any co-contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/proc.h>
45 #include <sys/fcntl.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/endian.h>
50 #include <sys/mbuf.h>
51 #include <sys/mchain.h>
52 #include <sys/md4.h>
53 #include <sys/md5.h>
54 #include <sys/iconv.h>
55 
56 #include <netsmb/smb.h>
57 #include <netsmb/smb_conn.h>
58 #include <netsmb/smb_subr.h>
59 #include <netsmb/smb_rq.h>
60 #include <netsmb/smb_dev.h>
61 
62 #include <crypto/des/des.h>
63 
64 #include "opt_netsmb.h"
65 
66 static u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
67 
68 static void
69 smb_E(const u_char *key, u_char *data, u_char *dest)
70 {
71 	des_key_schedule *ksp;
72 	u_char kk[8];
73 
74 	kk[0] = key[0] & 0xfe;
75 	kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
76 	kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
77 	kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
78 	kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
79 	kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
80 	kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
81 	kk[7] = key[6] << 1;
82 	ksp = malloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
83 	des_set_key(kk, *ksp);
84 	des_ecb_encrypt(data, dest, *ksp, 1);
85 	free(ksp, M_SMBTEMP);
86 }
87 
88 int
89 smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
90 {
91 	u_char *p, *P14, *S21;
92 
93 	p = malloc(14 + 21, M_SMBTEMP, M_WAITOK);
94 	bzero(p, 14 + 21);
95 	P14 = p;
96 	S21 = p + 14;
97 	bcopy(apwd, P14, min(14, strlen(apwd)));
98 	/*
99 	 * S21 = concat(Ex(P14, N8), zeros(5));
100 	 */
101 	smb_E(P14, N8, S21);
102 	smb_E(P14 + 7, N8, S21 + 8);
103 
104 	smb_E(S21, C8, RN);
105 	smb_E(S21 + 7, C8, RN + 8);
106 	smb_E(S21 + 14, C8, RN + 16);
107 	free(p, M_SMBTEMP);
108 	return 0;
109 }
110 
111 int
112 smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
113 {
114 	u_char S21[21];
115 	u_int16_t *unipwd;
116 	MD4_CTX *ctxp;
117 	u_int len;
118 
119 	len = strlen(apwd);
120 	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
121 	/*
122 	 * S21 = concat(MD4(U(apwd)), zeros(5));
123 	 */
124 	smb_strtouni(unipwd, apwd);
125 	ctxp = malloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
126 	MD4Init(ctxp);
127 	MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
128 	free(unipwd, M_SMBTEMP);
129 	bzero(S21, 21);
130 	MD4Final(S21, ctxp);
131 	free(ctxp, M_SMBTEMP);
132 
133 	smb_E(S21, C8, RN);
134 	smb_E(S21 + 7, C8, RN + 8);
135 	smb_E(S21 + 14, C8, RN + 16);
136 	return 0;
137 }
138 
139 /*
140  * Calculate message authentication code (MAC) key for virtual circuit.
141  */
142 int
143 smb_calcmackey(struct smb_vc *vcp)
144 {
145 	const char *pwd;
146 	u_int16_t *unipwd;
147 	u_int len;
148 	MD4_CTX md4;
149 	u_char S16[16], S21[21];
150 
151 	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
152 	    ("signatures not enabled"));
153 
154 	if (vcp->vc_mackey != NULL) {
155 		free(vcp->vc_mackey, M_SMBTEMP);
156 		vcp->vc_mackey = NULL;
157 		vcp->vc_mackeylen = 0;
158 		vcp->vc_seqno = 0;
159 	}
160 
161 	/*
162 	 * The partial MAC key is the concatenation of the 16 byte session
163 	 * key and the 24 byte challenge response.
164 	 */
165 	vcp->vc_mackeylen = 16 + 24;
166 	vcp->vc_mackey = malloc(vcp->vc_mackeylen, M_SMBTEMP, M_WAITOK);
167 
168 	/*
169 	 * Calculate session key:
170 	 *	MD4(MD4(U(PN)))
171 	 */
172 	pwd = smb_vc_getpass(vcp);
173 	len = strlen(pwd);
174 	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
175 	smb_strtouni(unipwd, pwd);
176 	MD4Init(&md4);
177 	MD4Update(&md4, (u_char *)unipwd, len * sizeof(u_int16_t));
178 	MD4Final(S16, &md4);
179 	MD4Init(&md4);
180 	MD4Update(&md4, S16, 16);
181 	MD4Final(vcp->vc_mackey, &md4);
182 	free(unipwd, M_SMBTEMP);
183 
184 	/*
185 	 * Calculate response to challenge:
186 	 *	Ex(concat(MD4(U(pass)), zeros(5)), C8)
187 	 */
188 	bzero(S21, 21);
189 	bcopy(S16, S21, 16);
190 	smb_E(S21, vcp->vc_ch, vcp->vc_mackey + 16);
191 	smb_E(S21 + 7, vcp->vc_ch, vcp->vc_mackey + 24);
192 	smb_E(S21 + 14, vcp->vc_ch, vcp->vc_mackey + 32);
193 
194 	return (0);
195 }
196 
197 /*
198  * Sign request with MAC.
199  */
200 int
201 smb_rq_sign(struct smb_rq *rqp)
202 {
203 	struct smb_vc *vcp = rqp->sr_vc;
204 	struct mbchain *mbp;
205 	struct mbuf *mb;
206 	MD5_CTX md5;
207 	u_char digest[16];
208 
209 	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
210 	    ("signatures not enabled"));
211 
212 	if (vcp->vc_mackey == NULL)
213 		/* XXX Should assert that cmd == SMB_COM_NEGOTIATE. */
214 		return (0);
215 
216 	/*
217 	 * This is a bit of a kludge. If the request is non-TRANSACTION,
218 	 * or it is the first request of a transaction, give it the next
219 	 * sequence number, and expect the reply to have the sequence number
220 	 * following that one. Otherwise, it is a secondary request in
221 	 * a transaction, and it gets the same sequence numbers as the
222 	 * primary request.
223 	 */
224 	if (rqp->sr_t2 == NULL ||
225 	    (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0) {
226 		rqp->sr_seqno = vcp->vc_seqno++;
227 		rqp->sr_rseqno = vcp->vc_seqno++;
228 	} else {
229 		/*
230 		 * Sequence numbers are already in the struct because
231 		 * smb_t2_request_int() uses the same one for all the
232 		 * requests in the transaction.
233 		 * (At least we hope so.)
234 		 */
235 		KASSERT(rqp->sr_t2 == NULL ||
236 		    (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0 ||
237 		    rqp->sr_t2->t2_rq == rqp,
238 		    ("sec t2 rq not using same smb_rq"));
239 	}
240 
241 	/* Initialize sec. signature field to sequence number + zeros. */
242 	le32enc(rqp->sr_rqsig, rqp->sr_seqno);
243 	le32enc(rqp->sr_rqsig + 4, 0);
244 
245 	/*
246 	 * Compute HMAC-MD5 of packet data, keyed by MAC key.
247 	 * Store the first 8 bytes in the sec. signature field.
248 	 */
249 	smb_rq_getrequest(rqp, &mbp);
250 	MD5Init(&md5);
251 	MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
252 	for (mb = mbp->mb_top; mb != NULL; mb = mb->m_next)
253 		MD5Update(&md5, mtod(mb, void *), mb->m_len);
254 	MD5Final(digest, &md5);
255 	bcopy(digest, rqp->sr_rqsig, 8);
256 
257 	return (0);
258 }
259 
260 /*
261  * Verify reply signature.
262  */
263 int
264 smb_rq_verify(struct smb_rq *rqp)
265 {
266 	struct smb_vc *vcp = rqp->sr_vc;
267 	struct mdchain *mdp;
268 	u_char sigbuf[8];
269 	MD5_CTX md5;
270 	u_char digest[16];
271 	struct mbuf *mb;
272 
273 	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
274 	    ("signatures not enabled"));
275 
276 	if (vcp->vc_mackey == NULL)
277 		/* XXX Should check that this is a SMB_COM_NEGOTIATE reply. */
278 		return (0);
279 
280 	/*
281 	 * Compute HMAC-MD5 of packet data, keyed by MAC key.
282 	 * We play games to pretend the security signature field
283 	 * contains their sequence number, to avoid modifying
284 	 * the packet itself.
285 	 */
286 	smb_rq_getreply(rqp, &mdp);
287 	mb = mdp->md_top;
288 	KASSERT(mb->m_len >= SMB_HDRLEN, ("forgot to m_pullup"));
289 	MD5Init(&md5);
290 	MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
291 	MD5Update(&md5, mtod(mb, void *), 14);
292 	*(u_int32_t *)sigbuf = htole32(rqp->sr_rseqno);
293 	*(u_int32_t *)(sigbuf + 4) = 0;
294 	MD5Update(&md5, sigbuf, 8);
295 	MD5Update(&md5, mtod(mb, u_char *) + 22, mb->m_len - 22);
296 	for (mb = mb->m_next; mb != NULL; mb = mb->m_next)
297 		MD5Update(&md5, mtod(mb, void *), mb->m_len);
298 	MD5Final(digest, &md5);
299 
300 	/*
301 	 * Now verify the signature.
302 	 */
303 	if (bcmp(mtod(mdp->md_top, u_char *) + 14, digest, 8) != 0)
304 		return (EAUTH);
305 
306 	return (0);
307 }
308