Path: blob/master/test/jdk/java/security/KeyFactory/KeyFactoryGetKeySpecForInvalidSpec.java
41149 views
/*1* Copyright (c) 2021, Amazon.com, Inc. 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 8254717 826340426* @summary isAssignableFrom checks in KeyFactorySpi.engineGetKeySpec appear to be backwards.27* @author Greg Rubin, Ziyi Luo28*/2930import java.math.BigInteger;31import java.security.KeyFactory;32import java.security.KeyPair;33import java.security.KeyPairGenerator;34import java.security.interfaces.RSAPrivateCrtKey;35import java.security.interfaces.RSAPrivateKey;36import java.security.spec.*;3738public class KeyFactoryGetKeySpecForInvalidSpec {3940// Test for 8263404: This method generates RSAPrivateKey (without Crt info) from a RSAPrivateCrtKey41public static RSAPrivateKey privateCrtToPrivate(RSAPrivateCrtKey crtKey) {42return new RSAPrivateKey() {43@Override44public BigInteger getPrivateExponent() {45return crtKey.getPrivateExponent();46}4748@Override49public String getAlgorithm() {50return crtKey.getAlgorithm();51}5253@Override54public String getFormat() {55return crtKey.getFormat();56}5758@Override59public byte[] getEncoded() {60return crtKey.getEncoded();61}6263@Override64public BigInteger getModulus() {65return crtKey.getModulus();66}67};68}6970public static void main(String[] args) throws Exception {71KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");72kg.initialize(2048);73KeyPair pair = kg.generateKeyPair();7475KeyFactory factory = KeyFactory.getInstance("RSA");7677// === Case 1: private key is RSAPrivateCrtKey, keySpec is RSAPrivateKeySpec78// === Expected: return RSAPrivateCrtKeySpec79// Since RSAPrivateCrtKeySpec inherits from RSAPrivateKeySpec, we'd expect this next line to return an instance of RSAPrivateKeySpec80// (because the private key has CRT parts).81KeySpec spec = factory.getKeySpec(pair.getPrivate(), RSAPrivateKeySpec.class);82if (!(spec instanceof RSAPrivateCrtKeySpec)) {83throw new Exception("Spec should be an instance of RSAPrivateCrtKeySpec");84}8586// === Case 2: private key is RSAPrivateCrtKey, keySpec is RSAPrivateCrtKeySpec87// === Expected: return RSAPrivateCrtKeySpec88spec = factory.getKeySpec(pair.getPrivate(), RSAPrivateCrtKeySpec.class);89if (!(spec instanceof RSAPrivateCrtKeySpec)) {90throw new Exception("Spec should be an instance of RSAPrivateCrtKeySpec");91}9293// === Case 3: private key is RSAPrivateKey, keySpec is RSAPrivateKeySpec94// === Expected: return RSAPrivateKeySpec not RSAPrivateCrtKeySpec95RSAPrivateKey notCrtKey = privateCrtToPrivate((RSAPrivateCrtKey)pair.getPrivate());96// InvalidKeySpecException should not be thrown97KeySpec notCrtSpec = factory.getKeySpec(notCrtKey, RSAPrivateKeySpec.class);98if (notCrtSpec instanceof RSAPrivateCrtKeySpec) {99throw new Exception("Spec should be an instance of RSAPrivateKeySpec not RSAPrivateCrtKeySpec");100}101if (!(notCrtSpec instanceof RSAPrivateKeySpec)) {102throw new Exception("Spec should be an instance of RSAPrivateKeySpec");103}104105// === Case 4: private key is RSAPrivateKey, keySpec is RSAPrivateCrtKeySpec106// === Expected: throw InvalidKeySpecException107try {108factory.getKeySpec(notCrtKey, RSAPrivateCrtKeySpec.class);109throw new Exception("InvalidKeySpecException is expected but not thrown");110} catch (InvalidKeySpecException e) {111// continue;112}113114// This next line should give an InvalidKeySpec exception115try {116spec = factory.getKeySpec(pair.getPublic(), FakeX509Spec.class);117throw new Exception("InvalidKeySpecException is expected but not thrown");118} catch (final ClassCastException ex) {119throw new Exception("InvalidKeySpecException is expected ClassCastException is thrown", ex);120} catch (final InvalidKeySpecException ex) {121// Pass122}123}124125public static class FakeX509Spec extends X509EncodedKeySpec {126public FakeX509Spec(byte[] encodedKey) {127super(encodedKey);128}129130public FakeX509Spec(byte[] encodedKey, String algorithm) {131super(encodedKey, algorithm);132}133}134}135136137