curl_ntlm_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://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) && !defined(USE_WINDOWS_SSPI)
  24. /*
  25. * NTLM details:
  26. *
  27. * http://davenport.sourceforge.net/ntlm.html
  28. * http://www.innovation.ch/java/ntlm.html
  29. */
  30. #ifdef USE_SSLEAY
  31. # ifdef USE_OPENSSL
  32. # include <openssl/des.h>
  33. # ifndef OPENSSL_NO_MD4
  34. # include <openssl/md4.h>
  35. # endif
  36. # include <openssl/md5.h>
  37. # include <openssl/ssl.h>
  38. # include <openssl/rand.h>
  39. # else
  40. # include <des.h>
  41. # ifndef OPENSSL_NO_MD4
  42. # include <md4.h>
  43. # endif
  44. # include <md5.h>
  45. # include <ssl.h>
  46. # include <rand.h>
  47. # endif
  48. # if (OPENSSL_VERSION_NUMBER < 0x00907001L)
  49. # define DES_key_schedule des_key_schedule
  50. # define DES_cblock des_cblock
  51. # define DES_set_odd_parity des_set_odd_parity
  52. # define DES_set_key des_set_key
  53. # define DES_ecb_encrypt des_ecb_encrypt
  54. # define DESKEY(x) x
  55. # define DESKEYARG(x) x
  56. # else
  57. # define DESKEYARG(x) *x
  58. # define DESKEY(x) &x
  59. # endif
  60. #elif defined(USE_GNUTLS_NETTLE)
  61. # include <nettle/des.h>
  62. # include <nettle/md4.h>
  63. #elif defined(USE_GNUTLS)
  64. # include <gcrypt.h>
  65. # define MD5_DIGEST_LENGTH 16
  66. # define MD4_DIGEST_LENGTH 16
  67. #elif defined(USE_NSS)
  68. # include <nss.h>
  69. # include <pk11pub.h>
  70. # include <hasht.h>
  71. # include "curl_md4.h"
  72. # define MD5_DIGEST_LENGTH MD5_LENGTH
  73. #elif defined(USE_DARWINSSL)
  74. # include <CommonCrypto/CommonCryptor.h>
  75. # include <CommonCrypto/CommonDigest.h>
  76. #else
  77. # error "Can't compile NTLM support without a crypto library."
  78. #endif
  79. #include "urldata.h"
  80. #include "non-ascii.h"
  81. #include "rawstr.h"
  82. #include "curl_memory.h"
  83. #include "curl_ntlm_core.h"
  84. #define _MPRINTF_REPLACE /* use our functions only */
  85. #include <curl/mprintf.h>
  86. /* The last #include file should be: */
  87. #include "memdebug.h"
  88. #ifdef USE_SSLEAY
  89. /*
  90. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
  91. * key schedule ks is also set.
  92. */
  93. static void setup_des_key(const unsigned char *key_56,
  94. DES_key_schedule DESKEYARG(ks))
  95. {
  96. DES_cblock key;
  97. key[0] = key_56[0];
  98. key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
  99. key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
  100. key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
  101. key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
  102. key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
  103. key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
  104. key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
  105. DES_set_odd_parity(&key);
  106. DES_set_key(&key, ks);
  107. }
  108. #else /* defined(USE_SSLEAY) */
  109. /*
  110. * Turns a 56 bit key into the 64 bit, odd parity key. Used by GnuTLS and NSS.
  111. */
  112. static void extend_key_56_to_64(const unsigned char *key_56, char *key)
  113. {
  114. key[0] = key_56[0];
  115. key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
  116. key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
  117. key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
  118. key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
  119. key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
  120. key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
  121. key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
  122. }
  123. #if defined(USE_GNUTLS_NETTLE)
  124. static void setup_des_key(const unsigned char *key_56,
  125. struct des_ctx *des)
  126. {
  127. char key[8];
  128. extend_key_56_to_64(key_56, key);
  129. des_set_key(des, (const uint8_t*)key);
  130. }
  131. #elif defined(USE_GNUTLS)
  132. /*
  133. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
  134. */
  135. static void setup_des_key(const unsigned char *key_56,
  136. gcry_cipher_hd_t *des)
  137. {
  138. char key[8];
  139. extend_key_56_to_64(key_56, key);
  140. gcry_cipher_setkey(*des, key, 8);
  141. }
  142. #elif defined(USE_NSS)
  143. /*
  144. * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
  145. * the expanded key. The caller is responsible for giving 64 bit of valid
  146. * data is IN and (at least) 64 bit large buffer as OUT.
  147. */
  148. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  149. const unsigned char *key_56)
  150. {
  151. const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
  152. PK11SlotInfo *slot = NULL;
  153. char key[8]; /* expanded 64 bit key */
  154. SECItem key_item;
  155. PK11SymKey *symkey = NULL;
  156. SECItem *param = NULL;
  157. PK11Context *ctx = NULL;
  158. int out_len; /* not used, required by NSS */
  159. bool rv = FALSE;
  160. /* use internal slot for DES encryption (requires NSS to be initialized) */
  161. slot = PK11_GetInternalKeySlot();
  162. if(!slot)
  163. return FALSE;
  164. /* expand the 56 bit key to 64 bit and wrap by NSS */
  165. extend_key_56_to_64(key_56, key);
  166. key_item.data = (unsigned char *)key;
  167. key_item.len = /* hard-wired */ 8;
  168. symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
  169. &key_item, NULL);
  170. if(!symkey)
  171. goto fail;
  172. /* create DES encryption context */
  173. param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
  174. if(!param)
  175. goto fail;
  176. ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
  177. if(!ctx)
  178. goto fail;
  179. /* perform the encryption */
  180. if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
  181. (unsigned char *)in, /* inbuflen */ 8)
  182. && SECSuccess == PK11_Finalize(ctx))
  183. rv = /* all OK */ TRUE;
  184. fail:
  185. /* cleanup */
  186. if(ctx)
  187. PK11_DestroyContext(ctx, PR_TRUE);
  188. if(symkey)
  189. PK11_FreeSymKey(symkey);
  190. if(param)
  191. SECITEM_FreeItem(param, PR_TRUE);
  192. PK11_FreeSlot(slot);
  193. return rv;
  194. }
  195. #elif defined(USE_DARWINSSL)
  196. static bool encrypt_des(const unsigned char *in, unsigned char *out,
  197. const unsigned char *key_56)
  198. {
  199. char key[8];
  200. size_t out_len;
  201. CCCryptorStatus err;
  202. extend_key_56_to_64(key_56, key);
  203. err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
  204. kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
  205. 8 /* outbuflen */, &out_len);
  206. return err == kCCSuccess;
  207. }
  208. #endif /* defined(USE_DARWINSSL) */
  209. #endif /* defined(USE_SSLEAY) */
  210. /*
  211. * takes a 21 byte array and treats it as 3 56-bit DES keys. The
  212. * 8 byte plaintext is encrypted with each key and the resulting 24
  213. * bytes are stored in the results array.
  214. */
  215. void Curl_ntlm_core_lm_resp(const unsigned char *keys,
  216. const unsigned char *plaintext,
  217. unsigned char *results)
  218. {
  219. #ifdef USE_SSLEAY
  220. DES_key_schedule ks;
  221. setup_des_key(keys, DESKEY(ks));
  222. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
  223. DESKEY(ks), DES_ENCRYPT);
  224. setup_des_key(keys + 7, DESKEY(ks));
  225. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
  226. DESKEY(ks), DES_ENCRYPT);
  227. setup_des_key(keys + 14, DESKEY(ks));
  228. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
  229. DESKEY(ks), DES_ENCRYPT);
  230. #elif defined(USE_GNUTLS_NETTLE)
  231. struct des_ctx des;
  232. setup_des_key(keys, &des);
  233. des_encrypt(&des, 8, results, plaintext);
  234. setup_des_key(keys + 7, &des);
  235. des_encrypt(&des, 8, results + 8, plaintext);
  236. setup_des_key(keys + 14, &des);
  237. des_encrypt(&des, 8, results + 16, plaintext);
  238. #elif defined(USE_GNUTLS)
  239. gcry_cipher_hd_t des;
  240. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  241. setup_des_key(keys, &des);
  242. gcry_cipher_encrypt(des, results, 8, plaintext, 8);
  243. gcry_cipher_close(des);
  244. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  245. setup_des_key(keys + 7, &des);
  246. gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
  247. gcry_cipher_close(des);
  248. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  249. setup_des_key(keys + 14, &des);
  250. gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
  251. gcry_cipher_close(des);
  252. #elif defined(USE_NSS) || defined(USE_DARWINSSL)
  253. encrypt_des(plaintext, results, keys);
  254. encrypt_des(plaintext, results + 8, keys + 7);
  255. encrypt_des(plaintext, results + 16, keys + 14);
  256. #endif
  257. }
  258. /*
  259. * Set up lanmanager hashed password
  260. */
  261. void Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
  262. const char *password,
  263. unsigned char *lmbuffer /* 21 bytes */)
  264. {
  265. CURLcode res;
  266. unsigned char pw[14];
  267. static const unsigned char magic[] = {
  268. 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
  269. };
  270. size_t len = CURLMIN(strlen(password), 14);
  271. Curl_strntoupper((char *)pw, password, len);
  272. memset(&pw[len], 0, 14 - len);
  273. /*
  274. * The LanManager hashed password needs to be created using the
  275. * password in the network encoding not the host encoding.
  276. */
  277. res = Curl_convert_to_network(data, (char *)pw, 14);
  278. if(res)
  279. return;
  280. {
  281. /* Create LanManager hashed password. */
  282. #ifdef USE_SSLEAY
  283. DES_key_schedule ks;
  284. setup_des_key(pw, DESKEY(ks));
  285. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
  286. DESKEY(ks), DES_ENCRYPT);
  287. setup_des_key(pw + 7, DESKEY(ks));
  288. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
  289. DESKEY(ks), DES_ENCRYPT);
  290. #elif defined(USE_GNUTLS_NETTLE)
  291. struct des_ctx des;
  292. setup_des_key(pw, &des);
  293. des_encrypt(&des, 8, lmbuffer, magic);
  294. setup_des_key(pw + 7, &des);
  295. des_encrypt(&des, 8, lmbuffer + 8, magic);
  296. #elif defined(USE_GNUTLS)
  297. gcry_cipher_hd_t des;
  298. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  299. setup_des_key(pw, &des);
  300. gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
  301. gcry_cipher_close(des);
  302. gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  303. setup_des_key(pw + 7, &des);
  304. gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
  305. gcry_cipher_close(des);
  306. #elif defined(USE_NSS) || defined(USE_DARWINSSL)
  307. encrypt_des(magic, lmbuffer, pw);
  308. encrypt_des(magic, lmbuffer + 8, pw + 7);
  309. #endif
  310. memset(lmbuffer + 16, 0, 21 - 16);
  311. }
  312. }
  313. #if USE_NTRESPONSES
  314. static void ascii_to_unicode_le(unsigned char *dest, const char *src,
  315. size_t srclen)
  316. {
  317. size_t i;
  318. for(i = 0; i < srclen; i++) {
  319. dest[2 * i] = (unsigned char)src[i];
  320. dest[2 * i + 1] = '\0';
  321. }
  322. }
  323. /*
  324. * Set up nt hashed passwords
  325. */
  326. CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
  327. const char *password,
  328. unsigned char *ntbuffer /* 21 bytes */)
  329. {
  330. size_t len = strlen(password);
  331. unsigned char *pw = malloc(len * 2);
  332. CURLcode result;
  333. if(!pw)
  334. return CURLE_OUT_OF_MEMORY;
  335. ascii_to_unicode_le(pw, password, len);
  336. /*
  337. * The NT hashed password needs to be created using the password in the
  338. * network encoding not the host encoding.
  339. */
  340. result = Curl_convert_to_network(data, (char *)pw, len * 2);
  341. if(result)
  342. return result;
  343. {
  344. /* Create NT hashed password. */
  345. #ifdef USE_SSLEAY
  346. MD4_CTX MD4pw;
  347. MD4_Init(&MD4pw);
  348. MD4_Update(&MD4pw, pw, 2 * len);
  349. MD4_Final(ntbuffer, &MD4pw);
  350. #elif defined(USE_GNUTLS_NETTLE)
  351. struct md4_ctx MD4pw;
  352. md4_init(&MD4pw);
  353. md4_update(&MD4pw, (unsigned int)(2 * len), pw);
  354. md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
  355. #elif defined(USE_GNUTLS)
  356. gcry_md_hd_t MD4pw;
  357. gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
  358. gcry_md_write(MD4pw, pw, 2 * len);
  359. memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
  360. gcry_md_close(MD4pw);
  361. #elif defined(USE_NSS)
  362. Curl_md4it(ntbuffer, pw, 2 * len);
  363. #elif defined(USE_DARWINSSL)
  364. (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
  365. #endif
  366. memset(ntbuffer + 16, 0, 21 - 16);
  367. }
  368. free(pw);
  369. return CURLE_OK;
  370. }
  371. #endif /* USE_NTRESPONSES */
  372. #endif /* USE_NTLM && !USE_WINDOWS_SSPI */