curl_ntlm_core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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.haxx.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_NTLM)
  24. /*
  25. * NTLM details:
  26. *
  27. * http://davenport.sourceforge.net/ntlm.html
  28. * https://www.innovation.ch/java/ntlm.html
  29. */
  30. #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
  31. #ifdef USE_OPENSSL
  32. # ifdef USE_OPENSSL
  33. # include <openssl/des.h>
  34. # ifndef OPENSSL_NO_MD4
  35. # include <openssl/md4.h>
  36. # endif
  37. # include <openssl/md5.h>
  38. # include <openssl/ssl.h>
  39. # include <openssl/rand.h>
  40. # else
  41. # include <des.h>
  42. # ifndef OPENSSL_NO_MD4
  43. # include <md4.h>
  44. # endif
  45. # include <md5.h>
  46. # include <ssl.h>
  47. # include <rand.h>
  48. # endif
  49. # if (OPENSSL_VERSION_NUMBER < 0x00907001L)
  50. # define DES_key_schedule des_key_schedule
  51. # define DES_cblock des_cblock
  52. # define DES_set_odd_parity des_set_odd_parity
  53. # define DES_set_key des_set_key
  54. # define DES_ecb_encrypt des_ecb_encrypt
  55. # define DESKEY(x) x
  56. # define DESKEYARG(x) x
  57. # else
  58. # define DESKEYARG(x) *x
  59. # define DESKEY(x) &x
  60. # endif
  61. #elif defined(USE_GNUTLS_NETTLE)
  62. # include <nettle/des.h>
  63. # include <nettle/md4.h>
  64. #elif defined(USE_GNUTLS)
  65. # include <gcrypt.h>
  66. # define MD5_DIGEST_LENGTH 16
  67. # define MD4_DIGEST_LENGTH 16
  68. #elif defined(USE_NSS)
  69. # include <nss.h>
  70. # include <pk11pub.h>
  71. # include <hasht.h>
  72. # include "curl_md4.h"
  73. # define MD5_DIGEST_LENGTH MD5_LENGTH
  74. #elif defined(USE_DARWINSSL)
  75. # include <CommonCrypto/CommonCryptor.h>
  76. # include <CommonCrypto/CommonDigest.h>
  77. #elif defined(USE_OS400CRYPTO)
  78. # include "cipher.mih" /* mih/cipher */
  79. # include "curl_md4.h"
  80. #elif defined(USE_WIN32_CRYPTO)
  81. # include <wincrypt.h>
  82. #else
  83. # error "Can't compile NTLM support without a crypto library."
  84. #endif
  85. #include "urldata.h"
  86. #include "non-ascii.h"
  87. #include "rawstr.h"
  88. #include "curl_ntlm_core.h"
  89. #include "curl_md5.h"
  90. #include "curl_hmac.h"
  91. #include "warnless.h"
  92. #include "curl_endian.h"
  93. #include "curl_des.h"
  94. #include "curl_printf.h"
  95. /* The last #include files should be: */
  96. #include "curl_memory.h"
  97. #include "memdebug.h"
  98. #define NTLM_HMAC_MD5_LEN (16)
  99. #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
  100. #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
  101. /*
  102. * Turns a 56-bit key into being 64-bit wide.
  103. */
  104. static void extend_key_56_to_64(const unsigned char *key_56, char *key)
  105. {
  106. key[0] = key_56[0];
  107. key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
  108. key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
  109. key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
  110. key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
  111. key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
  112. key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
  113. key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
  114. }
  115. #ifdef USE_OPENSSL
  116. /*
  117. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
  118. * key schedule ks is also set.
  119. */
  120. static void setup_des_key(const unsigned char *key_56,
  121. DES_key_schedule DESKEYARG(ks))
  122. {
  123. DES_cblock key;
  124. /* Expand the 56-bit key to 64-bits */
  125. extend_key_56_to_64(key_56, (char *) &key);
  126. /* Set the key parity to odd */
  127. DES_set_odd_parity(&key);
  128. /* Set the key */
  129. DES_set_key(&key, ks);
  130. }
  131. #elif defined(USE_GNUTLS_NETTLE)
  132. static void setup_des_key(const unsigned char *key_56,
  133. struct des_ctx *des)
  134. {
  135. char key[8];
  136. /* Expand the 56-bit key to 64-bits */
  137. extend_key_56_to_64(key_56, key);
  138. /* Set the key parity to odd */
  139. Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
  140. /* Set the key */
  141. des_set_key(des, (const uint8_t *) key);
  142. }
  143. #elif defined(USE_GNUTLS)
  144. /*
  145. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
  146. */
  147. static void setup_des_key(const unsigned char *key_56,
  148. gcry_cipher_hd_t *des)
  149. {
  150. char key[8];
  151. /* Expand the 56-bit key to 64-bits */
  152. extend_key_56_to_64(key_56, key);
  153. /* Set the key parity to odd */
  154. Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
  155. /* Set the key */
  156. gcry_cipher_setkey(*des, key, sizeof(key));
  157. }
  158. #elif defined(USE_NSS)
  159. /*
  160. * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
  161. * the expanded key. The caller is responsible for giving 64 bit of valid
  162. * data is IN and (at least) 64 bit large buffer as OUT.
  163. */
  164. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  165. const unsigned char *key_56)
  166. {
  167. const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
  168. PK11SlotInfo *slot = NULL;
  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. 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_DARWINSSL)
  215. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  216. const unsigned char *key_56)
  217. {
  218. char key[8];
  219. size_t out_len;
  220. CCCryptorStatus err;
  221. /* Expand the 56-bit key to 64-bits */
  222. extend_key_56_to_64(key_56, key);
  223. /* Set the key parity to odd */
  224. Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
  225. /* Perform the encryption */
  226. err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
  227. kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
  228. 8 /* outbuflen */, &out_len);
  229. return err == kCCSuccess;
  230. }
  231. #elif defined(USE_OS400CRYPTO)
  232. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  233. const unsigned char *key_56)
  234. {
  235. char key[8];
  236. _CIPHER_Control_T ctl;
  237. /* Setup the cipher control structure */
  238. ctl.Func_ID = ENCRYPT_ONLY;
  239. ctl.Data_Len = sizeof(key);
  240. /* Expand the 56-bit key to 64-bits */
  241. extend_key_56_to_64(key_56, ctl.Crypto_Key);
  242. /* Set the key parity to odd */
  243. Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
  244. /* Perform the encryption */
  245. _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
  246. return TRUE;
  247. }
  248. #elif defined(USE_WIN32_CRYPTO)
  249. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  250. const unsigned char *key_56)
  251. {
  252. HCRYPTPROV hprov;
  253. HCRYPTKEY hkey;
  254. struct {
  255. BLOBHEADER hdr;
  256. unsigned int len;
  257. char key[8];
  258. } blob;
  259. DWORD len = 8;
  260. /* Acquire the crypto provider */
  261. if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
  262. CRYPT_VERIFYCONTEXT))
  263. return FALSE;
  264. /* Setup the key blob structure */
  265. memset(&blob, 0, sizeof(blob));
  266. blob.hdr.bType = PLAINTEXTKEYBLOB;
  267. blob.hdr.bVersion = 2;
  268. blob.hdr.aiKeyAlg = CALG_DES;
  269. blob.len = sizeof(blob.key);
  270. /* Expand the 56-bit key to 64-bits */
  271. extend_key_56_to_64(key_56, blob.key);
  272. /* Set the key parity to odd */
  273. Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
  274. /* Import the key */
  275. if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
  276. CryptReleaseContext(hprov, 0);
  277. return FALSE;
  278. }
  279. memcpy(out, in, 8);
  280. /* Perform the encryption */
  281. CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
  282. CryptDestroyKey(hkey);
  283. CryptReleaseContext(hprov, 0);
  284. return TRUE;
  285. }
  286. #endif /* defined(USE_WIN32_CRYPTO) */
  287. /*
  288. * takes a 21 byte array and treats it as 3 56-bit DES keys. The
  289. * 8 byte plaintext is encrypted with each key and the resulting 24
  290. * bytes are stored in the results array.
  291. */
  292. void Curl_ntlm_core_lm_resp(const unsigned char *keys,
  293. const unsigned char *plaintext,
  294. unsigned char *results)
  295. {
  296. #ifdef USE_OPENSSL
  297. DES_key_schedule ks;
  298. setup_des_key(keys, DESKEY(ks));
  299. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
  300. DESKEY(ks), DES_ENCRYPT);
  301. setup_des_key(keys + 7, DESKEY(ks));
  302. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
  303. DESKEY(ks), DES_ENCRYPT);
  304. setup_des_key(keys + 14, DESKEY(ks));
  305. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
  306. DESKEY(ks), DES_ENCRYPT);
  307. #elif defined(USE_GNUTLS_NETTLE)
  308. struct des_ctx des;
  309. setup_des_key(keys, &des);
  310. des_encrypt(&des, 8, results, plaintext);
  311. setup_des_key(keys + 7, &des);
  312. des_encrypt(&des, 8, results + 8, plaintext);
  313. setup_des_key(keys + 14, &des);
  314. des_encrypt(&des, 8, results + 16, plaintext);
  315. #elif defined(USE_GNUTLS)
  316. gcry_cipher_hd_t des;
  317. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  318. setup_des_key(keys, &des);
  319. gcry_cipher_encrypt(des, results, 8, plaintext, 8);
  320. gcry_cipher_close(des);
  321. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  322. setup_des_key(keys + 7, &des);
  323. gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
  324. gcry_cipher_close(des);
  325. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  326. setup_des_key(keys + 14, &des);
  327. gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
  328. gcry_cipher_close(des);
  329. #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
  330. || defined(USE_WIN32_CRYPTO)
  331. encrypt_des(plaintext, results, keys);
  332. encrypt_des(plaintext, results + 8, keys + 7);
  333. encrypt_des(plaintext, results + 16, keys + 14);
  334. #endif
  335. }
  336. /*
  337. * Set up lanmanager hashed password
  338. */
  339. CURLcode Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
  340. const char *password,
  341. unsigned char *lmbuffer /* 21 bytes */)
  342. {
  343. CURLcode result;
  344. unsigned char pw[14];
  345. static const unsigned char magic[] = {
  346. 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
  347. };
  348. size_t len = CURLMIN(strlen(password), 14);
  349. Curl_strntoupper((char *)pw, password, len);
  350. memset(&pw[len], 0, 14 - len);
  351. /*
  352. * The LanManager hashed password needs to be created using the
  353. * password in the network encoding not the host encoding.
  354. */
  355. result = Curl_convert_to_network(data, (char *)pw, 14);
  356. if(result)
  357. return result;
  358. {
  359. /* Create LanManager hashed password. */
  360. #ifdef USE_OPENSSL
  361. DES_key_schedule ks;
  362. setup_des_key(pw, DESKEY(ks));
  363. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
  364. DESKEY(ks), DES_ENCRYPT);
  365. setup_des_key(pw + 7, DESKEY(ks));
  366. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
  367. DESKEY(ks), DES_ENCRYPT);
  368. #elif defined(USE_GNUTLS_NETTLE)
  369. struct des_ctx des;
  370. setup_des_key(pw, &des);
  371. des_encrypt(&des, 8, lmbuffer, magic);
  372. setup_des_key(pw + 7, &des);
  373. des_encrypt(&des, 8, lmbuffer + 8, magic);
  374. #elif defined(USE_GNUTLS)
  375. gcry_cipher_hd_t des;
  376. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  377. setup_des_key(pw, &des);
  378. gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
  379. gcry_cipher_close(des);
  380. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  381. setup_des_key(pw + 7, &des);
  382. gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
  383. gcry_cipher_close(des);
  384. #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
  385. || defined(USE_WIN32_CRYPTO)
  386. encrypt_des(magic, lmbuffer, pw);
  387. encrypt_des(magic, lmbuffer + 8, pw + 7);
  388. #endif
  389. memset(lmbuffer + 16, 0, 21 - 16);
  390. }
  391. return CURLE_OK;
  392. }
  393. #if USE_NTRESPONSES
  394. static void ascii_to_unicode_le(unsigned char *dest, const char *src,
  395. size_t srclen)
  396. {
  397. size_t i;
  398. for(i = 0; i < srclen; i++) {
  399. dest[2 * i] = (unsigned char)src[i];
  400. dest[2 * i + 1] = '\0';
  401. }
  402. }
  403. #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
  404. static void ascii_uppercase_to_unicode_le(unsigned char *dest,
  405. const char *src, size_t srclen)
  406. {
  407. size_t i;
  408. for(i = 0; i < srclen; i++) {
  409. dest[2 * i] = (unsigned char)(toupper(src[i]));
  410. dest[2 * i + 1] = '\0';
  411. }
  412. }
  413. #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
  414. /*
  415. * Set up nt hashed passwords
  416. * @unittest: 1600
  417. */
  418. CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
  419. const char *password,
  420. unsigned char *ntbuffer /* 21 bytes */)
  421. {
  422. size_t len = strlen(password);
  423. unsigned char *pw = malloc(len * 2);
  424. CURLcode result;
  425. if(!pw)
  426. return CURLE_OUT_OF_MEMORY;
  427. ascii_to_unicode_le(pw, password, len);
  428. /*
  429. * The NT hashed password needs to be created using the password in the
  430. * network encoding not the host encoding.
  431. */
  432. result = Curl_convert_to_network(data, (char *)pw, len * 2);
  433. if(result)
  434. return result;
  435. {
  436. /* Create NT hashed password. */
  437. #ifdef USE_OPENSSL
  438. MD4_CTX MD4pw;
  439. MD4_Init(&MD4pw);
  440. MD4_Update(&MD4pw, pw, 2 * len);
  441. MD4_Final(ntbuffer, &MD4pw);
  442. #elif defined(USE_GNUTLS_NETTLE)
  443. struct md4_ctx MD4pw;
  444. md4_init(&MD4pw);
  445. md4_update(&MD4pw, (unsigned int)(2 * len), pw);
  446. md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
  447. #elif defined(USE_GNUTLS)
  448. gcry_md_hd_t MD4pw;
  449. gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
  450. gcry_md_write(MD4pw, pw, 2 * len);
  451. memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
  452. gcry_md_close(MD4pw);
  453. #elif defined(USE_NSS) || defined(USE_OS400CRYPTO)
  454. Curl_md4it(ntbuffer, pw, 2 * len);
  455. #elif defined(USE_DARWINSSL)
  456. (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
  457. #elif defined(USE_WIN32_CRYPTO)
  458. HCRYPTPROV hprov;
  459. if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
  460. CRYPT_VERIFYCONTEXT)) {
  461. HCRYPTHASH hhash;
  462. if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
  463. DWORD length = 16;
  464. CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
  465. CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
  466. CryptDestroyHash(hhash);
  467. }
  468. CryptReleaseContext(hprov, 0);
  469. }
  470. #endif
  471. memset(ntbuffer + 16, 0, 21 - 16);
  472. }
  473. free(pw);
  474. return CURLE_OK;
  475. }
  476. #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
  477. /* This returns the HMAC MD5 digest */
  478. CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
  479. const unsigned char *data, unsigned int datalen,
  480. unsigned char *output)
  481. {
  482. HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
  483. if(!ctxt)
  484. return CURLE_OUT_OF_MEMORY;
  485. /* Update the digest with the given challenge */
  486. Curl_HMAC_update(ctxt, data, datalen);
  487. /* Finalise the digest */
  488. Curl_HMAC_final(ctxt, output);
  489. return CURLE_OK;
  490. }
  491. /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
  492. * (uppercase UserName + Domain) as the data
  493. */
  494. CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
  495. const char *domain, size_t domlen,
  496. unsigned char *ntlmhash,
  497. unsigned char *ntlmv2hash)
  498. {
  499. /* Unicode representation */
  500. size_t identity_len = (userlen + domlen) * 2;
  501. unsigned char *identity = malloc(identity_len);
  502. CURLcode result = CURLE_OK;
  503. if(!identity)
  504. return CURLE_OUT_OF_MEMORY;
  505. ascii_uppercase_to_unicode_le(identity, user, userlen);
  506. ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
  507. result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
  508. ntlmv2hash);
  509. free(identity);
  510. return result;
  511. }
  512. /*
  513. * Curl_ntlm_core_mk_ntlmv2_resp()
  514. *
  515. * This creates the NTLMv2 response as set in the ntlm type-3 message.
  516. *
  517. * Parameters:
  518. *
  519. * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
  520. * challenge_client [in] - The client nonce (8 bytes)
  521. * ntlm [in] - The ntlm data struct being used to read TargetInfo
  522. and Server challenge received in the type-2 message
  523. * ntresp [out] - The address where a pointer to newly allocated
  524. * memory holding the NTLMv2 response.
  525. * ntresp_len [out] - The length of the output message.
  526. *
  527. * Returns CURLE_OK on success.
  528. */
  529. CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
  530. unsigned char *challenge_client,
  531. struct ntlmdata *ntlm,
  532. unsigned char **ntresp,
  533. unsigned int *ntresp_len)
  534. {
  535. /* NTLMv2 response structure :
  536. ------------------------------------------------------------------------------
  537. 0 HMAC MD5 16 bytes
  538. ------BLOB--------------------------------------------------------------------
  539. 16 Signature 0x01010000
  540. 20 Reserved long (0x00000000)
  541. 24 Timestamp LE, 64-bit signed value representing the number of
  542. tenths of a microsecond since January 1, 1601.
  543. 32 Client Nonce 8 bytes
  544. 40 Unknown 4 bytes
  545. 44 Target Info N bytes (from the type-2 message)
  546. 44+N Unknown 4 bytes
  547. ------------------------------------------------------------------------------
  548. */
  549. unsigned int len = 0;
  550. unsigned char *ptr = NULL;
  551. unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
  552. curl_off_t tw;
  553. CURLcode result = CURLE_OK;
  554. #if CURL_SIZEOF_CURL_OFF_T < 8
  555. #error "this section needs 64bit support to work"
  556. #endif
  557. /* Calculate the timestamp */
  558. #ifdef DEBUGBUILD
  559. char *force_timestamp = getenv("CURL_FORCETIME");
  560. if(force_timestamp)
  561. tw = CURL_OFF_T_C(11644473600) * 10000000;
  562. else
  563. #endif
  564. tw = ((curl_off_t)time(NULL) + CURL_OFF_T_C(11644473600)) * 10000000;
  565. /* Calculate the response len */
  566. len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
  567. /* Allocate the response */
  568. ptr = malloc(len);
  569. if(!ptr)
  570. return CURLE_OUT_OF_MEMORY;
  571. memset(ptr, 0, len);
  572. /* Create the BLOB structure */
  573. snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
  574. NTLMv2_BLOB_SIGNATURE
  575. "%c%c%c%c", /* Reserved = 0 */
  576. 0, 0, 0, 0);
  577. Curl_write64_le(tw, ptr + 24);
  578. memcpy(ptr + 32, challenge_client, 8);
  579. memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
  580. /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
  581. memcpy(ptr + 8, &ntlm->nonce[0], 8);
  582. result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
  583. NTLMv2_BLOB_LEN + 8, hmac_output);
  584. if(result) {
  585. free(ptr);
  586. return result;
  587. }
  588. /* Concatenate the HMAC MD5 output with the BLOB */
  589. memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
  590. /* Return the response */
  591. *ntresp = ptr;
  592. *ntresp_len = len;
  593. return result;
  594. }
  595. /*
  596. * Curl_ntlm_core_mk_lmv2_resp()
  597. *
  598. * This creates the LMv2 response as used in the ntlm type-3 message.
  599. *
  600. * Parameters:
  601. *
  602. * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
  603. * challenge_client [in] - The client nonce (8 bytes)
  604. * challenge_client [in] - The server challenge (8 bytes)
  605. * lmresp [out] - The LMv2 response (24 bytes)
  606. *
  607. * Returns CURLE_OK on success.
  608. */
  609. CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
  610. unsigned char *challenge_client,
  611. unsigned char *challenge_server,
  612. unsigned char *lmresp)
  613. {
  614. unsigned char data[16];
  615. unsigned char hmac_output[16];
  616. CURLcode result = CURLE_OK;
  617. memcpy(&data[0], challenge_server, 8);
  618. memcpy(&data[8], challenge_client, 8);
  619. result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
  620. if(result)
  621. return result;
  622. /* Concatenate the HMAC MD5 output with the client nonce */
  623. memcpy(lmresp, hmac_output, 16);
  624. memcpy(lmresp+16, challenge_client, 8);
  625. return result;
  626. }
  627. #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
  628. #endif /* USE_NTRESPONSES */
  629. #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
  630. #endif /* USE_NTLM */