curl_ntlm_core.c 11 KB

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