Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/SigRecord.java
41149 views
1
/*
2
* Copyright (c) 2018, 2021, 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.HexFormat;
34
import java.util.List;
35
36
public final class SigRecord {
37
38
static final String TEST_SRC = System.getProperty("test.src", ".");
39
40
public static final class SigVector {
41
// digest algorithm to use
42
final String mdAlg;
43
44
// message to test
45
final String msg;
46
47
// expected signature
48
final String sig;
49
50
public SigVector(String mdAlg, String msg, String sig) {
51
if (mdAlg == null || mdAlg.isEmpty()) {
52
throw new IllegalArgumentException("Digest algo must be specified");
53
}
54
if (msg == null || mdAlg.isEmpty()) {
55
throw new IllegalArgumentException("Message must be specified");
56
}
57
if (sig == null || mdAlg.isEmpty()) {
58
throw new IllegalArgumentException("Signature must be specified");
59
}
60
this.mdAlg = mdAlg;
61
this.msg = msg;
62
this.sig = sig;
63
}
64
65
@Override
66
public String toString() {
67
return (mdAlg + ": msg=" + msg + ": sig=" + sig);
68
}
69
}
70
71
final String id;
72
// RSA private key value associated with the corresponding test vectors
73
final RSAPrivateKeySpec privKeySpec;
74
75
// RSA public key value associated with the corresponding test vectors
76
final RSAPublicKeySpec pubKeySpec;
77
78
// set of test vectors
79
final List<SigVector> testVectors;
80
81
SigRecord(String mod, String pubExp, String privExp, List<SigVector> testVectors) {
82
if (mod == null || mod.isEmpty()) {
83
throw new IllegalArgumentException("Modulus n must be specified");
84
}
85
if (pubExp == null || pubExp.isEmpty()) {
86
throw new IllegalArgumentException("Public Exponent e must be specified");
87
}
88
if (privExp == null || privExp.isEmpty()) {
89
throw new IllegalArgumentException("Private Exponent d must be specified");
90
}
91
if (testVectors == null || (testVectors.size() == 0)) {
92
throw new IllegalArgumentException("One or more test vectors must be specified");
93
}
94
95
BigInteger n = new BigInteger(1, HexFormat.of().parseHex(mod));
96
BigInteger e = new BigInteger(1, HexFormat.of().parseHex(pubExp));
97
BigInteger d = new BigInteger(1, HexFormat.of().parseHex(privExp));
98
this.id = ("n=" + mod + ", e=" + pubExp);
99
this.pubKeySpec = new RSAPublicKeySpec(n, e);
100
this.privKeySpec = new RSAPrivateKeySpec(n, d);
101
this.testVectors = testVectors;
102
}
103
104
/*
105
* Read a data file into an ArrayList.
106
* This function will exit the program if reading the file fails
107
* or if the file is not in the expected format.
108
*/
109
public static List<SigRecord> read(String filename)
110
throws IOException {
111
112
List<SigRecord> data = new ArrayList<>();
113
try (BufferedReader br = new BufferedReader(
114
new InputStreamReader(new FileInputStream(
115
TEST_SRC + File.separator + filename)))) {
116
String line;
117
String mod = null;
118
String pubExp = null;
119
String privExp = null;
120
List<SigVector> testVectors = new ArrayList<>();
121
while ((line = br.readLine()) != null) {
122
if (line.startsWith("n =")) {
123
mod = line.split("=")[1].trim();
124
} else if (line.startsWith("e =")) {
125
pubExp = line.split("=")[1].trim();
126
} else if (line.startsWith("d =")) {
127
privExp = line.split("=")[1].trim();
128
129
// now should start parsing for test vectors
130
String mdAlg = null;
131
String msg = null;
132
String sig = null;
133
boolean sigVectorDone = false;
134
while ((line = br.readLine()) != null) {
135
// we only care for lines starting with
136
// SHAALG, Msg, S
137
if (line.startsWith("SHAAlg =")) {
138
mdAlg = line.split(" = ")[1].trim();
139
} else if (line.startsWith("Msg =")) {
140
msg = line.split(" = ")[1].trim();
141
} else if (line.startsWith("S =")) {
142
sig = line.split(" = ")[1].trim();
143
} else if (line.startsWith("[mod")) {
144
sigVectorDone = true;
145
}
146
147
if ((mdAlg != null) && (msg != null) && (sig != null)) {
148
// finish off current SigVector
149
testVectors.add(new SigVector(mdAlg, msg, sig));
150
mdAlg = msg = sig = null;
151
}
152
if (sigVectorDone) {
153
break;
154
}
155
}
156
// finish off current SigRecord and clear data for next SigRecord
157
data.add(new SigRecord(mod, pubExp, privExp, testVectors));
158
mod = pubExp = privExp = null;
159
testVectors = new ArrayList<>();
160
}
161
}
162
163
if (data.isEmpty()) {
164
throw new RuntimeException("Nothing read from file "
165
+ filename);
166
}
167
}
168
return data;
169
}
170
171
@Override
172
public String toString() {
173
return (id + ", " + testVectors.size() + " test vectors");
174
}
175
}
176
177