2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. 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.services.hvves.client.producer.impl;
23 import io.netty.handler.ssl.SslContext;
24 import io.netty.handler.ssl.SslContextBuilder;
26 import io.vavr.control.Try;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.StandardOpenOption;
31 import java.security.GeneralSecurityException;
32 import java.security.KeyStore;
33 import javax.net.ssl.KeyManagerFactory;
34 import javax.net.ssl.TrustManagerFactory;
35 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.Password;
36 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.SecurityKeys;
39 * TODO: To be merged with org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory
41 public class SslFactory {
44 * Function for creating secure ssl context.
46 * @param keys - Security keys to be used
47 * @return configured SSL context
49 public Try<SslContext> createSecureContext(final SecurityKeys keys) {
50 final Try<KeyManagerFactory> keyManagerFactory =
51 keyManagerFactory(keys.keyStore(), keys.keyStorePassword());
52 final Try<TrustManagerFactory> trustManagerFactory =
53 trustManagerFactory(keys.trustStore(), keys.trustStorePassword());
55 return Try.success(SslContextBuilder.forClient())
56 .flatMap(ctx -> keyManagerFactory.map(ctx::keyManager))
57 .flatMap(ctx -> trustManagerFactory.map(ctx::trustManager))
58 .mapTry(SslContextBuilder::build);
61 private Try<KeyManagerFactory> keyManagerFactory(Path path, Password password) {
62 return password.useChecked(passwordChars -> {
63 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
64 kmf.init(loadKeyStoreFromFile(path, passwordChars), passwordChars);
69 private Try<TrustManagerFactory> trustManagerFactory(Path path, Password password) {
70 return password.useChecked(passwordChars -> {
71 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
72 tmf.init(loadKeyStoreFromFile(path, passwordChars));
77 private KeyStore loadKeyStoreFromFile(Path path, char[] keyStorePassword)
78 throws GeneralSecurityException, IOException {
79 KeyStore ks = KeyStore.getInstance("pkcs12");
80 ks.load(Files.newInputStream(path, StandardOpenOption.READ), keyStorePassword);