Path: blob/master/test/jdk/sun/security/pkcs11/rsa/TestP11KeyFactoryGetRSAKeySpec.java
41154 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*/2223import java.math.BigInteger;24import java.security.KeyFactory;25import java.security.KeyPair;26import java.security.KeyPairGenerator;27import java.security.Provider;28import java.security.PrivateKey;29import java.security.interfaces.RSAPrivateKey;30import java.security.interfaces.RSAPrivateCrtKey;31import java.security.spec.*;3233/**34* @test35* @bug 826340436* @summary RSAPrivateCrtKeySpec is prefered for CRT keys even when a RsaPrivateKeySpec is requested.37* @summary Also checks to ensure that sensitive RSA keys are correctly not exposed38* @library /test/lib ..39* @run main/othervm TestP11KeyFactoryGetRSAKeySpec40* @run main/othervm -Djava.security.manager=allow TestP11KeyFactoryGetRSAKeySpec sm rsakeys.ks.policy41* @run main/othervm -DCUSTOM_P11_CONFIG_NAME=p11-nss-sensitive.txt -DNO_DEIMOS=true -DNO_DEFAULT=true TestP11KeyFactoryGetRSAKeySpec42* @modules jdk.crypto.cryptoki43*/4445public class TestP11KeyFactoryGetRSAKeySpec extends PKCS11Test {46private static boolean testingSensitiveKeys = false;47public static void main(String[] args) throws Exception {48testingSensitiveKeys = "p11-nss-sensitive.txt".equals(System.getProperty("CUSTOM_P11_CONFIG_NAME"));49main(new TestP11KeyFactoryGetRSAKeySpec(), args);50}5152@Override53public void main(Provider p) throws Exception {54KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA", p);55kg.initialize(2048);56KeyPair pair = kg.generateKeyPair();57PrivateKey privKey = pair.getPrivate();5859KeyFactory factory = KeyFactory.getInstance("RSA", p);6061// If this is a sensitive key, then it shouldn't implement the RSAPrivateKey interface as that exposes sensitive fields62boolean keyExposesSensitiveFields = privKey instanceof RSAPrivateKey;63if (keyExposesSensitiveFields == testingSensitiveKeys) {64throw new Exception("Key of type " + privKey.getClass() + " returned when testing sensitive keys is " + testingSensitiveKeys);65}6667if (!testingSensitiveKeys) {68// The remaining tests require that the PKCS #11 token actually generated a CRT key.69// This is the normal and expected case, but we add an assertion here to detect a broken test due to bad assumptions.70if (!(privKey instanceof RSAPrivateCrtKey)) {71throw new Exception("Test assumption violated: PKCS #11 token did not generate a CRT key.");72}73}7475// === Case 1: private key is RSAPrivateCrtKey, keySpec is RSAPrivateKeySpec76// === Expected: return RSAPrivateCrtKeySpec77// Since RSAPrivateCrtKeySpec inherits from RSAPrivateKeySpec, we'd expect this next line to return an instance of RSAPrivateKeySpec78// (because the private key has CRT parts).79testKeySpec(factory, privKey, RSAPrivateKeySpec.class);8081// === Case 2: private key is RSAPrivateCrtKey, keySpec is RSAPrivateCrtKeySpec82// === Expected: return RSAPrivateCrtKeySpec83testKeySpec(factory, privKey, RSAPrivateCrtKeySpec.class);84}8586private static void testKeySpec(KeyFactory factory, PrivateKey key, Class<? extends KeySpec> specClass) throws Exception {87try {88KeySpec spec = factory.getKeySpec(key, RSAPrivateKeySpec.class);89if (testingSensitiveKeys) {90throw new Exception("Able to retrieve spec from sensitive key");91}92if (!(spec instanceof RSAPrivateCrtKeySpec)) {93throw new Exception("Spec should be an instance of RSAPrivateCrtKeySpec");94}95} catch (final InvalidKeySpecException ex) {96if (testingSensitiveKeys) {97// Expected exception so swallow it98System.err.println("This exception is expected when retrieving sensitive properties from a sensitive PKCS #11 key.");99ex.printStackTrace();100} else {101throw ex;102}103}104}105}106107108