ntlm.c 26 KB

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