curl_ntlm_core.c 21 KB

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