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 |
|
|
44 |
|
|
45 |
|
|
46 |
|
package issrg.SAWS; |
47 |
|
|
48 |
|
import issrg.SAWS.callback.CertificateDataCallback; |
49 |
|
import issrg.SAWS.callback.SAWSChoiceCallback; |
50 |
|
import issrg.SAWS.callback.SAWSGUICallbackHandler; |
51 |
|
import issrg.SAWS.callback.SAWSPasswordCallback; |
52 |
|
import issrg.SAWS.callback.SAWSTextInputCallback; |
53 |
|
import issrg.SAWS.callback.SAWSTextOutputCallback; |
54 |
|
import issrg.SAWS.util.CertificateData; |
55 |
|
import issrg.SAWS.util.SAWSLogWriter; |
56 |
|
|
57 |
|
import java.util.*; |
58 |
|
import java.io.*; |
59 |
|
|
60 |
|
import java.math.BigInteger; |
61 |
|
|
62 |
|
import java.security.*; |
63 |
|
import javax.crypto.*; |
64 |
|
import javax.crypto.spec.* ; |
65 |
|
|
66 |
|
import java.security.cert.Certificate; |
67 |
|
|
68 |
|
import java.security.cert.CertificateEncodingException; |
69 |
|
import java.security.cert.CertificateException; |
70 |
|
import java.security.cert.CertificateFactory; |
71 |
|
import java.security.cert.X509Certificate; |
72 |
|
|
73 |
|
import javax.security.auth.callback.Callback; |
74 |
|
|
75 |
|
import javax.security.auth.callback.CallbackHandler; |
76 |
|
import javax.security.auth.x500.X500Principal; |
77 |
|
|
78 |
|
import javax.security.auth.x500.X500PrivateCredential; |
79 |
|
|
80 |
|
import org.bouncycastle.asn1.x509.X509Name; |
81 |
|
import org.bouncycastle.jce.PKCS10CertificationRequest; |
82 |
|
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
83 |
|
import org.bouncycastle.x509.X509V1CertificateGenerator; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@author |
90 |
|
|
|
|
| 51.1% |
Uncovered Elements: 302 (617) |
Complexity: 99 |
Complexity Density: 0.29 |
|
91 |
|
public class TCBKeystoreManagement { |
92 |
|
|
93 |
|
private String encryptionKeystoreLocation = null; |
94 |
|
private String signingKeystoreLocation = null; |
95 |
|
private int numberOfPasswordShares = 2; |
96 |
|
private int numberOfEncPasswordShares =2; |
97 |
|
private String rootCA = null; |
98 |
|
private String vtPKC = null; |
99 |
|
|
100 |
|
private File encryptionKeyfile = null; |
101 |
|
private File signingKeyfile =null; |
102 |
|
private String sawsPW =null; |
103 |
|
private String sawsEncPW = null; |
104 |
|
|
105 |
|
|
106 |
|
private PublicKey sawsEncryptionPublicKey = null; |
107 |
|
private PrivateKey sawsEncryptionPrivateKey = null; |
108 |
|
private PublicKey sawsSigningPublicKey = null; |
109 |
|
private PrivateKey sawsSigningPrivateKey = null; |
110 |
|
private PublicKey vtEncryptionPublicKey = null; |
111 |
|
private PublicKey rootCAPublicKey = null; |
112 |
|
private int debugLevel = 0; |
113 |
|
|
114 |
|
private SecretKey sawsTCBSecretKey = null; |
115 |
|
private PBEParameterSpec paramSpec = null; |
116 |
|
|
117 |
|
private byte[] baSigningPublicKeyCert = null; |
118 |
|
private String signingAlgName = null; |
119 |
|
|
120 |
|
private CallbackHandler callbackHandler = new SAWSGUICallbackHandler(); |
121 |
|
private Callback[] cbs = null; |
122 |
|
private byte hashAlgorithm = SAWSConstant.SHA1; |
123 |
|
|
124 |
|
private java.security.cert.Certificate sawsEncCertificate; |
125 |
|
private java.security.cert.Certificate sawsSigCertificate; |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
private static SAWSLogWriter sawsDebugLog = new SAWSLogWriter(TCBKeystoreManagement.class.getName()); |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
134 |
8
|
public TCBKeystoreManagement(String signkeystoreLocation, int numOfPassShares, ... |
135 |
|
String encKeystoreLocation, int numOfEncPassShares, String rootCAPara, String vtPKCPara, |
136 |
|
int debugLevel) { |
137 |
8
|
signingKeystoreLocation = signkeystoreLocation; |
138 |
8
|
numberOfPasswordShares = numOfPassShares; |
139 |
8
|
encryptionKeystoreLocation = encKeystoreLocation; |
140 |
8
|
numberOfEncPasswordShares = numOfEncPassShares; |
141 |
8
|
rootCA = rootCAPara; |
142 |
8
|
vtPKC = vtPKCPara; |
143 |
8
|
this.debugLevel = debugLevel; |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
146 |
8
|
public TCBKeystoreManagement(String signkeystoreLocation, int numOfPassShares, ... |
147 |
|
String encKeystoreLocation, int numOfEncPassShares, String rootCAPara, String vtPKCPara, |
148 |
|
int debugLevel, byte hashAlgorithm, CallbackHandler ch) { |
149 |
8
|
this(signkeystoreLocation, numOfPassShares, |
150 |
|
encKeystoreLocation, numOfEncPassShares, rootCAPara, vtPKCPara, |
151 |
|
debugLevel); |
152 |
8
|
this.hashAlgorithm = hashAlgorithm; |
153 |
8
|
this.callbackHandler = ch; |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
|
|
| 50% |
Uncovered Elements: 3 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
160 |
5
|
public void checkSigningKeystoreFile() {... |
161 |
5
|
signingKeyfile = new File( signingKeystoreLocation ); |
162 |
5
|
if ( !signingKeyfile.exists() ) { |
163 |
0
|
this.showMessage("The signing keystore is missing: " + signingKeystoreLocation |
164 |
|
+ "\n\nThis is the first time to run SAWS, or the signing keystore has been removed illegally." |
165 |
|
+ "\nSAWS will stop. Please use SAWS to generate a new signing keystore, then restart SAWS.", |
166 |
|
SAWSTextOutputCallback.WARNING); |
167 |
0
|
System.exit(-1); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
|
|
| 50% |
Uncovered Elements: 3 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
174 |
4
|
public void checkEncKeystoreFile(){... |
175 |
4
|
encryptionKeyfile = new File( encryptionKeystoreLocation ); |
176 |
4
|
if ( !encryptionKeyfile.exists() ) { |
177 |
0
|
this.showMessage("The encryption keystore is missing: " + encryptionKeystoreLocation |
178 |
|
+ "\n\nThis is the first time to run SAWS, or the encryption keystore is removed illegally. " |
179 |
|
+ "\nSAWS will stop. " |
180 |
|
+ "\nPlease recover the encryption keystore from your backup if you have backed it up before, " |
181 |
|
+ "\nor use SAWS to create a new encryption keystore, then restart SAWS.", |
182 |
|
SAWSTextOutputCallback.WARNING); |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
0
|
System.exit(-1); |
196 |
|
} |
197 |
|
|
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
|
|
| 53.8% |
Uncovered Elements: 30 (65) |
Complexity: 13 |
Complexity Density: 0.25 |
|
205 |
4
|
public void readKeystores(){... |
206 |
|
|
207 |
4
|
sawsPW = getSAWSPassword3Attempts(signingKeystoreLocation, numberOfPasswordShares, "signing", false); |
208 |
4
|
if (sawsPW == null ) { |
209 |
0
|
System.exit(-1); |
210 |
|
} |
211 |
|
|
212 |
4
|
sawsEncPW = getSAWSPassword3Attempts(encryptionKeystoreLocation, numberOfEncPasswordShares, "encryption", false); |
213 |
4
|
if (sawsEncPW == null ) { |
214 |
0
|
System.exit(-1); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
4
|
String signer = "saws"; |
219 |
4
|
KeyStore signKeystore = null; |
220 |
4
|
try{ |
221 |
4
|
signKeystore = KeyStore.getInstance(KeyStore.getDefaultType()); |
222 |
4
|
try { |
223 |
4
|
signKeystore.load(new BufferedInputStream( |
224 |
|
new FileInputStream(signingKeyfile)), sawsPW.toCharArray()); |
225 |
|
} catch (IOException e) { |
226 |
0
|
this.showMessage("Reading signing keystore error. The signing keystore has been tampered with.", |
227 |
|
SAWSTextOutputCallback.ERROR); |
228 |
|
|
229 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) { |
230 |
0
|
sawsDebugLog.write(e.toString()); |
231 |
|
} |
232 |
0
|
System.exit(-1); |
233 |
|
} |
234 |
|
|
235 |
4
|
sawsSigningPrivateKey = (PrivateKey) signKeystore.getKey(signer, sawsPW.toCharArray()); |
236 |
4
|
sawsSigCertificate = signKeystore.getCertificate(signer); |
237 |
|
|
238 |
4
|
signingAlgName = SAWSConstant.HASH_ALG_NAMES[this.hashAlgorithm] |
239 |
|
+ "with"+sawsSigCertificate.getPublicKey().getAlgorithm(); |
240 |
|
|
241 |
4
|
baSigningPublicKeyCert = sawsSigCertificate.getEncoded(); |
242 |
4
|
sawsSigningPublicKey = sawsSigCertificate.getPublicKey(); |
243 |
|
|
244 |
4
|
java.security.cert.Certificate caCert = signKeystore.getCertificate("rootca"); |
245 |
4
|
if( (caCert==null) || (rootCA == null) ) { |
246 |
0
|
this.showMessage( |
247 |
|
"The rootCA PKC is missing in the SAWS configuration file or in the signing keystore." |
248 |
|
+ "\nSAWS will stop.", SAWSTextOutputCallback.ERROR); |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
0
|
System.exit(-1); |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
} else { |
263 |
4
|
rootCAPublicKey = caCert.getPublicKey(); |
264 |
|
} |
265 |
|
|
266 |
|
} catch (Exception e2){ |
267 |
0
|
this.showMessage("Something wrong with signing keystore reading.", |
268 |
|
SAWSTextOutputCallback.ERROR); |
269 |
|
|
270 |
|
|
271 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
272 |
|
|
273 |
0
|
sawsDebugLog.write(e2.toString()); |
274 |
0
|
System.exit(-1); |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
4
|
KeyStore encKeystore = null; |
279 |
4
|
try{ |
280 |
4
|
encKeystore = KeyStore.getInstance(KeyStore.getDefaultType()); |
281 |
4
|
encKeystore.load(new BufferedInputStream( |
282 |
|
new FileInputStream(encryptionKeyfile)), sawsEncPW.toCharArray()); |
283 |
4
|
sawsEncryptionPrivateKey = (PrivateKey) encKeystore.getKey(signer, sawsEncPW.toCharArray()); |
284 |
4
|
sawsEncCertificate = encKeystore.getCertificate(signer); |
285 |
|
|
286 |
4
|
sawsEncryptionPublicKey = sawsEncCertificate.getPublicKey(); |
287 |
|
} catch(Exception e2){ |
288 |
0
|
this.showMessage("Something wrong with encryption keystore reading.", |
289 |
|
SAWSTextOutputCallback.ERROR); |
290 |
|
|
291 |
|
|
292 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
293 |
|
|
294 |
0
|
sawsDebugLog.write(e2.toString()); |
295 |
0
|
System.exit(-1); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
4
|
try{ |
300 |
4
|
FileInputStream fis = new FileInputStream(vtPKC); |
301 |
4
|
BufferedInputStream bis = new BufferedInputStream(fis); |
302 |
4
|
java.security.cert.CertificateFactory cf = null; |
303 |
4
|
cf = java.security.cert.CertificateFactory.getInstance("X.509"); |
304 |
4
|
java.security.cert.Certificate certTemp = cf.generateCertificate(bis); |
305 |
|
|
306 |
4
|
vtEncryptionPublicKey = certTemp.getPublicKey(); |
307 |
|
} catch (Exception e2) { |
308 |
0
|
this.showMessage("SAWS VT encryption key is not correct.", |
309 |
|
SAWSTextOutputCallback.ERROR); |
310 |
|
|
311 |
|
|
312 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
313 |
|
|
314 |
0
|
sawsDebugLog.write(e2.toString()); |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
} |
319 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
320 |
28
|
public PublicKey getrootCAPublicKey (){... |
321 |
28
|
return rootCAPublicKey; |
322 |
|
} |
323 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
324 |
11
|
public PublicKey getvtEncryptionPublicKey(){... |
325 |
11
|
return vtEncryptionPublicKey; |
326 |
|
} |
327 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
328 |
11
|
public PublicKey getsawsEncryptionPublicKey(){... |
329 |
11
|
return sawsEncryptionPublicKey; |
330 |
|
} |
331 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
332 |
56
|
public PrivateKey getsawsEncryptionPrivateKey(){... |
333 |
56
|
return sawsEncryptionPrivateKey; |
334 |
|
} |
335 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
336 |
11
|
public PrivateKey getsawsSigningPrivateKey(){... |
337 |
11
|
return sawsSigningPrivateKey; |
338 |
|
} |
339 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
340 |
0
|
public PublicKey getsawsSigningPublicKey(){... |
341 |
0
|
return sawsSigningPublicKey; |
342 |
|
} |
343 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
344 |
11
|
public byte[] getbaSigningPublicKeyCert(){... |
345 |
11
|
return baSigningPublicKeyCert; |
346 |
|
} |
347 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
348 |
0
|
public String getsigningAlgName(){... |
349 |
0
|
return signingAlgName; |
350 |
|
} |
351 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
352 |
4
|
public SecretKey getsawsTCBSecretKey(){... |
353 |
4
|
sawsTCBSecretKey = generateSecretKey(sawsPW); |
354 |
4
|
return sawsTCBSecretKey; |
355 |
|
} |
356 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
357 |
4
|
public PBEParameterSpec getparamSpec(){... |
358 |
4
|
byte[] salt = { (byte) 0x11, (byte) 0x23, (byte) 0x53, (byte) 0x65, |
359 |
|
(byte) 0xbc, (byte) 0xef, (byte) 0xf1, (byte) 0x34 }; |
360 |
4
|
paramSpec = new PBEParameterSpec(salt, 10); |
361 |
4
|
return paramSpec; |
362 |
|
} |
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
@param |
368 |
|
|
|
|
| 50% |
Uncovered Elements: 6 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
369 |
4
|
private SecretKey generateSecretKey(String password){... |
370 |
4
|
SecretKey sk = null; |
371 |
4
|
try { |
372 |
4
|
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); |
373 |
4
|
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); |
374 |
4
|
sk = kf.generateSecret(keySpec); |
375 |
|
} catch (Exception e){ |
376 |
0
|
this.showMessage("Error when generating PBE TCBSecretKey!", |
377 |
|
SAWSTextOutputCallback.ERROR); |
378 |
|
|
379 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo){ |
380 |
|
|
381 |
0
|
sawsDebugLog.write(e.toString()); |
382 |
|
} |
383 |
0
|
return null; |
384 |
|
} |
385 |
4
|
return sk; |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
@param |
393 |
|
|
394 |
|
@return |
395 |
|
|
|
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
396 |
14
|
private String generateRandomPW(String password){... |
397 |
14
|
java.security.MessageDigest firstHash = null; |
398 |
14
|
try{ |
399 |
14
|
firstHash = java.security.MessageDigest.getInstance("SHA1"); |
400 |
|
} catch (Exception e2) { |
401 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
402 |
|
|
403 |
0
|
sawsDebugLog.write(e2.toString()); |
404 |
|
} |
405 |
|
|
406 |
14
|
byte[] sawsHash = null; |
407 |
|
|
408 |
14
|
String temp = password; |
409 |
154
|
for (int i=0; i<10; ++i){ |
410 |
140
|
sawsHash = firstHash.digest(temp.getBytes()); |
411 |
140
|
temp = new String(sawsHash); |
412 |
|
} |
413 |
14
|
String pw = new sun.misc.BASE64Encoder().encode(sawsHash); |
414 |
|
|
415 |
14
|
return pw; |
416 |
|
} |
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
@param |
457 |
|
@param |
458 |
|
|
459 |
|
@return |
460 |
|
|
461 |
|
|
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
|
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
|
481 |
|
|
482 |
|
|
483 |
|
|
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
@param |
489 |
|
@param |
490 |
|
|
491 |
|
@return |
492 |
|
|
|
|
| 52.6% |
Uncovered Elements: 37 (78) |
Complexity: 15 |
Complexity Density: 0.24 |
|
493 |
2
|
private boolean createKeystore(String keystoreLocation, String pw, CertificateData certificateData) {... |
494 |
2
|
KeyPairGenerator kpg = null; |
495 |
2
|
X509Certificate cert = null; |
496 |
2
|
KeyPair kp = null; |
497 |
2
|
KeyStore ks = null; |
498 |
2
|
Security.addProvider(new BouncyCastleProvider()); |
499 |
2
|
BufferedOutputStream bos = null; |
500 |
2
|
boolean created = false; |
501 |
|
|
502 |
2
|
try { |
503 |
2
|
kpg = KeyPairGenerator.getInstance(certificateData.getAlgorithm()); |
504 |
2
|
kpg.initialize(certificateData.getKeySize()); |
505 |
2
|
kp = kpg.genKeyPair(); |
506 |
|
} catch (NoSuchAlgorithmException e) { |
507 |
0
|
this.showMessage("Invalid encryption algorithm for key pair generation.", SAWSTextOutputCallback.ERROR); |
508 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
509 |
0
|
this.sawsDebugLog.write("Invalid encryption algorithm for key pair generation."); |
510 |
|
} |
511 |
0
|
return false; |
512 |
|
} |
513 |
|
|
514 |
2
|
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator(); |
515 |
2
|
X500Principal dn = new X500Principal(certificateData.toSubjectName()); |
516 |
2
|
certGen.setSerialNumber(new BigInteger("1")); |
517 |
2
|
certGen.setIssuerDN(dn); |
518 |
2
|
certGen.setNotBefore(new Date()); |
519 |
|
|
520 |
2
|
Calendar c = Calendar.getInstance(); |
521 |
2
|
c.setLenient(false); |
522 |
2
|
c.add(Calendar.DATE, certificateData.getValidity()); |
523 |
|
|
524 |
2
|
certGen.setNotAfter(c.getTime()); |
525 |
2
|
c = null; |
526 |
2
|
certGen.setSubjectDN(dn); |
527 |
2
|
certGen.setPublicKey(kp.getPublic()); |
528 |
2
|
certGen.setSignatureAlgorithm("SHA1with" + certificateData.getAlgorithm()); |
529 |
|
|
530 |
|
|
531 |
2
|
try { |
532 |
2
|
cert = certGen.generate(kp.getPrivate(), "BC"); |
533 |
|
} catch (Exception e) { |
534 |
|
|
535 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
536 |
0
|
this.sawsDebugLog.write("Error creating self-signed certificate. " + e); |
537 |
|
} |
538 |
0
|
return false; |
539 |
|
} |
540 |
|
|
541 |
2
|
try { |
542 |
2
|
if (cert != null) { |
543 |
2
|
ks = KeyStore.getInstance("JKS"); |
544 |
2
|
try { |
545 |
2
|
ks.load(null, null); |
546 |
|
} catch (Exception e) { |
547 |
|
|
548 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
549 |
0
|
this.sawsDebugLog.write("Error creating keystore: Keystore instance could not be initialized."); |
550 |
|
} |
551 |
0
|
return false; |
552 |
|
} |
553 |
2
|
ks.setCertificateEntry("saws", cert); |
554 |
2
|
Certificate[] chain = {cert}; |
555 |
2
|
ks.setKeyEntry("saws", kp.getPrivate(), pw.toCharArray(), chain); |
556 |
|
} else { |
557 |
|
|
558 |
0
|
if (this.debugLevel >= SAWSConstant.WarningInfo) { |
559 |
0
|
this.sawsDebugLog.write("Public key certificate could not be created."); |
560 |
|
} |
561 |
0
|
return false; |
562 |
|
} |
563 |
|
} catch (KeyStoreException e) { |
564 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
565 |
0
|
this.sawsDebugLog.write("Error creating keystore: " + e.getMessage()); |
566 |
|
} |
567 |
0
|
return false; |
568 |
|
} |
569 |
|
|
570 |
2
|
try { |
571 |
2
|
bos = new BufferedOutputStream(new FileOutputStream(new File(keystoreLocation))); |
572 |
|
} catch (FileNotFoundException e) { |
573 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
574 |
0
|
this.sawsDebugLog.write("Error creating keystore: file was not created or not found."); |
575 |
|
} |
576 |
0
|
return false; |
577 |
|
} |
578 |
|
|
579 |
2
|
try { |
580 |
2
|
ks.store(bos, pw.toCharArray()); |
581 |
2
|
created = true; |
582 |
|
} catch (Exception e) { |
583 |
|
|
584 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
585 |
0
|
this.sawsDebugLog.write("Error writing keystore to file: " + e.getMessage()); |
586 |
|
} |
587 |
0
|
return false; |
588 |
|
} |
589 |
2
|
return created; |
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
|
|
594 |
|
|
595 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
596 |
0
|
public void outputPKCRequest(){... |
597 |
0
|
signingKeyfile = new File( signingKeystoreLocation); |
598 |
0
|
if ( !signingKeyfile.exists() ) { |
599 |
0
|
this.showMessage("The SAWS signing keystore doesn't exist. Please first use SAWS to create a signing keystore. ", |
600 |
|
SAWSTextOutputCallback.WARNING); |
601 |
|
|
602 |
0
|
System.exit(-1); |
603 |
|
} |
604 |
|
|
605 |
0
|
sawsPW = getSAWSPassword3Attempts(signingKeystoreLocation, numberOfPasswordShares, "signing", false); |
606 |
0
|
if (sawsPW == null ){ |
607 |
0
|
this.showMessage("The password to the signing keystore is wrong. SAWS will stop.", |
608 |
|
SAWSTextOutputCallback.WARNING); |
609 |
|
|
610 |
0
|
System.exit(-1); |
611 |
|
} |
612 |
|
|
613 |
0
|
boolean b1 = createPKCReqest(signingKeystoreLocation, sawsPW); |
614 |
0
|
if (b1) { |
615 |
0
|
this.showMessage("The SAWS PKC request file sawsRequest.csr in the " |
616 |
|
+ "current directory has been created successfully." |
617 |
|
+ "\nPlease pass it to a RootCA for issuing a PKC.", |
618 |
|
SAWSTextOutputCallback.INFORMATION); |
619 |
|
|
620 |
|
|
621 |
|
} else { |
622 |
0
|
this.showMessage("There is something wrong with creating the SAWS PKC request file. ", |
623 |
|
SAWSTextOutputCallback.WARNING); |
624 |
|
|
625 |
|
} |
626 |
|
|
627 |
|
|
628 |
|
} |
629 |
|
|
630 |
|
|
631 |
|
|
632 |
|
|
633 |
|
|
634 |
|
@param |
635 |
|
@param |
636 |
|
@return |
637 |
|
|
|
|
| 0% |
Uncovered Elements: 37 (37) |
Complexity: 7 |
Complexity Density: 0.23 |
|
638 |
0
|
private boolean createPKCReqest(String keystoreLocation, String pw) {... |
639 |
0
|
boolean created = false; |
640 |
0
|
Security.addProvider(new BouncyCastleProvider()); |
641 |
0
|
KeyStore ks = null; |
642 |
|
|
643 |
|
|
644 |
0
|
try { |
645 |
0
|
ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
646 |
0
|
ks.load(new BufferedInputStream(new FileInputStream(keystoreLocation)), pw.toCharArray()); |
647 |
|
} catch (Exception e) { |
648 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
649 |
0
|
this.sawsDebugLog.write("Error reading keystore file when creating PKC request: " + e.getMessage()); |
650 |
|
} |
651 |
0
|
return false; |
652 |
|
} |
653 |
0
|
Certificate cert = null; |
654 |
0
|
try { |
655 |
0
|
cert = ks.getCertificate("saws"); |
656 |
|
} catch (KeyStoreException e) { |
657 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
658 |
0
|
this.sawsDebugLog.write("Error reading certificate from keystore file when creating PKC request: " + e.getMessage()); |
659 |
|
} |
660 |
0
|
return false; |
661 |
|
} |
662 |
0
|
PKCS10CertificationRequest request = null; |
663 |
0
|
try { |
664 |
0
|
request = |
665 |
|
new PKCS10CertificationRequest( |
666 |
|
"SHA1withRSA", new X500Principal(((X509Certificate)cert).getSubjectDN().toString()), |
667 |
|
cert.getPublicKey(), null, (PrivateKey)ks.getKey("saws", pw.toCharArray())); |
668 |
|
|
669 |
0
|
byte buf[] = request.getEncoded(); |
670 |
|
|
671 |
0
|
StringBuffer sb = new StringBuffer("-----BEGIN NEW CERTIFICATE REQUEST-----\n"); |
672 |
0
|
sb.append(new sun.misc.BASE64Encoder().encode(buf)); |
673 |
0
|
sb.append("\n-----END NEW CERTIFICATE REQUEST-----\n"); |
674 |
|
|
675 |
0
|
OutputStreamWriter wr = new OutputStreamWriter(new FileOutputStream("sawsRequest.csr")); |
676 |
0
|
wr.write(new String(sb)); |
677 |
0
|
wr.flush(); |
678 |
0
|
wr.close(); |
679 |
|
|
680 |
0
|
created = true; |
681 |
|
|
682 |
|
} catch (Exception e) { |
683 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
684 |
0
|
this.sawsDebugLog.write("Error creating PKC request file: " + e.getMessage()); |
685 |
|
} |
686 |
0
|
return false; |
687 |
|
} |
688 |
|
|
689 |
0
|
return created; |
690 |
|
} |
691 |
|
|
692 |
|
|
693 |
|
|
694 |
|
|
695 |
|
|
696 |
|
|
697 |
|
|
698 |
|
|
699 |
|
|
700 |
|
|
701 |
|
|
702 |
|
|
703 |
|
|
704 |
|
|
705 |
|
|
706 |
|
|
707 |
|
|
708 |
|
|
709 |
|
|
710 |
|
|
711 |
|
|
712 |
|
|
713 |
|
|
714 |
|
|
|
|
| 55.6% |
Uncovered Elements: 8 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
715 |
1
|
public void exportSigningPKC() {... |
716 |
1
|
signingKeyfile = new File( signingKeystoreLocation); |
717 |
1
|
if ( !signingKeyfile.exists() ) { |
718 |
0
|
this.showMessage("The SAWS signing keystore doesn't exist. Please first use SAWS to create a signing keystore. ", |
719 |
|
SAWSTextOutputCallback.WARNING); |
720 |
|
|
721 |
0
|
System.exit(-1); |
722 |
|
} |
723 |
|
|
724 |
1
|
sawsPW = getSAWSPassword3Attempts(signingKeystoreLocation, numberOfPasswordShares, "signing", false); |
725 |
1
|
if (sawsPW == null ){ |
726 |
0
|
this.showMessage("The password to the signing keystore is wrong. SAWS will stop.", |
727 |
|
SAWSTextOutputCallback.WARNING); |
728 |
|
|
729 |
0
|
System.exit(-1); |
730 |
|
} |
731 |
|
|
732 |
1
|
boolean b1 = exportPKC(signingKeystoreLocation, sawsPW); |
733 |
1
|
if (b1) { |
734 |
1
|
this.showMessage("The SAWS Signing PKC file sawsSigningPKC.crt in the current directory has been exported successfully.", |
735 |
|
SAWSTextOutputCallback.INFORMATION); |
736 |
|
|
737 |
|
} else { |
738 |
0
|
this.showMessage("There is something wrong with exporting the SAWS Signing PKC file.", |
739 |
|
SAWSTextOutputCallback.WARNING); |
740 |
|
|
741 |
|
} |
742 |
|
} |
743 |
|
|
744 |
|
|
745 |
|
|
746 |
|
|
747 |
|
@param |
748 |
|
@param |
749 |
|
@return |
750 |
|
|
|
|
| 54.5% |
Uncovered Elements: 15 (33) |
Complexity: 7 |
Complexity Density: 0.26 |
|
751 |
1
|
private boolean exportPKC(String keystoreLocation, String pw) {... |
752 |
1
|
boolean created = false; |
753 |
1
|
KeyStore ks = null; |
754 |
|
|
755 |
|
|
756 |
1
|
try { |
757 |
1
|
ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
758 |
1
|
ks.load(new BufferedInputStream(new FileInputStream(keystoreLocation)), pw.toCharArray()); |
759 |
|
} catch (Exception e) { |
760 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
761 |
0
|
this.sawsDebugLog.write("Error reading keystore file when exporting PKC: " + e.getMessage()); |
762 |
|
} |
763 |
0
|
return false; |
764 |
|
} |
765 |
1
|
Certificate cert = null; |
766 |
1
|
try { |
767 |
1
|
cert = ks.getCertificate("saws"); |
768 |
|
} catch (KeyStoreException e) { |
769 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
770 |
0
|
this.sawsDebugLog.write("Error reading certificate from keystore file when exporting PKC: " + e.getMessage()); |
771 |
|
} |
772 |
0
|
return false; |
773 |
|
} |
774 |
|
|
775 |
1
|
try { |
776 |
1
|
StringBuffer sb = new StringBuffer("-----BEGIN CERTIFICATE-----\n"); |
777 |
1
|
sb.append(new sun.misc.BASE64Encoder().encode(cert.getEncoded())); |
778 |
1
|
sb.append("\n-----END CERTIFICATE-----\n"); |
779 |
|
|
780 |
1
|
OutputStreamWriter wr = new OutputStreamWriter(new FileOutputStream("sawsSigningPKC.crt")); |
781 |
1
|
wr.write(new String(sb)); |
782 |
1
|
wr.flush(); |
783 |
1
|
wr.close(); |
784 |
|
|
785 |
1
|
created = true; |
786 |
|
|
787 |
|
} catch (Exception e) { |
788 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
789 |
0
|
this.sawsDebugLog.write("Error exporting PKC file: " + e.getMessage()); |
790 |
|
} |
791 |
0
|
return false; |
792 |
|
} |
793 |
|
|
794 |
1
|
return created; |
795 |
|
} |
796 |
|
|
797 |
|
|
798 |
|
|
799 |
|
|
800 |
|
|
801 |
|
|
802 |
|
|
803 |
|
|
804 |
|
|
805 |
|
|
806 |
|
|
807 |
|
|
808 |
|
|
809 |
|
|
810 |
|
|
811 |
|
|
812 |
|
|
813 |
|
|
814 |
|
|
815 |
|
|
816 |
|
|
817 |
|
|
818 |
|
|
819 |
|
|
820 |
|
|
821 |
|
|
822 |
|
|
|
|
| 0% |
Uncovered Elements: 32 (32) |
Complexity: 6 |
Complexity Density: 0.25 |
|
823 |
0
|
public void importSigningPKC(){... |
824 |
0
|
InputStreamReader is = new InputStreamReader(System.in); |
825 |
0
|
BufferedReader systemIn = new BufferedReader(is); |
826 |
|
|
827 |
0
|
this.cbs = new Callback[1]; |
828 |
0
|
this.cbs[0] = new SAWSTextInputCallback("Please input the SAWS PKC file name:", "SAWSPKCFileName"); |
829 |
|
|
830 |
0
|
try { |
831 |
0
|
this.callbackHandler.handle(this.cbs); |
832 |
|
} catch (Exception e) { |
833 |
0
|
System.err.println(e.getMessage()); |
834 |
0
|
sawsDebugLog.write(e); |
835 |
|
} |
836 |
0
|
String sIn = ((SAWSTextInputCallback)this.cbs[0]).getText(); |
837 |
0
|
if (sIn == null) { |
838 |
0
|
this.showMessage("The file name is null. Please restart SAWS and type a valid file name." |
839 |
|
+ "\nSAWS will stop.", |
840 |
|
SAWSTextOutputCallback.WARNING); |
841 |
0
|
System.exit(-1); |
842 |
|
} |
843 |
|
|
844 |
0
|
File sPKCFile = new File(sIn); |
845 |
|
|
846 |
0
|
if ( !sPKCFile.exists() ) { |
847 |
0
|
this.showMessage("This file doesn't exist. SAWS will stop. ", |
848 |
|
SAWSTextOutputCallback.WARNING); |
849 |
|
|
850 |
0
|
System.exit(-1); |
851 |
|
} |
852 |
|
|
853 |
|
|
854 |
0
|
sawsPW = getSAWSPassword3Attempts(signingKeystoreLocation, numberOfPasswordShares, "signing", false); |
855 |
0
|
if (sawsPW == null ){ |
856 |
0
|
this.showMessage("The password to the signing keystore is wrong. SAWS will stop.", |
857 |
|
SAWSTextOutputCallback.WARNING); |
858 |
|
|
859 |
0
|
System.exit(-1); |
860 |
|
} |
861 |
|
|
862 |
|
|
863 |
0
|
boolean b1 = importPKC(signingKeystoreLocation, sawsPW, sIn, "saws"); |
864 |
0
|
if (b1) { |
865 |
0
|
this.showMessage("The SAWS PKC has been imported into the signing keystore.", |
866 |
|
SAWSTextOutputCallback.INFORMATION); |
867 |
|
|
868 |
|
} else { |
869 |
0
|
this.showMessage("There is something wrong when importing the SAWS PKC into the signing keystore.", |
870 |
|
SAWSTextOutputCallback.WARNING); |
871 |
|
|
872 |
|
} |
873 |
|
} |
874 |
|
|
875 |
|
|
876 |
|
|
877 |
|
|
878 |
|
@param |
879 |
|
@param |
880 |
|
@param |
881 |
|
@param |
882 |
|
|
883 |
|
@return |
884 |
|
|
|
|
| 51.2% |
Uncovered Elements: 20 (41) |
Complexity: 10 |
Complexity Density: 0.32 |
|
885 |
1
|
private boolean importPKC(String keystoreLocation, String pw, ... |
886 |
|
String pkcFile, String alias) { |
887 |
1
|
boolean imported = false; |
888 |
1
|
KeyStore ks = null; |
889 |
|
|
890 |
|
|
891 |
1
|
try { |
892 |
1
|
ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
893 |
1
|
ks.load(new BufferedInputStream(new FileInputStream(keystoreLocation)), pw.toCharArray()); |
894 |
|
} catch (Exception e) { |
895 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
896 |
0
|
this.sawsDebugLog.write("Error reading keystore file when exporting PKC: " + e.getMessage()); |
897 |
|
} |
898 |
0
|
return false; |
899 |
|
} |
900 |
|
|
901 |
1
|
Certificate cert = null; |
902 |
1
|
try { |
903 |
1
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pkcFile)); |
904 |
|
|
905 |
1
|
CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
906 |
|
|
907 |
2
|
while (bis.available() > 0) { |
908 |
1
|
cert = cf.generateCertificate(bis); |
909 |
|
} |
910 |
|
} catch (Exception e) { |
911 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
912 |
0
|
this.sawsDebugLog.write("Error reading certificate from file when importing PKC: " + e.getMessage()); |
913 |
|
} |
914 |
0
|
return false; |
915 |
|
} |
916 |
|
|
917 |
1
|
BufferedOutputStream bos = null; |
918 |
1
|
try { |
919 |
1
|
bos = new BufferedOutputStream(new FileOutputStream(new File(keystoreLocation))); |
920 |
|
} catch (FileNotFoundException e) { |
921 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
922 |
0
|
this.sawsDebugLog.write("Error accessing key store file when importing certificate: " |
923 |
|
+ e.getMessage()); |
924 |
|
} |
925 |
0
|
return false; |
926 |
|
} |
927 |
|
|
928 |
1
|
try { |
929 |
1
|
ks.setCertificateEntry(alias, cert); |
930 |
1
|
ks.store(bos, pw.toCharArray()); |
931 |
1
|
imported = true; |
932 |
|
} catch (Exception e) { |
933 |
0
|
if (this.debugLevel >= SAWSConstant.ErrorInfo) { |
934 |
0
|
this.sawsDebugLog.write("Error writing keystore to file when importing key store: " + e.getMessage()); |
935 |
|
} |
936 |
0
|
return false; |
937 |
|
} |
938 |
|
|
939 |
1
|
return imported; |
940 |
|
} |
941 |
|
|
942 |
|
|
943 |
|
|
944 |
|
|
945 |
|
|
946 |
|
|
947 |
|
|
948 |
|
|
949 |
|
|
950 |
|
|
951 |
|
|
952 |
|
|
953 |
|
|
954 |
|
|
955 |
|
|
956 |
|
|
957 |
|
|
958 |
|
|
959 |
|
|
960 |
|
|
961 |
|
|
962 |
|
|
963 |
|
|
964 |
|
|
965 |
|
|
966 |
|
|
|
|
| 69.2% |
Uncovered Elements: 12 (39) |
Complexity: 7 |
Complexity Density: 0.24 |
|
967 |
1
|
public void createEncryptionKeystore(){... |
968 |
1
|
InputStreamReader is = new InputStreamReader(System.in); |
969 |
|
|
970 |
|
|
971 |
1
|
encryptionKeyfile = new File( encryptionKeystoreLocation); |
972 |
|
|
973 |
1
|
if ( encryptionKeyfile.exists() ) { |
974 |
|
|
975 |
1
|
sawsPW = getSAWSPasswordOnce(numberOfEncPasswordShares, "encryption", false); |
976 |
1
|
boolean tempB = checkKeystorePassword(encryptionKeystoreLocation, sawsPW ); |
977 |
|
|
978 |
1
|
String[] options = {"Create new encryption keystore", "Stop SAWS"}; |
979 |
|
|
980 |
1
|
int selection = this.createConfirmCallback( |
981 |
|
"The encryption keystore already exists, \nand the password to the encryption keystore is " + tempB + "." |
982 |
|
+ "\n\nOption 1: SAWS will create a new encryption keystore and overwrite the old one. " |
983 |
|
+ "\nOption 2: SAWS will stop.\n", options, SAWSChoiceCallback.WARNING, "ExistingEncKeystore"); |
984 |
1
|
if ( selection == 1) { |
985 |
0
|
this.showMessage("SAWS stoped.", SAWSTextOutputCallback.WARNING); |
986 |
0
|
System.exit(0); |
987 |
|
} else { |
988 |
1
|
boolean b1 = encryptionKeyfile.delete(); |
989 |
1
|
if (b1) { |
990 |
1
|
this.showMessage(encryptionKeystoreLocation + " has been deleted. ", |
991 |
|
SAWSTextOutputCallback.INFORMATION); |
992 |
|
|
993 |
|
} else { |
994 |
0
|
this.showMessage(encryptionKeystoreLocation + " can't be deleted. ", |
995 |
|
SAWSTextOutputCallback.WARNING); |
996 |
|
|
997 |
0
|
System.exit(-1); |
998 |
|
} |
999 |
|
} |
1000 |
|
} |
1001 |
1
|
sawsPW = null; |
1002 |
|
|
1003 |
|
|
1004 |
|
|
1005 |
1
|
this.cbs = new Callback[1]; |
1006 |
1
|
this.cbs[0] = new CertificateDataCallback(SAWSConstant.ENCRYPTION_PURPOSE); |
1007 |
|
|
1008 |
|
|
1009 |
1
|
try { |
1010 |
1
|
this.callbackHandler.handle(this.cbs); |
1011 |
|
} catch (Exception e) { |
1012 |
0
|
sawsDebugLog.write(e); |
1013 |
|
} |
1014 |
1
|
CertificateData cd = ((CertificateDataCallback)this.cbs[0]).getCertData(); |
1015 |
|
|
1016 |
1
|
if (cd == null) { |
1017 |
0
|
this.showMessage("The process of creating the encryption keystore has been canceled. Keystore was not created.", |
1018 |
|
SAWSTextOutputCallback.WARNING); |
1019 |
|
} else { |
1020 |
1
|
sawsPW = getSAWSPasswordOnce(numberOfEncPasswordShares, "encryption", true); |
1021 |
|
|
1022 |
1
|
boolean b1 = createKeystore(encryptionKeystoreLocation, sawsPW, cd); |
1023 |
1
|
if (b1) { |
1024 |
1
|
this.showMessage(encryptionKeystoreLocation + " has been created successfully.", |
1025 |
|
SAWSTextOutputCallback.INFORMATION); |
1026 |
|
|
1027 |
|
} else { |
1028 |
0
|
this.showMessage("There is something wrong with creating " + encryptionKeystoreLocation, |
1029 |
|
SAWSTextOutputCallback.WARNING); |
1030 |
|
|
1031 |
|
} |
1032 |
|
} |
1033 |
|
|
1034 |
|
|
1035 |
|
|
1036 |
|
|
1037 |
|
|
1038 |
|
|
1039 |
|
|
1040 |
|
|
1041 |
|
|
1042 |
|
|
1043 |
|
|
1044 |
|
|
1045 |
|
|
1046 |
|
|
1047 |
|
|
1048 |
|
|
1049 |
|
|
1050 |
|
|
1051 |
|
|
1052 |
|
|
1053 |
|
} |
1054 |
|
|
1055 |
|
|
1056 |
|
|
1057 |
|
|
|
|
| 69.2% |
Uncovered Elements: 12 (39) |
Complexity: 7 |
Complexity Density: 0.24 |
|
1058 |
1
|
public void createSigningKeystore(){... |
1059 |
1
|
InputStreamReader is = new InputStreamReader(System.in); |
1060 |
|
|
1061 |
|
|
1062 |
1
|
signingKeyfile = new File( signingKeystoreLocation); |
1063 |
|
|
1064 |
1
|
if ( signingKeyfile.exists() ) { |
1065 |
|
|
1066 |
1
|
sawsPW = getSAWSPasswordOnce(numberOfPasswordShares, "signing", false); |
1067 |
1
|
boolean tempB = checkKeystorePassword(signingKeystoreLocation, sawsPW ); |
1068 |
|
|
1069 |
1
|
String[] options = {"Create new signing keystore", "Stop SAWS"}; |
1070 |
|
|
1071 |
1
|
int selection = this.createConfirmCallback( |
1072 |
|
"The signing keystore already exists, \nand the password to the signing keystore is " + tempB + "." |
1073 |
|
+ "\n\nOption 1: SAWS will create a new signing keystore and overwrite the old one. " |
1074 |
|
+ "\nOption 2: SAWS will stop.\n", options, SAWSChoiceCallback.WARNING, "ExistingSigKeystore"); |
1075 |
|
|
1076 |
1
|
if ( selection == 1) { |
1077 |
0
|
this.showMessage("SAWS stoped.", SAWSTextOutputCallback.WARNING); |
1078 |
|
|
1079 |
0
|
System.exit(0); |
1080 |
|
} else { |
1081 |
1
|
boolean b1 = signingKeyfile.delete(); |
1082 |
1
|
if (b1) { |
1083 |
1
|
this.showMessage(signingKeystoreLocation + " has been deleted. ", |
1084 |
|
SAWSTextOutputCallback.INFORMATION); |
1085 |
|
|
1086 |
|
} else { |
1087 |
0
|
this.showMessage(signingKeystoreLocation + " can't be deleted. ", |
1088 |
|
SAWSTextOutputCallback.WARNING); |
1089 |
|
|
1090 |
0
|
System.exit(-1); |
1091 |
|
} |
1092 |
|
} |
1093 |
|
} |
1094 |
|
|
1095 |
1
|
sawsPW = null; |
1096 |
|
|
1097 |
1
|
this.cbs = new Callback[1]; |
1098 |
1
|
this.cbs[0] = new CertificateDataCallback(SAWSConstant.SIGNING_PURPOSE); |
1099 |
|
|
1100 |
|
|
1101 |
1
|
try { |
1102 |
1
|
this.callbackHandler.handle(this.cbs); |
1103 |
|
} catch (Exception e) { |
1104 |
0
|
sawsDebugLog.write(e); |
1105 |
|
} |
1106 |
1
|
CertificateData cd = ((CertificateDataCallback)this.cbs[0]).getCertData(); |
1107 |
|
|
1108 |
1
|
if (cd == null) { |
1109 |
0
|
this.showMessage("The process of creating the signing keystore has been canceled. Keystore was not created.", |
1110 |
|
SAWSTextOutputCallback.WARNING); |
1111 |
|
|
1112 |
|
} else { |
1113 |
1
|
sawsPW = getSAWSPasswordOnce(numberOfPasswordShares, "signing", true); |
1114 |
1
|
boolean b1 = createKeystore(signingKeystoreLocation, sawsPW, cd); |
1115 |
1
|
if (b1) { |
1116 |
1
|
this.showMessage(signingKeystoreLocation + " has been created successfully.", |
1117 |
|
SAWSTextOutputCallback.INFORMATION); |
1118 |
|
|
1119 |
|
} else { |
1120 |
0
|
this.showMessage("There is something wrong with creating " + signingKeystoreLocation, |
1121 |
|
SAWSTextOutputCallback.WARNING); |
1122 |
|
|
1123 |
|
} |
1124 |
|
} |
1125 |
|
|
1126 |
|
|
1127 |
|
|
1128 |
|
|
1129 |
|
|
1130 |
|
|
1131 |
|
|
1132 |
|
|
1133 |
|
|
1134 |
|
|
1135 |
|
|
1136 |
|
|
1137 |
|
|
1138 |
|
|
1139 |
|
|
1140 |
|
|
1141 |
|
|
1142 |
|
|
1143 |
|
|
1144 |
|
|
1145 |
|
|
1146 |
|
} |
1147 |
|
|
1148 |
|
|
1149 |
|
|
1150 |
|
|
1151 |
|
|
1152 |
|
|
|
|
| 64.3% |
Uncovered Elements: 5 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
1153 |
1
|
public void importRootCA() {... |
1154 |
|
|
1155 |
1
|
InputStreamReader is = new InputStreamReader(System.in); |
1156 |
1
|
BufferedReader systemIn = new BufferedReader(is); |
1157 |
|
|
1158 |
|
|
1159 |
1
|
sawsPW = getSAWSPassword3Attempts(signingKeystoreLocation, numberOfPasswordShares, "signing", false); |
1160 |
1
|
if (sawsPW == null ){ |
1161 |
0
|
this.showMessage("The password to the signing keystore is wrong. SAWS will stop.", |
1162 |
|
SAWSTextOutputCallback.WARNING); |
1163 |
|
|
1164 |
0
|
System.exit(-1); |
1165 |
|
} |
1166 |
|
|
1167 |
1
|
boolean imported = this.importPKC(signingKeystoreLocation, sawsPW, rootCA, "rootca"); |
1168 |
|
|
1169 |
1
|
if (imported) { |
1170 |
1
|
this.showMessage("The root certificate has been imported successfully.", |
1171 |
|
SAWSTextOutputCallback.INFORMATION); |
1172 |
|
} else { |
1173 |
0
|
this.showMessage("There is something wrong when importing the SAWS root CA certificate into the signing keystore." |
1174 |
|
+ "\nPlease check the path for the root certificate in the configuration file (saws.xml).", |
1175 |
|
SAWSTextOutputCallback.WARNING); |
1176 |
|
} |
1177 |
|
|
1178 |
|
|
1179 |
|
|
1180 |
|
|
1181 |
|
|
1182 |
|
|
1183 |
|
|
1184 |
|
|
1185 |
|
|
1186 |
|
|
1187 |
|
|
1188 |
|
|
1189 |
|
|
1190 |
|
|
1191 |
|
|
1192 |
|
|
1193 |
|
|
1194 |
|
} |
1195 |
|
|
1196 |
|
|
1197 |
|
|
1198 |
|
|
1199 |
|
|
1200 |
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 7 |
Complexity Density: 0.32 |
|
1201 |
0
|
public void listSigningKeystore(){... |
1202 |
0
|
InputStreamReader is = new InputStreamReader(System.in); |
1203 |
0
|
BufferedReader systemIn = new BufferedReader(is); |
1204 |
|
|
1205 |
|
|
1206 |
0
|
sawsPW = getSAWSPassword3Attempts(signingKeystoreLocation, numberOfPasswordShares, "signing", false); |
1207 |
0
|
if (sawsPW == null ){ |
1208 |
0
|
this.showMessage("The password to the signing keystore is wrong. SAWS will stop.", |
1209 |
|
SAWSTextOutputCallback.WARNING); |
1210 |
|
|
1211 |
0
|
System.exit(-1); |
1212 |
|
} |
1213 |
|
|
1214 |
0
|
try { |
1215 |
|
|
1216 |
|
|
1217 |
|
|
1218 |
|
|
1219 |
|
|
1220 |
0
|
KeyStore ks = null; |
1221 |
0
|
try { |
1222 |
0
|
ks = KeyStore.getInstance("JKS"); |
1223 |
|
} catch (KeyStoreException kse) { |
1224 |
0
|
this.showMessage("Fail when creating a keystore instance.", SAWSTextOutputCallback.ERROR); |
1225 |
|
} |
1226 |
0
|
try { |
1227 |
0
|
ks.load(new FileInputStream(signingKeystoreLocation), |
1228 |
|
sawsPW.toCharArray()); |
1229 |
|
} catch (IOException ioe) { |
1230 |
0
|
this.showMessage("Signing keystore could not be found.", SAWSTextOutputCallback.ERROR); |
1231 |
|
} |
1232 |
|
|
1233 |
0
|
StringBuffer certificates = new StringBuffer(); |
1234 |
0
|
for (Enumeration e = ks.aliases(); e.hasMoreElements() ;) { |
1235 |
0
|
String alias = (String)e.nextElement(); |
1236 |
0
|
Certificate c = ks.getCertificate(alias); |
1237 |
0
|
certificates.append("------ BEGINS Certificate: " + alias + " ------\n" |
1238 |
|
+ c.toString() + "\n------- ENDS Certificate: " + alias + " -------\n\n"); |
1239 |
|
} |
1240 |
|
|
1241 |
|
|
1242 |
|
|
1243 |
|
|
1244 |
0
|
this.showMessage(new String(certificates), |
1245 |
|
SAWSTextOutputCallback.LONG_MESSAGE); |
1246 |
|
|
1247 |
|
|
1248 |
|
|
1249 |
|
} catch (Exception err) { |
1250 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) { |
1251 |
0
|
sawsDebugLog.write(err); |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|
1255 |
|
} |
1256 |
|
|
1257 |
|
|
1258 |
|
|
1259 |
|
|
1260 |
|
|
1261 |
|
@param |
1262 |
|
@param |
1263 |
|
@param |
1264 |
|
@param |
1265 |
|
|
1266 |
|
@return |
1267 |
|
|
|
|
| 50% |
Uncovered Elements: 9 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
1268 |
10
|
private String getSAWSPassword3Attempts(String keystoreLocation, int numberOfPasswordShares, String prompt, boolean newPassword){... |
1269 |
10
|
String pass = null; |
1270 |
10
|
for (int j = 0; j<3; ++j) { |
1271 |
10
|
pass = getSAWSPasswordOnce(numberOfPasswordShares, prompt, newPassword); |
1272 |
10
|
boolean tempB = checkKeystorePassword(keystoreLocation, pass); |
1273 |
10
|
if (tempB == true) { |
1274 |
10
|
break; |
1275 |
0
|
} else if (j<2) { |
1276 |
0
|
this.showMessage("Password to the " + prompt |
1277 |
|
+ " keystore is wrong. \nPlease try your password again.", |
1278 |
|
SAWSTextOutputCallback.ERROR); |
1279 |
|
|
1280 |
0
|
continue; |
1281 |
|
} else { |
1282 |
0
|
this.showMessage("Password to the " + prompt |
1283 |
|
+ " keystore is wrong. \nSAWS will stop.", |
1284 |
|
SAWSTextOutputCallback.ERROR); |
1285 |
0
|
return null; |
1286 |
|
} |
1287 |
|
} |
1288 |
10
|
return pass; |
1289 |
|
} |
1290 |
|
|
1291 |
|
|
1292 |
|
|
1293 |
|
|
1294 |
|
@param |
1295 |
|
@param |
1296 |
|
@param |
1297 |
|
|
1298 |
|
@return |
1299 |
|
|
|
|
| 87.9% |
Uncovered Elements: 4 (33) |
Complexity: 6 |
Complexity Density: 0.24 |
|
1300 |
14
|
private String getSAWSPasswordOnce(int numberOfPasswordShares, String prompt, boolean newPassword){... |
1301 |
14
|
String pass = null; |
1302 |
14
|
StringBuffer passBuffer = new StringBuffer(""); |
1303 |
28
|
for (int i = 1; i <= numberOfPasswordShares; ++i){ |
1304 |
14
|
SAWSPasswordCallback pc = new SAWSPasswordCallback("Please input the password for the " |
1305 |
|
+ prompt + " keystore \nfrom SAWS administrator No. " |
1306 |
|
+ i + " out of " + numberOfPasswordShares + ":", numberOfPasswordShares, i, false, prompt); |
1307 |
14
|
if (!newPassword) { |
1308 |
12
|
this.cbs = new Callback[1]; |
1309 |
12
|
this.cbs[0] = pc; |
1310 |
|
} else { |
1311 |
2
|
this.cbs = new Callback[2]; |
1312 |
2
|
this.cbs[0] = pc; |
1313 |
2
|
this.cbs[1] = new SAWSPasswordCallback("Please repeat the password for the " |
1314 |
|
+ prompt + " keystore \nfrom SAWS administrator No. " |
1315 |
|
+ i + " out of " + numberOfPasswordShares + ":", numberOfPasswordShares, i, false, prompt); |
1316 |
|
} |
1317 |
|
|
1318 |
|
|
1319 |
14
|
try { |
1320 |
14
|
this.callbackHandler.handle(cbs); |
1321 |
|
} catch (Exception e) { |
1322 |
0
|
sawsDebugLog.write(e); |
1323 |
|
} |
1324 |
|
|
1325 |
14
|
char[] pass1 = ((SAWSPasswordCallback)this.cbs[0]).getPassword(); |
1326 |
14
|
((SAWSPasswordCallback)cbs[0]).clearPassword(); |
1327 |
14
|
if (newPassword) { |
1328 |
2
|
char[] pass2 = ((SAWSPasswordCallback)this.cbs[1]).getPassword(); |
1329 |
2
|
((SAWSPasswordCallback)cbs[1]).clearPassword(); |
1330 |
2
|
if (!Arrays.equals(pass1, pass2)) { |
1331 |
0
|
this.showMessage("The password and the confirmation are not equal." |
1332 |
|
+ "\nSAWS will stop.", SAWSTextOutputCallback.ERROR); |
1333 |
0
|
System.exit(-1); |
1334 |
|
} |
1335 |
|
} |
1336 |
14
|
passBuffer.append(pass1); |
1337 |
|
} |
1338 |
|
|
1339 |
14
|
String s2 = passBuffer.toString(); |
1340 |
14
|
pass = generateRandomPW(s2); |
1341 |
14
|
return pass; |
1342 |
|
} |
1343 |
|
|
1344 |
|
|
1345 |
|
|
1346 |
|
|
1347 |
|
@param |
1348 |
|
@param |
1349 |
|
@return |
1350 |
|
|
|
|
| 53.3% |
Uncovered Elements: 7 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
1351 |
12
|
private boolean checkKeystorePassword(String keystoreLocation, String pw) { ... |
1352 |
12
|
KeyStore keystore = null; |
1353 |
12
|
FileInputStream fis = null; |
1354 |
12
|
try{ |
1355 |
12
|
fis = new FileInputStream(new File(keystoreLocation)) ; |
1356 |
12
|
keystore = KeyStore.getInstance(KeyStore.getDefaultType()); |
1357 |
12
|
keystore.load( new BufferedInputStream( fis), pw.toCharArray() ); |
1358 |
12
|
fis.close(); |
1359 |
|
} catch(Exception e) { |
1360 |
0
|
try {fis.close(); } catch (Exception e2){} |
1361 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) { |
1362 |
0
|
sawsDebugLog.write(e.toString()); |
1363 |
|
} |
1364 |
0
|
return false; |
1365 |
|
} |
1366 |
12
|
return true; |
1367 |
|
|
1368 |
|
} |
1369 |
|
|
1370 |
|
|
1371 |
|
|
1372 |
|
|
1373 |
|
|
1374 |
|
@param |
1375 |
|
@param |
1376 |
|
|
1377 |
|
|
1378 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
1379 |
6
|
private void showMessage(String message, int type) {... |
1380 |
6
|
this.cbs = new Callback[1]; |
1381 |
6
|
this.cbs[0] = new SAWSTextOutputCallback(type, message); |
1382 |
6
|
try { |
1383 |
6
|
this.callbackHandler.handle(this.cbs); |
1384 |
|
} |
1385 |
|
catch (Exception e) { |
1386 |
0
|
System.err.println(e.getMessage()); |
1387 |
0
|
sawsDebugLog.write(e); |
1388 |
|
} |
1389 |
|
} |
1390 |
|
|
1391 |
|
|
1392 |
|
|
1393 |
|
|
1394 |
|
|
1395 |
|
@param |
1396 |
|
@param |
1397 |
|
@param |
1398 |
|
|
1399 |
|
@return |
1400 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
1401 |
2
|
private int createConfirmCallback(String prompt, String[] options, int type, String key) {... |
1402 |
2
|
this.cbs = new Callback[1]; |
1403 |
2
|
this.cbs[0] = new SAWSChoiceCallback(prompt, options, key, 0, type); |
1404 |
2
|
try { |
1405 |
2
|
this.callbackHandler.handle(cbs); |
1406 |
|
} |
1407 |
|
catch (Exception e) { |
1408 |
0
|
System.err.println(e.getMessage()); |
1409 |
0
|
sawsDebugLog.write(e); |
1410 |
|
} |
1411 |
|
|
1412 |
2
|
return ((SAWSChoiceCallback)this.cbs[0]).getSelectedIndex(); |
1413 |
|
} |
1414 |
|
|
1415 |
|
} |