Skip to content

Commit e9ba915

Browse files
committed
8275887: jarsigner prints invalid digest/signature algorithm warnings if keysize is weak/disabled
Reviewed-by: mdoerr Backport-of: 6bc6980a7d975dbe06b319bf6ac9625749663060
1 parent 2f0f34c commit e9ba915

File tree

9 files changed

+95
-83
lines changed

9 files changed

+95
-83
lines changed

src/java.base/share/classes/sun/security/pkcs/SignerInfo.java

+26-11
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,21 @@ public class SignerInfo implements DerEncoder {
8484
/**
8585
* A map containing the algorithms in this SignerInfo. This is used to
8686
* avoid checking algorithms to see if they are disabled more than once.
87-
* The key is the AlgorithmId of the algorithm, and the value is the name of
88-
* the field or attribute.
87+
* The key is the AlgorithmId of the algorithm, and the value is a record
88+
* containing the name of the field or attribute and whether the key
89+
* should also be checked (ex: if it is a signature algorithm).
8990
*/
90-
private Map<AlgorithmId, String> algorithms = new HashMap<>();
91+
private class AlgorithmInfo {
92+
String field;
93+
boolean checkKey;
94+
private AlgorithmInfo(String f, boolean cK) {
95+
field = f;
96+
checkKey = cK;
97+
}
98+
String field() { return field; }
99+
boolean checkKey() { return checkKey; }
100+
}
101+
private Map<AlgorithmId, AlgorithmInfo> algorithms = new HashMap<>();
91102

92103
public SignerInfo(X500Name issuerName,
93104
BigInteger serial,
@@ -323,7 +334,8 @@ SignerInfo verify(PKCS7 block, byte[] data)
323334
}
324335

325336
String digestAlgName = digestAlgorithmId.getName();
326-
algorithms.put(digestAlgorithmId, "SignerInfo digestAlgorithm field");
337+
algorithms.put(digestAlgorithmId,
338+
new AlgorithmInfo("SignerInfo digestAlgorithm field", false));
327339

328340
byte[] dataSigned;
329341

@@ -382,7 +394,8 @@ SignerInfo verify(PKCS7 block, byte[] data)
382394
new AlgorithmId(oid,
383395
digestEncryptionAlgorithmId.getParameters());
384396
algorithms.put(sigAlgId,
385-
"SignerInfo digestEncryptionAlgorithm field");
397+
new AlgorithmInfo(
398+
"SignerInfo digestEncryptionAlgorithm field", true));
386399
} catch (NoSuchAlgorithmException ignore) {
387400
}
388401

