curl_ntlm_core.c 21 KB

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