42f022b576c026014b85949f2ebcb12e6dfcbf6b
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / connect / AbstractSslContext.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect;
18
19 import java.io.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.security.GeneralSecurityException;
25 import java.security.KeyStore;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.SecureRandom;
28 import java.security.cert.X509Certificate;
29
30 import javax.net.ssl.KeyManager;
31 import javax.net.ssl.KeyManagerFactory;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManager;
34 import javax.net.ssl.TrustManagerFactory;
35 import javax.net.ssl.X509TrustManager;
36
37 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import net.sf.json.JSONObject;
42
43 /**
44  * SSL context
45  * .</br>
46  *
47  * @author
48  * @version VFC 1.0 Sep 14, 2016
49  */
50 public class AbstractSslContext {
51
52     private static final Logger LOG = LoggerFactory.getLogger(AbstractSslContext.class);
53
54     protected AbstractSslContext() {
55         // constructor
56     }
57
58     private static SSLContext getSSLContext() throws NoSuchAlgorithmException {
59         return SSLContext.getInstance("TLSv1.2");
60     }
61
62     protected static SSLContext getAnonymousSSLContext() throws GeneralSecurityException {
63         SSLContext sslContext = getSSLContext();
64         sslContext.init(null, new TrustManager[] {new TrustAnyTrustManager()}, new SecureRandom());
65         return sslContext;
66     }
67
68     protected static SSLContext getCertificateSSLContext() throws GeneralSecurityException {
69         SSLContext sslContext = getSSLContext();
70         JSONObject sslConf = null;
71         try {
72             sslConf = readSSLConfToJson();
73         } catch(Exception e) {
74             LOG.error("readSSLConfToJson error", e);
75         }
76         sslContext.init(createKeyManager(sslConf), createTrustManager(sslConf), new SecureRandom());
77         return sslContext;
78     }
79
80     protected static KeyManager[] createKeyManager(JSONObject sslConf) {
81         KeyManager[] kms = null;
82         try {
83             String CERT_STORE = "etc/conf/server.p12";
84             String CERT_STORE_PASSWORD = "Changeme_123";
85             String KEY_STORE_TYPE = "PKCS12";
86             if(sslConf != null) {
87                 CERT_STORE = sslConf.getString("keyStore");
88                 CERT_STORE_PASSWORD = sslConf.getString("keyStorePass");
89                 KEY_STORE_TYPE = sslConf.getString("keyStoreType");
90             }
91             // load jks file
92             FileInputStream f_certStore = new FileInputStream(CERT_STORE);
93             KeyStore ks = KeyStore.getInstance(KEY_STORE_TYPE);
94             ks.load(f_certStore, CERT_STORE_PASSWORD.toCharArray());
95             f_certStore.close();
96
97             // init and create
98             String alg = KeyManagerFactory.getDefaultAlgorithm();
99             KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg);
100             kmFact.init(ks, CERT_STORE_PASSWORD.toCharArray());
101
102             kms = kmFact.getKeyManagers();
103         } catch(Exception e) {
104             LOG.error("create KeyManager fail!", e);
105         }
106         return kms;
107     }
108
109     protected static TrustManager[] createTrustManager(JSONObject sslConf) {
110         TrustManager[] tms = null;
111         try {
112
113             String TRUST_STORE = "etc/conf/trust.jks";
114             String TRUST_STORE_PASSWORD = "Changeme_123";
115             String TRUST_STORE_TYPE = "jks";
116             if(sslConf != null) {
117                 TRUST_STORE = sslConf.getString("trustStore");
118                 TRUST_STORE_PASSWORD = sslConf.getString("trustStorePass");
119                 TRUST_STORE_TYPE = sslConf.getString("trustStoreType");
120             }
121             FileInputStream f_trustStore = new FileInputStream(TRUST_STORE);
122             KeyStore ks = KeyStore.getInstance(TRUST_STORE_TYPE);
123             ks.load(f_trustStore, TRUST_STORE_PASSWORD.toCharArray());
124             f_trustStore.close();
125
126             String alg = TrustManagerFactory.getDefaultAlgorithm();
127             TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
128             tmFact.init(ks);
129             tms = tmFact.getTrustManagers();
130
131         } catch(Exception e) {
132             LOG.error("create TrustManager fail!", e);
133         }
134         return tms;
135     }
136
137     /**
138      * readSSLConfToJson
139      * 
140      * @return
141      * @throws IOException
142      * @since VFC 1.0
143      */
144     public static JSONObject readSSLConfToJson() throws IOException {
145         JSONObject sslJson = null;
146         InputStream ins = null;
147         BufferedInputStream bins = null;
148         String fileContent = "";
149
150         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
151                 + "etc" + System.getProperty("file.separator") + "conf" + System.getProperty("file.separator")
152                 + "sslconf.json";
153
154         try {
155             ins = new FileInputStream(fileName);
156             bins = new BufferedInputStream(ins);
157
158             byte[] contentByte = new byte[ins.available()];
159             int num = bins.read(contentByte);
160
161             if(num > 0) {
162                 fileContent = new String(contentByte);
163             }
164             sslJson = JSONObject.fromObject(fileContent);
165         } catch(FileNotFoundException e) {
166             LOG.error(fileName + "is not found!", e);
167         } catch(Exception e) {
168             LOG.error("read sslconf file fail.please check if the 'sslconf.json' is exist.");
169         } finally {
170             if(ins != null) {
171                 ins.close();
172             }
173             if(bins != null) {
174                 bins.close();
175             }
176         }
177
178         return sslJson;
179     }
180
181     private static class TrustAnyTrustManager implements X509TrustManager {
182
183         @Override
184         public X509Certificate[] getAcceptedIssuers() {
185             return new X509Certificate[] {};
186         }
187
188         @Override
189         public void checkServerTrusted(X509Certificate[] certs, String authType) {
190             // NOSONAR
191         }
192
193         @Override
194         public void checkClientTrusted(X509Certificate[] certs, String authType) {
195             // NOSONAR
196         }
197     }
198 }