curl_ntlm_core.c 21 KB

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