Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FileKeyPairProvider.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package com.gitblit.transport.ssh;
  20. import java.io.FileInputStream;
  21. import java.io.InputStreamReader;
  22. import java.security.KeyPair;
  23. import java.util.Arrays;
  24. import java.util.Iterator;
  25. import java.util.NoSuchElementException;
  26. import org.apache.sshd.common.keyprovider.AbstractKeyPairProvider;
  27. import org.apache.sshd.common.util.SecurityUtils;
  28. import org.bouncycastle.openssl.PEMDecryptorProvider;
  29. import org.bouncycastle.openssl.PEMEncryptedKeyPair;
  30. import org.bouncycastle.openssl.PEMKeyPair;
  31. import org.bouncycastle.openssl.PEMParser;
  32. import org.bouncycastle.openssl.PasswordFinder;
  33. import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
  34. import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
  35. /**
  36. * This host key provider loads private keys from the specified files.
  37. *
  38. * Note that this class has a direct dependency on BouncyCastle and won't work
  39. * unless it has been correctly registered as a security provider.
  40. *
  41. * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  42. */
  43. public class FileKeyPairProvider extends AbstractKeyPairProvider {
  44. private String[] files;
  45. private PasswordFinder passwordFinder;
  46. public FileKeyPairProvider() {
  47. }
  48. public FileKeyPairProvider(String[] files) {
  49. this.files = files;
  50. }
  51. public FileKeyPairProvider(String[] files, PasswordFinder passwordFinder) {
  52. this.files = files;
  53. this.passwordFinder = passwordFinder;
  54. }
  55. public String[] getFiles() {
  56. return files;
  57. }
  58. public void setFiles(String[] files) {
  59. this.files = files;
  60. }
  61. public PasswordFinder getPasswordFinder() {
  62. return passwordFinder;
  63. }
  64. public void setPasswordFinder(PasswordFinder passwordFinder) {
  65. this.passwordFinder = passwordFinder;
  66. }
  67. public Iterable<KeyPair> loadKeys() {
  68. if (!SecurityUtils.isBouncyCastleRegistered()) {
  69. throw new IllegalStateException("BouncyCastle must be registered as a JCE provider");
  70. }
  71. return new Iterable<KeyPair>() {
  72. @Override
  73. public Iterator<KeyPair> iterator() {
  74. return new Iterator<KeyPair>() {
  75. private final Iterator<String> iterator = Arrays.asList(files).iterator();
  76. private KeyPair nextKeyPair;
  77. private boolean nextKeyPairSet = false;
  78. @Override
  79. public boolean hasNext() {
  80. return nextKeyPairSet || setNextObject();
  81. }
  82. @Override
  83. public KeyPair next() {
  84. if (!nextKeyPairSet) {
  85. if (!setNextObject()) {
  86. throw new NoSuchElementException();
  87. }
  88. }
  89. nextKeyPairSet = false;
  90. return nextKeyPair;
  91. }
  92. @Override
  93. public void remove() {
  94. throw new UnsupportedOperationException();
  95. }
  96. private boolean setNextObject() {
  97. while (iterator.hasNext()) {
  98. String file = iterator.next();
  99. nextKeyPair = doLoadKey(file);
  100. if (nextKeyPair != null) {
  101. nextKeyPairSet = true;
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. };
  108. }
  109. };
  110. }
  111. protected KeyPair doLoadKey(String file) {
  112. try {
  113. PEMParser r = new PEMParser(new InputStreamReader(new FileInputStream(file)));
  114. try {
  115. Object o = r.readObject();
  116. JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
  117. pemConverter.setProvider("BC");
  118. if (passwordFinder != null && o instanceof PEMEncryptedKeyPair) {
  119. JcePEMDecryptorProviderBuilder decryptorBuilder = new JcePEMDecryptorProviderBuilder();
  120. PEMDecryptorProvider pemDecryptor = decryptorBuilder.build(passwordFinder.getPassword());
  121. o = pemConverter.getKeyPair(((PEMEncryptedKeyPair) o).decryptKeyPair(pemDecryptor));
  122. }
  123. if (o instanceof PEMKeyPair) {
  124. o = pemConverter.getKeyPair((PEMKeyPair)o);
  125. return (KeyPair) o;
  126. } else if (o instanceof KeyPair) {
  127. return (KeyPair) o;
  128. }
  129. } finally {
  130. r.close();
  131. }
  132. } catch (Exception e) {
  133. log.warn("Unable to read key " + file, e);
  134. }
  135. return null;
  136. }
  137. }