Skip to content

Commit 13c07e6

Browse files
committed
8257722: Improve "keytool -printcert -jarfile" output
Reviewed-by: phh Backport-of: de93b1d0e83a9428dae4a9609996fe7b7e9b4932
1 parent 7c5f9c6 commit 13c07e6

File tree

4 files changed

+169
-87
lines changed

4 files changed

+169
-87
lines changed

src/java.base/share/classes/sun/security/tools/keytool/Main.java

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,6 +2756,23 @@ private static String oneInMany(String label, int i, int num) {
27562756
}
27572757
}
27582758

2759+
private static String oneInManys(String label, int certNo, int certCnt, int signerNo,
2760+
int signerCnt) {
2761+
if (certCnt == 1 && signerCnt == 1) {
2762+
return label;
2763+
}
2764+
if (certCnt > 1 && signerCnt == 1) {
2765+
return String.format(rb.getString("one.in.many1"), label, certNo);
2766+
}
2767+
if (certCnt == 1 && signerCnt > 1) {
2768+
return String.format(rb.getString("one.in.many2"), label, signerNo);
2769+
}
2770+
if (certCnt > 1 && signerCnt > 1) {
2771+
return String.format(rb.getString("one.in.many3"), label, certNo, signerNo);
2772+
}
2773+
return label;
2774+
}
2775+
27592776
private void doPrintCert(final PrintStream out) throws Exception {
27602777
if (jarfile != null) {
27612778
// reset "jdk.certpath.disabledAlgorithms" security property
@@ -2764,7 +2781,7 @@ private void doPrintCert(final PrintStream out) throws Exception {
27642781

27652782
JarFile jf = new JarFile(jarfile, true);
27662783
Enumeration<JarEntry> entries = jf.entries();
2767-
Set<CodeSigner> ss = new HashSet<>();
2784+
LinkedHashSet<CodeSigner> ss = new LinkedHashSet<>();
27682785
byte[] buffer = new byte[8192];
27692786
int pos = 0;
27702787
while (entries.hasMoreElements()) {
@@ -2781,48 +2798,59 @@ private void doPrintCert(final PrintStream out) throws Exception {
27812798
for (CodeSigner signer: signers) {
27822799
if (!ss.contains(signer)) {
27832800
ss.add(signer);
2784-
out.printf(rb.getString("Signer.d."), ++pos);
2785-
out.println();
2786-
out.println();
2787-
out.println(rb.getString("Signature."));
2788-
out.println();
2789-
2790-
List<? extends Certificate> certs
2791-
= signer.getSignerCertPath().getCertificates();
2792-
int cc = 0;
2793-
for (Certificate cert: certs) {
2794-
X509Certificate x = (X509Certificate)cert;
2795-
if (rfc) {
2796-
out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
2797-
dumpCert(x, out);
2798-
} else {
2799-
printX509Cert(x, out);
2800-
}
2801-
out.println();
2802-
checkWeak(oneInMany(rb.getString("the.certificate"), cc++, certs.size()), x);
2803-
}
2804-
Timestamp ts = signer.getTimestamp();
2805-
if (ts != null) {
2806-
out.println(rb.getString("Timestamp."));
2807-
out.println();
2808-
certs = ts.getSignerCertPath().getCertificates();
2809-
cc = 0;
2810-
for (Certificate cert: certs) {
2811-
X509Certificate x = (X509Certificate)cert;
2812-
if (rfc) {
2813-
out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
2814-
dumpCert(x, out);
2815-
} else {
2816-
printX509Cert(x, out);
2817-
}
2818-
out.println();
2819-
checkWeak(oneInMany(rb.getString("the.tsa.certificate"), cc++, certs.size()), x);
2820-
}
2821-
}
28222801
}
28232802
}
28242803
}
28252804
}
2805+
2806+
for (CodeSigner signer: ss) {
2807+
out.printf(rb.getString("Signer.d."), ++pos);
2808+
out.println();
2809+
out.println();
2810+
2811+
List<? extends Certificate> certs
2812+
= signer.getSignerCertPath().getCertificates();
2813+
int cc = 0;
2814+
for (Certificate cert: certs) {
2815+
out.printf(rb.getString("Certificate.d."), ++cc);
2816+
out.println();
2817+
X509Certificate x = (X509Certificate)cert;
2818+
if (rfc) {
2819+
out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
2820+
dumpCert(x, out);
2821+
} else {
2822+
printX509Cert(x, out);
2823+
}
2824+
out.println();
2825+
checkWeak(oneInManys(rb.getString(
2826+
"the.certificate"), cc,
2827+
certs.size(), pos,
2828+
ss.size()), x);
2829+
}
2830+
Timestamp ts = signer.getTimestamp();
2831+
if (ts != null) {
2832+
out.println(rb.getString("Timestamp."));
2833+
out.println();
2834+
certs = ts.getSignerCertPath().getCertificates();
2835+
cc = 0;
2836+
for (Certificate cert: certs) {
2837+
out.printf(rb.getString("Certificate.d."), ++cc);
2838+
out.println();
2839+
X509Certificate x = (X509Certificate)cert;
2840+
if (rfc) {
2841+
out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
2842+
dumpCert(x, out);
2843+
} else {
2844+
printX509Cert(x, out);
2845+
}
2846+
out.println();
2847+
checkWeak(oneInManys(rb.getString(
2848+
"the.tsa.certificate"), cc,
2849+
certs.size(), pos,
2850+
ss.size()), x);
2851+
}
2852+
}
2853+
}
28262854
jf.close();
28272855
if (ss.isEmpty()) {
28282856
out.println(rb.getString("Not.a.signed.jar.file"));

src/java.base/share/classes/sun/security/tools/keytool/Resources.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ public class Resources extends java.util.ListResourceBundle {
393393
{".WARNING.WARNING.WARNING.",
394394
"***************** WARNING WARNING WARNING *****************"},
395395
{"Signer.d.", "Signer #%d:"},
396+
{"Certificate.d.", "Certificate #%d:"},
396397
{"Timestamp.", "Timestamp:"},
397-
{"Signature.", "Signature:"},
398398
{"CRLs.", "CRLs:"},
399399
{"Certificate.owner.", "Certificate owner: "},
400400
{"Not.a.signed.jar.file", "Not a signed jar file"},
@@ -463,6 +463,9 @@ public class Resources extends java.util.ListResourceBundle {
463463
{"the.input", "The input"},
464464
{"reply", "Reply"},
465465
{"one.in.many", "%1$s #%2$d of %3$d"},
466+
{"one.in.many1", "%1$s #%2$d"},
467+
{"one.in.many2", "%1$s of signer #%2$d"},
468+
{"one.in.many3", "%1$s #%2$d of signer #%3$d"},
466469
{"alias.in.cacerts", "Issuer <%s> in cacerts"},
467470
{"alias.in.keystore", "Issuer <%s>"},
468471
{"with.weak", "%s (weak)"},

test/jdk/sun/security/tools/jarsigner/TimestampCheck.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -719,51 +719,6 @@ static void checkMultiple(String file) throws Exception {
719719
.shouldMatch("Signature algorithm: .*key.*(disabled)");
720720
}
721721

722-
static void checkWeak(String file) throws Exception {
723-
verify(file)
724-
.shouldHaveExitValue(0)
725-
.shouldNotContain("treated as unsigned");
726-
verify(file, "-verbose")
727-
.shouldHaveExitValue(0)
728-
.shouldNotContain("treated as unsigned")
729-
.shouldMatch("Digest algorithm: .*(weak)")
730-
.shouldMatch("Signature algorithm: .*(weak)")
731-
.shouldMatch("Timestamp digest algorithm: .*(weak)")
732-
.shouldNotMatch("Timestamp signature algorithm: .*(weak).*(weak)")
733-
.shouldMatch("Timestamp signature algorithm: .*key.*(weak)");
734-
verify(file, "-J-Djava.security.debug=jar")
735-
.shouldHaveExitValue(0)
736-
.shouldNotMatch("SignatureException:.*disabled");
737-
738-
// keytool should print out warnings when reading or
739-
// generating cert/cert req using legacy algorithms.
740-
String sout = SecurityTools.keytool("-printcert -jarfile " + file)
741-
.stderrShouldContain("The TSA certificate uses a 1024-bit RSA key" +
742-
" which is considered a security risk." +
743-
" This key size will be disabled in a future update.")
744-
.getStdout();
745-
if (sout.indexOf("weak", sout.indexOf("Timestamp:")) < 0) {
746-
throw new RuntimeException("timestamp not weak: " + sout);
747-
}
748-
}
749-
750-
static void checkHalfWeak(String file) throws Exception {
751-
verify(file)
752-
.shouldHaveExitValue(0)
753-
.shouldNotContain("treated as unsigned");
754-
verify(file, "-verbose")
755-
.shouldHaveExitValue(0)
756-
.shouldNotContain("treated as unsigned")
757-
.shouldMatch("Digest algorithm: .*(weak)")
758-
.shouldNotMatch("Signature algorithm: .*(weak)")
759-
.shouldNotMatch("Signature algorithm: .*(disabled)")
760-
.shouldNotMatch("Timestamp digest algorithm: .*(weak)")
761-
.shouldNotMatch("Timestamp signature algorithm: .*(weak).*(weak)")
762-
.shouldNotMatch("Timestamp signature algorithm: .*(disabled).*(disabled)")
763-
.shouldNotMatch("Timestamp signature algorithm: .*key.*(weak)")
764-
.shouldNotMatch("Timestamp signature algorithm: .*key.*(disabled)");
765-
}
766-
767722
static void checkMultipleWeak(String file) throws Exception {
768723
verify(file)
769724
.shouldHaveExitValue(0)

test/jdk/sun/security/tools/keytool/ReadJar.java

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021, 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
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 6890872 8168882
26+
* @bug 6890872 8168882 8257722
2727
* @summary keytool -printcert to recognize signed jar files
2828
* @library /test/lib
2929
* @build jdk.test.lib.SecurityTools
@@ -42,11 +42,24 @@
4242
import jdk.test.lib.SecurityTools;
4343
import jdk.test.lib.process.OutputAnalyzer;
4444
import jdk.test.lib.util.JarUtils;
45+
import java.nio.file.Path;
4546

4647
public class ReadJar {
4748

49+
static OutputAnalyzer kt(String cmd, String ks) throws Exception {
50+
return SecurityTools.keytool("-storepass changeit " + cmd +
51+
" -keystore " + ks);
52+
}
53+
54+
static void gencert(String owner, String cmd) throws Exception {
55+
kt("-certreq -alias " + owner + " -file tmp.req", "ks");
56+
kt("-gencert -infile tmp.req -outfile tmp.cert " + cmd, "ks");
57+
kt("-importcert -alias " + owner + " -file tmp.cert", "ks");
58+
}
59+
4860
public static void main(String[] args) throws Throwable {
4961
testWithMD5();
62+
testCertOutput();
5063
}
5164

5265
// make sure that -printcert option works
@@ -91,4 +104,87 @@ private static void printCert(String jar) throws Throwable {
91104
out.shouldHaveExitValue(0);
92105
out.shouldNotContain("Not a signed jar file");
93106
}
107+
108+
private static void testCertOutput() throws Throwable {
109+
kt("-genkeypair -keyalg rsa -alias e0 -dname CN=E0 " +
110+
"-keysize 512", "ks");
111+
JarUtils.createJarFile(Path.of("a0.jar"), Path.of("."), Path.of("ks"));
112+
// sign a0.jar file
113+
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
114+
" a0.jar e0")
115+
.shouldHaveExitValue(0);
116+
117+
SecurityTools.keytool("-printcert -jarfile a0.jar")
118+
.shouldNotContain("Signature:")
119+
.shouldContain("Signer #1:")
120+
.shouldContain("Certificate #1:")
121+
.shouldNotContain("Certificate #2:")
122+
.shouldNotContain("Signer #2:")
123+
.shouldMatch("The certificate uses a 512-bit RSA key.*is disabled")
124+
.shouldHaveExitValue(0);
125+
126+
kt("-genkeypair -keyalg rsa -alias ca1 -dname CN=CA1 -ext bc:c " +
127+
"-keysize 512", "ks");
128+
kt("-genkeypair -keyalg rsa -alias e1 -dname CN=E1", "ks");
129+
gencert("e1", "-alias ca1 -ext san=dns:e1");
130+
131+
JarUtils.createJarFile(Path.of("a1.jar"), Path.of("."), Path.of("ks"));
132+
// sign a1.jar file
133+
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
134+
" a1.jar e1")
135+
.shouldHaveExitValue(0);
136+
137+
SecurityTools.keytool("-printcert -jarfile a1.jar")
138+
.shouldNotContain("Signature:")
139+
.shouldContain("Signer #1:")
140+
.shouldContain("Certificate #1:")
141+
.shouldContain("Certificate #2:")
142+
.shouldNotContain("Signer #2:")
143+
.shouldMatch("The certificate #2 uses a 512-bit RSA key.*is disabled")
144+
.shouldHaveExitValue(0);
145+
146+
kt("-genkeypair -keyalg rsa -alias ca2 -dname CN=CA2 -ext bc:c " +
147+
"-sigalg SHA1withRSA", "ks");
148+
kt("-genkeypair -keyalg rsa -alias e2 -dname CN=E2", "ks");
149+
gencert("e2", "-alias ca2 -ext san=dns:e2");
150+
151+
// sign a1.jar file again with different signer
152+
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
153+
" a1.jar e2")
154+
.shouldHaveExitValue(0);
155+
156+
SecurityTools.keytool("-printcert -jarfile a1.jar")
157+
.shouldNotContain("Signature:")
158+
.shouldContain("Signer #1:")
159+
.shouldContain("Certificate #1:")
160+
.shouldContain("Certificate #2:")
161+
.shouldContain("Signer #2:")
162+
.shouldMatch("The certificate #.* of signer #.*" + "uses the SHA1withRSA.*will be disabled")
163+
.shouldMatch("The certificate #.* of signer #.*" + "uses a 512-bit RSA key.*is disabled")
164+
.shouldHaveExitValue(0);
165+
166+
kt("-genkeypair -keyalg rsa -alias e3 -dname CN=E3",
167+
"ks");
168+
JarUtils.createJarFile(Path.of("a2.jar"), Path.of("."), Path.of("ks"));
169+
// sign a2.jar file
170+
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
171+
" a2.jar e3")
172+
.shouldHaveExitValue(0);
173+
174+
kt("-genkeypair -keyalg rsa -alias e4 -dname CN=E4 " +
175+
"-keysize 1024", "ks");
176+
// sign a2.jar file again with different signer
177+
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
178+
" a2.jar e4")
179+
.shouldHaveExitValue(0);
180+
181+
SecurityTools.keytool("-printcert -jarfile a2.jar")
182+
.shouldNotContain("Signature:")
183+
.shouldContain("Signer #1:")
184+
.shouldContain("Certificate #1:")
185+
.shouldNotContain("Certificate #2:")
186+
.shouldContain("Signer #2:")
187+
.shouldMatch("The certificate of signer #.*" + "uses a 1024-bit RSA key.*will be disabled")
188+
.shouldHaveExitValue(0);
189+
}
94190
}

0 commit comments

Comments
 (0)