author | stefank |
Mon, 04 Apr 2016 09:15:01 +0200 | |
changeset 37241 | b9961c99c356 |
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:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 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 4921804 6324825 |
|
27 |
* @summary Verify that DH works properly |
|
28 |
* @author Andreas Sterbenz |
|
29 |
* @library .. |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
30 |
* @run main/othervm TestDH |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
31 |
* @run main/othervm TestDH sm |
2 | 32 |
*/ |
33 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
34 |
import java.security.KeyPair; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
35 |
import java.security.KeyPairGenerator; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
36 |
import java.security.Provider; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
37 |
import java.util.Arrays; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
38 |
import javax.crypto.KeyAgreement; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
39 |
import javax.crypto.SecretKey; |
2 | 40 |
|
41 |
public class TestDH extends PKCS11Test { |
|
42 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
43 |
@Override |
2 | 44 |
public void main(Provider p) throws Exception { |
45 |
if (p.getService("KeyAgreement", "DH") == null) { |
|
46 |
System.out.println("DH not supported, skipping"); |
|
47 |
return; |
|
48 |
} |
|
49 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p); |
|
50 |
kpg.initialize(512); |
|
51 |
KeyPair kp1 = kpg.generateKeyPair(); |
|
52 |
KeyPair kp2 = kpg.generateKeyPair(); |
|
53 |
||
54 |
KeyAgreement ka1, ka2; |
|
55 |
ka1 = KeyAgreement.getInstance("DH", p); |
|
56 |
ka1.init(kp1.getPrivate()); |
|
57 |
ka1.doPhase(kp2.getPublic(), true); |
|
58 |
System.out.println("Derive 1..."); |
|
59 |
byte[] secret1 = ka1.generateSecret(); |
|
60 |
||
61 |
ka1.init(kp2.getPrivate()); |
|
62 |
ka1.doPhase(kp1.getPublic(), true); |
|
63 |
System.out.println("Derive 2..."); |
|
64 |
byte[] secret2 = ka1.generateSecret(); |
|
65 |
||
66 |
if (Arrays.equals(secret1, secret2) == false) { |
|
67 |
throw new Exception("Secrets (1,2) do not match"); |
|
68 |
} |
|
69 |
||
70 |
ka2 = KeyAgreement.getInstance("DH", "SunJCE"); |
|
71 |
ka2.init(kp1.getPrivate()); |
|
72 |
ka2.doPhase(kp2.getPublic(), true); |
|
73 |
System.out.println("Derive 3..."); |
|
74 |
byte[] secret3 = ka2.generateSecret(); |
|
75 |
||
76 |
if (Arrays.equals(secret1, secret3) == false) { |
|
77 |
throw new Exception("Secrets (1,3) do not match"); |
|
78 |
} |
|
79 |
||
80 |
ka2.init(kp2.getPrivate()); |
|
81 |
ka2.doPhase(kp1.getPublic(), true); |
|
82 |
System.out.println("Derive 4..."); |
|
83 |
byte[] secret4 = ka2.generateSecret(); |
|
84 |
||
85 |
if (Arrays.equals(secret1, secret4) == false) { |
|
86 |
throw new Exception("Secrets (1,4) do not match"); |
|
87 |
} |
|
88 |
||
89 |
testAlgorithm(ka2, kp2, ka1, kp1, "DES"); |
|
90 |
testAlgorithm(ka2, kp2, ka1, kp1, "DESede"); |
|
91 |
// testAlgorithm(ka2, kp2, ka1, kp1, "AES"); |
|
92 |
// testAlgorithm(ka2, kp2, ka1, kp1, "RC4"); |
|
93 |
testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish"); |
|
94 |
testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret"); |
|
95 |
} |
|
96 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
97 |
private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1, |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
98 |
KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
99 |
SecretKey key1; |
2 | 100 |
|
101 |
ka1.init(kp1.getPrivate()); |
|
102 |
ka1.doPhase(kp2.getPublic(), true); |
|
103 |
System.out.println("Derive " + algorithm + " using SunJCE..."); |
|
104 |
key1 = ka1.generateSecret(algorithm); |
|
105 |
||
106 |
ka2.init(kp1.getPrivate()); |
|
107 |
ka2.doPhase(kp2.getPublic(), true); |
|
108 |
System.out.println("Derive " + algorithm + " using PKCS#11..."); |
|
109 |
SecretKey key2 = ka2.generateSecret(algorithm); |
|
110 |
||
111 |
byte[] b1 = key1.getEncoded(); |
|
112 |
byte[] b2 = key2.getEncoded(); |
|
113 |
||
114 |
if (Arrays.equals(b1, b2) == false) { |
|
115 |
System.out.println(b1.length + " bytes: " + toString(b1)); |
|
116 |
System.out.println(b2.length + " bytes: " + toString(b2)); |
|
117 |
throw new Exception(algorithm + " secret mismatch"); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
public static void main(String[] args) throws Exception { |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
5506
diff
changeset
|
122 |
main(new TestDH(), args); |
2 | 123 |
} |
124 |
||
125 |
} |