Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HttpClientConnector.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.transport.sshd.proxy;
  44. import static java.text.MessageFormat.format;
  45. import java.io.IOException;
  46. import java.net.HttpURLConnection;
  47. import java.net.InetSocketAddress;
  48. import java.nio.charset.StandardCharsets;
  49. import java.util.ArrayList;
  50. import java.util.Arrays;
  51. import java.util.Iterator;
  52. import java.util.List;
  53. import org.apache.sshd.client.session.ClientSession;
  54. import org.apache.sshd.common.io.IoSession;
  55. import org.apache.sshd.common.util.Readable;
  56. import org.apache.sshd.common.util.buffer.Buffer;
  57. import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
  58. import org.eclipse.jgit.annotations.NonNull;
  59. import org.eclipse.jgit.internal.transport.sshd.GssApiMechanisms;
  60. import org.eclipse.jgit.internal.transport.sshd.SshdText;
  61. import org.eclipse.jgit.internal.transport.sshd.auth.AuthenticationHandler;
  62. import org.eclipse.jgit.internal.transport.sshd.auth.BasicAuthentication;
  63. import org.eclipse.jgit.internal.transport.sshd.auth.GssApiAuthentication;
  64. import org.eclipse.jgit.util.Base64;
  65. import org.ietf.jgss.GSSContext;
  66. /**
  67. * Simple HTTP proxy connector using Basic Authentication.
  68. */
  69. public class HttpClientConnector extends AbstractClientProxyConnector {
  70. private static final String HTTP_HEADER_PROXY_AUTHENTICATION = "Proxy-Authentication:"; //$NON-NLS-1$
  71. private static final String HTTP_HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization:"; //$NON-NLS-1$
  72. private HttpAuthenticationHandler basic;
  73. private HttpAuthenticationHandler negotiate;
  74. private List<HttpAuthenticationHandler> availableAuthentications;
  75. private Iterator<HttpAuthenticationHandler> clientAuthentications;
  76. private HttpAuthenticationHandler authenticator;
  77. private boolean ongoing;
  78. /**
  79. * Creates a new {@link HttpClientConnector}. The connector supports
  80. * anonymous proxy connections as well as Basic and Negotiate
  81. * authentication.
  82. *
  83. * @param proxyAddress
  84. * of the proxy server we're connecting to
  85. * @param remoteAddress
  86. * of the target server to connect to
  87. */
  88. public HttpClientConnector(@NonNull InetSocketAddress proxyAddress,
  89. @NonNull InetSocketAddress remoteAddress) {
  90. this(proxyAddress, remoteAddress, null, null);
  91. }
  92. /**
  93. * Creates a new {@link HttpClientConnector}. The connector supports
  94. * anonymous proxy connections as well as Basic and Negotiate
  95. * authentication. If a user name and password are given, the connector
  96. * tries pre-emptive Basic authentication.
  97. *
  98. * @param proxyAddress
  99. * of the proxy server we're connecting to
  100. * @param remoteAddress
  101. * of the target server to connect to
  102. * @param proxyUser
  103. * to authenticate at the proxy with
  104. * @param proxyPassword
  105. * to authenticate at the proxy with
  106. */
  107. public HttpClientConnector(@NonNull InetSocketAddress proxyAddress,
  108. @NonNull InetSocketAddress remoteAddress, String proxyUser,
  109. char[] proxyPassword) {
  110. super(proxyAddress, remoteAddress, proxyUser, proxyPassword);
  111. basic = new HttpBasicAuthentication();
  112. negotiate = new NegotiateAuthentication();
  113. availableAuthentications = new ArrayList<>(2);
  114. availableAuthentications.add(negotiate);
  115. availableAuthentications.add(basic);
  116. clientAuthentications = availableAuthentications.iterator();
  117. }
  118. private void close() {
  119. HttpAuthenticationHandler current = authenticator;
  120. authenticator = null;
  121. if (current != null) {
  122. current.close();
  123. }
  124. }
  125. @Override
  126. public void sendClientProxyMetadata(ClientSession sshSession)
  127. throws Exception {
  128. init(sshSession);
  129. IoSession session = sshSession.getIoSession();
  130. session.addCloseFutureListener(f -> close());
  131. StringBuilder msg = connect();
  132. if (proxyUser != null && !proxyUser.isEmpty()
  133. || proxyPassword != null && proxyPassword.length > 0) {
  134. authenticator = basic;
  135. basic.setParams(null);
  136. basic.start();
  137. msg = authenticate(msg, basic.getToken());
  138. clearPassword();
  139. proxyUser = null;
  140. }
  141. ongoing = true;
  142. try {
  143. send(msg, session);
  144. } catch (Exception e) {
  145. ongoing = false;
  146. throw e;
  147. }
  148. }
  149. private void send(StringBuilder msg, IoSession session) throws Exception {
  150. byte[] data = eol(msg).toString().getBytes(StandardCharsets.US_ASCII);
  151. Buffer buffer = new ByteArrayBuffer(data.length, false);
  152. buffer.putRawBytes(data);
  153. session.writePacket(buffer).verify(getTimeout());
  154. }
  155. private StringBuilder connect() {
  156. StringBuilder msg = new StringBuilder();
  157. // Persistent connections are the default in HTTP 1.1 (see RFC 2616),
  158. // but let's be explicit.
  159. return msg.append(format(
  160. "CONNECT {0}:{1} HTTP/1.1\r\nProxy-Connection: keep-alive\r\nConnection: keep-alive\r\nHost: {0}:{1}\r\n", //$NON-NLS-1$
  161. remoteAddress.getHostString(),
  162. Integer.toString(remoteAddress.getPort())));
  163. }
  164. private StringBuilder authenticate(StringBuilder msg, String token) {
  165. msg.append(HTTP_HEADER_PROXY_AUTHORIZATION).append(' ').append(token);
  166. return eol(msg);
  167. }
  168. private StringBuilder eol(StringBuilder msg) {
  169. return msg.append('\r').append('\n');
  170. }
  171. @Override
  172. public void messageReceived(IoSession session, Readable buffer)
  173. throws Exception {
  174. try {
  175. int length = buffer.available();
  176. byte[] data = new byte[length];
  177. buffer.getRawBytes(data, 0, length);
  178. String[] reply = new String(data, StandardCharsets.US_ASCII)
  179. .split("\r\n"); //$NON-NLS-1$
  180. handleMessage(session, Arrays.asList(reply));
  181. } catch (Exception e) {
  182. if (authenticator != null) {
  183. authenticator.close();
  184. authenticator = null;
  185. }
  186. ongoing = false;
  187. try {
  188. setDone(false);
  189. } catch (Exception inner) {
  190. e.addSuppressed(inner);
  191. }
  192. throw e;
  193. }
  194. }
  195. private void handleMessage(IoSession session, List<String> reply)
  196. throws Exception {
  197. if (reply.isEmpty() || reply.get(0).isEmpty()) {
  198. throw new IOException(
  199. format(SshdText.get().proxyHttpUnexpectedReply,
  200. proxyAddress, "<empty>")); //$NON-NLS-1$
  201. }
  202. try {
  203. StatusLine status = HttpParser.parseStatusLine(reply.get(0));
  204. if (!ongoing) {
  205. throw new IOException(format(
  206. SshdText.get().proxyHttpUnexpectedReply, proxyAddress,
  207. Integer.toString(status.getResultCode()),
  208. status.getReason()));
  209. }
  210. switch (status.getResultCode()) {
  211. case HttpURLConnection.HTTP_OK:
  212. if (authenticator != null) {
  213. authenticator.close();
  214. }
  215. authenticator = null;
  216. ongoing = false;
  217. setDone(true);
  218. break;
  219. case HttpURLConnection.HTTP_PROXY_AUTH:
  220. List<AuthenticationChallenge> challenges = HttpParser
  221. .getAuthenticationHeaders(reply,
  222. HTTP_HEADER_PROXY_AUTHENTICATION);
  223. authenticator = selectProtocol(challenges, authenticator);
  224. if (authenticator == null) {
  225. throw new IOException(
  226. format(SshdText.get().proxyCannotAuthenticate,
  227. proxyAddress));
  228. }
  229. String token = authenticator.getToken();
  230. if (token == null) {
  231. throw new IOException(
  232. format(SshdText.get().proxyCannotAuthenticate,
  233. proxyAddress));
  234. }
  235. send(authenticate(connect(), token), session);
  236. break;
  237. default:
  238. throw new IOException(format(SshdText.get().proxyHttpFailure,
  239. proxyAddress, Integer.toString(status.getResultCode()),
  240. status.getReason()));
  241. }
  242. } catch (HttpParser.ParseException e) {
  243. throw new IOException(
  244. format(SshdText.get().proxyHttpUnexpectedReply,
  245. proxyAddress, reply.get(0)));
  246. }
  247. }
  248. private HttpAuthenticationHandler selectProtocol(
  249. List<AuthenticationChallenge> challenges,
  250. HttpAuthenticationHandler current) throws Exception {
  251. if (current != null && !current.isDone()) {
  252. AuthenticationChallenge challenge = getByName(challenges,
  253. current.getName());
  254. if (challenge != null) {
  255. current.setParams(challenge);
  256. current.process();
  257. return current;
  258. }
  259. }
  260. if (current != null) {
  261. current.close();
  262. }
  263. while (clientAuthentications.hasNext()) {
  264. HttpAuthenticationHandler next = clientAuthentications.next();
  265. if (!next.isDone()) {
  266. AuthenticationChallenge challenge = getByName(challenges,
  267. next.getName());
  268. if (challenge != null) {
  269. next.setParams(challenge);
  270. next.start();
  271. return next;
  272. }
  273. }
  274. }
  275. return null;
  276. }
  277. private AuthenticationChallenge getByName(
  278. List<AuthenticationChallenge> challenges,
  279. String name) {
  280. return challenges.stream()
  281. .filter(c -> c.getMechanism().equalsIgnoreCase(name))
  282. .findFirst().orElse(null);
  283. }
  284. private interface HttpAuthenticationHandler
  285. extends AuthenticationHandler<AuthenticationChallenge, String> {
  286. public String getName();
  287. }
  288. /**
  289. * @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
  290. */
  291. private class HttpBasicAuthentication
  292. extends BasicAuthentication<AuthenticationChallenge, String>
  293. implements HttpAuthenticationHandler {
  294. private boolean asked;
  295. public HttpBasicAuthentication() {
  296. super(proxyAddress, proxyUser, proxyPassword);
  297. }
  298. @Override
  299. public String getName() {
  300. return "Basic"; //$NON-NLS-1$
  301. }
  302. @Override
  303. protected void askCredentials() {
  304. // We ask only once.
  305. if (asked) {
  306. throw new IllegalStateException(
  307. "Basic auth: already asked user for password"); //$NON-NLS-1$
  308. }
  309. asked = true;
  310. super.askCredentials();
  311. done = true;
  312. }
  313. @Override
  314. public String getToken() throws Exception {
  315. if (user.indexOf(':') >= 0) {
  316. throw new IOException(format(
  317. SshdText.get().proxyHttpInvalidUserName, proxy, user));
  318. }
  319. byte[] rawUser = user.getBytes(StandardCharsets.UTF_8);
  320. byte[] toEncode = new byte[rawUser.length + 1 + password.length];
  321. System.arraycopy(rawUser, 0, toEncode, 0, rawUser.length);
  322. toEncode[rawUser.length] = ':';
  323. System.arraycopy(password, 0, toEncode, rawUser.length + 1,
  324. password.length);
  325. Arrays.fill(password, (byte) 0);
  326. String result = Base64.encodeBytes(toEncode);
  327. Arrays.fill(toEncode, (byte) 0);
  328. return getName() + ' ' + result;
  329. }
  330. }
  331. /**
  332. * @see <a href="https://tools.ietf.org/html/rfc4559">RFC 4559</a>
  333. */
  334. private class NegotiateAuthentication
  335. extends GssApiAuthentication<AuthenticationChallenge, String>
  336. implements HttpAuthenticationHandler {
  337. public NegotiateAuthentication() {
  338. super(proxyAddress);
  339. }
  340. @Override
  341. public String getName() {
  342. return "Negotiate"; //$NON-NLS-1$
  343. }
  344. @Override
  345. public String getToken() throws Exception {
  346. return getName() + ' ' + Base64.encodeBytes(token);
  347. }
  348. @Override
  349. protected GSSContext createContext() throws Exception {
  350. return GssApiMechanisms.createContext(GssApiMechanisms.SPNEGO,
  351. GssApiMechanisms.getCanonicalName(proxyAddress));
  352. }
  353. @Override
  354. protected byte[] extractToken(AuthenticationChallenge input)
  355. throws Exception {
  356. String received = input.getToken();
  357. if (received == null) {
  358. return new byte[0];
  359. }
  360. return Base64.decode(received);
  361. }
  362. }
  363. }