Path: blob/master/test/jdk/sun/security/mscapi/PublicKeyInterop.java
41152 views
/*1* Copyright (c) 2011, 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 6888925 8180570 823780426* @summary SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.27* @requires os.family == "windows"28* @library /test/lib29* @modules java.base/sun.security.util30*/3132import java.security.*;33import java.util.*;34import javax.crypto.*;3536import jdk.test.lib.SecurityTools;37import jdk.test.lib.hexdump.HexPrinter;3839/*40* Confirm interoperability of RSA public keys between SunMSCAPI and SunJCE41* security providers.42*/43public class PublicKeyInterop {4445public static void main(String[] arg) throws Exception {4647cleanup();48SecurityTools.keytool("-genkeypair",49"-storetype", "Windows-My",50"-keyalg", "RSA",51"-alias", "6888925",52"-dname", "cn=6888925,c=US",53"-noprompt").shouldHaveExitValue(0);5455try {56run();57} finally {58cleanup();59}60}6162private static void cleanup() {63try {64KeyStore ks = KeyStore.getInstance("Windows-MY");65ks.load(null, null);66ks.deleteEntry("6888925");67ks.store(null, null);68} catch (Exception e) {69System.out.println("No such entry.");70}71}7273static void run() throws Exception {7475KeyStore ks = KeyStore.getInstance("Windows-MY");76ks.load(null, null);77System.out.println("Loaded keystore: Windows-MY");7879PublicKey myPuKey = ks.getCertificate("6888925").getPublicKey();80System.out.println("Public key is a " + myPuKey.getClass().getName());81PrivateKey myPrKey = (PrivateKey) ks.getKey("6888925", null);82System.out.println("Private key is a " + myPrKey.getClass().getName());83System.out.println();8485byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};86HexPrinter hp = HexPrinter.simple();87System.out.println("Plaintext:\n" + hp.toString(plain) + "\n");8889Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");90rsa.init(Cipher.ENCRYPT_MODE, myPuKey);91byte[] encrypted = rsa.doFinal(plain);92System.out.println("Encrypted plaintext using RSA Cipher from " +93rsa.getProvider().getName() + " JCE provider\n");94System.out.println(hp.toString(encrypted) + "\n");9596Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");97rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);98byte[] encrypted2 = rsa2.doFinal(plain);99System.out.println("Encrypted plaintext using RSA Cipher from " +100rsa2.getProvider().getName() + " JCE provider\n");101System.out.println(hp.toString(encrypted2) + "\n");102103Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");104rsa3.init(Cipher.DECRYPT_MODE, myPrKey);105byte[] decrypted = rsa3.doFinal(encrypted);106System.out.println("Decrypted first ciphertext using RSA Cipher from " +107rsa3.getProvider().getName() + " JCE provider\n");108System.out.println(hp.toString(decrypted) + "\n");109if (! Arrays.equals(plain, decrypted)) {110throw new Exception("First decrypted ciphertext does not match " +111"original plaintext");112}113114decrypted = rsa3.doFinal(encrypted2);115System.out.println("Decrypted second ciphertext using RSA Cipher from "116+ rsa3.getProvider().getName() + " JCE provider\n");117System.out.println(hp.toString(decrypted) + "\n");118if (! Arrays.equals(plain, decrypted)) {119throw new Exception("Second decrypted ciphertext does not match " +120"original plaintext");121}122}123}124125126