Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check for a trusted CA of the TSA certificate #683

Merged
merged 4 commits into from
Aug 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions core/src/main/java/net/sourceforge/jnlp/tools/JarCertVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,22 @@ VerifyResult verifyJarEntryCerts(final String jarPath, final boolean jarHasManif
final ZonedDateTime notBefore = zonedDateTime(((X509Certificate) cert).getNotBefore());
final ZonedDateTime notAfter = zonedDateTime(((X509Certificate) cert).getNotAfter());

final Optional<Timestamp> optionalSignatureTimestamp = Optional.ofNullable(codeSigners.get(certPath))
final Optional<Timestamp> optionalTsa = Optional.ofNullable(codeSigners.get(certPath))
.map(CodeSigner::getTimestamp);
final X509Certificate tsaCertificate = (X509Certificate) optionalSignatureTimestamp
final X509Certificate tsaCertificate = (X509Certificate) optionalTsa
.map(Timestamp::getSignerCertPath)
.map(CertPath::getCertificates)
.filter(certs -> !certs.isEmpty())
.map(certs -> certs.get(0))
.filter(c -> c instanceof X509Certificate)
.orElse(null);

if (tsaCertificate != null) {
if (tsaCertificate != null && isTrustedTsa(optionalTsa.map(Timestamp::getSignerCertPath).orElseThrow(() -> new RuntimeException("cert path is null even tough we have a tsa certificate")))) {
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved
final ZonedDateTime tsaNotBefore = zonedDateTime(tsaCertificate.getNotBefore());
final ZonedDateTime tsaNotAfter = zonedDateTime(tsaCertificate.getNotAfter());
final ZonedDateTime signedAt = zonedDateTime(optionalSignatureTimestamp
final ZonedDateTime signedAt = zonedDateTime(optionalTsa.map(Timestamp::getTimestamp)
.orElseThrow(() -> new RuntimeException("timestamp is null even tough we have a tsa certificate"))
.getTimestamp());
);

if (signedAt.isBefore(tsaNotBefore) || now.isBefore(tsaNotBefore)) {
certInfo.setNotYetValidCert();
Expand Down Expand Up @@ -415,6 +415,24 @@ VerifyResult verifyJarEntryCerts(final String jarPath, final boolean jarHasManif
return result;
}

private boolean isTrustedTsa(CertPath certPath) {
try {
final List<KeyStore> caKeyStores = KeyStores.getCAKeyStores();
// Check entire cert path for a trusted CA
for (final Certificate c : certPath.getCertificates()) {
if (c instanceof X509Certificate) {
final X509Certificate x509 = (X509Certificate) c;
if (CertificateUtils.inKeyStores(x509, caKeyStores)) {
return true;
}
}
}
} catch (RuntimeException e) {
LOG.warn("Unable to read through CA cert store files.");
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
}

private VerifyResult verifySigners(final Map<CertPath, Integer> jarSignCount) {
for (CertPath entryCertPath : jarSignCount.keySet()) {
if (certs.containsKey(entryCertPath) && !certs.get(entryCertPath).hasSigningIssues()) {
Expand Down