|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Amazon.com, Inc. or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @test |
| 26 | + * @bug 8254717 |
| 27 | + * @summary isAssignableFrom checks in KeyFactorySpi.engineGetKeySpec appear to be backwards. |
| 28 | + * @author Greg Rubin, Ziyi Luo |
| 29 | + */ |
| 30 | + |
| 31 | +import java.security.KeyFactory; |
| 32 | +import java.security.KeyPair; |
| 33 | +import java.security.KeyPairGenerator; |
| 34 | +import java.security.spec.*; |
| 35 | + |
| 36 | +public class KeyFactoryGetKeySpecForInvalidSpec { |
| 37 | + public static void main(String[] args) throws Exception { |
| 38 | + KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA"); |
| 39 | + kg.initialize(2048); |
| 40 | + KeyPair pair = kg.generateKeyPair(); |
| 41 | + |
| 42 | + KeyFactory factory = KeyFactory.getInstance("RSA"); |
| 43 | + |
| 44 | + // Since RSAPrivateCrtKeySpec inherits from RSAPrivateKeySpec, we'd expect this next line to return an instance of RSAPrivateKeySpec |
| 45 | + // (because the private key has CRT parts). |
| 46 | + KeySpec spec = factory.getKeySpec(pair.getPrivate(), RSAPrivateKeySpec.class); |
| 47 | + if (!(spec instanceof RSAPrivateCrtKeySpec)) { |
| 48 | + throw new Exception("Spec should be an instance of RSAPrivateCrtKeySpec"); |
| 49 | + } |
| 50 | + |
| 51 | + // This next line should give an InvalidKeySpec exception |
| 52 | + try { |
| 53 | + spec = factory.getKeySpec(pair.getPublic(), FakeX509Spec.class); |
| 54 | + throw new Exception("InvalidKeySpecException is expected but not thrown"); |
| 55 | + } catch (final ClassCastException ex) { |
| 56 | + throw new Exception("InvalidKeySpecException is expected ClassCastException is thrown", ex); |
| 57 | + } catch (final InvalidKeySpecException ex) { |
| 58 | + // Pass |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static class FakeX509Spec extends X509EncodedKeySpec { |
| 63 | + public FakeX509Spec(byte[] encodedKey) { |
| 64 | + super(encodedKey); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Dec 18, 2022
Review
Issues