47
47
import java .text .DateFormat ;
48
48
import java .text .ParseException ;
49
49
import java .text .SimpleDateFormat ;
50
- import java .util .Arrays ;
51
- import java .util .Date ;
52
- import java .util .Map ;
53
- import java .util .Scanner ;
50
+ import java .util .*;
54
51
55
52
import static org .hamcrest .CoreMatchers .containsString ;
56
53
import static org .hamcrest .CoreMatchers .equalTo ;
@@ -70,6 +67,44 @@ public class OpenSSHKeyFileTest {
70
67
final char [] correctPassphrase = "test_passphrase" .toCharArray ();
71
68
final char [] incorrectPassphrase = new char []{' ' };
72
69
70
+ private static class WipeTrackingPasswordFinder implements PasswordFinder {
71
+ private int reqCounter = 0 ;
72
+
73
+ final private String password ;
74
+ final private boolean withRetry ;
75
+ final private ArrayList <char []> toWipe = new ArrayList <>();
76
+
77
+ WipeTrackingPasswordFinder (String password , Boolean withRetry ) {
78
+ this .password = password ;
79
+ this .withRetry = withRetry ;
80
+ }
81
+
82
+ @ Override
83
+ public char [] reqPassword (Resource <?> resource ) {
84
+ char [] passwordChars ;
85
+ if (withRetry && reqCounter < 3 ) {
86
+ reqCounter ++;
87
+ // Return an incorrect password three times before returning the correct one.
88
+ passwordChars = (password + "incorrect" ).toCharArray ();
89
+ } else {
90
+ passwordChars = password .toCharArray ();
91
+ }
92
+ toWipe .add (passwordChars );
93
+ return passwordChars ;
94
+ }
95
+
96
+ @ Override
97
+ public boolean shouldRetry (Resource <?> resource ) {
98
+ return withRetry && reqCounter <= 3 ;
99
+ }
100
+
101
+ public void assertWiped () {
102
+ for (char [] passwordChars : toWipe ) {
103
+ assertArrayEquals (new char [passwordChars .length ], passwordChars );
104
+ }
105
+ }
106
+ };
107
+
73
108
final PasswordFinder onlyGivesWhenReady = new PasswordFinder () {
74
109
@ Override
75
110
public char [] reqPassword (Resource resource ) {
@@ -249,27 +284,11 @@ public void shouldLoadECDSAPrivateKeyAsOpenSSHV1() throws IOException {
249
284
250
285
private void checkOpenSSHKeyV1 (String key , final String password , boolean withRetry ) throws IOException {
251
286
OpenSSHKeyV1KeyFile keyFile = new OpenSSHKeyV1KeyFile ();
252
- keyFile .init (new File (key ), new PasswordFinder () {
253
- private int reqCounter = 0 ;
254
-
255
- @ Override
256
- public char [] reqPassword (Resource <?> resource ) {
257
- if (withRetry && reqCounter < 3 ) {
258
- reqCounter ++;
259
- // Return an incorrect password three times before returning the correct one.
260
- return (password + "incorrect" ).toCharArray ();
261
- } else {
262
- return password .toCharArray ();
263
- }
264
- }
265
-
266
- @ Override
267
- public boolean shouldRetry (Resource <?> resource ) {
268
- return withRetry && reqCounter <= 3 ;
269
- }
270
- });
287
+ WipeTrackingPasswordFinder pwf = new WipeTrackingPasswordFinder (password , withRetry );
288
+ keyFile .init (new File (key ), pwf );
271
289
PrivateKey aPrivate = keyFile .getPrivate ();
272
290
assertThat (aPrivate .getAlgorithm (), equalTo ("EdDSA" ));
291
+ pwf .assertWiped ();
273
292
}
274
293
275
294
@ Test
0 commit comments