curl_ntlm_core.c 21 KB

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