You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EncryptedFileKeyPairProvider.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.transport.sshd;
  44. import static java.text.MessageFormat.format;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.nio.file.Path;
  48. import java.security.GeneralSecurityException;
  49. import java.security.InvalidKeyException;
  50. import java.security.KeyPair;
  51. import java.security.NoSuchProviderException;
  52. import java.security.PrivateKey;
  53. import java.util.Collection;
  54. import java.util.Iterator;
  55. import java.util.List;
  56. import javax.security.auth.DestroyFailedException;
  57. import org.apache.sshd.common.config.keys.FilePasswordProvider;
  58. import org.apache.sshd.common.config.keys.loader.KeyPairResourceParser;
  59. import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
  60. import org.apache.sshd.common.util.io.IoUtils;
  61. import org.apache.sshd.common.util.security.SecurityUtils;
  62. import org.eclipse.jgit.internal.transport.sshd.RepeatingFilePasswordProvider.ResourceDecodeResult;
  63. /**
  64. * A {@link FileKeyPairProvider} that asks repeatedly for a passphrase for an
  65. * encrypted private key if the {@link FilePasswordProvider} is a
  66. * {@link RepeatingFilePasswordProvider}.
  67. */
  68. public abstract class EncryptedFileKeyPairProvider extends FileKeyPairProvider {
  69. // TODO: remove this class once we're based on sshd > 2.1.0. See upstream
  70. // issue SSHD-850 https://issues.apache.org/jira/browse/SSHD-850 and commit
  71. // https://github.com/apache/mina-sshd/commit/f19bd2e34
  72. /**
  73. * Creates a new {@link EncryptedFileKeyPairProvider} for the given
  74. * {@link Path}s.
  75. *
  76. * @param paths
  77. * to read keys from
  78. */
  79. public EncryptedFileKeyPairProvider(List<Path> paths) {
  80. super(paths);
  81. }
  82. @Override
  83. protected KeyPair doLoadKey(String resourceKey, InputStream inputStream,
  84. FilePasswordProvider provider)
  85. throws IOException, GeneralSecurityException {
  86. if (!(provider instanceof RepeatingFilePasswordProvider)) {
  87. return super.doLoadKey(resourceKey, inputStream, provider);
  88. }
  89. KeyPairResourceParser parser = SecurityUtils.getKeyPairResourceParser();
  90. if (parser == null) {
  91. // This is an internal configuration error, thus no translation.
  92. throw new NoSuchProviderException(
  93. "No registered key-pair resource parser"); //$NON-NLS-1$
  94. }
  95. RepeatingFilePasswordProvider realProvider = (RepeatingFilePasswordProvider) provider;
  96. // Read the stream now so that we can process the content several
  97. // times.
  98. List<String> lines = IoUtils.readAllLines(inputStream);
  99. Collection<KeyPair> ids = null;
  100. while (ids == null) {
  101. try {
  102. ids = parser.loadKeyPairs(resourceKey, realProvider, lines);
  103. realProvider.handleDecodeAttemptResult(resourceKey, "", null); //$NON-NLS-1$
  104. // No exception; success. Exit the loop even if ids is still
  105. // null!
  106. break;
  107. } catch (IOException | GeneralSecurityException
  108. | RuntimeException e) {
  109. ResourceDecodeResult loadResult = realProvider
  110. .handleDecodeAttemptResult(resourceKey, "", e); //$NON-NLS-1$
  111. if (loadResult == null
  112. || loadResult == ResourceDecodeResult.TERMINATE) {
  113. throw e;
  114. } else if (loadResult == ResourceDecodeResult.RETRY) {
  115. continue;
  116. }
  117. // IGNORE doesn't make any sense here, but OK, let's ignore it.
  118. // ids == null, so we'll throw an exception below.
  119. break;
  120. }
  121. }
  122. if (ids == null) {
  123. // The javadoc on loadKeyPairs says it might return null if no
  124. // key pair found. Bad API.
  125. throw new InvalidKeyException(
  126. format(SshdText.get().identityFileNoKey, resourceKey));
  127. }
  128. Iterator<KeyPair> keys = ids.iterator();
  129. if (!keys.hasNext()) {
  130. throw new InvalidKeyException(format(
  131. SshdText.get().identityFileUnsupportedFormat, resourceKey));
  132. }
  133. KeyPair result = keys.next();
  134. if (keys.hasNext()) {
  135. log.warn(format(SshdText.get().identityFileMultipleKeys,
  136. resourceKey));
  137. keys.forEachRemaining(k -> {
  138. PrivateKey pk = k.getPrivate();
  139. if (pk != null) {
  140. try {
  141. pk.destroy();
  142. } catch (DestroyFailedException e) {
  143. // Ignore
  144. }
  145. }
  146. });
  147. }
  148. return result;
  149. }
  150. }