test/jdk/sun/security/tools/keytool/DefaultSignatureAlgorithm.java
changeset 55664 ceafb2debc68
parent 55663 63d13c01f2b8
parent 55662 1d5ce4787723
child 55665 70b1c1bec669
equal deleted inserted replaced
55663:63d13c01f2b8 55664:ceafb2debc68
     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  * @key intermittent
       
    28  * @summary New default -sigalg for keytool
       
    29  * @modules java.base/sun.security.tools.keytool
       
    30  * @modules jdk.crypto.ec
       
    31  * @run main/othervm DefaultSignatureAlgorithm RSA 1024 SHA256withRSA
       
    32  * @run main/othervm DefaultSignatureAlgorithm RSA 3072 SHA256withRSA
       
    33  * @run main/othervm DefaultSignatureAlgorithm RSA 3073 SHA384withRSA
       
    34  * @run main/othervm DefaultSignatureAlgorithm DSA 1024 SHA256withDSA
       
    35  * @run main/othervm/timeout=700 DefaultSignatureAlgorithm DSA 3072
       
    36  *      SHA256withDSA
       
    37  * @run main/othervm DefaultSignatureAlgorithm EC 192 SHA256withECDSA
       
    38  * @run main/othervm DefaultSignatureAlgorithm EC 384 SHA384withECDSA
       
    39  * @run main/othervm DefaultSignatureAlgorithm EC 571 SHA512withECDSA
       
    40  * @run main/othervm DefaultSignatureAlgorithm EC 571 SHA256withECDSA
       
    41  *      SHA256withECDSA
       
    42  */
       
    43 
       
    44 import sun.security.tools.keytool.Main;
       
    45 
       
    46 import java.io.File;
       
    47 import java.security.KeyStore;
       
    48 import java.security.cert.X509Certificate;
       
    49 
       
    50 public class DefaultSignatureAlgorithm {
       
    51 
       
    52     public static void main(String[] args) throws Exception {
       
    53         if(args == null || args.length < 3) {
       
    54             throw new RuntimeException("Invalid arguments provided.");
       
    55         }
       
    56         String sigAlg = (args.length == 4) ? args[3] : null;
       
    57         run(args[0], Integer.valueOf(args[1]), args[2], sigAlg);
       
    58     }
       
    59 
       
    60     private static void run(String keyAlg, int keySize,
       
    61                     String expectedSigAlg, String sigAlg) throws Exception {
       
    62         String alias = keyAlg + keySize + System.currentTimeMillis();
       
    63         String cmd = "-keystore ks -storepass changeit" +
       
    64                 " -keypass changeit -alias " + alias +
       
    65                 " -keyalg " + keyAlg + " -keysize " + keySize +
       
    66                 " -genkeypair -dname CN=" + alias + " -debug";
       
    67         if (sigAlg != null) {
       
    68             cmd += " -sigalg " + sigAlg;
       
    69         }
       
    70         Main.main(cmd.split(" "));
       
    71 
       
    72         KeyStore ks = KeyStore.getInstance(
       
    73                 new File("ks"), "changeit".toCharArray());
       
    74         X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
       
    75         String actualSigAlg = cert.getSigAlgName();
       
    76         if (!actualSigAlg.equals(expectedSigAlg)) {
       
    77             throw new Exception("Failure at " + alias + ": expected "
       
    78                     + expectedSigAlg + ", actually " + actualSigAlg);
       
    79         }
       
    80     }
       
    81 }