2
0

curl_ntlm_core.c 22 KB

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