Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / main / java / org / onap / dcaegen2 / platform / mod / util / SSLUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.util;
22
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;
30
31 public final class SSLUtils {
32
33     static {
34         // for localhost testing only
35         HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
36             public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
37                 return true;
38             }
39         });
40     }
41
42     private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[] { new X509TrustManager() {
43         public X509Certificate[] getAcceptedIssuers() {
44             return null;
45         }
46
47         public void checkClientTrusted(X509Certificate[] certs, String authType) {
48         }
49
50         public void checkServerTrusted(X509Certificate[] certs, String authType) {
51         }
52     } };
53
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());
59     }
60
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);
64     }
65
66     private SSLUtils() {
67         throw new UnsupportedOperationException("Do not instantiate libraries.");
68     }
69 }