Path: blob/master/test/jdk/sun/security/rsa/pss/TestPSSKeySupport.java
41153 views
/*1* Copyright (c) 2018, 2021, 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 8242556 8172366 825471726* @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);100RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;101KeySpec rsaSpec3 = new RSAPrivateCrtKeySpec(rsaKey.getModulus(),102rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),103rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient(), rsaKey.getParams());104PrivateKey key7 = kf.generatePrivate(rsaSpec3);105testKey(key6, key6);106testKey(key6, key7);107}108109private static void test(KeyFactory kf, Key key) throws Exception {110if (key.getAlgorithm().equals(ALGO) == false) {111throw new Exception("Error: key algo should be " + ALGO);112}113if (key instanceof PublicKey) {114testPublic(kf, (PublicKey)key);115} else if (key instanceof PrivateKey) {116testPrivate(kf, (PrivateKey)key);117}118}119120private static void checkKeyPair(KeyPair kp) throws Exception {121PublicKey pubKey = kp.getPublic();122if (!(pubKey instanceof RSAPublicKey)) {123throw new Exception("Error: public key should be RSAPublicKey");124}125PrivateKey privKey = kp.getPrivate();126if (!(privKey instanceof RSAPrivateKey)) {127throw new Exception("Error: private key should be RSAPrivateKey");128}129}130131public static void main(String[] args) throws Exception {132KeyPairGenerator kpg =133KeyPairGenerator.getInstance(ALGO, "SunRsaSign");134135// Algorithm-Independent Initialization136kpg.initialize(2048);137KeyPair kp = kpg.generateKeyPair();138checkKeyPair(kp);139BigInteger pubExp = ((RSAPublicKey)kp.getPublic()).getPublicExponent();140141// Algorithm-specific Initialization142PSSParameterSpec params = new PSSParameterSpec("SHA-256", "MGF1",143MGF1ParameterSpec.SHA256, 32, 1);144kpg.initialize(new RSAKeyGenParameterSpec(2048, pubExp, params));145KeyPair kp2 = kpg.generateKeyPair();146checkKeyPair(kp2);147148params = new PSSParameterSpec("SHA3-256", "MGF1",149new MGF1ParameterSpec("SHA3-256"), 32, 1);150kpg.initialize(new RSAKeyGenParameterSpec(2048, pubExp, params));151KeyPair kp3 = kpg.generateKeyPair();152checkKeyPair(kp3);153154KeyFactory kf = KeyFactory.getInstance(ALGO, "SunRsaSign");155test(kf, kp.getPublic());156test(kf, kp.getPrivate());157test(kf, kp2.getPublic());158test(kf, kp2.getPrivate());159test(kf, kp3.getPublic());160test(kf, kp3.getPrivate());161}162}163164165