Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/NSASuiteB/TestDSAGenParameterSpec.java
38854 views
1
/*
2
* Copyright (c) 2015, 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
import java.security.AlgorithmParameterGenerator;
25
import java.security.AlgorithmParameters;
26
import java.security.InvalidAlgorithmParameterException;
27
import java.security.InvalidParameterException;
28
import java.security.KeyPairGenerator;
29
import java.security.NoSuchAlgorithmException;
30
import java.security.NoSuchProviderException;
31
import java.security.spec.DSAGenParameterSpec;
32
import java.security.spec.DSAParameterSpec;
33
import java.security.spec.InvalidParameterSpecException;
34
35
/*
36
* @test
37
* @bug 8075286
38
* @summary Verify that DSAGenParameterSpec can and can only be used to generate
39
* DSA within some certain range of key sizes as described in the class
40
* specification (L, N) as (1024, 160), (2048, 224) and (2048, 256)
41
* should be OK for DSAGenParameterSpec.
42
* @run main TestDSAGenParameterSpec 512 160
43
* @run main TestDSAGenParameterSpec 1024 160 true
44
* @run main TestDSAGenParameterSpec 1024 224
45
* @run main TestDSAGenParameterSpec 2048 160
46
* @run main/timeout=300 TestDSAGenParameterSpec 2048 224 true
47
* @run main/timeout=300 TestDSAGenParameterSpec 2048 256 true
48
*/
49
public class TestDSAGenParameterSpec {
50
51
private static final String ALGORITHM_NAME = "DSA";
52
private static final String PROVIDER_NAME = "SUN";
53
54
private static void testDSAGenParameterSpec(DataTuple dataTuple)
55
throws NoSuchAlgorithmException, NoSuchProviderException,
56
InvalidParameterSpecException, InvalidAlgorithmParameterException {
57
System.out.printf("Test case: primePLen=%d, " + "subprimeQLen=%d%n",
58
dataTuple.primePLen, dataTuple.subprimeQLen);
59
60
AlgorithmParameterGenerator apg
61
= AlgorithmParameterGenerator.getInstance(ALGORITHM_NAME,
62
PROVIDER_NAME);
63
64
DSAGenParameterSpec genParamSpec = createGenParameterSpec(dataTuple);
65
// genParamSpec will be null if IllegalAE is thrown when expected.
66
if (genParamSpec == null) {
67
return;
68
}
69
70
try {
71
apg.init(genParamSpec, null);
72
AlgorithmParameters param = apg.generateParameters();
73
74
checkParam(param, genParamSpec);
75
System.out.println("Test case passed");
76
} catch (InvalidParameterException ipe) {
77
throw new RuntimeException("Test case failed.", ipe);
78
}
79
}
80
81
private static void checkParam(AlgorithmParameters param,
82
DSAGenParameterSpec genParam) throws InvalidParameterSpecException,
83
NoSuchAlgorithmException, NoSuchProviderException,
84
InvalidAlgorithmParameterException {
85
String algorithm = param.getAlgorithm();
86
if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {
87
throw new RuntimeException(
88
"Unexpected type of parameters: " + algorithm);
89
}
90
91
DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
92
int valueL = spec.getP().bitLength();
93
int strengthP = genParam.getPrimePLength();
94
if (strengthP != valueL) {
95
System.out.printf("P: Expected %d but actual %d%n", strengthP,
96
valueL);
97
throw new RuntimeException("Wrong P strength");
98
}
99
100
int valueN = spec.getQ().bitLength();
101
int strengthQ = genParam.getSubprimeQLength();
102
if (strengthQ != valueN) {
103
System.out.printf("Q: Expected %d but actual %d%n", strengthQ,
104
valueN);
105
throw new RuntimeException("Wrong Q strength");
106
}
107
108
if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {
109
System.out.println("Defaut seed length should be the same as Q.");
110
throw new RuntimeException("Wrong seed length");
111
}
112
113
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME,
114
PROVIDER_NAME);
115
keyGen.initialize(spec);
116
}
117
118
private static DSAGenParameterSpec createGenParameterSpec(
119
DataTuple dataTuple) {
120
DSAGenParameterSpec genParamSpec = null;
121
try {
122
genParamSpec = new DSAGenParameterSpec(dataTuple.primePLen,
123
dataTuple.subprimeQLen);
124
if (!dataTuple.isDSASpecSupported) {
125
throw new RuntimeException(
126
"Test case failed: the key length must not supported");
127
}
128
} catch (IllegalArgumentException e) {
129
if (!dataTuple.isDSASpecSupported) {
130
System.out.println("Test case passed: expected "
131
+ "IllegalArgumentException is caught");
132
} else {
133
throw new RuntimeException("Test case failed: unexpected "
134
+ "IllegalArgumentException is thrown", e);
135
}
136
}
137
138
return genParamSpec;
139
}
140
141
public static void main(String[] args) throws Exception {
142
if (args == null || args.length < 2) {
143
throw new RuntimeException("Invalid number of arguments to generate"
144
+ " DSA parameter.");
145
}
146
DataTuple dataTuple = null;
147
switch (args.length) {
148
case 3:
149
dataTuple = new DataTuple(Integer.valueOf(args[0]),
150
Integer.valueOf(args[1]), Boolean.valueOf(args[2]));
151
break;
152
case 2:
153
dataTuple = new DataTuple(Integer.valueOf(args[0]),
154
Integer.valueOf(args[1]), false);
155
break;
156
default:
157
throw new RuntimeException("Unsupported arguments found.");
158
}
159
testDSAGenParameterSpec(dataTuple);
160
161
}
162
163
private static class DataTuple {
164
165
private int primePLen;
166
private int subprimeQLen;
167
private boolean isDSASpecSupported;
168
169
private DataTuple(int primePLen, int subprimeQLen,
170
boolean isDSASpecSupported) {
171
this.primePLen = primePLen;
172
this.subprimeQLen = subprimeQLen;
173
this.isDSASpecSupported = isDSASpecSupported;
174
}
175
}
176
}
177
178