Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/pss/TestPSSKeySupport.java
38853 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 8146293 824255626* @summary Test RSASSA-PSS Key related support such as KeyPairGenerator27* and KeyFactory of the SunRsaSign provider28*/2930import java.io.*;31import java.util.*;32import java.math.BigInteger;3334import java.security.*;35import java.security.interfaces.*;36import java.security.spec.*;3738public class TestPSSKeySupport {3940private static final String ALGO = "RSASSA-PSS";4142/**43* Test that key1 (reference key) and key2 (key to be tested) are44* equivalent45*/46private static void testKey(Key key1, Key key2) throws Exception {47if (key2.getAlgorithm().equals(ALGO) == false) {48throw new Exception("Algorithm not " + ALGO);49}50if (key1 instanceof PublicKey) {51if (key2.getFormat().equals("X.509") == false) {52throw new Exception("Format not X.509");53}54} else if (key1 instanceof PrivateKey) {55if (key2.getFormat().equals("PKCS#8") == false) {56throw new Exception("Format not PKCS#8");57}58}59if (key1.equals(key2) == false) {60throw new Exception("Keys not equal");61}62if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {63throw new Exception("Encodings not equal");64}65}6667private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {68System.out.println("Testing public key...");69PublicKey key2 = (PublicKey)kf.translateKey(key);70KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);71PublicKey key3 = kf.generatePublic(rsaSpec);72KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);73PublicKey key4 = kf.generatePublic(x509Spec);74KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());75PublicKey key5 = kf.generatePublic(x509Spec2);76testKey(key, key);77testKey(key, key2);78testKey(key, key3);79testKey(key, key4);80testKey(key, key5);81}8283private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {84System.out.println("Testing private key...");85PrivateKey key2 = (PrivateKey)kf.translateKey(key);86KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);87PrivateKey key3 = kf.generatePrivate(rsaSpec);88KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);89PrivateKey key4 = kf.generatePrivate(pkcs8Spec);90KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());91PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);92testKey(key, key);93testKey(key, key2);94testKey(key, key3);95testKey(key, key4);96testKey(key, key5);9798KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);99PrivateKey key6 = kf.generatePrivate(rsaSpec2);100RSAPrivateKey rsaKey = (RSAPrivateKey)key;101KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(),102rsaKey.getPrivateExponent(), rsaKey.getParams());103PrivateKey key7 = kf.generatePrivate(rsaSpec3);104testKey(key6, key6);105testKey(key6, key7);106}107108private static void test(KeyFactory kf, Key key) throws Exception {109if (key.getAlgorithm().equals(ALGO) == false) {110throw new Exception("Error: key algo should be " + ALGO);111}112if (key instanceof PublicKey) {113testPublic(kf, (PublicKey)key);114} else if (key instanceof PrivateKey) {115testPrivate(kf, (PrivateKey)key);116}117}118119private static void checkKeyPair(KeyPair kp) throws Exception {120PublicKey pubKey = kp.getPublic();121if (!(pubKey instanceof RSAPublicKey)) {122throw new Exception("Error: public key should be RSAPublicKey");123}124PrivateKey privKey = kp.getPrivate();125if (!(privKey instanceof RSAPrivateKey)) {126throw new Exception("Error: private key should be RSAPrivateKey");127}128}129130public static void main(String[] args) throws Exception {131KeyPairGenerator kpg =132KeyPairGenerator.getInstance(ALGO, "SunRsaSign");133134// Algorithm-Independent Initialization135kpg.initialize(2048);136KeyPair kp = kpg.generateKeyPair();137checkKeyPair(kp);138BigInteger pubExp = ((RSAPublicKey)kp.getPublic()).getPublicExponent();139140// Algorithm-specific Initialization141PSSParameterSpec params = new PSSParameterSpec("SHA-256", "MGF1",142MGF1ParameterSpec.SHA256, 32, 1);143kpg.initialize(new RSAKeyGenParameterSpec(2048, pubExp, params));144KeyPair kp2 = kpg.generateKeyPair();145checkKeyPair(kp2);146147KeyFactory kf = KeyFactory.getInstance(ALGO, "SunRsaSign");148test(kf, kp.getPublic());149test(kf, kp.getPrivate());150test(kf, kp2.getPublic());151test(kf, kp2.getPrivate());152153}154}155156157