cce811c54242398a441c6d5f4b9f1d0258dfa79e
[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 public class SslFactory {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(SslFactory.class);
44
45     /**
46      * Function for creating secure ssl context.
47      *
48      * @param keyStorePath - path to file with keystore
49      * @param keyStorePasswordPath - path to file with keystore password
50      * @param trustStorePath - path to file with truststore
51      * @param trustStorePasswordPath - path to file with truststore password
52      * @return configured ssl context
53      */
54     public SslContext createSecureContext(String keyStorePath,
55         String keyStorePasswordPath,
56         String trustStorePath,
57         String trustStorePasswordPath) throws SSLException {
58         LOGGER.info("Creating secure ssl context for: {} {}", keyStorePath, trustStorePath);
59         try {
60             return SslContextBuilder
61                 .forClient()
62                 .keyManager(keyManagerFactory(keyStorePath, loadPasswordFromFile(keyStorePasswordPath)))
63                 .trustManager(trustManagerFactory(trustStorePath, loadPasswordFromFile(trustStorePasswordPath)))
64                 .build();
65         } catch (GeneralSecurityException | IOException ex) {
66             throw new SSLException(ex);
67         }
68     }
69
70     /**
71      * Function for creating insecure ssl context.
72      *
73      * @return configured insecure ssl context
74      */
75     public SslContext createInsecureContext() throws SSLException {
76         LOGGER.info("Creating insecure ssl context");
77         return SslContextBuilder
78             .forClient()
79             .trustManager(InsecureTrustManagerFactory.INSTANCE)
80             .build();
81     }
82
83     private KeyManagerFactory keyManagerFactory(String path, String password)
84             throws GeneralSecurityException, IOException {
85         KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
86         kmf.init(loadKeyStoreFromFile(path, password),
87             password.toCharArray());
88         return kmf;
89     }
90
91     private TrustManagerFactory trustManagerFactory(String path, String password)
92             throws GeneralSecurityException, IOException {
93         TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
94         tmf.init(loadKeyStoreFromFile(path, password));
95         return tmf;
96     }
97
98     private KeyStore loadKeyStoreFromFile(String path, String keyStorePassword)
99             throws GeneralSecurityException, IOException {
100         KeyStore ks = KeyStore.getInstance("jks");
101         ks.load(getResource(path), keyStorePassword.toCharArray());
102         return ks;
103     }
104
105     private InputStream getResource(String path) throws FileNotFoundException {
106         return new FileInputStream(path);
107     }
108
109     private String loadPasswordFromFile(String path) throws IOException {
110         return new String(Files.readAllBytes(Paths.get(path)));
111     }
112 }