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/pkcs11/tls/TestPRF.java
38855 views
1
/*
2
* Copyright (c) 2005, 2016, 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
/**
25
* @test
26
* @bug 6316539 6345251
27
* @summary Basic known-answer-test for TlsPrf
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm TestPRF
31
* @run main/othervm TestPRF sm policy
32
*/
33
34
import java.io.BufferedReader;
35
import java.nio.file.Files;
36
import java.nio.file.Paths;
37
import java.security.Provider;
38
import java.util.Arrays;
39
import javax.crypto.KeyGenerator;
40
import javax.crypto.SecretKey;
41
import javax.crypto.spec.SecretKeySpec;
42
import sun.security.internal.spec.TlsPrfParameterSpec;
43
44
public class TestPRF extends PKCS11Test {
45
46
private static final int PREFIX_LENGTH = "prf-output: ".length();
47
48
public static void main(String[] args) throws Exception {
49
main(new TestPRF(), args);
50
}
51
52
@Override
53
public void main(Provider provider) throws Exception {
54
if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {
55
System.out.println("Provider does not support algorithm, skipping");
56
return;
57
}
58
59
try (BufferedReader reader = Files.newBufferedReader(
60
Paths.get(BASE, "prfdata.txt"))) {
61
62
int n = 0;
63
int lineNumber = 0;
64
65
byte[] secret = null;
66
String label = null;
67
byte[] seed = null;
68
int length = 0;
69
byte[] output = null;
70
71
while (true) {
72
String line = reader.readLine();
73
lineNumber++;
74
if (line == null) {
75
break;
76
}
77
if (line.startsWith("prf-") == false) {
78
continue;
79
}
80
81
String data = line.substring(PREFIX_LENGTH);
82
if (line.startsWith("prf-secret:")) {
83
secret = parse(data);
84
} else if (line.startsWith("prf-label:")) {
85
label = data;
86
} else if (line.startsWith("prf-seed:")) {
87
seed = parse(data);
88
} else if (line.startsWith("prf-length:")) {
89
length = Integer.parseInt(data);
90
} else if (line.startsWith("prf-output:")) {
91
output = parse(data);
92
93
System.out.print(".");
94
n++;
95
96
KeyGenerator kg =
97
KeyGenerator.getInstance("SunTlsPrf", provider);
98
SecretKey inKey;
99
if (secret == null) {
100
inKey = null;
101
} else {
102
inKey = new SecretKeySpec(secret, "Generic");
103
}
104
TlsPrfParameterSpec spec =
105
new TlsPrfParameterSpec(inKey, label, seed, length,
106
null, -1, -1);
107
SecretKey key;
108
try {
109
kg.init(spec);
110
key = kg.generateKey();
111
} catch (Exception e) {
112
if (secret == null) {
113
// This fails on Solaris, but since we never call this
114
// API for this case in JSSE, ignore the failure.
115
// (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
116
// mechanism)
117
System.out.print("X");
118
continue;
119
}
120
System.out.println();
121
throw new Exception("Error on line: " + lineNumber, e);
122
}
123
byte[] enc = key.getEncoded();
124
if (Arrays.equals(output, enc) == false) {
125
System.out.println();
126
System.out.println("expected: " + toString(output));
127
System.out.println("actual: " + toString(enc));
128
throw new Exception("mismatch line: " + lineNumber);
129
}
130
} else {
131
throw new Exception("Unknown line: " + line);
132
}
133
}
134
if (n == 0) {
135
throw new Exception("no tests");
136
}
137
System.out.println();
138
System.out.println("OK: " + n + " tests");
139
}
140
}
141
142
}
143
144