curl_ntlm_core.c 22 KB

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