Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk21 Public archive

Commit ed98bd7

Browse files
committedJun 28, 2023
8292704: sun/security/tools/jarsigner/compatibility/Compatibility.java use wrong key size for EC
Reviewed-by: valeriep, hchao Backport-of: 130a9f138759c2f8504a83a6f3a93b1f219f0a42
1 parent 359bd63 commit ed98bd7

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed
 

‎test/jdk/sun/security/tools/jarsigner/compatibility/Compatibility.java

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,7 @@
3131
* its usages, please look through the README.
3232
*
3333
* @library /test/lib ../warnings
34-
* @compile -source 1.7 -target 1.7 JdkUtils.java
34+
* @compile -source 1.8 -target 1.8 JdkUtils.java
3535
* @run main/manual/othervm Compatibility
3636
*/
3737

@@ -67,7 +67,6 @@
6767
import java.util.stream.Collectors;
6868
import java.util.stream.IntStream;
6969

70-
import jdk.test.lib.Platform;
7170
import jdk.test.lib.process.OutputAnalyzer;
7271
import jdk.test.lib.process.ProcessTools;
7372
import jdk.test.lib.util.JarUtils;
@@ -460,7 +459,7 @@ private static int[] keySizes(String keyAlgorithm) throws IOException {
460459
if (RSA.equals(keyAlgorithm) || DSA.equals(keyAlgorithm)) {
461460
return new int[] { 1024, 2048, 0 }; // 0 is no keysize specified
462461
} else if (EC.equals(keyAlgorithm)) {
463-
return new int[] { 384, 571, 0 }; // 0 is no keysize specified
462+
return new int[] { 384, 521, 0 }; // 0 is no keysize specified
464463
} else {
465464
throw new RuntimeException("problem determining key sizes");
466465
}
@@ -717,7 +716,7 @@ private static void verifying(SignItem signItem, VerifyItem verifyItem)
717716
try {
718717
String match = "^ ("
719718
+ " Signature algorithm: " + signItem.certInfo.
720-
expectedSigalg() + ", " + signItem.certInfo.
719+
expectedSigalg(signItem) + ", " + signItem.certInfo.
721720
expectedKeySize() + "-bit key"
722721
+ ")|("
723722
+ " Digest algorithm: " + signItem.expectedDigestAlg()
@@ -845,6 +844,7 @@ private static Status verifyingStatus(SignItem signItem, VerifyItem
845844

846845
if (isWeakAlg(signItem.expectedDigestAlg())
847846
&& line.contains(Test.WEAK_ALGORITHM_WARNING)) continue;
847+
if (line.contains(Test.WEAK_KEY_WARNING)) continue;
848848
if (Test.CERTIFICATE_SELF_SIGNED.equals(line)) continue;
849849
if (Test.HAS_EXPIRED_CERT_VERIFYING_WARNING.equals(line)
850850
&& signItem.certInfo.expired) continue;
@@ -1183,19 +1183,56 @@ private String sigalg() {
11831183
}
11841184

11851185
private String expectedSigalg() {
1186-
return (DEFAULT.equals(this.digestAlgorithm) ? this.digestAlgorithm
1187-
: "SHA-256").replace("-", "") + "with" +
1188-
keyAlgorithm + (EC.equals(keyAlgorithm) ? "DSA" : "");
1186+
return "SHA256with" + keyAlgorithm + (EC.equals(keyAlgorithm) ? "DSA" : "");
1187+
}
1188+
1189+
private String expectedSigalg(SignItem signer) {
1190+
if (!DEFAULT.equals(digestAlgorithm)) {
1191+
return "SHA256with" + keyAlgorithm + (EC.equals(keyAlgorithm) ? "DSA" : "");
1192+
1193+
} else {
1194+
// default algorithms documented for jarsigner here:
1195+
// https://docs.oracle.com/en/java/javase/17/docs/specs/man/jarsigner.html#supported-algorithms
1196+
// https://docs.oracle.com/en/java/javase/20/docs/specs/man/jarsigner.html#supported-algorithms
1197+
int expectedKeySize = expectedKeySize();
1198+
switch (keyAlgorithm) {
1199+
case DSA:
1200+
return "SHA256withDSA";
1201+
case RSA: {
1202+
if ((signer.jdkInfo.majorVersion >= 20 && expectedKeySize < 624)
1203+
|| (signer.jdkInfo.majorVersion < 20 && expectedKeySize <= 3072)) {
1204+
return "SHA256withRSA";
1205+
} else if (expectedKeySize <= 7680) {
1206+
return "SHA384withRSA";
1207+
} else {
1208+
return "SHA512withRSA";
1209+
}
1210+
}
1211+
case EC: {
1212+
if (signer.jdkInfo.majorVersion < 20 && expectedKeySize < 384) {
1213+
return "SHA256withECDSA";
1214+
} else if (expectedKeySize < 512) {
1215+
return "SHA384withECDSA";
1216+
} else {
1217+
return "SHA512withECDSA";
1218+
}
1219+
}
1220+
default:
1221+
throw new RuntimeException("Unsupported/expected key algorithm: " + keyAlgorithm);
1222+
}
1223+
}
11891224
}
11901225

11911226
private int expectedKeySize() {
11921227
if (keySize != 0) return keySize;
11931228

11941229
// defaults
1195-
if (RSA.equals(keyAlgorithm) || DSA.equals(keyAlgorithm)) {
1196-
return 3072;
1230+
if (RSA.equals(keyAlgorithm)) {
1231+
return jdkInfo.majorVersion >= 20 ? 3072 : 2048;
1232+
} else if (DSA.equals(keyAlgorithm)) {
1233+
return 2048;
11971234
} else if (EC.equals(keyAlgorithm)) {
1198-
return 384;
1235+
return jdkInfo.majorVersion >= 20 ? 384 : 256;
11991236
} else {
12001237
throw new RuntimeException("problem determining key size");
12011238
}
@@ -1391,7 +1428,9 @@ private SignItem digestAlgorithm(String digestAlgorithm) {
13911428
}
13921429

13931430
String expectedDigestAlg() {
1394-
return digestAlgorithm != null ? digestAlgorithm : "SHA-256";
1431+
return digestAlgorithm != null
1432+
? digestAlgorithm
1433+
: jdkInfo.majorVersion >= 20 ? "SHA-384" : "SHA-256";
13951434
}
13961435

13971436
private SignItem tsaDigestAlgorithm(String tsaDigestAlgorithm) {
@@ -1540,7 +1579,7 @@ private static String reportRow(SignItem signItem, VerifyItem verifyItem) {
15401579
s_values_add.accept(i -> i.unsignedJar + " -> " + i.signedJar);
15411580
s_values_add.accept(i -> i.certInfo.toString());
15421581
s_values_add.accept(i -> i.jdkInfo.version);
1543-
s_values_add.accept(i -> i.certInfo.expectedSigalg());
1582+
s_values_add.accept(i -> i.certInfo.expectedSigalg(i));
15441583
s_values_add.accept(i ->
15451584
null2Default(i.digestAlgorithm, i.expectedDigestAlg()));
15461585
s_values_add.accept(i -> i.tsaIndex == -1 ? "" :

‎test/jdk/sun/security/tools/jarsigner/warnings/Test.java

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public abstract class Test {
148148
= "algorithm is considered a security risk. "
149149
+ "This algorithm will be disabled in a future update.";
150150

151+
static final String WEAK_KEY_WARNING
152+
= "This key size will be disabled in a future update.";
153+
151154
static final String JAR_SIGNED = "jar signed.";
152155

153156
static final String JAR_VERIFIED = "jar verified.";

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jun 28, 2023

@openjdk-notifier[bot]
This repository has been archived.