0d98fa0bb043c5b957be8b79a83c868fd0ad6019
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / impl / DefaultSslHandlerFactoryProvider.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.client.mdsal.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.IOException;
13 import java.security.GeneralSecurityException;
14 import java.security.KeyStore;
15 import java.security.KeyStoreException;
16 import java.security.cert.X509Certificate;
17 import java.util.Set;
18 import javax.annotation.PreDestroy;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.netconf.client.SslHandlerFactory;
23 import org.opendaylight.netconf.client.mdsal.api.SslHandlerFactoryProvider;
24 import org.opendaylight.netconf.keystore.legacy.NetconfKeystore;
25 import org.opendaylight.netconf.keystore.legacy.NetconfKeystoreService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.connection.parameters.protocol.Specification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.connection.parameters.protocol.specification.TlsCase;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Deactivate;
32 import org.osgi.service.component.annotations.Reference;
33
34 @Singleton
35 @Component(service = SslHandlerFactoryProvider.class)
36 public final class DefaultSslHandlerFactoryProvider implements SslHandlerFactoryProvider, AutoCloseable {
37     private static final X509Certificate[] EMPTY_CERTS = { };
38     private static final char[] EMPTY_CHARS = { };
39
40     private final @NonNull SslHandlerFactory nospecFactory = new SslHandlerFactoryImpl(this, Set.of());
41     private final Registration reg;
42
43     private volatile @NonNull NetconfKeystore keystore = NetconfKeystore.EMPTY;
44
45     @Inject
46     @Activate
47     public DefaultSslHandlerFactoryProvider(@Reference final NetconfKeystoreService keystoreService) {
48         reg = keystoreService.registerKeystoreConsumer(this::onKeystoreUpdated);
49     }
50
51     @Override
52     @PreDestroy
53     @Deactivate
54     public void close() {
55         reg.close();
56     }
57
58     private void onKeystoreUpdated(final @NonNull NetconfKeystore newKeystore) {
59         keystore = newKeystore;
60     }
61
62     @Override
63     public SslHandlerFactory getSslHandlerFactory(final Specification specification) {
64         if (specification == null) {
65             return nospecFactory;
66         }
67         if (specification instanceof TlsCase tlsSpecification) {
68             final var excludedVersions = tlsSpecification.nonnullTls().getExcludedVersions();
69             return excludedVersions == null || excludedVersions.isEmpty() ? nospecFactory
70                 : new SslHandlerFactoryImpl(this, excludedVersions);
71         }
72         throw new IllegalArgumentException("Cannot get TLS specification from: " + specification);
73     }
74
75     /**
76      * Using private keys and trusted certificates to create a new JDK <code>KeyStore</code> which
77      * will be used by TLS clients to create <code>SSLEngine</code>. The private keys are essential
78      * to create JDK <code>KeyStore</code> while the trusted certificates are optional.
79      *
80      * @param allowedKeys Set of keys to include during KeyStore generation, empty set will create
81      *                   a KeyStore with all possible keys.
82      * @return A JDK KeyStore object
83      * @throws GeneralSecurityException If any security exception occurred
84      * @throws IOException If there is an I/O problem with the keystore data
85      */
86     KeyStore getJavaKeyStore(final Set<String> allowedKeys) throws GeneralSecurityException, IOException {
87         requireNonNull(allowedKeys);
88         final var current = keystore;
89         if (current.privateKeys().isEmpty()) {
90             throw new KeyStoreException("No keystore private key found");
91         }
92
93         final var keyStore = KeyStore.getInstance("JKS");
94         keyStore.load(null, null);
95
96         // Private keys first
97         for (var entry : current.privateKeys().entrySet()) {
98             final var alias = entry.getKey();
99             if (allowedKeys.isEmpty() || allowedKeys.contains(alias)) {
100                 final var privateKey = entry.getValue();
101                 keyStore.setKeyEntry(alias, privateKey.key(), EMPTY_CHARS,
102                     privateKey.certificateChain().toArray(EMPTY_CERTS));
103             }
104         }
105
106         for (var entry : current.trustedCertificates().entrySet()) {
107             keyStore.setCertificateEntry(entry.getKey(), entry.getValue());
108         }
109
110         return keyStore;
111     }
112 }