First version of NonRT RIC Controlpanel
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / util / HttpsURLConnectionUtils.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.oransc.portal.nonrtric.controlpanel.util;
23
24 import java.security.KeyManagementException;
25 import java.security.NoSuchAlgorithmException;
26
27 import javax.net.ssl.HostnameVerifier;
28 import javax.net.ssl.HttpsURLConnection;
29 import javax.net.ssl.SSLContext;
30 import javax.net.ssl.TrustManager;
31
32 import org.apache.axis2.java.security.TrustAllTrustManager;
33
34 /**
35  * Disables and enables certificate and host-name checking in
36  * HttpsURLConnection, the default JVM implementation of the HTTPS/TLS protocol.
37  * Has no effect on implementations such as Apache Http Client, Ok Http.
38  *
39  * https://stackoverflow.com/questions/23504819/how-to-disable-ssl-certificate-checking-with-spring-resttemplate/58291331#58291331
40  */
41 public final class HttpsURLConnectionUtils {
42
43     private static final HostnameVerifier jvmHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
44
45     private static final HostnameVerifier trivialHostnameVerifier =
46         (hostname, sslSession) -> hostname.equalsIgnoreCase(sslSession.getPeerHost());
47
48     private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[] {new TrustAllTrustManager()};
49
50     public static void turnOffSslChecking() throws NoSuchAlgorithmException, KeyManagementException {
51         HttpsURLConnection.setDefaultHostnameVerifier(trivialHostnameVerifier);
52         // Install the all-trusting trust manager
53         SSLContext sc = SSLContext.getInstance("TLS");
54         sc.init(null, UNQUESTIONING_TRUST_MANAGER, null);
55         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
56     }
57
58     public static void turnOnSslChecking() throws KeyManagementException, NoSuchAlgorithmException {
59         HttpsURLConnection.setDefaultHostnameVerifier(jvmHostnameVerifier);
60         // Return it to the initial state (discovered by reflection, now hardcoded)
61         SSLContext sc = SSLContext.getInstance("TLS");
62         sc.init(null, null, null);
63         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
64     }
65
66     private HttpsURLConnectionUtils() {
67         throw new UnsupportedOperationException("Do not instantiate libraries.");
68     }
69 }