Path: blob/master/test/jdk/java/security/KeyRep/Serial.java
41149 views
/*1* Copyright (c) 2003, 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 4532506 499959926* @summary Serializing KeyPair on one VM (Sun),27* and Deserializing on another (IBM) fails28* @run main/othervm/java.security.policy=Serial.policy Serial29*/3031import java.io.*;32import java.math.BigInteger;33import java.security.*;34import javax.crypto.*;35import javax.crypto.spec.*;3637public class Serial {3839// providers40private static final String SUN = "SUN";41private static final String RSA = "SunRsaSign";42private static final String JCE = "SunJCE";4344public static void main(String[] args) throws Exception {4546// generate DSA key pair47KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", SUN);48kpg.initialize(512);49KeyPair dsaKp = kpg.genKeyPair();5051// serialize DSA key pair52ByteArrayOutputStream baos = new ByteArrayOutputStream();53ObjectOutputStream oos = new ObjectOutputStream(baos);54oos.writeObject(dsaKp);55oos.close();5657// deserialize DSA key pair58ObjectInputStream ois = new ObjectInputStream59(new ByteArrayInputStream(baos.toByteArray()));60KeyPair dsaKp2 = (KeyPair)ois.readObject();61ois.close();6263if (!dsaKp2.getPublic().equals(dsaKp.getPublic()) ||64!dsaKp2.getPrivate().equals(dsaKp.getPrivate())) {65throw new SecurityException("DSA test failed");66}6768// generate RSA key pair69kpg = KeyPairGenerator.getInstance("RSA", RSA);70kpg.initialize(512);71KeyPair rsaKp = kpg.genKeyPair();7273// serialize RSA key pair74baos.reset();75oos = new ObjectOutputStream(baos);76oos.writeObject(rsaKp);77oos.close();7879// deserialize RSA key pair80ois = new ObjectInputStream81(new ByteArrayInputStream(baos.toByteArray()));82KeyPair rsaKp2 = (KeyPair)ois.readObject();83ois.close();8485if (!rsaKp2.getPublic().equals(rsaKp.getPublic()) ||86!rsaKp2.getPrivate().equals(rsaKp.getPrivate())) {87throw new SecurityException("RSA test failed");88}8990// generate DH key pair91kpg = KeyPairGenerator.getInstance("DiffieHellman", JCE);92kpg.initialize(new DHParameterSpec(skip1024Modulus, skip1024Base));93KeyPair dhKp = kpg.genKeyPair();9495// serialize DH key pair96baos.reset();97oos = new ObjectOutputStream(baos);98oos.writeObject(dhKp);99oos.close();100101// deserialize DH key pair102ois = new ObjectInputStream103(new ByteArrayInputStream(baos.toByteArray()));104KeyPair dhKp2 = (KeyPair)ois.readObject();105ois.close();106107if (!dhKp2.getPublic().equals(dhKp.getPublic()) ||108!dhKp2.getPrivate().equals(dhKp.getPrivate())) {109throw new SecurityException("DH test failed");110}111112// generate RC5 key113SecretKeySpec rc5Key = new SecretKeySpec(new byte[128], "RC5");114115// serialize RC5 key116baos.reset();117oos = new ObjectOutputStream(baos);118oos.writeObject(rc5Key);119oos.close();120121// deserialize RC5 key122ois = new ObjectInputStream123(new ByteArrayInputStream(baos.toByteArray()));124SecretKey rc5Key2 = (SecretKey)ois.readObject();125ois.close();126127if (!rc5Key.equals(rc5Key2)) {128throw new SecurityException("RC5 test failed");129}130131// generate PBE key132133// Salt134byte[] salt = {135(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,136(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99137};138139// Iteration count140int count = 20;141142// Create PBE parameter set143PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);144145char[] password = new char[] {'f', 'o', 'o'};146PBEKeySpec pbeKeySpec = new PBEKeySpec(password);147SecretKeyFactory keyFac =148SecretKeyFactory.getInstance("PBEWithMD5AndDES", JCE);149SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);150151// serialize PBE key152baos.reset();153oos = new ObjectOutputStream(baos);154oos.writeObject(pbeKey);155oos.close();156157// deserialize PBE key158ois = new ObjectInputStream159(new ByteArrayInputStream(baos.toByteArray()));160SecretKey pbeKey2 = (SecretKey)ois.readObject();161ois.close();162163if (!pbeKey.equals(pbeKey2)) {164throw new SecurityException("PBE test failed");165}166167checkKey("AES", 128);168checkKey("Blowfish", -1);169checkKey("DES", 56);170checkKey("DESede", 168);171checkKey("HmacMD5", -1);172checkKey("HmacSHA1", -1);173}174175private static void checkKey(String algorithm, int size) throws Exception {176// generate key177KeyGenerator kg = KeyGenerator.getInstance(algorithm, JCE);178if (size > 0) {179kg.init(size);180}181SecretKey key = kg.generateKey();182183// serialize key184ByteArrayOutputStream baos = new ByteArrayOutputStream();185ObjectOutputStream oos = new ObjectOutputStream(baos);186oos.writeObject(key);187oos.close();188189// deserialize key190ObjectInputStream ois = new ObjectInputStream191(new ByteArrayInputStream(baos.toByteArray()));192SecretKey key2 = (SecretKey)ois.readObject();193ois.close();194195if (!key.equals(key2)) {196throw new SecurityException(algorithm + " test failed");197}198}199200// The 1024 bit Diffie-Hellman modulus values used by SKIP201private static final byte skip1024ModulusBytes[] = {202(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,203(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,204(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,205(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,206(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,207(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,208(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,209(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,210(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,211(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,212(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,213(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,214(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,215(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,216(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,217(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,218(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,219(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,220(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,221(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,222(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,223(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,224(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,225(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,226(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,227(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,228(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,229(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,230(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,231(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,232(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,233(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7234};235236// The SKIP 1024 bit modulus237private static final BigInteger skip1024Modulus238= new BigInteger(1, skip1024ModulusBytes);239240// The base used with the SKIP 1024 bit modulus241private static final BigInteger skip1024Base = BigInteger.valueOf(2);242}243244245