curl_ntlm_core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(USE_CURL_NTLM_CORE)
  26. /*
  27. * NTLM details:
  28. *
  29. * https://davenport.sourceforge.net/ntlm.html
  30. * https://www.innovation.ch/java/ntlm.html
  31. */
  32. /* Please keep the SSL backend-specific #if branches in this order:
  33. 1. USE_OPENSSL
  34. 2. USE_WOLFSSL
  35. 3. USE_GNUTLS
  36. 4. -
  37. 5. USE_MBEDTLS
  38. 6. USE_OS400CRYPTO
  39. 7. USE_WIN32_CRYPTO
  40. This ensures that:
  41. - the same SSL branch gets activated throughout this source
  42. file even if multiple backends are enabled at the same time.
  43. - OpenSSL has higher priority than Windows Crypt, due
  44. to issues with the latter supporting NTLM2Session responses
  45. in NTLM type-3 messages.
  46. */
  47. #if defined(USE_OPENSSL)
  48. #include <openssl/opensslconf.h>
  49. #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  50. #define USE_OPENSSL_DES
  51. #endif
  52. #elif defined(USE_WOLFSSL)
  53. #include <wolfssl/options.h>
  54. #if !defined(NO_DES3)
  55. #define USE_OPENSSL_DES
  56. #endif
  57. #endif
  58. #if defined(USE_OPENSSL_DES)
  59. #if defined(USE_OPENSSL)
  60. # include <openssl/des.h>
  61. # include <openssl/md5.h>
  62. # include <openssl/ssl.h>
  63. # include <openssl/rand.h>
  64. # if defined(OPENSSL_IS_AWSLC)
  65. # define DES_set_key_unchecked (void)DES_set_key
  66. # define DESKEYARG(x) *x
  67. # define DESKEY(x) &x
  68. # else
  69. # define DESKEYARG(x) *x
  70. # define DESKEY(x) &x
  71. # endif
  72. #else
  73. # include <wolfssl/openssl/des.h>
  74. # include <wolfssl/openssl/md5.h>
  75. # include <wolfssl/openssl/ssl.h>
  76. # include <wolfssl/openssl/rand.h>
  77. # if defined(OPENSSL_COEXIST)
  78. # define DES_key_schedule WOLFSSL_DES_key_schedule
  79. # define DES_cblock WOLFSSL_DES_cblock
  80. # define DES_set_odd_parity wolfSSL_DES_set_odd_parity
  81. # define DES_set_key wolfSSL_DES_set_key
  82. # define DES_set_key_unchecked wolfSSL_DES_set_key_unchecked
  83. # define DES_ecb_encrypt wolfSSL_DES_ecb_encrypt
  84. # define DESKEY(x) ((WOLFSSL_DES_key_schedule *)(x))
  85. # define DESKEYARG(x) *x
  86. # else
  87. # define DESKEYARG(x) *x
  88. # define DESKEY(x) &x
  89. # endif
  90. #endif
  91. #elif defined(USE_GNUTLS)
  92. # include <nettle/des.h>
  93. #elif defined(USE_MBEDTLS)
  94. # include <mbedtls/des.h>
  95. #elif defined(USE_OS400CRYPTO)
  96. # include "cipher.mih" /* mih/cipher */
  97. #elif defined(USE_WIN32_CRYPTO)
  98. # include <wincrypt.h>
  99. #else
  100. # error "cannot compile NTLM support without a crypto library with DES."
  101. # define CURL_NTLM_NOT_SUPPORTED
  102. #endif
  103. #include "urldata.h"
  104. #include "strcase.h"
  105. #include "curl_ntlm_core.h"
  106. #include "curl_md5.h"
  107. #include "curl_hmac.h"
  108. #include "curlx/warnless.h"
  109. #include "curl_endian.h"
  110. #include "curl_des.h"
  111. #include "curl_md4.h"
  112. /* The last 3 #include files should be in this order */
  113. #include "curl_printf.h"
  114. #include "curl_memory.h"
  115. #include "memdebug.h"
  116. #if !defined(CURL_NTLM_NOT_SUPPORTED)
  117. /*
  118. * Turns a 56-bit key into being 64-bit wide.
  119. */
  120. static void extend_key_56_to_64(const unsigned char *key_56, char *key)
  121. {
  122. key[0] = (char)key_56[0];
  123. key[1] = (char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
  124. key[2] = (char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
  125. key[3] = (char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
  126. key[4] = (char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
  127. key[5] = (char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
  128. key[6] = (char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
  129. key[7] = (char) ((key_56[6] << 1) & 0xFF);
  130. }
  131. #endif
  132. #if defined(USE_OPENSSL_DES)
  133. /*
  134. * Turns a 56-bit key into a 64-bit, odd parity key and sets the key. The
  135. * key schedule ks is also set.
  136. */
  137. static void setup_des_key(const unsigned char *key_56,
  138. DES_key_schedule DESKEYARG(ks))
  139. {
  140. DES_cblock key;
  141. /* Expand the 56-bit key to 64 bits */
  142. extend_key_56_to_64(key_56, (char *) &key);
  143. /* Set the key parity to odd */
  144. DES_set_odd_parity(&key);
  145. /* Set the key */
  146. DES_set_key_unchecked(&key, ks);
  147. }
  148. #elif defined(USE_GNUTLS)
  149. static void setup_des_key(const unsigned char *key_56,
  150. struct des_ctx *des)
  151. {
  152. char key[8];
  153. /* Expand the 56-bit key to 64 bits */
  154. extend_key_56_to_64(key_56, key);
  155. /* Set the key parity to odd */
  156. Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
  157. /* Set the key */
  158. des_set_key(des, (const uint8_t *) key);
  159. }
  160. #elif defined(USE_MBEDTLS)
  161. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  162. const unsigned char *key_56)
  163. {
  164. mbedtls_des_context ctx;
  165. char key[8];
  166. /* Expand the 56-bit key to 64 bits */
  167. extend_key_56_to_64(key_56, key);
  168. /* Set the key parity to odd */
  169. mbedtls_des_key_set_parity((unsigned char *) key);
  170. /* Perform the encryption */
  171. mbedtls_des_init(&ctx);
  172. mbedtls_des_setkey_enc(&ctx, (unsigned char *) key);
  173. return mbedtls_des_crypt_ecb(&ctx, in, out) == 0;
  174. }
  175. #elif defined(USE_OS400CRYPTO)
  176. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  177. const unsigned char *key_56)
  178. {
  179. char key[8];
  180. _CIPHER_Control_T ctl;
  181. /* Setup the cipher control structure */
  182. ctl.Func_ID = ENCRYPT_ONLY;
  183. ctl.Data_Len = sizeof(key);
  184. /* Expand the 56-bit key to 64 bits */
  185. extend_key_56_to_64(key_56, ctl.Crypto_Key);
  186. /* Set the key parity to odd */
  187. Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
  188. /* Perform the encryption */
  189. _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
  190. return TRUE;
  191. }
  192. #elif defined(USE_WIN32_CRYPTO)
  193. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  194. const unsigned char *key_56)
  195. {
  196. HCRYPTPROV hprov;
  197. HCRYPTKEY hkey;
  198. struct {
  199. BLOBHEADER hdr;
  200. unsigned int len;
  201. char key[8];
  202. } blob;
  203. DWORD len = 8;
  204. /* Acquire the crypto provider */
  205. if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
  206. CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  207. return FALSE;
  208. /* Setup the key blob structure */
  209. memset(&blob, 0, sizeof(blob));
  210. blob.hdr.bType = PLAINTEXTKEYBLOB;
  211. blob.hdr.bVersion = 2;
  212. blob.hdr.aiKeyAlg = CALG_DES;
  213. blob.len = sizeof(blob.key);
  214. /* Expand the 56-bit key to 64 bits */
  215. extend_key_56_to_64(key_56, blob.key);
  216. /* Set the key parity to odd */
  217. Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
  218. /* Import the key */
  219. if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
  220. CryptReleaseContext(hprov, 0);
  221. return FALSE;
  222. }
  223. memcpy(out, in, 8);
  224. /* Perform the encryption */
  225. CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
  226. CryptDestroyKey(hkey);
  227. CryptReleaseContext(hprov, 0);
  228. return TRUE;
  229. }
  230. #endif /* defined(USE_WIN32_CRYPTO) */
  231. /*
  232. * takes a 21 byte array and treats it as 3 56-bit DES keys. The
  233. * 8 byte plaintext is encrypted with each key and the resulting 24
  234. * bytes are stored in the results array.
  235. */
  236. void Curl_ntlm_core_lm_resp(const unsigned char *keys,
  237. const unsigned char *plaintext,
  238. unsigned char *results)
  239. {
  240. #if defined(USE_OPENSSL_DES)
  241. DES_key_schedule ks;
  242. setup_des_key(keys, DESKEY(ks));
  243. DES_ecb_encrypt((DES_cblock*)CURL_UNCONST(plaintext),
  244. (DES_cblock*)results, DESKEY(ks), DES_ENCRYPT);
  245. setup_des_key(keys + 7, DESKEY(ks));
  246. DES_ecb_encrypt((DES_cblock*)CURL_UNCONST(plaintext),
  247. (DES_cblock*)(results + 8), DESKEY(ks), DES_ENCRYPT);
  248. setup_des_key(keys + 14, DESKEY(ks));
  249. DES_ecb_encrypt((DES_cblock*)CURL_UNCONST(plaintext),
  250. (DES_cblock*)(results + 16), DESKEY(ks), DES_ENCRYPT);
  251. #elif defined(USE_GNUTLS)
  252. struct des_ctx des;
  253. setup_des_key(keys, &des);
  254. des_encrypt(&des, 8, results, plaintext);
  255. setup_des_key(keys + 7, &des);
  256. des_encrypt(&des, 8, results + 8, plaintext);
  257. setup_des_key(keys + 14, &des);
  258. des_encrypt(&des, 8, results + 16, plaintext);
  259. #elif defined(USE_MBEDTLS) || defined(USE_OS400CRYPTO) || \
  260. defined(USE_WIN32_CRYPTO)
  261. encrypt_des(plaintext, results, keys);
  262. encrypt_des(plaintext, results + 8, keys + 7);
  263. encrypt_des(plaintext, results + 16, keys + 14);
  264. #else
  265. (void)keys;
  266. (void)plaintext;
  267. (void)results;
  268. #endif
  269. }
  270. /*
  271. * Set up lanmanager hashed password
  272. */
  273. CURLcode Curl_ntlm_core_mk_lm_hash(const char *password,
  274. unsigned char *lmbuffer /* 21 bytes */)
  275. {
  276. unsigned char pw[14];
  277. #if !defined(CURL_NTLM_NOT_SUPPORTED)
  278. static const unsigned char magic[] = {
  279. 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
  280. };
  281. #endif
  282. size_t len = CURLMIN(strlen(password), 14);
  283. Curl_strntoupper((char *)pw, password, len);
  284. memset(&pw[len], 0, 14 - len);
  285. {
  286. /* Create LanManager hashed password. */
  287. #if defined(USE_OPENSSL_DES)
  288. DES_key_schedule ks;
  289. setup_des_key(pw, DESKEY(ks));
  290. DES_ecb_encrypt((DES_cblock *)CURL_UNCONST(magic),
  291. (DES_cblock *)lmbuffer, DESKEY(ks), DES_ENCRYPT);
  292. setup_des_key(pw + 7, DESKEY(ks));
  293. DES_ecb_encrypt((DES_cblock *)CURL_UNCONST(magic),
  294. (DES_cblock *)(lmbuffer + 8), DESKEY(ks), DES_ENCRYPT);
  295. #elif defined(USE_GNUTLS)
  296. struct des_ctx des;
  297. setup_des_key(pw, &des);
  298. des_encrypt(&des, 8, lmbuffer, magic);
  299. setup_des_key(pw + 7, &des);
  300. des_encrypt(&des, 8, lmbuffer + 8, magic);
  301. #elif defined(USE_MBEDTLS) || defined(USE_OS400CRYPTO) || \
  302. defined(USE_WIN32_CRYPTO)
  303. encrypt_des(magic, lmbuffer, pw);
  304. encrypt_des(magic, lmbuffer + 8, pw + 7);
  305. #endif
  306. memset(lmbuffer + 16, 0, 21 - 16);
  307. }
  308. return CURLE_OK;
  309. }
  310. static void ascii_to_unicode_le(unsigned char *dest, const char *src,
  311. size_t srclen)
  312. {
  313. size_t i;
  314. for(i = 0; i < srclen; i++) {
  315. dest[2 * i] = (unsigned char)src[i];
  316. dest[2 * i + 1] = '\0';
  317. }
  318. }
  319. #if !defined(USE_WINDOWS_SSPI)
  320. static void ascii_uppercase_to_unicode_le(unsigned char *dest,
  321. const char *src, size_t srclen)
  322. {
  323. size_t i;
  324. for(i = 0; i < srclen; i++) {
  325. dest[2 * i] = (unsigned char)(Curl_raw_toupper(src[i]));
  326. dest[2 * i + 1] = '\0';
  327. }
  328. }
  329. #endif /* !USE_WINDOWS_SSPI */
  330. /*
  331. * Set up nt hashed passwords
  332. * @unittest: 1600
  333. */
  334. CURLcode Curl_ntlm_core_mk_nt_hash(const char *password,
  335. unsigned char *ntbuffer /* 21 bytes */)
  336. {
  337. size_t len = strlen(password);
  338. unsigned char *pw;
  339. CURLcode result;
  340. if(len > SIZE_T_MAX/2) /* avoid integer overflow */
  341. return CURLE_OUT_OF_MEMORY;
  342. pw = len ? malloc(len * 2) : (unsigned char *)strdup("");
  343. if(!pw)
  344. return CURLE_OUT_OF_MEMORY;
  345. ascii_to_unicode_le(pw, password, len);
  346. /* Create NT hashed password. */
  347. result = Curl_md4it(ntbuffer, pw, 2 * len);
  348. if(!result)
  349. memset(ntbuffer + 16, 0, 21 - 16);
  350. free(pw);
  351. return result;
  352. }
  353. #if !defined(USE_WINDOWS_SSPI)
  354. #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
  355. #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
  356. /* Timestamp in tenths of a microsecond since January 1, 1601 00:00:00 UTC. */
  357. struct ms_filetime {
  358. unsigned int dwLowDateTime;
  359. unsigned int dwHighDateTime;
  360. };
  361. /* Convert a time_t to an MS FILETIME (MS-DTYP section 2.3.3). */
  362. static void time2filetime(struct ms_filetime *ft, time_t t)
  363. {
  364. #if SIZEOF_TIME_T > 4
  365. t = (t + (curl_off_t)11644473600) * 10000000;
  366. ft->dwLowDateTime = (unsigned int) (t & 0xFFFFFFFF);
  367. ft->dwHighDateTime = (unsigned int) (t >> 32);
  368. #else
  369. unsigned int r, s;
  370. unsigned int i;
  371. ft->dwLowDateTime = (unsigned int)t & 0xFFFFFFFF;
  372. ft->dwHighDateTime = 0;
  373. # ifndef HAVE_TIME_T_UNSIGNED
  374. /* Extend sign if needed. */
  375. if(ft->dwLowDateTime & 0x80000000)
  376. ft->dwHighDateTime = ~(unsigned int)0;
  377. # endif
  378. /* Bias seconds to Jan 1, 1601.
  379. 134774 days = 11644473600 seconds = 0x2B6109100 */
  380. r = ft->dwLowDateTime;
  381. ft->dwLowDateTime = (ft->dwLowDateTime + 0xB6109100U) & 0xFFFFFFFF;
  382. ft->dwHighDateTime += ft->dwLowDateTime < r ? 0x03 : 0x02;
  383. /* Convert to tenths of microseconds. */
  384. ft->dwHighDateTime *= 10000000;
  385. i = 32;
  386. do {
  387. i -= 8;
  388. s = ((ft->dwLowDateTime >> i) & 0xFF) * (10000000 - 1);
  389. r = (s << i) & 0xFFFFFFFF;
  390. s >>= 1; /* Split shift to avoid width overflow. */
  391. s >>= 31 - i;
  392. ft->dwLowDateTime = (ft->dwLowDateTime + r) & 0xFFFFFFFF;
  393. if(ft->dwLowDateTime < r)
  394. s++;
  395. ft->dwHighDateTime += s;
  396. } while(i);
  397. ft->dwHighDateTime &= 0xFFFFFFFF;
  398. #endif
  399. }
  400. /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
  401. * (uppercase UserName + Domain) as the data
  402. */
  403. CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
  404. const char *domain, size_t domlen,
  405. unsigned char *ntlmhash,
  406. unsigned char *ntlmv2hash)
  407. {
  408. /* Unicode representation */
  409. size_t identity_len;
  410. unsigned char *identity;
  411. CURLcode result = CURLE_OK;
  412. if((userlen > CURL_MAX_INPUT_LENGTH) || (domlen > CURL_MAX_INPUT_LENGTH))
  413. return CURLE_OUT_OF_MEMORY;
  414. identity_len = (userlen + domlen) * 2;
  415. identity = malloc(identity_len + 1);
  416. if(!identity)
  417. return CURLE_OUT_OF_MEMORY;
  418. ascii_uppercase_to_unicode_le(identity, user, userlen);
  419. ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
  420. result = Curl_hmacit(&Curl_HMAC_MD5, ntlmhash, 16, identity, identity_len,
  421. ntlmv2hash);
  422. free(identity);
  423. return result;
  424. }
  425. /*
  426. * Curl_ntlm_core_mk_ntlmv2_resp()
  427. *
  428. * This creates the NTLMv2 response as set in the NTLM type-3 message.
  429. *
  430. * Parameters:
  431. *
  432. * ntlmv2hash [in] - The NTLMv2 hash (16 bytes)
  433. * challenge_client [in] - The client nonce (8 bytes)
  434. * ntlm [in] - The NTLM data struct being used to read TargetInfo
  435. and Server challenge received in the type-2 message
  436. * ntresp [out] - The address where a pointer to newly allocated
  437. * memory holding the NTLMv2 response.
  438. * ntresp_len [out] - The length of the output message.
  439. *
  440. * Returns CURLE_OK on success.
  441. */
  442. CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
  443. unsigned char *challenge_client,
  444. struct ntlmdata *ntlm,
  445. unsigned char **ntresp,
  446. unsigned int *ntresp_len)
  447. {
  448. /* NTLMv2 response structure :
  449. ------------------------------------------------------------------------------
  450. 0 HMAC MD5 16 bytes
  451. ------BLOB--------------------------------------------------------------------
  452. 16 Signature 0x01010000
  453. 20 Reserved long (0x00000000)
  454. 24 Timestamp LE, 64-bit signed value representing the number of
  455. tenths of a microsecond since January 1, 1601.
  456. 32 Client Nonce 8 bytes
  457. 40 Unknown 4 bytes
  458. 44 Target Info N bytes (from the type-2 message)
  459. 44+N Unknown 4 bytes
  460. ------------------------------------------------------------------------------
  461. */
  462. unsigned int len = 0;
  463. unsigned char *ptr = NULL;
  464. unsigned char hmac_output[HMAC_MD5_LENGTH];
  465. struct ms_filetime tw;
  466. CURLcode result = CURLE_OK;
  467. /* Calculate the timestamp */
  468. #ifdef DEBUGBUILD
  469. char *force_timestamp = getenv("CURL_FORCETIME");
  470. if(force_timestamp)
  471. time2filetime(&tw, (time_t) 0);
  472. else
  473. #endif
  474. time2filetime(&tw, time(NULL));
  475. /* Calculate the response len */
  476. len = HMAC_MD5_LENGTH + NTLMv2_BLOB_LEN;
  477. /* Allocate the response */
  478. ptr = calloc(1, len);
  479. if(!ptr)
  480. return CURLE_OUT_OF_MEMORY;
  481. /* Create the BLOB structure */
  482. msnprintf((char *)ptr + HMAC_MD5_LENGTH, NTLMv2_BLOB_LEN,
  483. "%c%c%c%c" /* NTLMv2_BLOB_SIGNATURE */
  484. "%c%c%c%c" /* Reserved = 0 */
  485. "%c%c%c%c%c%c%c%c", /* Timestamp */
  486. NTLMv2_BLOB_SIGNATURE[0], NTLMv2_BLOB_SIGNATURE[1],
  487. NTLMv2_BLOB_SIGNATURE[2], NTLMv2_BLOB_SIGNATURE[3],
  488. 0, 0, 0, 0,
  489. LONGQUARTET(tw.dwLowDateTime), LONGQUARTET(tw.dwHighDateTime));
  490. memcpy(ptr + 32, challenge_client, 8);
  491. if(ntlm->target_info_len)
  492. memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
  493. /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
  494. memcpy(ptr + 8, &ntlm->nonce[0], 8);
  495. result = Curl_hmacit(&Curl_HMAC_MD5, ntlmv2hash, HMAC_MD5_LENGTH, ptr + 8,
  496. NTLMv2_BLOB_LEN + 8, hmac_output);
  497. if(result) {
  498. free(ptr);
  499. return result;
  500. }
  501. /* Concatenate the HMAC MD5 output with the BLOB */
  502. memcpy(ptr, hmac_output, HMAC_MD5_LENGTH);
  503. /* Return the response */
  504. *ntresp = ptr;
  505. *ntresp_len = len;
  506. return result;
  507. }
  508. /*
  509. * Curl_ntlm_core_mk_lmv2_resp()
  510. *
  511. * This creates the LMv2 response as used in the NTLM type-3 message.
  512. *
  513. * Parameters:
  514. *
  515. * ntlmv2hash [in] - The NTLMv2 hash (16 bytes)
  516. * challenge_client [in] - The client nonce (8 bytes)
  517. * challenge_client [in] - The server challenge (8 bytes)
  518. * lmresp [out] - The LMv2 response (24 bytes)
  519. *
  520. * Returns CURLE_OK on success.
  521. */
  522. CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
  523. unsigned char *challenge_client,
  524. unsigned char *challenge_server,
  525. unsigned char *lmresp)
  526. {
  527. unsigned char data[16];
  528. unsigned char hmac_output[16];
  529. CURLcode result = CURLE_OK;
  530. memcpy(&data[0], challenge_server, 8);
  531. memcpy(&data[8], challenge_client, 8);
  532. result = Curl_hmacit(&Curl_HMAC_MD5, ntlmv2hash, 16, &data[0], 16,
  533. hmac_output);
  534. if(result)
  535. return result;
  536. /* Concatenate the HMAC MD5 output with the client nonce */
  537. memcpy(lmresp, hmac_output, 16);
  538. memcpy(lmresp + 16, challenge_client, 8);
  539. return result;
  540. }
  541. #endif /* !USE_WINDOWS_SSPI */
  542. #endif /* USE_CURL_NTLM_CORE */