J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4413634 |
| 27 | * @summary Make sure that RSA Private CRT Key factory generation using |
| 28 | * java.security.spec.RSAPrivateCrtKeySpec passes |
| 29 | */ |
| 30 | |
| 31 | import java.math.BigInteger; |
| 32 | import java.security.KeyFactory; |
| 33 | import java.security.interfaces.RSAPrivateCrtKey; |
| 34 | import java.security.spec.PKCS8EncodedKeySpec; |
| 35 | import java.security.spec.RSAPrivateCrtKeySpec; |
| 36 | import java.util.Arrays; |
| 37 | |
| 38 | public class GenerateRSAPrivateCrtKey { |
| 39 | |
| 40 | public static void main(String[] args) throws Exception { |
| 41 | |
| 42 | // Create an RSA Private Key from the CRT information |
| 43 | RSAPrivateCrtKeySpec rsaCrtSpec = |
| 44 | new RSAPrivateCrtKeySpec(new BigInteger(1, modulus), |
| 45 | new BigInteger(1, pubExpo), |
| 46 | new BigInteger(1, priExpo), |
| 47 | new BigInteger(1, primeP), |
| 48 | new BigInteger(1, primeQ), |
| 49 | new BigInteger(1, expoP), |
| 50 | new BigInteger(1, expoQ), |
| 51 | new BigInteger(1, coeff)); |
| 52 | |
| 53 | // Create an RSA private key from the CRT specification |
| 54 | KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign"); |
| 55 | RSAPrivateCrtKey rsaPriKey = |
| 56 | (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec); |
| 57 | |
| 58 | // test resulting key against original specification |
| 59 | if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient())) |
| 60 | throw new Exception("coefficients not equal"); |
| 61 | if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP())) |
| 62 | throw new Exception("primeExponentPs not equal"); |
| 63 | if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ())) |
| 64 | throw new Exception("primeExponentQs not equal"); |
| 65 | if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP())) |
| 66 | throw new Exception("primePs not equal"); |
| 67 | if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ())) |
| 68 | throw new Exception("primeQs not equal"); |
| 69 | if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent())) |
| 70 | throw new Exception("public exponents not equal"); |
| 71 | if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent())) |
| 72 | throw new Exception("private exponents not equal"); |
| 73 | if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus())) |
| 74 | throw new Exception("modulus not equal"); |
| 75 | if (!rsaPriKey.getFormat().equals("PKCS#8") && |
| 76 | !rsaPriKey.getFormat().equals("PKCS8")) |
| 77 | throw new Exception("format not PKCS#8"); |
| 78 | if (!rsaPriKey.getAlgorithm().equals("RSA")) |
| 79 | throw new Exception("algorithm not RSA"); |
| 80 | if (rsaPriKey.getEncoded() == null) |
| 81 | throw new Exception("encoded key is null"); |
| 82 | |
| 83 | PKCS8EncodedKeySpec pkcs8Key = |
| 84 | new PKCS8EncodedKeySpec(rsaPriKey.getEncoded()); |
| 85 | |
| 86 | RSAPrivateCrtKey rsaPriKey2 |
| 87 | = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key); |
| 88 | if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded())) |
| 89 | throw new Exception("encoded keys not equal"); |
| 90 | } |
| 91 | |
| 92 | static byte[] modulus = { |
| 93 | (byte)0xab, (byte)0x38, (byte)0x39, (byte)0x40, |
| 94 | (byte)0x54, (byte)0x2c, (byte)0xac, (byte)0x9a, |
| 95 | (byte)0xc0, (byte)0x37, (byte)0x40, (byte)0xd0, |
| 96 | (byte)0x49, (byte)0x04, (byte)0xed, (byte)0x51, |
| 97 | (byte)0x0e, (byte)0x95, (byte)0x72, (byte)0x02, |
| 98 | (byte)0x51, (byte)0xc2, (byte)0xad, (byte)0x9d, |
| 99 | (byte)0xa7, (byte)0xeb, (byte)0xba, (byte)0x29, |
| 100 | (byte)0xae, (byte)0xd4, (byte)0x49, (byte)0x79, |
| 101 | (byte)0x53, (byte)0xfa, (byte)0xdf, (byte)0x01, |
| 102 | (byte)0x6c, (byte)0xbc, (byte)0x69, (byte)0x46, |
| 103 | (byte)0x4c, (byte)0x83, (byte)0x1b, (byte)0xd9, |
| 104 | (byte)0x3b, (byte)0x59, (byte)0x42, (byte)0x04, |
| 105 | (byte)0x99, (byte)0x0f, (byte)0x63, (byte)0x24, |
| 106 | (byte)0x75, (byte)0xa0, (byte)0xbe, (byte)0x6f, |
| 107 | (byte)0x92, (byte)0x4d, (byte)0x9d, (byte)0xa2, |
| 108 | (byte)0x40, (byte)0xda, (byte)0xf8, (byte)0x49 |
| 109 | }; |
| 110 | |
| 111 | static byte[] pubExpo = { |
| 112 | (byte)0x01, (byte)0x00, (byte)0x01 |
| 113 | }; |
| 114 | |
| 115 | static byte[] priExpo = { |
| 116 | (byte)0x4a, (byte)0xd2, (byte)0xe7, (byte)0x32, |
| 117 | (byte)0x15, (byte)0x96, (byte)0xf0, (byte)0x57, |
| 118 | (byte)0x30, (byte)0x68, (byte)0xf5, (byte)0x0a, |
| 119 | (byte)0x10, (byte)0xde, (byte)0xf6, (byte)0x56, |
| 120 | (byte)0xd5, (byte)0xe8, (byte)0xb9, (byte)0x4a, |
| 121 | (byte)0x0a, (byte)0x30, (byte)0xe9, (byte)0x6e, |
| 122 | (byte)0x5c, (byte)0x53, (byte)0xc7, (byte)0xa7, |
| 123 | (byte)0x2f, (byte)0x9f, (byte)0xd5, (byte)0xfb, |
| 124 | (byte)0x58, (byte)0x9b, (byte)0x1e, (byte)0x5b, |
| 125 | (byte)0xe8, (byte)0x6e, (byte)0xae, (byte)0x02, |
| 126 | (byte)0xaa, (byte)0x15, (byte)0x23, (byte)0x67, |
| 127 | (byte)0xaa, (byte)0x20, (byte)0x9e, (byte)0x82, |
| 128 | (byte)0x76, (byte)0x4c, (byte)0xad, (byte)0xe1, |
| 129 | (byte)0x95, (byte)0xde, (byte)0xe3, (byte)0x25, |
| 130 | (byte)0x66, (byte)0x2f, (byte)0xb0, (byte)0xab, |
| 131 | (byte)0x1c, (byte)0xe5, (byte)0xa0, (byte)0x01 |
| 132 | }; |
| 133 | |
| 134 | static byte[] primeP = { |
| 135 | (byte)0xd1, (byte)0xeb, (byte)0x51, (byte)0xbd, |
| 136 | (byte)0x09, (byte)0x26, (byte)0x7e, (byte)0xe7, |
| 137 | (byte)0x12, (byte)0x8c, (byte)0xeb, (byte)0x5c, |
| 138 | (byte)0x32, (byte)0x18, (byte)0xd1, (byte)0x60, |
| 139 | (byte)0x0b, (byte)0x49, (byte)0x67, (byte)0x8f, |
| 140 | (byte)0x78, (byte)0x3c, (byte)0x58, (byte)0xc5, |
| 141 | (byte)0xb0, (byte)0x01, (byte)0x70, (byte)0xee, |
| 142 | (byte)0x1a, (byte)0xcf, (byte)0x6e, (byte)0xe1 |
| 143 | }; |
| 144 | |
| 145 | static byte[] primeQ = { |
| 146 | (byte)0xd0, (byte)0xce, (byte)0x21, (byte)0x83, |
| 147 | (byte)0x41, (byte)0x73, (byte)0xf6, (byte)0x84, |
| 148 | (byte)0x32, (byte)0x06, (byte)0xa8, (byte)0xa6, |
| 149 | (byte)0xad, (byte)0x13, (byte)0x2b, (byte)0x65, |
| 150 | (byte)0x27, (byte)0x86, (byte)0x28, (byte)0xef, |
| 151 | (byte)0x0e, (byte)0x8c, (byte)0xca, (byte)0x4f, |
| 152 | (byte)0x20, (byte)0xc0, (byte)0x19, (byte)0x95, |
| 153 | (byte)0xfe, (byte)0x6c, (byte)0x3e, (byte)0x69 |
| 154 | }; |
| 155 | |
| 156 | static byte[] expoP = { |
| 157 | (byte)0x1a, (byte)0x49, (byte)0x9c, (byte)0xb7, |
| 158 | (byte)0xce, (byte)0x80, (byte)0x8a, (byte)0x9d, |
| 159 | (byte)0xc7, (byte)0x3d, (byte)0xec, (byte)0x6f, |
| 160 | (byte)0x64, (byte)0x3a, (byte)0xa5, (byte)0x65, |
| 161 | (byte)0xa0, (byte)0xa4, (byte)0x35, (byte)0x9a, |
| 162 | (byte)0xca, (byte)0xd4, (byte)0xcb, (byte)0xcd, |
| 163 | (byte)0x1d, (byte)0xc8, (byte)0x60, (byte)0x6b, |
| 164 | (byte)0x00, (byte)0xe2, (byte)0x7f, (byte)0x21 |
| 165 | }; |
| 166 | |
| 167 | static byte[] expoQ = { |
| 168 | (byte)0xa7, (byte)0x93, (byte)0xd7, (byte)0x77, |
| 169 | (byte)0x94, (byte)0xef, (byte)0x31, (byte)0x78, |
| 170 | (byte)0x55, (byte)0x01, (byte)0xdd, (byte)0x16, |
| 171 | (byte)0xaf, (byte)0xae, (byte)0xc3, (byte)0xd4, |
| 172 | (byte)0x12, (byte)0x0d, (byte)0x6d, (byte)0x0a, |
| 173 | (byte)0xb6, (byte)0xdd, (byte)0xad, (byte)0x7c, |
| 174 | (byte)0x25, (byte)0xe7, (byte)0xa6, (byte)0x61, |
| 175 | (byte)0x27, (byte)0xe8, (byte)0xcd, (byte)0x89 |
| 176 | }; |
| 177 | |
| 178 | static byte[] coeff = { |
| 179 | (byte)0x0b, (byte)0xdb, (byte)0x90, (byte)0x7f, |
| 180 | (byte)0x33, (byte)0xc5, (byte)0x1f, (byte)0x5b, |
| 181 | (byte)0x4d, (byte)0xa4, (byte)0x86, (byte)0xda, |
| 182 | (byte)0x77, (byte)0xd4, (byte)0xb3, (byte)0x1d, |
| 183 | (byte)0xbc, (byte)0xc3, (byte)0xae, (byte)0x0b, |
| 184 | (byte)0xac, (byte)0x91, (byte)0xf3, (byte)0x38, |
| 185 | (byte)0x4a, (byte)0xcf, (byte)0x10, (byte)0xb1, |
| 186 | (byte)0x5e, (byte)0x5a, (byte)0xd1, (byte)0x86 |
| 187 | }; |
| 188 | } |