|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8259401 |
| 27 | + * @summary Check certificates in signer's cert chain to see if warning emitted |
| 28 | + * @library /test/lib |
| 29 | + */ |
| 30 | + |
| 31 | +import jdk.test.lib.SecurityTools; |
| 32 | +import jdk.test.lib.process.OutputAnalyzer; |
| 33 | +import jdk.test.lib.util.JarUtils; |
| 34 | + |
| 35 | +import java.nio.file.Path; |
| 36 | + |
| 37 | +public class CheckSignerCertChain { |
| 38 | + |
| 39 | + static OutputAnalyzer kt(String cmd, String ks) throws Exception { |
| 40 | + return SecurityTools.keytool("-storepass changeit " + cmd + |
| 41 | + " -keystore " + ks); |
| 42 | + } |
| 43 | + |
| 44 | + static void gencert(String owner, String cmd) throws Exception { |
| 45 | + kt("-certreq -alias " + owner + " -file tmp.req", "ks"); |
| 46 | + kt("-gencert -infile tmp.req -outfile tmp.cert " + cmd, "ks"); |
| 47 | + kt("-importcert -alias " + owner + " -file tmp.cert", "ks"); |
| 48 | + } |
| 49 | + |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + |
| 52 | + // root certificate using SHA1withRSA and 1024-bit key |
| 53 | + System.out.println("Generating a root cert using SHA1withRSA and 1024-bit key"); |
| 54 | + kt("-genkeypair -keyalg rsa -alias ca -dname CN=CA -ext bc:c " + |
| 55 | + "-keysize 1024 -sigalg SHA1withRSA", "ks"); |
| 56 | + kt("-genkeypair -keyalg rsa -alias ca1 -dname CN=CA1", "ks"); |
| 57 | + kt("-genkeypair -keyalg rsa -alias e1 -dname CN=E1", "ks"); |
| 58 | + |
| 59 | + // intermediate certificate using SHA1withRSA and 2048-bit key |
| 60 | + System.out.println("Generating an intermediate cert using SHA1withRSA and 2048-bit key"); |
| 61 | + gencert("ca1", "-alias ca -ext san=dns:ca1 -ext bc:c " + |
| 62 | + "-sigalg SHA1withRSA "); |
| 63 | + |
| 64 | + // end entity certificate using SHA256withRSA and 2048-bit key |
| 65 | + System.out.println("Generating an end entity cert using SHA256withRSA and 2048-bit key"); |
| 66 | + gencert("e1", "-alias ca1 -ext san=dns:e1 "); |
| 67 | + |
| 68 | + JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("ks")); |
| 69 | + |
| 70 | + SecurityTools.jarsigner("-keystore ks -storepass changeit " + |
| 71 | + "-signedjar signeda.jar " + |
| 72 | + "-sigalg SHA256withRSA " + |
| 73 | + "-verbose" + |
| 74 | + " a.jar e1") |
| 75 | + .shouldContain("Signature algorithm: SHA1withRSA (weak), 2048-bit key") |
| 76 | + // For trusted cert, warning should be generated for its weak 1024-bit |
| 77 | + // key, but not for its SHA1withRSA algorithm. |
| 78 | + .shouldContain("Signature algorithm: SHA1withRSA, 1024-bit key (weak)") |
| 79 | + .shouldHaveExitValue(0); |
| 80 | + |
| 81 | + kt("-exportcert -alias ca -rfc -file cacert", "ks"); |
| 82 | + kt("-importcert -noprompt -file cacert", "caks"); |
| 83 | + |
| 84 | + SecurityTools.jarsigner("-verify -certs signeda.jar " + |
| 85 | + "-keystore caks -storepass changeit -verbose -debug") |
| 86 | + .shouldContain("Signature algorithm: SHA1withRSA (weak), 2048-bit key") |
| 87 | + // For trusted cert, warning should be generated for its weak 1024-bit |
| 88 | + // key, but not for its SHA1withRSA algorithm. |
| 89 | + .shouldContain("Signature algorithm: SHA1withRSA, 1024-bit key (weak)") |
| 90 | + .shouldHaveExitValue(0); |
| 91 | + } |
| 92 | +} |
0 commit comments