1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8181048
  27  * @summary verify that when the returned DSA KeyPairGenerator is
  28  * an instance of java.security.interfaces.DSAKeyPairGenerator,
  29  * the behavior is compliant with the javadoc spec.
  30  * @run main/othervm -Djdk.security.legacyDSAKeyPairGenerator=tRUe TestLegacyDSAKeyPairGenerator
  31  */
  32 
  33 import java.security.*;
  34 import java.security.interfaces.*;
  35 
  36 public class TestLegacyDSAKeyPairGenerator {
  37 
  38     private static void checkKeyLength(KeyPair kp, int len) throws Exception {
  39         DSAPublicKey key = (DSAPublicKey)kp.getPublic();
  40         int n = key.getParams().getP().bitLength();
  41         System.out.println("Key length: " + n);
  42         if (len != n) {
  43             throw new Exception("Wrong key length");
  44         }
  45     }
  46 
  47     public static void main(String[] args) throws Exception {
  48         KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", "SUN");
  49         // check the returned object implements the legacy interface
  50         if (!(kpg instanceof DSAKeyPairGenerator)) {
  51             throw new Exception("Should be an instance of DSAKeyPairGenerator");
  52         }
  53         System.out.println("Returned an instance of DSAKeyPairGenerator");
  54         // check the default key size is 1024 when initiaize(..) is not called
  55         KeyPair kp1 = kpg.generateKeyPair();
  56         checkKeyLength(kp1, 1024);
  57         KeyPair kp2 = kpg.generateKeyPair();
  58         checkKeyLength(kp2, 1024);
  59         System.out.println("Used 1024 default key size");
  60 
  61         // check kp1 and kp2 uses the same DSA parameters p, q, g
  62         DSAParams param1 = ((DSAPublicKey)kp1.getPublic()).getParams();
  63         DSAParams param2 = ((DSAPublicKey)kp2.getPublic()).getParams();
  64         if ((param1.getP().compareTo(param2.getP()) != 0) ||
  65             (param1.getQ().compareTo(param2.getQ()) != 0) ||
  66             (param1.getG().compareTo(param2.getG()) != 0)) {
  67             throw new RuntimeException("Key params mismatch");
  68         }
  69         System.out.println("Used same default params");
  70 
  71         // check that the documented exception is thrown if no cached parameters
  72         int sizeNotInCache = (1024 - 64);
  73         try {
  74             ((DSAKeyPairGenerator)kpg).initialize(sizeNotInCache, false, null);
  75             throw new RuntimeException("Expected IPE not thrown");
  76         } catch (InvalidParameterException ipe) {
  77             System.out.println("Throwed expected IPE");
  78         }
  79         ((DSAKeyPairGenerator)kpg).initialize(sizeNotInCache, true, null);
  80         KeyPair kp = kpg.generateKeyPair();
  81         checkKeyLength(kp, sizeNotInCache);
  82         System.out.println("Generated requested key size");
  83     }
  84 }