诸暨麻将添加redis
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 
 

921 行
29 KiB

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