2
0

curl_ntlm_core.c 19 KB

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