ntlm.c 29 KB

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