aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/transport/http/JDKHttpConnectionFactory.java
blob: 1b5d1b3c435e13b9777408fc7739f8724fc41ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2013, 2020 Christian Halstrick <christian.halstrick@sap.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.transport.http;

import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.text.MessageFormat;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

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

/**
 * A factory returning instances of
 * {@link org.eclipse.jgit.transport.http.JDKHttpConnection}
 *
 * @since 3.3
 */
public class JDKHttpConnectionFactory implements HttpConnectionFactory2 {

	@Override
	public HttpConnection create(URL url) throws IOException {
		return new JDKHttpConnection(url);
	}

	@Override
	public HttpConnection create(URL url, Proxy proxy)
			throws IOException {
		return new JDKHttpConnection(url, proxy);
	}

	@Override
	public GitSession newSession() {
		return new JdkConnectionSession();
	}

	private static class JdkConnectionSession implements GitSession {

		private SSLContext securityContext;

		private SSLSocketFactory socketFactory;

		@Override
		public JDKHttpConnection configure(HttpConnection connection,
				boolean sslVerify) throws GeneralSecurityException {
			if (!(connection instanceof JDKHttpConnection)) {
				throw new IllegalArgumentException(MessageFormat.format(
						JGitText.get().httpWrongConnectionType,
						JDKHttpConnection.class.getName(),
						connection.getClass().getName()));
			}
			JDKHttpConnection conn = (JDKHttpConnection) connection;
			String scheme = conn.getURL().getProtocol();
			if (!"https".equals(scheme) || sslVerify) { //$NON-NLS-1$
				// sslVerify == true: use the JDK defaults
				return conn;
			}
			if (securityContext == null) {
				securityContext = SSLContext.getInstance("TLS"); //$NON-NLS-1$
				TrustManager[] trustAllCerts = {
						new NoCheckX509TrustManager() };
				securityContext.init(null, trustAllCerts, null);
				socketFactory = new DelegatingSSLSocketFactory(
						securityContext.getSocketFactory()) {

					@Override
					protected void configure(SSLSocket socket) {
						HttpSupport.configureTLS(socket);
					}
				};
			}
			conn.setHostnameVerifier((name, session) -> true);
			((HttpsURLConnection) conn.wrappedUrlConnection)
					.setSSLSocketFactory(socketFactory);
			return conn;
		}

		@Override
		public void close() {
			securityContext = null;
			socketFactory = null;
		}
	}

}