io.cloudslang.content.vmware.connection.impl.DisableSecurity Maven / Gradle / Ivy
/*
* (c) Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License v2.0 which accompany this distribution.
*
* The Apache License is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudslang.content.vmware.connection.impl;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
class DisableSecurity {
private static final String TLS = "TLSv1.2";
public static void trustEveryone() throws NoSuchAlgorithmException, KeyManagementException {
// Declare a host name verifier that will automatically enable the connection.
// The host name verifier is invoked during the SSL handshake.
HostnameVerifier verifier = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
// Create the trust manager.
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager trustManager = new TrustAllTrustManager();
trustAllCerts[0] = trustManager;
// Create the SSL context
SSLContext sc = SSLContext.getInstance(TLS);
// Create the session context
SSLSessionContext sslsc = sc.getServerSessionContext();
// Initialize the contexts; the session context takes the trust manager.
sslsc.setSessionTimeout(0);
sc.init(null, trustAllCerts, null);
// Use the default socket factory to create the socket for the secure connection
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Set the default host name verifier to enable the connection.
HttpsURLConnection.setDefaultHostnameVerifier(verifier);
}
/*
* Authentication is handled by using a TrustManager and supplying a hostname verifier method.
*/
private static class TrustAllTrustManager implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
}