Skip to content

Commit 50a9827

Browse files
authored
Merge pull request #136 from scottslewis/sslcontext
SslcontextFactory introduced to ECF core and used in JRE httpclient
2 parents e355fb4 + 8e0e523 commit 50a9827

File tree

16 files changed

+791
-90
lines changed

16 files changed

+791
-90
lines changed

framework/bundles/org.eclipse.ecf/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-Name: %plugin.name
33
Bundle-SymbolicName: org.eclipse.ecf;singleton:=true
44
Automatic-Module-Name: org.eclipse.ecf
5-
Bundle-Version: 3.11.0.qualifier
5+
Bundle-Version: 3.12.0.qualifier
66
Bundle-Activator: org.eclipse.ecf.internal.core.ECFPlugin
77
Bundle-Vendor: %plugin.provider
88
Bundle-Localization: plugin

framework/bundles/org.eclipse.ecf/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</parent>
1111
<groupId>org.eclipse.ecf</groupId>
1212
<artifactId>org.eclipse.ecf</artifactId>
13-
<version>3.11.0-SNAPSHOT</version>
13+
<version>3.12.0-SNAPSHOT</version>
1414
<packaging>eclipse-plugin</packaging>
1515
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/****************************************************************************
2+
* Copyright (c) 2024 Composent, Inc. and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* Contributors: Composent, Inc. - initial API and implementation
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
*****************************************************************************/
12+
package org.eclipse.ecf.core.security;
13+
14+
import java.security.*;
15+
import java.util.Optional;
16+
import javax.net.ssl.SSLContext;
17+
import org.eclipse.core.runtime.IStatus;
18+
import org.eclipse.core.runtime.Status;
19+
import org.eclipse.ecf.internal.core.identity.Activator;
20+
import org.osgi.framework.BundleContext;
21+
import org.osgi.util.tracker.ServiceTracker;
22+
23+
/**
24+
* @since 3.12
25+
*/
26+
public class ECFSSLContextFactory implements SSLContextFactory {
27+
28+
private final ServiceTracker<Provider, Provider> providerTracker;
29+
private final String defaultProtocol;
30+
private final String defaultProviderName;
31+
32+
public ECFSSLContextFactory(BundleContext context, String defaultProtocol) {
33+
this(context, defaultProtocol, null);
34+
}
35+
36+
public ECFSSLContextFactory(BundleContext context, String defaultProtocol, String defaultProviderName) {
37+
this.defaultProtocol = defaultProtocol;
38+
this.defaultProviderName = defaultProviderName;
39+
this.providerTracker = new ServiceTracker<Provider, Provider>(context, Provider.class, null);
40+
this.providerTracker.open();
41+
}
42+
43+
@Override
44+
public SSLContext getDefault() throws NoSuchAlgorithmException, NoSuchProviderException {
45+
return getInstance0(this.defaultProtocol, this.defaultProviderName);
46+
}
47+
48+
protected SSLContext getInstance0(String protocol, String providerName) throws NoSuchAlgorithmException, NoSuchProviderException {
49+
if (protocol == null) {
50+
return SSLContext.getDefault();
51+
}
52+
Provider provider = findProvider(providerName);
53+
if (provider == null)
54+
throw new NoSuchProviderException("No provider registered named '" + providerName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
55+
return SSLContext.getInstance(protocol, provider);
56+
}
57+
58+
@Override
59+
public SSLContext getInstance(String protocol) throws NoSuchAlgorithmException, NoSuchProviderException {
60+
return getInstance0(protocol, this.defaultProviderName);
61+
}
62+
63+
public void close() {
64+
this.providerTracker.close();
65+
}
66+
67+
protected Provider findProvider(String providerName) {
68+
if (providerName == null) {
69+
return this.providerTracker.getService();
70+
}
71+
Optional<Provider> optResult = this.providerTracker.getTracked().values().stream().filter(p ->
72+
// test that providerName is equal to Provider.getName()
73+
providerName.equals(p.getName())).findFirst();
74+
// If there are matching Providers, use first (highest priority from sorted map) and use to create SSLContext.
75+
// If none, then throw
76+
if (optResult.isPresent()) {
77+
return optResult.get();
78+
}
79+
// If providerName is same as current default SSLContext then use it
80+
try {
81+
SSLContext defaultContext = SSLContext.getDefault();
82+
if (providerName.equals(defaultContext.getProvider().getName())) {
83+
return defaultContext.getProvider();
84+
}
85+
} catch (NoSuchAlgorithmException e) {
86+
Activator.getDefault().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not get SSLContext.getDefault()", e)); //$NON-NLS-1$
87+
}
88+
return null;
89+
}
90+
91+
@Override
92+
public SSLContext getInstance(String protocol, String providerName) throws NoSuchAlgorithmException, NoSuchProviderException {
93+
return getInstance0(protocol, providerName);
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/****************************************************************************
2+
* Copyright (c) 2024 Composent, Inc. and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* Contributors: Composent, Inc. - initial API and implementation
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
*****************************************************************************/
12+
package org.eclipse.ecf.core.security;
13+
14+
import java.security.NoSuchAlgorithmException;
15+
import java.security.NoSuchProviderException;
16+
import javax.net.ssl.SSLContext;
17+
18+
/**
19+
* @since 3.12
20+
*/
21+
public interface SSLContextFactory {
22+
23+
SSLContext getDefault() throws NoSuchAlgorithmException, NoSuchProviderException;
24+
25+
SSLContext getInstance(String protocol) throws NoSuchAlgorithmException, NoSuchProviderException;
26+
27+
SSLContext getInstance(String protocol, String providerName) throws NoSuchAlgorithmException, NoSuchProviderException;
28+
29+
}

framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/internal/core/ECFPlugin.java

+20
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
package org.eclipse.ecf.internal.core;
1313

1414
import java.util.*;
15+
import javax.net.ssl.SSLContext;
1516
import org.eclipse.core.runtime.*;
1617
import org.eclipse.ecf.core.*;
1718
import org.eclipse.ecf.core.identity.ID;
1819
import org.eclipse.ecf.core.provider.IContainerInstantiator;
20+
import org.eclipse.ecf.core.security.ECFSSLContextFactory;
21+
import org.eclipse.ecf.core.security.SSLContextFactory;
1922
import org.eclipse.ecf.core.start.ECFStartJob;
2023
import org.eclipse.ecf.core.start.IECFStart;
2124
import org.eclipse.ecf.core.util.*;
@@ -92,6 +95,10 @@ public class ECFPlugin implements BundleActivator {
9295

9396
private BundleActivator ecfTrustManager;
9497

98+
private ServiceRegistration sslContextFactoryRegistration;
99+
100+
private ECFSSLContextFactory ecfSSLContextFactory;
101+
95102
/**
96103
* Returns the shared instance.
97104
* @return ECFPlugin
@@ -221,6 +228,11 @@ public void ungetService(Bundle bundle, ServiceRegistration registration, Object
221228
containerFactoryServiceRegistration = ctxt.registerService(IContainerFactory.class.getName(), sf, null);
222229
containerManagerServiceRegistration = ctxt.registerService(IContainerManager.class.getName(), sf, null);
223230

231+
// Register SSLContextFactory
232+
SSLContext defaultContext = SSLContext.getDefault();
233+
ecfSSLContextFactory = new ECFSSLContextFactory(ctxt, defaultContext.getProtocol(), defaultContext.getProvider().getName());
234+
sslContextFactoryRegistration = ctxt.registerService(SSLContextFactory.class, ecfSSLContextFactory, null);
235+
224236
SafeRunner.run(new ExtensionRegistryRunnable(this.context) {
225237
protected void runWithRegistry(IExtensionRegistry registry) throws Exception {
226238
if (registry != null) {
@@ -317,6 +329,14 @@ protected void runWithRegistry(IExtensionRegistry registry) throws Exception {
317329
containerManagerServiceRegistration.unregister();
318330
containerManagerServiceRegistration = null;
319331
}
332+
if (sslContextFactoryRegistration != null) {
333+
sslContextFactoryRegistration.unregister();
334+
sslContextFactoryRegistration = null;
335+
if (ecfSSLContextFactory != null) {
336+
ecfSSLContextFactory.close();
337+
ecfSSLContextFactory = null;
338+
}
339+
}
320340
if (adapterManagerTracker != null) {
321341
adapterManagerTracker.close();
322342
adapterManagerTracker = null;

providers/bundles/org.eclipse.ecf.provider.filetransfer.httpclientjava/META-INF/MANIFEST.MF

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %plugin.name
44
Bundle-SymbolicName: org.eclipse.ecf.provider.filetransfer.httpclientjava;singleton:=true
5-
Bundle-Version: 2.0.300.qualifier
5+
Bundle-Version: 2.0.0.qualifier
66
Bundle-Vendor: %plugin.provider
77
Bundle-Localization: plugin
88
Automatic-Module-Name: org.eclipse.ecf.provider.filetransfer.httpclientjava
99
Bundle-RequiredExecutionEnvironment: JavaSE-17
1010
Require-Bundle: org.eclipse.equinox.common;bundle-version="3.13.0",
11-
org.eclipse.ecf;bundle-version="3.9.101",
11+
org.eclipse.ecf;bundle-version="3.12.0",
1212
org.eclipse.core.jobs;bundle-version="3.10.800"
1313
Import-Package: javax.net.ssl,
1414
org.eclipse.ecf.filetransfer;version="5.0.0",

providers/bundles/org.eclipse.ecf.provider.filetransfer.httpclientjava/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath>../../../</relativePath>
1010
</parent>
1111
<artifactId>org.eclipse.ecf.provider.filetransfer.httpclientjava</artifactId>
12-
<version>2.0.300-SNAPSHOT</version>
12+
<version>2.0.0-SNAPSHOT</version>
1313
<packaging>eclipse-plugin</packaging>
1414

1515
<build>

providers/bundles/org.eclipse.ecf.provider.filetransfer.httpclientjava/src/org/eclipse/ecf/internal/provider/filetransfer/httpclientjava/Activator.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
import java.util.List;
2828
import java.util.Map;
2929

30-
import javax.net.ssl.SSLSocketFactory;
31-
3230
import org.eclipse.core.runtime.IStatus;
3331
import org.eclipse.core.runtime.Status;
32+
import org.eclipse.ecf.core.security.SSLContextFactory;
3433
import org.eclipse.ecf.core.util.ECFRuntimeException;
3534
import org.eclipse.ecf.core.util.LogHelper;
3635
import org.eclipse.ecf.core.util.Trace;
@@ -118,7 +117,7 @@ public void removedService(ServiceReference<HttpClient> reference, HttpClient se
118117

119118
private ServiceTracker<LogService, LogService> logServiceTracker = null;
120119

121-
private ServiceTracker<SSLSocketFactory, SSLSocketFactory> sslSocketFactoryTracker;
120+
private ServiceTracker<SSLContextFactory, SSLContextFactory> sslContextFactoryTracker;
122121

123122
private ServiceTracker<INTLMProxyHandler, INTLMProxyHandler> ntlmProxyHandlerTracker;
124123

@@ -181,8 +180,8 @@ private void applyDebugOptions(BundleContext ctxt) {
181180

182181
@Override
183182
public synchronized void stop(BundleContext ctxt) throws Exception {
184-
if (sslSocketFactoryTracker != null) {
185-
sslSocketFactoryTracker.close();
183+
if (sslContextFactoryTracker != null) {
184+
sslContextFactoryTracker.close();
186185
}
187186

188187
if (logServiceTracker != null) {
@@ -247,12 +246,15 @@ public void log(IStatus status) {
247246
}
248247
}
249248

250-
public synchronized SSLSocketFactory getSSLSocketFactory() {
251-
if (sslSocketFactoryTracker == null) {
252-
sslSocketFactoryTracker = new ServiceTracker<SSLSocketFactory, SSLSocketFactory>(this.context, SSLSocketFactory.class, null);
253-
sslSocketFactoryTracker.open();
249+
/**
250+
* @since 2.0
251+
*/
252+
public synchronized SSLContextFactory getSSLContextFactory() {
253+
if (sslContextFactoryTracker == null) {
254+
sslContextFactoryTracker = new ServiceTracker<SSLContextFactory, SSLContextFactory>(this.context, SSLContextFactory.class, null);
255+
sslContextFactoryTracker.open();
254256
}
255-
SSLSocketFactory service = sslSocketFactoryTracker.getService();
257+
SSLContextFactory service = sslContextFactoryTracker.getService();
256258
return service;
257259
}
258260

providers/bundles/org.eclipse.ecf.provider.filetransfer.httpclientjava/src/org/eclipse/ecf/internal/provider/filetransfer/httpclientjava/ECFHttpClientFactory.java

+9
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
import java.net.http.HttpClient;
1818
import java.net.http.HttpClient.Redirect;
1919
import java.net.http.HttpRequest;
20+
import java.security.NoSuchAlgorithmException;
21+
import java.security.NoSuchProviderException;
2022
import java.time.Duration;
2123
import java.util.Arrays;
2224
import java.util.HashMap;
2325
import java.util.List;
2426
import java.util.Map;
2527

28+
import org.eclipse.core.runtime.IStatus;
29+
import org.eclipse.core.runtime.Status;
2630
import org.eclipse.ecf.core.util.Trace;
2731
import org.eclipse.ecf.internal.provider.filetransfer.DebugOptions;
2832
import org.eclipse.ecf.provider.filetransfer.httpclientjava.HttpClientOptions;
@@ -48,6 +52,11 @@ public class ECFHttpClientFactory implements IHttpClientFactory {
4852
public HttpClient.Builder newClient() {
4953

5054
HttpClient.Builder builder = HttpClient.newBuilder().followRedirects(Redirect.NORMAL);
55+
try {
56+
builder.sslContext(Activator.getDefault().getSSLContextFactory().getDefault());
57+
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
58+
Activator.getDefault().log(new Status(IStatus.ERROR,Activator.PLUGIN_ID,"Could not set SSLContext when creating jre HttpClient", e));
59+
}
5160
builder = Activator.getDefault().runModifiers(builder, new ModifierRunner<HttpClient.Builder>() {
5261
@Override
5362
public HttpClient.Builder run(IHttpClientModifier modifier, HttpClient.Builder value) {

releng/features/org.eclipse.ecf.xmpp.feature/feature.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<feature
33
id="org.eclipse.ecf.xmpp.feature"
44
label="ECF XMPP Provider"
5-
version="1.0.500.qualifier"
5+
version="1.0.600.qualifier"
66
provider-name="%providerName"
77
license-feature="org.eclipse.license"
88
license-feature-version="0.0.0">
@@ -45,6 +45,6 @@ https://wiki.eclipse.org/ECF#OSGi_Remote_Services
4545

4646
<plugin
4747
id="org.xbill.dns"
48-
version="3.6.1"/>
48+
version="0.0.0"/>
4949

5050
</feature>

releng/features/org.eclipse.ecf.xmpp.feature/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<relativePath>../../../</relativePath>
1010
</parent>
1111
<artifactId>org.eclipse.ecf.xmpp.feature</artifactId>
12-
<version>1.0.500-SNAPSHOT</version>
12+
<version>1.0.600-SNAPSHOT</version>
1313
<packaging>eclipse-feature</packaging>
1414
</project>

releng/org.eclipse.ecf.releng.target/ecf-2024-06.target

+11-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,22 @@
6363
<unit id="org.osgi.util.xml" version="0.0.0"/>
6464
<unit id="org.sat4j.pb" version="0.0.0"/>
6565
<unit id="org.tukaani.xz" version="0.0.0"/>
66-
<unit id="org.xbill.dns" version="0.0.0"/>
6766
<repository location="https://download.eclipse.org/tools/orbit/simrel/orbit-aggregation/milestone/latest"/>
6867
</location>
6968
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
7069
<unit id="org.eclipse.equinox.core.feature.feature.group" version="0.0.0"/>
7170
<unit id="org.eclipse.sdk.feature.group" version="0.0.0"/>
7271
<repository location="https://download.eclipse.org/eclipse/updates/4.32/R-4.32-202406010610"/>
7372
</location>
73+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven">
74+
<dependencies>
75+
<dependency>
76+
<groupId>dnsjava</groupId>
77+
<artifactId>dnsjava</artifactId>
78+
<version>3.6.2</version>
79+
<type>jar</type>
80+
</dependency>
81+
</dependencies>
82+
</location>
7483
</locations>
75-
</target>
84+
</target>

0 commit comments

Comments
 (0)