curl_ntlm_core.c 24 KB

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