curl_ntlm_core.c 20 KB

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