1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk feature sdnr wt
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 package org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeConnector.test;
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.net.MalformedURLException;
29 import java.security.KeyManagementException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.SecureRandom;
32 import java.security.cert.Certificate;
33 import java.security.cert.X509Certificate;
34 import javax.net.ssl.HttpsURLConnection;
35 import javax.net.ssl.SSLContext;
36 import javax.net.ssl.SSLPeerUnverifiedException;
37 import javax.net.ssl.SSLSocketFactory;
38 import javax.net.ssl.TrustManager;
39 import javax.net.ssl.X509TrustManager;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeProviderClient;
42 public class HttpsClient{
44 private static final MyLogger LOG = MyLogger.getLogger(DcaeProviderClient.class);
48 TrustManager tm = new X509TrustManager() {
51 public void checkClientTrusted(X509Certificate[] chain, String authType)
52 throws java.security.cert.CertificateException {
53 //do nothing, you're the client
57 public void checkServerTrusted(X509Certificate[] chain, String authType)
58 throws java.security.cert.CertificateException {
59 /* chain[chain.length -1] is the candidate for the
61 * Look it up to see whether it's in your list.
62 * If not, ask the user for permission to add it.
63 * If not granted, reject.
64 * Validate the chain using CertPathValidator and
65 * your list of trusted roots.
70 public X509Certificate[] getAcceptedIssuers() {
71 //also only relevant for servers
76 TrustManager tml[] = new TrustManager[1];
81 SSLContext ctx = SSLContext.getInstance("TLS");
82 ctx.init(null, tml, null);
83 @SuppressWarnings("unused")
84 SSLSocketFactory sslF = ctx.getSocketFactory();
86 } catch (NoSuchAlgorithmException | KeyManagementException e) {
93 void setupAllTrustingManager() {
94 // Create a trust manager that does not validate certificate chains
95 TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
97 public X509Certificate[] getAcceptedIssuers(){return null;}
99 public void checkClientTrusted(X509Certificate[] certs, String authType){}
101 public void checkServerTrusted(X509Certificate[] certs, String authType){}
104 // Install the all-trusting trust manager
106 SSLContext sc = SSLContext.getInstance("TLS");
107 sc.init(null, trustAllCerts, new SecureRandom());
108 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
109 } catch (Exception e) {
114 void testIt(String https_url, String keyStoreName, String keyStorePassword){
116 LOG.info("Message to: {} begin.", https_url);
118 if (https_url.equals("off")) {
119 LOG.info("Function switched off");
124 KeyManagerFactory keyManagerFactory = null;
127 KeyStore ks = KeyStore.getInstance("JKS");
128 FileInputStream in = new FileInputStream(keyStoreName);
129 ks.load(in, keyStorePassword.toCharArray());
131 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
132 FileInputStream in2 = new FileInputStream("etc/eventprovider.cert");
133 X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in2);
135 KeyStore.Entry newEntry = new KeyStore.TrustedCertificateEntry(cert);
136 ks.setEntry("someAlias", newEntry, null);
138 keyManagerFactory = KeyManagerFactory.getInstance("X509");
139 keyManagerFactory.init(ks, "yourKeyStorePassword".toCharArray());
141 } catch (KeyStoreException e1) {
142 LOG.info("Exception: {}", e1.getMessage());
143 } catch (FileNotFoundException e1) {
144 LOG.info("Exception: {}", e1.getMessage());
145 } catch (NoSuchAlgorithmException e1) {
146 LOG.info("Exception: {}", e1.getMessage());
147 } catch (CertificateException e1) {
148 LOG.info("Exception: {}", e1.getMessage());
149 } catch (IOException e1) {
150 LOG.info("Exception: {}", e1.getMessage());
151 } catch (UnrecoverableKeyException e1) {
152 LOG.info("Exception: {}", e1.getMessage());
155 // Create a trust manager that does not validate certificate chains
156 TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
158 public X509Certificate[] getAcceptedIssuers(){return null;}
160 public void checkClientTrusted(X509Certificate[] certs, String authType){}
162 public void checkServerTrusted(X509Certificate[] certs, String authType){}
165 File file = new File(keyStoreName);
166 LOG.info("Setup keystore begin "+keyStoreName+" "+keyStorePassword+" Exists: "+file.exists());
168 System.setProperty("javax.net.debug","ssl");
169 System.setProperty("javax.net.ssl.keyStoreType", "jks");
170 System.setProperty("javax.net.ssl.keyStore", keyStoreName);
171 System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
173 LOG.info("Setup keystore complete");
175 javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
176 (hostname, sslSession) -> {
177 LOG.info("Hostname check {}", hostname);
180 LOG.info("Setup name verifier.");
184 SSLContext sslContext = SSLContext.getInstance("TLS");
185 sslContext.init(keyManagerFactory.getKeyManagers(), trustAllCerts, null);
186 SSLContext.setDefault(sslContext);
189 URL url = new URL(https_url);
190 LOG.info("Url object created");
192 HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
194 LOG.info("openConnection");
196 //dumpl all cert info
197 print_https_cert(con);
199 //dump all the content
202 } catch (MalformedURLException e) {
203 LOG.info("Exception: {}", e.getMessage());
204 } catch (IOException e) {
205 LOG.info("Exception: {}", e.getMessage());
208 LOG.info("Message to: {} end.", https_url);
212 private void print_https_cert(HttpsURLConnection con){
214 StringBuffer logMsg = new StringBuffer();
219 logMsg.append("Response Code : " + con.getResponseCode());
220 logMsg.append("Cipher Suite : " + con.getCipherSuite());
223 Certificate[] certs = con.getServerCertificates();
224 for(Certificate cert : certs){
225 logMsg.append("Cert Type : " + cert.getType());
226 logMsg.append("Cert Hash Code : " + cert.hashCode());
227 logMsg.append("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
228 logMsg.append("Cert Public Key Format : " + cert.getPublicKey().getFormat());
233 } catch (SSLPeerUnverifiedException e) {
234 logMsg.append(e.getMessage());
235 } catch (IOException e){
236 logMsg.append(e.getMessage());
239 logMsg.append("No connection");
242 LOG.info(logMsg.toString());
245 private void print_content(HttpsURLConnection con){
247 StringBuffer logMsg = new StringBuffer();
253 logMsg.append("****** Content of the URL ********");
256 new InputStreamReader(con.getInputStream()));
260 while ((input = br.readLine()) != null){
261 logMsg.append(input);
266 } catch (IOException e) {
267 logMsg.append(e.getMessage());
271 logMsg.append("No connection");
274 LOG.info(logMsg.toString());
278 private static class MyLogger {
280 private void out( String s, Object...oList) {
281 StringBuffer sb = new StringBuffer();
282 sb.append("-------> ");
286 for (Object o: oList) {
290 sb.append(o.toString());
293 System.out.println(sb.toString());
296 void info( String s, Object...o) {
300 static MyLogger getLogger(Class<?> c) {
301 return new MyLogger();