Path: blob/master/test/jdk/sun/security/provider/DSA/TestLegacyDSAKeyPairGenerator.java
41154 views
/*1* Copyright (c) 2017, 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 818104826* @summary verify that when the returned DSA KeyPairGenerator is27* an instance of java.security.interfaces.DSAKeyPairGenerator,28* the behavior is compliant with the javadoc spec.29* @run main/othervm -Djdk.security.legacyDSAKeyPairGenerator=tRUe TestLegacyDSAKeyPairGenerator30*/3132import java.security.*;33import java.security.interfaces.*;3435public class TestLegacyDSAKeyPairGenerator {3637private static void checkKeyLength(KeyPair kp, int len) throws Exception {38DSAPublicKey key = (DSAPublicKey)kp.getPublic();39int n = key.getParams().getP().bitLength();40System.out.println("Key length: " + n);41if (len != n) {42throw new Exception("Wrong key length");43}44}4546public static void main(String[] args) throws Exception {47KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", "SUN");48// check the returned object implements the legacy interface49if (!(kpg instanceof DSAKeyPairGenerator)) {50throw new Exception("Should be an instance of DSAKeyPairGenerator");51}52System.out.println("Returned an instance of DSAKeyPairGenerator");53// check the default key size is 1024 when initiaize(..) is not called54KeyPair kp1 = kpg.generateKeyPair();55checkKeyLength(kp1, 1024);56KeyPair kp2 = kpg.generateKeyPair();57checkKeyLength(kp2, 1024);58System.out.println("Used 1024 default key size");5960// check kp1 and kp2 uses the same DSA parameters p, q, g61DSAParams param1 = ((DSAPublicKey)kp1.getPublic()).getParams();62DSAParams param2 = ((DSAPublicKey)kp2.getPublic()).getParams();63if ((param1.getP().compareTo(param2.getP()) != 0) ||64(param1.getQ().compareTo(param2.getQ()) != 0) ||65(param1.getG().compareTo(param2.getG()) != 0)) {66throw new RuntimeException("Key params mismatch");67}68System.out.println("Used same default params");6970// check that the documented exception is thrown if no cached parameters71int sizeNotInCache = (1024 - 64);72try {73((DSAKeyPairGenerator)kpg).initialize(sizeNotInCache, false, null);74throw new RuntimeException("Expected IPE not thrown");75} catch (InvalidParameterException ipe) {76System.out.println("Throwed expected IPE");77}78((DSAKeyPairGenerator)kpg).initialize(sizeNotInCache, true, null);79KeyPair kp = kpg.generateKeyPair();80checkKeyLength(kp, sizeNotInCache);81System.out.println("Generated requested key size");82}83}848586