Path: blob/master/test/jdk/java/security/KeyStore/EntryMethods.java
41149 views
/*1* Copyright (c) 2003, 2016, 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* @test 1.5, 03/06/2425* @bug 4850376 8130850 813018126* @summary Provide generic storage KeyStore storage facilities27*/2829import java.security.*;30import java.security.cert.*;31import java.util.*;32import java.io.*;3334public class EntryMethods35extends Provider36implements KeyStore.Entry37{3839private static FileInputStream pre15fis;40private static char[] password = {'f', 'o', 'o', 'b', 'a', 'r'};41private static char[] badPwd = {'b', 'a', 'd', 'p', 'w', 'd'};4243public static class FooProtect implements KeyStore.ProtectionParameter { }44public static class FooParameter implements KeyStore.LoadStoreParameter {45public KeyStore.ProtectionParameter getProtectionParameter() {46return null;47}48}4950public static class MyLoadStoreParameter51implements KeyStore.LoadStoreParameter {5253private KeyStore.ProtectionParameter protection;5455MyLoadStoreParameter(KeyStore.ProtectionParameter protection) {56this.protection = protection;57}5859public KeyStore.ProtectionParameter getProtectionParameter() {60return protection;61}62}6364public static class FooEntry implements KeyStore.Entry { }6566public EntryMethods() throws Exception {67super("EntryMethods", "0.0", "EntryMethods");6869pre15fis = new FileInputStream70(System.getProperty("test.src") + "/EntryMethods.pre15.keystore");7172AccessController.doPrivileged(new PrivilegedAction() {73public Object run() {74put("KeyStore.Pre15KeyStore", "EntryMethods$Pre15");75put("KeyStore.Post15KeyStore", "EntryMethods$Post15");76put("KeyStore.UnrecoverableKeyStore",77"EntryMethods$UnrecoverableKS");78return null;79}80});81}8283public static void main(String[] args) throws Exception {8485EntryMethods entry = new EntryMethods();8687// test pre-JDK1.5 KeyStore throws UnsupportedOperationExceptions88// for new methods89KeyStore pre15KS = KeyStore.getInstance("Pre15KeyStore", entry);90testPre15(pre15KS);9192// test post-JDK1.5 KeyStore does right thing with new methods93KeyStore post15KS = KeyStore.getInstance("Post15KeyStore", entry);94testPost15(post15KS);9596// test post-JDK1.5 KeyStore can throw new UnrecoverableEntryException97KeyStore uKS = KeyStore.getInstance("UnrecoverableKeyStore", entry);98testUnrecoverable(uKS);99}100101private static void testPre15(KeyStore ks) throws Exception {102103int tNum = 1;104KeyStore.Entry e = null;105106// TEST load null param107ks.load((KeyStore.LoadStoreParameter)null);108System.out.println("[Pre1.5] test " + tNum++ + " passed");109110111// TEST load random param112try {113ks.load(new FooParameter());114throw new SecurityException("[Pre1.5] test " + tNum + " failed");115} catch (UnsupportedOperationException uoe) {116System.out.println("[Pre1.5] test " + tNum++ + " passed");117} catch (NoSuchAlgorithmException nsae) {118System.out.println("[Pre1.5] test " + tNum++ + " passed");119}120121122// TEST load custom param123ks.load(new MyLoadStoreParameter(124new KeyStore.PasswordProtection(password)));125System.out.println("[Pre1.5] test " + tNum++ + " passed");126127128// TEST store random param129ks.load(pre15fis, password);130131// setup for later user132KeyStore.Entry pkeNew = ks.getEntry("privkey",133new KeyStore.PasswordProtection(password));134KeyStore.Entry tceNew = ks.getEntry("trustedcert", null);135136try {137ks.store(new FooParameter());138throw new SecurityException("[Pre1.5] test " + tNum + " failed");139} catch (UnsupportedOperationException uoe) {140// good141System.out.println("[Pre1.5] test " + tNum++ + " passed");142}143144145// TEST store null param146try {147ks.store(null);148throw new SecurityException("[Pre1.5] test " + tNum + " failed");149} catch (UnsupportedOperationException uoe) {150// good151System.out.println("[Pre1.5] test " + tNum++ + " passed");152}153154155// TEST getEntry with alias/protParam - use invalid alias156e = ks.getEntry("notPresent",157new KeyStore.PasswordProtection(password));158if (e == null) {159System.out.println("[Pre1.5] test " + tNum++ + " passed");160} else {161throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +162"expected null entry returned");163}164165166// TEST getEntry with alias/null protParam - get private key167try {168e = ks.getEntry("privkey", null);169throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +170"expected UnrecoverableEntryException");171} catch (UnrecoverableEntryException uee) {172System.out.println("[Pre1.5] test " + tNum++ + " passed");173}174175176// TEST getEntry with alias/bad password - get private key177try {178e = ks.getEntry("privkey",179new KeyStore.PasswordProtection(badPwd));180throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +181"expected UnrecoverableEntryException");182} catch (UnrecoverableEntryException uee) {183System.out.println("[Pre1.5] test " + tNum++ + " passed");184}185186187// TEST getEntry with alias/unknown protection - get private key188try {189e = ks.getEntry("privkey", new FooProtect());190throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +191"expected UnsupportedOperationException");192} catch (UnsupportedOperationException uoe) {193System.out.println("[Pre1.5] test " + tNum++ + " passed");194}195196197// TEST getEntry with alias/protParam - get private key198e = ks.getEntry("privkey", new KeyStore.PasswordProtection(password));199if (e instanceof KeyStore.PrivateKeyEntry) {200System.out.println("[Pre1.5] test " + tNum++ + " passed");201} else {202throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +203"expected PrivateKeyEntry");204}205206207// TEST getEntry with alias/null protParam - get trusted cert208e = ks.getEntry("trustedcert", null);209if (e instanceof KeyStore.TrustedCertificateEntry) {210System.out.println("[Pre1.5] test " + tNum++ + " passed");211} else {212throw new SecurityException("[Pre1.5] test " + tNum + " failed");213}214215216// TEST getEntry with alias/non-null protParam - get trusted cert217try {218e = ks.getEntry("trustedcert",219new KeyStore.PasswordProtection(password));220throw new SecurityException("[Pre1.5] test " + tNum + " failed");221} catch (UnsupportedOperationException uoe) {222System.out.println("[Pre1.5] test " + tNum++ + " passed");223}224225226// TEST setEntry with alias/entry/protParam - use invalid alias227try {228ks.setEntry("foo", new FooEntry(),229new KeyStore.PasswordProtection(password));230throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +231"expected KeyStoreException");232} catch (KeyStoreException kse) {233// good234System.out.println("[Pre1.5] test " + tNum++ + " passed");235}236237238// TEST setEntry with alias/entry/null protParam - set private key239try {240ks.setEntry("newPrivKey", pkeNew, null);241throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +242"expected KeyStoreException");243} catch (KeyStoreException kse) {244System.out.println("[Pre1.5] test " + tNum++ + " passed");245}246247248// TEST setEntry with alias/entry/random protParam - set private key249try {250ks.setEntry("newPrivKey", pkeNew, new FooProtect());251throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +252"expected KeyStoreException");253} catch (KeyStoreException kse) {254System.out.println("[Pre1.5] test " + tNum++ + " passed");255}256257258// TEST setEntry with alias/entry/protParam - set private key259ks.setEntry("newPrivKey", pkeNew,260new KeyStore.PasswordProtection(password));261e = ks.getEntry("newPrivKey",262new KeyStore.PasswordProtection(password));263if (e instanceof KeyStore.PrivateKeyEntry) {264System.out.println("[Pre1.5] test " + tNum++ + " passed");265} else {266throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +267"expected PrivateKeyEntry");268}269270271// TEST setEntry with alias/entry/non null protParam - set trusted cert272try {273ks.setEntry("newTrustedcert", tceNew,274new KeyStore.PasswordProtection(password));275throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +276"expected KeyStoreException");277} catch (KeyStoreException kse) {278// good279System.out.println("[Pre1.5] test " + tNum++ + " passed");280}281282283// TEST setEntry with alias/entry/null protParam - set trusted cert284ks.setEntry("newTrustedcert", tceNew, null);285e = ks.getEntry("newTrustedcert", null);286if (e instanceof KeyStore.TrustedCertificateEntry) {287System.out.println("[Pre1.5] test " + tNum++ + " passed");288} else {289throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +290"expected TrustedCertificateEntry");291}292293294// TEST entryInstanceOf - invalid alias295if (ks.entryInstanceOf("foo", EntryMethods.class) == false) {296System.out.println("[Pre1.5] test " + tNum++ + " passed");297} else {298throw new SecurityException("[Pre1.5] test " + tNum + " failed");299}300301302// TEST entryInstanceOf - false case303if (ks.entryInstanceOf("privkey", EntryMethods.class) == false) {304System.out.println("[Pre1.5] test " + tNum++ + " passed");305} else {306throw new SecurityException("[Pre1.5] test " + tNum + " failed");307}308309310// TEST entryInstanceOf - true case, trustedcert entry311if (ks.entryInstanceOf("trustedcert",312KeyStore.TrustedCertificateEntry.class)) {313System.out.println("[Pre1.5] test " + tNum++ + " passed");314} else {315throw new SecurityException("[Pre1.5] test " + tNum + " failed");316}317318319// TEST entryInstanceOf - true case, private key entry320if (ks.entryInstanceOf("privkey",321KeyStore.PrivateKeyEntry.class)) {322System.out.println("[Pre1.5] test " + tNum++ + " passed");323} else {324throw new SecurityException("[Pre1.5] test " + tNum + " failed");325}326327}328329private static void testPost15(KeyStore ks) throws Exception {330331KeyStore.Entry e = null;332333ks.load(new EntryMethods.FooParameter());334ks.store(new EntryMethods.FooParameter());335336e = ks.getEntry("foo", new KeyStore.PasswordProtection(password));337if (!(e instanceof EntryMethods.FooEntry)) {338throw new SecurityException339("testPost15 getEntry(String, ProtParm) " +340"expected EntryMethods.FooEntry returned");341}342343ks.setEntry("foo", new EntryMethods.FooEntry(),344new KeyStore.PasswordProtection(password));345346if (!ks.entryInstanceOf("foo", KeyStore.PrivateKeyEntry.class)) {347throw new SecurityException348("testPost15 entryInstanceOf(String, Class) " +349"expected true returned");350}351352System.out.println("[Post1.5] tests all passed");353}354355private static void testUnrecoverable(KeyStore ks) throws Exception {356ks.load(new EntryMethods.FooParameter());357try {358ks.getEntry("foo", new KeyStore.PasswordProtection(password));359throw new SecurityException360("UnrecoverableEntryException not thrown for " +361"getEntry(String, ProtectionParam)");362} catch (UnrecoverableEntryException uee) {363// good364System.out.println("[UnrecoverableEntry] test passed");365}366}367368public static class Pre15 extends KeyStoreSpi {369370private static KeyStore jks = getJKS();371372private static KeyStore getJKS() {373try {374return (KeyStore) KeyStore.getInstance("JKS");375} catch (Exception e) {376e.printStackTrace();377throw new RuntimeException(e);378}379}380public Pre15() {381}382383public Key engineGetKey(String alias, char[] password)384throws NoSuchAlgorithmException, UnrecoverableKeyException {385try {386return jks.getKey(alias, password);387} catch (KeyStoreException ke) {388throw new RuntimeException("Unexpected exception", ke);389}390}391392public java.security.cert.Certificate[] engineGetCertificateChain393(String alias) {394try {395return jks.getCertificateChain(alias);396} catch (KeyStoreException ke) {397throw new RuntimeException("Unexpected exception", ke);398}399}400401public java.security.cert.Certificate engineGetCertificate402(String alias) {403try {404return jks.getCertificate(alias);405} catch (KeyStoreException ke) {406throw new RuntimeException("Unexpected exception", ke);407}408}409410public Date engineGetCreationDate(String alias) {411try {412return jks.getCreationDate(alias);413} catch (KeyStoreException ke) {414throw new RuntimeException("Unexpected exception", ke);415}416}417418public void engineSetKeyEntry(String alias, Key key,419char[] password,420java.security.cert.Certificate[] chain)421throws KeyStoreException {422jks.setKeyEntry(alias, key, password, chain);423}424425public void engineSetKeyEntry(String alias, byte[] key,426java.security.cert.Certificate[] chain)427throws KeyStoreException {428jks.setKeyEntry(alias, key, chain);429}430431public void engineSetCertificateEntry(String alias,432java.security.cert.Certificate cert)433throws KeyStoreException {434jks.setCertificateEntry(alias, cert);435}436437public void engineDeleteEntry(String alias)438throws KeyStoreException {439jks.deleteEntry(alias);440}441442public Enumeration engineAliases() {443try {444return jks.aliases();445} catch (KeyStoreException ke) {446throw new RuntimeException("Unexpected exception", ke);447}448449}450451public boolean engineContainsAlias(String alias) {452try {453return jks.containsAlias(alias);454} catch (KeyStoreException ke) {455throw new RuntimeException("Unexpected exception", ke);456}457}458459public int engineSize() {460try {461return jks.size();462} catch (KeyStoreException ke) {463throw new RuntimeException("Unexpected exception", ke);464}465}466467public boolean engineIsKeyEntry(String alias) {468try {469return jks.isKeyEntry(alias);470} catch (KeyStoreException ke) {471throw new RuntimeException("Unexpected exception", ke);472}473}474475public boolean engineIsCertificateEntry(String alias) {476try {477return jks.isCertificateEntry(alias);478} catch (KeyStoreException ke) {479throw new RuntimeException("Unexpected exception", ke);480}481}482483public String engineGetCertificateAlias484(java.security.cert.Certificate cert) {485try {486return jks.getCertificateAlias(cert);487} catch (KeyStoreException ke) {488throw new RuntimeException("Unexpected exception", ke);489}490}491492public void engineStore(OutputStream stream, char[] password)493throws IOException, NoSuchAlgorithmException, CertificateException {494try {495jks.store(stream, password);496} catch (KeyStoreException ke) {497throw new RuntimeException("Unexpected exception", ke);498}499}500501public void engineLoad(InputStream stream, char[] password)502throws IOException, NoSuchAlgorithmException, CertificateException {503jks.load(stream, password);504}505}506507public static class Post15 extends Pre15 {508509public void engineStore(KeyStore.LoadStoreParameter parameter)510throws IOException, NoSuchAlgorithmException, CertificateException {511if (!(parameter instanceof EntryMethods.FooParameter)) {512throw new IOException("Post15 engineStore method expected " +513"FooParameter");514}515}516517public void engineLoad(KeyStore.LoadStoreParameter parameter)518throws IOException, NoSuchAlgorithmException, CertificateException {519if (!(parameter instanceof EntryMethods.FooParameter)) {520throw new IOException("Post15 engineLoadFrom method expected " +521"FooParameter");522}523}524525public KeyStore.Entry engineGetEntry(String alias,526KeyStore.ProtectionParameter protectionParam)527throws UnrecoverableEntryException {528if (!alias.equals("foo")) {529throw new SecurityException530("Post15 engineGetEntry(String, ProtectionParam) " +531"expected [foo] alias");532}533KeyStore.PasswordProtection pwdParam =534(KeyStore.PasswordProtection)protectionParam;535if (pwdParam.getPassword().length != 6) {536throw new SecurityException537("Post15 engineGetEntry(String, ProtectionParam) " +538"expected [foobar] password");539}540541return new EntryMethods.FooEntry();542}543544public void engineSetEntry(String alias, KeyStore.Entry entry,545KeyStore.ProtectionParameter protectionParam) {546if (!alias.equals("foo") ||547!(entry instanceof EntryMethods.FooEntry)) {548throw new SecurityException549("Post15 engineSetEntry(String, entry, ProtParm) " +550"expected [foo] alias and EntryMethods.FooEntry");551}552553KeyStore.PasswordProtection pwdParam =554(KeyStore.PasswordProtection)protectionParam;555if (pwdParam.getPassword().length != 6) {556throw new SecurityException557("Post15 engineSetEntry(String, entry, ProtParm) " +558"expected [foobar] password");559}560}561562public boolean engineEntryInstanceOf(String alias,563Class<? extends KeyStore.Entry> entryClass)564{565if (!alias.equals("foo") ||566entryClass != KeyStore.PrivateKeyEntry.class) {567throw new SecurityException568("Post15 engineEntryInstanceOf(String, Class) " +569"expected [foo] alias " +570"and [KeyStore.PrivateKeyEntry] class");571}572return true;573}574}575576public static class UnrecoverableKS extends Post15 {577public KeyStore.Entry engineGetEntry(String alias,578KeyStore.ProtectionParameter protectionParam)579throws UnrecoverableEntryException {580throw new UnrecoverableEntryException();581}582}583}584585586