1
0

http_ntlm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <[email protected]>, 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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. /* NTLM details:
  25. http://davenport.sourceforge.net/ntlm.html
  26. http://www.innovation.ch/java/ntlm.html
  27. */
  28. #ifndef CURL_DISABLE_HTTP
  29. #ifdef USE_SSLEAY
  30. /* We need OpenSSL for the crypto lib to provide us with MD4 and DES */
  31. /* -- WIN32 approved -- */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdarg.h>
  35. #include <stdlib.h>
  36. #include <ctype.h>
  37. #include "urldata.h"
  38. #include "sendf.h"
  39. #include "strequal.h"
  40. #include "base64.h"
  41. #include "http_ntlm.h"
  42. #include "url.h"
  43. #include "curl_memory.h"
  44. #define _MPRINTF_REPLACE /* use our functions only */
  45. #include <curl/mprintf.h>
  46. #include <openssl/des.h>
  47. #include <openssl/md4.h>
  48. #include <openssl/ssl.h>
  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. /* This is how things were done in the old days */
  56. #define DESKEY(x) x
  57. #define DESKEYARG(x) x
  58. #else
  59. /* Modern version */
  60. #define DESKEYARG(x) *x
  61. #define DESKEY(x) &x
  62. #endif
  63. /* The last #include file should be: */
  64. #include "memdebug.h"
  65. /* Define this to make the type-3 message include the NT response message */
  66. #define USE_NTRESPONSES 1
  67. /*
  68. (*) = A "security buffer" is a triplet consisting of two shorts and one
  69. long:
  70. 1. a 'short' containing the length of the buffer in bytes
  71. 2. a 'short' containing the allocated space for the buffer in bytes
  72. 3. a 'long' containing the offset to the start of the buffer from the
  73. beginning of the NTLM message, in bytes.
  74. */
  75. CURLntlm Curl_input_ntlm(struct connectdata *conn,
  76. bool proxy, /* if proxy or not */
  77. char *header) /* rest of the www-authenticate:
  78. header */
  79. {
  80. /* point to the correct struct with this */
  81. struct ntlmdata *ntlm;
  82. ntlm = proxy?&conn->proxyntlm:&conn->ntlm;
  83. /* skip initial whitespaces */
  84. while(*header && isspace((int)*header))
  85. header++;
  86. if(checkprefix("NTLM", header)) {
  87. unsigned char buffer[256];
  88. header += strlen("NTLM");
  89. while(*header && isspace((int)*header))
  90. header++;
  91. if(*header) {
  92. /* We got a type-2 message here:
  93. Index Description Content
  94. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  95. (0x4e544c4d53535000)
  96. 8 NTLM Message Type long (0x02000000)
  97. 12 Target Name security buffer(*)
  98. 20 Flags long
  99. 24 Challenge 8 bytes
  100. (32) Context (optional) 8 bytes (two consecutive longs)
  101. (40) Target Information (optional) security buffer(*)
  102. 32 (48) start of data block
  103. */
  104. size_t size = Curl_base64_decode(header, (char *)buffer);
  105. ntlm->state = NTLMSTATE_TYPE2; /* we got a type-2 */
  106. if(size >= 48)
  107. /* the nonce of interest is index [24 .. 31], 8 bytes */
  108. memcpy(ntlm->nonce, &buffer[24], 8);
  109. /* at index decimal 20, there's a 32bit NTLM flag field */
  110. }
  111. else {
  112. if(ntlm->state >= NTLMSTATE_TYPE1)
  113. return CURLNTLM_BAD;
  114. ntlm->state = NTLMSTATE_TYPE1; /* we should sent away a type-1 */
  115. }
  116. }
  117. return CURLNTLM_FINE;
  118. }
  119. /*
  120. * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
  121. * key schedule ks is also set.
  122. */
  123. static void setup_des_key(unsigned char *key_56,
  124. DES_key_schedule DESKEYARG(ks))
  125. {
  126. DES_cblock key;
  127. key[0] = key_56[0];
  128. key[1] = ((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1);
  129. key[2] = ((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2);
  130. key[3] = ((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3);
  131. key[4] = ((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4);
  132. key[5] = ((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5);
  133. key[6] = ((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6);
  134. key[7] = (key_56[6] << 1) & 0xFF;
  135. DES_set_odd_parity(&key);
  136. DES_set_key(&key, ks);
  137. }
  138. /*
  139. * takes a 21 byte array and treats it as 3 56-bit DES keys. The
  140. * 8 byte plaintext is encrypted with each key and the resulting 24
  141. * bytes are stored in the results array.
  142. */
  143. static void calc_resp(unsigned char *keys,
  144. unsigned char *plaintext,
  145. unsigned char *results)
  146. {
  147. DES_key_schedule ks;
  148. setup_des_key(keys, DESKEY(ks));
  149. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
  150. DESKEY(ks), DES_ENCRYPT);
  151. setup_des_key(keys+7, DESKEY(ks));
  152. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results+8),
  153. DESKEY(ks), DES_ENCRYPT);
  154. setup_des_key(keys+14, DESKEY(ks));
  155. DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results+16),
  156. DESKEY(ks), DES_ENCRYPT);
  157. }
  158. /*
  159. * Set up lanmanager and nt hashed passwords
  160. */
  161. static void mkhash(char *password,
  162. unsigned char *nonce, /* 8 bytes */
  163. unsigned char *lmresp /* must fit 0x18 bytes */
  164. #ifdef USE_NTRESPONSES
  165. , unsigned char *ntresp /* must fit 0x18 bytes */
  166. #endif
  167. )
  168. {
  169. unsigned char lmbuffer[21];
  170. #ifdef USE_NTRESPONSES
  171. unsigned char ntbuffer[21];
  172. #endif
  173. unsigned char *pw;
  174. static const unsigned char magic[] = {
  175. 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25
  176. };
  177. unsigned int i;
  178. size_t len = strlen(password);
  179. /* make it fit at least 14 bytes */
  180. pw = malloc(len<7?14:len*2);
  181. if(!pw)
  182. return; /* this will lead to a badly generated package */
  183. if (len > 14)
  184. len = 14;
  185. for (i=0; i<len; i++)
  186. pw[i] = toupper(password[i]);
  187. for (; i<14; i++)
  188. pw[i] = 0;
  189. {
  190. /* create LanManager hashed password */
  191. DES_key_schedule ks;
  192. setup_des_key(pw, DESKEY(ks));
  193. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
  194. DESKEY(ks), DES_ENCRYPT);
  195. setup_des_key(pw+7, DESKEY(ks));
  196. DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer+8),
  197. DESKEY(ks), DES_ENCRYPT);
  198. memset(lmbuffer+16, 0, 5);
  199. }
  200. /* create LM responses */
  201. calc_resp(lmbuffer, nonce, lmresp);
  202. #ifdef USE_NTRESPONSES
  203. {
  204. /* create NT hashed password */
  205. MD4_CTX MD4;
  206. len = strlen(password);
  207. for (i=0; i<len; i++) {
  208. pw[2*i] = password[i];
  209. pw[2*i+1] = 0;
  210. }
  211. MD4_Init(&MD4);
  212. MD4_Update(&MD4, pw, 2*len);
  213. MD4_Final(ntbuffer, &MD4);
  214. memset(ntbuffer+16, 0, 8);
  215. }
  216. calc_resp(ntbuffer, nonce, ntresp);
  217. #endif
  218. free(pw);
  219. }
  220. #define SHORTPAIR(x) ((x) & 0xff), ((x) >> 8)
  221. #define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8)&0xff), \
  222. (((x) >>16)&0xff), ((x)>>24)
  223. /* this is for creating ntlm header output */
  224. CURLcode Curl_output_ntlm(struct connectdata *conn,
  225. bool proxy)
  226. {
  227. const char *domain=""; /* empty */
  228. const char *host=""; /* empty */
  229. int domlen=(int)strlen(domain);
  230. int hostlen = (int)strlen(host);
  231. int hostoff; /* host name offset */
  232. int domoff; /* domain name offset */
  233. size_t size;
  234. char *base64=NULL;
  235. unsigned char ntlmbuf[256]; /* enough, unless the host/domain is very long */
  236. /* point to the address of the pointer that holds the string to sent to the
  237. server, which is for a plain host or for a HTTP proxy */
  238. char **allocuserpwd;
  239. /* point to the name and password for this */
  240. char *userp;
  241. char *passwdp;
  242. /* point to the correct struct with this */
  243. struct ntlmdata *ntlm;
  244. struct auth *authp;
  245. curlassert(conn);
  246. curlassert(conn->data);
  247. if(proxy) {
  248. allocuserpwd = &conn->allocptr.proxyuserpwd;
  249. userp = conn->proxyuser;
  250. passwdp = conn->proxypasswd;
  251. ntlm = &conn->proxyntlm;
  252. authp = &conn->data->state.authproxy;
  253. }
  254. else {
  255. allocuserpwd = &conn->allocptr.userpwd;
  256. userp = conn->user;
  257. passwdp = conn->passwd;
  258. ntlm = &conn->ntlm;
  259. authp = &conn->data->state.authhost;
  260. }
  261. authp->done = FALSE;
  262. /* not set means empty */
  263. if(!userp)
  264. userp=(char *)"";
  265. if(!passwdp)
  266. passwdp=(char *)"";
  267. switch(ntlm->state) {
  268. case NTLMSTATE_TYPE1:
  269. default: /* for the weird cases we (re)start here */
  270. hostoff = 32;
  271. domoff = hostoff + hostlen;
  272. /* Create and send a type-1 message:
  273. Index Description Content
  274. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  275. (0x4e544c4d53535000)
  276. 8 NTLM Message Type long (0x01000000)
  277. 12 Flags long
  278. 16 Supplied Domain security buffer(*)
  279. 24 Supplied Workstation security buffer(*)
  280. 32 start of data block
  281. */
  282. snprintf((char *)ntlmbuf, sizeof(ntlmbuf), "NTLMSSP%c"
  283. "\x01%c%c%c" /* 32-bit type = 1 */
  284. "%c%c%c%c" /* 32-bit NTLM flag field */
  285. "%c%c" /* domain length */
  286. "%c%c" /* domain allocated space */
  287. "%c%c" /* domain name offset */
  288. "%c%c" /* 2 zeroes */
  289. "%c%c" /* host length */
  290. "%c%c" /* host allocated space */
  291. "%c%c" /* host name offset */
  292. "%c%c" /* 2 zeroes */
  293. "%s" /* host name */
  294. "%s", /* domain string */
  295. 0, /* trailing zero */
  296. 0,0,0, /* part of type-1 long */
  297. LONGQUARTET(
  298. NTLMFLAG_NEGOTIATE_OEM| /* 2 */
  299. NTLMFLAG_NEGOTIATE_NTLM_KEY /* 200 */
  300. /* equals 0x0202 */
  301. ),
  302. SHORTPAIR(domlen),
  303. SHORTPAIR(domlen),
  304. SHORTPAIR(domoff),
  305. 0,0,
  306. SHORTPAIR(hostlen),
  307. SHORTPAIR(hostlen),
  308. SHORTPAIR(hostoff),
  309. 0,0,
  310. host, domain);
  311. /* initial packet length */
  312. size = 32 + hostlen + domlen;
  313. /* now keeper of the base64 encoded package size */
  314. size = Curl_base64_encode((char *)ntlmbuf, size, &base64);
  315. if(size >0 ) {
  316. Curl_safefree(*allocuserpwd);
  317. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  318. proxy?"Proxy-":"",
  319. base64);
  320. free(base64);
  321. }
  322. else
  323. return CURLE_OUT_OF_MEMORY; /* FIX TODO */
  324. break;
  325. case NTLMSTATE_TYPE2:
  326. /* We received the type-2 already, create a type-3 message:
  327. Index Description Content
  328. 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
  329. (0x4e544c4d53535000)
  330. 8 NTLM Message Type long (0x03000000)
  331. 12 LM/LMv2 Response security buffer(*)
  332. 20 NTLM/NTLMv2 Response security buffer(*)
  333. 28 Domain Name security buffer(*)
  334. 36 User Name security buffer(*)
  335. 44 Workstation Name security buffer(*)
  336. (52) Session Key (optional) security buffer(*)
  337. (60) Flags (optional) long
  338. 52 (64) start of data block
  339. */
  340. {
  341. int lmrespoff;
  342. int ntrespoff;
  343. int useroff;
  344. unsigned char lmresp[0x18]; /* fixed-size */
  345. #ifdef USE_NTRESPONSES
  346. unsigned char ntresp[0x18]; /* fixed-size */
  347. #endif
  348. const char *user;
  349. int userlen;
  350. user = strchr(userp, '\\');
  351. if(!user)
  352. user = strchr(userp, '/');
  353. if (user) {
  354. domain = userp;
  355. domlen = (int)(user - domain);
  356. user++;
  357. }
  358. else
  359. user = userp;
  360. userlen = (int)strlen(user);
  361. mkhash(passwdp, &ntlm->nonce[0], lmresp
  362. #ifdef USE_NTRESPONSES
  363. , ntresp
  364. #endif
  365. );
  366. domoff = 64; /* always */
  367. useroff = domoff + domlen;
  368. hostoff = useroff + userlen;
  369. lmrespoff = hostoff + hostlen;
  370. ntrespoff = lmrespoff + 0x18;
  371. /* Create the big type-3 message binary blob */
  372. size = snprintf((char *)ntlmbuf, sizeof(ntlmbuf),
  373. "NTLMSSP%c"
  374. "\x03%c%c%c" /* type-3, 32 bits */
  375. "%c%c%c%c" /* LanManager length + allocated space */
  376. "%c%c" /* LanManager offset */
  377. "%c%c" /* 2 zeroes */
  378. "%c%c" /* NT-response length */
  379. "%c%c" /* NT-response allocated space */
  380. "%c%c" /* NT-response offset */
  381. "%c%c" /* 2 zeroes */
  382. "%c%c" /* domain length */
  383. "%c%c" /* domain allocated space */
  384. "%c%c" /* domain name offset */
  385. "%c%c" /* 2 zeroes */
  386. "%c%c" /* user length */
  387. "%c%c" /* user allocated space */
  388. "%c%c" /* user offset */
  389. "%c%c" /* 2 zeroes */
  390. "%c%c" /* host length */
  391. "%c%c" /* host allocated space */
  392. "%c%c" /* host offset */
  393. "%c%c%c%c%c%c" /* 6 zeroes */
  394. "\xff\xff" /* message length */
  395. "%c%c" /* 2 zeroes */
  396. "\x01\x82" /* flags */
  397. "%c%c" /* 2 zeroes */
  398. /* domain string */
  399. /* user string */
  400. /* host string */
  401. /* LanManager response */
  402. /* NT response */
  403. ,
  404. 0, /* zero termination */
  405. 0,0,0, /* type-3 long, the 24 upper bits */
  406. SHORTPAIR(0x18), /* LanManager response length, twice */
  407. SHORTPAIR(0x18),
  408. SHORTPAIR(lmrespoff),
  409. 0x0, 0x0,
  410. #ifdef USE_NTRESPONSES
  411. SHORTPAIR(0x18), /* NT-response length, twice */
  412. SHORTPAIR(0x18),
  413. #else
  414. 0x0, 0x0,
  415. 0x0, 0x0,
  416. #endif
  417. SHORTPAIR(ntrespoff),
  418. 0x0, 0x0,
  419. SHORTPAIR(domlen),
  420. SHORTPAIR(domlen),
  421. SHORTPAIR(domoff),
  422. 0x0, 0x0,
  423. SHORTPAIR(userlen),
  424. SHORTPAIR(userlen),
  425. SHORTPAIR(useroff),
  426. 0x0, 0x0,
  427. SHORTPAIR(hostlen),
  428. SHORTPAIR(hostlen),
  429. SHORTPAIR(hostoff),
  430. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  431. 0x0, 0x0,
  432. 0x0, 0x0);
  433. /* size is now 64 */
  434. size=64;
  435. ntlmbuf[62]=ntlmbuf[63]=0;
  436. memcpy(&ntlmbuf[size], domain, domlen);
  437. size += domlen;
  438. memcpy(&ntlmbuf[size], user, userlen);
  439. size += userlen;
  440. /* we append the binary hashes to the end of the blob */
  441. if(size < ((int)sizeof(ntlmbuf) - 0x18)) {
  442. memcpy(&ntlmbuf[size], lmresp, 0x18);
  443. size += 0x18;
  444. }
  445. #ifdef USE_NTRESPONSES
  446. if(size < ((int)sizeof(ntlmbuf) - 0x18)) {
  447. memcpy(&ntlmbuf[size], ntresp, 0x18);
  448. size += 0x18;
  449. }
  450. #endif
  451. ntlmbuf[56] = (unsigned char)(size & 0xff);
  452. ntlmbuf[57] = (unsigned char)(size >> 8);
  453. /* convert the binary blob into base64 */
  454. size = Curl_base64_encode((char *)ntlmbuf, size, &base64);
  455. if(size >0 ) {
  456. Curl_safefree(*allocuserpwd);
  457. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  458. proxy?"Proxy-":"",
  459. base64);
  460. free(base64);
  461. }
  462. else
  463. return CURLE_OUT_OF_MEMORY; /* FIX TODO */
  464. ntlm->state = NTLMSTATE_TYPE3; /* we sent a type-3 */
  465. authp->done = TRUE;
  466. }
  467. break;
  468. case NTLMSTATE_TYPE3:
  469. /* connection is already authenticated,
  470. * don't send a header in future requests */
  471. if(*allocuserpwd) {
  472. free(*allocuserpwd);
  473. *allocuserpwd=NULL;
  474. }
  475. authp->done = TRUE;
  476. break;
  477. }
  478. return CURLE_OK;
  479. }
  480. #endif /* USE_SSLEAY */
  481. #endif /* !CURL_DISABLE_HTTP */