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 KeyFactory works 29 * @author Andreas Sterbenz 30 */ 31 32 import java.util.*; 33 34 import java.security.*; 35 import java.security.interfaces.*; 36 import java.security.spec.*; 37 38 public class Failover { 39 main(String[] args)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 main0(String[] args)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 KeyFactory kf; 55 kf = KeyFactory.getInstance("FOO"); 56 kf.generatePublic(null); 57 kf.generatePublic(null); 58 kf.generatePrivate(null); 59 60 kf = KeyFactory.getInstance("FOO"); 61 kf.generatePrivate(null); 62 kf.getKeySpec(null, null); 63 kf.translateKey(null); 64 65 kf = KeyFactory.getInstance("FOO"); 66 kf.getKeySpec(null, null); 67 kf.translateKey(null); 68 69 kf = KeyFactory.getInstance("FOO"); 70 kf.translateKey(null); 71 72 // somewhat more real tests using DSA 73 System.out.println("DSA tests..."); 74 75 KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); 76 kpg.initialize(512); 77 KeyPair kp = kpg.generateKeyPair(); 78 79 kf = KeyFactory.getInstance("DSA"); 80 System.out.println(kf.translateKey(kp.getPrivate())); 81 82 kf = KeyFactory.getInstance("DSA"); 83 KeySpec spec = kf.getKeySpec(kp.getPublic(), DSAPublicKeySpec.class); 84 85 kf = KeyFactory.getInstance("DSA"); 86 System.out.println(kf.generatePublic(spec)); 87 88 kf = KeyFactory.getInstance("DSA"); 89 try { 90 kf.generatePrivate(spec); 91 throw new Exception("no exception"); 92 } catch (InvalidKeySpecException e) { 93 System.out.println(e); 94 } 95 } 96 97 private static class ProviderPass extends Provider { ProviderPass()98 ProviderPass() { 99 super("Pass", "1.0", "Pass"); 100 put("KeyFactory.FOO" , "Failover$KeyFactoryPass"); 101 } 102 } 103 104 private static class ProviderFail extends Provider { ProviderFail()105 ProviderFail() { 106 super("Fail", "1.0", "Fail"); 107 put("KeyFactory.FOO" , "Failover$KeyFactoryFail"); 108 put("KeyFactory.DSA" , "Failover$KeyFactoryFail"); 109 } 110 } 111 112 public static class KeyFactoryPass extends KeyFactorySpi { 113 engineGeneratePublic(KeySpec keySpec)114 protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { 115 System.out.println("MyKeyFactoryPass.engineGeneratePublic()"); 116 return null; 117 } 118 engineGeneratePrivate(KeySpec keySpec)119 protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { 120 System.out.println("MyKeyFactoryPass.engineGeneratePrivate()"); 121 return null; 122 } 123 124 protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)125 engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException 126 { 127 System.out.println("MyKeyFactoryPass.engineGetKeySpec()"); 128 return null; 129 } 130 engineTranslateKey(Key key)131 protected Key engineTranslateKey(Key key) throws InvalidKeyException { 132 System.out.println("MyKeyFactoryPass.engineTranslateKey()"); 133 return null; 134 } 135 136 } 137 138 public static class KeyFactoryFail extends KeyFactorySpi { 139 engineGeneratePublic(KeySpec keySpec)140 protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { 141 System.out.println("MyKeyFactoryFail.engineGeneratePublic()"); 142 throw new InvalidKeySpecException(); 143 } 144 engineGeneratePrivate(KeySpec keySpec)145 protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { 146 System.out.println("MyKeyFactoryFail.engineGeneratePrivate()"); 147 throw new InvalidKeySpecException(); 148 } 149 engineGetKeySpec(Key key, Class<T> keySpec)150 protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException { 151 System.out.println("MyKeyFactoryFail.engineGetKeySpec()"); 152 throw new InvalidKeySpecException(); 153 } 154 engineTranslateKey(Key key)155 protected Key engineTranslateKey(Key key) throws InvalidKeyException { 156 System.out.println("MyKeyFactoryFail.engineTranslateKey()"); 157 throw new InvalidKeyException(); 158 } 159 160 } 161 162 } 163