1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.internal.transport.sshd;
44
45 import static java.text.MessageFormat.format;
46
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.nio.file.Path;
50 import java.security.GeneralSecurityException;
51 import java.security.InvalidKeyException;
52 import java.security.KeyPair;
53 import java.security.NoSuchProviderException;
54 import java.security.PrivateKey;
55 import java.util.Collection;
56 import java.util.Iterator;
57 import java.util.List;
58
59 import javax.security.auth.DestroyFailedException;
60
61 import org.apache.sshd.common.config.keys.FilePasswordProvider;
62 import org.apache.sshd.common.config.keys.loader.KeyPairResourceParser;
63 import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
64 import org.apache.sshd.common.util.io.IoUtils;
65 import org.apache.sshd.common.util.security.SecurityUtils;
66 import org.eclipse.jgit.internal.transport.sshd.RepeatingFilePasswordProvider.ResourceDecodeResult;
67
68
69
70
71
72
73 public abstract class EncryptedFileKeyPairProvider extends FileKeyPairProvider {
74
75
76
77
78
79
80
81
82
83
84
85
86 public EncryptedFileKeyPairProvider(List<Path> paths) {
87 super(paths);
88 }
89
90 @Override
91 protected KeyPair doLoadKey(String resourceKey, InputStream inputStream,
92 FilePasswordProvider provider)
93 throws IOException, GeneralSecurityException {
94 if (!(provider instanceof RepeatingFilePasswordProvider)) {
95 return super.doLoadKey(resourceKey, inputStream, provider);
96 }
97 KeyPairResourceParser parser = SecurityUtils.getKeyPairResourceParser();
98 if (parser == null) {
99
100 throw new NoSuchProviderException(
101 "No registered key-pair resource parser");
102 }
103 RepeatingFilePasswordProvider realProvider = (RepeatingFilePasswordProvider) provider;
104
105
106 List<String> lines = IoUtils.readAllLines(inputStream);
107 Collection<KeyPair> ids = null;
108 while (ids == null) {
109 try {
110 ids = parser.loadKeyPairs(resourceKey, realProvider, lines);
111 realProvider.handleDecodeAttemptResult(resourceKey, "", null);
112
113
114 break;
115 } catch (IOException | GeneralSecurityException
116 | RuntimeException e) {
117 ResourceDecodeResult loadResult = realProvider
118 .handleDecodeAttemptResult(resourceKey, "", e);
119 if (loadResult == null
120 || loadResult == ResourceDecodeResult.TERMINATE) {
121 throw e;
122 } else if (loadResult == ResourceDecodeResult.RETRY) {
123 continue;
124 }
125
126
127 break;
128 }
129 }
130 if (ids == null) {
131
132
133 throw new InvalidKeyException(
134 format(SshdText.get().identityFileNoKey, resourceKey));
135 }
136 Iterator<KeyPair> keys = ids.iterator();
137 if (!keys.hasNext()) {
138 throw new InvalidKeyException(format(
139 SshdText.get().identityFileUnsupportedFormat, resourceKey));
140 }
141 KeyPair result = keys.next();
142 if (keys.hasNext()) {
143 log.warn(format(SshdText.get().identityFileMultipleKeys,
144 resourceKey));
145 keys.forEachRemaining(k -> {
146 PrivateKey pk = k.getPrivate();
147 if (pk != null) {
148 try {
149 pk.destroy();
150 } catch (DestroyFailedException e) {
151
152 }
153 }
154 });
155 }
156 return result;
157 }
158 }