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 6313661
|
|
27 |
* @summary Basic known-answer-test for TlsPrf
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
import java.util.*;
|
|
33 |
|
|
34 |
import java.security.Security;
|
|
35 |
import java.security.Provider;
|
|
36 |
|
|
37 |
import javax.crypto.KeyGenerator;
|
|
38 |
import javax.crypto.SecretKey;
|
|
39 |
|
|
40 |
import javax.crypto.spec.*;
|
|
41 |
|
|
42 |
import sun.security.internal.spec.*;
|
|
43 |
|
|
44 |
public class TestPRF extends Utils {
|
|
45 |
|
|
46 |
private static int PREFIX_LENGTH = "prf-output: ".length();
|
|
47 |
|
|
48 |
public static void main(String[] args) throws Exception {
|
|
49 |
Provider provider = Security.getProvider("SunJCE");
|
|
50 |
|
|
51 |
InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));
|
|
52 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
53 |
|
|
54 |
int n = 0;
|
|
55 |
int lineNumber = 0;
|
|
56 |
|
|
57 |
byte[] secret = null;
|
|
58 |
String label = null;
|
|
59 |
byte[] seed = null;
|
|
60 |
int length = 0;
|
|
61 |
byte[] output = null;
|
|
62 |
|
|
63 |
while (true) {
|
|
64 |
String line = reader.readLine();
|
|
65 |
lineNumber++;
|
|
66 |
if (line == null) {
|
|
67 |
break;
|
|
68 |
}
|
|
69 |
if (line.startsWith("prf-") == false) {
|
|
70 |
continue;
|
|
71 |
}
|
|
72 |
|
|
73 |
String data = line.substring(PREFIX_LENGTH);
|
|
74 |
if (line.startsWith("prf-secret:")) {
|
|
75 |
secret = parse(data);
|
|
76 |
} else if (line.startsWith("prf-label:")) {
|
|
77 |
label = data;
|
|
78 |
} else if (line.startsWith("prf-seed:")) {
|
|
79 |
seed = parse(data);
|
|
80 |
} else if (line.startsWith("prf-length:")) {
|
|
81 |
length = Integer.parseInt(data);
|
|
82 |
} else if (line.startsWith("prf-output:")) {
|
|
83 |
output = parse(data);
|
|
84 |
|
|
85 |
System.out.print(".");
|
|
86 |
n++;
|
|
87 |
|
7043
|
88 |
KeyGenerator kg =
|
|
89 |
KeyGenerator.getInstance("SunTlsPrf", provider);
|
2
|
90 |
SecretKey inKey;
|
|
91 |
if (secret == null) {
|
|
92 |
inKey = null;
|
|
93 |
} else {
|
|
94 |
inKey = new SecretKeySpec(secret, "Generic");
|
|
95 |
}
|
7043
|
96 |
TlsPrfParameterSpec spec =
|
|
97 |
new TlsPrfParameterSpec(inKey, label, seed, length,
|
|
98 |
null, -1, -1);
|
2
|
99 |
kg.init(spec);
|
|
100 |
SecretKey key = kg.generateKey();
|
|
101 |
byte[] enc = key.getEncoded();
|
|
102 |
if (Arrays.equals(output, enc) == false) {
|
|
103 |
throw new Exception("mismatch line: " + lineNumber);
|
|
104 |
}
|
|
105 |
} else {
|
|
106 |
throw new Exception("Unknown line: " + line);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
if (n == 0) {
|
|
110 |
throw new Exception("no tests");
|
|
111 |
}
|
|
112 |
in.close();
|
|
113 |
System.out.println();
|
|
114 |
System.out.println("OK: " + n + " tests");
|
|
115 |
}
|
|
116 |
|
|
117 |
}
|