curl_ntlm_core.c 19 KB

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