Path: blob/master/test/jdk/java/security/KeyFactory/GenerateRSAPrivateCrtKey.java
41149 views
/*1* Copyright (c) 2001, 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 441363426* @summary Make sure that RSA Private CRT Key factory generation using27* java.security.spec.RSAPrivateCrtKeySpec passes28*/2930import java.math.BigInteger;31import java.security.KeyFactory;32import java.security.interfaces.RSAPrivateCrtKey;33import java.security.spec.PKCS8EncodedKeySpec;34import java.security.spec.RSAPrivateCrtKeySpec;35import java.util.Arrays;3637public class GenerateRSAPrivateCrtKey {3839public static void main(String[] args) throws Exception {4041// Create an RSA Private Key from the CRT information42RSAPrivateCrtKeySpec rsaCrtSpec =43new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),44new BigInteger(1, pubExpo),45new BigInteger(1, priExpo),46new BigInteger(1, primeP),47new BigInteger(1, primeQ),48new BigInteger(1, expoP),49new BigInteger(1, expoQ),50new BigInteger(1, coeff));5152// Create an RSA private key from the CRT specification53KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");54RSAPrivateCrtKey rsaPriKey =55(RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);5657// test resulting key against original specification58if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))59throw new Exception("coefficients not equal");60if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))61throw new Exception("primeExponentPs not equal");62if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))63throw new Exception("primeExponentQs not equal");64if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))65throw new Exception("primePs not equal");66if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))67throw new Exception("primeQs not equal");68if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))69throw new Exception("public exponents not equal");70if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))71throw new Exception("private exponents not equal");72if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))73throw new Exception("modulus not equal");74if (!rsaPriKey.getFormat().equals("PKCS#8") &&75!rsaPriKey.getFormat().equals("PKCS8"))76throw new Exception("format not PKCS#8");77if (!rsaPriKey.getAlgorithm().equals("RSA"))78throw new Exception("algorithm not RSA");79if (rsaPriKey.getEncoded() == null)80throw new Exception("encoded key is null");8182PKCS8EncodedKeySpec pkcs8Key =83new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());8485RSAPrivateCrtKey rsaPriKey286= (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);87if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))88throw new Exception("encoded keys not equal");89}9091static byte[] modulus = {92(byte)0xab, (byte)0x38, (byte)0x39, (byte)0x40,93(byte)0x54, (byte)0x2c, (byte)0xac, (byte)0x9a,94(byte)0xc0, (byte)0x37, (byte)0x40, (byte)0xd0,95(byte)0x49, (byte)0x04, (byte)0xed, (byte)0x51,96(byte)0x0e, (byte)0x95, (byte)0x72, (byte)0x02,97(byte)0x51, (byte)0xc2, (byte)0xad, (byte)0x9d,98(byte)0xa7, (byte)0xeb, (byte)0xba, (byte)0x29,99(byte)0xae, (byte)0xd4, (byte)0x49, (byte)0x79,100(byte)0x53, (byte)0xfa, (byte)0xdf, (byte)0x01,101(byte)0x6c, (byte)0xbc, (byte)0x69, (byte)0x46,102(byte)0x4c, (byte)0x83, (byte)0x1b, (byte)0xd9,103(byte)0x3b, (byte)0x59, (byte)0x42, (byte)0x04,104(byte)0x99, (byte)0x0f, (byte)0x63, (byte)0x24,105(byte)0x75, (byte)0xa0, (byte)0xbe, (byte)0x6f,106(byte)0x92, (byte)0x4d, (byte)0x9d, (byte)0xa2,107(byte)0x40, (byte)0xda, (byte)0xf8, (byte)0x49108};109110static byte[] pubExpo = {111(byte)0x01, (byte)0x00, (byte)0x01112};113114static byte[] priExpo = {115(byte)0x4a, (byte)0xd2, (byte)0xe7, (byte)0x32,116(byte)0x15, (byte)0x96, (byte)0xf0, (byte)0x57,117(byte)0x30, (byte)0x68, (byte)0xf5, (byte)0x0a,118(byte)0x10, (byte)0xde, (byte)0xf6, (byte)0x56,119(byte)0xd5, (byte)0xe8, (byte)0xb9, (byte)0x4a,120(byte)0x0a, (byte)0x30, (byte)0xe9, (byte)0x6e,121(byte)0x5c, (byte)0x53, (byte)0xc7, (byte)0xa7,122(byte)0x2f, (byte)0x9f, (byte)0xd5, (byte)0xfb,123(byte)0x58, (byte)0x9b, (byte)0x1e, (byte)0x5b,124(byte)0xe8, (byte)0x6e, (byte)0xae, (byte)0x02,125(byte)0xaa, (byte)0x15, (byte)0x23, (byte)0x67,126(byte)0xaa, (byte)0x20, (byte)0x9e, (byte)0x82,127(byte)0x76, (byte)0x4c, (byte)0xad, (byte)0xe1,128(byte)0x95, (byte)0xde, (byte)0xe3, (byte)0x25,129(byte)0x66, (byte)0x2f, (byte)0xb0, (byte)0xab,130(byte)0x1c, (byte)0xe5, (byte)0xa0, (byte)0x01131};132133static byte[] primeP = {134(byte)0xd1, (byte)0xeb, (byte)0x51, (byte)0xbd,135(byte)0x09, (byte)0x26, (byte)0x7e, (byte)0xe7,136(byte)0x12, (byte)0x8c, (byte)0xeb, (byte)0x5c,137(byte)0x32, (byte)0x18, (byte)0xd1, (byte)0x60,138(byte)0x0b, (byte)0x49, (byte)0x67, (byte)0x8f,139(byte)0x78, (byte)0x3c, (byte)0x58, (byte)0xc5,140(byte)0xb0, (byte)0x01, (byte)0x70, (byte)0xee,141(byte)0x1a, (byte)0xcf, (byte)0x6e, (byte)0xe1142};143144static byte[] primeQ = {145(byte)0xd0, (byte)0xce, (byte)0x21, (byte)0x83,146(byte)0x41, (byte)0x73, (byte)0xf6, (byte)0x84,147(byte)0x32, (byte)0x06, (byte)0xa8, (byte)0xa6,148(byte)0xad, (byte)0x13, (byte)0x2b, (byte)0x65,149(byte)0x27, (byte)0x86, (byte)0x28, (byte)0xef,150(byte)0x0e, (byte)0x8c, (byte)0xca, (byte)0x4f,151(byte)0x20, (byte)0xc0, (byte)0x19, (byte)0x95,152(byte)0xfe, (byte)0x6c, (byte)0x3e, (byte)0x69153};154155static byte[] expoP = {156(byte)0x1a, (byte)0x49, (byte)0x9c, (byte)0xb7,157(byte)0xce, (byte)0x80, (byte)0x8a, (byte)0x9d,158(byte)0xc7, (byte)0x3d, (byte)0xec, (byte)0x6f,159(byte)0x64, (byte)0x3a, (byte)0xa5, (byte)0x65,160(byte)0xa0, (byte)0xa4, (byte)0x35, (byte)0x9a,161(byte)0xca, (byte)0xd4, (byte)0xcb, (byte)0xcd,162(byte)0x1d, (byte)0xc8, (byte)0x60, (byte)0x6b,163(byte)0x00, (byte)0xe2, (byte)0x7f, (byte)0x21164};165166static byte[] expoQ = {167(byte)0xa7, (byte)0x93, (byte)0xd7, (byte)0x77,168(byte)0x94, (byte)0xef, (byte)0x31, (byte)0x78,169(byte)0x55, (byte)0x01, (byte)0xdd, (byte)0x16,170(byte)0xaf, (byte)0xae, (byte)0xc3, (byte)0xd4,171(byte)0x12, (byte)0x0d, (byte)0x6d, (byte)0x0a,172(byte)0xb6, (byte)0xdd, (byte)0xad, (byte)0x7c,173(byte)0x25, (byte)0xe7, (byte)0xa6, (byte)0x61,174(byte)0x27, (byte)0xe8, (byte)0xcd, (byte)0x89175};176177static byte[] coeff = {178(byte)0x0b, (byte)0xdb, (byte)0x90, (byte)0x7f,179(byte)0x33, (byte)0xc5, (byte)0x1f, (byte)0x5b,180(byte)0x4d, (byte)0xa4, (byte)0x86, (byte)0xda,181(byte)0x77, (byte)0xd4, (byte)0xb3, (byte)0x1d,182(byte)0xbc, (byte)0xc3, (byte)0xae, (byte)0x0b,183(byte)0xac, (byte)0x91, (byte)0xf3, (byte)0x38,184(byte)0x4a, (byte)0xcf, (byte)0x10, (byte)0xb1,185(byte)0x5e, (byte)0x5a, (byte)0xd1, (byte)0x86186};187}188189190