Path: blob/master/test/jdk/sun/security/tools/keytool/ProbingFailure.java
41152 views
/*1* Copyright (c) 2018, 2019, 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 821410026* @summary use of keystore probing results in unnecessary exception thrown27* @library /test/lib28* @compile -XDignore.symbol.file ProbingFailure.java29* @run main ProbingFailure30*/3132import jdk.test.lib.SecurityTools;33import jdk.test.lib.process.OutputAnalyzer;3435import java.io.IOException;36import java.io.InputStream;37import java.io.OutputStream;38import java.security.Key;39import java.security.KeyStore;40import java.security.KeyStoreException;41import java.security.KeyStoreSpi;42import java.security.NoSuchAlgorithmException;43import java.security.Provider;44import java.security.UnrecoverableKeyException;45import java.security.cert.Certificate;46import java.security.cert.CertificateException;47import java.util.Date;48import java.util.Enumeration;4950public class ProbingFailure {5152public static void main(String[] args) throws Exception {5354// genkeypair55kt("-genkeypair -keystore mks -alias a -dname CN=A -keyalg DSA -storetype MYKS")56.shouldHaveExitValue(0);5758// list59kt("-list -keystore mks -storetype MYKS")60.shouldHaveExitValue(0);6162kt("-list -keystore mks")63.shouldHaveExitValue(1)64.shouldContain("Unrecognized keystore format");6566// importkeystore67kt("-importkeystore -srckeystore mks -srcstoretype MYKS -destkeystore p12")68.shouldHaveExitValue(0);6970kt("-importkeystore -srckeystore mks -destkeystore p12a")71.shouldHaveExitValue(1)72.shouldContain("Unrecognized keystore format");7374// in-place importkeystore75kt("-importkeystore -srckeystore mks -srcstoretype MYKS -destkeystore mks -deststoretype myks")76.shouldContain("The original keystore \"mks\" is backed up")77.shouldHaveExitValue(0);7879kt("-importkeystore -srckeystore mks -srcstoretype MYKS -destkeystore mks")80.shouldContain("Migrated \"mks\" to PKCS12")81.shouldHaveExitValue(0);8283kt("-importkeystore -srckeystore p12 -destkeystore p12 -deststoretype MYKS")84.shouldContain("Migrated \"p12\" to MYKS")85.shouldHaveExitValue(0);86}8788static OutputAnalyzer kt(String cmd) throws Exception {89return SecurityTools.keytool(90"-storepass changeit -keypass changeit -debug "91+ "-srcstorepass changeit -deststorepass changeit "92+ "-providerclass ProbingFailure$MyProvider "93+ "-providerpath " + System.getProperty("test.classes")94+ " " + cmd);95}9697public static class MyProvider extends Provider {98public MyProvider() {99super("MP", "1.0", "My Provider");100put("KeyStore.MYKS", "ProbingFailure$MyKS");101}102}103104// The MYKS keystore prepends a zero byte before a PKCS12 file105// and does not support probing.106public static class MyKS extends KeyStoreSpi {107108KeyStore ks;109110public MyKS() {111try {112ks = KeyStore.getInstance("PKCS12");113} catch (Exception e) {114throw new RuntimeException(e);115}116}117118@Override119public Key engineGetKey(String alias, char[] password)120throws NoSuchAlgorithmException, UnrecoverableKeyException {121try {122return ks.getKey(alias, password);123} catch (KeyStoreException e) {124throw new RuntimeException(e);125}126}127128@Override129public Certificate[] engineGetCertificateChain(String alias) {130try {131return ks.getCertificateChain(alias);132} catch (KeyStoreException e) {133throw new RuntimeException(e);134}135}136137@Override138public Certificate engineGetCertificate(String alias) {139try {140return ks.getCertificate(alias);141} catch (KeyStoreException e) {142throw new RuntimeException(e);143}144}145146@Override147public Date engineGetCreationDate(String alias) {148try {149return ks.getCreationDate(alias);150} catch (KeyStoreException e) {151throw new RuntimeException(e);152}153}154155@Override156public void engineSetKeyEntry(String alias, Key key, char[] password,157Certificate[] chain) throws KeyStoreException {158ks.setKeyEntry(alias, key, password, chain);159}160161@Override162public void engineSetKeyEntry(String alias, byte[] key,163Certificate[] chain) throws KeyStoreException {164ks.setKeyEntry(alias, key, chain);165}166167@Override168public void engineSetCertificateEntry(String alias, Certificate cert)169throws KeyStoreException {170ks.setCertificateEntry(alias, cert);171}172173@Override174public void engineDeleteEntry(String alias) throws KeyStoreException {175ks.deleteEntry(alias);176}177178@Override179public Enumeration<String> engineAliases() {180try {181return ks.aliases();182} catch (KeyStoreException e) {183throw new RuntimeException(e);184}185}186187@Override188public boolean engineContainsAlias(String alias) {189try {190return ks.containsAlias(alias);191} catch (KeyStoreException e) {192throw new RuntimeException(e);193}194}195196@Override197public int engineSize() {198try {199return ks.size();200} catch (KeyStoreException e) {201throw new RuntimeException(e);202}203}204205@Override206public boolean engineIsKeyEntry(String alias) {207try {208return ks.isKeyEntry(alias);209} catch (KeyStoreException e) {210throw new RuntimeException(e);211}212}213214@Override215public boolean engineIsCertificateEntry(String alias) {216try {217return ks.isCertificateEntry(alias);218} catch (KeyStoreException e) {219throw new RuntimeException(e);220}221}222223@Override224public String engineGetCertificateAlias(Certificate cert) {225try {226return ks.getCertificateAlias(cert);227} catch (KeyStoreException e) {228throw new RuntimeException(e);229}230}231232@Override233public void engineStore(OutputStream stream, char[] password)234throws IOException, NoSuchAlgorithmException, CertificateException {235stream.write(0);236try {237ks.store(stream, password);238} catch (KeyStoreException e) {239throw new RuntimeException(e);240}241}242243@Override244public void engineLoad(InputStream stream, char[] password)245throws IOException, NoSuchAlgorithmException, CertificateException {246if (stream != null) {247stream.read();248}249ks.load(stream, password);250}251252@Override253public boolean engineProbe(InputStream stream) {254return false;255}256}257}258259260