KeyPasswordProvider.java

  1. /*
  2.  * Copyright (C) 2018, 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.io.IOException;
  12. import java.security.GeneralSecurityException;

  13. import org.eclipse.jgit.transport.URIish;

  14. /**
  15.  * A {@code KeyPasswordProvider} provides passwords for encrypted private keys.
  16.  *
  17.  * @since 5.2
  18.  */
  19. public interface KeyPasswordProvider {

  20.     /**
  21.      * Obtains a passphrase to use to decrypt an ecrypted private key. Returning
  22.      * {@code null} or an empty array will skip this key. To cancel completely,
  23.      * the operation should raise
  24.      * {@link java.util.concurrent.CancellationException}.
  25.      *
  26.      * @param uri
  27.      *            identifying the key resource that is being attempted to be
  28.      *            loaded
  29.      * @param attempt
  30.      *            the number of previous attempts to get a passphrase; &gt;= 0
  31.      * @return the passphrase
  32.      * @throws IOException
  33.      *             if no password can be obtained
  34.      */
  35.     char[] getPassphrase(URIish uri, int attempt) throws IOException;

  36.     /**
  37.      * Define the maximum number of attempts to get a passphrase that should be
  38.      * attempted for one identity resource through this provider.
  39.      *
  40.      * @param maxNumberOfAttempts
  41.      *            number of times to ask for a passphrase;
  42.      *            {@link IllegalArgumentException} may be thrown if &lt;= 0
  43.      */
  44.     void setAttempts(int maxNumberOfAttempts);

  45.     /**
  46.      * Gets the maximum number of attempts to get a passphrase that should be
  47.      * attempted for one identity resource through this provider. The default
  48.      * return 1.
  49.      *
  50.      * @return the number of times to ask for a passphrase; should be &gt;= 1.
  51.      */
  52.     default int getAttempts() {
  53.         return 1;
  54.     }

  55.     /**
  56.      * Invoked after a key has been loaded. If this raises an exception, the
  57.      * original {@code error} is lost unless it is attached to that exception.
  58.      *
  59.      * @param uri
  60.      *            identifying the key resource the key was attempted to be
  61.      *            loaded from
  62.      * @param attempt
  63.      *            the number of times {@link #getPassphrase(URIish, int)} had
  64.      *            been called; zero indicates that {@code uri} refers to a
  65.      *            non-encrypted key
  66.      * @param error
  67.      *            {@code null} if the key was loaded successfully; otherwise an
  68.      *            exception indicating why the key could not be loaded
  69.      * @return {@code true} to re-try again; {@code false} to re-raise the
  70.      *         {@code error} exception; Ignored if the key was loaded
  71.      *         successfully, i.e., if {@code error == null}.
  72.      * @throws IOException
  73.      * @throws GeneralSecurityException
  74.      */
  75.     boolean keyLoaded(URIish uri, int attempt, Exception error)
  76.             throws IOException, GeneralSecurityException;
  77. }