Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyPairGenerator/SolarisShortDSA.java
38812 views
/*1* Copyright (c) 2011, 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 708141126* @summary DSA keypair generation affected by Solaris bug27*/2829import java.security.KeyPair;30import java.security.KeyPairGenerator;31import java.security.Signature;32import sun.security.provider.DSAPrivateKey;3334public class SolarisShortDSA {35static byte[] data = new byte[0];36public static void main(String args[]) throws Exception {37for (int i=0; i<10000; i++) {38KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");39KeyPair kp = kpg.generateKeyPair();40DSAPrivateKey dpk = (DSAPrivateKey)kp.getPrivate();41int len = dpk.getX().bitLength();42if (len <= 152) {43if (!use(kp)) {44String os = System.getProperty("os.name");45// Solaris bug, update the following line once it's fixed46if (os.equals("SunOS")) {47throw new IllegalStateException(48"Don't panic. This is a Solaris bug");49} else {50throw new RuntimeException("Real test failure");51}52}53break;54}55}56}5758static boolean use(KeyPair kp) throws Exception {59Signature sig = Signature.getInstance("SHA1withDSA");60sig.initSign(kp.getPrivate());61sig.update(data);62byte[] signed = sig.sign();63Signature sig2 = Signature.getInstance("SHA1withDSA");64sig2.initVerify(kp.getPublic());65sig2.update(data);66return sig2.verify(signed);67}68}697071