Path: blob/master/test/jdk/sun/security/tools/keytool/GroupName.java
41152 views
/*1* Copyright (c) 2018, 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*/2223import jdk.test.lib.Asserts;24import jdk.test.lib.SecurityTools;25import jdk.test.lib.process.OutputAnalyzer;2627import java.io.File;28import java.security.KeyStore;29import java.security.interfaces.ECKey;3031/**32* @test33* @bug 8213400 821417934* @summary Support choosing group name in keytool keypair generation35* @library /test/lib36*/3738public class GroupName {3940private static final String COMMON = "-keystore ks "41+ "-storepass changeit -keypass changeit -debug";4243public static void main(String[] args) throws Throwable {44gen("a", "-keyalg RSA -groupname secp256r1")45.shouldHaveExitValue(1);4647gen("b", "-keyalg EC")48.shouldHaveExitValue(0)49.shouldNotContain("Specifying -keysize for generating EC keys is deprecated");50checkCurveName("b", "secp256r1");5152gen("c", "-keyalg EC -keysize 256")53.shouldHaveExitValue(0)54.shouldContain("Specifying -keysize for generating EC keys is deprecated")55.shouldContain("please use \"-groupname secp256r1\" instead.");56checkCurveName("c", "secp256r1");5758gen("d", "-keyalg EC -keysize 256 -groupname secp256r1")59.shouldHaveExitValue(1)60.shouldContain("Cannot specify both -groupname and -keysize");6162gen("e", "-keyalg EC -groupname secp256r1")63.shouldHaveExitValue(0)64.shouldNotContain("Specifying -keysize for generating EC keys is deprecated");65checkCurveName("e", "secp256r1");6667kt("-list -v")68.shouldHaveExitValue(0)69.shouldContain("Subject Public Key Algorithm: 256-bit EC (secp256r1) key");70}7172private static void checkCurveName(String a, String name)73throws Exception {74KeyStore ks = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());75ECKey key = (ECKey)ks.getCertificate(a).getPublicKey();76// The following check is highly implementation dependent. In OpenJDK,77// params.toString() should contain all alternative names and the OID.78Asserts.assertTrue(key.getParams().toString().contains(name));79}8081private static OutputAnalyzer kt(String cmd) throws Throwable {82return SecurityTools.keytool(COMMON + " " + cmd);83}8485private static OutputAnalyzer gen(String a, String extra) throws Throwable {86return kt("-genkeypair -alias " + a + " -dname CN=" + a + " " + extra);87}88}899091