ntlm.c 27 KB

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