ntlm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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.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. * https://davenport.sourceforge.io/ntlm.html
  28. * https://www.innovation.ch/java/ntlm.html
  29. */
  30. #define DEBUG_ME 0
  31. #include "urldata.h"
  32. #include "sendf.h"
  33. #include "curl_ntlm_core.h"
  34. #include "curl_gethostname.h"
  35. #include "curl_multibyte.h"
  36. #include "curl_md5.h"
  37. #include "warnless.h"
  38. #include "rand.h"
  39. #include "vtls/vtls.h"
  40. /* SSL backend-specific #if branches in this file must be kept in the order
  41. documented in curl_ntlm_core. */
  42. #if defined(NTLM_NEEDS_NSS_INIT)
  43. #include "vtls/nssg.h" /* for Curl_nss_force_init() */
  44. #endif
  45. #define BUILDING_CURL_NTLM_MSGS_C
  46. #include "vauth/vauth.h"
  47. #include "vauth/ntlm.h"
  48. #include "curl_endian.h"
  49. #include "curl_printf.h"
  50. /* The last #include files should be: */
  51. #include "curl_memory.h"
  52. #include "memdebug.h"
  53. /* "NTLMSSP" signature is always in ASCII regardless of the platform */
  54. #define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
  55. #if DEBUG_ME
  56. # define DEBUG_OUT(x) x
  57. static void ntlm_print_flags(FILE *handle, unsigned long flags)
  58. {
  59. if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
  60. fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
  61. if(flags & NTLMFLAG_NEGOTIATE_OEM)
  62. fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
  63. if(flags & NTLMFLAG_REQUEST_TARGET)
  64. fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
  65. if(flags & (1<<3))
  66. fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
  67. if(flags & NTLMFLAG_NEGOTIATE_SIGN)
  68. fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
  69. if(flags & NTLMFLAG_NEGOTIATE_SEAL)
  70. fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
  71. if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
  72. fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
  73. if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
  74. fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
  75. if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
  76. fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
  77. if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
  78. fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
  79. if(flags & (1<<10))
  80. fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
  81. if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
  82. fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
  83. if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
  84. fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
  85. if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
  86. fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
  87. if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
  88. fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
  89. if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
  90. fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
  91. if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
  92. fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
  93. if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
  94. fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
  95. if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
  96. fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
  97. if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
  98. fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
  99. if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
  100. fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
  101. if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
  102. fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
  103. if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
  104. fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
  105. if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
  106. fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
  107. if(flags & (1<<24))
  108. fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
  109. if(flags & (1<<25))
  110. fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
  111. if(flags & (1<<26))
  112. fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
  113. if(flags & (1<<27))
  114. fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
  115. if(flags & (1<<28))
  116. fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
  117. if(flags & NTLMFLAG_NEGOTIATE_128)
  118. fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
  119. if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
  120. fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
  121. if(flags & NTLMFLAG_NEGOTIATE_56)
  122. fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
  123. }
  124. static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
  125. {
  126. const char *p = buf;
  127. (void) handle;
  128. fprintf(stderr, "0x");
  129. while(len-- > 0)
  130. fprintf(stderr, "%02.2x", (unsigned int)*p++);
  131. }
  132. #else
  133. # define DEBUG_OUT(x) Curl_nop_stmt
  134. #endif
  135. /*
  136. * ntlm_decode_type2_target()
  137. *
  138. * This is used to decode the "target info" in the NTLM type-2 message
  139. * received.
  140. *
  141. * Parameters:
  142. *
  143. * data [in] - The session handle.
  144. * type2ref [in] - The type-2 message.
  145. * ntlm [in/out] - The NTLM data struct being used and modified.
  146. *
  147. * Returns CURLE_OK on success.
  148. */
  149. static CURLcode ntlm_decode_type2_target(struct Curl_easy *data,
  150. const struct bufref *type2ref,
  151. struct ntlmdata *ntlm)
  152. {
  153. unsigned short target_info_len = 0;
  154. unsigned int target_info_offset = 0;
  155. const unsigned char *type2 = Curl_bufref_ptr(type2ref);
  156. size_t type2len = Curl_bufref_len(type2ref);
  157. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  158. (void) data;
  159. #endif
  160. if(type2len >= 48) {
  161. target_info_len = Curl_read16_le(&type2[40]);
  162. target_info_offset = Curl_read32_le(&type2[44]);
  163. if(target_info_len > 0) {
  164. if((target_info_offset > type2len) ||
  165. (target_info_offset + target_info_len) > type2len ||
  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");
  169. return CURLE_BAD_CONTENT_ENCODING;
  170. }
  171. free(ntlm->target_info); /* replace any previous data */
  172. ntlm->target_info = malloc(target_info_len);
  173. if(!ntlm->target_info)
  174. return CURLE_OUT_OF_MEMORY;
  175. memcpy(ntlm->target_info, &type2[target_info_offset], target_info_len);
  176. }
  177. }
  178. ntlm->target_info_len = target_info_len;
  179. return CURLE_OK;
  180. }
  181. /*
  182. NTLM message structure notes:
  183. A 'short' is a 'network short', a little-endian 16-bit unsigned value.
  184. A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
  185. A 'security buffer' represents a triplet used to point to a buffer,
  186. consisting of two shorts and one long:
  187. 1. A 'short' containing the length of the buffer content in bytes.
  188. 2. A 'short' containing the allocated space for the buffer in bytes.
  189. 3. A 'long' containing the offset to the start of the buffer in bytes,
  190. from the beginning of the NTLM message.
  191. */
  192. /*
  193. * Curl_auth_is_ntlm_supported()
  194. *
  195. * This is used to evaluate if NTLM is supported.
  196. *
  197. * Parameters: None
  198. *
  199. * Returns TRUE as NTLM as handled by libcurl.
  200. */
  201. bool Curl_auth_is_ntlm_supported(void)
  202. {
  203. return TRUE;
  204. }
  205. /*
  206. * Curl_auth_decode_ntlm_type2_message()
  207. *
  208. * This is used to decode an NTLM type-2 message. The raw NTLM message is
  209. * checked * for validity before the appropriate data for creating a type-3
  210. * message is * written to the given NTLM data structure.
  211. *
  212. * Parameters:
  213. *
  214. * data [in] - The session handle.
  215. * type2ref [in] - The type-2 message.
  216. * ntlm [in/out] - The NTLM data struct being used and modified.
  217. *
  218. * Returns CURLE_OK on success.
  219. */
  220. CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
  221. const struct bufref *type2ref,
  222. struct ntlmdata *ntlm)
  223. {
  224. static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
  225. /* NTLM type-2 message structure:
  226. Index Description Content
  227. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  228. (0x4e544c4d53535000)
  229. 8 NTLM Message Type long (0x02000000)
  230. 12 Target Name security buffer
  231. 20 Flags long
  232. 24 Challenge 8 bytes
  233. (32) Context 8 bytes (two consecutive longs) (*)
  234. (40) Target Information security buffer (*)
  235. (48) OS Version Structure 8 bytes (*)
  236. 32 (48) (56) Start of data block (*)
  237. (*) -> Optional
  238. */
  239. CURLcode result = CURLE_OK;
  240. const unsigned char *type2 = Curl_bufref_ptr(type2ref);
  241. size_t type2len = Curl_bufref_len(type2ref);
  242. #if defined(NTLM_NEEDS_NSS_INIT)
  243. /* Make sure the crypto backend is initialized */
  244. result = Curl_nss_force_init(data);
  245. if(result)
  246. return result;
  247. #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
  248. (void)data;
  249. #endif
  250. ntlm->flags = 0;
  251. if((type2len < 32) ||
  252. (memcmp(type2, NTLMSSP_SIGNATURE, 8) != 0) ||
  253. (memcmp(type2 + 8, type2_marker, sizeof(type2_marker)) != 0)) {
  254. /* This was not a good enough type-2 message */
  255. infof(data, "NTLM handshake failure (bad type-2 message)");
  256. return CURLE_BAD_CONTENT_ENCODING;
  257. }
  258. ntlm->flags = Curl_read32_le(&type2[20]);
  259. memcpy(ntlm->nonce, &type2[24], 8);
  260. if(ntlm->flags & NTLMFLAG_NEGOTIATE_TARGET_INFO) {
  261. result = ntlm_decode_type2_target(data, type2ref, ntlm);
  262. if(result) {
  263. infof(data, "NTLM handshake failure (bad type-2 message)");
  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. return result;
  276. }
  277. /* copy the source to the destination and fill in zeroes in every
  278. other destination byte! */
  279. static void unicodecpy(unsigned char *dest, const char *src, size_t length)
  280. {
  281. size_t i;
  282. for(i = 0; i < length; i++) {
  283. dest[2 * i] = (unsigned char)src[i];
  284. dest[2 * i + 1] = '\0';
  285. }
  286. }
  287. /*
  288. * Curl_auth_create_ntlm_type1_message()
  289. *
  290. * This is used to generate an NTLM type-1 message ready for sending to the
  291. * recipient using the appropriate compile time crypto API.
  292. *
  293. * Parameters:
  294. *
  295. * data [in] - The session handle.
  296. * userp [in] - The user name in the format User or Domain\User.
  297. * passwdp [in] - The user's password.
  298. * service [in] - The service type such as http, smtp, pop or imap.
  299. * host [in] - The host name.
  300. * ntlm [in/out] - The NTLM data struct being used and modified.
  301. * out [out] - The result storage.
  302. *
  303. * Returns CURLE_OK on success.
  304. */
  305. CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
  306. const char *userp,
  307. const char *passwdp,
  308. const char *service,
  309. const char *hostname,
  310. struct ntlmdata *ntlm,
  311. struct bufref *out)
  312. {
  313. /* NTLM type-1 message structure:
  314. Index Description Content
  315. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  316. (0x4e544c4d53535000)
  317. 8 NTLM Message Type long (0x01000000)
  318. 12 Flags long
  319. (16) Supplied Domain security buffer (*)
  320. (24) Supplied Workstation security buffer (*)
  321. (32) OS Version Structure 8 bytes (*)
  322. (32) (40) Start of data block (*)
  323. (*) -> Optional
  324. */
  325. size_t size;
  326. char *ntlmbuf;
  327. const char *host = ""; /* empty */
  328. const char *domain = ""; /* empty */
  329. size_t hostlen = 0;
  330. size_t domlen = 0;
  331. size_t hostoff = 0;
  332. size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
  333. domain are empty */
  334. (void)data;
  335. (void)userp;
  336. (void)passwdp;
  337. (void)service,
  338. (void)hostname,
  339. /* Clean up any former leftovers and initialise to defaults */
  340. Curl_auth_cleanup_ntlm(ntlm);
  341. ntlmbuf = aprintf(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. NTLMFLAG_NEGOTIATE_NTLM2_KEY |
  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. if(!ntlmbuf)
  372. return CURLE_OUT_OF_MEMORY;
  373. /* Initial packet length */
  374. size = 32 + hostlen + domlen;
  375. DEBUG_OUT({
  376. fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
  377. "0x%08.8x ",
  378. LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
  379. NTLMFLAG_REQUEST_TARGET |
  380. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  381. NTLMFLAG_NEGOTIATE_NTLM2_KEY |
  382. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
  383. NTLMFLAG_NEGOTIATE_OEM |
  384. NTLMFLAG_REQUEST_TARGET |
  385. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  386. NTLMFLAG_NEGOTIATE_NTLM2_KEY |
  387. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
  388. ntlm_print_flags(stderr,
  389. NTLMFLAG_NEGOTIATE_OEM |
  390. NTLMFLAG_REQUEST_TARGET |
  391. NTLMFLAG_NEGOTIATE_NTLM_KEY |
  392. NTLMFLAG_NEGOTIATE_NTLM2_KEY |
  393. NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
  394. fprintf(stderr, "\n****\n");
  395. });
  396. Curl_bufref_set(out, ntlmbuf, size, curl_free);
  397. return CURLE_OK;
  398. }
  399. /*
  400. * Curl_auth_create_ntlm_type3_message()
  401. *
  402. * This is used to generate an already encoded NTLM type-3 message ready for
  403. * sending to the recipient using the appropriate compile time crypto API.
  404. *
  405. * Parameters:
  406. *
  407. * data [in] - The session handle.
  408. * userp [in] - The user name in the format User or Domain\User.
  409. * passwdp [in] - The user's password.
  410. * ntlm [in/out] - The NTLM data struct being used and modified.
  411. * out [out] - The result storage.
  412. *
  413. * Returns CURLE_OK on success.
  414. */
  415. CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
  416. const char *userp,
  417. const char *passwdp,
  418. struct ntlmdata *ntlm,
  419. struct bufref *out)
  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. int ntrespoff;
  443. unsigned int ntresplen = 24;
  444. unsigned char ntresp[24]; /* fixed-size */
  445. unsigned char *ptr_ntresp = &ntresp[0];
  446. unsigned char *ntlmv2resp = NULL;
  447. bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
  448. char host[HOSTNAME_MAX + 1] = "";
  449. const char *user;
  450. const char *domain = "";
  451. size_t hostoff = 0;
  452. size_t useroff = 0;
  453. size_t domoff = 0;
  454. size_t hostlen = 0;
  455. size_t userlen = 0;
  456. size_t domlen = 0;
  457. user = strchr(userp, '\\');
  458. if(!user)
  459. user = strchr(userp, '/');
  460. if(user) {
  461. domain = userp;
  462. domlen = (user - domain);
  463. user++;
  464. }
  465. else
  466. user = userp;
  467. userlen = strlen(user);
  468. /* Get the machine's un-qualified host name as NTLM doesn't like the fully
  469. qualified domain name */
  470. if(Curl_gethostname(host, sizeof(host))) {
  471. infof(data, "gethostname() failed, continuing without");
  472. hostlen = 0;
  473. }
  474. else {
  475. hostlen = strlen(host);
  476. }
  477. if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
  478. unsigned char ntbuffer[0x18];
  479. unsigned char entropy[8];
  480. unsigned char ntlmv2hash[0x18];
  481. /* Full NTLM version 2
  482. Although this cannot be negotiated, it is used here if available, as
  483. servers featuring extended security are likely supporting also
  484. NTLMv2. */
  485. result = Curl_rand(data, entropy, 8);
  486. if(result)
  487. return result;
  488. result = Curl_ntlm_core_mk_nt_hash(passwdp, ntbuffer);
  489. if(result)
  490. return result;
  491. result = Curl_ntlm_core_mk_ntlmv2_hash(user, userlen, domain, domlen,
  492. ntbuffer, ntlmv2hash);
  493. if(result)
  494. return result;
  495. /* LMv2 response */
  496. result = Curl_ntlm_core_mk_lmv2_resp(ntlmv2hash, entropy,
  497. &ntlm->nonce[0], lmresp);
  498. if(result)
  499. return result;
  500. /* NTLMv2 response */
  501. result = Curl_ntlm_core_mk_ntlmv2_resp(ntlmv2hash, entropy,
  502. ntlm, &ntlmv2resp, &ntresplen);
  503. if(result)
  504. return result;
  505. ptr_ntresp = ntlmv2resp;
  506. }
  507. else {
  508. unsigned char ntbuffer[0x18];
  509. unsigned char lmbuffer[0x18];
  510. /* NTLM version 1 */
  511. result = Curl_ntlm_core_mk_nt_hash(passwdp, ntbuffer);
  512. if(result)
  513. return result;
  514. Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
  515. result = Curl_ntlm_core_mk_lm_hash(passwdp, lmbuffer);
  516. if(result)
  517. return result;
  518. Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
  519. ntlm->flags &= ~NTLMFLAG_NEGOTIATE_NTLM2_KEY;
  520. /* A safer but less compatible alternative is:
  521. * Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
  522. * See https://davenport.sourceforge.io/ntlm.html#ntlmVersion2 */
  523. }
  524. if(unicode) {
  525. domlen = domlen * 2;
  526. userlen = userlen * 2;
  527. hostlen = hostlen * 2;
  528. }
  529. lmrespoff = 64; /* size of the message header */
  530. ntrespoff = lmrespoff + 0x18;
  531. domoff = ntrespoff + ntresplen;
  532. useroff = domoff + domlen;
  533. hostoff = useroff + userlen;
  534. /* Create the big type-3 message binary blob */
  535. size = msnprintf((char *)ntlmbuf, NTLM_BUFSIZE,
  536. NTLMSSP_SIGNATURE "%c"
  537. "\x03%c%c%c" /* 32-bit type = 3 */
  538. "%c%c" /* LanManager length */
  539. "%c%c" /* LanManager allocated space */
  540. "%c%c" /* LanManager offset */
  541. "%c%c" /* 2 zeroes */
  542. "%c%c" /* NT-response length */
  543. "%c%c" /* NT-response allocated space */
  544. "%c%c" /* NT-response offset */
  545. "%c%c" /* 2 zeroes */
  546. "%c%c" /* domain length */
  547. "%c%c" /* domain allocated space */
  548. "%c%c" /* domain name offset */
  549. "%c%c" /* 2 zeroes */
  550. "%c%c" /* user length */
  551. "%c%c" /* user allocated space */
  552. "%c%c" /* user offset */
  553. "%c%c" /* 2 zeroes */
  554. "%c%c" /* host length */
  555. "%c%c" /* host allocated space */
  556. "%c%c" /* host offset */
  557. "%c%c" /* 2 zeroes */
  558. "%c%c" /* session key length (unknown purpose) */
  559. "%c%c" /* session key allocated space (unknown purpose) */
  560. "%c%c" /* session key offset (unknown purpose) */
  561. "%c%c" /* 2 zeroes */
  562. "%c%c%c%c", /* flags */
  563. /* domain string */
  564. /* user string */
  565. /* host string */
  566. /* LanManager response */
  567. /* NT response */
  568. 0, /* zero termination */
  569. 0, 0, 0, /* type-3 long, the 24 upper bits */
  570. SHORTPAIR(0x18), /* LanManager response length, twice */
  571. SHORTPAIR(0x18),
  572. SHORTPAIR(lmrespoff),
  573. 0x0, 0x0,
  574. SHORTPAIR(ntresplen), /* NT-response length, twice */
  575. SHORTPAIR(ntresplen),
  576. SHORTPAIR(ntrespoff),
  577. 0x0, 0x0,
  578. SHORTPAIR(domlen),
  579. SHORTPAIR(domlen),
  580. SHORTPAIR(domoff),
  581. 0x0, 0x0,
  582. SHORTPAIR(userlen),
  583. SHORTPAIR(userlen),
  584. SHORTPAIR(useroff),
  585. 0x0, 0x0,
  586. SHORTPAIR(hostlen),
  587. SHORTPAIR(hostlen),
  588. SHORTPAIR(hostoff),
  589. 0x0, 0x0,
  590. 0x0, 0x0,
  591. 0x0, 0x0,
  592. 0x0, 0x0,
  593. 0x0, 0x0,
  594. LONGQUARTET(ntlm->flags));
  595. DEBUGASSERT(size == 64);
  596. DEBUGASSERT(size == (size_t)lmrespoff);
  597. /* We append the binary hashes */
  598. if(size < (NTLM_BUFSIZE - 0x18)) {
  599. memcpy(&ntlmbuf[size], lmresp, 0x18);
  600. size += 0x18;
  601. }
  602. DEBUG_OUT({
  603. fprintf(stderr, "**** TYPE3 header lmresp=");
  604. ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
  605. });
  606. /* ntresplen + size should not be risking an integer overflow here */
  607. if(ntresplen + size > sizeof(ntlmbuf)) {
  608. failf(data, "incoming NTLM message too big");
  609. return CURLE_OUT_OF_MEMORY;
  610. }
  611. DEBUGASSERT(size == (size_t)ntrespoff);
  612. memcpy(&ntlmbuf[size], ptr_ntresp, ntresplen);
  613. size += ntresplen;
  614. DEBUG_OUT({
  615. fprintf(stderr, "\n ntresp=");
  616. ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], ntresplen);
  617. });
  618. free(ntlmv2resp);/* Free the dynamic buffer allocated for NTLMv2 */
  619. DEBUG_OUT({
  620. fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
  621. LONGQUARTET(ntlm->flags), ntlm->flags);
  622. ntlm_print_flags(stderr, ntlm->flags);
  623. fprintf(stderr, "\n****\n");
  624. });
  625. /* Make sure that the domain, user and host strings fit in the
  626. buffer before we copy them there. */
  627. if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
  628. failf(data, "user + domain + host name too big");
  629. return CURLE_OUT_OF_MEMORY;
  630. }
  631. DEBUGASSERT(size == domoff);
  632. if(unicode)
  633. unicodecpy(&ntlmbuf[size], domain, domlen / 2);
  634. else
  635. memcpy(&ntlmbuf[size], domain, domlen);
  636. size += domlen;
  637. DEBUGASSERT(size == useroff);
  638. if(unicode)
  639. unicodecpy(&ntlmbuf[size], user, userlen / 2);
  640. else
  641. memcpy(&ntlmbuf[size], user, userlen);
  642. size += userlen;
  643. DEBUGASSERT(size == hostoff);
  644. if(unicode)
  645. unicodecpy(&ntlmbuf[size], host, hostlen / 2);
  646. else
  647. memcpy(&ntlmbuf[size], host, hostlen);
  648. size += hostlen;
  649. /* Return the binary blob. */
  650. result = Curl_bufref_memdup(out, ntlmbuf, size);
  651. Curl_auth_cleanup_ntlm(ntlm);
  652. return result;
  653. }
  654. /*
  655. * Curl_auth_cleanup_ntlm()
  656. *
  657. * This is used to clean up the NTLM specific data.
  658. *
  659. * Parameters:
  660. *
  661. * ntlm [in/out] - The NTLM data struct being cleaned up.
  662. *
  663. */
  664. void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm)
  665. {
  666. /* Free the target info */
  667. Curl_safefree(ntlm->target_info);
  668. /* Reset any variables */
  669. ntlm->target_info_len = 0;
  670. }
  671. #endif /* USE_NTLM && !USE_WINDOWS_SSPI */