smb_crypt.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD$");
  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. #include <netsmb/smb.h>
  56. #include <netsmb/smb_conn.h>
  57. #include <netsmb/smb_subr.h>
  58. #include <netsmb/smb_rq.h>
  59. #include <netsmb/smb_dev.h>
  60. #include <crypto/des/des.h>
  61. #include "opt_netsmb.h"
  62. static u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
  63. static void
  64. smb_E(const u_char *key, u_char *data, u_char *dest)
  65. {
  66. des_key_schedule *ksp;
  67. u_char kk[8];
  68. kk[0] = key[0] & 0xfe;
  69. kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
  70. kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
  71. kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
  72. kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
  73. kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
  74. kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
  75. kk[7] = key[6] << 1;
  76. ksp = malloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
  77. des_set_key(kk, *ksp);
  78. des_ecb_encrypt(data, dest, *ksp, 1);
  79. free(ksp, M_SMBTEMP);
  80. }
  81. int
  82. smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
  83. {
  84. u_char *p, *P14, *S21;
  85. p = malloc(14 + 21, M_SMBTEMP, M_WAITOK);
  86. bzero(p, 14 + 21);
  87. P14 = p;
  88. S21 = p + 14;
  89. bcopy(apwd, P14, min(14, strlen(apwd)));
  90. /*
  91. * S21 = concat(Ex(P14, N8), zeros(5));
  92. */
  93. smb_E(P14, N8, S21);
  94. smb_E(P14 + 7, N8, S21 + 8);
  95. smb_E(S21, C8, RN);
  96. smb_E(S21 + 7, C8, RN + 8);
  97. smb_E(S21 + 14, C8, RN + 16);
  98. free(p, M_SMBTEMP);
  99. return 0;
  100. }
  101. int
  102. smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
  103. {
  104. u_char S21[21];
  105. u_int16_t *unipwd;
  106. MD4_CTX *ctxp;
  107. u_int len;
  108. len = strlen(apwd);
  109. unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
  110. /*
  111. * S21 = concat(MD4(U(apwd)), zeros(5));
  112. */
  113. smb_strtouni(unipwd, apwd);
  114. ctxp = malloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
  115. MD4Init(ctxp);
  116. MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
  117. free(unipwd, M_SMBTEMP);
  118. bzero(S21, 21);
  119. MD4Final(S21, ctxp);
  120. free(ctxp, M_SMBTEMP);
  121. smb_E(S21, C8, RN);
  122. smb_E(S21 + 7, C8, RN + 8);
  123. smb_E(S21 + 14, C8, RN + 16);
  124. return 0;
  125. }
  126. /*
  127. * Calculate message authentication code (MAC) key for virtual circuit.
  128. */
  129. int
  130. smb_calcmackey(struct smb_vc *vcp)
  131. {
  132. const char *pwd;
  133. u_int16_t *unipwd;
  134. u_int len;
  135. MD4_CTX md4;
  136. u_char S16[16], S21[21];
  137. KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
  138. ("signatures not enabled"));
  139. if (vcp->vc_mackey != NULL) {
  140. free(vcp->vc_mackey, M_SMBTEMP);
  141. vcp->vc_mackey = NULL;
  142. vcp->vc_mackeylen = 0;
  143. vcp->vc_seqno = 0;
  144. }
  145. /*
  146. * The partial MAC key is the concatenation of the 16 byte session
  147. * key and the 24 byte challenge response.
  148. */
  149. vcp->vc_mackeylen = 16 + 24;
  150. vcp->vc_mackey = malloc(vcp->vc_mackeylen, M_SMBTEMP, M_WAITOK);
  151. /*
  152. * Calculate session key:
  153. * MD4(MD4(U(PN)))
  154. */
  155. pwd = smb_vc_getpass(vcp);
  156. len = strlen(pwd);
  157. unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
  158. smb_strtouni(unipwd, pwd);
  159. MD4Init(&md4);
  160. MD4Update(&md4, (u_char *)unipwd, len * sizeof(u_int16_t));
  161. MD4Final(S16, &md4);
  162. MD4Init(&md4);
  163. MD4Update(&md4, S16, 16);
  164. MD4Final(vcp->vc_mackey, &md4);
  165. free(unipwd, M_SMBTEMP);
  166. /*
  167. * Calculate response to challenge:
  168. * Ex(concat(MD4(U(pass)), zeros(5)), C8)
  169. */
  170. bzero(S21, 21);
  171. bcopy(S16, S21, 16);
  172. smb_E(S21, vcp->vc_ch, vcp->vc_mackey + 16);
  173. smb_E(S21 + 7, vcp->vc_ch, vcp->vc_mackey + 24);
  174. smb_E(S21 + 14, vcp->vc_ch, vcp->vc_mackey + 32);
  175. return (0);
  176. }
  177. /*
  178. * Sign request with MAC.
  179. */
  180. int
  181. smb_rq_sign(struct smb_rq *rqp)
  182. {
  183. struct smb_vc *vcp = rqp->sr_vc;
  184. struct mbchain *mbp;
  185. struct mbuf *mb;
  186. MD5_CTX md5;
  187. u_char digest[16];
  188. KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
  189. ("signatures not enabled"));
  190. if (vcp->vc_mackey == NULL)
  191. /* XXX Should assert that cmd == SMB_COM_NEGOTIATE. */
  192. return (0);
  193. /*
  194. * This is a bit of a kludge. If the request is non-TRANSACTION,
  195. * or it is the first request of a transaction, give it the next
  196. * sequence number, and expect the reply to have the sequence number
  197. * following that one. Otherwise, it is a secondary request in
  198. * a transaction, and it gets the same sequence numbers as the
  199. * primary request.
  200. */
  201. if (rqp->sr_t2 == NULL ||
  202. (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0) {
  203. rqp->sr_seqno = vcp->vc_seqno++;
  204. rqp->sr_rseqno = vcp->vc_seqno++;
  205. } else {
  206. /*
  207. * Sequence numbers are already in the struct because
  208. * smb_t2_request_int() uses the same one for all the
  209. * requests in the transaction.
  210. * (At least we hope so.)
  211. */
  212. KASSERT(rqp->sr_t2 == NULL ||
  213. (rqp->sr_t2->t2_flags & SMBT2_SECONDARY) == 0 ||
  214. rqp->sr_t2->t2_rq == rqp,
  215. ("sec t2 rq not using same smb_rq"));
  216. }
  217. /* Initialize sec. signature field to sequence number + zeros. */
  218. le32enc(rqp->sr_rqsig, rqp->sr_seqno);
  219. le32enc(rqp->sr_rqsig + 4, 0);
  220. /*
  221. * Compute HMAC-MD5 of packet data, keyed by MAC key.
  222. * Store the first 8 bytes in the sec. signature field.
  223. */
  224. smb_rq_getrequest(rqp, &mbp);
  225. MD5Init(&md5);
  226. MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
  227. for (mb = mbp->mb_top; mb != NULL; mb = mb->m_next)
  228. MD5Update(&md5, mtod(mb, void *), mb->m_len);
  229. MD5Final(digest, &md5);
  230. bcopy(digest, rqp->sr_rqsig, 8);
  231. return (0);
  232. }
  233. /*
  234. * Verify reply signature.
  235. */
  236. int
  237. smb_rq_verify(struct smb_rq *rqp)
  238. {
  239. struct smb_vc *vcp = rqp->sr_vc;
  240. struct mdchain *mdp;
  241. u_char sigbuf[8];
  242. MD5_CTX md5;
  243. u_char digest[16];
  244. struct mbuf *mb;
  245. KASSERT(vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE,
  246. ("signatures not enabled"));
  247. if (vcp->vc_mackey == NULL)
  248. /* XXX Should check that this is a SMB_COM_NEGOTIATE reply. */
  249. return (0);
  250. /*
  251. * Compute HMAC-MD5 of packet data, keyed by MAC key.
  252. * We play games to pretend the security signature field
  253. * contains their sequence number, to avoid modifying
  254. * the packet itself.
  255. */
  256. smb_rq_getreply(rqp, &mdp);
  257. mb = mdp->md_top;
  258. KASSERT(mb->m_len >= SMB_HDRLEN, ("forgot to m_pullup"));
  259. MD5Init(&md5);
  260. MD5Update(&md5, vcp->vc_mackey, vcp->vc_mackeylen);
  261. MD5Update(&md5, mtod(mb, void *), 14);
  262. *(u_int32_t *)sigbuf = htole32(rqp->sr_rseqno);
  263. *(u_int32_t *)(sigbuf + 4) = 0;
  264. MD5Update(&md5, sigbuf, 8);
  265. MD5Update(&md5, mtod(mb, u_char *) + 22, mb->m_len - 22);
  266. for (mb = mb->m_next; mb != NULL; mb = mb->m_next)
  267. MD5Update(&md5, mtod(mb, void *), mb->m_len);
  268. MD5Final(digest, &md5);
  269. /*
  270. * Now verify the signature.
  271. */
  272. if (bcmp(mtod(mdp->md_top, u_char *) + 14, digest, 8) != 0)
  273. return (EAUTH);
  274. return (0);
  275. }