curl_ntlm_core.c 21 KB

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