2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4894125
|
|
27 |
* @summary test that failover for KeyPairGenerator works
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
import java.security.*;
|
|
34 |
import java.security.interfaces.*;
|
|
35 |
import java.security.spec.*;
|
|
36 |
|
|
37 |
public class Failover {
|
|
38 |
|
|
39 |
public static void main(String[] args) throws Exception {
|
|
40 |
Security.insertProviderAt(new ProviderFail(), 1);
|
|
41 |
Security.addProvider(new ProviderPass());
|
|
42 |
System.out.println(Arrays.asList(Security.getProviders()));
|
|
43 |
|
|
44 |
KeyPairGenerator kpg;
|
|
45 |
kpg = KeyPairGenerator.getInstance("FOO");
|
|
46 |
kpg.generateKeyPair();
|
|
47 |
kpg.generateKeyPair();
|
|
48 |
|
|
49 |
kpg = KeyPairGenerator.getInstance("FOO");
|
|
50 |
kpg.initialize(1024);
|
|
51 |
kpg.initialize(1024);
|
|
52 |
kpg.initialize(null, null);
|
|
53 |
kpg.generateKeyPair();
|
|
54 |
|
|
55 |
kpg = KeyPairGenerator.getInstance("FOO");
|
|
56 |
kpg.initialize(null, null);
|
|
57 |
kpg.generateKeyPair();
|
|
58 |
|
|
59 |
kpg = KeyPairGenerator.getInstance("FOO");
|
|
60 |
kpg.initialize(512);
|
|
61 |
kpg.generateKeyPair();
|
|
62 |
kpg.generateKeyPair();
|
|
63 |
|
|
64 |
// the SUN DSA KeyPairGenerator implementation extends
|
|
65 |
// KeyPairGenerator (in order to implement
|
|
66 |
// java.security.interfaces.DSAKeyPairGenerator)
|
|
67 |
// failover cannot work
|
|
68 |
kpg = KeyPairGenerator.getInstance("DSA");
|
|
69 |
try {
|
|
70 |
kpg.initialize(1024);
|
|
71 |
throw new Exception("no exception");
|
|
72 |
} catch (InvalidParameterException e) {
|
|
73 |
System.out.println(e);
|
|
74 |
}
|
|
75 |
|
|
76 |
KeyPair kp;
|
|
77 |
kpg = KeyPairGenerator.getInstance("RSA");
|
|
78 |
kpg.initialize(512);
|
|
79 |
kp = kpg.generateKeyPair();
|
|
80 |
System.out.println(kp.getPublic());
|
|
81 |
|
|
82 |
kpg = KeyPairGenerator.getInstance("RSA");
|
|
83 |
kpg.initialize(768);
|
|
84 |
kp = kpg.generateKeyPair();
|
|
85 |
System.out.println(kp.getPublic());
|
|
86 |
|
|
87 |
kpg = KeyPairGenerator.getInstance("RSA");
|
|
88 |
kp = kpg.generateKeyPair();
|
|
89 |
System.out.println(kp.getPublic());
|
|
90 |
|
|
91 |
kpg = KeyPairGenerator.getInstance("RSA");
|
|
92 |
try {
|
|
93 |
kpg.initialize(128);
|
|
94 |
throw new Exception("no exception");
|
|
95 |
} catch (InvalidParameterException e) {
|
|
96 |
System.out.println(e);
|
|
97 |
}
|
|
98 |
|
|
99 |
}
|
|
100 |
|
|
101 |
private static class ProviderPass extends Provider {
|
|
102 |
ProviderPass() {
|
|
103 |
super("Pass", 1.0d, "Pass");
|
|
104 |
put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorPass");
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
private static class ProviderFail extends Provider {
|
|
109 |
ProviderFail() {
|
|
110 |
super("Fail", 1.0d, "Fail");
|
|
111 |
put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorFail");
|
|
112 |
put("KeyPairGenerator.DSA" , "Failover$KeyPairGeneratorFail");
|
|
113 |
put("KeyPairGenerator.RSA" , "Failover$KeyPairGeneratorFail");
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
public static class KeyPairGeneratorPass extends KeyPairGeneratorSpi {
|
|
118 |
|
|
119 |
public void initialize(int keysize, SecureRandom random) {
|
|
120 |
System.out.println("KeyPairGeneratorPass.initialize(" + keysize + ", " + random + ")");
|
|
121 |
}
|
|
122 |
|
|
123 |
public void initialize(AlgorithmParameterSpec params,
|
|
124 |
SecureRandom random) throws InvalidAlgorithmParameterException {
|
|
125 |
System.out.println("KeyPairGeneratorPass.initialize()");
|
|
126 |
}
|
|
127 |
|
|
128 |
public KeyPair generateKeyPair() {
|
|
129 |
System.out.println("KeyPairGeneratorPass.generateKeyPair()");
|
|
130 |
return null;
|
|
131 |
}
|
|
132 |
|
|
133 |
}
|
|
134 |
|
|
135 |
public static class KeyPairGeneratorFail extends KeyPairGeneratorSpi {
|
|
136 |
|
|
137 |
public void initialize(int keysize, SecureRandom random) {
|
|
138 |
if (keysize != 512) {
|
|
139 |
System.out.println("KeyPairGeneratorFail.initialize()");
|
|
140 |
throw new InvalidParameterException();
|
|
141 |
}
|
|
142 |
System.out.println("KeyPairGeneratorFail.initialize() PASS");
|
|
143 |
}
|
|
144 |
|
|
145 |
public void initialize(AlgorithmParameterSpec params,
|
|
146 |
SecureRandom random) throws InvalidAlgorithmParameterException {
|
|
147 |
System.out.println("KeyPairGeneratorFail.initialize()");
|
|
148 |
throw new InvalidParameterException();
|
|
149 |
}
|
|
150 |
|
|
151 |
public KeyPair generateKeyPair() {
|
|
152 |
System.out.println("KeyPairGeneratorFail.generateKeyPair()");
|
|
153 |
throw new InvalidParameterException();
|
|
154 |
}
|
|
155 |
|
|
156 |
}
|
|
157 |
|
|
158 |
}
|