2
0

curl_ntlm_core.c 19 KB

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