1139823Simp/*-
275374Sbp * Copyright (c) 2000-2001, Boris Popov
375374Sbp * All rights reserved.
475374Sbp *
5124087Stjr * Copyright (c) 2003, 2004 Tim J. Robbins.
6124087Stjr * All rights reserved.
7124087Stjr *
875374Sbp * Redistribution and use in source and binary forms, with or without
975374Sbp * modification, are permitted provided that the following conditions
1075374Sbp * are met:
1175374Sbp * 1. Redistributions of source code must retain the above copyright
1275374Sbp *    notice, this list of conditions and the following disclaimer.
1375374Sbp * 2. Redistributions in binary form must reproduce the above copyright
1475374Sbp *    notice, this list of conditions and the following disclaimer in the
1575374Sbp *    documentation and/or other materials provided with the distribution.
1675374Sbp * 3. All advertising materials mentioning features or use of this software
1775374Sbp *    must display the following acknowledgement:
1875374Sbp *    This product includes software developed by Boris Popov.
1975374Sbp * 4. Neither the name of the author nor the names of any co-contributors
2075374Sbp *    may be used to endorse or promote products derived from this software
2175374Sbp *    without specific prior written permission.
2275374Sbp *
2375374Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2475374Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2575374Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2675374Sbp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2775374Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2875374Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2975374Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3075374Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3175374Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3275374Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3375374Sbp * SUCH DAMAGE.
3475374Sbp */
35116189Sobrien
36116189Sobrien#include <sys/cdefs.h>
37116189Sobrien__FBSDID("$FreeBSD$");
38116189Sobrien
3975374Sbp#include <sys/param.h>
4075374Sbp#include <sys/malloc.h>
4175374Sbp#include <sys/kernel.h>
4275374Sbp#include <sys/systm.h>
4375374Sbp#include <sys/conf.h>
4475374Sbp#include <sys/proc.h>
4575374Sbp#include <sys/fcntl.h>
4675374Sbp#include <sys/socket.h>
4775374Sbp#include <sys/socketvar.h>
4875374Sbp#include <sys/sysctl.h>
49124087Stjr#include <sys/endian.h>
50124087Stjr#include <sys/mbuf.h>
51124087Stjr#include <sys/mchain.h>
5275374Sbp#include <sys/md4.h>
53124087Stjr#include <sys/md5.h>
5475374Sbp#include <sys/iconv.h>
5575374Sbp
5675374Sbp#include <netsmb/smb.h>
5775374Sbp#include <netsmb/smb_conn.h>
5875374Sbp#include <netsmb/smb_subr.h>
59124087Stjr#include <netsmb/smb_rq.h>
6075374Sbp#include <netsmb/smb_dev.h>
6175374Sbp
62156326Syar#include <crypto/des/des.h>
63156326Syar
6475374Sbp#include "opt_netsmb.h"
6575374Sbp
6675374Sbpstatic u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
6775374Sbp
6875374Sbp
6975374Sbpstatic void
7075374Sbpsmb_E(const u_char *key, u_char *data, u_char *dest)
7175374Sbp{
7275374Sbp	des_key_schedule *ksp;
7375374Sbp	u_char kk[8];
7475374Sbp
7575374Sbp	kk[0] = key[0] & 0xfe;
7675374Sbp	kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
7775374Sbp	kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
7875374Sbp	kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
7975374Sbp	kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
8075374Sbp	kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
8175374Sbp	kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
8275374Sbp	kk[7] = key[6] << 1;
83111119Simp	ksp = malloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
8478064Sume	des_set_key((des_cblock *)kk, *ksp);
8578064Sume	des_ecb_encrypt((des_cblock *)data, (des_cblock *)dest, *ksp, 1);
8675374Sbp	free(ksp, M_SMBTEMP);
8775374Sbp}
8875374Sbp
8975374Sbp
9075374Sbpint
9175374Sbpsmb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
9275374Sbp{
9375374Sbp	u_char *p, *P14, *S21;
9475374Sbp
95111119Simp	p = malloc(14 + 21, M_SMBTEMP, M_WAITOK);
9675374Sbp	bzero(p, 14 + 21);
9775374Sbp	P14 = p;
9875374Sbp	S21 = p + 14;
9975374Sbp	bcopy(apwd, P14, min(14, strlen(apwd)));
10075374Sbp	/*
10175374Sbp	 * S21 = concat(Ex(P14, N8), zeros(5));
10275374Sbp	 */
10375374Sbp	smb_E(P14, N8, S21);
10475374Sbp	smb_E(P14 + 7, N8, S21 + 8);
10575374Sbp
10675374Sbp	smb_E(S21, C8, RN);
10775374Sbp	smb_E(S21 + 7, C8, RN + 8);
10875374Sbp	smb_E(S21 + 14, C8, RN + 16);
10975374Sbp	free(p, M_SMBTEMP);
11075374Sbp	return 0;
11175374Sbp}
11275374Sbp
11375374Sbpint
11475374Sbpsmb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
11575374Sbp{
11675374Sbp	u_char S21[21];
11775374Sbp	u_int16_t *unipwd;
11875374Sbp	MD4_CTX *ctxp;
11975374Sbp	int len;
12075374Sbp
12175374Sbp	len = strlen(apwd);
122111119Simp	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
12375374Sbp	/*
12475374Sbp	 * S21 = concat(MD4(U(apwd)), zeros(5));
12575374Sbp	 */
12675374Sbp	smb_strtouni(unipwd, apwd);
127111119Simp	ctxp = malloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
12875374Sbp	MD4Init(ctxp);
12975374Sbp	MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
13075374Sbp	free(unipwd, M_SMBTEMP);
13175374Sbp	bzero(S21, 21);
13275374Sbp	MD4Final(S21, ctxp);
13375374Sbp	free(ctxp, M_SMBTEMP);
13475374Sbp
13575374Sbp	smb_E(S21, C8, RN);
13675374Sbp	smb_E(S21 + 7, C8, RN + 8);
13775374Sbp	smb_E(S21 + 14, C8, RN + 16);
13875374Sbp	return 0;
13975374Sbp}
14075374Sbp
141124087Stjr/*
142124087Stjr * Calculate message authentication code (MAC) key for virtual circuit.
143124087Stjr */
144124087Stjrint
145124087Stjrsmb_calcmackey(struct smb_vc *vcp)
146124087Stjr{
147124087Stjr	const char *pwd;
148124087Stjr	u_int16_t *unipwd;
149124087Stjr	int len;
150124087Stjr	MD4_CTX md4;
151124087Stjr	u_char S16[16], S21[21];
152124087Stjr
153124087Stjr	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
154124087Stjr	    ("signatures not enabled"));
155124087Stjr
156124087Stjr	if (vcp->vc_mackey != NULL) {
157124087Stjr		free(vcp->vc_mackey, M_SMBTEMP);
158124087Stjr		vcp->vc_mackey = NULL;
159124087Stjr		vcp->vc_mackeylen = 0;
160124087Stjr		vcp->vc_seqno = 0;
161124087Stjr	}
162124087Stjr
163124087Stjr	/*
164124087Stjr	 * The partial MAC key is the concatenation of the 16 byte session
165124087Stjr	 * key and the 24 byte challenge response.
166124087Stjr	 */
167124087Stjr	vcp->vc_mackeylen = 16 + 24;
168124087Stjr	vcp->vc_mackey = malloc(vcp->vc_mackeylen, M_SMBTEMP, M_WAITOK);
169124087Stjr
170124087Stjr	/*
171124087Stjr	 * Calculate session key:
172124087Stjr	 *	MD4(MD4(U(PN)))
173124087Stjr	 */
174124087Stjr	pwd = smb_vc_getpass(vcp);
175124087Stjr	len = strlen(pwd);
176124087Stjr	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
177124087Stjr	smb_strtouni(unipwd, pwd);
178124087Stjr	MD4Init(&md4);
179124087Stjr	MD4Update(&md4, (u_char *)unipwd, len * sizeof(u_int16_t));
180124087Stjr	MD4Final(S16, &md4);
181124087Stjr	MD4Init(&md4);
182124087Stjr	MD4Update(&md4, S16, 16);
183124087Stjr	MD4Final(vcp->vc_mackey, &md4);
184124087Stjr	free(unipwd, M_SMBTEMP);
185124087Stjr
186124087Stjr	/*
187124087Stjr	 * Calculate response to challenge:
188124087Stjr	 *	Ex(concat(MD4(U(pass)), zeros(5)), C8)
189124087Stjr	 */
190124087Stjr	bzero(S21, 21);
191124087Stjr	bcopy(S16, S21, 16);
192124087Stjr	smb_E(S21, vcp->vc_ch, vcp->vc_mackey + 16);
193124087Stjr	smb_E(S21 + 7, vcp->vc_ch, vcp->vc_mackey + 24);
194124087Stjr	smb_E(S21 + 14, vcp->vc_ch, vcp->vc_mackey + 32);
195124087Stjr
196124087Stjr	return (0);
197124087Stjr}
198124087Stjr
199124087Stjr/*
200124087Stjr * Sign request with MAC.
201124087Stjr */
202124087Stjrint
203124087Stjrsmb_rq_sign(struct smb_rq *rqp)
204124087Stjr{
205124087Stjr	struct smb_vc *vcp = rqp->sr_vc;
206124087Stjr	struct mbchain *mbp;
207124087Stjr	struct mbuf *mb;
208124087Stjr	MD5_CTX md5;
209124087Stjr	u_char digest[16];
210124087Stjr
211124087Stjr	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
212124087Stjr	    ("signatures not enabled"));
213124087Stjr
214124087Stjr	if (vcp->vc_mackey == NULL)
215124087Stjr		/* XXX Should assert that cmd == SMB_COM_NEGOTIATE. */
216124087Stjr		return (0);
217124087Stjr
218124087Stjr	/*
219124087Stjr	 * This is a bit of a kludge. If the request is non-TRANSACTION,
220124087Stjr	 * or it is the first request of a transaction, give it the next
221124087Stjr	 * sequence number, and expect the reply to have the sequence number
222124087Stjr	 * following that one. Otherwise, it is a secondary request in
223124087Stjr	 * a transaction, and it gets the same sequence numbers as the
224124087Stjr	 * primary request.
225124087Stjr	 */
226124087Stjr	if (rqp->sr_t2 == NULL ||
227124087Stjr	    (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0) {
228124087Stjr		rqp->sr_seqno = vcp->vc_seqno++;
229124087Stjr		rqp->sr_rseqno = vcp->vc_seqno++;
230124087Stjr	} else {
231124087Stjr		/*
232124087Stjr		 * Sequence numbers are already in the struct because
233124087Stjr		 * smb_t2_request_int() uses the same one for all the
234124087Stjr		 * requests in the transaction.
235124087Stjr		 * (At least we hope so.)
236124087Stjr		 */
237124087Stjr		KASSERT(rqp->sr_t2 == NULL ||
238124087Stjr		    (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0 ||
239124087Stjr		    rqp->sr_t2->t2_rq == rqp,
240124087Stjr		    ("sec t2 rq not using same smb_rq"));
241124087Stjr	}
242124087Stjr
243124087Stjr	/* Initialize sec. signature field to sequence number + zeros. */
244161523Smarcel	le32enc(rqp->sr_rqsig, rqp->sr_seqno);
245161523Smarcel	le32enc(rqp->sr_rqsig + 4, 0);
246124087Stjr
247124087Stjr	/*
248124087Stjr	 * Compute HMAC-MD5 of packet data, keyed by MAC key.
249124087Stjr	 * Store the first 8 bytes in the sec. signature field.
250124087Stjr	 */
251124087Stjr	smb_rq_getrequest(rqp, &mbp);
252124087Stjr	MD5Init(&md5);
253124087Stjr	MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
254124087Stjr	for (mb = mbp->mb_top; mb != NULL; mb = mb->m_next)
255124087Stjr		MD5Update(&md5, mtod(mb, void *), mb->m_len);
256124087Stjr	MD5Final(digest, &md5);
257124087Stjr	bcopy(digest, rqp->sr_rqsig, 8);
258124087Stjr
259124087Stjr	return (0);
260124087Stjr}
261124087Stjr
262124087Stjr/*
263124087Stjr * Verify reply signature.
264124087Stjr */
265124087Stjrint
266124087Stjrsmb_rq_verify(struct smb_rq *rqp)
267124087Stjr{
268124087Stjr	struct smb_vc *vcp = rqp->sr_vc;
269124087Stjr	struct mdchain *mdp;
270124087Stjr	u_char sigbuf[8];
271124087Stjr	MD5_CTX md5;
272124087Stjr	u_char digest[16];
273124087Stjr	struct mbuf *mb;
274124087Stjr
275124087Stjr	KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
276124087Stjr	    ("signatures not enabled"));
277124087Stjr
278124087Stjr	if (vcp->vc_mackey == NULL)
279124087Stjr		/* XXX Should check that this is a SMB_COM_NEGOTIATE reply. */
280124087Stjr		return (0);
281124087Stjr
282124087Stjr	/*
283124087Stjr	 * Compute HMAC-MD5 of packet data, keyed by MAC key.
284124087Stjr	 * We play games to pretend the security signature field
285124087Stjr	 * contains their sequence number, to avoid modifying
286124087Stjr	 * the packet itself.
287124087Stjr	 */
288124087Stjr	smb_rq_getreply(rqp, &mdp);
289124087Stjr	mb = mdp->md_top;
290124087Stjr	KASSERT(mb->m_len >= SMB_HDRLEN, ("forgot to m_pullup"));
291124087Stjr	MD5Init(&md5);
292124087Stjr	MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
293124087Stjr	MD5Update(&md5, mtod(mb, void *), 14);
294124087Stjr	*(u_int32_t *)sigbuf = htole32(rqp->sr_rseqno);
295124087Stjr	*(u_int32_t *)(sigbuf + 4) = 0;
296124087Stjr	MD5Update(&md5, sigbuf, 8);
297124087Stjr	MD5Update(&md5, mtod(mb, u_char *) + 22, mb->m_len - 22);
298124087Stjr	for (mb = mb->m_next; mb != NULL; mb = mb->m_next)
299124087Stjr		MD5Update(&md5, mtod(mb, void *), mb->m_len);
300124087Stjr	MD5Final(digest, &md5);
301124087Stjr
302124087Stjr	/*
303124087Stjr	 * Now verify the signature.
304124087Stjr	 */
305124087Stjr	if (bcmp(mtod(mdp->md_top, u_char *) + 14, digest, 8) != 0)
306124087Stjr		return (EAUTH);
307124087Stjr
308124087Stjr	return (0);
309124087Stjr}
310