ntlm.c 27 KB

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