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;
42 * @deprecated org.onap.dcaegen2.services.sdk.security.ssl.SslFactory should be used instead
45 public class SslFactory {
47 private static final Logger LOGGER = LoggerFactory.getLogger(SslFactory.class);
50 * Function for creating secure ssl context.
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
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);
64 return SslContextBuilder
66 .keyManager(keyManagerFactory(keyStorePath, loadPasswordFromFile(keyStorePasswordPath)))
67 .trustManager(trustManagerFactory(trustStorePath, loadPasswordFromFile(trustStorePasswordPath)))
69 } catch (GeneralSecurityException | IOException ex) {
70 throw new SSLException(ex);
75 * Function for creating insecure ssl context.
77 * @return configured insecure ssl context
79 public SslContext createInsecureContext() throws SSLException {
80 LOGGER.info("Creating insecure ssl context");
81 return SslContextBuilder
83 .trustManager(InsecureTrustManagerFactory.INSTANCE)
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());
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));
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());
109 private InputStream getResource(String path) throws FileNotFoundException {
110 return new FileInputStream(path);
113 private String loadPasswordFromFile(String path) throws IOException {
114 return new String(Files.readAllBytes(Paths.get(path)));