curl_ntlm_core.c 22 KB

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