curl_ntlm_core.c 19 KB

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