curl_ntlm_core.c 23 KB

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