1 /*
2  * Copyright (c) 2013, 2015, 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 7171982
27  * @summary Test that SunJCE.getInstance() is retrieving a provider when
28  * SunJCE has been removed from the provider list.
29  * @run main/othervm SunJCEGetInstance
30  */
31 
32 import java.security.Security;
33 import java.security.Provider;
34 import javax.crypto.Cipher;
35 import javax.crypto.spec.SecretKeySpec;
36 
37 
38 public class SunJCEGetInstance {
main(String[] args)39     public static void main(String[] args) throws Exception {
40         Cipher jce;
41 
42         try{
43             // Remove SunJCE from Provider list
44             Provider prov = Security.getProvider("SunJCE");
45             Security.removeProvider("SunJCE");
46             // Create our own instance of SunJCE provider.  Purposefully not
47             // using SunJCE.getInstance() so we can have our own instance
48             // for the test.
49             jce = Cipher.getInstance("AES/CBC/PKCS5Padding", prov);
50 
51             jce.init(Cipher.ENCRYPT_MODE,
52                 new SecretKeySpec("1234567890abcedf".getBytes(), "AES"));
53             jce.doFinal("PlainText".getBytes());
54         } catch (Exception e) {
55             System.err.println("Setup failure:  ");
56             throw e;
57         }
58 
59         // Get parameters which will call SunJCE.getInstance().  Failure
60         // would occur on this line.
61         try {
62             jce.getParameters().getEncoded();
63 
64         } catch (Exception e) {
65             System.err.println("Test Failure");
66             throw e;
67         }
68         System.out.println("Passed");
69     }
70 }
71