Enabling profile handling for subsciption
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / SecurityContextFactoryImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.sparky.security;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.security.KeyManagementException;
29 import java.security.KeyStore;
30 import java.security.KeyStoreException;
31 import java.security.NoSuchAlgorithmException;
32 import java.security.UnrecoverableKeyException;
33 import java.security.cert.CertificateException;
34 import java.security.cert.X509Certificate;
35
36 import javax.net.ssl.KeyManagerFactory;
37 import javax.net.ssl.SSLContext;
38 import javax.net.ssl.TrustManager;
39 import javax.net.ssl.X509TrustManager;
40
41 /**
42  * The Class SecurityContextFactoryImpl.
43  */
44 public class SecurityContextFactoryImpl implements SecurityContextFactory {
45
46   protected String sslAlgorithm;
47   protected String keyManagerAlgortihm;
48   protected String keyStoreType;
49   protected boolean serverCertificationChainValidationEnabled;
50   protected String trustStoreFileName;
51   protected String clientCertPassword;
52   protected FileInputStream clientCertFileInputStream;
53   protected String clientCertFileName;
54   protected byte[] clientCertBytes;
55
56   /**
57    * Instantiates a new security context factory impl.
58    */
59   public SecurityContextFactoryImpl() {
60     this.sslAlgorithm = "TLS";
61     this.keyManagerAlgortihm = "SunX509";
62     this.keyStoreType = "PKCS12";
63     this.serverCertificationChainValidationEnabled = false;
64     this.clientCertFileInputStream = null;
65     this.clientCertFileName = null;
66   }
67
68   @Override
69   public String getSslAlgorithm() {
70     return sslAlgorithm;
71   }
72
73   @Override
74   public void setSslAlgorithm(String sslAlgorithm) {
75     this.sslAlgorithm = sslAlgorithm;
76   }
77
78   @Override
79   public String getKeyManagerAlgortihm() {
80     return keyManagerAlgortihm;
81   }
82
83   @Override
84   public void setKeyManagerAlgortihm(String keyManagerAlgortihm) {
85     this.keyManagerAlgortihm = keyManagerAlgortihm;
86   }
87
88   @Override
89   public String getKeyStoreType() {
90     return keyStoreType;
91   }
92
93   @Override
94   public void setKeyStoreType(String keyStoreType) {
95     this.keyStoreType = keyStoreType;
96   }
97
98   @Override
99   public boolean isServerCertificationChainValidationEnabled() {
100     return serverCertificationChainValidationEnabled;
101   }
102
103   @Override
104   public void setServerCertificationChainValidationEnabled(
105       boolean serverCertificationChainValidationEnabled) {
106     this.serverCertificationChainValidationEnabled = serverCertificationChainValidationEnabled;
107   }
108
109   @Override
110   public void setClientCertFileName(String filename) throws IOException {
111     this.clientCertFileName = filename;
112     
113     if (filename == null) {
114       this.clientCertBytes = null;
115     } else {
116       this.clientCertBytes = Files.readAllBytes(new File(filename).toPath());
117     }
118   }
119
120   @Override
121   public void setClientCertFileInputStream(FileInputStream fis) {
122     this.clientCertFileInputStream = fis;
123   }
124
125   @Override
126   public FileInputStream getClientCertFileInputStream() {
127     return this.clientCertFileInputStream;
128   }
129
130   @Override
131   public SSLContext getSecureContext() throws KeyManagementException, NoSuchAlgorithmException,
132       KeyStoreException, CertificateException, IOException, UnrecoverableKeyException {
133
134     TrustManager[] trustAllCerts = null;
135
136     if (serverCertificationChainValidationEnabled) {
137
138       System.setProperty("javax.net.ssl.trustStore", trustStoreFileName);
139
140     } else {
141
142       // Create a trust manager that does not validate certificate chains
143       trustAllCerts = new TrustManager[] {new X509TrustManager() {
144         @Override
145         public X509Certificate[] getAcceptedIssuers() {
146           return null;
147         }
148
149         @Override
150         public void checkClientTrusted(X509Certificate[] certs, String authType) {}
151
152         @Override
153         public void checkServerTrusted(X509Certificate[] certs, String authType) {}
154       } };
155     }
156
157     KeyManagerFactory kmf = KeyManagerFactory.getInstance(keyManagerAlgortihm);
158
159     KeyStore ks = KeyStore.getInstance(keyStoreType);
160
161     char[] pwd = null;
162     if (clientCertPassword != null) {
163       pwd = clientCertPassword.toCharArray();
164     }
165     
166     if (clientCertBytes != null) {
167       ks.load(new ByteArrayInputStream(clientCertBytes), pwd);
168     } else {
169       ks.load(null, pwd);
170     }
171
172     kmf.init(ks, pwd);
173
174     SSLContext ctx = SSLContext.getInstance(sslAlgorithm);
175     ctx.init(kmf.getKeyManagers(), trustAllCerts, null);
176
177     return ctx;
178
179   }
180
181   @Override
182   public String getTrustStoreFileName() {
183     return this.trustStoreFileName;
184   }
185
186   @Override
187   public void setTrustStoreFileName(String filename) {
188     this.trustStoreFileName = filename;
189   }
190
191   @Override
192   public String getClientCertPassword() {
193     return this.clientCertPassword;
194   }
195
196   @Override
197   public void setClientCertPassword(String password) {
198     this.clientCertPassword = password;
199   }
200
201 }