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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.security.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.jcajce.JcaPEMKeyConverter;
  33. import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
  34. /**
  35. * This host key provider loads private keys from the specified files.
  36. *
  37. * Note that this class has a direct dependency on BouncyCastle and won't work
  38. * unless it has been correctly registered as a security provider.
  39. *
  40. * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  41. */
  42. public class FileKeyPairProvider extends AbstractKeyPairProvider {
  43. private String[] files;
  44. public FileKeyPairProvider() {
  45. }
  46. public FileKeyPairProvider(String[] files) {
  47. this.files = files;
  48. }
  49. public String[] getFiles() {
  50. return files;
  51. }
  52. public void setFiles(String[] files) {
  53. this.files = files;
  54. }
  55. public Iterable<KeyPair> loadKeys() {
  56. if (!SecurityUtils.isBouncyCastleRegistered()) {
  57. throw new IllegalStateException("BouncyCastle must be registered as a JCE provider");
  58. }
  59. return new Iterable<KeyPair>() {
  60. @Override
  61. public Iterator<KeyPair> iterator() {
  62. return new Iterator<KeyPair>() {
  63. private final Iterator<String> iterator = Arrays.asList(files).iterator();
  64. private KeyPair nextKeyPair;
  65. private boolean nextKeyPairSet = false;
  66. @Override
  67. public boolean hasNext() {
  68. return nextKeyPairSet || setNextObject();
  69. }
  70. @Override
  71. public KeyPair next() {
  72. if (!nextKeyPairSet) {
  73. if (!setNextObject()) {
  74. throw new NoSuchElementException();
  75. }
  76. }
  77. nextKeyPairSet = false;
  78. return nextKeyPair;
  79. }
  80. @Override
  81. public void remove() {
  82. throw new UnsupportedOperationException();
  83. }
  84. private boolean setNextObject() {
  85. while (iterator.hasNext()) {
  86. String file = iterator.next();
  87. nextKeyPair = doLoadKey(file);
  88. if (nextKeyPair != null) {
  89. nextKeyPairSet = true;
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. };
  96. }
  97. };
  98. }
  99. protected KeyPair doLoadKey(String file) {
  100. try {
  101. PEMParser r = new PEMParser(new InputStreamReader(new FileInputStream(file)));
  102. try {
  103. Object o = r.readObject();
  104. JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
  105. pemConverter.setProvider("BC");
  106. if (o instanceof PEMKeyPair) {
  107. o = pemConverter.getKeyPair((PEMKeyPair)o);
  108. return (KeyPair) o;
  109. } else if (o instanceof KeyPair) {
  110. return (KeyPair) o;
  111. }
  112. } finally {
  113. r.close();
  114. }
  115. } catch (Exception e) {
  116. log.warn("Unable to read key " + file, e);
  117. }
  118. return null;
  119. }
  120. }