ntlm.c 28 KB

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