92de6608b6632f982a7bcb075a521666f43b78a3
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.ssl;
22
23 import io.netty.handler.ssl.SslContext;
24 import io.netty.handler.ssl.SslContextBuilder;
25 import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import javax.net.ssl.KeyManagerFactory;
30 import javax.net.ssl.SSLException;
31 import javax.net.ssl.TrustManagerFactory;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.security.GeneralSecurityException;
39 import java.security.KeyStore;
40
41 /**
42  * @deprecated org.onap.dcaegen2.services.sdk.security.ssl.SslFactory should be used instead
43  */
44 @Deprecated
45 public class SslFactory {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(SslFactory.class);
48
49     /**
50      * Function for creating secure ssl context.
51      *
52      * @param keyStorePath - path to file with keystore
53      * @param keyStorePasswordPath - path to file with keystore password
54      * @param trustStorePath - path to file with truststore
55      * @param trustStorePasswordPath - path to file with truststore password
56      * @return configured ssl context
57      */
58     public SslContext createSecureContext(String keyStorePath,
59         String keyStorePasswordPath,
60         String trustStorePath,
61         String trustStorePasswordPath) throws SSLException {
62         LOGGER.info("Creating secure ssl context for: {} {}", keyStorePath, trustStorePath);
63         try {
64             return SslContextBuilder
65                 .forClient()
66                 .keyManager(keyManagerFactory(keyStorePath, loadPasswordFromFile(keyStorePasswordPath)))
67                 .trustManager(trustManagerFactory(trustStorePath, loadPasswordFromFile(trustStorePasswordPath)))
68                 .build();
69         } catch (GeneralSecurityException | IOException ex) {
70             throw new SSLException(ex);
71         }
72     }
73
74     /**
75      * Function for creating insecure ssl context.
76      *
77      * @return configured insecure ssl context
78      */
79     public SslContext createInsecureContext() throws SSLException {
80         LOGGER.info("Creating insecure ssl context");
81         return SslContextBuilder
82             .forClient()
83             .trustManager(InsecureTrustManagerFactory.INSTANCE)
84             .build();
85     }
86
87     private KeyManagerFactory keyManagerFactory(String path, String password)
88             throws GeneralSecurityException, IOException {
89         KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
90         kmf.init(loadKeyStoreFromFile(path, password),
91             password.toCharArray());
92         return kmf;
93     }
94
95     private TrustManagerFactory trustManagerFactory(String path, String password)
96             throws GeneralSecurityException, IOException {
97         TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
98         tmf.init(loadKeyStoreFromFile(path, password));
99         return tmf;
100     }
101
102     private KeyStore loadKeyStoreFromFile(String path, String keyStorePassword)
103             throws GeneralSecurityException, IOException {
104         KeyStore ks = KeyStore.getInstance("jks");
105         ks.load(getResource(path), keyStorePassword.toCharArray());
106         return ks;
107     }
108
109     private InputStream getResource(String path) throws FileNotFoundException {
110         return new FileInputStream(path);
111     }
112
113     private String loadPasswordFromFile(String path) throws IOException {
114         return new String(Files.readAllBytes(Paths.get(path)));
115     }
116 }