curl_ntlm_core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(USE_NTLM) && !defined(USE_WINDOWS_SSPI)
  24. /*
  25. * NTLM details:
  26. *
  27. * http://davenport.sourceforge.net/ntlm.html
  28. * http://www.innovation.ch/java/ntlm.html
  29. */
  30. #ifdef USE_SSLEAY
  31. # ifdef USE_OPENSSL
  32. # include <openssl/des.h>
  33. # ifndef OPENSSL_NO_MD4
  34. # include <openssl/md4.h>
  35. # endif
  36. # include <openssl/md5.h>
  37. # include <openssl/ssl.h>
  38. # include <openssl/rand.h>
  39. # else
  40. # include <des.h>
  41. # ifndef OPENSSL_NO_MD4
  42. # include <md4.h>
  43. # endif
  44. # include <md5.h>
  45. # include <ssl.h>
  46. # include <rand.h>
  47. # endif
  48. # if (OPENSSL_VERSION_NUMBER < 0x00907001L)
  49. # define DES_key_schedule des_key_schedule
  50. # define DES_cblock des_cblock
  51. # define DES_set_odd_parity des_set_odd_parity
  52. # define DES_set_key des_set_key
  53. # define DES_ecb_encrypt des_ecb_encrypt
  54. # define DESKEY(x) x
  55. # define DESKEYARG(x) x
  56. # else
  57. # define DESKEYARG(x) *x
  58. # define DESKEY(x) &x
  59. # endif
  60. #elif defined(USE_GNUTLS_NETTLE)
  61. # include <nettle/des.h>
  62. # include <nettle/md4.h>
  63. #elif defined(USE_GNUTLS)
  64. # include <gcrypt.h>
  65. # define MD5_DIGEST_LENGTH 16
  66. # define MD4_DIGEST_LENGTH 16
  67. #elif defined(USE_NSS)
  68. # include <nss.h>
  69. # include <pk11pub.h>
  70. # include <hasht.h>
  71. # include "curl_md4.h"
  72. # define MD5_DIGEST_LENGTH MD5_LENGTH
  73. #elif defined(USE_DARWINSSL)
  74. # include <CommonCrypto/CommonCryptor.h>
  75. # include <CommonCrypto/CommonDigest.h>
  76. #else
  77. # error "Can't compile NTLM support without a crypto library."
  78. #endif
  79. #include "urldata.h"
  80. #include "non-ascii.h"
  81. #include "rawstr.h"
  82. #include "curl_memory.h"
  83. #include "curl_ntlm_core.h"
  84. #include "curl_md5.h"
  85. #include "curl_hmac.h"
  86. #include "warnless.h"
  87. #define _MPRINTF_REPLACE /* use our functions only */
  88. #include <curl/mprintf.h>
  89. /* The last #include file should be: */
  90. #include "memdebug.h"
  91. #define NTLM_HMAC_MD5_LEN (16)
  92. #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
  93. #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
  94. #ifdef USE_SSLEAY
  95. /*
  96. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
  97. * key schedule ks is also set.
  98. */
  99. static void setup_des_key(const unsigned char *key_56,
  100. DES_key_schedule DESKEYARG(ks))
  101. {
  102. DES_cblock key;
  103. key[0] = key_56[0];
  104. key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
  105. key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
  106. key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
  107. key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
  108. key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
  109. key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
  110. key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
  111. DES_set_odd_parity(&key);
  112. DES_set_key(&key, ks);
  113. }
  114. #else /* defined(USE_SSLEAY) */
  115. /*
  116. * Turns a 56 bit key into the 64 bit, odd parity key. Used by GnuTLS and NSS.
  117. */
  118. static void extend_key_56_to_64(const unsigned char *key_56, char *key)
  119. {
  120. key[0] = key_56[0];
  121. key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
  122. key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
  123. key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
  124. key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
  125. key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
  126. key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
  127. key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
  128. }
  129. #if defined(USE_GNUTLS_NETTLE)
  130. static void setup_des_key(const unsigned char *key_56,
  131. struct des_ctx *des)
  132. {
  133. char key[8];
  134. extend_key_56_to_64(key_56, key);
  135. des_set_key(des, (const uint8_t*)key);
  136. }
  137. #elif defined(USE_GNUTLS)
  138. /*
  139. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
  140. */
  141. static void setup_des_key(const unsigned char *key_56,
  142. gcry_cipher_hd_t *des)
  143. {
  144. char key[8];
  145. extend_key_56_to_64(key_56, key);
  146. gcry_cipher_setkey(*des, key, 8);
  147. }
  148. #elif defined(USE_NSS)
  149. /*
  150. * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
  151. * the expanded key. The caller is responsible for giving 64 bit of valid
  152. * data is IN and (at least) 64 bit large buffer as OUT.
  153. */
  154. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  155. const unsigned char *key_56)
  156. {
  157. const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
  158. PK11SlotInfo *slot = NULL;
  159. char key[8]; /* expanded 64 bit key */
  160. SECItem key_item;
  161. PK11SymKey *symkey = NULL;
  162. SECItem *param = NULL;
  163. PK11Context *ctx = NULL;
  164. int out_len; /* not used, required by NSS */
  165. bool rv = FALSE;
  166. /* use internal slot for DES encryption (requires NSS to be initialized) */
  167. slot = PK11_GetInternalKeySlot();
  168. if(!slot)
  169. return FALSE;
  170. /* expand the 56 bit key to 64 bit and wrap by NSS */
  171. extend_key_56_to_64(key_56, key);
  172. key_item.data = (unsigned char *)key;
  173. key_item.len = /* hard-wired */ 8;
  174. symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
  175. &key_item, NULL);
  176. if(!symkey)
  177. goto fail;
  178. /* create DES encryption context */
  179. param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
  180. if(!param)
  181. goto fail;
  182. ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
  183. if(!ctx)
  184. goto fail;
  185. /* perform the encryption */
  186. if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
  187. (unsigned char *)in, /* inbuflen */ 8)
  188. && SECSuccess == PK11_Finalize(ctx))
  189. rv = /* all OK */ TRUE;
  190. fail:
  191. /* cleanup */
  192. if(ctx)
  193. PK11_DestroyContext(ctx, PR_TRUE);
  194. if(symkey)
  195. PK11_FreeSymKey(symkey);
  196. if(param)
  197. SECITEM_FreeItem(param, PR_TRUE);
  198. PK11_FreeSlot(slot);
  199. return rv;
  200. }
  201. #elif defined(USE_DARWINSSL)
  202. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  203. const unsigned char *key_56)
  204. {
  205. char key[8];
  206. size_t out_len;
  207. CCCryptorStatus err;
  208. extend_key_56_to_64(key_56, key);
  209. err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
  210. kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
  211. 8 /* outbuflen */, &out_len);
  212. return err == kCCSuccess;
  213. }
  214. #endif /* defined(USE_DARWINSSL) */
  215. #endif /* defined(USE_SSLEAY) */
  216. /*
  217. * takes a 21 byte array and treats it as 3 56-bit DES keys. The
  218. * 8 byte plaintext is encrypted with each key and the resulting 24
  219. * bytes are stored in the results array.
  220. */
  221. void Curl_ntlm_core_lm_resp(const unsigned char *keys,
  222. const unsigned char *plaintext,
  223. unsigned char *results)
  224. {
  225. #ifdef USE_SSLEAY
  226. DES_key_schedule ks;
  227. setup_des_key(keys, DESKEY(ks));
  228. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
  229. DESKEY(ks), DES_ENCRYPT);
  230. setup_des_key(keys + 7, DESKEY(ks));
  231. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
  232. DESKEY(ks), DES_ENCRYPT);
  233. setup_des_key(keys + 14, DESKEY(ks));
  234. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
  235. DESKEY(ks), DES_ENCRYPT);
  236. #elif defined(USE_GNUTLS_NETTLE)
  237. struct des_ctx des;
  238. setup_des_key(keys, &des);
  239. des_encrypt(&des, 8, results, plaintext);
  240. setup_des_key(keys + 7, &des);
  241. des_encrypt(&des, 8, results + 8, plaintext);
  242. setup_des_key(keys + 14, &des);
  243. des_encrypt(&des, 8, results + 16, plaintext);
  244. #elif defined(USE_GNUTLS)
  245. gcry_cipher_hd_t des;
  246. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  247. setup_des_key(keys, &des);
  248. gcry_cipher_encrypt(des, results, 8, plaintext, 8);
  249. gcry_cipher_close(des);
  250. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  251. setup_des_key(keys + 7, &des);
  252. gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
  253. gcry_cipher_close(des);
  254. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  255. setup_des_key(keys + 14, &des);
  256. gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
  257. gcry_cipher_close(des);
  258. #elif defined(USE_NSS) || defined(USE_DARWINSSL)
  259. encrypt_des(plaintext, results, keys);
  260. encrypt_des(plaintext, results + 8, keys + 7);
  261. encrypt_des(plaintext, results + 16, keys + 14);
  262. #endif
  263. }
  264. /*
  265. * Set up lanmanager hashed password
  266. */
  267. void Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
  268. const char *password,
  269. unsigned char *lmbuffer /* 21 bytes */)
  270. {
  271. CURLcode res;
  272. unsigned char pw[14];
  273. static const unsigned char magic[] = {
  274. 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
  275. };
  276. size_t len = CURLMIN(strlen(password), 14);
  277. Curl_strntoupper((char *)pw, password, len);
  278. memset(&pw[len], 0, 14 - len);
  279. /*
  280. * The LanManager hashed password needs to be created using the
  281. * password in the network encoding not the host encoding.
  282. */
  283. res = Curl_convert_to_network(data, (char *)pw, 14);
  284. if(res)
  285. return;
  286. {
  287. /* Create LanManager hashed password. */
  288. #ifdef USE_SSLEAY
  289. DES_key_schedule ks;
  290. setup_des_key(pw, DESKEY(ks));
  291. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
  292. DESKEY(ks), DES_ENCRYPT);
  293. setup_des_key(pw + 7, DESKEY(ks));
  294. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
  295. DESKEY(ks), DES_ENCRYPT);
  296. #elif defined(USE_GNUTLS_NETTLE)
  297. struct des_ctx des;
  298. setup_des_key(pw, &des);
  299. des_encrypt(&des, 8, lmbuffer, magic);
  300. setup_des_key(pw + 7, &des);
  301. des_encrypt(&des, 8, lmbuffer + 8, magic);
  302. #elif defined(USE_GNUTLS)
  303. gcry_cipher_hd_t des;
  304. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  305. setup_des_key(pw, &des);
  306. gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
  307. gcry_cipher_close(des);
  308. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  309. setup_des_key(pw + 7, &des);
  310. gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
  311. gcry_cipher_close(des);
  312. #elif defined(USE_NSS) || defined(USE_DARWINSSL)
  313. encrypt_des(magic, lmbuffer, pw);
  314. encrypt_des(magic, lmbuffer + 8, pw + 7);
  315. #endif
  316. memset(lmbuffer + 16, 0, 21 - 16);
  317. }
  318. }
  319. #if USE_NTRESPONSES
  320. static void ascii_to_unicode_le(unsigned char *dest, const char *src,
  321. size_t srclen)
  322. {
  323. size_t i;
  324. for(i = 0; i < srclen; i++) {
  325. dest[2 * i] = (unsigned char)src[i];
  326. dest[2 * i + 1] = '\0';
  327. }
  328. }
  329. static void ascii_uppercase_to_unicode_le(unsigned char *dest,
  330. const char *src, size_t srclen)
  331. {
  332. size_t i;
  333. for(i = 0; i < srclen; i++) {
  334. dest[2 * i] = (unsigned char)(toupper(src[i]));
  335. dest[2 * i + 1] = '\0';
  336. }
  337. }
  338. static void write32_le(const int value, unsigned char *buffer)
  339. {
  340. buffer[0] = (char)(value & 0x000000FF);
  341. buffer[1] = (char)((value & 0x0000FF00) >> 8);
  342. buffer[2] = (char)((value & 0x00FF0000) >> 16);
  343. buffer[3] = (char)((value & 0xFF000000) >> 24);
  344. }
  345. #if defined(HAVE_LONGLONG)
  346. static void write64_le(const long long value, unsigned char *buffer)
  347. #else
  348. static void write64_le(const __int64 value, unsigned char *buffer)
  349. #endif
  350. {
  351. write32_le((int)value, buffer);
  352. write32_le((int)(value >> 32), buffer + 4);
  353. }
  354. /*
  355. * Set up nt hashed passwords
  356. */
  357. CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
  358. const char *password,
  359. unsigned char *ntbuffer /* 21 bytes */)
  360. {
  361. size_t len = strlen(password);
  362. unsigned char *pw = malloc(len * 2);
  363. CURLcode result;
  364. if(!pw)
  365. return CURLE_OUT_OF_MEMORY;
  366. ascii_to_unicode_le(pw, password, len);
  367. /*
  368. * The NT hashed password needs to be created using the password in the
  369. * network encoding not the host encoding.
  370. */
  371. result = Curl_convert_to_network(data, (char *)pw, len * 2);
  372. if(result)
  373. return result;
  374. {
  375. /* Create NT hashed password. */
  376. #ifdef USE_SSLEAY
  377. MD4_CTX MD4pw;
  378. MD4_Init(&MD4pw);
  379. MD4_Update(&MD4pw, pw, 2 * len);
  380. MD4_Final(ntbuffer, &MD4pw);
  381. #elif defined(USE_GNUTLS_NETTLE)
  382. struct md4_ctx MD4pw;
  383. md4_init(&MD4pw);
  384. md4_update(&MD4pw, (unsigned int)(2 * len), pw);
  385. md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
  386. #elif defined(USE_GNUTLS)
  387. gcry_md_hd_t MD4pw;
  388. gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
  389. gcry_md_write(MD4pw, pw, 2 * len);
  390. memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
  391. gcry_md_close(MD4pw);
  392. #elif defined(USE_NSS)
  393. Curl_md4it(ntbuffer, pw, 2 * len);
  394. #elif defined(USE_DARWINSSL)
  395. (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
  396. #endif
  397. memset(ntbuffer + 16, 0, 21 - 16);
  398. }
  399. free(pw);
  400. return CURLE_OK;
  401. }
  402. /* This returns the HMAC MD5 digest */
  403. CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
  404. const unsigned char *data, unsigned int datalen,
  405. unsigned char *output)
  406. {
  407. HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
  408. if(!ctxt)
  409. return CURLE_OUT_OF_MEMORY;
  410. /* Update the digest with the given challenge */
  411. Curl_HMAC_update(ctxt, data, datalen);
  412. /* Finalise the digest */
  413. Curl_HMAC_final(ctxt, output);
  414. return CURLE_OK;
  415. }
  416. /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
  417. * (uppercase UserName + Domain) as the data
  418. */
  419. CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
  420. const char *domain, size_t domlen,
  421. unsigned char *ntlmhash,
  422. unsigned char *ntlmv2hash)
  423. {
  424. /* Unicode representation */
  425. size_t identity_len = (userlen + domlen) * 2;
  426. unsigned char *identity = malloc(identity_len);
  427. CURLcode res = CURLE_OK;
  428. if(!identity)
  429. return CURLE_OUT_OF_MEMORY;
  430. ascii_uppercase_to_unicode_le(identity, user, userlen);
  431. ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
  432. res = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
  433. ntlmv2hash);
  434. Curl_safefree(identity);
  435. return res;
  436. }
  437. /*
  438. * Curl_ntlm_core_mk_ntlmv2_resp()
  439. *
  440. * This creates the NTLMv2 response as set in the ntlm type-3 message.
  441. *
  442. * Parameters:
  443. *
  444. * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
  445. * challenge_client [in] - The client nonce (8 bytes)
  446. * ntlm [in] - The ntlm data struct being used to read TargetInfo
  447. and Server challenge received in the type-2 message
  448. * ntresp [out] - The address where a pointer to newly allocated
  449. * memory holding the NTLMv2 response.
  450. * ntresp_len [out] - The length of the output message.
  451. *
  452. * Returns CURLE_OK on success.
  453. */
  454. CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
  455. unsigned char *challenge_client,
  456. struct ntlmdata *ntlm,
  457. unsigned char **ntresp,
  458. unsigned int *ntresp_len)
  459. {
  460. /* NTLMv2 response structure :
  461. ------------------------------------------------------------------------------
  462. 0 HMAC MD5 16 bytes
  463. ------BLOB--------------------------------------------------------------------
  464. 16 Signature 0x01010000
  465. 20 Reserved long (0x00000000)
  466. 24 Timestamp LE, 64-bit signed value representing the number of
  467. tenths of a microsecond since January 1, 1601.
  468. 32 Client Nonce 8 bytes
  469. 40 Unknown 4 bytes
  470. 44 Target Info N bytes (from the type-2 message)
  471. 44+N Unknown 4 bytes
  472. ------------------------------------------------------------------------------
  473. */
  474. unsigned int len = 0;
  475. unsigned char *ptr = NULL;
  476. unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
  477. #if defined(HAVE_LONGLONG)
  478. long long tw;
  479. #else
  480. __int64 tw;
  481. #endif
  482. CURLcode res = CURLE_OK;
  483. /* Calculate the timestamp */
  484. #ifdef DEBUGBUILD
  485. char *force_timestamp = getenv("CURL_FORCETIME");
  486. if(force_timestamp)
  487. tw = 11644473600ULL * 10000000ULL;
  488. else
  489. #endif
  490. tw = ((long long)time(NULL) + 11644473600ULL) * 10000000ULL;
  491. /* Calculate the response len */
  492. len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
  493. /* Allocate the response */
  494. ptr = malloc(len);
  495. if(!ptr)
  496. return CURLE_OUT_OF_MEMORY;
  497. memset(ptr, 0, len);
  498. /* Create the BLOB structure */
  499. snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
  500. NTLMv2_BLOB_SIGNATURE
  501. "%c%c%c%c", /* Reserved = 0 */
  502. 0, 0, 0, 0);
  503. write64_le(tw, ptr + 24);
  504. memcpy(ptr + 32, challenge_client, 8);
  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. res = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
  509. NTLMv2_BLOB_LEN + 8, hmac_output);
  510. if(res) {
  511. Curl_safefree(ptr);
  512. return res;
  513. }
  514. /* Concatenate the HMAC MD5 output with the BLOB */
  515. memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
  516. /* Return the response */
  517. *ntresp = ptr;
  518. *ntresp_len = len;
  519. return res;
  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 res = CURLE_OK;
  543. memcpy(&data[0], challenge_server, 8);
  544. memcpy(&data[8], challenge_client, 8);
  545. res = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
  546. if(res)
  547. return res;
  548. /* Concatenate the HMAC MD5 output with the client nonce */
  549. memcpy(lmresp, hmac_output, 16);
  550. memcpy(lmresp+16, challenge_client, 8);
  551. return res;
  552. }
  553. #endif /* USE_NTRESPONSES */
  554. #endif /* USE_NTLM && !USE_WINDOWS_SSPI */