curl_ntlm_core.c 23 KB

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