Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyRep/Serial.java
38811 views
/*1* Copyright (c) 2003, 2004, 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/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// exclude test is ECF provider is installed, see 492329091if (Security.getProvider("SunPKCS11-Solaris") == null) {92// generate DH key pair93kpg = KeyPairGenerator.getInstance("DiffieHellman", JCE);94kpg.initialize(new DHParameterSpec(skip1024Modulus, skip1024Base));95KeyPair dhKp = kpg.genKeyPair();9697// serialize DH key pair98baos.reset();99oos = new ObjectOutputStream(baos);100oos.writeObject(dhKp);101oos.close();102103// deserialize DH key pair104ois = new ObjectInputStream105(new ByteArrayInputStream(baos.toByteArray()));106KeyPair dhKp2 = (KeyPair)ois.readObject();107ois.close();108109if (!dhKp2.getPublic().equals(dhKp.getPublic()) ||110!dhKp2.getPrivate().equals(dhKp.getPrivate())) {111throw new SecurityException("DH test failed");112}113}114115// generate RC5 key116SecretKeySpec rc5Key = new SecretKeySpec(new byte[128], "RC5");117118// serialize RC5 key119baos.reset();120oos = new ObjectOutputStream(baos);121oos.writeObject(rc5Key);122oos.close();123124// deserialize RC5 key125ois = new ObjectInputStream126(new ByteArrayInputStream(baos.toByteArray()));127SecretKey rc5Key2 = (SecretKey)ois.readObject();128ois.close();129130if (!rc5Key.equals(rc5Key2)) {131throw new SecurityException("RC5 test failed");132}133134// generate PBE key135136// Salt137byte[] salt = {138(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,139(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99140};141142// Iteration count143int count = 20;144145// Create PBE parameter set146PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);147148char[] password = new char[] {'f', 'o', 'o'};149PBEKeySpec pbeKeySpec = new PBEKeySpec(password);150SecretKeyFactory keyFac =151SecretKeyFactory.getInstance("PBEWithMD5AndDES", JCE);152SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);153154// serialize PBE key155baos.reset();156oos = new ObjectOutputStream(baos);157oos.writeObject(pbeKey);158oos.close();159160// deserialize PBE key161ois = new ObjectInputStream162(new ByteArrayInputStream(baos.toByteArray()));163SecretKey pbeKey2 = (SecretKey)ois.readObject();164ois.close();165166if (!pbeKey.equals(pbeKey2)) {167throw new SecurityException("PBE test failed");168}169170checkKey("AES", 128);171checkKey("Blowfish", -1);172checkKey("DES", 56);173checkKey("DESede", 168);174checkKey("HmacMD5", -1);175checkKey("HmacSHA1", -1);176}177178private static void checkKey(String algorithm, int size) throws Exception {179// generate key180KeyGenerator kg = KeyGenerator.getInstance(algorithm, JCE);181if (size > 0) {182kg.init(size);183}184SecretKey key = kg.generateKey();185186// serialize key187ByteArrayOutputStream baos = new ByteArrayOutputStream();188ObjectOutputStream oos = new ObjectOutputStream(baos);189oos.writeObject(key);190oos.close();191192// deserialize key193ObjectInputStream ois = new ObjectInputStream194(new ByteArrayInputStream(baos.toByteArray()));195SecretKey key2 = (SecretKey)ois.readObject();196ois.close();197198if (!key.equals(key2)) {199throw new SecurityException(algorithm + " test failed");200}201}202203// The 1024 bit Diffie-Hellman modulus values used by SKIP204private static final byte skip1024ModulusBytes[] = {205(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,206(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,207(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,208(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,209(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,210(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,211(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,212(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,213(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,214(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,215(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,216(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,217(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,218(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,219(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,220(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,221(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,222(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,223(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,224(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,225(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,226(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,227(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,228(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,229(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,230(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,231(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,232(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,233(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,234(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,235(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,236(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7237};238239// The SKIP 1024 bit modulus240private static final BigInteger skip1024Modulus241= new BigInteger(1, skip1024ModulusBytes);242243// The base used with the SKIP 1024 bit modulus244private static final BigInteger skip1024Base = BigInteger.valueOf(2);245}246247248