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