jdk/test/sun/security/tools/keytool/DefaultSignatureAlgorithm.java
author skovalev
Thu, 15 Sep 2016 13:03:03 +0300
changeset 40975 680639c9b307
parent 39464 f49988e2d982
child 44107 a3d145e4f03c
permissions -rw-r--r--
8165689: Fix module dependencies for sun/security/pkcs11/* tests Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     1
/*
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     4
 *
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     8
 *
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    13
 * accompanied this code).
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    14
 *
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    18
 *
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    21
 * questions.
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    22
 */
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    23
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    24
/*
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    25
 * @test
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    26
 * @bug 8138766
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    27
 * @summary New default -sigalg for keytool
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    28
 * @modules java.base/sun.security.tools.keytool
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 39464
diff changeset
    29
 * @modules jdk.crypto.ec
39464
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    30
 * @run main/othervm DefaultSignatureAlgorithm RSA 1024 SHA256withRSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    31
 * @run main/othervm DefaultSignatureAlgorithm RSA 3072 SHA256withRSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    32
 * @run main/othervm DefaultSignatureAlgorithm RSA 3073 SHA384withRSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    33
 * @run main/othervm DefaultSignatureAlgorithm DSA 1024 SHA256withDSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    34
 * @run main/othervm/timeout=700 DefaultSignatureAlgorithm DSA 3072
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    35
 *      SHA256withDSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    36
 * @run main/othervm DefaultSignatureAlgorithm EC 192 SHA256withECDSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    37
 * @run main/othervm DefaultSignatureAlgorithm EC 384 SHA384withECDSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    38
 * @run main/othervm DefaultSignatureAlgorithm EC 571 SHA512withECDSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    39
 * @run main/othervm DefaultSignatureAlgorithm EC 571 SHA256withECDSA
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    40
 *      SHA256withECDSA
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    41
 */
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    42
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    43
import sun.security.tools.keytool.Main;
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    44
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    45
import java.io.File;
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    46
import java.security.KeyStore;
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    47
import java.security.cert.X509Certificate;
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    48
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    49
public class DefaultSignatureAlgorithm {
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    50
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
39464
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    52
        if(args == null || args.length < 3) {
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    53
            throw new RuntimeException("Invalid arguments provided.");
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    54
        }
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    55
        String sigAlg = (args.length == 4) ? args[3] : null;
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    56
        run(args[0], Integer.valueOf(args[1]), args[2], sigAlg);
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    57
    }
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    58
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    59
    private static void run(String keyAlg, int keySize,
39464
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    60
                    String expectedSigAlg, String sigAlg) throws Exception {
f49988e2d982 8159861: sun/security/tools/keytool/DefaultSignatureAlgorithm.java timeout
ssahoo
parents: 38439
diff changeset
    61
        String alias = keyAlg + keySize + System.currentTimeMillis();
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    62
        String cmd = "-keystore ks -storepass changeit" +
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    63
                " -keypass changeit -alias " + alias +
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    64
                " -keyalg " + keyAlg + " -keysize " + keySize +
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    65
                " -genkeypair -dname CN=" + alias + " -debug";
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    66
        if (sigAlg != null) {
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    67
            cmd += " -sigalg " + sigAlg;
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    68
        }
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    69
        Main.main(cmd.split(" "));
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    70
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    71
        KeyStore ks = KeyStore.getInstance(
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    72
                new File("ks"), "changeit".toCharArray());
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    73
        X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    74
        String actualSigAlg = cert.getSigAlgName();
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    75
        if (!actualSigAlg.equals(expectedSigAlg)) {
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    76
            throw new Exception("Failure at " + alias + ": expected "
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    77
                    + expectedSigAlg + ", actually " + actualSigAlg);
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    78
        }
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    79
    }
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents:
diff changeset
    80
}