Path: blob/master/test/jdk/java/security/KeyStore/OneProbeOneNot.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 824567926* @summary KeyStore cannot probe PKCS12 keystore if BouncyCastle is the top security provider27* @run main/othervm OneProbeOneNot28*/2930import java.io.File;31import java.io.InputStream;32import java.io.OutputStream;33import java.nio.file.Files;34import java.nio.file.Path;35import java.security.*;36import java.security.cert.Certificate;37import java.util.Date;38import java.util.Enumeration;3940public class OneProbeOneNot {41public static final void main(String[] args) throws Exception {42Files.write(Path.of("ks"), "".getBytes());43// 1st provider do not support probe44Security.insertProviderAt(new P1(), 1);45// 2nd provider support probe46Security.insertProviderAt(new P2(), 2);47KeyStore ks = KeyStore.getInstance(new File("ks"), (char[])null);48System.out.println(ks.getProvider().getName());49System.out.println(ks.getType());50}5152public static class P1 extends Provider {53public P1() {54super("P1", "P1", "P1");55putService(new Service(this, "KeyStore", "Oops",56K1.class.getName(), null, null));57}58}59public static class P2 extends Provider {60public P2() {61super("P2", "P2", "P2");62putService(new Service(this, "KeyStore", "Oops",63K2.class.getName(), null, null));64}65}6667public static class K1 extends KeyStoreSpi {68public Key engineGetKey(String a, char[] p) { return null; }69public Certificate[] engineGetCertificateChain(String a) { return null; }70public Certificate engineGetCertificate(String a) { return null; }71public Date engineGetCreationDate(String a) { return null; }72public void engineSetKeyEntry(String a, Key k, char[] p, Certificate[] c) { }73public void engineSetKeyEntry(String a, byte[] k, Certificate[] c) { }74public void engineSetCertificateEntry(String a, Certificate c) { }75public void engineDeleteEntry(String a) { }76public Enumeration<String> engineAliases() { return null; }77public boolean engineContainsAlias(String a) { return false; }78public int engineSize() { return 0; }79public boolean engineIsKeyEntry(String a) { return false; }80public boolean engineIsCertificateEntry(String a) { return false; }81public String engineGetCertificateAlias(Certificate c) { return null; }82public void engineStore(OutputStream stream, char[] password) { }83public void engineLoad(InputStream stream, char[] password) { }84}8586public static class K2 extends K1 {87public boolean engineProbe(InputStream s) {88return true;89}90}91}929394