-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTestDSASignatureInterop.java
More file actions
227 lines (196 loc) · 8.47 KB
/
TestDSASignatureInterop.java
File metadata and controls
227 lines (196 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright IBM Corp. 2023, 2026
*
* This code is free software; you can redistribute it and/or modify it
* under the terms provided by IBM in the LICENSE file that accompanied
* this code, including the "Classpath" Exception described therein.
*/
package ibm.jceplus.junit.tests;
import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.Parameter;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@Tag(Tags.OPENJCEPLUS_NAME)
@Tag(Tags.OPENJCEPLUS_FIPS_NAME)
@Tag(Tags.OPENJCEPLUS_MULTITHREAD_NAME)
@Tag(Tags.OPENJCEPLUS_FIPS_MULTITHREAD_NAME)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ParameterizedClass
@MethodSource("ibm.jceplus.junit.tests.TestArguments#getOpenJCEPlusWithSUNInteropProvider")
public class TestDSASignatureInterop extends BaseTestSignatureInterop {
@Parameter(0)
TestProvider provider;
@Parameter(1)
TestProvider interopProvider;
static final byte[] origMsg = "this is the original message to be signed".getBytes();
@BeforeEach
public void setUp() throws Exception {
setAndInsertProvider(provider);
setAndInsertInteropProvider(interopProvider);
}
@Test
public void testSHA1withDSA_1024() throws Exception {
KeyPair keyPair = null;
try {
keyPair = generateKeyPair(1024);
} catch (NoSuchAlgorithmException nsae) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertEquals("no such algorithm: DSA for provider OpenJCEPlusFIPS", nsae.getMessage());
return;
} else {
throw nsae;
}
}
try {
doSignVerify("SHA1withDSA", origMsg, keyPair.getPrivate(), keyPair.getPublic());
} catch (InvalidParameterException | InvalidKeyException ipex) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertTrue(true);
} else {
assertTrue(false);
}
}
}
@Test
public void testSHA224withDSA_1024() throws Exception {
KeyPair keyPair = null;
try {
keyPair = generateKeyPair(1024);
} catch (NoSuchAlgorithmException nsae) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertEquals("no such algorithm: DSA for provider OpenJCEPlusFIPS", nsae.getMessage());
return;
} else {
throw nsae;
}
}
try {
doSignVerify("SHA224withDSA", origMsg, keyPair.getPrivate(), keyPair.getPublic());
} catch (InvalidParameterException | InvalidKeyException ipex) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertTrue(true);
} else {
assertTrue(false);
}
}
}
@Test
public void testSHA256withDSA_1024() throws Exception {
KeyPair keyPair = null;
try {
keyPair = generateKeyPair(1024);
} catch (NoSuchAlgorithmException nsae) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertEquals("no such algorithm: DSA for provider OpenJCEPlusFIPS", nsae.getMessage());
return;
} else {
throw nsae;
}
}
try {
doSignVerify("SHA256withDSA", origMsg, keyPair.getPrivate(), keyPair.getPublic());
} catch (InvalidParameterException | InvalidKeyException ipex) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertTrue(true);
} else {
assertTrue(false);
}
}
}
@Test
public void testNONEwithDSA_2048_hash20() throws Exception {
KeyPair keyPair = null;
try {
keyPair = generateKeyPair(2048);
} catch (NoSuchAlgorithmException nsae) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertEquals("no such algorithm: DSA for provider OpenJCEPlusFIPS", nsae.getMessage());
return;
} else {
throw nsae;
}
}
byte[] sslHash = Arrays.copyOf(origMsg, 20);
doSignVerify("NONEwithDSA", sslHash, keyPair.getPrivate(), keyPair.getPublic());
}
@Test
public void test_encoding() throws Exception {
test_dsa_encoded(getProviderName(), getProviderName());
test_dsa_encoded(getInteropProviderName(), getInteropProviderName());
test_dsa_encoded(getProviderName(), getInteropProviderName());
test_dsa_encoded(getInteropProviderName(), getProviderName());
}
void test_dsa_encoded(String providerNameX, String providerNameY) throws Exception {
System.out.println("providerNameX = " + providerNameX + " providerNameY=" + providerNameY);
KeyFactory dsaKeyFactoryX = KeyFactory.getInstance("DSA", providerNameX);
KeyPair dsaKeyPairX = null;
try {
dsaKeyPairX = generateKeyPair(2048);
} catch (NoSuchAlgorithmException nsae) {
if (getProviderName().equals("OpenJCEPlusFIPS")) {
assertEquals("no such algorithm: DSA for provider OpenJCEPlusFIPS", nsae.getMessage());
return;
} else {
throw nsae;
}
}
X509EncodedKeySpec x509SpecX = new X509EncodedKeySpec(dsaKeyPairX.getPublic().getEncoded());
PKCS8EncodedKeySpec pkcs8SpecX = new PKCS8EncodedKeySpec(
dsaKeyPairX.getPrivate().getEncoded());
DSAPublicKey dsaPubX = (DSAPublicKey) dsaKeyFactoryX.generatePublic(x509SpecX);
DSAPrivateKey dsaPrivX = (DSAPrivateKey) dsaKeyFactoryX.generatePrivate(pkcs8SpecX);
if (!Arrays.equals(dsaPubX.getEncoded(), dsaKeyPairX.getPublic().getEncoded())) {
fail("DSA public key does not match generated public key");
}
if (!Arrays.equals(dsaPrivX.getEncoded(), dsaKeyPairX.getPrivate().getEncoded())) {
fail("DSA private key does not match generated public key");
}
KeyFactory dsaKeyFactoryY = KeyFactory.getInstance("DSA", providerNameY);
X509EncodedKeySpec x509KeySpecY = new X509EncodedKeySpec(
dsaKeyPairX.getPublic().getEncoded());
PublicKey dsaPubY = dsaKeyFactoryY.generatePublic(x509KeySpecY);
PrivateKey dsaPrivY = dsaKeyFactoryY
.generatePrivate(new PKCS8EncodedKeySpec(dsaKeyPairX.getPrivate().getEncoded()));
if (!Arrays.equals(dsaPubX.getEncoded(), dsaPubY.getEncoded())) {
fail("DSA public key from provider X " + providerNameX
+ " does not match public key encoding from provider" + providerNameY);
}
if (!Arrays.equals(dsaPrivX.getEncoded(), dsaPrivY.getEncoded())) {
fail("DSA private key from provider X " + providerNameX
+ " does not match private key encoding from provider" + providerNameY);
}
if (!Arrays.equals(dsaPrivY.getEncoded(), dsaKeyPairX.getPrivate().getEncoded())) {
fail("DSA private key does not match private public key");
}
if (!Arrays.equals(dsaPrivY.getEncoded(), dsaKeyPairX.getPrivate().getEncoded())) {
fail("DSA private key does not match generated prviate key");
}
if (!Arrays.equals(dsaPubY.getEncoded(), dsaKeyPairX.getPublic().getEncoded())) {
fail("DSA private key does not match generated prviate key");
}
}
protected KeyPair generateKeyPair(int keysize) throws Exception {
KeyPairGenerator dsaKeyPairGen = KeyPairGenerator.getInstance("DSA", getProviderName());
dsaKeyPairGen.initialize(keysize);
return dsaKeyPairGen.generateKeyPair();
}
}