2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.platform.mod.util;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.cert.X509Certificate;
26 import javax.net.ssl.HttpsURLConnection;
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.TrustManager;
29 import javax.net.ssl.X509TrustManager;
31 public final class SSLUtils {
34 // for localhost testing only
35 HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
36 public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
42 private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[] { new X509TrustManager() {
43 public X509Certificate[] getAcceptedIssuers() {
47 public void checkClientTrusted(X509Certificate[] certs, String authType) {
50 public void checkServerTrusted(X509Certificate[] certs, String authType) {
54 public static void turnOffSslChecking() throws NoSuchAlgorithmException, KeyManagementException {
55 // Install the all-trusting trust manager
56 final SSLContext sc = SSLContext.getInstance("SSL");
57 sc.init(null, UNQUESTIONING_TRUST_MANAGER, null);
58 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
61 public static void turnOnSslChecking() throws KeyManagementException, NoSuchAlgorithmException {
62 // Return it to the initial state (discovered by reflection, now hardcoded)
63 SSLContext.getInstance("SSL").init(null, null, null);
67 throw new UnsupportedOperationException("Do not instantiate libraries.");