author | jjiang |
Wed, 15 Aug 2018 18:41:18 +0800 | |
changeset 51460 | 97e361fe3433 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
40975
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
24 |
/* |
2 | 25 |
* @test |
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
26 |
* @bug 6316539 8136355 |
2 | 27 |
* @summary Known-answer-test for TlsMasterSecret generator |
28 |
* @author Andreas Sterbenz |
|
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
29 |
* @library /test/lib .. |
30820 | 30 |
* @modules java.base/sun.security.internal.interfaces |
31 |
* java.base/sun.security.internal.spec |
|
43248
5e15de85a1a0
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents:
42693
diff
changeset
|
32 |
* jdk.crypto.cryptoki |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
33 |
* @run main/othervm TestMasterSecret |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
34 |
* @run main/othervm TestMasterSecret sm TestMasterSecret.policy |
2 | 35 |
*/ |
36 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
37 |
import java.io.BufferedReader; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
38 |
import java.nio.file.Files; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
39 |
import java.nio.file.Paths; |
2 | 40 |
import java.security.Provider; |
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
41 |
import java.security.InvalidAlgorithmParameterException; |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
42 |
import java.util.Arrays; |
2 | 43 |
import javax.crypto.KeyGenerator; |
44 |
import javax.crypto.SecretKey; |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
45 |
import javax.crypto.spec.SecretKeySpec; |
2 | 46 |
import sun.security.internal.interfaces.TlsMasterSecret; |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
47 |
import sun.security.internal.spec.TlsMasterSecretParameterSpec; |
2 | 48 |
|
49 |
public class TestMasterSecret extends PKCS11Test { |
|
50 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
51 |
private static final int PREFIX_LENGTH = "m-premaster: ".length(); |
2 | 52 |
|
53 |
public static void main(String[] args) throws Exception { |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
54 |
main(new TestMasterSecret(), args); |
2 | 55 |
} |
56 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
57 |
@Override |
2 | 58 |
public void main(Provider provider) throws Exception { |
59 |
if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) { |
|
60 |
System.out.println("Not supported by provider, skipping"); |
|
61 |
return; |
|
62 |
} |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
63 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
64 |
try (BufferedReader reader = Files.newBufferedReader( |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
65 |
Paths.get(BASE, "masterdata.txt"))) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
66 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
67 |
int n = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
68 |
int lineNumber = 0; |
2 | 69 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
70 |
String algorithm = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
71 |
byte[] premaster = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
72 |
byte[] clientRandom = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
73 |
byte[] serverRandom = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
74 |
int protoMajor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
75 |
int protoMinor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
76 |
int preMajor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
77 |
int preMinor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
78 |
byte[] master = null; |
2 | 79 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
80 |
while (true) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
81 |
String line = reader.readLine(); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
82 |
lineNumber++; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
83 |
if (line == null) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
84 |
break; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
85 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
86 |
if (line.startsWith("m-") == false) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
87 |
continue; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
88 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
89 |
String data = line.substring(PREFIX_LENGTH); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
90 |
if (line.startsWith("m-algorithm:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
91 |
algorithm = data; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
92 |
} else if (line.startsWith("m-premaster:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
93 |
premaster = parse(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
94 |
} else if (line.startsWith("m-crandom:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
95 |
clientRandom = parse(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
96 |
} else if (line.startsWith("m-srandom:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
97 |
serverRandom = parse(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
98 |
} else if (line.startsWith("m-protomajor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
99 |
protoMajor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
100 |
} else if (line.startsWith("m-protominor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
101 |
protoMinor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
102 |
} else if (line.startsWith("m-premajor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
103 |
preMajor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
104 |
} else if (line.startsWith("m-preminor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
105 |
preMinor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
106 |
} else if (line.startsWith("m-master:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
107 |
master = parse(data); |
2 | 108 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
109 |
System.out.print("."); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
110 |
n++; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
111 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
112 |
KeyGenerator kg = |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
113 |
KeyGenerator.getInstance("SunTlsMasterSecret", provider); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
114 |
SecretKey premasterKey = |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
115 |
new SecretKeySpec(premaster, algorithm); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
116 |
TlsMasterSecretParameterSpec spec = |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
117 |
new TlsMasterSecretParameterSpec(premasterKey, |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
118 |
protoMajor, protoMinor, clientRandom, serverRandom, |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
119 |
null, -1, -1); |
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
120 |
|
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
121 |
try { |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
122 |
kg.init(spec); |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
123 |
TlsMasterSecret key = (TlsMasterSecret)kg.generateKey(); |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
124 |
byte[] enc = key.getEncoded(); |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
125 |
if (Arrays.equals(master, enc) == false) { |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
126 |
throw new Exception("mismatch line: " + lineNumber); |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
127 |
} |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
128 |
if ((preMajor != key.getMajorVersion()) || |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
129 |
(preMinor != key.getMinorVersion())) { |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
130 |
throw new Exception("version mismatch line: " + lineNumber); |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
131 |
} |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
132 |
} catch (InvalidAlgorithmParameterException iape) { |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
133 |
// SSLv3 support is removed in S12 |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
134 |
if (preMajor == 3 && preMinor == 0) { |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
135 |
System.out.println("Skip testing SSLv3"); |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
136 |
continue; |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
40975
diff
changeset
|
137 |
} |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
138 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
139 |
} else { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
140 |
throw new Exception("Unknown line: " + line); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
141 |
} |
2 | 142 |
} |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
143 |
if (n == 0) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
144 |
throw new Exception("no tests"); |
2 | 145 |
} |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
146 |
System.out.println(); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
147 |
System.out.println("OK: " + n + " tests"); |
2 | 148 |
} |
149 |
} |
|
150 |
||
151 |
} |