hammer2 - Fix flush issues with unmounted PFSs and shutdown panic
[dragonfly.git] / sys / netproto / smb / smb_crypt.c
bloba66a1ce3cdbc955ef1cad0722096eb4a60d480ee
1 /*
2 * Copyright (c) 2000-2001, Boris Popov
3 * All rights reserved.
5 * Copyright (c) 2003, 2004 Tim J. Robbins.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
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>
42 #include <sys/conf.h>
43 #include <sys/proc.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>
50 #include <sys/mbuf.h>
51 #include <sys/mchain.h>
53 #include <sys/md4.h>
54 #include <sys/md5.h>
55 #include <sys/iconv.h>
57 #include "smb.h"
58 #include "smb_conn.h"
59 #include "smb_subr.h"
60 #include "smb_rq.h"
61 #include "smb_dev.h"
63 #include "opt_netsmb.h"
65 #include <crypto/des/des.h>
67 static u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
70 static void
71 smb_E(const u_char *key, u_char *data, u_char *dest)
73 des_key_schedule *ksp;
74 u_char kk[8];
76 kk[0] = key[0] & 0xfe;
77 kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
78 kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
79 kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
80 kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
81 kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
82 kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
83 kk[7] = key[6] << 1;
84 ksp = kmalloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
85 des_set_key((des_cblock *)kk, *ksp);
86 des_ecb_encrypt((des_cblock *)data, (des_cblock *)dest, *ksp, 1);
87 kfree(ksp, M_SMBTEMP);
90 int
91 smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
93 u_char *p, *P14, *S21;
95 p = kmalloc(14 + 21, M_SMBTEMP, M_WAITOK | M_ZERO);
96 P14 = p;
97 S21 = p + 14;
98 bcopy(apwd, P14, min(14, strlen(apwd)));
100 * S21 = concat(Ex(P14, N8), zeros(5));
102 smb_E(P14, N8, S21);
103 smb_E(P14 + 7, N8, S21 + 8);
105 smb_E(S21, C8, RN);
106 smb_E(S21 + 7, C8, RN + 8);
107 smb_E(S21 + 14, C8, RN + 16);
108 kfree(p, M_SMBTEMP);
109 return 0;
113 smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
115 u_char S21[21];
116 u_int16_t *unipwd;
117 MD4_CTX *ctxp;
118 int len;
120 len = strlen(apwd);
121 unipwd = kmalloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
123 * S21 = concat(MD4(U(apwd)), zeros(5));
125 smb_strtouni(unipwd, apwd);
126 ctxp = kmalloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
127 MD4Init(ctxp);
128 MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
129 kfree(unipwd, M_SMBTEMP);
130 bzero(S21, 21);
131 MD4Final(S21, ctxp);
132 kfree(ctxp, M_SMBTEMP);
134 smb_E(S21, C8, RN);
135 smb_E(S21 + 7, C8, RN + 8);
136 smb_E(S21 + 14, C8, RN + 16);
137 return 0;
141 * Calculate message authentication code (MAC) key for virtual circuit.
144 smb_calcmackey(struct smb_vc *vcp)
146 const char *pwd;
147 u_int16_t *unipwd;
148 int len;
149 MD4_CTX md4;
150 u_char S16[16], S21[21];
152 KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
153 ("signatures not enabled"));
155 if (vcp->vc_mackey != NULL) {
156 kfree(vcp->vc_mackey, M_SMBTEMP);
157 vcp->vc_mackey = NULL;
158 vcp->vc_mackeylen = 0;
159 vcp->vc_seqno = 0;
163 * The partial MAC key is the concatenation of the 16 byte session
164 * key and the 24 byte challenge response.
166 vcp->vc_mackeylen = 16 + 24;
167 vcp->vc_mackey = kmalloc(vcp->vc_mackeylen, M_SMBTEMP, M_WAITOK);
170 * Calculate session key:
171 * MD4(MD4(U(PN)))
173 pwd = smb_vc_getpass(vcp);
174 len = strlen(pwd);
175 unipwd = kmalloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
176 smb_strtouni(unipwd, pwd);
177 MD4Init(&md4);
178 MD4Update(&md4, (u_char *)unipwd, len * sizeof(u_int16_t));
179 MD4Final(S16, &md4);
180 MD4Init(&md4);
181 MD4Update(&md4, S16, 16);
182 MD4Final(vcp->vc_mackey, &md4);
183 kfree(unipwd, M_SMBTEMP);
186 * Calculate response to challenge:
187 * Ex(concat(MD4(U(pass)), zeros(5)), C8)
189 bzero(S21, 21);
190 bcopy(S16, S21, 16);
191 smb_E(S21, vcp->vc_ch, vcp->vc_mackey + 16);
192 smb_E(S21 + 7, vcp->vc_ch, vcp->vc_mackey + 24);
193 smb_E(S21 + 14, vcp->vc_ch, vcp->vc_mackey + 32);
195 return (0);
199 * Sign request with MAC.
202 smb_rq_sign(struct smb_rq *rqp)
204 struct smb_vc *vcp = rqp->sr_vc;
205 struct mbchain *mbp;
206 struct mbuf *mb;
207 MD5_CTX md5;
208 u_char digest[16];
210 KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
211 ("signatures not enabled"));
213 if (vcp->vc_mackey == NULL)
214 /* XXX Should assert that cmd == SMB_COM_NEGOTIATE. */
215 return (0);
218 * This is a bit of a kludge. If the request is non-TRANSACTION,
219 * or it is the first request of a transaction, give it the next
220 * sequence number, and expect the reply to have the sequence number
221 * following that one. Otherwise, it is a secondary request in
222 * a transaction, and it gets the same sequence numbers as the
223 * primary request.
225 if (rqp->sr_t2 == NULL ||
226 (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0) {
227 rqp->sr_seqno = vcp->vc_seqno++;
228 rqp->sr_rseqno = vcp->vc_seqno++;
229 } else {
231 * Sequence numbers are already in the struct because
232 * smb_t2_request_int() uses the same one for all the
233 * requests in the transaction.
234 * (At least we hope so.)
236 KASSERT(rqp->sr_t2 == NULL ||
237 (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0 ||
238 rqp->sr_t2->t2_rq == rqp,
239 ("sec t2 rq not using same smb_rq"));
242 /* Initialize sec. signature field to sequence number + zeros. */
243 *(u_int32_t *)rqp->sr_rqsig = htole32(rqp->sr_seqno);
244 *(u_int32_t *)(rqp->sr_rqsig + 4) = 0;
247 * Compute HMAC-MD5 of packet data, keyed by MAC key.
248 * Store the first 8 bytes in the sec. signature field.
250 smb_rq_getrequest(rqp, &mbp);
251 MD5Init(&md5);
252 MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
253 for (mb = mbp->mb_top; mb != NULL; mb = mb->m_next)
254 MD5Update(&md5, mtod(mb, void *), mb->m_len);
255 MD5Final(digest, &md5);
256 bcopy(digest, rqp->sr_rqsig, 8);
258 return (0);
262 * Verify reply signature.
265 smb_rq_verify(struct smb_rq *rqp)
267 struct smb_vc *vcp = rqp->sr_vc;
268 struct mdchain *mdp;
269 u_char sigbuf[8];
270 MD5_CTX md5;
271 u_char digest[16];
272 struct mbuf *mb;
274 KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
275 ("signatures not enabled"));
277 if (vcp->vc_mackey == NULL)
278 /* XXX Should check that this is a SMB_COM_NEGOTIATE reply. */
279 return (0);
282 * Compute HMAC-MD5 of packet data, keyed by MAC key.
283 * We play games to pretend the security signature field
284 * contains their sequence number, to avoid modifying
285 * the packet itself.
287 smb_rq_getreply(rqp, &mdp);
288 mb = mdp->md_top;
289 KASSERT(mb->m_len >= SMB_HDRLEN, ("forgot to m_pullup"));
290 MD5Init(&md5);
291 MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
292 MD5Update(&md5, mtod(mb, void *), 14);
293 *(u_int32_t *)sigbuf = htole32(rqp->sr_rseqno);
294 *(u_int32_t *)(sigbuf + 4) = 0;
295 MD5Update(&md5, sigbuf, 8);
296 MD5Update(&md5, mtod(mb, u_char *) + 22, mb->m_len - 22);
297 for (mb = mb->m_next; mb != NULL; mb = mb->m_next)
298 MD5Update(&md5, mtod(mb, void *), mb->m_len);
299 MD5Final(digest, &md5);
302 * Now verify the signature.
304 if (bcmp(mtod(mdp->md_top, u_char *) + 14, digest, 8) != 0)
305 return (EAUTH);
307 return (0);