JDKHttpConnectionFactory.java

  1. /*
  2.  * Copyright (C) 2013, 2020 Christian Halstrick <christian.halstrick@sap.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.transport.http;

  11. import java.io.IOException;
  12. import java.net.Proxy;
  13. import java.net.URL;
  14. import java.security.GeneralSecurityException;
  15. import java.text.MessageFormat;

  16. import javax.net.ssl.HttpsURLConnection;
  17. import javax.net.ssl.SSLContext;
  18. import javax.net.ssl.SSLSocket;
  19. import javax.net.ssl.SSLSocketFactory;
  20. import javax.net.ssl.TrustManager;

  21. import org.eclipse.jgit.internal.JGitText;
  22. import org.eclipse.jgit.internal.transport.http.DelegatingSSLSocketFactory;
  23. import org.eclipse.jgit.util.HttpSupport;

  24. /**
  25.  * A factory returning instances of
  26.  * {@link org.eclipse.jgit.transport.http.JDKHttpConnection}
  27.  *
  28.  * @since 3.3
  29.  */
  30. public class JDKHttpConnectionFactory implements HttpConnectionFactory2 {

  31.     @Override
  32.     public HttpConnection create(URL url) throws IOException {
  33.         return new JDKHttpConnection(url);
  34.     }

  35.     @Override
  36.     public HttpConnection create(URL url, Proxy proxy)
  37.             throws IOException {
  38.         return new JDKHttpConnection(url, proxy);
  39.     }

  40.     @Override
  41.     public GitSession newSession() {
  42.         return new JdkConnectionSession();
  43.     }

  44.     private static class JdkConnectionSession implements GitSession {

  45.         private SSLContext securityContext;

  46.         private SSLSocketFactory socketFactory;

  47.         @Override
  48.         public JDKHttpConnection configure(HttpConnection connection,
  49.                 boolean sslVerify) throws GeneralSecurityException {
  50.             if (!(connection instanceof JDKHttpConnection)) {
  51.                 throw new IllegalArgumentException(MessageFormat.format(
  52.                         JGitText.get().httpWrongConnectionType,
  53.                         JDKHttpConnection.class.getName(),
  54.                         connection.getClass().getName()));
  55.             }
  56.             JDKHttpConnection conn = (JDKHttpConnection) connection;
  57.             String scheme = conn.getURL().getProtocol();
  58.             if (!"https".equals(scheme) || sslVerify) { //$NON-NLS-1$
  59.                 // sslVerify == true: use the JDK defaults
  60.                 return conn;
  61.             }
  62.             if (securityContext == null) {
  63.                 securityContext = SSLContext.getInstance("TLS"); //$NON-NLS-1$
  64.                 TrustManager[] trustAllCerts = {
  65.                         new NoCheckX509TrustManager() };
  66.                 securityContext.init(null, trustAllCerts, null);
  67.                 socketFactory = new DelegatingSSLSocketFactory(
  68.                         securityContext.getSocketFactory()) {

  69.                     @Override
  70.                     protected void configure(SSLSocket socket) {
  71.                         HttpSupport.configureTLS(socket);
  72.                     }
  73.                 };
  74.             }
  75.             conn.setHostnameVerifier((name, session) -> true);
  76.             ((HttpsURLConnection) conn.wrappedUrlConnection)
  77.                     .setSSLSocketFactory(socketFactory);
  78.             return conn;
  79.         }

  80.         @Override
  81.         public void close() {
  82.             securityContext = null;
  83.             socketFactory = null;
  84.         }
  85.     }

  86. }