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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.ssl;
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;
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;
41 public class SslFactory {
43 private static final Logger LOGGER = LoggerFactory.getLogger(SslFactory.class);
46 * Function for creating secure ssl context.
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
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);
60 return SslContextBuilder
62 .keyManager(keyManagerFactory(keyStorePath, loadPasswordFromFile(keyStorePasswordPath)))
63 .trustManager(trustManagerFactory(trustStorePath, loadPasswordFromFile(trustStorePasswordPath)))
65 } catch (GeneralSecurityException | IOException ex) {
66 throw new SSLException(ex);
71 * Function for creating insecure ssl context.
73 * @return configured insecure ssl context
75 public SslContext createInsecureContext() throws SSLException {
76 LOGGER.info("Creating insecure ssl context");
77 return SslContextBuilder
79 .trustManager(InsecureTrustManagerFactory.INSTANCE)
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());
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));
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());
105 private InputStream getResource(String path) throws FileNotFoundException {
106 return new FileInputStream(path);
109 private String loadPasswordFromFile(String path) throws IOException {
110 return new String(Files.readAllBytes(Paths.get(path)));