Test framework for sms client code.
[aaf/sms.git] / sms-client / src / java / test / SmsSecureSocket.java
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.aaf.sms.test;
18
19 import java.io.FileInputStream;
20 import javax.net.ssl.KeyManagerFactory;
21 import javax.net.ssl.SSLContext;
22 import javax.net.ssl.SSLSessionContext;
23 import javax.net.ssl.SSLSocketFactory;
24 import javax.net.ssl.TrustManagerFactory;
25 import java.security.KeyStore;
26 import java.security.Provider;
27 import java.security.SecureRandom;
28 import java.security.Security;
29
30 public class SmsSecureSocket {
31     private SSLSocketFactory ssf = null;
32     public SmsSecureSocket() throws Exception {
33         // Set up the Sun PKCS 11 provider
34         Provider p = Security.getProvider("SunPKCS11-pkcs11Test");
35         if (p==null) {
36             throw new RuntimeException("could not get security provider");
37         }
38
39         // Load the key store
40         char[] pin = "123456789".toCharArray();
41         KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
42         keyStore.load(null, pin);
43
44         // Load the CA certificate
45         FileInputStream tst = new FileInputStream("/ca.jks");
46         KeyStore trustStore = KeyStore.getInstance("JKS");
47         trustStore.load(tst, pin);
48
49         KeyManagerFactory keyManagerFactory =
50              KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
51         //Add to keystore to key manager
52         keyManagerFactory.init(keyStore, pin);
53
54         TrustManagerFactory trustManagerFactory =
55              TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
56         trustManagerFactory.init(trustStore);
57
58         //Create the context
59         SSLContext context = SSLContext.getInstance("TLS");
60         context.init(keyManagerFactory.getKeyManagers(),
61              trustManagerFactory.getTrustManagers(), new SecureRandom());
62         //Create a socket factory
63         SSLSocketFactory ssf = context.getSocketFactory();
64     }
65     public SSLSocketFactory getSSF() {
66         return(ssf);
67     }
68 }