ntlm.c 27 KB

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