4 package org.opendaylight.mwtn.dcaeConnector.test;
6 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.net.MalformedURLException;
12 import java.security.KeyManagementException;
13 import java.security.NoSuchAlgorithmException;
14 import java.security.SecureRandom;
15 import java.security.cert.Certificate;
16 import java.security.cert.X509Certificate;
18 import javax.net.ssl.HttpsURLConnection;
19 import javax.net.ssl.SSLContext;
20 import javax.net.ssl.SSLPeerUnverifiedException;
21 import javax.net.ssl.SSLSocketFactory;
22 import javax.net.ssl.TrustManager;
23 import javax.net.ssl.X509TrustManager;
25 import org.opendaylight.mwtn.dcaeConnector.impl.DcaeProviderClient;
31 public class HttpsClient{
33 private static final MyLogger LOG = MyLogger.getLogger(DcaeProviderClient.class);
37 TrustManager tm = new X509TrustManager() {
40 public void checkClientTrusted(X509Certificate[] chain, String authType)
41 throws java.security.cert.CertificateException {
42 //do nothing, you're the client
46 public void checkServerTrusted(X509Certificate[] chain, String authType)
47 throws java.security.cert.CertificateException {
48 /* chain[chain.length -1] is the candidate for the
50 * Look it up to see whether it's in your list.
51 * If not, ask the user for permission to add it.
52 * If not granted, reject.
53 * Validate the chain using CertPathValidator and
54 * your list of trusted roots.
59 public X509Certificate[] getAcceptedIssuers() {
60 //also only relevant for servers
65 TrustManager tml[] = new TrustManager[1];
70 SSLContext ctx = SSLContext.getInstance("TLS");
71 ctx.init(null, tml, null);
72 @SuppressWarnings("unused")
73 SSLSocketFactory sslF = ctx.getSocketFactory();
75 } catch (NoSuchAlgorithmException | KeyManagementException e) {
82 void setupAllTrustingManager() {
83 // Create a trust manager that does not validate certificate chains
84 TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
86 public X509Certificate[] getAcceptedIssuers(){return null;}
88 public void checkClientTrusted(X509Certificate[] certs, String authType){}
90 public void checkServerTrusted(X509Certificate[] certs, String authType){}
93 // Install the all-trusting trust manager
95 SSLContext sc = SSLContext.getInstance("TLS");
96 sc.init(null, trustAllCerts, new SecureRandom());
97 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
98 } catch (Exception e) {
103 void testIt(String https_url, String keyStoreName, String keyStorePassword){
105 LOG.info("Message to: {} begin.", https_url);
107 if (https_url.equals("off")) {
108 LOG.info("Function switched off");
113 KeyManagerFactory keyManagerFactory = null;
116 KeyStore ks = KeyStore.getInstance("JKS");
117 FileInputStream in = new FileInputStream(keyStoreName);
118 ks.load(in, keyStorePassword.toCharArray());
120 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
121 FileInputStream in2 = new FileInputStream("etc/eventprovider.cert");
122 X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in2);
124 KeyStore.Entry newEntry = new KeyStore.TrustedCertificateEntry(cert);
125 ks.setEntry("someAlias", newEntry, null);
127 keyManagerFactory = KeyManagerFactory.getInstance("X509");
128 keyManagerFactory.init(ks, "yourKeyStorePassword".toCharArray());
130 } catch (KeyStoreException e1) {
131 LOG.info("Exception: {}", e1.getMessage());
132 } catch (FileNotFoundException e1) {
133 LOG.info("Exception: {}", e1.getMessage());
134 } catch (NoSuchAlgorithmException e1) {
135 LOG.info("Exception: {}", e1.getMessage());
136 } catch (CertificateException e1) {
137 LOG.info("Exception: {}", e1.getMessage());
138 } catch (IOException e1) {
139 LOG.info("Exception: {}", e1.getMessage());
140 } catch (UnrecoverableKeyException e1) {
141 LOG.info("Exception: {}", e1.getMessage());
144 // Create a trust manager that does not validate certificate chains
145 TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
147 public X509Certificate[] getAcceptedIssuers(){return null;}
149 public void checkClientTrusted(X509Certificate[] certs, String authType){}
151 public void checkServerTrusted(X509Certificate[] certs, String authType){}
154 File file = new File(keyStoreName);
155 LOG.info("Setup keystore begin "+keyStoreName+" "+keyStorePassword+" Exists: "+file.exists());
157 System.setProperty("javax.net.debug","ssl");
158 System.setProperty("javax.net.ssl.keyStoreType", "jks");
159 System.setProperty("javax.net.ssl.keyStore", keyStoreName);
160 System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
162 LOG.info("Setup keystore complete");
164 javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
165 (hostname, sslSession) -> {
166 LOG.info("Hostname check {}", hostname);
169 LOG.info("Setup name verifier.");
173 SSLContext sslContext = SSLContext.getInstance("TLS");
174 sslContext.init(keyManagerFactory.getKeyManagers(), trustAllCerts, null);
175 SSLContext.setDefault(sslContext);
178 URL url = new URL(https_url);
179 LOG.info("Url object created");
181 HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
183 LOG.info("openConnection");
185 //dumpl all cert info
186 print_https_cert(con);
188 //dump all the content
191 } catch (MalformedURLException e) {
192 LOG.info("Exception: {}", e.getMessage());
193 } catch (IOException e) {
194 LOG.info("Exception: {}", e.getMessage());
197 LOG.info("Message to: {} end.", https_url);
201 private void print_https_cert(HttpsURLConnection con){
203 StringBuffer logMsg = new StringBuffer();
208 logMsg.append("Response Code : " + con.getResponseCode());
209 logMsg.append("Cipher Suite : " + con.getCipherSuite());
212 Certificate[] certs = con.getServerCertificates();
213 for(Certificate cert : certs){
214 logMsg.append("Cert Type : " + cert.getType());
215 logMsg.append("Cert Hash Code : " + cert.hashCode());
216 logMsg.append("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
217 logMsg.append("Cert Public Key Format : " + cert.getPublicKey().getFormat());
222 } catch (SSLPeerUnverifiedException e) {
223 logMsg.append(e.getMessage());
224 } catch (IOException e){
225 logMsg.append(e.getMessage());
228 logMsg.append("No connection");
231 LOG.info(logMsg.toString());
234 private void print_content(HttpsURLConnection con){
236 StringBuffer logMsg = new StringBuffer();
242 logMsg.append("****** Content of the URL ********");
245 new InputStreamReader(con.getInputStream()));
249 while ((input = br.readLine()) != null){
250 logMsg.append(input);
255 } catch (IOException e) {
256 logMsg.append(e.getMessage());
260 logMsg.append("No connection");
263 LOG.info(logMsg.toString());
267 private static class MyLogger {
269 private void out( String s, Object...oList) {
270 StringBuffer sb = new StringBuffer();
271 sb.append("-------> ");
275 for (Object o: oList) {
279 sb.append(o.toString());
282 System.out.println(sb.toString());
285 void info( String s, Object...o) {
289 static MyLogger getLogger(Class<?> c) {
290 return new MyLogger();