Path: blob/master/test/jdk/sun/security/mscapi/KeyAlgorithms.java
41149 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*/2223/**24* @test25* @bug 8213009 823780426* @summary Make sure SunMSCAPI keys have correct algorithm names27* @requires os.family == "windows"28* @library /test/lib29* @modules jdk.crypto.mscapi30*/3132import java.security.*;3334import jdk.test.lib.Asserts;35import jdk.test.lib.SecurityTools;3637public class KeyAlgorithms {3839private static final String ALIAS = "8213009";40private static final String ALG = "RSA";4142public static void main(String[] arg) throws Exception {4344cleanup();45SecurityTools.keytool("-genkeypair",46"-storetype", "Windows-My",47"-keyalg", ALG,48"-alias", ALIAS,49"-dname", "cn=" + ALIAS,50"-noprompt").shouldHaveExitValue(0);5152try {53test(loadKeysFromKeyStore());54} finally {55cleanup();56}5758test(generateKeys());59}6061private static void cleanup() {62try {63KeyStore ks = KeyStore.getInstance("Windows-MY");64ks.load(null, null);65ks.deleteEntry(ALIAS);66ks.store(null, null);67} catch (Exception e) {68System.out.println("No such entry.");69}70}7172static KeyPair loadKeysFromKeyStore() throws Exception {73KeyStore ks = KeyStore.getInstance("Windows-MY");74ks.load(null, null);75return new KeyPair(ks.getCertificate(ALIAS).getPublicKey(),76(PrivateKey) ks.getKey(ALIAS, null));77}7879static KeyPair generateKeys() throws Exception {80KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALG, "SunMSCAPI");81return kpg.generateKeyPair();82}8384static void test(KeyPair kp) {85Asserts.assertEQ(kp.getPrivate().getAlgorithm(), ALG);86Asserts.assertEQ(kp.getPublic().getAlgorithm(), ALG);87}88}899091