curl_ntlm_core.c 20 KB

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