Path: blob/master/test/jdk/sun/security/rsa/PrivateKeyEqualityTest.java
41149 views
/*1* Copyright (c) 2015, 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*/22import java.security.KeyFactory;23import java.security.KeyPairGenerator;24import java.security.KeyPair;25import java.security.NoSuchAlgorithmException;26import java.security.NoSuchProviderException;27import java.security.interfaces.RSAPrivateCrtKey;28import java.security.interfaces.RSAPrivateKey;29import java.security.spec.InvalidKeySpecException;30import java.security.spec.RSAPrivateKeySpec;31import java.security.spec.PKCS8EncodedKeySpec;32import java.security.spec.RSAPrivateCrtKeySpec;3334/**35* @test36* @bug 8044199 466648537* @summary Equality checking for RSAPrivateKey by SunRsaSign provider.38*/39public class PrivateKeyEqualityTest {40/**41* ALGORITHM name, fixed as RSA.42*/43private static final String KEYALG = "RSA";4445/**46* JDK default RSA Provider.47*/48private static final String PROVIDER_NAME = "SunRsaSign";4950public static void main(String[] args) throws NoSuchAlgorithmException,51NoSuchProviderException, InvalidKeySpecException {52// Generate the first key.53KeyPairGenerator generator54= KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);55KeyPair keyPair = generator.generateKeyPair();56RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();57if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {58System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());59throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");60}6162// Generate the second key.63KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);64RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(65rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());66RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(67rsaPrivateKeySpec);6869// Generate the third key.70PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(71rsaPrivateKey.getEncoded());72RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(73encodedKeySpec);7475// Check for equality.76if (rsaPrivateKey.equals(rsaPrivateKey2)) {77throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");78}79if (!rsaPrivateKey3.equals(rsaPrivateKey)) {80throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");81}82if (rsaPrivateKey3.equals(rsaPrivateKey2)) {83throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");84}85if (rsaPrivateKey2.equals(rsaPrivateKey3)) {86throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");87}8889// Generate the fourth key.90RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey)rsaPrivateKey;91RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(92rsaPrivateCrtKey.getModulus(),93rsaPrivateCrtKey.getPublicExponent(),94rsaPrivateCrtKey.getPrivateExponent(),95rsaPrivateCrtKey.getPrimeP(),96rsaPrivateCrtKey.getPrimeQ(),97rsaPrivateCrtKey.getPrimeExponentP(),98rsaPrivateCrtKey.getPrimeExponentQ(),99rsaPrivateCrtKey.getCrtCoefficient()100);101RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(102rsaPrivateCrtKeySpec);103if (!rsaPrivateKey.equals(rsaPrivateKey4)) {104throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");105}106}107}108109110