curl_ntlm_core.c 21 KB

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