Skip to content

Commit 5eb5d7f

Browse files
committed
Make key store types extensible conveniently
1 parent 6acd186 commit 5eb5d7f

37 files changed

+1967
-1125
lines changed

jsign-cli/src/test/java/net/jsign/JsignCLITest.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class JsignCLITest {
5555
private JsignCLI cli;
5656
private File sourceFile = new File("target/test-classes/wineyes.exe");
5757
private File targetFile = new File("target/test-classes/wineyes-signed-with-cli.exe");
58-
58+
5959
private String keystore = "keystore.jks";
6060
private String alias = "test";
6161
private String keypass = "password";
@@ -65,12 +65,12 @@ public class JsignCLITest {
6565
@Before
6666
public void setUp() throws Exception {
6767
cli = new JsignCLI();
68-
68+
6969
// remove the files signed previously
7070
if (targetFile.exists()) {
7171
assertTrue("Unable to remove the previously signed file", targetFile.delete());
7272
}
73-
73+
7474
assertEquals("Source file CRC32", SOURCE_FILE_CRC32, FileUtils.checksumCRC32(sourceFile));
7575
Thread.sleep(100);
7676
FileUtils.copyFile(sourceFile, targetFile);
@@ -219,7 +219,7 @@ public void testSigningMultipleFiles() throws Exception {
219219
public void testSigningMultipleFilesWithListFile() throws Exception {
220220
File listFile = new File("target/test-classes/files.txt");
221221
Files.write(listFile.toPath(), Arrays.asList("# first file", '"' + targetFile.getPath() + '"', " ", "# second file", targetFile.getAbsolutePath()));
222-
222+
223223
cli.execute("--name=WinEyes", "--url=http://www.steelblue.com/WinEyes", "--alg=SHA-1", "--keystore=target/test-classes/keystores/" + keystore, "--keypass=" + keypass, "@" + listFile);
224224

225225
assertTrue("The file " + targetFile + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile));
@@ -271,7 +271,7 @@ public void testSigningPowerShell() throws Exception {
271271
File sourceFile = new File("target/test-classes/hello-world.ps1");
272272
File targetFile = new File("target/test-classes/hello-world-signed-with-cli.ps1");
273273
FileUtils.copyFile(sourceFile, targetFile);
274-
274+
275275
cli.execute("--alg=SHA-1", "--replace", "--encoding=ISO-8859-1", "--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass, "" + targetFile);
276276

277277
PowerShellScript script = new PowerShellScript(targetFile);
@@ -284,7 +284,7 @@ public void testSigningPowerShellWithDefaultEncoding() throws Exception {
284284
File sourceFile = new File("target/test-classes/hello-world.ps1");
285285
File targetFile = new File("target/test-classes/hello-world-signed-with-cli.ps1");
286286
FileUtils.copyFile(sourceFile, targetFile);
287-
287+
288288
cli.execute("--alg=SHA-1", "--replace", "--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass, "" + targetFile);
289289

290290
PowerShellScript script = new PowerShellScript(targetFile);
@@ -297,7 +297,7 @@ public void testSigningMSI() throws Exception {
297297
File sourceFile = new File("target/test-classes/minimal.msi");
298298
File targetFile = new File("target/test-classes/minimal-signed-with-cli.msi");
299299
FileUtils.copyFile(sourceFile, targetFile);
300-
300+
301301
cli.execute("--alg=SHA-1", "--replace", "--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass, "" + targetFile);
302302

303303
try (MSIFile file = new MSIFile(targetFile)) {
@@ -308,7 +308,7 @@ public void testSigningMSI() throws Exception {
308308
@Test
309309
public void testSigningPKCS12() throws Exception {
310310
cli.execute("--name=WinEyes", "--url=http://www.steelblue.com/WinEyes", "--alg=SHA-256", "--keystore=target/test-classes/keystores/keystore.p12", "--alias=test", "--storepass=password", "" + targetFile);
311-
311+
312312
assertTrue("The file " + targetFile + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile));
313313

314314
try (PEFile peFile = new PEFile(targetFile)) {
@@ -341,7 +341,7 @@ public void testSigningJKS() throws Exception {
341341
@Test
342342
public void testSigningPVKSPC() throws Exception {
343343
cli.execute("--url=http://www.steelblue.com/WinEyes", "--certfile=target/test-classes/keystores/jsign-test-certificate-full-chain.spc", "--keyfile=target/test-classes/keystores/privatekey-encrypted.pvk", "--storepass=password", "" + targetFile);
344-
344+
345345
assertTrue("The file " + targetFile + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile));
346346

347347
try (PEFile peFile = new PEFile(targetFile)) {
@@ -352,7 +352,7 @@ public void testSigningPVKSPC() throws Exception {
352352
@Test
353353
public void testSigningPEM() throws Exception {
354354
cli.execute("--certfile=target/test-classes/keystores/jsign-test-certificate.pem", "--keyfile=target/test-classes/keystores/privatekey.pkcs8.pem", "--keypass=password", "" + targetFile);
355-
355+
356356
assertTrue("The file " + targetFile + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile));
357357

358358
try (PEFile peFile = new PEFile(targetFile)) {
@@ -363,7 +363,7 @@ public void testSigningPEM() throws Exception {
363363
@Test
364364
public void testSigningEncryptedPEM() throws Exception {
365365
cli.execute("--certfile=target/test-classes/keystores/jsign-test-certificate.pem", "--keyfile=target/test-classes/keystores/privatekey-encrypted.pkcs1.pem", "--keypass=password", "" + targetFile);
366-
366+
367367
assertTrue("The file " + targetFile + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile));
368368

369369
try (PEFile peFile = new PEFile(targetFile)) {
@@ -373,7 +373,7 @@ public void testSigningEncryptedPEM() throws Exception {
373373

374374
@Test
375375
public void testSigningWithYubikey() throws Exception {
376-
Assume.assumeTrue("No Yubikey detected", YubiKey.isPresent());
376+
Assume.assumeTrue("No Yubikey detected", YubiKeyKeyStore.isPresent());
377377

378378
cli.execute("--storetype=YUBIKEY", "--certfile=target/test-classes/keystores/jsign-test-certificate-full-chain.spc", "--storepass=123456", "--alias=X.509 Certificate for Digital Signature", "" + targetFile, "" + targetFile);
379379
}
@@ -383,7 +383,7 @@ public void testTimestampingAuthenticode() throws Exception {
383383
File targetFile2 = new File("target/test-classes/wineyes-timestamped-with-cli-authenticode.exe");
384384
FileUtils.copyFile(sourceFile, targetFile2);
385385
cli.execute("--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass, "--tsaurl=http://timestamp.sectigo.com", "--tsmode=authenticode", "" + targetFile2);
386-
386+
387387
assertTrue("The file " + targetFile2 + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile2));
388388

389389
try (PEFile peFile = new PEFile(targetFile2)) {
@@ -416,18 +416,18 @@ public HttpFilters filterRequest(HttpRequest originalRequest) {
416416
}
417417
})
418418
.start();
419-
419+
420420
try {
421421
File targetFile2 = new File("target/test-classes/wineyes-timestamped-with-cli-rfc3161-proxy-unauthenticated.exe");
422422
FileUtils.copyFile(sourceFile, targetFile2);
423423
cli.execute("--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass,
424424
"--tsaurl=http://timestamp.sectigo.com", "--tsmode=rfc3161", "--tsretries=1", "--tsretrywait=1",
425425
"--proxyUrl=localhost:" + proxy.getListenAddress().getPort(),
426426
"" + targetFile2);
427-
427+
428428
assertTrue("The file " + targetFile2 + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile2));
429429
assertTrue("The proxy wasn't used", proxyUsed.get());
430-
430+
431431
try (PEFile peFile = new PEFile(targetFile2)) {
432432
SignatureAssert.assertSigned(peFile, SHA256);
433433
}
@@ -469,10 +469,10 @@ public String getRealm() {
469469
"--proxyUser=jsign",
470470
"--proxyPass=jsign",
471471
"" + targetFile2);
472-
472+
473473
assertTrue("The file " + targetFile2 + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile2));
474474
assertTrue("The proxy wasn't used", proxyUsed.get());
475-
475+
476476
try (PEFile peFile = new PEFile(targetFile2)) {
477477
SignatureAssert.assertSigned(peFile, SHA256);
478478
}
@@ -486,11 +486,11 @@ public void testReplaceSignature() throws Exception {
486486
File targetFile2 = new File("target/test-classes/wineyes-re-signed.exe");
487487
FileUtils.copyFile(sourceFile, targetFile2);
488488
cli.execute("--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass, "" + targetFile2);
489-
489+
490490
assertTrue("The file " + targetFile2 + " wasn't changed", SOURCE_FILE_CRC32 != FileUtils.checksumCRC32(targetFile2));
491-
491+
492492
cli.execute("--keystore=target/test-classes/keystores/" + keystore, "--alias=" + alias, "--keypass=" + keypass, "--alg=SHA-512", "--replace", "" + targetFile2);
493-
493+
494494
try (PEFile peFile = new PEFile(targetFile2)) {
495495
SignatureAssert.assertSigned(peFile, SHA512);
496496
}
@@ -526,7 +526,7 @@ public Integer getStatus() {
526526
}
527527

528528
public void checkPermission(Permission perm) { }
529-
529+
530530
public void checkPermission(Permission perm, Object context) { }
531531

532532
public void checkExit(int status) {

jsign-core/src/main/java/net/jsign/SignerHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public SignerHelper param(String key, String value) {
261261
if (value == null) {
262262
return this;
263263
}
264-
264+
265265
switch (key) {
266266
case PARAM_COMMAND: return command(value);
267267
case PARAM_KEYSTORE: return keystore(value);
@@ -328,7 +328,7 @@ private AuthenticodeSigner build() throws SignerException {
328328
} catch (KeyStoreException e) {
329329
throw new SignerException("Failed to load the keystore " + (ksparams.keystore() != null ? ksparams.keystore() : ""), e);
330330
}
331-
KeyStoreType storetype = ksparams.storetype();
331+
JsignKeyStore storetype = ksparams.storetype();
332332
Provider provider = ksparams.provider();
333333

334334
Set<String> aliases = null;
@@ -403,12 +403,12 @@ private AuthenticodeSigner build() throws SignerException {
403403
}
404404

405405
// enable timestamping with Azure Trusted Signing
406-
if (tsaurl == null && storetype == KeyStoreType.TRUSTEDSIGNING) {
406+
if ((tsaurl == null) && (storetype instanceof AzureTrustedSigningKeyStore)) {
407407
tsaurl = "http://timestamp.acs.microsoft.com/";
408408
tsmode = TimestampingMode.RFC3161.name();
409409
tsretries = 3;
410410
}
411-
411+
412412
// configure the signer
413413
return new AuthenticodeSigner(chain, privateKey)
414414
.withProgramName(name)
@@ -434,7 +434,7 @@ public void sign(File file) throws SignerException {
434434
if (!file.exists()) {
435435
throw new SignerException("The file " + file + " couldn't be found");
436436
}
437-
437+
438438
try (Signable signable = Signable.of(file, encoding)) {
439439
File detachedSignature = getDetachedSignature(file);
440440
if (detached && detachedSignature.exists()) {
@@ -638,7 +638,7 @@ private void timestamp(File file) throws SignerException {
638638
SignerId signerId = signerInformation.getSID();
639639
X509CertificateHolder certificate = (X509CertificateHolder) signature.getCertificates().getMatches(signerId).iterator().next();
640640

641-
String digestAlgorithmName = new DefaultAlgorithmNameFinder().getAlgorithmName(signerInformation.getDigestAlgorithmID());
641+
String digestAlgorithmName = new DefaultAlgorithmNameFinder().getAlgorithmName(signerInformation.getDigestAlgorithmID());
642642
String keyAlgorithmName = new DefaultAlgorithmNameFinder().getAlgorithmName(new ASN1ObjectIdentifier(signerInformation.getEncryptionAlgOID()));
643643
String name = digestAlgorithmName + "/" + keyAlgorithmName + " signature from '" + certificate.getSubject() + "'";
644644

jsign-core/src/test/java/net/jsign/PESignerTest.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private KeyStore getKeyStore() throws Exception {
6666
public void testSign() throws Exception {
6767
File sourceFile = new File("target/test-classes/wineyes.exe");
6868
File targetFile = new File("target/test-classes/wineyes-signed.exe");
69-
69+
7070
FileUtils.copyFile(sourceFile, targetFile);
7171

7272
PESigner signer = new PESigner(getKeyStore(), ALIAS, PRIVATE_KEY_PASSWORD)
@@ -96,7 +96,7 @@ public void testSignWithUnknownKeyStoreEntry() {
9696
public void testSigningWithKeyAndChain() throws Exception {
9797
File sourceFile = new File("target/test-classes/wineyes.exe");
9898
File targetFile = new File("target/test-classes/wineyes-signed-key-chain.exe");
99-
99+
100100
FileUtils.copyFile(sourceFile, targetFile);
101101

102102
Certificate[] chain;
@@ -132,7 +132,7 @@ public void testSigningWithKeyAndChain() throws Exception {
132132

133133
@Test
134134
public void testSigningWithYubikey() throws Exception {
135-
Assume.assumeTrue("No Yubikey detected", YubiKey.isPresent());
135+
Assume.assumeTrue("No Yubikey detected", YubiKeyKeyStore.isPresent());
136136

137137
File sourceFile = new File("target/test-classes/wineyes.exe");
138138
File targetFile = new File("target/test-classes/wineyes-signed-yubikey.exe");
@@ -166,7 +166,7 @@ public void testNullChain() throws Exception {
166166
public void testSigningWithMismatchingKeyAndCertificate() throws Exception {
167167
File sourceFile = new File("target/test-classes/wineyes.exe");
168168
File targetFile = new File("target/test-classes/wineyes-signed-mismatching-key-certificate.exe");
169-
169+
170170
FileUtils.copyFile(sourceFile, targetFile);
171171

172172
Certificate[] chain;
@@ -202,7 +202,7 @@ public void testTimestampRFC3161() throws Exception {
202202
public void testTimestamp(TimestampingMode mode, DigestAlgorithm alg) throws Exception {
203203
File sourceFile = new File("target/test-classes/wineyes.exe");
204204
File targetFile = new File("target/test-classes/wineyes-timestamped-" + mode.name().toLowerCase() + ".exe");
205-
205+
206206
FileUtils.copyFile(sourceFile, targetFile);
207207

208208
PESigner signer = new PESigner(getKeyStore(), ALIAS, PRIVATE_KEY_PASSWORD);
@@ -234,7 +234,7 @@ public void testWithTimestamper() throws Exception {
234234
signer.withDigestAlgorithm(SHA1);
235235
signer.withTimestamping(true);
236236
signer.withTimestamper(new AuthenticodeTimestamper() {
237-
237+
238238
@Override
239239
protected CMSSignedData timestamp(DigestAlgorithm algo, byte[] encryptedDigest) throws IOException, TimestampingException {
240240
called.add(true);
@@ -257,7 +257,7 @@ protected CMSSignedData timestamp(DigestAlgorithm algo, byte[] encryptedDigest)
257257
public void testSignTwice() throws Exception {
258258
File sourceFile = new File("target/test-classes/wineyes.exe");
259259
File targetFile = new File("target/test-classes/wineyes-signed-twice.exe");
260-
260+
261261
FileUtils.copyFile(sourceFile, targetFile);
262262

263263
try (PEFile peFile = new PEFile(targetFile)) {
@@ -286,7 +286,7 @@ public void testSignTwice() throws Exception {
286286
public void testSignThreeTimes() throws Exception {
287287
File sourceFile = new File("target/test-classes/wineyes.exe");
288288
File targetFile = new File("target/test-classes/wineyes-signed-three-times.exe");
289-
289+
290290
FileUtils.copyFile(sourceFile, targetFile);
291291

292292
try (PEFile peFile = new PEFile(targetFile)) {
@@ -323,7 +323,7 @@ public void testSignThreeTimes() throws Exception {
323323
public void testReplaceSignature() throws Exception {
324324
File sourceFile = new File("target/test-classes/wineyes.exe");
325325
File targetFile = new File("target/test-classes/wineyes-re-signed.exe");
326-
326+
327327
FileUtils.copyFile(sourceFile, targetFile);
328328

329329
try (PEFile peFile = new PEFile(targetFile)) {
@@ -359,16 +359,16 @@ public void testInvalidRFC3161TimestampingAuthority() throws Exception {
359359
public void testInvalidTimestampingAuthority(TimestampingMode mode) throws Exception {
360360
File sourceFile = new File("target/test-classes/wineyes.exe");
361361
File targetFile = new File("target/test-classes/wineyes-timestamped-unavailable-" + mode.name().toLowerCase() + ".exe");
362-
362+
363363
FileUtils.copyFile(sourceFile, targetFile);
364-
364+
365365
PESigner signer = new PESigner(getKeyStore(), ALIAS, PRIVATE_KEY_PASSWORD);
366366
signer.withDigestAlgorithm(SHA1);
367367
signer.withTimestamping(true);
368368
signer.withTimestampingMode(mode);
369369
signer.withTimestampingAuthority("http://www.google.com/" + mode.name().toLowerCase());
370370
signer.withTimestampingRetries(1);
371-
371+
372372
try (PEFile peFile = new PEFile(targetFile)) {
373373
Exception e = assertThrows(TimestampingException.class, () -> signer.sign(peFile));
374374
assertTrue("Missing suppressed IOException", e.getSuppressed() != null && e.getSuppressed().length > 0 && e.getSuppressed()[0].getClass().equals(IOException.class));
@@ -390,16 +390,16 @@ public void testBrokenRFC3161TimestampingAuthority() throws Exception {
390390
public void testBrokenTimestampingAuthority(TimestampingMode mode) throws Exception {
391391
File sourceFile = new File("target/test-classes/wineyes.exe");
392392
File targetFile = new File("target/test-classes/wineyes-timestamped-broken-" + mode.name().toLowerCase() + ".exe");
393-
393+
394394
FileUtils.copyFile(sourceFile, targetFile);
395-
395+
396396
PESigner signer = new PESigner(getKeyStore(), ALIAS, PRIVATE_KEY_PASSWORD);
397397
signer.withDigestAlgorithm(SHA1);
398398
signer.withTimestamping(true);
399399
signer.withTimestampingMode(mode);
400400
signer.withTimestampingAuthority("http://github.com");
401401
signer.withTimestampingRetries(1);
402-
402+
403403
try (PEFile peFile = new PEFile(targetFile)) {
404404
assertThrows(TimestampingException.class, () -> signer.sign(peFile));
405405
}
@@ -434,7 +434,7 @@ public void testRFC3161TimestampingFailover() throws Exception {
434434
public void testTimestampingFailover(TimestampingMode mode, String validURL) throws Exception {
435435
File sourceFile = new File("target/test-classes/wineyes.exe");
436436
File targetFile = new File("target/test-classes/wineyes-timestamped-failover-" + mode.name().toLowerCase() + ".exe");
437-
437+
438438
FileUtils.copyFile(sourceFile, targetFile);
439439

440440
PESigner signer = new PESigner(getKeyStore(), ALIAS, PRIVATE_KEY_PASSWORD);
@@ -490,7 +490,7 @@ public void testWithSignatureAlgorithmSHA1withRSA() throws Exception {
490490
@Test
491491
public void testWithSignatureAlgorithmSHA256withRSAandMGF1() throws Exception {
492492
Security.addProvider(new BouncyCastleProvider());
493-
493+
494494
File sourceFile = new File("target/test-classes/wineyes.exe");
495495
File targetFile = new File("target/test-classes/wineyes-signed.exe");
496496

0 commit comments

Comments
 (0)