2
|
1 |
/*
|
7043
|
2 |
* Copyright (c) 2005, 2010, 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 6345251
|
|
27 |
* @summary Basic known-answer-test for TlsPrf
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
* @library ..
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.util.*;
|
|
34 |
|
|
35 |
import java.security.Security;
|
|
36 |
import java.security.Provider;
|
|
37 |
|
|
38 |
import javax.crypto.KeyGenerator;
|
|
39 |
import javax.crypto.SecretKey;
|
|
40 |
|
|
41 |
import javax.crypto.spec.*;
|
|
42 |
|
|
43 |
import sun.security.internal.spec.*;
|
|
44 |
|
|
45 |
public class TestPRF extends PKCS11Test {
|
|
46 |
|
|
47 |
private static int PREFIX_LENGTH = "prf-output: ".length();
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
main(new TestPRF());
|
|
51 |
}
|
|
52 |
|
|
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 |
InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));
|
|
60 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
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 |
|
7043
|
96 |
KeyGenerator kg =
|
|
97 |
KeyGenerator.getInstance("SunTlsPrf", provider);
|
2
|
98 |
SecretKey inKey;
|
|
99 |
if (secret == null) {
|
|
100 |
inKey = null;
|
|
101 |
} else {
|
|
102 |
inKey = new SecretKeySpec(secret, "Generic");
|
|
103 |
}
|
7043
|
104 |
TlsPrfParameterSpec spec =
|
|
105 |
new TlsPrfParameterSpec(inKey, label, seed, length,
|
|
106 |
null, -1, -1);
|
2
|
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.
|
7043
|
115 |
// (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
|
|
116 |
// mechanism)
|
2
|
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 |
in.close();
|
|
138 |
System.out.println();
|
|
139 |
System.out.println("OK: " + n + " tests");
|
|
140 |
}
|
|
141 |
|
|
142 |
}
|