curl_ntlm_msgs.c 29 KB

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