curl_ntlm_core.c 21 KB

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