DefaultSignatureAlgorithm.java revision 14559:b244dce93e06
1/*
2 * Copyright (c) 2016, 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 8138766
27 * @summary New default -sigalg for keytool
28 * @modules java.base/sun.security.tools.keytool
29 */
30
31import sun.security.tools.keytool.Main;
32
33import java.io.File;
34import java.security.KeyStore;
35import java.security.cert.X509Certificate;
36
37public class DefaultSignatureAlgorithm {
38
39    private static int counter = 0;
40
41    public static void main(String[] args) throws Exception {
42
43        // Calculating large RSA keys are too slow.
44        run("RSA", 1024, null, "SHA256withRSA");
45        run("RSA", 3072, null, "SHA256withRSA");
46        run("RSA", 3073, null, "SHA384withRSA");
47
48        run("DSA", 1024, null, "SHA256withDSA");
49        run("DSA", 3072, null, "SHA256withDSA");
50
51        run("EC", 192, null, "SHA256withECDSA");
52        run("EC", 384, null, "SHA384withECDSA");
53        run("EC", 571, null, "SHA512withECDSA");
54
55        // If you specify one, it will be used.
56        run("EC", 571, "SHA256withECDSA", "SHA256withECDSA");
57    }
58
59    private static void run(String keyAlg, int keySize,
60                    String sigAlg, String expectedSigAlg) throws Exception {
61        String alias = keyAlg + keySize + (counter++);
62        String cmd = "-keystore ks -storepass changeit" +
63                " -keypass changeit -alias " + alias +
64                " -keyalg " + keyAlg + " -keysize " + keySize +
65                " -genkeypair -dname CN=" + alias + " -debug";
66        if (sigAlg != null) {
67            cmd += " -sigalg " + sigAlg;
68        }
69        Main.main(cmd.split(" "));
70
71        KeyStore ks = KeyStore.getInstance(
72                new File("ks"), "changeit".toCharArray());
73        X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
74        String actualSigAlg = cert.getSigAlgName();
75        if (!actualSigAlg.equals(expectedSigAlg)) {
76            throw new Exception("Failure at " + alias + ": expected "
77                    + expectedSigAlg + ", actually " + actualSigAlg);
78        }
79    }
80}
81