@@ -569,7 +582,8 @@ private void verifyTimestamp(TimestampToken token)
569582
throws NoSuchAlgorithmException, SignatureException {
570583

571584
AlgorithmId digestAlgId = token.getHashAlgorithm();
572-
algorithms.put(digestAlgId, "TimestampToken digestAlgorithm field");
585+
algorithms.put(digestAlgId,
586+
new AlgorithmInfo("TimestampToken digestAlgorithm field", false));
573587

574588
MessageDigest md = MessageDigest.getInstance(digestAlgId.getName());
575589

@@ -626,18 +640,19 @@ public String toString() {
626640
*/
627641
public static Set<String> verifyAlgorithms(SignerInfo[] infos,
628642
JarConstraintsParameters params, String name) throws SignatureException {
629-
Map<AlgorithmId, String> algorithms = new HashMap<>();
643+
Map<AlgorithmId, AlgorithmInfo> algorithms = new HashMap<>();
630644
for (SignerInfo info : infos) {
631645
algorithms.putAll(info.algorithms);
632646
}
633647

634648
Set<String> enabledAlgorithms = new HashSet<>();
635649
try {
636-
for (Map.Entry<AlgorithmId, String> algorithm : algorithms.entrySet()) {
637-
params.setExtendedExceptionMsg(name, algorithm.getValue());
638-
AlgorithmId algId = algorithm.getKey();
650+
for (var algEntry : algorithms.entrySet()) {
651+
AlgorithmInfo info = algEntry.getValue();
652+
params.setExtendedExceptionMsg(name, info.field());
653+
AlgorithmId algId = algEntry.getKey();
639654
JAR_DISABLED_CHECK.permits(algId.getName(),
640-
algId.getParameters(), params);
655+
algId.getParameters(), params, info.checkKey());
641656
enabledAlgorithms.add(algId.getName());
642657
}
643658
} catch (CertPathValidatorException e) {

src/java.base/share/classes/sun/security/provider/certpath/AlgorithmChecker.java

+3-28
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.security.AlgorithmParameters;
3939
import java.security.GeneralSecurityException;
4040
import java.security.cert.Certificate;
41-
import java.security.cert.X509CRL;
4241
import java.security.cert.X509Certificate;
4342
import java.security.cert.PKIXCertPathChecker;
4443
import java.security.cert.TrustAnchor;
@@ -57,7 +56,6 @@
5756
import sun.security.validator.Validator;
5857
import sun.security.x509.AlgorithmId;
5958
import sun.security.x509.X509CertImpl;
60-
import sun.security.x509.X509CRLImpl;
6159

6260
/**
6361
* A {@code PKIXCertPathChecker} implementation to check whether a
@@ -285,13 +283,13 @@ public void check(Certificate cert,
285283
// Check against local constraints if it is DisabledAlgorithmConstraints
286284
if (constraints instanceof DisabledAlgorithmConstraints) {
287285
((DisabledAlgorithmConstraints)constraints).permits(currSigAlg,
288-
currSigAlgParams, cp);
286+
currSigAlgParams, cp, true);
289287
// DisabledAlgorithmsConstraints does not check primitives, so key
290288
// additional key check.
291289

292290
} else {
293291
// Perform the default constraints checking anyway.
294-
certPathDefaultConstraints.permits(currSigAlg, currSigAlgParams, cp);
292+
certPathDefaultConstraints.permits(currSigAlg, currSigAlgParams, cp, true);
295293
// Call locally set constraints to check key with primitives.
296294
if (!constraints.permits(primitives, currPubKey)) {
297295
throw new CertPathValidatorException(
@@ -377,29 +375,6 @@ void trySetTrustAnchor(TrustAnchor anchor) {
377375
}
378376
}
379377

380-
/**
381-
* Check the signature algorithm with the specified public key.
382-
*
383-
* @param key the public key to verify the CRL signature
384-
* @param crl the target CRL
385-
* @param variant the Validator variant of the operation. A null value
386-
* passed will set it to Validator.GENERIC.
387-
* @param anchor the trust anchor selected to validate the CRL issuer
388-
*/
389-
static void check(PublicKey key, X509CRL crl, String variant,
390-
TrustAnchor anchor) throws CertPathValidatorException {
391-
392-
X509CRLImpl x509CRLImpl = null;
393-
try {
394-
x509CRLImpl = X509CRLImpl.toImpl(crl);
395-
} catch (CRLException ce) {
396-
throw new CertPathValidatorException(ce);
397-
}
398-
399-
AlgorithmId algorithmId = x509CRLImpl.getSigAlgId();
400-
check(key, algorithmId, variant, anchor);
401-
}
402-
403378
/**
404379
* Check the signature algorithm with the specified public key.
405380
*
@@ -414,7 +389,7 @@ static void check(PublicKey key, AlgorithmId algorithmId, String variant,
414389

415390
certPathDefaultConstraints.permits(algorithmId.getName(),
416391
algorithmId.getParameters(),
417-
new CertPathConstraintsParameters(key, variant, anchor));
392+
new CertPathConstraintsParameters(key, variant, anchor), true);
418393
}
419394
}
420395

src/java.base/share/classes/sun/security/provider/certpath/DistributionPointFetcher.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
688688

689689
// check the crl signature algorithm
690690
try {
691-
AlgorithmChecker.check(prevKey, crl, variant, anchor);
691+
AlgorithmChecker.check(prevKey, crlImpl.getSigAlgId(),
692+
variant, anchor);
692693
} catch (CertPathValidatorException cpve) {
693694
if (debug != null) {
694695
debug.println("CRL signature algorithm check failed: " + cpve);

src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java

+25-18
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,16 @@ public final boolean permits(Set<CryptoPrimitive> primitives,
194194
}
195195

196196
public final void permits(String algorithm, AlgorithmParameters ap,
197-
ConstraintsParameters cp) throws CertPathValidatorException {
198-
199-
permits(algorithm, cp);
197+
ConstraintsParameters cp, boolean checkKey)
198+
throws CertPathValidatorException {
199+
permits(algorithm, cp, checkKey);
200200
if (ap != null) {
201201
permits(ap, cp);
202202
}
203203
}
204204

205205
private void permits(AlgorithmParameters ap, ConstraintsParameters cp)
206-
throws CertPathValidatorException {
206+
throws CertPathValidatorException {
207207

208208
switch (ap.getAlgorithm().toUpperCase(Locale.ENGLISH)) {
209209
case "RSASSA-PSS":
@@ -221,36 +221,38 @@ private void permitsPSSParams(AlgorithmParameters ap,
221221
PSSParameterSpec pssParams =
222222
ap.getParameterSpec(PSSParameterSpec.class);
223223
String digestAlg = pssParams.getDigestAlgorithm();
224-
permits(digestAlg, cp);
224+
permits(digestAlg, cp, false);
225225
AlgorithmParameterSpec mgfParams = pssParams.getMGFParameters();
226226
if (mgfParams instanceof MGF1ParameterSpec) {
227227
String mgfDigestAlg =
228228
((MGF1ParameterSpec)mgfParams).getDigestAlgorithm();
229229
if (!mgfDigestAlg.equalsIgnoreCase(digestAlg)) {
230-
permits(mgfDigestAlg, cp);
230+
permits(mgfDigestAlg, cp, false);
231231
}
232232
}
233233
} catch (InvalidParameterSpecException ipse) {
234234
// ignore
235235
}
236236
}
237237

238-
public final void permits(String algorithm, ConstraintsParameters cp)
239-
throws CertPathValidatorException {
238+
public final void permits(String algorithm, ConstraintsParameters cp,
239+
boolean checkKey) throws CertPathValidatorException {
240240

241-
// Check if named curves in the key are disabled.
242-
for (Key key : cp.getKeys()) {
243-
for (String curve : getNamedCurveFromKey(key)) {
244-
if (!cachedCheckAlgorithm(curve)) {
245-
throw new CertPathValidatorException(
241+
if (checkKey) {
242+
// Check if named curves in the key are disabled.
243+
for (Key key : cp.getKeys()) {
244+
for (String curve : getNamedCurveFromKey(key)) {
245+
if (!cachedCheckAlgorithm(curve)) {
246+
throw new CertPathValidatorException(
246247
"Algorithm constraints check failed on disabled " +
247248
"algorithm: " + curve,
248249
null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
250+
}
249251
}
250252
}
251253
}
252254

253-
algorithmConstraints.permits(algorithm, cp);
255+
algorithmConstraints.permits(algorithm, cp, checkKey);
254256
}
255257

256258
private static List<String> getNamedCurveFromKey(Key key) {
@@ -484,8 +486,8 @@ public boolean permits(String algorithm, AlgorithmParameters aps) {
484486
return true;
485487
}
486488

487-
public void permits(String algorithm, ConstraintsParameters cp)
488-
throws CertPathValidatorException {
489+
public void permits(String algorithm, ConstraintsParameters cp,
490+
boolean checkKey) throws CertPathValidatorException {
489491

490492
if (debug != null) {
491493
debug.println("Constraints.permits(): " + algorithm + ", "
@@ -499,8 +501,10 @@ public void permits(String algorithm, ConstraintsParameters cp)
499501
algorithms.add(algorithm);
500502
}
501503

502-
for (Key key : cp.getKeys()) {
503-
algorithms.add(key.getAlgorithm());
504+
if (checkKey) {
505+
for (Key key : cp.getKeys()) {
506+
algorithms.add(key.getAlgorithm());
507+
}
504508
}
505509

506510
// Check all applicable constraints
@@ -510,6 +514,9 @@ public void permits(String algorithm, ConstraintsParameters cp)
510514
continue;
511515
}
512516
for (Constraint constraint : list) {
517+
if (!checkKey && constraint instanceof KeySizeConstraint) {
518+
continue;
519+
}
513520
constraint.permits(cp);
514521
}
515522
}

src/java.base/share/classes/sun/security/util/JarConstraintsParameters.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,11 @@ public JarConstraintsParameters(CodeSigner[] signers) {
9898
this.timestamp = latestTimestamp;
9999
}
100100

101-
public JarConstraintsParameters(List<X509Certificate> chain, Timestamp timestamp) {
101+
public JarConstraintsParameters(List<X509Certificate> chain, Date timestamp) {
102102
this.keys = new HashSet<>();
103103
this.certsIssuedByAnchor = new HashSet<>();
104104
addToCertsAndKeys(chain);
105-
if (timestamp != null) {
106-
addToCertsAndKeys(timestamp.getSignerCertPath());
107-
this.timestamp = timestamp.getTimestamp();
108-
} else {
109-
this.timestamp = null;
110-
}
105+
this.timestamp = timestamp;
111106
}
112107

113108
// extract last certificate and signer's public key from chain
@@ -178,7 +173,7 @@ public void setExtendedExceptionMsg(String file, String target) {
178173

179174
@Override
180175
public String extendedExceptionMsg() {
181-
return message;
176+
return message == null ? "." : message;
182177
}
183178

184179
@Override

src/java.base/share/classes/sun/security/util/ManifestEntryVerifier.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private boolean checkConstraints(String algorithm,
295295
params.setExtendedExceptionMsg(JarFile.MANIFEST_NAME,
296296
name + " entry");
297297
DisabledAlgorithmConstraints.jarConstraints()
298-
.permits(algorithm, params);
298+
.permits(algorithm, params, false);
299299
return true;
300300
} catch (GeneralSecurityException e) {
301301
if (debug != null) {

src/java.base/share/classes/sun/security/util/SignatureFileVerifier.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ private boolean permittedCheck(String key, String algorithm) {
359359
try {
360360
params.setExtendedExceptionMsg(name + ".SF", key + " attribute");
361361
DisabledAlgorithmConstraints
362-
.jarConstraints().permits(algorithm, params);
362+
.jarConstraints().permits(algorithm, params, false);
363363
} catch (GeneralSecurityException e) {
364364
permittedAlgs.put(algorithm, Boolean.FALSE);
365365
permittedAlgs.put(key.toUpperCase(), Boolean.FALSE);

src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,14 @@ void verifyJar(String jarName)
973973
Calendar c = Calendar.getInstance(
974974
TimeZone.getTimeZone("UTC"),
975975
Locale.getDefault(Locale.Category.FORMAT));
976-
c.setTime(tsTokenInfo.getDate());
976+
Date tsDate = tsTokenInfo.getDate();
977+
c.setTime(tsDate);
977978
JarConstraintsParameters jcp =
978-
new JarConstraintsParameters(chain, si.getTimestamp());
979+
new JarConstraintsParameters(chain, tsDate);
980+
JarConstraintsParameters jcpts =
981+
new JarConstraintsParameters(
982+
tsSi.getCertificateChain(tsToken),
983+
tsDate);
979984
history = String.format(
980985
rb.getString("history.with.ts"),
981986
signer.getSubjectX500Principal(),
@@ -984,9 +989,9 @@ void verifyJar(String jarName)
984989
verifyWithWeak(key, jcp),
985990
c,
986991
tsSigner.getSubjectX500Principal(),
987-
verifyWithWeak(tsDigestAlg, DIGEST_PRIMITIVE_SET, true, jcp),
988-
verifyWithWeak(tsSigAlg, SIG_PRIMITIVE_SET, true, jcp),
989-
verifyWithWeak(tsKey, jcp));
992+
verifyWithWeak(tsDigestAlg, DIGEST_PRIMITIVE_SET, true, jcpts),
993+
verifyWithWeak(tsSigAlg, SIG_PRIMITIVE_SET, true, jcpts),
994+
verifyWithWeak(tsKey, jcpts));
990995
} else {
991996
JarConstraintsParameters jcp =
992997
new JarConstraintsParameters(chain, null);
@@ -1333,13 +1338,13 @@ private String verifyWithWeak(String alg, Set<CryptoPrimitive> primitiveSet,
13331338
boolean tsa, JarConstraintsParameters jcp) {
13341339

13351340
try {
1336-
DISABLED_CHECK.permits(alg, jcp);
1341+
DISABLED_CHECK.permits(alg, jcp, false);
13371342
} catch (CertPathValidatorException e) {
13381343
disabledAlgFound = true;
13391344
return String.format(rb.getString("with.disabled"), alg);
13401345
}
13411346
try {
1342-
LEGACY_CHECK.permits(alg, jcp);
1347+
LEGACY_CHECK.permits(alg, jcp, false);
13431348
return alg;
13441349
} catch (CertPathValidatorException e) {
13451350
if (primitiveSet == SIG_PRIMITIVE_SET) {
@@ -1361,13 +1366,13 @@ private String verifyWithWeak(String alg, Set<CryptoPrimitive> primitiveSet,
13611366
private String verifyWithWeak(PublicKey key, JarConstraintsParameters jcp) {
13621367
int kLen = KeyUtil.getKeySize(key);
13631368
try {
1364-
DISABLED_CHECK.permits(key.getAlgorithm(), jcp);
1369+
DISABLED_CHECK.permits(key.getAlgorithm(), jcp, true);
13651370
} catch (CertPathValidatorException e) {
13661371
disabledAlgFound = true;
13671372
return String.format(rb.getString("key.bit.disabled"), kLen);
13681373
}
13691374
try {
1370-
LEGACY_CHECK.permits(key.getAlgorithm(), jcp);
1375+
LEGACY_CHECK.permits(key.getAlgorithm(), jcp, true);
13711376
if (kLen >= 0) {
13721377
return String.format(rb.getString("key.bit"), kLen);
13731378
} else {
@@ -1384,9 +1389,9 @@ private void checkWeakSign(String alg, Set<CryptoPrimitive> primitiveSet,
13841389
boolean tsa, JarConstraintsParameters jcp) {
13851390

13861391
try {
1387-
DISABLED_CHECK.permits(alg, jcp);
1392+
DISABLED_CHECK.permits(alg, jcp, false);
13881393
try {
1389-
LEGACY_CHECK.permits(alg, jcp);
1394+
LEGACY_CHECK.permits(alg, jcp, false);
13901395
} catch (CertPathValidatorException e) {
13911396
if (primitiveSet == SIG_PRIMITIVE_SET) {
13921397
legacyAlg |= 2;
@@ -1413,9 +1418,9 @@ private void checkWeakSign(String alg, Set<CryptoPrimitive> primitiveSet,
14131418

14141419
private void checkWeakSign(PrivateKey key, JarConstraintsParameters jcp) {
14151420
try {
1416-
DISABLED_CHECK.permits(key.getAlgorithm(), jcp);
1421+
DISABLED_CHECK.permits(key.getAlgorithm(), jcp, true);
14171422
try {
1418-
LEGACY_CHECK.permits(key.getAlgorithm(), jcp);
1423+
LEGACY_CHECK.permits(key.getAlgorithm(), jcp, true);
14191424
} catch (CertPathValidatorException e) {
14201425
legacyAlg |= 8;
14211426
}

0 commit comments

Comments
 (0)