ServerKeyDatabase.java

  1. /*
  2.  * Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.transport.sshd;

  11. import java.net.InetSocketAddress;
  12. import java.security.PublicKey;
  13. import java.util.List;

  14. import org.eclipse.jgit.annotations.NonNull;
  15. import org.eclipse.jgit.transport.CredentialsProvider;

  16. /**
  17.  * An interface for a database of known server keys, supporting finding all
  18.  * known keys and also deciding whether a server key is to be accepted.
  19.  * <p>
  20.  * Connection addresses are given as strings of the format
  21.  * {@code [hostName]:port} if using a non-standard port (i.e., not port 22),
  22.  * otherwise just {@code hostname}.
  23.  * </p>
  24.  *
  25.  * @since 5.5
  26.  */
  27. public interface ServerKeyDatabase {

  28.     /**
  29.      * Retrieves all known host keys for the given addresses.
  30.      *
  31.      * @param connectAddress
  32.      *            IP address the session tried to connect to
  33.      * @param remoteAddress
  34.      *            IP address as reported for the remote end point
  35.      * @param config
  36.      *            giving access to potentially interesting configuration
  37.      *            settings
  38.      * @return the list of known keys for the given addresses
  39.      */
  40.     @NonNull
  41.     List<PublicKey> lookup(@NonNull String connectAddress,
  42.             @NonNull InetSocketAddress remoteAddress,
  43.             @NonNull Configuration config);

  44.     /**
  45.      * Determines whether to accept a received server host key.
  46.      *
  47.      * @param connectAddress
  48.      *            IP address the session tried to connect to
  49.      * @param remoteAddress
  50.      *            IP address as reported for the remote end point
  51.      * @param serverKey
  52.      *            received from the remote end
  53.      * @param config
  54.      *            giving access to potentially interesting configuration
  55.      *            settings
  56.      * @param provider
  57.      *            for interacting with the user, if required; may be
  58.      *            {@code null}
  59.      * @return {@code true} if the serverKey is accepted, {@code false}
  60.      *         otherwise
  61.      */
  62.     boolean accept(@NonNull String connectAddress,
  63.             @NonNull InetSocketAddress remoteAddress,
  64.             @NonNull PublicKey serverKey,
  65.             @NonNull Configuration config, CredentialsProvider provider);

  66.     /**
  67.      * A simple provider for ssh config settings related to host key checking.
  68.      * An instance is created by the JGit sshd framework and passed into
  69.      * {@link ServerKeyDatabase#lookup(String, InetSocketAddress, Configuration)}
  70.      * and
  71.      * {@link ServerKeyDatabase#accept(String, InetSocketAddress, PublicKey, Configuration, CredentialsProvider)}.
  72.      */
  73.     interface Configuration {

  74.         /**
  75.          * Retrieves the list of file names from the "UserKnownHostsFile" ssh
  76.          * config.
  77.          *
  78.          * @return the list as configured, with ~ already replaced
  79.          */
  80.         List<String> getUserKnownHostsFiles();

  81.         /**
  82.          * Retrieves the list of file names from the "GlobalKnownHostsFile" ssh
  83.          * config.
  84.          *
  85.          * @return the list as configured, with ~ already replaced
  86.          */
  87.         List<String> getGlobalKnownHostsFiles();

  88.         /**
  89.          * The possible values for the "StrictHostKeyChecking" ssh config.
  90.          */
  91.         enum StrictHostKeyChecking {
  92.             /**
  93.              * "ask"; default: ask the user whether to accept (and store) a new
  94.              * or mismatched key.
  95.              */
  96.             ASK,
  97.             /**
  98.              * "yes", "on": never accept new or mismatched keys.
  99.              */
  100.             REQUIRE_MATCH,
  101.             /**
  102.              * "no", "off": always accept new or mismatched keys.
  103.              */
  104.             ACCEPT_ANY,
  105.             /**
  106.              * "accept-new": accept new keys, but never accept modified keys.
  107.              */
  108.             ACCEPT_NEW
  109.         }

  110.         /**
  111.          * Obtains the value of the "StrictHostKeyChecking" ssh config.
  112.          *
  113.          * @return the {@link StrictHostKeyChecking}
  114.          */
  115.         @NonNull
  116.         StrictHostKeyChecking getStrictHostKeyChecking();

  117.         /**
  118.          * Obtains the value of the "HashKnownHosts" ssh config.
  119.          *
  120.          * @return {@code true} if new entries should be stored with hashed host
  121.          *         information, {@code false} otherwise
  122.          */
  123.         boolean getHashKnownHosts();

  124.         /**
  125.          * Obtains the user name used in the connection attempt.
  126.          *
  127.          * @return the user name
  128.          */
  129.         @NonNull
  130.         String getUsername();
  131.     }
  132. }