PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/sys/netsmb/smb_crypt.c

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