Skip to content

Commit e1cb05c

Browse files
committed
code cleanup
1 parent 7cac328 commit e1cb05c

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

core/src/main/java/net/sourceforge/jnlp/security/KeystorePasswordAttempter.java

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
class KeystorePasswordAttempter {
6161

62-
private final static Logger LOG = LoggerFactory.getLogger(KeystorePasswordAttempter.class);
62+
private static final Logger LOG = LoggerFactory.getLogger(KeystorePasswordAttempter.class);
6363

6464
private static final char[] DEFAULT_PASSWORD = "changeit".toCharArray();
6565

@@ -76,19 +76,7 @@ public SavedPassword(char[] pass) {
7676
}
7777
}
7878

79-
/**
80-
* This password can read any keystore. But if you save with him, integrity
81-
* of keystore will be lost for ever.
82-
*/
83-
static class AlmightyPassword extends SavedPassword {
84-
85-
public AlmightyPassword() {
86-
super(null);
87-
}
88-
89-
}
90-
91-
static abstract class KeystoreOperation {
79+
abstract static class KeystoreOperation {
9280

9381
protected final KeyManagerFactory kmf;
9482
protected final KeyStore ks;
@@ -105,10 +93,6 @@ public KeystoreOperation(KeyStore ks, String alias, Key key, Certificate[] certC
10593
this(null, ks, alias, key, certChain, null);
10694
}
10795

108-
public KeystoreOperation(KeyStore ks, String alias, Key key, Certificate[] certChain, File f) {
109-
this(null, ks, alias, key, certChain, f);
110-
}
111-
11296
public KeystoreOperation(KeyManagerFactory kmf, KeyStore ks) {
11397
this(kmf, ks, null, null, null, null);
11498
}
@@ -138,31 +122,31 @@ private KeystorePasswordAttempter(SavedPassword... initialPasswords) {
138122
}
139123

140124
Key unlockKeystore(KeystoreOperation operation) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, IOException, CertificateException {
141-
final String keyStoreFileName = operation.f != null ? operation.f.getAbsolutePath() : "Unknown";
142-
SavedPassword successfulKey = successfulPerKeystore.get(keyStoreFileName);
143-
LOG.debug("unlockKeyStore: For file {}, found successful pass = {} ", keyStoreFileName, successfulKey != null);
125+
final String keyStorePath = operation.f != null ? operation.f.getCanonicalPath() : "Unknown";
126+
final SavedPassword successfulKey = successfulPerKeystore.get(keyStorePath);
127+
LOG.debug("unlockKeyStore: For file {}, found successful pass = {} ", keyStorePath, successfulKey != null);
144128

145129
Exception firstEx = null;
146130
String messages = "";
147-
List<SavedPassword> localPasses = new ArrayList<>();
148-
if (successfulKey != null){
131+
final List<SavedPassword> localPasses = new ArrayList<>();
132+
if (successfulKey != null) {
149133
//successful must be first. If it is not, then writing to keystore by illegal password, will kill keystore's integrity
150134
localPasses.add(successfulKey);
151135
}
152136
localPasses.addAll(passes);
153137
for (int i = 0; i < localPasses.size(); i++) {
154-
SavedPassword pass = localPasses.get(i);
138+
final SavedPassword pass = localPasses.get(i);
155139
try {
156-
LOG.debug("unlockKeyStore: Operating Keystore {}", keyStoreFileName);
140+
LOG.debug("unlockKeyStore: Operating Keystore {}", keyStorePath);
157141
//we expect, that any keystore is loaded before read.
158142
//so we are writing by correct password
159143
//if no successful password was provided during reading, then finish(firstEx); will save us from overwrite
160-
Key result = operation.operateKeystore(pass.pass);
144+
final Key result = operation.operateKeystore(pass.pass);
161145
//ok we were successful
162146
//save the loading password for storing purposes (and another reading too)
163-
if (!keyStoreFileName.equals("Unknown")) {
164-
LOG.debug("Store successful pass for key {}", keyStoreFileName);
165-
successfulPerKeystore.put(keyStoreFileName, pass);
147+
if (operation.f != null) {
148+
LOG.debug("unlockKeyStore: Store successful pass for file {}", keyStorePath);
149+
successfulPerKeystore.put(keyStorePath, pass);
166150
}
167151
return result;
168152
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | IOException | CertificateException ex) {
@@ -173,24 +157,24 @@ Key unlockKeystore(KeystoreOperation operation) throws KeyStoreException, NoSuch
173157
LOG.error(IcedTeaWebConstants.DEFAULT_ERROR_MESSAGE, ex);
174158
//tried all known, ask for new or finally die
175159
if (i + 1 == localPasses.size()) {
176-
String s1 = Translator.R("KSresultUntilNow", messages, operation.getId(), keyStoreFileName, "" + (i + 1));
160+
String s1 = Translator.R("KSresultUntilNow", messages, operation.getId(), keyStorePath, localPasses.size());
177161
LOG.info(s1);
178-
LOG.info("Invalid password For keystore {} ?", keyStoreFileName);
162+
LOG.info("Invalid password For keystore {} ?", keyStorePath);
179163
if (JNLPRuntime.isHeadless()) {
180164
OutputController.getLogger().printOutLn(s1 + "\n" + Translator.R("KSheadlesWarning"));
181-
String s = OutputController.getLogger().readLine();
165+
final String s = OutputController.getLogger().readLine();
182166
if (s == null || s.trim().isEmpty()) {
183167
finish(firstEx);
168+
} else {
169+
addPnewPassword(s, localPasses);
184170
}
185-
//if input is null or empty , exception is thrown from finish method
186-
addPnewPassword(s, localPasses);
187171
} else {
188-
String s = JOptionPane.showInputDialog(null, s1 + "\n" + Translator.R("KSnwPassHelp"), Translator.R("KSTitle"), JOptionPane.OK_CANCEL_OPTION);
172+
final String s = JOptionPane.showInputDialog(null, s1 + "\n" + Translator.R("KSnwPassHelp"), Translator.R("KSTitle"), JOptionPane.OK_CANCEL_OPTION);
189173
if (s == null) {
190174
finish(firstEx);
175+
} else {
176+
addPnewPassword(s, localPasses);
191177
}
192-
//if input is null, exception is thrown from finish method
193-
addPnewPassword(s, localPasses);
194178
}
195179
//user already read all messages, now show only last one
196180
messages = "";

0 commit comments

Comments
 (0)