curl_ntlm_msgs.c 32 KB

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