chap_ms.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * chap_ms.c - Microsoft MS-CHAP compatible implementation.
  3. *
  4. * Copyright (c) 1995 Eric Rosenquist. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. The name(s) of the authors of this software must not be used to
  19. * endorse or promote products derived from this software without
  20. * prior written permission.
  21. *
  22. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  23. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  25. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  26. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  27. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  28. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29. */
  30. /*
  31. * Modifications by Lauri Pesonen / lpesonen@clinet.fi, april 1997
  32. *
  33. * Implemented LANManager type password response to MS-CHAP challenges.
  34. * Now pppd provides both NT style and LANMan style blocks, and the
  35. * prefered is set by option "ms-lanman". Default is to use NT.
  36. * The hash text (StdText) was taken from Win95 RASAPI32.DLL.
  37. *
  38. * You should also use DOMAIN\\USERNAME as described in README.MSCHAP80
  39. */
  40. /*
  41. * Modifications by Frank Cusack, frank@google.com, March 2002.
  42. *
  43. * Implemented MS-CHAPv2 functionality, heavily based on sample
  44. * implementation in RFC 2759. Implemented MPPE functionality,
  45. * heavily based on sample implementation in RFC 3079.
  46. *
  47. * Copyright (c) 2002 Google, Inc. All rights reserved.
  48. *
  49. * Redistribution and use in source and binary forms, with or without
  50. * modification, are permitted provided that the following conditions
  51. * are met:
  52. *
  53. * 1. Redistributions of source code must retain the above copyright
  54. * notice, this list of conditions and the following disclaimer.
  55. *
  56. * 2. Redistributions in binary form must reproduce the above copyright
  57. * notice, this list of conditions and the following disclaimer in
  58. * the documentation and/or other materials provided with the
  59. * distribution.
  60. *
  61. * 3. The name(s) of the authors of this software must not be used to
  62. * endorse or promote products derived from this software without
  63. * prior written permission.
  64. *
  65. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  66. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  67. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  68. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  69. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  70. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  71. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  72. *
  73. */
  74. #define RCSID "$Id: chap_ms.c,v 1.38 2007/12/01 20:10:51 carlsonj Exp $"
  75. #ifdef CHAPMS
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include <ctype.h>
  80. #include <sys/types.h>
  81. #include <sys/time.h>
  82. #include <unistd.h>
  83. #include "pppd.h"
  84. #include "chap-new.h"
  85. #include "chap_ms.h"
  86. #include "md4.h"
  87. #include "sha1.h"
  88. #include "pppcrypt.h"
  89. #include "magic.h"
  90. static const char rcsid[] = RCSID;
  91. static void ascii2unicode __P((char[], int, u_char[]));
  92. static void NTPasswordHash __P((u_char *, int, u_char[MD4_SIGNATURE_SIZE]));
  93. static void ChallengeResponse __P((u_char *, u_char *, u_char[24]));
  94. static void ChapMS_NT __P((u_char *, char *, int, u_char[24]));
  95. static void ChapMS2_NT __P((u_char *, u_char[16], char *, char *, int,
  96. u_char[24]));
  97. static void GenerateAuthenticatorResponsePlain
  98. __P((char*, int, u_char[24], u_char[16], u_char *,
  99. char *, u_char[41]));
  100. #ifdef MSLANMAN
  101. static void ChapMS_LANMan __P((u_char *, char *, int, u_char *));
  102. #endif
  103. #ifdef MPPE
  104. static void Set_Start_Key __P((u_char *, char *, int));
  105. static void SetMasterKeys __P((char *, int, u_char[24], int));
  106. #endif
  107. #ifdef MSLANMAN
  108. bool ms_lanman = 0; /* Use LanMan password instead of NT */
  109. /* Has meaning only with MS-CHAP challenges */
  110. #endif
  111. #ifdef MPPE
  112. u_char mppe_send_key[MPPE_MAX_KEY_LEN];
  113. u_char mppe_recv_key[MPPE_MAX_KEY_LEN];
  114. int mppe_keys_set = 0; /* Have the MPPE keys been set? */
  115. #ifdef DEBUGMPPEKEY
  116. /* For MPPE debug */
  117. /* Use "[]|}{?/><,`!2&&(" (sans quotes) for RFC 3079 MS-CHAPv2 test value */
  118. static char *mschap_challenge = NULL;
  119. /* Use "!@\#$%^&*()_+:3|~" (sans quotes, backslash is to escape #) for ... */
  120. static char *mschap2_peer_challenge = NULL;
  121. #endif
  122. #include "fsm.h" /* Need to poke MPPE options */
  123. #include "ccp.h"
  124. #include <net/ppp-comp.h>
  125. #endif
  126. /*
  127. * Command-line options.
  128. */
  129. static option_t chapms_option_list[] = {
  130. #ifdef MSLANMAN
  131. { "ms-lanman", o_bool, &ms_lanman,
  132. "Use LanMan passwd when using MS-CHAP", 1 },
  133. #endif
  134. #ifdef DEBUGMPPEKEY
  135. { "mschap-challenge", o_string, &mschap_challenge,
  136. "specify CHAP challenge" },
  137. { "mschap2-peer-challenge", o_string, &mschap2_peer_challenge,
  138. "specify CHAP peer challenge" },
  139. #endif
  140. { NULL }
  141. };
  142. /*
  143. * chapms_generate_challenge - generate a challenge for MS-CHAP.
  144. * For MS-CHAP the challenge length is fixed at 8 bytes.
  145. * The length goes in challenge[0] and the actual challenge starts
  146. * at challenge[1].
  147. */
  148. static void
  149. chapms_generate_challenge(unsigned char *challenge)
  150. {
  151. *challenge++ = 8;
  152. #ifdef DEBUGMPPEKEY
  153. if (mschap_challenge && strlen(mschap_challenge) == 8)
  154. memcpy(challenge, mschap_challenge, 8);
  155. else
  156. #endif
  157. random_bytes(challenge, 8);
  158. }
  159. static void
  160. chapms2_generate_challenge(unsigned char *challenge)
  161. {
  162. *challenge++ = 16;
  163. #ifdef DEBUGMPPEKEY
  164. if (mschap_challenge && strlen(mschap_challenge) == 16)
  165. memcpy(challenge, mschap_challenge, 16);
  166. else
  167. #endif
  168. random_bytes(challenge, 16);
  169. }
  170. static int
  171. chapms_verify_response(int id, char *name,
  172. unsigned char *secret, int secret_len,
  173. unsigned char *challenge, unsigned char *response,
  174. char *message, int message_space)
  175. {
  176. unsigned char md[MS_CHAP_RESPONSE_LEN];
  177. int diff;
  178. int challenge_len, response_len;
  179. challenge_len = *challenge++; /* skip length, is 8 */
  180. response_len = *response++;
  181. if (response_len != MS_CHAP_RESPONSE_LEN)
  182. goto bad;
  183. #ifndef MSLANMAN
  184. if (!response[MS_CHAP_USENT]) {
  185. /* Should really propagate this into the error packet. */
  186. notice("Peer request for LANMAN auth not supported");
  187. goto bad;
  188. }
  189. #endif
  190. /* Generate the expected response. */
  191. ChapMS(challenge, (char *)secret, secret_len, md);
  192. #ifdef MSLANMAN
  193. /* Determine which part of response to verify against */
  194. if (!response[MS_CHAP_USENT])
  195. diff = memcmp(&response[MS_CHAP_LANMANRESP],
  196. &md[MS_CHAP_LANMANRESP], MS_CHAP_LANMANRESP_LEN);
  197. else
  198. #endif
  199. diff = memcmp(&response[MS_CHAP_NTRESP], &md[MS_CHAP_NTRESP],
  200. MS_CHAP_NTRESP_LEN);
  201. if (diff == 0) {
  202. slprintf(message, message_space, "Access granted");
  203. return 1;
  204. }
  205. bad:
  206. /* See comments below for MS-CHAP V2 */
  207. slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0",
  208. challenge_len, challenge);
  209. return 0;
  210. }
  211. static int
  212. chapms2_verify_response(int id, char *name,
  213. unsigned char *secret, int secret_len,
  214. unsigned char *challenge, unsigned char *response,
  215. char *message, int message_space)
  216. {
  217. unsigned char md[MS_CHAP2_RESPONSE_LEN];
  218. char saresponse[MS_AUTH_RESPONSE_LENGTH+1];
  219. int challenge_len, response_len;
  220. challenge_len = *challenge++; /* skip length, is 16 */
  221. response_len = *response++;
  222. if (response_len != MS_CHAP2_RESPONSE_LEN)
  223. goto bad; /* not even the right length */
  224. /* Generate the expected response and our mutual auth. */
  225. ChapMS2(challenge, &response[MS_CHAP2_PEER_CHALLENGE], name,
  226. (char *)secret, secret_len, md,
  227. (unsigned char *)saresponse, MS_CHAP2_AUTHENTICATOR);
  228. /* compare MDs and send the appropriate status */
  229. /*
  230. * Per RFC 2759, success message must be formatted as
  231. * "S=<auth_string> M=<message>"
  232. * where
  233. * <auth_string> is the Authenticator Response (mutual auth)
  234. * <message> is a text message
  235. *
  236. * However, some versions of Windows (win98 tested) do not know
  237. * about the M=<message> part (required per RFC 2759) and flag
  238. * it as an error (reported incorrectly as an encryption error
  239. * to the user). Since the RFC requires it, and it can be
  240. * useful information, we supply it if the peer is a conforming
  241. * system. Luckily (?), win98 sets the Flags field to 0x04
  242. * (contrary to RFC requirements) so we can use that to
  243. * distinguish between conforming and non-conforming systems.
  244. *
  245. * Special thanks to Alex Swiridov <say@real.kharkov.ua> for
  246. * help debugging this.
  247. */
  248. if (memcmp(&md[MS_CHAP2_NTRESP], &response[MS_CHAP2_NTRESP],
  249. MS_CHAP2_NTRESP_LEN) == 0) {
  250. if (response[MS_CHAP2_FLAGS])
  251. slprintf(message, message_space, "S=%s", saresponse);
  252. else
  253. slprintf(message, message_space, "S=%s M=%s",
  254. saresponse, "Access granted");
  255. return 1;
  256. }
  257. bad:
  258. /*
  259. * Failure message must be formatted as
  260. * "E=e R=r C=c V=v M=m"
  261. * where
  262. * e = error code (we use 691, ERROR_AUTHENTICATION_FAILURE)
  263. * r = retry (we use 1, ok to retry)
  264. * c = challenge to use for next response, we reuse previous
  265. * v = Change Password version supported, we use 0
  266. * m = text message
  267. *
  268. * The M=m part is only for MS-CHAPv2. Neither win2k nor
  269. * win98 (others untested) display the message to the user anyway.
  270. * They also both ignore the E=e code.
  271. *
  272. * Note that it's safe to reuse the same challenge as we don't
  273. * actually accept another response based on the error message
  274. * (and no clients try to resend a response anyway).
  275. *
  276. * Basically, this whole bit is useless code, even the small
  277. * implementation here is only because of overspecification.
  278. */
  279. slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0 M=%s",
  280. challenge_len, challenge, "Access denied");
  281. return 0;
  282. }
  283. static void
  284. chapms_make_response(unsigned char *response, int id, char *our_name,
  285. unsigned char *challenge, char *secret, int secret_len,
  286. unsigned char *private)
  287. {
  288. challenge++; /* skip length, should be 8 */
  289. *response++ = MS_CHAP_RESPONSE_LEN;
  290. ChapMS(challenge, secret, secret_len, response);
  291. }
  292. struct chapms2_response_cache_entry {
  293. int id;
  294. unsigned char challenge[16];
  295. unsigned char response[MS_CHAP2_RESPONSE_LEN];
  296. unsigned char auth_response[MS_AUTH_RESPONSE_LENGTH];
  297. };
  298. #define CHAPMS2_MAX_RESPONSE_CACHE_SIZE 10
  299. static struct chapms2_response_cache_entry
  300. chapms2_response_cache[CHAPMS2_MAX_RESPONSE_CACHE_SIZE];
  301. static int chapms2_response_cache_next_index = 0;
  302. static int chapms2_response_cache_size = 0;
  303. static void
  304. chapms2_add_to_response_cache(int id, unsigned char *challenge,
  305. unsigned char *response,
  306. unsigned char *auth_response)
  307. {
  308. int i = chapms2_response_cache_next_index;
  309. chapms2_response_cache[i].id = id;
  310. memcpy(chapms2_response_cache[i].challenge, challenge, 16);
  311. memcpy(chapms2_response_cache[i].response, response,
  312. MS_CHAP2_RESPONSE_LEN);
  313. memcpy(chapms2_response_cache[i].auth_response,
  314. auth_response, MS_AUTH_RESPONSE_LENGTH);
  315. chapms2_response_cache_next_index =
  316. (i + 1) % CHAPMS2_MAX_RESPONSE_CACHE_SIZE;
  317. if (chapms2_response_cache_next_index > chapms2_response_cache_size)
  318. chapms2_response_cache_size = chapms2_response_cache_next_index;
  319. dbglog("added response cache entry %d", i);
  320. }
  321. static struct chapms2_response_cache_entry*
  322. chapms2_find_in_response_cache(int id, unsigned char *challenge,
  323. unsigned char *auth_response)
  324. {
  325. int i;
  326. for (i = 0; i < chapms2_response_cache_size; i++) {
  327. if (id == chapms2_response_cache[i].id
  328. && (!challenge
  329. || memcmp(challenge,
  330. chapms2_response_cache[i].challenge,
  331. 16) == 0)
  332. && (!auth_response
  333. || memcmp(auth_response,
  334. chapms2_response_cache[i].auth_response,
  335. MS_AUTH_RESPONSE_LENGTH) == 0)) {
  336. dbglog("response found in cache (entry %d)", i);
  337. return &chapms2_response_cache[i];
  338. }
  339. }
  340. return NULL; /* not found */
  341. }
  342. static void
  343. chapms2_make_response(unsigned char *response, int id, char *our_name,
  344. unsigned char *challenge, char *secret, int secret_len,
  345. unsigned char *private)
  346. {
  347. const struct chapms2_response_cache_entry *cache_entry;
  348. unsigned char auth_response[MS_AUTH_RESPONSE_LENGTH+1];
  349. challenge++; /* skip length, should be 16 */
  350. *response++ = MS_CHAP2_RESPONSE_LEN;
  351. cache_entry = chapms2_find_in_response_cache(id, challenge, NULL);
  352. if (cache_entry) {
  353. memcpy(response, cache_entry->response, MS_CHAP2_RESPONSE_LEN);
  354. return;
  355. }
  356. ChapMS2(challenge,
  357. #ifdef DEBUGMPPEKEY
  358. mschap2_peer_challenge,
  359. #else
  360. NULL,
  361. #endif
  362. our_name, secret, secret_len, response, auth_response,
  363. MS_CHAP2_AUTHENTICATEE);
  364. chapms2_add_to_response_cache(id, challenge, response, auth_response);
  365. }
  366. static int
  367. chapms2_check_success(int id, unsigned char *msg, int len)
  368. {
  369. if ((len < MS_AUTH_RESPONSE_LENGTH + 2) ||
  370. strncmp((char *)msg, "S=", 2) != 0) {
  371. /* Packet does not start with "S=" */
  372. error("MS-CHAPv2 Success packet is badly formed.");
  373. return 0;
  374. }
  375. msg += 2;
  376. len -= 2;
  377. if (len < MS_AUTH_RESPONSE_LENGTH
  378. || !chapms2_find_in_response_cache(id, NULL /* challenge */, msg)) {
  379. /* Authenticator Response did not match expected. */
  380. error("MS-CHAPv2 mutual authentication failed.");
  381. return 0;
  382. }
  383. /* Authenticator Response matches. */
  384. msg += MS_AUTH_RESPONSE_LENGTH; /* Eat it */
  385. len -= MS_AUTH_RESPONSE_LENGTH;
  386. if ((len >= 3) && !strncmp((char *)msg, " M=", 3)) {
  387. msg += 3; /* Eat the delimiter */
  388. } else if (len) {
  389. /* Packet has extra text which does not begin " M=" */
  390. error("MS-CHAPv2 Success packet is badly formed.");
  391. return 0;
  392. }
  393. return 1;
  394. }
  395. static void
  396. chapms_handle_failure(unsigned char *inp, int len)
  397. {
  398. int err;
  399. char *p, *msg;
  400. /* We want a null-terminated string for strxxx(). */
  401. msg = malloc(len + 1);
  402. if (!msg) {
  403. notice("Out of memory in chapms_handle_failure");
  404. return;
  405. }
  406. BCOPY(inp, msg, len);
  407. msg[len] = 0;
  408. p = msg;
  409. /*
  410. * Deal with MS-CHAP formatted failure messages; just print the
  411. * M=<message> part (if any). For MS-CHAP we're not really supposed
  412. * to use M=<message>, but it shouldn't hurt. See
  413. * chapms[2]_verify_response.
  414. */
  415. if (!strncmp(p, "E=", 2))
  416. err = strtol(p+2, NULL, 10); /* Remember the error code. */
  417. else
  418. goto print_msg; /* Message is badly formatted. */
  419. if (len && ((p = strstr(p, " M=")) != NULL)) {
  420. /* M=<message> field found. */
  421. p += 3;
  422. } else {
  423. /* No M=<message>; use the error code. */
  424. switch (err) {
  425. case MS_CHAP_ERROR_RESTRICTED_LOGON_HOURS:
  426. p = "E=646 Restricted logon hours";
  427. break;
  428. case MS_CHAP_ERROR_ACCT_DISABLED:
  429. p = "E=647 Account disabled";
  430. break;
  431. case MS_CHAP_ERROR_PASSWD_EXPIRED:
  432. p = "E=648 Password expired";
  433. break;
  434. case MS_CHAP_ERROR_NO_DIALIN_PERMISSION:
  435. p = "E=649 No dialin permission";
  436. break;
  437. case MS_CHAP_ERROR_AUTHENTICATION_FAILURE:
  438. p = "E=691 Authentication failure";
  439. break;
  440. case MS_CHAP_ERROR_CHANGING_PASSWORD:
  441. /* Should never see this, we don't support Change Password. */
  442. p = "E=709 Error changing password";
  443. break;
  444. default:
  445. free(msg);
  446. error("Unknown MS-CHAP authentication failure: %.*v",
  447. len, inp);
  448. return;
  449. }
  450. }
  451. print_msg:
  452. if (p != NULL)
  453. error("MS-CHAP authentication failed: %v", p);
  454. free(msg);
  455. }
  456. static void
  457. ChallengeResponse(u_char *challenge,
  458. u_char PasswordHash[MD4_SIGNATURE_SIZE],
  459. u_char response[24])
  460. {
  461. u_char ZPasswordHash[21];
  462. BZERO(ZPasswordHash, sizeof(ZPasswordHash));
  463. BCOPY(PasswordHash, ZPasswordHash, MD4_SIGNATURE_SIZE);
  464. #if 0
  465. dbglog("ChallengeResponse - ZPasswordHash %.*B",
  466. sizeof(ZPasswordHash), ZPasswordHash);
  467. #endif
  468. (void) DesSetkey(ZPasswordHash + 0);
  469. DesEncrypt(challenge, response + 0);
  470. (void) DesSetkey(ZPasswordHash + 7);
  471. DesEncrypt(challenge, response + 8);
  472. (void) DesSetkey(ZPasswordHash + 14);
  473. DesEncrypt(challenge, response + 16);
  474. #if 0
  475. dbglog("ChallengeResponse - response %.24B", response);
  476. #endif
  477. }
  478. void
  479. ChallengeHash(u_char PeerChallenge[16], u_char *rchallenge,
  480. char *username, u_char Challenge[8])
  481. {
  482. SHA1_CTX sha1Context;
  483. u_char sha1Hash[SHA1_SIGNATURE_SIZE];
  484. char *user;
  485. /* remove domain from "domain\username" */
  486. if ((user = strrchr(username, '\\')) != NULL)
  487. ++user;
  488. else
  489. user = username;
  490. SHA1_Init(&sha1Context);
  491. SHA1_Update(&sha1Context, PeerChallenge, 16);
  492. SHA1_Update(&sha1Context, rchallenge, 16);
  493. SHA1_Update(&sha1Context, (unsigned char *)user, strlen(user));
  494. SHA1_Final(sha1Hash, &sha1Context);
  495. BCOPY(sha1Hash, Challenge, 8);
  496. }
  497. /*
  498. * Convert the ASCII version of the password to Unicode.
  499. * This implicitly supports 8-bit ISO8859/1 characters.
  500. * This gives us the little-endian representation, which
  501. * is assumed by all M$ CHAP RFCs. (Unicode byte ordering
  502. * is machine-dependent.)
  503. */
  504. static void
  505. ascii2unicode(char ascii[], int ascii_len, u_char unicode[])
  506. {
  507. int i;
  508. BZERO(unicode, ascii_len * 2);
  509. for (i = 0; i < ascii_len; i++)
  510. unicode[i * 2] = (u_char) ascii[i];
  511. }
  512. static void
  513. NTPasswordHash(u_char *secret, int secret_len, u_char hash[MD4_SIGNATURE_SIZE])
  514. {
  515. #ifdef __NetBSD__
  516. /* NetBSD uses the libc md4 routines which take bytes instead of bits */
  517. int mdlen = secret_len;
  518. #else
  519. int mdlen = secret_len * 8;
  520. #endif
  521. MD4_CTX md4Context;
  522. MD4Init(&md4Context);
  523. /* MD4Update can take at most 64 bytes at a time */
  524. while (mdlen > 512) {
  525. MD4Update(&md4Context, secret, 512);
  526. secret += 64;
  527. mdlen -= 512;
  528. }
  529. MD4Update(&md4Context, secret, mdlen);
  530. MD4Final(hash, &md4Context);
  531. }
  532. static void
  533. ChapMS_NT(u_char *rchallenge, char *secret, int secret_len,
  534. u_char NTResponse[24])
  535. {
  536. u_char unicodePassword[MAX_NT_PASSWORD * 2];
  537. u_char PasswordHash[MD4_SIGNATURE_SIZE];
  538. /* Hash the Unicode version of the secret (== password). */
  539. ascii2unicode(secret, secret_len, unicodePassword);
  540. NTPasswordHash(unicodePassword, secret_len * 2, PasswordHash);
  541. ChallengeResponse(rchallenge, PasswordHash, NTResponse);
  542. }
  543. static void
  544. ChapMS2_NT(u_char *rchallenge, u_char PeerChallenge[16], char *username,
  545. char *secret, int secret_len, u_char NTResponse[24])
  546. {
  547. u_char unicodePassword[MAX_NT_PASSWORD * 2];
  548. u_char PasswordHash[MD4_SIGNATURE_SIZE];
  549. u_char Challenge[8];
  550. ChallengeHash(PeerChallenge, rchallenge, username, Challenge);
  551. /* Hash the Unicode version of the secret (== password). */
  552. ascii2unicode(secret, secret_len, unicodePassword);
  553. NTPasswordHash(unicodePassword, secret_len * 2, PasswordHash);
  554. ChallengeResponse(Challenge, PasswordHash, NTResponse);
  555. }
  556. #ifdef MSLANMAN
  557. static u_char *StdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */
  558. static void
  559. ChapMS_LANMan(u_char *rchallenge, char *secret, int secret_len,
  560. unsigned char *response)
  561. {
  562. int i;
  563. u_char UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */
  564. u_char PasswordHash[MD4_SIGNATURE_SIZE];
  565. /* LANMan password is case insensitive */
  566. BZERO(UcasePassword, sizeof(UcasePassword));
  567. for (i = 0; i < secret_len; i++)
  568. UcasePassword[i] = (u_char)toupper(secret[i]);
  569. (void) DesSetkey(UcasePassword + 0);
  570. DesEncrypt( StdText, PasswordHash + 0 );
  571. (void) DesSetkey(UcasePassword + 7);
  572. DesEncrypt( StdText, PasswordHash + 8 );
  573. ChallengeResponse(rchallenge, PasswordHash, &response[MS_CHAP_LANMANRESP]);
  574. }
  575. #endif
  576. void
  577. GenerateAuthenticatorResponse(u_char PasswordHashHash[MD4_SIGNATURE_SIZE],
  578. u_char NTResponse[24], u_char PeerChallenge[16],
  579. u_char *rchallenge, char *username,
  580. u_char authResponse[MS_AUTH_RESPONSE_LENGTH+1])
  581. {
  582. /*
  583. * "Magic" constants used in response generation, from RFC 2759.
  584. */
  585. u_char Magic1[39] = /* "Magic server to client signing constant" */
  586. { 0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76,
  587. 0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65,
  588. 0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67,
  589. 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74 };
  590. u_char Magic2[41] = /* "Pad to make it do more than one iteration" */
  591. { 0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B,
  592. 0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F,
  593. 0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E,
  594. 0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F,
  595. 0x6E };
  596. int i;
  597. SHA1_CTX sha1Context;
  598. u_char Digest[SHA1_SIGNATURE_SIZE];
  599. u_char Challenge[8];
  600. SHA1_Init(&sha1Context);
  601. SHA1_Update(&sha1Context, PasswordHashHash, MD4_SIGNATURE_SIZE);
  602. SHA1_Update(&sha1Context, NTResponse, 24);
  603. SHA1_Update(&sha1Context, Magic1, sizeof(Magic1));
  604. SHA1_Final(Digest, &sha1Context);
  605. ChallengeHash(PeerChallenge, rchallenge, username, Challenge);
  606. SHA1_Init(&sha1Context);
  607. SHA1_Update(&sha1Context, Digest, sizeof(Digest));
  608. SHA1_Update(&sha1Context, Challenge, sizeof(Challenge));
  609. SHA1_Update(&sha1Context, Magic2, sizeof(Magic2));
  610. SHA1_Final(Digest, &sha1Context);
  611. /* Convert to ASCII hex string. */
  612. for (i = 0; i < MAX((MS_AUTH_RESPONSE_LENGTH / 2), sizeof(Digest)); i++)
  613. sprintf((char *)&authResponse[i * 2], "%02X", Digest[i]);
  614. }
  615. static void
  616. GenerateAuthenticatorResponsePlain
  617. (char *secret, int secret_len,
  618. u_char NTResponse[24], u_char PeerChallenge[16],
  619. u_char *rchallenge, char *username,
  620. u_char authResponse[MS_AUTH_RESPONSE_LENGTH+1])
  621. {
  622. u_char unicodePassword[MAX_NT_PASSWORD * 2];
  623. u_char PasswordHash[MD4_SIGNATURE_SIZE];
  624. u_char PasswordHashHash[MD4_SIGNATURE_SIZE];
  625. /* Hash (x2) the Unicode version of the secret (== password). */
  626. ascii2unicode(secret, secret_len, unicodePassword);
  627. NTPasswordHash(unicodePassword, secret_len * 2, PasswordHash);
  628. NTPasswordHash(PasswordHash, sizeof(PasswordHash),
  629. PasswordHashHash);
  630. GenerateAuthenticatorResponse(PasswordHashHash, NTResponse, PeerChallenge,
  631. rchallenge, username, authResponse);
  632. }
  633. #ifdef MPPE
  634. /*
  635. * Set mppe_xxxx_key from the NTPasswordHashHash.
  636. * RFC 2548 (RADIUS support) requires us to export this function (ugh).
  637. */
  638. void
  639. mppe_set_keys(u_char *rchallenge, u_char PasswordHashHash[MD4_SIGNATURE_SIZE])
  640. {
  641. SHA1_CTX sha1Context;
  642. u_char Digest[SHA1_SIGNATURE_SIZE]; /* >= MPPE_MAX_KEY_LEN */
  643. SHA1_Init(&sha1Context);
  644. SHA1_Update(&sha1Context, PasswordHashHash, MD4_SIGNATURE_SIZE);
  645. SHA1_Update(&sha1Context, PasswordHashHash, MD4_SIGNATURE_SIZE);
  646. SHA1_Update(&sha1Context, rchallenge, 8);
  647. SHA1_Final(Digest, &sha1Context);
  648. /* Same key in both directions. */
  649. BCOPY(Digest, mppe_send_key, sizeof(mppe_send_key));
  650. BCOPY(Digest, mppe_recv_key, sizeof(mppe_recv_key));
  651. mppe_keys_set = 1;
  652. }
  653. /*
  654. * Set mppe_xxxx_key from MS-CHAP credentials. (see RFC 3079)
  655. */
  656. static void
  657. Set_Start_Key(u_char *rchallenge, char *secret, int secret_len)
  658. {
  659. u_char unicodePassword[MAX_NT_PASSWORD * 2];
  660. u_char PasswordHash[MD4_SIGNATURE_SIZE];
  661. u_char PasswordHashHash[MD4_SIGNATURE_SIZE];
  662. /* Hash (x2) the Unicode version of the secret (== password). */
  663. ascii2unicode(secret, secret_len, unicodePassword);
  664. NTPasswordHash(unicodePassword, secret_len * 2, PasswordHash);
  665. NTPasswordHash(PasswordHash, sizeof(PasswordHash), PasswordHashHash);
  666. mppe_set_keys(rchallenge, PasswordHashHash);
  667. }
  668. /*
  669. * Set mppe_xxxx_key from MS-CHAPv2 credentials. (see RFC 3079)
  670. *
  671. * This helper function used in the Winbind module, which gets the
  672. * NTHashHash from the server.
  673. */
  674. void
  675. mppe_set_keys2(u_char PasswordHashHash[MD4_SIGNATURE_SIZE],
  676. u_char NTResponse[24], int IsServer)
  677. {
  678. SHA1_CTX sha1Context;
  679. u_char MasterKey[SHA1_SIGNATURE_SIZE]; /* >= MPPE_MAX_KEY_LEN */
  680. u_char Digest[SHA1_SIGNATURE_SIZE]; /* >= MPPE_MAX_KEY_LEN */
  681. u_char SHApad1[40] =
  682. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  683. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  684. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  685. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  686. u_char SHApad2[40] =
  687. { 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
  688. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
  689. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
  690. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2 };
  691. /* "This is the MPPE Master Key" */
  692. u_char Magic1[27] =
  693. { 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
  694. 0x68, 0x65, 0x20, 0x4d, 0x50, 0x50, 0x45, 0x20, 0x4d,
  695. 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x4b, 0x65, 0x79 };
  696. /* "On the client side, this is the send key; "
  697. "on the server side, it is the receive key." */
  698. u_char Magic2[84] =
  699. { 0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
  700. 0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
  701. 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
  702. 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x6b, 0x65, 0x79,
  703. 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
  704. 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x64, 0x65,
  705. 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
  706. 0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
  707. 0x6b, 0x65, 0x79, 0x2e };
  708. /* "On the client side, this is the receive key; "
  709. "on the server side, it is the send key." */
  710. u_char Magic3[84] =
  711. { 0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
  712. 0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
  713. 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
  714. 0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
  715. 0x6b, 0x65, 0x79, 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68,
  716. 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73,
  717. 0x69, 0x64, 0x65, 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73,
  718. 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20,
  719. 0x6b, 0x65, 0x79, 0x2e };
  720. u_char *s;
  721. SHA1_Init(&sha1Context);
  722. SHA1_Update(&sha1Context, PasswordHashHash, MD4_SIGNATURE_SIZE);
  723. SHA1_Update(&sha1Context, NTResponse, 24);
  724. SHA1_Update(&sha1Context, Magic1, sizeof(Magic1));
  725. SHA1_Final(MasterKey, &sha1Context);
  726. /*
  727. * generate send key
  728. */
  729. if (IsServer)
  730. s = Magic3;
  731. else
  732. s = Magic2;
  733. SHA1_Init(&sha1Context);
  734. SHA1_Update(&sha1Context, MasterKey, 16);
  735. SHA1_Update(&sha1Context, SHApad1, sizeof(SHApad1));
  736. SHA1_Update(&sha1Context, s, 84);
  737. SHA1_Update(&sha1Context, SHApad2, sizeof(SHApad2));
  738. SHA1_Final(Digest, &sha1Context);
  739. BCOPY(Digest, mppe_send_key, sizeof(mppe_send_key));
  740. /*
  741. * generate recv key
  742. */
  743. if (IsServer)
  744. s = Magic2;
  745. else
  746. s = Magic3;
  747. SHA1_Init(&sha1Context);
  748. SHA1_Update(&sha1Context, MasterKey, 16);
  749. SHA1_Update(&sha1Context, SHApad1, sizeof(SHApad1));
  750. SHA1_Update(&sha1Context, s, 84);
  751. SHA1_Update(&sha1Context, SHApad2, sizeof(SHApad2));
  752. SHA1_Final(Digest, &sha1Context);
  753. BCOPY(Digest, mppe_recv_key, sizeof(mppe_recv_key));
  754. mppe_keys_set = 1;
  755. }
  756. /*
  757. * Set mppe_xxxx_key from MS-CHAPv2 credentials. (see RFC 3079)
  758. */
  759. static void
  760. SetMasterKeys(char *secret, int secret_len, u_char NTResponse[24], int IsServer)
  761. {
  762. u_char unicodePassword[MAX_NT_PASSWORD * 2];
  763. u_char PasswordHash[MD4_SIGNATURE_SIZE];
  764. u_char PasswordHashHash[MD4_SIGNATURE_SIZE];
  765. /* Hash (x2) the Unicode version of the secret (== password). */
  766. ascii2unicode(secret, secret_len, unicodePassword);
  767. NTPasswordHash(unicodePassword, secret_len * 2, PasswordHash);
  768. NTPasswordHash(PasswordHash, sizeof(PasswordHash), PasswordHashHash);
  769. mppe_set_keys2(PasswordHashHash, NTResponse, IsServer);
  770. }
  771. #endif /* MPPE */
  772. void
  773. ChapMS(u_char *rchallenge, char *secret, int secret_len,
  774. unsigned char *response)
  775. {
  776. BZERO(response, MS_CHAP_RESPONSE_LEN);
  777. ChapMS_NT(rchallenge, secret, secret_len, &response[MS_CHAP_NTRESP]);
  778. #ifdef MSLANMAN
  779. ChapMS_LANMan(rchallenge, secret, secret_len,
  780. &response[MS_CHAP_LANMANRESP]);
  781. /* preferred method is set by option */
  782. response[MS_CHAP_USENT] = !ms_lanman;
  783. #else
  784. response[MS_CHAP_USENT] = 1;
  785. #endif
  786. #ifdef MPPE
  787. Set_Start_Key(rchallenge, secret, secret_len);
  788. #endif
  789. }
  790. /*
  791. * If PeerChallenge is NULL, one is generated and the PeerChallenge
  792. * field of response is filled in. Call this way when generating a response.
  793. * If PeerChallenge is supplied, it is copied into the PeerChallenge field.
  794. * Call this way when verifying a response (or debugging).
  795. * Do not call with PeerChallenge = response.
  796. *
  797. * The PeerChallenge field of response is then used for calculation of the
  798. * Authenticator Response.
  799. */
  800. void
  801. ChapMS2(u_char *rchallenge, u_char *PeerChallenge,
  802. char *user, char *secret, int secret_len, unsigned char *response,
  803. u_char authResponse[], int authenticator)
  804. {
  805. /* ARGSUSED */
  806. u_char *p = &response[MS_CHAP2_PEER_CHALLENGE];
  807. int i;
  808. BZERO(response, MS_CHAP2_RESPONSE_LEN);
  809. /* Generate the Peer-Challenge if requested, or copy it if supplied. */
  810. if (!PeerChallenge)
  811. for (i = 0; i < MS_CHAP2_PEER_CHAL_LEN; i++)
  812. *p++ = (u_char) (drand48() * 0xff);
  813. else
  814. BCOPY(PeerChallenge, &response[MS_CHAP2_PEER_CHALLENGE],
  815. MS_CHAP2_PEER_CHAL_LEN);
  816. /* Generate the NT-Response */
  817. ChapMS2_NT(rchallenge, &response[MS_CHAP2_PEER_CHALLENGE], user,
  818. secret, secret_len, &response[MS_CHAP2_NTRESP]);
  819. /* Generate the Authenticator Response. */
  820. GenerateAuthenticatorResponsePlain(secret, secret_len,
  821. &response[MS_CHAP2_NTRESP],
  822. &response[MS_CHAP2_PEER_CHALLENGE],
  823. rchallenge, user, authResponse);
  824. #ifdef MPPE
  825. SetMasterKeys(secret, secret_len,
  826. &response[MS_CHAP2_NTRESP], authenticator);
  827. #endif
  828. }
  829. #ifdef MPPE
  830. /*
  831. * Set MPPE options from plugins.
  832. */
  833. void
  834. set_mppe_enc_types(int policy, int types)
  835. {
  836. /* Early exit for unknown policies. */
  837. if (policy != MPPE_ENC_POL_ENC_ALLOWED ||
  838. policy != MPPE_ENC_POL_ENC_REQUIRED)
  839. return;
  840. /* Don't modify MPPE if it's optional and wasn't already configured. */
  841. if (policy == MPPE_ENC_POL_ENC_ALLOWED && !ccp_wantoptions[0].mppe)
  842. return;
  843. /*
  844. * Disable undesirable encryption types. Note that we don't ENABLE
  845. * any encryption types, to avoid overriding manual configuration.
  846. */
  847. switch(types) {
  848. case MPPE_ENC_TYPES_RC4_40:
  849. ccp_wantoptions[0].mppe &= ~MPPE_OPT_128; /* disable 128-bit */
  850. break;
  851. case MPPE_ENC_TYPES_RC4_128:
  852. ccp_wantoptions[0].mppe &= ~MPPE_OPT_40; /* disable 40-bit */
  853. break;
  854. default:
  855. break;
  856. }
  857. }
  858. #endif /* MPPE */
  859. static struct chap_digest_type chapms_digest = {
  860. CHAP_MICROSOFT, /* code */
  861. chapms_generate_challenge,
  862. chapms_verify_response,
  863. chapms_make_response,
  864. NULL, /* check_success */
  865. chapms_handle_failure,
  866. };
  867. static struct chap_digest_type chapms2_digest = {
  868. CHAP_MICROSOFT_V2, /* code */
  869. chapms2_generate_challenge,
  870. chapms2_verify_response,
  871. chapms2_make_response,
  872. chapms2_check_success,
  873. chapms_handle_failure,
  874. };
  875. void
  876. chapms_init(void)
  877. {
  878. chap_register_digest(&chapms_digest);
  879. chap_register_digest(&chapms2_digest);
  880. add_options(chapms_option_list);
  881. }
  882. #endif /* CHAPMS */