curl_ntlm_msgs.c 29 KB

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