Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

999 lines
32 KiB

  1. /* Copyright (c) 1993, Microsoft Corporation, all rights reserved
  2. **
  3. ** ntauth.c
  4. ** Remote Access PPP Challenge Handshake Authentication Protocol
  5. ** NT Authentication routines
  6. **
  7. ** These routines are specific to the NT platform.
  8. **
  9. ** 11/05/93 Steve Cobb (from MikeSa's AMB authentication code)
  10. */
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <ntlsa.h>
  15. #include <ntmsv1_0.h>
  16. #include <crypt.h>
  17. #include <windows.h>
  18. #include <lmcons.h>
  19. #include <lmapibuf.h>
  20. #include <lmaccess.h>
  21. #include <raserror.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <rasman.h>
  25. #include <rasppp.h>
  26. #include <pppcp.h>
  27. #include <rtutils.h>
  28. #include <rasauth.h>
  29. #define INCL_CLSA
  30. #define INCL_RASAUTHATTRIBUTES
  31. #define INCL_HOSTWIRE
  32. #define INCL_MISC
  33. #include <ppputil.h>
  34. #include "sha.h"
  35. #include "raschap.h"
  36. //**
  37. //
  38. // Call: MakeChangePasswordV1RequestAttributes
  39. //
  40. // Returns: NO_ERROR - Success
  41. // Non-zero returns - Failure
  42. //
  43. // Description:
  44. //
  45. DWORD
  46. MakeChangePasswordV1RequestAttributes(
  47. IN CHAPWB* pwb,
  48. IN BYTE bId,
  49. IN PCHAR pchIdentity,
  50. IN PBYTE Challenge,
  51. IN PENCRYPTED_LM_OWF_PASSWORD pEncryptedLmOwfOldPassword,
  52. IN PENCRYPTED_LM_OWF_PASSWORD pEncryptedLmOwfNewPassword,
  53. IN PENCRYPTED_NT_OWF_PASSWORD pEncryptedNtOwfOldPassword,
  54. IN PENCRYPTED_NT_OWF_PASSWORD pEncryptedNtOwfNewPassword,
  55. IN WORD LenPassword,
  56. IN WORD wFlags,
  57. IN DWORD cbChallenge,
  58. IN BYTE * pbChallenge
  59. )
  60. {
  61. DWORD dwRetCode;
  62. BYTE MsChapChangePw1[72+6];
  63. BYTE MsChapChallenge[MAXCHALLENGELEN+6];
  64. if ( pwb->pUserAttributes != NULL )
  65. {
  66. RasAuthAttributeDestroy( pwb->pUserAttributes );
  67. pwb->pUserAttributes = NULL;
  68. }
  69. //
  70. // Allocate the appropriate amount
  71. //
  72. if ( ( pwb->pUserAttributes = RasAuthAttributeCreate( 3 ) ) == NULL )
  73. {
  74. return( GetLastError() );
  75. }
  76. dwRetCode = RasAuthAttributeInsert( 0,
  77. pwb->pUserAttributes,
  78. raatUserName,
  79. FALSE,
  80. strlen( pchIdentity ),
  81. pchIdentity );
  82. if ( dwRetCode != NO_ERROR )
  83. {
  84. RasAuthAttributeDestroy( pwb->pUserAttributes );
  85. pwb->pUserAttributes = NULL;
  86. return( dwRetCode );
  87. }
  88. //
  89. // Build vendor specific attribute for MS-CHAP challenge
  90. //
  91. HostToWireFormat32( 311, MsChapChallenge ); // Vendor Id
  92. MsChapChallenge[4] = 11; // Vendor Type
  93. MsChapChallenge[5] = 2+(BYTE)cbChallenge; // Vendor Length
  94. CopyMemory( MsChapChallenge+6, pbChallenge, cbChallenge );
  95. dwRetCode = RasAuthAttributeInsert( 1,
  96. pwb->pUserAttributes,
  97. raatVendorSpecific,
  98. FALSE,
  99. cbChallenge+6,
  100. MsChapChallenge);
  101. if ( dwRetCode != NO_ERROR )
  102. {
  103. RasAuthAttributeDestroy( pwb->pUserAttributes );
  104. pwb->pUserAttributes = NULL;
  105. return( dwRetCode );
  106. }
  107. //
  108. // Insert change password attribute
  109. //
  110. HostToWireFormat32( 311, MsChapChangePw1 ); // Vendor Id
  111. MsChapChangePw1[4] = 3; // Vendor Type
  112. MsChapChangePw1[5] = 72; // Vendor Length
  113. MsChapChangePw1[6] = 5; // Code
  114. MsChapChangePw1[7] = bId; // Identifier
  115. CopyMemory( MsChapChangePw1+8,
  116. pEncryptedLmOwfOldPassword,
  117. 16 );
  118. CopyMemory( MsChapChangePw1+8+16,
  119. pEncryptedLmOwfNewPassword,
  120. 16 );
  121. CopyMemory( MsChapChangePw1+8+16+16,
  122. pEncryptedNtOwfOldPassword,
  123. 16 );
  124. CopyMemory( MsChapChangePw1+8+16+16+16,
  125. pEncryptedNtOwfNewPassword,
  126. 16 );
  127. HostToWireFormat16( LenPassword, MsChapChangePw1+8+16+16+16+16 );
  128. HostToWireFormat16( wFlags, MsChapChangePw1+8+16+16+16+16+2 );
  129. //
  130. // Build vendor specific attribute for MS-CHAP change password 1
  131. //
  132. dwRetCode = RasAuthAttributeInsert( 2,
  133. pwb->pUserAttributes,
  134. raatVendorSpecific,
  135. FALSE,
  136. 72+4,
  137. MsChapChangePw1);
  138. if ( dwRetCode != NO_ERROR )
  139. {
  140. RasAuthAttributeDestroy( pwb->pUserAttributes );
  141. pwb->pUserAttributes = NULL;
  142. return( dwRetCode );
  143. }
  144. return( dwRetCode );
  145. }
  146. //**
  147. //
  148. // Call: MakeChangePasswordV2RequestAttributes
  149. //
  150. // Returns: NO_ERROR - Success
  151. // Non-zero returns - Failure
  152. //
  153. // Description:
  154. //
  155. DWORD
  156. MakeChangePasswordV2RequestAttributes(
  157. IN CHAPWB* pwb,
  158. IN BYTE bId,
  159. IN CHAR* pchIdentity,
  160. IN SAMPR_ENCRYPTED_USER_PASSWORD* pNewEncryptedWithOldNtOwf,
  161. IN ENCRYPTED_NT_OWF_PASSWORD* pOldNtOwfEncryptedWithNewNtOwf,
  162. IN SAMPR_ENCRYPTED_USER_PASSWORD* pNewEncryptedWithOldLmOwf,
  163. IN ENCRYPTED_NT_OWF_PASSWORD* pOldLmOwfEncryptedWithNewNtOwf,
  164. IN DWORD cbChallenge,
  165. IN BYTE * pbChallenge,
  166. IN BYTE * pbResponse,
  167. IN WORD wFlags
  168. )
  169. {
  170. DWORD dwRetCode;
  171. BYTE MsChapChallenge[MAXCHALLENGELEN+6];
  172. BYTE MsChapChangePw2[86+4];
  173. BYTE NtPassword1[250+4];
  174. BYTE NtPassword2[250+4];
  175. BYTE NtPassword3[34+4];
  176. BYTE LmPassword1[250+4];
  177. BYTE LmPassword2[250+4];
  178. BYTE LmPassword3[34+4];
  179. if ( pwb->pUserAttributes != NULL )
  180. {
  181. RasAuthAttributeDestroy( pwb->pUserAttributes );
  182. pwb->pUserAttributes = NULL;
  183. }
  184. //
  185. // Allocate the appropriate amount
  186. //
  187. pwb->pUserAttributes = RasAuthAttributeCreate( 9 );
  188. if ( pwb->pUserAttributes == NULL )
  189. {
  190. return( GetLastError() );
  191. }
  192. dwRetCode = RasAuthAttributeInsert( 0,
  193. pwb->pUserAttributes,
  194. raatUserName,
  195. FALSE,
  196. strlen( pchIdentity ),
  197. pchIdentity );
  198. if ( dwRetCode != NO_ERROR )
  199. {
  200. RasAuthAttributeDestroy( pwb->pUserAttributes );
  201. pwb->pUserAttributes = NULL;
  202. return( dwRetCode );
  203. }
  204. //
  205. // Build vendor specific attribute for MS-CHAP challenge
  206. //
  207. HostToWireFormat32( 311, MsChapChallenge ); // Vendor Id
  208. MsChapChallenge[4] = 11; // Vendor Type
  209. MsChapChallenge[5] = 2+(BYTE)cbChallenge; // Vendor Length
  210. CopyMemory( MsChapChallenge+6, pbChallenge, cbChallenge );
  211. dwRetCode = RasAuthAttributeInsert( 1,
  212. pwb->pUserAttributes,
  213. raatVendorSpecific,
  214. FALSE,
  215. cbChallenge+6,
  216. MsChapChallenge);
  217. if ( dwRetCode != NO_ERROR )
  218. {
  219. RasAuthAttributeDestroy( pwb->pUserAttributes );
  220. pwb->pUserAttributes = NULL;
  221. return( dwRetCode );
  222. }
  223. //
  224. // Insert change password attribute
  225. //
  226. HostToWireFormat32( 311, MsChapChangePw2 ); // Vendor Id
  227. MsChapChangePw2[4] = 4; // Vendor Type
  228. MsChapChangePw2[5] = 86; // Vendor Length
  229. MsChapChangePw2[6] = 6; // Code
  230. MsChapChangePw2[7] = bId; // Identifier
  231. CopyMemory( MsChapChangePw2+8,
  232. pOldNtOwfEncryptedWithNewNtOwf,
  233. 16 );
  234. CopyMemory( MsChapChangePw2+8+16,
  235. pOldLmOwfEncryptedWithNewNtOwf,
  236. 16 );
  237. CopyMemory( MsChapChangePw2+8+16+16, pbResponse, 24+24 );
  238. HostToWireFormat16( (WORD)wFlags, MsChapChangePw2+8+16+16+24+24 );
  239. //
  240. // Build vendor specific attribute for MS-CHAP change password 2
  241. //
  242. dwRetCode = RasAuthAttributeInsert( 2,
  243. pwb->pUserAttributes,
  244. raatVendorSpecific,
  245. FALSE,
  246. 86+4,
  247. MsChapChangePw2);
  248. if ( dwRetCode != NO_ERROR )
  249. {
  250. RasAuthAttributeDestroy( pwb->pUserAttributes );
  251. pwb->pUserAttributes = NULL;
  252. return( dwRetCode );
  253. }
  254. //
  255. // Insert the new password attributes
  256. //
  257. HostToWireFormat32( 311, NtPassword1 ); // Vendor Id
  258. NtPassword1[4] = 6; // Vendor Type
  259. NtPassword1[5] = 249; // Vendor Length
  260. NtPassword1[6] = 6; // Code
  261. NtPassword1[7] = bId; // Identifier
  262. HostToWireFormat16( (WORD)1, NtPassword1+8 ); // Sequence number
  263. CopyMemory( NtPassword1+10, (PBYTE)pNewEncryptedWithOldNtOwf, 243 );
  264. dwRetCode = RasAuthAttributeInsert( 3,
  265. pwb->pUserAttributes,
  266. raatVendorSpecific,
  267. FALSE,
  268. 249+4,
  269. NtPassword1);
  270. if ( dwRetCode != NO_ERROR )
  271. {
  272. RasAuthAttributeDestroy( pwb->pUserAttributes );
  273. pwb->pUserAttributes = NULL;
  274. return( dwRetCode );
  275. }
  276. HostToWireFormat32( 311, NtPassword2 ); // Vendor Id
  277. NtPassword2[4] = 6; // Vendor Type
  278. NtPassword2[5] = 249; // Vendor Length
  279. NtPassword2[6] = 6; // Code
  280. NtPassword2[7] = bId; // Identifier
  281. HostToWireFormat16( (WORD)2, NtPassword2+8 ); // Sequence number
  282. CopyMemory( NtPassword2+10,
  283. ((PBYTE)pNewEncryptedWithOldNtOwf)+243,
  284. 243 );
  285. dwRetCode = RasAuthAttributeInsert( 4,
  286. pwb->pUserAttributes,
  287. raatVendorSpecific,
  288. FALSE,
  289. 249+4,
  290. NtPassword2 );
  291. if ( dwRetCode != NO_ERROR )
  292. {
  293. RasAuthAttributeDestroy( pwb->pUserAttributes );
  294. pwb->pUserAttributes = NULL;
  295. return( dwRetCode );
  296. }
  297. HostToWireFormat32( 311, NtPassword3 ); // Vendor Id
  298. NtPassword3[4] = 6; // Vendor Type
  299. NtPassword3[5] = 36; // Vendor Length
  300. NtPassword3[6] = 6; // Code
  301. NtPassword3[7] = bId; // Identifier
  302. HostToWireFormat16( (WORD)3, NtPassword3+8 ); // Sequence number
  303. CopyMemory( NtPassword3+10,
  304. ((PBYTE)pNewEncryptedWithOldNtOwf)+486,
  305. 30 );
  306. dwRetCode = RasAuthAttributeInsert( 5,
  307. pwb->pUserAttributes,
  308. raatVendorSpecific,
  309. FALSE,
  310. 36+4,
  311. NtPassword3 );
  312. if ( dwRetCode != NO_ERROR )
  313. {
  314. RasAuthAttributeDestroy( pwb->pUserAttributes );
  315. pwb->pUserAttributes = NULL;
  316. return( dwRetCode );
  317. }
  318. HostToWireFormat32( 311, LmPassword1 ); // Vendor Id
  319. LmPassword1[4] = 5; // Vendor Type
  320. LmPassword1[5] = 249; // Vendor Length
  321. LmPassword1[6] = 6; // Code
  322. LmPassword1[7] = bId; // Identifier
  323. HostToWireFormat16( (WORD)1, LmPassword1+8 ); // Sequence number
  324. CopyMemory( LmPassword1+10, pNewEncryptedWithOldLmOwf, 243 );
  325. dwRetCode = RasAuthAttributeInsert( 6,
  326. pwb->pUserAttributes,
  327. raatVendorSpecific,
  328. FALSE,
  329. 249+4,
  330. LmPassword1);
  331. if ( dwRetCode != NO_ERROR )
  332. {
  333. RasAuthAttributeDestroy( pwb->pUserAttributes );
  334. pwb->pUserAttributes = NULL;
  335. return( dwRetCode );
  336. }
  337. HostToWireFormat32( 311, LmPassword2 ); // Vendor Id
  338. LmPassword2[4] = 5; // Vendor Type
  339. LmPassword2[5] = 249; // Vendor Length
  340. LmPassword2[6] = 6; // Code
  341. LmPassword2[7] = bId; // Identifier
  342. HostToWireFormat16( (WORD)2, LmPassword2+8 ); // Sequence number
  343. CopyMemory( LmPassword2+10,
  344. ((PBYTE)pNewEncryptedWithOldLmOwf)+243,
  345. 243 );
  346. dwRetCode = RasAuthAttributeInsert( 7,
  347. pwb->pUserAttributes,
  348. raatVendorSpecific,
  349. FALSE,
  350. 249+4,
  351. LmPassword2 );
  352. if ( dwRetCode != NO_ERROR )
  353. {
  354. RasAuthAttributeDestroy( pwb->pUserAttributes );
  355. pwb->pUserAttributes = NULL;
  356. return( dwRetCode );
  357. }
  358. HostToWireFormat32( 311, LmPassword3 ); // Vendor Id
  359. LmPassword3[4] = 5; // Vendor Type
  360. LmPassword3[5] = 36; // Vendor Length
  361. LmPassword3[6] = 6; // Code
  362. LmPassword3[7] = bId; // Identifier
  363. HostToWireFormat16( (WORD)3, LmPassword3+8 ); // Sequence number
  364. CopyMemory( LmPassword3+10,
  365. ((PBYTE)pNewEncryptedWithOldLmOwf)+486,
  366. 30 );
  367. dwRetCode = RasAuthAttributeInsert( 8,
  368. pwb->pUserAttributes,
  369. raatVendorSpecific,
  370. FALSE,
  371. 36+4,
  372. LmPassword3 );
  373. if ( dwRetCode != NO_ERROR )
  374. {
  375. RasAuthAttributeDestroy( pwb->pUserAttributes );
  376. pwb->pUserAttributes = NULL;
  377. return( dwRetCode );
  378. }
  379. return( dwRetCode );
  380. }
  381. //**
  382. //
  383. // Call: MakeChangePasswordV3RequestAttributes
  384. //
  385. // Returns: NO_ERROR - Success
  386. // Non-zero returns - Failure
  387. //
  388. // Description:
  389. //
  390. DWORD
  391. MakeChangePasswordV3RequestAttributes(
  392. IN CHAPWB* pwb,
  393. IN BYTE bId,
  394. IN CHAR* pchIdentity,
  395. IN CHANGEPW3* pchangepw3,
  396. IN DWORD cbChallenge,
  397. IN BYTE * pbChallenge
  398. )
  399. {
  400. DWORD dwRetCode;
  401. BYTE MsChapChallenge[MAXCHALLENGELEN+6];
  402. BYTE MsChapChangePw3[70+4];
  403. BYTE NtPassword1[250+4];
  404. BYTE NtPassword2[250+4];
  405. BYTE NtPassword3[34+4];
  406. if ( pwb->pUserAttributes != NULL )
  407. {
  408. RasAuthAttributeDestroy( pwb->pUserAttributes );
  409. pwb->pUserAttributes = NULL;
  410. }
  411. //
  412. // Allocate the appropriate amount
  413. //
  414. pwb->pUserAttributes = RasAuthAttributeCreate( 6 );
  415. if ( pwb->pUserAttributes == NULL )
  416. {
  417. return( GetLastError() );
  418. }
  419. dwRetCode = RasAuthAttributeInsert( 0,
  420. pwb->pUserAttributes,
  421. raatUserName,
  422. FALSE,
  423. strlen( pchIdentity ),
  424. pchIdentity );
  425. if ( dwRetCode != NO_ERROR )
  426. {
  427. RasAuthAttributeDestroy( pwb->pUserAttributes );
  428. pwb->pUserAttributes = NULL;
  429. return( dwRetCode );
  430. }
  431. //
  432. // Build vendor specific attribute for MS-CHAP challenge
  433. //
  434. HostToWireFormat32( 311, MsChapChallenge ); // Vendor Id
  435. MsChapChallenge[4] = 11; // Vendor Type
  436. MsChapChallenge[5] = 2+(BYTE)cbChallenge; // Vendor Length
  437. CopyMemory( MsChapChallenge+6, pbChallenge, cbChallenge );
  438. dwRetCode = RasAuthAttributeInsert( 1,
  439. pwb->pUserAttributes,
  440. raatVendorSpecific,
  441. FALSE,
  442. cbChallenge+6,
  443. MsChapChallenge);
  444. if ( dwRetCode != NO_ERROR )
  445. {
  446. RasAuthAttributeDestroy( pwb->pUserAttributes );
  447. pwb->pUserAttributes = NULL;
  448. return( dwRetCode );
  449. }
  450. //
  451. // Insert change password attribute
  452. //
  453. HostToWireFormat32( 311, MsChapChangePw3 ); // Vendor Id
  454. MsChapChangePw3[4] = 27; // Vendor Type
  455. MsChapChangePw3[5] = 70; // Vendor Length
  456. MsChapChangePw3[6] = 7; // Code
  457. MsChapChangePw3[7] = bId; // Identifier
  458. CopyMemory( MsChapChangePw3+8, pchangepw3->abEncryptedHash, 16 );
  459. CopyMemory( MsChapChangePw3+8+16, pchangepw3->abPeerChallenge, 24 );
  460. CopyMemory( MsChapChangePw3+8+16+24, pchangepw3->abNTResponse, 24 );
  461. HostToWireFormat16( (WORD)0, MsChapChangePw3+8+16+24+24 );
  462. //
  463. // Build vendor specific attribute for MS-CHAP2-PW
  464. //
  465. dwRetCode = RasAuthAttributeInsert( 2,
  466. pwb->pUserAttributes,
  467. raatVendorSpecific,
  468. FALSE,
  469. 70+4,
  470. MsChapChangePw3);
  471. if ( dwRetCode != NO_ERROR )
  472. {
  473. RasAuthAttributeDestroy( pwb->pUserAttributes );
  474. pwb->pUserAttributes = NULL;
  475. return( dwRetCode );
  476. }
  477. //
  478. // Insert the new password attributes
  479. //
  480. HostToWireFormat32( 311, NtPassword1 ); // Vendor Id
  481. NtPassword1[4] = 6; // Vendor Type
  482. NtPassword1[5] = 249; // Vendor Length
  483. NtPassword1[6] = 6; // Code
  484. NtPassword1[7] = bId; // Identifier
  485. HostToWireFormat16( (WORD)1, NtPassword1+8 ); // Sequence number
  486. CopyMemory( NtPassword1+10, pchangepw3->abEncryptedPassword, 243 );
  487. dwRetCode = RasAuthAttributeInsert( 3,
  488. pwb->pUserAttributes,
  489. raatVendorSpecific,
  490. FALSE,
  491. 249+4,
  492. NtPassword1);
  493. if ( dwRetCode != NO_ERROR )
  494. {
  495. RasAuthAttributeDestroy( pwb->pUserAttributes );
  496. pwb->pUserAttributes = NULL;
  497. return( dwRetCode );
  498. }
  499. HostToWireFormat32( 311, NtPassword2 ); // Vendor Id
  500. NtPassword2[4] = 6; // Vendor Type
  501. NtPassword2[5] = 249; // Vendor Length
  502. NtPassword2[6] = 6; // Code
  503. NtPassword2[7] = bId; // Identifier
  504. HostToWireFormat16( (WORD)2, NtPassword2+8 ); // Sequence number
  505. CopyMemory( NtPassword2+10,
  506. pchangepw3->abEncryptedPassword+243,
  507. 243 );
  508. dwRetCode = RasAuthAttributeInsert( 4,
  509. pwb->pUserAttributes,
  510. raatVendorSpecific,
  511. FALSE,
  512. 249+4,
  513. NtPassword2 );
  514. if ( dwRetCode != NO_ERROR )
  515. {
  516. RasAuthAttributeDestroy( pwb->pUserAttributes );
  517. pwb->pUserAttributes = NULL;
  518. return( dwRetCode );
  519. }
  520. HostToWireFormat32( 311, NtPassword3 ); // Vendor Id
  521. NtPassword3[4] = 6; // Vendor Type
  522. NtPassword3[5] = 36; // Vendor Length
  523. NtPassword3[6] = 6; // Code
  524. NtPassword3[7] = bId; // Identifier
  525. HostToWireFormat16( (WORD)3, NtPassword3+8 ); // Sequence number
  526. CopyMemory( NtPassword3+10,
  527. pchangepw3->abEncryptedPassword+486,
  528. 30 );
  529. dwRetCode = RasAuthAttributeInsert( 5,
  530. pwb->pUserAttributes,
  531. raatVendorSpecific,
  532. FALSE,
  533. 36+4,
  534. NtPassword3 );
  535. if ( dwRetCode != NO_ERROR )
  536. {
  537. RasAuthAttributeDestroy( pwb->pUserAttributes );
  538. pwb->pUserAttributes = NULL;
  539. return( dwRetCode );
  540. }
  541. return( dwRetCode );
  542. }
  543. //**
  544. //
  545. // Call: MakeAuthenticationRequestAttributes
  546. //
  547. // Returns: NO_ERROR - Success
  548. // Non-zero returns - Failure
  549. //
  550. // Description:
  551. //
  552. DWORD
  553. MakeAuthenticationRequestAttributes(
  554. IN CHAPWB * pwb,
  555. IN BOOL fMSChap,
  556. IN BYTE bAlgorithm,
  557. IN CHAR* szUserName,
  558. IN BYTE* pbChallenge,
  559. IN DWORD cbChallenge,
  560. IN BYTE* pbResponse,
  561. IN DWORD cbResponse,
  562. IN BYTE bId
  563. )
  564. {
  565. DWORD dwRetCode;
  566. BYTE abResponse[MD5RESPONSELEN+1];
  567. if ( pwb->pUserAttributes != NULL )
  568. {
  569. RasAuthAttributeDestroy( pwb->pUserAttributes );
  570. pwb->pUserAttributes = NULL;
  571. }
  572. //
  573. // Allocate the appropriate amount
  574. //
  575. if ( ( pwb->pUserAttributes = RasAuthAttributeCreate( 3 ) ) == NULL )
  576. {
  577. return( GetLastError() );
  578. }
  579. dwRetCode = RasAuthAttributeInsert( 0,
  580. pwb->pUserAttributes,
  581. raatUserName,
  582. FALSE,
  583. strlen( szUserName ),
  584. szUserName );
  585. if ( dwRetCode != NO_ERROR )
  586. {
  587. RasAuthAttributeDestroy( pwb->pUserAttributes );
  588. pwb->pUserAttributes = NULL;
  589. return( dwRetCode );
  590. }
  591. if ( fMSChap )
  592. {
  593. BYTE MsChapChallenge[MAXCHALLENGELEN+6];
  594. HostToWireFormat32( 311, MsChapChallenge ); // Vendor Id
  595. MsChapChallenge[4] = 11; // Vendor Type
  596. MsChapChallenge[5] = 2+(BYTE)cbChallenge; // Vendor Length
  597. CopyMemory( MsChapChallenge+6, pbChallenge, cbChallenge );
  598. //
  599. // Build vendor specific attribute for MS-CHAP challenge
  600. //
  601. dwRetCode = RasAuthAttributeInsert( 1,
  602. pwb->pUserAttributes,
  603. raatVendorSpecific,
  604. FALSE,
  605. cbChallenge+6,
  606. MsChapChallenge );
  607. }
  608. else
  609. {
  610. dwRetCode = RasAuthAttributeInsert( 1,
  611. pwb->pUserAttributes,
  612. raatMD5CHAPChallenge,
  613. FALSE,
  614. cbChallenge,
  615. pbChallenge );
  616. }
  617. if ( dwRetCode != NO_ERROR )
  618. {
  619. RasAuthAttributeDestroy( pwb->pUserAttributes );
  620. pwb->pUserAttributes = NULL;
  621. return( dwRetCode );
  622. }
  623. if ( fMSChap && ( bAlgorithm == PPP_CHAP_DIGEST_MSEXT ) )
  624. {
  625. BYTE MsChapResponse[56];
  626. HostToWireFormat32( 311, MsChapResponse ); // Vendor Id
  627. MsChapResponse[4] = 1; // Vendor Type
  628. MsChapResponse[5] = (BYTE)52; // Vendor Length
  629. MsChapResponse[6] = bId; // Ident
  630. MsChapResponse[7] = pbResponse[cbResponse-1]; // Flags
  631. CopyMemory( MsChapResponse+8, pbResponse, cbResponse-1 );
  632. dwRetCode = RasAuthAttributeInsert( 2,
  633. pwb->pUserAttributes,
  634. raatVendorSpecific,
  635. FALSE,
  636. 56,
  637. MsChapResponse);
  638. }
  639. else if ( fMSChap && ( bAlgorithm == PPP_CHAP_DIGEST_MSEXT_NEW ) )
  640. {
  641. BYTE MsChap2Response[56];
  642. HostToWireFormat32( 311, MsChap2Response ); // Vendor Id
  643. MsChap2Response[4] = 25; // Vendor Type
  644. MsChap2Response[5] = (BYTE)52; // Vendor Length
  645. MsChap2Response[6] = bId; // Ident
  646. MsChap2Response[7] = 0; // Flags
  647. CopyMemory( MsChap2Response+8, pbResponse, cbResponse-1 );
  648. dwRetCode = RasAuthAttributeInsert( 2,
  649. pwb->pUserAttributes,
  650. raatVendorSpecific,
  651. FALSE,
  652. 56,
  653. MsChap2Response);
  654. }
  655. else
  656. {
  657. abResponse[0] = bId;
  658. CopyMemory( abResponse+1, pbResponse, cbResponse );
  659. dwRetCode = RasAuthAttributeInsert( 2,
  660. pwb->pUserAttributes,
  661. raatMD5CHAPPassword,
  662. FALSE,
  663. cbResponse+1,
  664. abResponse );
  665. }
  666. if ( dwRetCode != NO_ERROR )
  667. {
  668. RasAuthAttributeDestroy( pwb->pUserAttributes );
  669. pwb->pUserAttributes = NULL;
  670. return( dwRetCode );
  671. }
  672. return( dwRetCode );
  673. }
  674. //**
  675. //
  676. // Call: GetErrorCodeFromAttributes
  677. //
  678. // Returns: NO_ERROR - Success
  679. // Non-zero returns - Failure
  680. //
  681. // Description: Will extract the error code returned from the authentication
  682. // provider and insert it into the reponse sent to the client
  683. //
  684. DWORD
  685. GetErrorCodeFromAttributes(
  686. IN CHAPWB* pwb
  687. )
  688. {
  689. RAS_AUTH_ATTRIBUTE * pAttribute;
  690. RAS_AUTH_ATTRIBUTE * pAttributes = pwb->pAttributesFromAuthenticator;
  691. DWORD dwRetCode = NO_ERROR;
  692. //
  693. // Search for MS-CHAP Error attributes
  694. //
  695. pAttribute = RasAuthAttributeGetVendorSpecific( 311, 2, pAttributes );
  696. if ( pAttribute != NULL )
  697. {
  698. CHAR chErrorBuffer[150];
  699. CHAR* pszValue;
  700. DWORD cbError = (DWORD)*(((PBYTE)(pAttribute->Value))+5);
  701. //
  702. // Leave one byte for NULL terminator
  703. //
  704. if ( cbError > sizeof( chErrorBuffer ) - 1 )
  705. {
  706. cbError = sizeof( chErrorBuffer ) - 1;
  707. }
  708. ZeroMemory( chErrorBuffer, sizeof( chErrorBuffer ) );
  709. //
  710. // We do -2 below to account for the size of the vendor
  711. // attribute itself and the id.
  712. //
  713. CopyMemory( chErrorBuffer,
  714. (CHAR *)((PBYTE)(pAttribute->Value) + 7),
  715. cbError - 2 );
  716. pszValue = strstr( chErrorBuffer, "E=" );
  717. if ( pszValue )
  718. {
  719. pwb->result.dwError = (DWORD )atol( pszValue + 2 );
  720. }
  721. pszValue = strstr( chErrorBuffer, "R=1" );
  722. if ( pszValue )
  723. {
  724. pwb->dwTriesLeft = 1;
  725. }
  726. }
  727. else
  728. {
  729. //
  730. // If we did not get an error code attribute back then assume an
  731. // access denied
  732. //
  733. TRACE("No error code attribute returned, assuming access denied");
  734. pwb->result.dwError = ERROR_AUTHENTICATION_FAILURE;
  735. }
  736. return( dwRetCode );
  737. }
  738. //**
  739. //
  740. // Call: GetEncryptedPasswordsForChangePassword2
  741. //
  742. // Returns: NO_ERROR - Success
  743. // Non-zero returns - Failure
  744. //
  745. // Description:
  746. //
  747. DWORD
  748. GetEncryptedPasswordsForChangePassword2(
  749. IN CHAR* pszOldPassword,
  750. IN CHAR* pszNewPassword,
  751. OUT SAMPR_ENCRYPTED_USER_PASSWORD* pNewEncryptedWithOldNtOwf,
  752. OUT ENCRYPTED_NT_OWF_PASSWORD* pOldNtOwfEncryptedWithNewNtOwf,
  753. OUT SAMPR_ENCRYPTED_USER_PASSWORD* pNewEncryptedWithOldLmOwf,
  754. OUT ENCRYPTED_NT_OWF_PASSWORD* pOldLmOwfEncryptedWithNewNtOwf,
  755. OUT BOOLEAN* pfLmPresent )
  756. {
  757. DWORD dwErr;
  758. BOOL fLmPresent;
  759. UNICODE_STRING uniOldPassword;
  760. UNICODE_STRING uniNewPassword;
  761. TRACE("GetEncryptedPasswordsForChangePassword2...");
  762. uniOldPassword.Buffer = NULL;
  763. uniNewPassword.Buffer = NULL;
  764. if (!RtlCreateUnicodeStringFromAsciiz(
  765. &uniOldPassword, pszOldPassword )
  766. || !RtlCreateUnicodeStringFromAsciiz(
  767. &uniNewPassword, pszNewPassword ))
  768. {
  769. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  770. }
  771. else
  772. {
  773. dwErr =
  774. SamiEncryptPasswords(
  775. &uniOldPassword,
  776. &uniNewPassword,
  777. pNewEncryptedWithOldNtOwf,
  778. pOldNtOwfEncryptedWithNewNtOwf,
  779. pfLmPresent,
  780. pNewEncryptedWithOldLmOwf,
  781. pOldLmOwfEncryptedWithNewNtOwf );
  782. }
  783. /* Erase password buffers.
  784. */
  785. if (uniOldPassword.Buffer)
  786. {
  787. ZeroMemory( uniOldPassword.Buffer,
  788. lstrlenW( uniOldPassword.Buffer ) * sizeof( WCHAR ) );
  789. }
  790. if (uniNewPassword.Buffer)
  791. {
  792. ZeroMemory( uniNewPassword.Buffer,
  793. lstrlenW( uniNewPassword.Buffer ) * sizeof( WCHAR ) );
  794. }
  795. RtlFreeUnicodeString( &uniOldPassword );
  796. RtlFreeUnicodeString( &uniNewPassword );
  797. TRACE1("GetEncryptedPasswordsForChangePassword2 done(%d)",dwErr);
  798. return dwErr;
  799. }