TestDSAGenParameterSpec.java revision 12801:a831c364751d
168349Sobrien/* 268349Sobrien * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3267843Sdelphij * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 468349Sobrien * 568349Sobrien * This code is free software; you can redistribute it and/or modify it 6267843Sdelphij * under the terms of the GNU General Public License version 2 only, as 7267843Sdelphij * published by the Free Software Foundation. 8267843Sdelphij * 9267843Sdelphij * This code is distributed in the hope that it will be useful, but WITHOUT 1068349Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11267843Sdelphij * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12267843Sdelphij * version 2 for more details (a copy is included in the LICENSE file that 13267843Sdelphij * accompanied this code). 14267843Sdelphij * 1568349Sobrien * You should have received a copy of the GNU General Public License version 16267843Sdelphij * 2 along with this work; if not, write to the Free Software Foundation, 1768349Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18267843Sdelphij * 1968349Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20267843Sdelphij * or visit www.oracle.com if you need additional information or have any 21267843Sdelphij * questions. 22267843Sdelphij */ 23267843Sdelphij 24267843Sdelphijimport java.security.AlgorithmParameterGenerator; 25267843Sdelphijimport java.security.AlgorithmParameters; 26267843Sdelphijimport java.security.InvalidAlgorithmParameterException; 27267843Sdelphijimport java.security.InvalidParameterException; 28267843Sdelphijimport java.security.KeyPairGenerator; 29267843Sdelphijimport java.security.NoSuchAlgorithmException; 30267843Sdelphijimport java.security.NoSuchProviderException; 31267843Sdelphijimport java.security.spec.DSAGenParameterSpec; 3268349Sobrienimport java.security.spec.DSAParameterSpec; 3368349Sobrienimport java.security.spec.InvalidParameterSpecException; 3468349Sobrienimport java.util.Arrays; 35import java.util.List; 36 37/* 38 * @test 39 * @bug 8075286 40 * @summary Verify that DSAGenParameterSpec can and can only be used to generate 41 * DSA within some certain range of key sizes as described in the class 42 * specification (L, N) as (1024, 160), (2048, 224), (2048, 256) and 43 * (3072, 256) should be OK for DSAGenParameterSpec. But the real 44 * implementation SUN doesn't support (3072, 256). 45 * @run main TestDSAGenParameterSpec 46 */ 47public class TestDSAGenParameterSpec { 48 49 private static final String ALGORITHM_NAME = "DSA"; 50 private static final String PROVIDER_NAME = "SUN"; 51 52 private static final List<DataTuple> DATA = Arrays.asList( 53 new DataTuple(1024, 160, true, true), 54 new DataTuple(2048, 224, true, true), 55 new DataTuple(2048, 256, true, true), 56 new DataTuple(3072, 256, true, false), 57 new DataTuple(1024, 224), 58 new DataTuple(2048, 160), 59 new DataTuple(4096, 256), 60 new DataTuple(512, 160), 61 new DataTuple(3072, 224)); 62 63 private static void testDSAGenParameterSpec(DataTuple dataTuple) 64 throws NoSuchAlgorithmException, NoSuchProviderException, 65 InvalidParameterSpecException, InvalidAlgorithmParameterException { 66 System.out.printf("Test case: primePLen=%d, " + "subprimeQLen=%d%n", 67 dataTuple.primePLen, dataTuple.subprimeQLen); 68 69 AlgorithmParameterGenerator apg = 70 AlgorithmParameterGenerator.getInstance(ALGORITHM_NAME, 71 PROVIDER_NAME); 72 73 DSAGenParameterSpec genParamSpec = createGenParameterSpec(dataTuple); 74 // genParamSpec will be null if IllegalAE is thrown when expected. 75 if (genParamSpec == null) { 76 return; 77 } 78 79 try { 80 apg.init(genParamSpec, null); 81 AlgorithmParameters param = apg.generateParameters(); 82 83 checkParam(param, genParamSpec); 84 System.out.println("Test case passed"); 85 } catch (InvalidParameterException ipe) { 86 // The DSAGenParameterSpec API support this, but the real 87 // implementation in SUN doesn't 88 if (!dataTuple.isSunProviderSupported) { 89 System.out.println("Test case passed: expected " 90 + "InvalidParameterException is caught"); 91 } else { 92 throw new RuntimeException("Test case failed.", ipe); 93 } 94 } 95 } 96 97 private static void checkParam(AlgorithmParameters param, 98 DSAGenParameterSpec genParam) throws InvalidParameterSpecException, 99 NoSuchAlgorithmException, NoSuchProviderException, 100 InvalidAlgorithmParameterException { 101 String algorithm = param.getAlgorithm(); 102 if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) { 103 throw new RuntimeException( 104 "Unexpected type of parameters: " + algorithm); 105 } 106 107 DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class); 108 int valueL = spec.getP().bitLength(); 109 int strengthP = genParam.getPrimePLength(); 110 if (strengthP != valueL) { 111 System.out.printf("P: Expected %d but actual %d%n", strengthP, 112 valueL); 113 throw new RuntimeException("Wrong P strength"); 114 } 115 116 int valueN = spec.getQ().bitLength(); 117 int strengthQ = genParam.getSubprimeQLength(); 118 if (strengthQ != valueN) { 119 System.out.printf("Q: Expected %d but actual %d%n", strengthQ, 120 valueN); 121 throw new RuntimeException("Wrong Q strength"); 122 } 123 124 if (genParam.getSubprimeQLength() != genParam.getSeedLength()) { 125 System.out.println("Defaut seed length should be the same as Q."); 126 throw new RuntimeException("Wrong seed length"); 127 } 128 129 // use the parameters to generate real DSA keys 130 KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME, 131 PROVIDER_NAME); 132 keyGen.initialize(spec); 133 keyGen.generateKeyPair(); 134 } 135 136 private static DSAGenParameterSpec createGenParameterSpec( 137 DataTuple dataTuple) { 138 DSAGenParameterSpec genParamSpec = null; 139 try { 140 genParamSpec = new DSAGenParameterSpec(dataTuple.primePLen, 141 dataTuple.subprimeQLen); 142 if (!dataTuple.isDSASpecSupported) { 143 throw new RuntimeException( 144 "Test case failed: the key length must not supported"); 145 } 146 } catch (IllegalArgumentException e) { 147 if (!dataTuple.isDSASpecSupported) { 148 System.out.println("Test case passed: expected " 149 + "IllegalArgumentException is caught"); 150 } else { 151 throw new RuntimeException("Test case failed: unexpected " 152 + "IllegalArgumentException is thrown", e); 153 } 154 } 155 156 return genParamSpec; 157 } 158 159 public static void main(String[] args) throws Exception { 160 for (DataTuple dataTuple : DATA) { 161 testDSAGenParameterSpec(dataTuple); 162 } 163 System.out.println("All tests passed"); 164 } 165 166 private static class DataTuple { 167 168 private int primePLen; 169 private int subprimeQLen; 170 private boolean isDSASpecSupported; 171 private boolean isSunProviderSupported; 172 173 private DataTuple(int primePLen, int subprimeQLen, 174 boolean isDSASpecSupported, boolean isSunProviderSupported) { 175 this.primePLen = primePLen; 176 this.subprimeQLen = subprimeQLen; 177 this.isDSASpecSupported = isDSASpecSupported; 178 this.isSunProviderSupported = isSunProviderSupported; 179 } 180 181 private DataTuple(int primePLen, int subprimeQLen) { 182 this(primePLen, subprimeQLen, false, false); 183 } 184 } 185} 186