author | erikj |
Fri, 01 Apr 2016 17:08:43 +0200 | |
changeset 36725 | f458544b0d76 |
parent 35379 | 1e8e336ef66b |
child 40975 | 680639c9b307 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, 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 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6316539 |
|
27 |
* @summary Known-answer-test for TlsMasterSecret generator |
|
28 |
* @author Andreas Sterbenz |
|
29 |
* @library .. |
|
30820 | 30 |
* @modules java.base/sun.security.internal.interfaces |
31 |
* java.base/sun.security.internal.spec |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
32 |
* @run main/othervm TestMasterSecret |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
33 |
* @run main/othervm TestMasterSecret sm TestMasterSecret.policy |
2 | 34 |
*/ |
35 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
36 |
import java.io.BufferedReader; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
37 |
import java.nio.file.Files; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
38 |
import java.nio.file.Paths; |
2 | 39 |
import java.security.Provider; |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
40 |
import java.util.Arrays; |
2 | 41 |
import javax.crypto.KeyGenerator; |
42 |
import javax.crypto.SecretKey; |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
43 |
import javax.crypto.spec.SecretKeySpec; |
2 | 44 |
import sun.security.internal.interfaces.TlsMasterSecret; |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
45 |
import sun.security.internal.spec.TlsMasterSecretParameterSpec; |
2 | 46 |
|
47 |
public class TestMasterSecret extends PKCS11Test { |
|
48 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
49 |
private static final int PREFIX_LENGTH = "m-premaster: ".length(); |
2 | 50 |
|
51 |
public static void main(String[] args) throws Exception { |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
52 |
main(new TestMasterSecret(), args); |
2 | 53 |
} |
54 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
55 |
@Override |
2 | 56 |
public void main(Provider provider) throws Exception { |
57 |
if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) { |
|
58 |
System.out.println("Not supported by provider, skipping"); |
|
59 |
return; |
|
60 |
} |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
61 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
62 |
try (BufferedReader reader = Files.newBufferedReader( |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
63 |
Paths.get(BASE, "masterdata.txt"))) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
64 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
65 |
int n = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
66 |
int lineNumber = 0; |
2 | 67 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
68 |
String algorithm = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
69 |
byte[] premaster = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
70 |
byte[] clientRandom = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
71 |
byte[] serverRandom = null; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
72 |
int protoMajor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
73 |
int protoMinor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
74 |
int preMajor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
75 |
int preMinor = 0; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
76 |
byte[] master = null; |
2 | 77 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
78 |
while (true) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
79 |
String line = reader.readLine(); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
80 |
lineNumber++; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
81 |
if (line == null) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
82 |
break; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
83 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
84 |
if (line.startsWith("m-") == false) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
85 |
continue; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
86 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
87 |
String data = line.substring(PREFIX_LENGTH); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
88 |
if (line.startsWith("m-algorithm:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
89 |
algorithm = data; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
90 |
} else if (line.startsWith("m-premaster:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
91 |
premaster = parse(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
92 |
} else if (line.startsWith("m-crandom:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
93 |
clientRandom = parse(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
94 |
} else if (line.startsWith("m-srandom:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
95 |
serverRandom = parse(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
96 |
} else if (line.startsWith("m-protomajor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
97 |
protoMajor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
98 |
} else if (line.startsWith("m-protominor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
99 |
protoMinor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
100 |
} else if (line.startsWith("m-premajor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
101 |
preMajor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
102 |
} else if (line.startsWith("m-preminor:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
103 |
preMinor = Integer.parseInt(data); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
104 |
} else if (line.startsWith("m-master:")) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
105 |
master = parse(data); |
2 | 106 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
107 |
System.out.print("."); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
108 |
n++; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
109 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
110 |
KeyGenerator kg = |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
111 |
KeyGenerator.getInstance("SunTlsMasterSecret", provider); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
112 |
SecretKey premasterKey = |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
113 |
new SecretKeySpec(premaster, algorithm); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
114 |
TlsMasterSecretParameterSpec spec = |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
115 |
new TlsMasterSecretParameterSpec(premasterKey, |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
116 |
protoMajor, protoMinor, clientRandom, serverRandom, |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
117 |
null, -1, -1); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
118 |
kg.init(spec); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
119 |
TlsMasterSecret key = (TlsMasterSecret)kg.generateKey(); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
120 |
byte[] enc = key.getEncoded(); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
121 |
if (Arrays.equals(master, enc) == false) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
122 |
throw new Exception("mismatch line: " + lineNumber); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
123 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
124 |
if ((preMajor != key.getMajorVersion()) || |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
125 |
(preMinor != key.getMinorVersion())) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
126 |
throw new Exception("version mismatch line: " + lineNumber); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
127 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
128 |
} else { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
129 |
throw new Exception("Unknown line: " + line); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
130 |
} |
2 | 131 |
} |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
132 |
if (n == 0) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
133 |
throw new Exception("no tests"); |
2 | 134 |
} |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
135 |
System.out.println(); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30820
diff
changeset
|
136 |
System.out.println("OK: " + n + " tests"); |
2 | 137 |
} |
138 |
} |
|
139 |
||
140 |
} |