Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/EntryMethods.java
38811 views
/*1* Copyright (c) 2003, 2004, 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 485037626* @summary Provide generic storage KeyStore storage facilities27*/2829import java.security.*;30import java.security.cert.*;31import java.util.*;32import java.io.*;3334import sun.security.provider.*;3536public class EntryMethods37extends Provider38implements KeyStore.Entry39{4041private static FileInputStream pre15fis;42private static char[] password = {'f', 'o', 'o', 'b', 'a', 'r'};43private static char[] badPwd = {'b', 'a', 'd', 'p', 'w', 'd'};4445public static class FooProtect implements KeyStore.ProtectionParameter { }46public static class FooParameter implements KeyStore.LoadStoreParameter {47public KeyStore.ProtectionParameter getProtectionParameter() {48return null;49}50}5152public static class FooEntry implements KeyStore.Entry { }5354public EntryMethods() throws Exception {55super("EntryMethods", 0.0, "EntryMethods");5657pre15fis = new FileInputStream58(System.getProperty("test.src") + "/EntryMethods.pre15.keystore");5960AccessController.doPrivileged(new PrivilegedAction() {61public Object run() {62put("KeyStore.Pre15KeyStore", "EntryMethods$Pre15");63put("KeyStore.Post15KeyStore", "EntryMethods$Post15");64put("KeyStore.UnrecoverableKeyStore",65"EntryMethods$UnrecoverableKS");66return null;67}68});69}7071public static void main(String[] args) throws Exception {7273EntryMethods entry = new EntryMethods();7475// test pre-JDK1.5 KeyStore throws UnsupportedOperationExceptions76// for new methods77KeyStore pre15KS = KeyStore.getInstance("Pre15KeyStore", entry);78testPre15(pre15KS);7980// test post-JDK1.5 KeyStore does right thing with new methods81KeyStore post15KS = KeyStore.getInstance("Post15KeyStore", entry);82testPost15(post15KS);8384// test post-JDK1.5 KeyStore can throw new UnrecoverableEntryException85KeyStore uKS = KeyStore.getInstance("UnrecoverableKeyStore", entry);86testUnrecoverable(uKS);87}8889private static void testPre15(KeyStore ks) throws Exception {9091int tNum = 1;92KeyStore.Entry e = null;9394// TEST load null param95ks.load((KeyStore.LoadStoreParameter)null);96System.out.println("[Pre1.5] test " + tNum++ + " passed");979899// TEST load random param100try {101ks.load(new FooParameter());102throw new SecurityException("[Pre1.5] test " + tNum + " failed");103} catch (UnsupportedOperationException uoe) {104System.out.println("[Pre1.5] test " + tNum++ + " passed");105}106107108// TEST store random param109ks.load(pre15fis, password);110111// setup for later user112KeyStore.Entry pkeNew = ks.getEntry("privkey",113new KeyStore.PasswordProtection(password));114KeyStore.Entry tceNew = ks.getEntry("trustedcert", null);115116try {117ks.store(new FooParameter());118throw new SecurityException("[Pre1.5] test " + tNum + " failed");119} catch (UnsupportedOperationException uoe) {120// good121System.out.println("[Pre1.5] test " + tNum++ + " passed");122}123124125// TEST store null param126try {127ks.store(null);128throw new SecurityException("[Pre1.5] test " + tNum + " failed");129} catch (UnsupportedOperationException uoe) {130// good131System.out.println("[Pre1.5] test " + tNum++ + " passed");132}133134135// TEST getEntry with alias/protParam - use invalid alias136e = ks.getEntry("notPresent",137new KeyStore.PasswordProtection(password));138if (e == null) {139System.out.println("[Pre1.5] test " + tNum++ + " passed");140} else {141throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +142"expected null entry returned");143}144145146// TEST getEntry with alias/null protParam - get private key147try {148e = ks.getEntry("privkey", null);149throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +150"expected UnrecoverableEntryException");151} catch (UnrecoverableEntryException uee) {152System.out.println("[Pre1.5] test " + tNum++ + " passed");153}154155156// TEST getEntry with alias/bad password - get private key157try {158e = ks.getEntry("privkey",159new KeyStore.PasswordProtection(badPwd));160throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +161"expected UnrecoverableEntryException");162} catch (UnrecoverableEntryException uee) {163System.out.println("[Pre1.5] test " + tNum++ + " passed");164}165166167// TEST getEntry with alias/unknown protection - get private key168try {169e = ks.getEntry("privkey", new FooProtect());170throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +171"expected UnsupportedOperationException");172} catch (UnsupportedOperationException uoe) {173System.out.println("[Pre1.5] test " + tNum++ + " passed");174}175176177// TEST getEntry with alias/protParam - get private key178e = ks.getEntry("privkey", new KeyStore.PasswordProtection(password));179if (e instanceof KeyStore.PrivateKeyEntry) {180System.out.println("[Pre1.5] test " + tNum++ + " passed");181} else {182throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +183"expected PrivateKeyEntry");184}185186187// TEST getEntry with alias/null protParam - get trusted cert188e = ks.getEntry("trustedcert", null);189if (e instanceof KeyStore.TrustedCertificateEntry) {190System.out.println("[Pre1.5] test " + tNum++ + " passed");191} else {192throw new SecurityException("[Pre1.5] test " + tNum + " failed");193}194195196// TEST getEntry with alias/non-null protParam - get trusted cert197try {198e = ks.getEntry("trustedcert",199new KeyStore.PasswordProtection(password));200throw new SecurityException("[Pre1.5] test " + tNum + " failed");201} catch (UnsupportedOperationException uoe) {202System.out.println("[Pre1.5] test " + tNum++ + " passed");203}204205206// TEST setEntry with alias/entry/protParam - use invalid alias207try {208ks.setEntry("foo", new FooEntry(),209new KeyStore.PasswordProtection(password));210throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +211"expected KeyStoreException");212} catch (KeyStoreException kse) {213// good214System.out.println("[Pre1.5] test " + tNum++ + " passed");215}216217218// TEST setEntry with alias/entry/null protParam - set private key219try {220ks.setEntry("newPrivKey", pkeNew, null);221throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +222"expected KeyStoreException");223} catch (KeyStoreException kse) {224System.out.println("[Pre1.5] test " + tNum++ + " passed");225}226227228// TEST setEntry with alias/entry/random protParam - set private key229try {230ks.setEntry("newPrivKey", pkeNew, new FooProtect());231throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +232"expected KeyStoreException");233} catch (KeyStoreException kse) {234System.out.println("[Pre1.5] test " + tNum++ + " passed");235}236237238// TEST setEntry with alias/entry/protParam - set private key239ks.setEntry("newPrivKey", pkeNew,240new KeyStore.PasswordProtection(password));241e = ks.getEntry("newPrivKey",242new KeyStore.PasswordProtection(password));243if (e instanceof KeyStore.PrivateKeyEntry) {244System.out.println("[Pre1.5] test " + tNum++ + " passed");245} else {246throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +247"expected PrivateKeyEntry");248}249250251// TEST setEntry with alias/entry/non null protParam - set trusted cert252try {253ks.setEntry("newTrustedcert", tceNew,254new KeyStore.PasswordProtection(password));255throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +256"expected KeyStoreException");257} catch (KeyStoreException kse) {258// good259System.out.println("[Pre1.5] test " + tNum++ + " passed");260}261262263// TEST setEntry with alias/entry/null protParam - set trusted cert264ks.setEntry("newTrustedcert", tceNew, null);265e = ks.getEntry("newTrustedcert", null);266if (e instanceof KeyStore.TrustedCertificateEntry) {267System.out.println("[Pre1.5] test " + tNum++ + " passed");268} else {269throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +270"expected TrustedCertificateEntry");271}272273274// TEST entryInstanceOf - invalid alias275if (ks.entryInstanceOf("foo", EntryMethods.class) == false) {276System.out.println("[Pre1.5] test " + tNum++ + " passed");277} else {278throw new SecurityException("[Pre1.5] test " + tNum + " failed");279}280281282// TEST entryInstanceOf - false case283if (ks.entryInstanceOf("privkey", EntryMethods.class) == false) {284System.out.println("[Pre1.5] test " + tNum++ + " passed");285} else {286throw new SecurityException("[Pre1.5] test " + tNum + " failed");287}288289290// TEST entryInstanceOf - true case, trustedcert entry291if (ks.entryInstanceOf("trustedcert",292KeyStore.TrustedCertificateEntry.class)) {293System.out.println("[Pre1.5] test " + tNum++ + " passed");294} else {295throw new SecurityException("[Pre1.5] test " + tNum + " failed");296}297298299// TEST entryInstanceOf - true case, private key entry300if (ks.entryInstanceOf("privkey",301KeyStore.PrivateKeyEntry.class)) {302System.out.println("[Pre1.5] test " + tNum++ + " passed");303} else {304throw new SecurityException("[Pre1.5] test " + tNum + " failed");305}306307}308309private static void testPost15(KeyStore ks) throws Exception {310311KeyStore.Entry e = null;312313ks.load(new EntryMethods.FooParameter());314ks.store(new EntryMethods.FooParameter());315316e = ks.getEntry("foo", new KeyStore.PasswordProtection(password));317if (!(e instanceof EntryMethods.FooEntry)) {318throw new SecurityException319("testPost15 getEntry(String, ProtParm) " +320"expected EntryMethods.FooEntry returned");321}322323ks.setEntry("foo", new EntryMethods.FooEntry(),324new KeyStore.PasswordProtection(password));325326if (!ks.entryInstanceOf("foo", KeyStore.PrivateKeyEntry.class)) {327throw new SecurityException328("testPost15 entryInstanceOf(String, Class) " +329"expected true returned");330}331332System.out.println("[Post1.5] tests all passed");333}334335private static void testUnrecoverable(KeyStore ks) throws Exception {336ks.load(new EntryMethods.FooParameter());337try {338ks.getEntry("foo", new KeyStore.PasswordProtection(password));339throw new SecurityException340("UnrecoverableEntryException not thrown for " +341"getEntry(String, ProtectionParam)");342} catch (UnrecoverableEntryException uee) {343// good344System.out.println("[UnrecoverableEntry] test passed");345}346}347348public static class Pre15 extends KeyStoreSpi {349350private static KeyStoreSpi jks = getJKS();351352// javac does not allow direct access to class (javac bug?)353// use reflection instead354private static KeyStoreSpi getJKS() {355try {356Class clazz = Class.forName("sun.security.provider.JavaKeyStore$JKS");357return (KeyStoreSpi)clazz.newInstance();358} catch (Exception e) {359e.printStackTrace();360throw new RuntimeException(e);361}362}363364public Key engineGetKey(String alias, char[] password)365throws NoSuchAlgorithmException, UnrecoverableKeyException {366return jks.engineGetKey(alias, password);367}368369public java.security.cert.Certificate[] engineGetCertificateChain370(String alias) {371return jks.engineGetCertificateChain(alias);372}373374public java.security.cert.Certificate engineGetCertificate375(String alias) {376return jks.engineGetCertificate(alias);377}378379public Date engineGetCreationDate(String alias) {380return jks.engineGetCreationDate(alias);381}382383public void engineSetKeyEntry(String alias, Key key,384char[] password,385java.security.cert.Certificate[] chain)386throws KeyStoreException {387jks.engineSetKeyEntry(alias, key, password, chain);388}389390public void engineSetKeyEntry(String alias, byte[] key,391java.security.cert.Certificate[] chain)392throws KeyStoreException {393jks.engineSetKeyEntry(alias, key, chain);394}395396public void engineSetCertificateEntry(String alias,397java.security.cert.Certificate cert)398throws KeyStoreException {399jks.engineSetCertificateEntry(alias, cert);400}401402public void engineDeleteEntry(String alias)403throws KeyStoreException {404jks.engineDeleteEntry(alias);405}406407public Enumeration engineAliases() {408return jks.engineAliases();409}410411public boolean engineContainsAlias(String alias) {412return jks.engineContainsAlias(alias);413}414415public int engineSize() {416return jks.engineSize();417}418419public boolean engineIsKeyEntry(String alias) {420return jks.engineIsKeyEntry(alias);421}422423public boolean engineIsCertificateEntry(String alias) {424return jks.engineIsCertificateEntry(alias);425}426427public String engineGetCertificateAlias428(java.security.cert.Certificate cert) {429return jks.engineGetCertificateAlias(cert);430}431432public void engineStore(OutputStream stream, char[] password)433throws IOException, NoSuchAlgorithmException, CertificateException {434jks.engineStore(stream, password);435}436437public void engineLoad(InputStream stream, char[] password)438throws IOException, NoSuchAlgorithmException, CertificateException {439jks.engineLoad(stream, password);440}441}442443public static class Post15 extends Pre15 {444445public void engineStore(KeyStore.LoadStoreParameter parameter)446throws IOException, NoSuchAlgorithmException, CertificateException {447if (!(parameter instanceof EntryMethods.FooParameter)) {448throw new IOException("Post15 engineStore method expected " +449"FooParameter");450}451}452453public void engineLoad(KeyStore.LoadStoreParameter parameter)454throws IOException, NoSuchAlgorithmException, CertificateException {455if (!(parameter instanceof EntryMethods.FooParameter)) {456throw new IOException("Post15 engineLoadFrom method expected " +457"FooParameter");458}459}460461public KeyStore.Entry engineGetEntry(String alias,462KeyStore.ProtectionParameter protectionParam)463throws UnrecoverableEntryException {464if (!alias.equals("foo")) {465throw new SecurityException466("Post15 engineGetEntry(String, ProtectionParam) " +467"expected [foo] alias");468}469KeyStore.PasswordProtection pwdParam =470(KeyStore.PasswordProtection)protectionParam;471if (pwdParam.getPassword().length != 6) {472throw new SecurityException473("Post15 engineGetEntry(String, ProtectionParam) " +474"expected [foobar] password");475}476477return new EntryMethods.FooEntry();478}479480public void engineSetEntry(String alias, KeyStore.Entry entry,481KeyStore.ProtectionParameter protectionParam) {482if (!alias.equals("foo") ||483!(entry instanceof EntryMethods.FooEntry)) {484throw new SecurityException485("Post15 engineSetEntry(String, entry, ProtParm) " +486"expected [foo] alias and EntryMethods.FooEntry");487}488489KeyStore.PasswordProtection pwdParam =490(KeyStore.PasswordProtection)protectionParam;491if (pwdParam.getPassword().length != 6) {492throw new SecurityException493("Post15 engineSetEntry(String, entry, ProtParm) " +494"expected [foobar] password");495}496}497498public boolean engineEntryInstanceOf(String alias,499Class<? extends KeyStore.Entry> entryClass)500{501if (!alias.equals("foo") ||502entryClass != KeyStore.PrivateKeyEntry.class) {503throw new SecurityException504("Post15 engineEntryInstanceOf(String, Class) " +505"expected [foo] alias " +506"and [KeyStore.PrivateKeyEntry] class");507}508return true;509}510}511512public static class UnrecoverableKS extends Post15 {513public KeyStore.Entry engineGetEntry(String alias,514KeyStore.ProtectionParameter protectionParam)515throws UnrecoverableEntryException {516throw new UnrecoverableEntryException();517}518}519}520521522