2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.nettyutil.handler.ssh.client;
10 import static com.google.common.base.Verify.verify;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.List;
15 import java.util.stream.Stream;
16 import org.opendaylight.netconf.shaded.sshd.client.ClientBuilder;
17 import org.opendaylight.netconf.shaded.sshd.client.SshClient;
18 import org.opendaylight.netconf.shaded.sshd.common.NamedFactory;
19 import org.opendaylight.netconf.shaded.sshd.common.kex.BuiltinDHFactories;
20 import org.opendaylight.netconf.shaded.sshd.common.kex.KeyExchangeFactory;
21 import org.opendaylight.netconf.shaded.sshd.common.signature.BuiltinSignatures;
22 import org.opendaylight.netconf.shaded.sshd.common.signature.Signature;
25 * A {@link ClientBuilder} which builds {@link NetconfSshClient} instances.
28 public class NetconfClientBuilder extends ClientBuilder {
29 // RFC8332 rsa-sha2-256/rsa-sha2-512 are not a part of Mina's default set of signatures for clients as of 2.5.1.
30 // Add them to ensure interop with modern highly-secured devices.
31 private static final ImmutableList<NamedFactory<Signature>> FULL_SIGNATURE_PREFERENCE =
32 Stream.concat(DEFAULT_SIGNATURE_PREFERENCE.stream(), Stream.of(
33 BuiltinSignatures.rsaSHA512, BuiltinSignatures.rsaSHA256))
34 .filter(BuiltinSignatures::isSupported)
36 .collect(ImmutableList.toImmutableList());
38 // The SHA1 algorithm is disabled by default in Mina SSHD since 2.6.0.
39 // More details available here: https://issues.apache.org/jira/browse/SSHD-1004
40 // This block adds diffie-hellman-group14-sha1 back to the list of supported algorithms.
41 private static final ImmutableList<BuiltinDHFactories> FULL_DH_FACTORIES_LIST =
42 Stream.concat(DEFAULT_KEX_PREFERENCE.stream(), Stream.of(BuiltinDHFactories.dhg14))
43 .collect(ImmutableList.toImmutableList());
44 private static final List<KeyExchangeFactory> FULL_KEX_PREFERENCE =
45 NamedFactory.setUpTransformedFactories(true, FULL_DH_FACTORIES_LIST, DH2KEX);
48 public NetconfSshClient build() {
49 final SshClient client = super.build();
50 verify(client instanceof NetconfSshClient, "Unexpected client %s", client);
51 return (NetconfSshClient) client;
55 protected ClientBuilder fillWithDefaultValues() {
56 if (factory == null) {
57 factory = NetconfSshClient.DEFAULT_NETCONF_SSH_CLIENT_FACTORY;
59 if (signatureFactories == null) {
60 signatureFactories = FULL_SIGNATURE_PREFERENCE;
62 if (keyExchangeFactories == null) {
63 keyExchangeFactories = FULL_KEX_PREFERENCE;
65 return super.fillWithDefaultValues();