Path: blob/master/test/jdk/sun/security/tools/keytool/fakegen/DefaultSignatureAlgorithm.java
41154 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8138766 8227059 822759526* @summary New default -sigalg for keytool27* @library /test/lib28* @build java.base/sun.security.rsa.RSAKeyPairGenerator29* java.base/sun.security.provider.DSAKeyPairGenerator30* jdk.crypto.ec/sun.security.ec.ECKeyPairGenerator31* @run main DefaultSignatureAlgorithm32* @modules jdk.crypto.ec33*/3435import jdk.test.lib.Asserts;36import jdk.test.lib.SecurityTools;37import jdk.test.lib.process.OutputAnalyzer;3839import java.io.File;40import java.security.KeyStore;41import java.security.cert.X509Certificate;4243public class DefaultSignatureAlgorithm {4445static int pos = 0;4647public static void main(String[] args) throws Exception {48check("RSA", 1024, null, "SHA256withRSA");49check("RSA", 3072, null, "SHA256withRSA");50check("RSA", 3073, null, "SHA384withRSA");51check("RSA", 7680, null, "SHA384withRSA");52check("RSA", 7681, null, "SHA512withRSA");5354check("DSA", 1024, null, "SHA256withDSA");55check("DSA", 3072, null, "SHA256withDSA");5657check("EC", 384, null, "SHA384withECDSA");5859check("EC", 384, "SHA256withECDSA", "SHA256withECDSA");60}6162private static void check(String keyAlg, int keySize,63String requestedSigAlg, String expectedSigAlg)64throws Exception {65String alias = keyAlg + keySize + "-" + pos++;66String sigAlgParam = requestedSigAlg == null67? ""68: (" -sigalg " + requestedSigAlg);69genkeypair(alias,70"-keyalg " + keyAlg + " -keysize " + keySize + sigAlgParam)71.shouldHaveExitValue(0);7273KeyStore ks = KeyStore.getInstance(74new File("ks"), "changeit".toCharArray());75X509Certificate cert = (X509Certificate)ks.getCertificate(alias);76Asserts.assertEQ(cert.getPublicKey().getAlgorithm(), keyAlg);77Asserts.assertEQ(cert.getSigAlgName(), expectedSigAlg);78}7980static OutputAnalyzer genkeypair(String alias, String options)81throws Exception {82String patchArg = "-J--patch-module=java.base="83+ System.getProperty("test.classes")84+ File.separator + "patches" + File.separator + "java.base"85+ " -J--patch-module=jdk.crypto.ec="86+ System.getProperty("test.classes")87+ File.separator + "patches" + File.separator + "jdk.crypto.ec";88return kt(patchArg + " -genkeypair -alias " + alias89+ " -dname CN=" + alias + " " + options);90}9192static OutputAnalyzer kt(String cmd)93throws Exception {94return SecurityTools.keytool("-storepass changeit -keypass changeit "95+ "-keystore ks " + cmd);96}97}9899100