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/rsa/SigRecord.java
38839 views
1
/*
2
* Copyright (c) 2018, 2020, 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.io.BufferedReader;
25
import java.io.File;
26
import java.io.FileInputStream;
27
import java.io.IOException;
28
import java.io.InputStreamReader;
29
import java.math.BigInteger;
30
import java.security.*;
31
import java.security.spec.*;
32
import java.util.ArrayList;
33
import java.util.List;
34
35
public final class SigRecord {
36
37
static final String TEST_SRC = System.getProperty("test.src", ".");
38
39
// utility method for converting byte array to hex string
40
static String toHexString(byte[] array) {
41
StringBuilder sb = new StringBuilder(array.length * 2);
42
for (byte b : array) {
43
// The single digits 0123456789abcdef get a leading 0
44
if ((b >= 0x00) && (b < 0x10)) {
45
sb.append('0');
46
}
47
sb.append(Integer.toHexString(b & 0xff));
48
}
49
return sb.toString();
50
}
51
52
// utility method for converting hex string to byte array
53
static byte[] toByteArray(String s) {
54
byte[] bytes = new byte[s.length() / 2];
55
for (int i = 0; i < bytes.length; i++) {
56
int index = i * 2;
57
int v = Integer.parseInt(s.substring(index, index + 2), 16);
58
bytes[i] = (byte) v;
59
}
60
return bytes;
61
}
62
63
public static final class SigVector {
64
// digest algorithm to use
65
final String mdAlg;
66
67
// message to test
68
final String msg;
69
70
// expected signature
71
final String sig;
72
73
public SigVector(String mdAlg, String msg, String sig) {
74
if (mdAlg == null || mdAlg.isEmpty()) {
75
throw new IllegalArgumentException("Digest algo must be specified");
76
}
77
if (msg == null || mdAlg.isEmpty()) {
78
throw new IllegalArgumentException("Message must be specified");
79
}
80
if (sig == null || mdAlg.isEmpty()) {
81
throw new IllegalArgumentException("Signature must be specified");
82
}
83
this.mdAlg = mdAlg;
84
this.msg = msg;
85
this.sig = sig;
86
}
87
88
@Override
89
public String toString() {
90
return (mdAlg + ": msg=" + msg + ": sig=" + sig);
91
}
92
}
93
94
final String id;
95
// RSA private key value associated with the corresponding test vectors
96
final RSAPrivateKeySpec privKeySpec;
97
98
// RSA public key value associated with the corresponding test vectors
99
final RSAPublicKeySpec pubKeySpec;
100
101
// set of test vectors
102
final List<SigVector> testVectors;
103
104
SigRecord(String mod, String pubExp, String privExp, List<SigVector> testVectors) {
105
if (mod == null || mod.isEmpty()) {
106
throw new IllegalArgumentException("Modulus n must be specified");
107
}
108
if (pubExp == null || pubExp.isEmpty()) {
109
throw new IllegalArgumentException("Public Exponent e must be specified");
110
}
111
if (privExp == null || privExp.isEmpty()) {
112
throw new IllegalArgumentException("Private Exponent d must be specified");
113
}
114
if (testVectors == null || (testVectors.size() == 0)) {
115
throw new IllegalArgumentException("One or more test vectors must be specified");
116
}
117
118
BigInteger n = new BigInteger(1, toByteArray(mod));
119
BigInteger e = new BigInteger(1, toByteArray(pubExp));
120
BigInteger d = new BigInteger(1, toByteArray(privExp));
121
this.id = ("n=" + mod + ", e=" + pubExp);
122
this.pubKeySpec = new RSAPublicKeySpec(n, e);
123
this.privKeySpec = new RSAPrivateKeySpec(n, d);
124
this.testVectors = testVectors;
125
}
126
127
/*
128
* Read a data file into an ArrayList.
129
* This function will exit the program if reading the file fails
130
* or if the file is not in the expected format.
131
*/
132
public static List<SigRecord> read(String filename)
133
throws IOException {
134
135
List<SigRecord> data = new ArrayList<>();
136
try (BufferedReader br = new BufferedReader(
137
new InputStreamReader(new FileInputStream(
138
TEST_SRC + File.separator + filename)))) {
139
String line;
140
String mod = null;
141
String pubExp = null;
142
String privExp = null;
143
List<SigVector> testVectors = new ArrayList<>();
144
while ((line = br.readLine()) != null) {
145
if (line.startsWith("n =")) {
146
mod = line.split("=")[1].trim();
147
} else if (line.startsWith("e =")) {
148
pubExp = line.split("=")[1].trim();
149
} else if (line.startsWith("d =")) {
150
privExp = line.split("=")[1].trim();
151
152
// now should start parsing for test vectors
153
String mdAlg = null;
154
String msg = null;
155
String sig = null;
156
boolean sigVectorDone = false;
157
while ((line = br.readLine()) != null) {
158
// we only care for lines starting with
159
// SHAALG, Msg, S
160
if (line.startsWith("SHAAlg =")) {
161
mdAlg = line.split(" = ")[1].trim();
162
} else if (line.startsWith("Msg =")) {
163
msg = line.split(" = ")[1].trim();
164
} else if (line.startsWith("S =")) {
165
sig = line.split(" = ")[1].trim();
166
} else if (line.startsWith("[mod")) {
167
sigVectorDone = true;
168
}
169
170
if ((mdAlg != null) && (msg != null) && (sig != null)) {
171
// finish off current SigVector
172
testVectors.add(new SigVector(mdAlg, msg, sig));
173
mdAlg = msg = sig = null;
174
}
175
if (sigVectorDone) {
176
break;
177
}
178
}
179
// finish off current SigRecord and clear data for next SigRecord
180
data.add(new SigRecord(mod, pubExp, privExp, testVectors));
181
mod = pubExp = privExp = null;
182
testVectors = new ArrayList<>();
183
}
184
}
185
186
if (data.isEmpty()) {
187
throw new RuntimeException("Nothing read from file "
188
+ filename);
189
}
190
}
191
return data;
192
}
193
194
@Override
195
public String toString() {
196
return (id + ", " + testVectors.size() + " test vectors");
197
}
198
}
199
200