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