PageRenderTime 160ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Externals/curl/lib/vauth/ntlm.c

https://gitlab.com/Hexexpeck/dolphin-emulator
C | 842 lines | 524 code | 115 blank | 203 comment | 81 complexity | 2b10e4be41a9aa113ac99390e5a07579 MD5 | raw file
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(USE_NTLM) && !defined(USE_WINDOWS_SSPI)
  24. /*
  25. * NTLM details:
  26. *
  27. * http://davenport.sourceforge.net/ntlm.html
  28. * https://www.innovation.ch/java/ntlm.html
  29. */
  30. #define DEBUG_ME 0
  31. #include "urldata.h"
  32. #include "non-ascii.h"
  33. #include "sendf.h"
  34. #include "curl_base64.h"
  35. #include "curl_ntlm_core.h"
  36. #include "curl_gethostname.h"
  37. #include "curl_multibyte.h"
  38. #include "warnless.h"
  39. #include "vtls/vtls.h"
  40. #ifdef USE_NSS
  41. #include "vtls/nssg.h" /* for Curl_nss_force_init() */
  42. #endif
  43. #define BUILDING_CURL_NTLM_MSGS_C
  44. #include "vauth/vauth.h"
  45. #include "vauth/ntlm.h"
  46. #include "curl_endian.h"
  47. #include "curl_printf.h"
  48. /* The last #include files should be: */
  49. #include "curl_memory.h"
  50. #include "memdebug.h"
  51. /* "NTLMSSP" signature is always in ASCII regardless of the platform */
  52. #define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
  53. #define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
  54. #define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8) & 0xff), \
  55. (((x) >> 16) & 0xff), (((x) >> 24) & 0xff)
  56. #if DEBUG_ME
  57. # define DEBUG_OUT(x) x
  58. static void ntlm_print_flags(FILE *handle, unsigned long flags)
  59. {
  60. if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
  61. fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
  62. if(flags & NTLMFLAG_NEGOTIATE_OEM)
  63. fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
  64. if(flags & NTLMFLAG_REQUEST_TARGET)
  65. fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
  66. if(flags & (1<<3))
  67. fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
  68. if(flags & NTLMFLAG_NEGOTIATE_SIGN)
  69. fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
  70. if(flags & NTLMFLAG_NEGOTIATE_SEAL)
  71. fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
  72. if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
  73. fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
  74. if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
  75. fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
  76. if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
  77. fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
  78. if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
  79. fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
  80. if(flags & (1<<10))
  81. fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
  82. if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
  83. fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
  84. if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
  85. fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
  86. if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
  87. fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
  88. if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
  89. fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
  90. if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
  91. fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
  92. if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
  93. fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
  94. if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
  95. fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
  96. if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
  97. fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
  98. if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
  99. fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
  100. if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
  101. fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
  102. if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
  103. fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
  104. if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
  105. fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
  106. if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
  107. fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
  108. if(flags & (1<<24))
  109. fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
  110. if(flags & (1<<25))
  111. fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
  112. if(flags & (1<<26))
  113. fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
  114. if(flags & (1<<27))
  115. fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
  116. if(flags & (1<<28))
  117. fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
  118. if(flags & NTLMFLAG_NEGOTIATE_128)
  119. fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
  120. if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
  121. fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
  122. if(flags & NTLMFLAG_NEGOTIATE_56)
  123. fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
  124. }
  125. static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
  126. {
  127. const char *p = buf;
  128. (void) handle;
  129. fprintf(stderr, "0x");
  130. while(len-- > 0)
  131. fprintf(stderr, "%02.2x", (unsigned int)*p++);
  132. }
  133. #else
  134. # define DEBUG_OUT(x) Curl_nop_stmt
  135. #endif
  136. /*
  137. * ntlm_decode_type2_target()
  138. *
  139. * This is used to decode the "target info" in the NTLM type-2 message
  140. * received.
  141. *
  142. * Parameters:
  143. *
  144. * data [in] - The session handle.
  145. * buffer [in] - The decoded type-2 message.
  146. * size [in] - The input buffer size, at least 32 bytes.
  147. * ntlm [in/out] - The NTLM data struct being used and modified.
  148. *
  149. * Returns CURLE_OK on success.
  150. */
  151. static CURLcode ntlm_decode_type2_target(struct SessionHandle *data,
  152. unsigned char *buffer,
  153. size_t size,
  154. struct ntlmdata *ntlm)
  155. {
  156. unsigned short target_info_len = 0;
  157. unsigned int target_info_offset = 0;
  158. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  159. (void) data;
  160. #endif
  161. if(size >= 48) {
  162. target_info_len = Curl_read16_le(&buffer[40]);
  163. target_info_offset = Curl_read32_le(&buffer[44]);
  164. if(target_info_len > 0) {
  165. if(((target_info_offset + target_info_len) > size) ||
  166. (target_info_offset < 48)) {
  167. infof(data, "NTLM handshake failure (bad type-2 message). "
  168. "Target Info Offset Len is set incorrect by the peer\n");
  169. return CURLE_BAD_CONTENT_ENCODING;
  170. }
  171. ntlm->target_info = malloc(target_info_len);
  172. if(!ntlm->target_info)
  173. return CURLE_OUT_OF_MEMORY;
  174. memcpy(ntlm->target_info, &buffer[target_info_offset], target_info_len);
  175. }
  176. }
  177. ntlm->target_info_len = target_info_len;
  178. return CURLE_OK;
  179. }
  180. /*
  181. NTLM message structure notes:
  182. A 'short' is a 'network short', a little-endian 16-bit unsigned value.
  183. A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
  184. A 'security buffer' represents a triplet used to point to a buffer,
  185. consisting of two shorts and one long:
  186. 1. A 'short' containing the length of the buffer content in bytes.
  187. 2. A 'short' containing the allocated space for the buffer in bytes.
  188. 3. A 'long' containing the offset to the start of the buffer in bytes,
  189. from the beginning of the NTLM message.
  190. */
  191. /*
  192. * Curl_auth_decode_ntlm_type2_message()
  193. *
  194. * This is used to decode an already encoded NTLM type-2 message. The message
  195. * is first decoded from a base64 string into a raw NTLM message and checked
  196. * for validity before the appropriate data for creating a type-3 message is
  197. * written to the given NTLM data structure.
  198. *
  199. * Parameters:
  200. *
  201. * data [in] - The session handle.
  202. * type2msg [in] - The base64 encoded type-2 message.
  203. * ntlm [in/out] - The NTLM data struct being used and modified.
  204. *
  205. * Returns CURLE_OK on success.
  206. */
  207. CURLcode Curl_auth_decode_ntlm_type2_message(struct SessionHandle *data,
  208. const char *type2msg,
  209. struct ntlmdata *ntlm)
  210. {
  211. static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
  212. /* NTLM type-2 message structure:
  213. Index Description Content
  214. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  215. (0x4e544c4d53535000)
  216. 8 NTLM Message Type long (0x02000000)
  217. 12 Target Name security buffer
  218. 20 Flags long
  219. 24 Challenge 8 bytes
  220. (32) Context 8 bytes (two consecutive longs) (*)
  221. (40) Target Information security buffer (*)
  222. (48) OS Version Structure 8 bytes (*)
  223. 32 (48) (56) Start of data block (*)
  224. (*) -> Optional
  225. */
  226. CURLcode result = CURLE_OK;
  227. unsigned char *type2 = NULL;
  228. size_t type2_len = 0;
  229. #if defined(USE_NSS)
  230. /* Make sure the crypto backend is initialized */
  231. result = Curl_nss_force_init(data);
  232. if(result)
  233. return result;
  234. #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
  235. (void)data;
  236. #endif
  237. /* Decode the base-64 encoded type-2 message */
  238. if(strlen(type2msg) && *type2msg != '=') {
  239. result = Curl_base64_decode(type2msg, &type2, &type2_len);
  240. if(result)
  241. return result;
  242. }
  243. /* Ensure we have a valid type-2 message */
  244. if(!type2) {
  245. infof(data, "NTLM handshake failure (empty type-2 message)\n");
  246. return CURLE_BAD_CONTENT_ENCODING;
  247. }
  248. ntlm->flags = 0;
  249. if((type2_len < 32) ||
  250. (memcmp(type2, NTLMSSP_SIGNATURE, 8) != 0) ||
  251. (memcmp(type2 + 8, type2_marker, sizeof(type2_marker)) != 0)) {
  252. /* This was not a good enough type-2 message */
  253. free(type2);
  254. infof(data, "NTLM handshake failure (bad type-2 message)\n");
  255. return CURLE_BAD_CONTENT_ENCODING;
  256. }
  257. ntlm->flags = Curl_read32_le(&type2[20]);
  258. memcpy(ntlm->nonce, &type2[24], 8);
  259. if(ntlm->flags & NTLMFLAG_NEGOTIATE_TARGET_INFO) {
  260. result = ntlm_decode_type2_target(data, type2, type2_len, ntlm);
  261. if(result) {
  262. free(type2);
  263. infof(data, "NTLM handshake failure (bad type-2 message)\n");
  264. return result;
  265. }
  266. }
  267. DEBUG_OUT({
  268. fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
  269. ntlm_print_flags(stderr, ntlm->flags);
  270. fprintf(stderr, "\n nonce=");
  271. ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
  272. fprintf(stderr, "\n****\n");
  273. fprintf(stderr, "**** Header %s\n ", header);
  274. });
  275. free(type2);
  276. return result;
  277. }
  278. /* copy the source to the destination and fill in zeroes in every
  279. other destination byte! */
  280. static void unicodecpy(unsigned char *dest, const char *src, size_t length)
  281. {
  282. size_t i;
  283. for(i = 0; i < length; i++) {
  284. dest[2 * i] = (unsigned char)src[i];
  285. dest[2 * i + 1] = '\0';
  286. }
  287. }
  288. /*
  289. * Curl_auth_create_ntlm_type1_message()
  290. *
  291. * This is used to generate an already encoded NTLM type-1 message ready for
  292. * sending to the recipient using the appropriate compile time crypto API.
  293. *
  294. * Parameters:
  295. *
  296. * userp [in] - The user name in the format User or Domain\User.
  297. * passdwp [in] - The user's password.
  298. * ntlm [in/out] - The NTLM data struct being used and modified.
  299. * outptr [in/out] - The address where a pointer to newly allocated memory
  300. * holding the result will be stored upon completion.
  301. * outlen [out] - The length of the output message.
  302. *
  303. * Returns CURLE_OK on success.
  304. */
  305. CURLcode Curl_auth_create_ntlm_type1_message(const char *userp,
  306. const char *passwdp,
  307. struct ntlmdata *ntlm,
  308. char **outptr, size_t *outlen)
  309. {
  310. /* NTLM type-1 message structure:
  311. Index Description Content
  312. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  313. (0x4e544c4d53535000)
  314. 8 NTLM Message Type long (0x01000000)
  315. 12 Flags long
  316. (16) Supplied Domain security buffer (*)
  317. (24) Supplied Workstation security buffer (*)
  318. (32) OS Version Structure 8 bytes (*)
  319. (32) (40) Start of data block (*)
  320. (*) -> Optional
  321. */
  322. size_t size;
  323. unsigned char ntlmbuf[NTLM_BUFSIZE];
  324. const char *host = ""; /* empty */
  325. const char *domain = ""; /* empty */
  326. size_t hostlen = 0;
  327. size_t domlen = 0;
  328. size_t hostoff = 0;
  329. size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
  330. domain are empty */
  331. (void)userp;
  332. (void)passwdp;
  333. /* Clean up any former leftovers and initialise to defaults */
  334. Curl_auth_ntlm_cleanup(ntlm);
  335. #if USE_NTRESPONSES && USE_NTLM2SESSION
  336. #define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
  337. #else
  338. #define NTLM2FLAG 0
  339. #endif
  340. snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
  341. NTLMSSP_SIGNATURE "%c"
  342. "\x01%c%c%c" /* 32-bit type = 1 */
  343. "%c%c%c%c" /* 32-bit NTLM flag field */
  344. "%c%c" /* domain length */
  345. "%c%c" /* domain allocated space */
  346. "%c%c" /* domain name offset */
  347. "%c%c" /* 2 zeroes */
  348. "%c%c" /* host length */
  349. "%c%c" /* host allocated space */
  350. "%c%c" /* host name offset */
  351. "%c%c" /* 2 zeroes */
  352. "%s" /* host name */
  353. "%s", /* domain string */
  354. 0, /* trailing zero */
  355. 0, 0, 0, /* part of type-1 long */
  356. LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
  357. NTLMFLAG_REQUEST_TARGET |
  358. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  359. NTLM2FLAG |
  360. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
  361. SHORTPAIR(domlen),
  362. SHORTPAIR(domlen),
  363. SHORTPAIR(domoff),
  364. 0, 0,
  365. SHORTPAIR(hostlen),
  366. SHORTPAIR(hostlen),
  367. SHORTPAIR(hostoff),
  368. 0, 0,
  369. host, /* this is empty */
  370. domain /* this is empty */);
  371. /* Initial packet length */
  372. size = 32 + hostlen + domlen;
  373. DEBUG_OUT({
  374. fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
  375. "0x%08.8x ",
  376. LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
  377. NTLMFLAG_REQUEST_TARGET |
  378. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  379. NTLM2FLAG |
  380. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
  381. NTLMFLAG_NEGOTIATE_OEM |
  382. NTLMFLAG_REQUEST_TARGET |
  383. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  384. NTLM2FLAG |
  385. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
  386. ntlm_print_flags(stderr,
  387. NTLMFLAG_NEGOTIATE_OEM |
  388. NTLMFLAG_REQUEST_TARGET |
  389. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  390. NTLM2FLAG |
  391. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
  392. fprintf(stderr, "\n****\n");
  393. });
  394. /* Return with binary blob encoded into base64 */
  395. return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
  396. }
  397. /*
  398. * Curl_auth_create_ntlm_type3_message()
  399. *
  400. * This is used to generate an already encoded NTLM type-3 message ready for
  401. * sending to the recipient using the appropriate compile time crypto API.
  402. *
  403. * Parameters:
  404. *
  405. * data [in] - The session handle.
  406. * userp [in] - The user name in the format User or Domain\User.
  407. * passdwp [in] - The user's password.
  408. * ntlm [in/out] - The NTLM data struct being used and modified.
  409. * outptr [in/out] - The address where a pointer to newly allocated memory
  410. * holding the result will be stored upon completion.
  411. * outlen [out] - The length of the output message.
  412. *
  413. * Returns CURLE_OK on success.
  414. */
  415. CURLcode Curl_auth_create_ntlm_type3_message(struct SessionHandle *data,
  416. const char *userp,
  417. const char *passwdp,
  418. struct ntlmdata *ntlm,
  419. char **outptr, size_t *outlen)
  420. {
  421. /* NTLM type-3 message structure:
  422. Index Description Content
  423. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  424. (0x4e544c4d53535000)
  425. 8 NTLM Message Type long (0x03000000)
  426. 12 LM/LMv2 Response security buffer
  427. 20 NTLM/NTLMv2 Response security buffer
  428. 28 Target Name security buffer
  429. 36 User Name security buffer
  430. 44 Workstation Name security buffer
  431. (52) Session Key security buffer (*)
  432. (60) Flags long (*)
  433. (64) OS Version Structure 8 bytes (*)
  434. 52 (64) (72) Start of data block
  435. (*) -> Optional
  436. */
  437. CURLcode result = CURLE_OK;
  438. size_t size;
  439. unsigned char ntlmbuf[NTLM_BUFSIZE];
  440. int lmrespoff;
  441. unsigned char lmresp[24]; /* fixed-size */
  442. #if USE_NTRESPONSES
  443. int ntrespoff;
  444. unsigned int ntresplen = 24;
  445. unsigned char ntresp[24]; /* fixed-size */
  446. unsigned char *ptr_ntresp = &ntresp[0];
  447. unsigned char *ntlmv2resp = NULL;
  448. #endif
  449. bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
  450. char host[HOSTNAME_MAX + 1] = "";
  451. const char *user;
  452. const char *domain = "";
  453. size_t hostoff = 0;
  454. size_t useroff = 0;
  455. size_t domoff = 0;
  456. size_t hostlen = 0;
  457. size_t userlen = 0;
  458. size_t domlen = 0;
  459. user = strchr(userp, '\\');
  460. if(!user)
  461. user = strchr(userp, '/');
  462. if(user) {
  463. domain = userp;
  464. domlen = (user - domain);
  465. user++;
  466. }
  467. else
  468. user = userp;
  469. if(user)
  470. userlen = strlen(user);
  471. /* Get the machine's un-qualified host name as NTLM doesn't like the fully
  472. qualified domain name */
  473. if(Curl_gethostname(host, sizeof(host))) {
  474. infof(data, "gethostname() failed, continuing without!\n");
  475. hostlen = 0;
  476. }
  477. else {
  478. hostlen = strlen(host);
  479. }
  480. #if USE_NTRESPONSES && USE_NTLM_V2
  481. if(ntlm->target_info_len) {
  482. unsigned char ntbuffer[0x18];
  483. unsigned int entropy[2];
  484. unsigned char ntlmv2hash[0x18];
  485. entropy[0] = Curl_rand(data);
  486. entropy[1] = Curl_rand(data);
  487. result = Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer);
  488. if(result)
  489. return result;
  490. result = Curl_ntlm_core_mk_ntlmv2_hash(user, userlen, domain, domlen,
  491. ntbuffer, ntlmv2hash);
  492. if(result)
  493. return result;
  494. /* LMv2 response */
  495. result = Curl_ntlm_core_mk_lmv2_resp(ntlmv2hash,
  496. (unsigned char *)&entropy[0],
  497. &ntlm->nonce[0], lmresp);
  498. if(result)
  499. return result;
  500. /* NTLMv2 response */
  501. result = Curl_ntlm_core_mk_ntlmv2_resp(ntlmv2hash,
  502. (unsigned char *)&entropy[0],
  503. ntlm, &ntlmv2resp, &ntresplen);
  504. if(result)
  505. return result;
  506. ptr_ntresp = ntlmv2resp;
  507. }
  508. else
  509. #endif
  510. #if USE_NTRESPONSES && USE_NTLM2SESSION
  511. /* We don't support NTLM2 if we don't have USE_NTRESPONSES */
  512. if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
  513. unsigned char ntbuffer[0x18];
  514. unsigned char tmp[0x18];
  515. unsigned char md5sum[MD5_DIGEST_LENGTH];
  516. unsigned int entropy[2];
  517. /* Need to create 8 bytes random data */
  518. entropy[0] = Curl_rand(data);
  519. entropy[1] = Curl_rand(data);
  520. /* 8 bytes random data as challenge in lmresp */
  521. memcpy(lmresp, entropy, 8);
  522. /* Pad with zeros */
  523. memset(lmresp + 8, 0, 0x10);
  524. /* Fill tmp with challenge(nonce?) + entropy */
  525. memcpy(tmp, &ntlm->nonce[0], 8);
  526. memcpy(tmp + 8, entropy, 8);
  527. result = Curl_ssl_md5sum(tmp, 16, md5sum, MD5_DIGEST_LENGTH);
  528. if(!result)
  529. /* We shall only use the first 8 bytes of md5sum, but the des code in
  530. Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
  531. result = Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer);
  532. if(result)
  533. return result;
  534. Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
  535. /* End of NTLM2 Session code */
  536. }
  537. else
  538. #endif
  539. {
  540. #if USE_NTRESPONSES
  541. unsigned char ntbuffer[0x18];
  542. #endif
  543. unsigned char lmbuffer[0x18];
  544. #if USE_NTRESPONSES
  545. result = Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer);
  546. if(result)
  547. return result;
  548. Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
  549. #endif
  550. result = Curl_ntlm_core_mk_lm_hash(data, passwdp, lmbuffer);
  551. if(result)
  552. return result;
  553. Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
  554. /* A safer but less compatible alternative is:
  555. * Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
  556. * See http://davenport.sourceforge.net/ntlm.html#ntlmVersion2 */
  557. }
  558. if(unicode) {
  559. domlen = domlen * 2;
  560. userlen = userlen * 2;
  561. hostlen = hostlen * 2;
  562. }
  563. lmrespoff = 64; /* size of the message header */
  564. #if USE_NTRESPONSES
  565. ntrespoff = lmrespoff + 0x18;
  566. domoff = ntrespoff + ntresplen;
  567. #else
  568. domoff = lmrespoff + 0x18;
  569. #endif
  570. useroff = domoff + domlen;
  571. hostoff = useroff + userlen;
  572. /* Create the big type-3 message binary blob */
  573. size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
  574. NTLMSSP_SIGNATURE "%c"
  575. "\x03%c%c%c" /* 32-bit type = 3 */
  576. "%c%c" /* LanManager length */
  577. "%c%c" /* LanManager allocated space */
  578. "%c%c" /* LanManager offset */
  579. "%c%c" /* 2 zeroes */
  580. "%c%c" /* NT-response length */
  581. "%c%c" /* NT-response allocated space */
  582. "%c%c" /* NT-response offset */
  583. "%c%c" /* 2 zeroes */
  584. "%c%c" /* domain length */
  585. "%c%c" /* domain allocated space */
  586. "%c%c" /* domain name offset */
  587. "%c%c" /* 2 zeroes */
  588. "%c%c" /* user length */
  589. "%c%c" /* user allocated space */
  590. "%c%c" /* user offset */
  591. "%c%c" /* 2 zeroes */
  592. "%c%c" /* host length */
  593. "%c%c" /* host allocated space */
  594. "%c%c" /* host offset */
  595. "%c%c" /* 2 zeroes */
  596. "%c%c" /* session key length (unknown purpose) */
  597. "%c%c" /* session key allocated space (unknown purpose) */
  598. "%c%c" /* session key offset (unknown purpose) */
  599. "%c%c" /* 2 zeroes */
  600. "%c%c%c%c", /* flags */
  601. /* domain string */
  602. /* user string */
  603. /* host string */
  604. /* LanManager response */
  605. /* NT response */
  606. 0, /* zero termination */
  607. 0, 0, 0, /* type-3 long, the 24 upper bits */
  608. SHORTPAIR(0x18), /* LanManager response length, twice */
  609. SHORTPAIR(0x18),
  610. SHORTPAIR(lmrespoff),
  611. 0x0, 0x0,
  612. #if USE_NTRESPONSES
  613. SHORTPAIR(ntresplen), /* NT-response length, twice */
  614. SHORTPAIR(ntresplen),
  615. SHORTPAIR(ntrespoff),
  616. 0x0, 0x0,
  617. #else
  618. 0x0, 0x0,
  619. 0x0, 0x0,
  620. 0x0, 0x0,
  621. 0x0, 0x0,
  622. #endif
  623. SHORTPAIR(domlen),
  624. SHORTPAIR(domlen),
  625. SHORTPAIR(domoff),
  626. 0x0, 0x0,
  627. SHORTPAIR(userlen),
  628. SHORTPAIR(userlen),
  629. SHORTPAIR(useroff),
  630. 0x0, 0x0,
  631. SHORTPAIR(hostlen),
  632. SHORTPAIR(hostlen),
  633. SHORTPAIR(hostoff),
  634. 0x0, 0x0,
  635. 0x0, 0x0,
  636. 0x0, 0x0,
  637. 0x0, 0x0,
  638. 0x0, 0x0,
  639. LONGQUARTET(ntlm->flags));
  640. DEBUGASSERT(size == 64);
  641. DEBUGASSERT(size == (size_t)lmrespoff);
  642. /* We append the binary hashes */
  643. if(size < (NTLM_BUFSIZE - 0x18)) {
  644. memcpy(&ntlmbuf[size], lmresp, 0x18);
  645. size += 0x18;
  646. }
  647. DEBUG_OUT({
  648. fprintf(stderr, "**** TYPE3 header lmresp=");
  649. ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
  650. });
  651. #if USE_NTRESPONSES
  652. if(size < (NTLM_BUFSIZE - ntresplen)) {
  653. DEBUGASSERT(size == (size_t)ntrespoff);
  654. memcpy(&ntlmbuf[size], ptr_ntresp, ntresplen);
  655. size += ntresplen;
  656. }
  657. DEBUG_OUT({
  658. fprintf(stderr, "\n ntresp=");
  659. ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], ntresplen);
  660. });
  661. free(ntlmv2resp);/* Free the dynamic buffer allocated for NTLMv2 */
  662. #endif
  663. DEBUG_OUT({
  664. fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
  665. LONGQUARTET(ntlm->flags), ntlm->flags);
  666. ntlm_print_flags(stderr, ntlm->flags);
  667. fprintf(stderr, "\n****\n");
  668. });
  669. /* Make sure that the domain, user and host strings fit in the
  670. buffer before we copy them there. */
  671. if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
  672. failf(data, "user + domain + host name too big");
  673. return CURLE_OUT_OF_MEMORY;
  674. }
  675. DEBUGASSERT(size == domoff);
  676. if(unicode)
  677. unicodecpy(&ntlmbuf[size], domain, domlen / 2);
  678. else
  679. memcpy(&ntlmbuf[size], domain, domlen);
  680. size += domlen;
  681. DEBUGASSERT(size == useroff);
  682. if(unicode)
  683. unicodecpy(&ntlmbuf[size], user, userlen / 2);
  684. else
  685. memcpy(&ntlmbuf[size], user, userlen);
  686. size += userlen;
  687. DEBUGASSERT(size == hostoff);
  688. if(unicode)
  689. unicodecpy(&ntlmbuf[size], host, hostlen / 2);
  690. else
  691. memcpy(&ntlmbuf[size], host, hostlen);
  692. size += hostlen;
  693. /* Convert domain, user, and host to ASCII but leave the rest as-is */
  694. result = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
  695. size - domoff);
  696. if(result)
  697. return CURLE_CONV_FAILED;
  698. /* Return with binary blob encoded into base64 */
  699. result = Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
  700. Curl_auth_ntlm_cleanup(ntlm);
  701. return result;
  702. }
  703. /*
  704. * Curl_auth_ntlm_cleanup()
  705. *
  706. * This is used to clean up the NTLM specific data.
  707. *
  708. * Parameters:
  709. *
  710. * ntlm [in/out] - The NTLM data struct being cleaned up.
  711. *
  712. */
  713. void Curl_auth_ntlm_cleanup(struct ntlmdata *ntlm)
  714. {
  715. /* Free the target info */
  716. Curl_safefree(ntlm->target_info);
  717. /* Reset any variables */
  718. ntlm->target_info_len = 0;
  719. }
  720. #endif /* USE_NTLM && !USE_WINDOWS_SSPI */