Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyStore/Basic.java
38855 views
/*1* Copyright (c) 2003, 2006, 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*/2223import java.io.*;24import java.util.*;25import java.lang.reflect.*;2627import java.security.KeyStore;28import java.security.KeyStoreException;29import java.security.KeyFactory;30import java.security.KeyPairGenerator;31import java.security.KeyPair;32import java.security.SecureRandom;33import java.security.AuthProvider;34import java.security.PrivateKey;35import java.security.Provider;36import java.security.ProviderException;37import java.security.Signature;38import java.security.Security;3940import java.security.cert.*;41import java.security.spec.*;42import java.security.interfaces.*;4344import javax.crypto.SecretKey;4546import javax.security.auth.Subject;47import javax.security.auth.login.LoginException;4849import com.sun.security.auth.module.*;50import com.sun.security.auth.callback.*;515253public class Basic extends PKCS11Test {5455private static final char SEP = File.separatorChar;5657private static String DIR = System.getProperty("DIR");58private static char[] tokenPwd;59private static final char[] ibuttonPwd =60new char[0];61private static final char[] activcardPwd =62new char[] { '1', '1', '2', '2', '3', '3' };63private static final char[] nssPwd =64new char[] { 't', 'e', 's', 't', '1', '2' };65private static final char[] solarisPwd =66new char[] { 'p', 'i', 'n' };67private static final char[] sca1000Pwd =68new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };69private static final char[] sPwd = { 'f', 'o', 'o' };7071private static SecretKey sk1;72private static SecretKey sk2;73private static SecretKey sk3;74private static SecretKey sk4;7576private static RSAPrivateCrtKey pk1;77private static PrivateKey pk2;78private static PrivateKey pk3;7980private static Certificate[] chain1;81private static Certificate[] chain2;82private static Certificate[] chain3;83private static Certificate[] chain4;8485private static X509Certificate randomCert;8687private static KeyStore ks;88private static final String KS_TYPE = "PKCS11";89private static Provider provider;9091private static class FooEntry implements KeyStore.Entry { }9293private static class P11SecretKey implements SecretKey {94String alg;95int length;96public P11SecretKey(String alg, int length) {97this.alg = alg;98this.length = length;99}100public String getAlgorithm() { return alg; }101public String getFormat() { return "raw"; }102public byte[] getEncoded() { return new byte[length/8]; }103}104105public static void main(String[] args) throws Exception {106main(new Basic());107}108109public void main(Provider p) throws Exception {110111this.provider = p;112113// get private keys114KeyFactory kf = KeyFactory.getInstance("RSA", "SunJSSE");115KeyFactory dsaKf = KeyFactory.getInstance("DSA", "SUN");116117ObjectInputStream ois1 = new ObjectInputStream118(new FileInputStream(new File(DIR, "pk1.key")));119byte[] keyBytes = (byte[])ois1.readObject();120ois1.close();121PrivateKey tmpKey =122kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));123pk1 = (RSAPrivateCrtKey)tmpKey;124125ObjectInputStream ois2 = new ObjectInputStream126(new FileInputStream(new File(DIR, "pk2.key")));127keyBytes = (byte[])ois2.readObject();128ois2.close();129pk2 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));130131ObjectInputStream ois3 = new ObjectInputStream132(new FileInputStream(new File(DIR, "pk3.key")));133keyBytes = (byte[])ois3.readObject();134pk3 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));135ois3.close();136137// get cert chains for private keys138CertificateFactory cf = CertificateFactory.getInstance("X.509", "SUN");139Certificate caCert = (X509Certificate)cf.generateCertificate140(new FileInputStream(new File(DIR, "ca.cert")));141Certificate ca2Cert = (X509Certificate)cf.generateCertificate142(new FileInputStream(new File(DIR, "ca2.cert")));143Certificate pk1cert = (X509Certificate)cf.generateCertificate144(new FileInputStream(new File(DIR, "pk1.cert")));145Certificate pk1cert2 = (X509Certificate)cf.generateCertificate146(new FileInputStream(new File(DIR, "pk1.cert2")));147Certificate pk2cert = (X509Certificate)cf.generateCertificate148(new FileInputStream(new File(DIR, "pk2.cert")));149Certificate pk3cert = (X509Certificate)cf.generateCertificate150(new FileInputStream(new File(DIR, "pk3.cert")));151chain1 = new Certificate[] { pk1cert, caCert };152chain2 = new Certificate[] { pk2cert, caCert };153chain3 = new Certificate[] { pk3cert, caCert };154chain4 = new Certificate[] { pk1cert2, ca2Cert };155156// create secret keys157sk1 = new P11SecretKey("DES", 64);158sk2 = new P11SecretKey("DESede", 192);159sk3 = new P11SecretKey("AES", 128);160sk4 = new P11SecretKey("RC4", 128);161162// read randomCert163randomCert = (X509Certificate)cf.generateCertificate164(new FileInputStream(new File(DIR, "random.cert")));165166doTest();167}168169private static void doTest() throws Exception {170171String token = System.getProperty("TOKEN");172String test = System.getProperty("TEST");173174if (token == null || token.length() == 0) {175throw new Exception("token arg required");176}177if (test == null || test.length() == 0) {178throw new Exception("test arg required");179}180181if ("ibutton".equals(token)) {182tokenPwd = ibuttonPwd;183} else if ("activcard".equals(token)) {184tokenPwd = activcardPwd;185} else if ("nss".equals(token)) {186tokenPwd = nssPwd;187} else if ("sca1000".equals(token)) {188tokenPwd = sca1000Pwd;189} else if ("solaris".equals(token)) {190tokenPwd = solarisPwd;191}192193if ("list".equals(test)) {194Basic.list();195} else if ("basic".equals(test)) {196197int testnum = 1;198199if ("ibutton".equals(token)) {200// pkey and setAttribute201testnum = Basic.pkey(testnum);202testnum = Basic.setAttribute(testnum);203} else if ("activcard".equals(token)) {204// sign205testnum = Basic.signAlias(testnum, null);206} else if ("nss".equals(token)) {207// setAttribute, pkey, sign208testnum = Basic.setAttribute(testnum);209testnum = Basic.pkey(testnum);210testnum = Basic.sign(testnum);211testnum = Basic.copy(testnum);212} else if ("solaris".equals(token)) {213testnum = Basic.setAttribute(testnum);214testnum = Basic.pkey(testnum);215testnum = Basic.sign(testnum);216testnum = Basic.skey(testnum);217testnum = Basic.copy(testnum);218} else if ("sca1000".equals(token)) {219// setAttribute, pkey, sign, skey, copy220testnum = Basic.setAttribute(testnum);221testnum = Basic.pkey(testnum);222testnum = Basic.sign(testnum);223testnum = Basic.skey(testnum);224testnum = Basic.copy(testnum);225}226227} else if ("pkey".equals(test)) {228Basic.pkey(1);229} else if ("skey".equals(test)) {230Basic.skey(1);231} else if ("setAttribute".equals(test)) {232Basic.setAttribute(1);233} else if ("copy".equals(test)) {234Basic.copy(1);235} else if ("sign".equals(test)) {236Basic.sign(1);237} else if ("module".equals(test)) {238Basic.module();239} else if ("nss-extended".equals(test)) {240241// this only works if NSS_TEST is set to true in P11KeyStore.java242243int testnum = 1;244testnum = Basic.setAttribute(testnum);245testnum = Basic.pkey(testnum);246testnum = Basic.sign(testnum);247testnum = Basic.extended(testnum);248} else {249System.out.println("unrecognized command");250}251}252253private static int sign(int testnum) throws Exception {254if (ks == null) {255ks = KeyStore.getInstance(KS_TYPE, provider);256ks.load(null, tokenPwd);257}258if (!ks.containsAlias("pk1")) {259ks.setKeyEntry("pk1", pk1, null, chain1);260}261System.out.println("test " + testnum++ + " passed");262263return signAlias(testnum, "pk1");264}265266private static int signAlias(int testnum, String alias) throws Exception {267268if (ks == null) {269ks = KeyStore.getInstance(KS_TYPE, provider);270ks.load(null, tokenPwd);271}272273if (alias == null) {274Enumeration enu = ks.aliases();275if (enu.hasMoreElements()) {276alias = (String)enu.nextElement();277}278}279280PrivateKey pkey = (PrivateKey)ks.getKey(alias, null);281if ("RSA".equals(pkey.getAlgorithm())) {282System.out.println("got [" + alias + "] signing key: " + pkey);283} else {284throw new SecurityException285("expected RSA, got " + pkey.getAlgorithm());286}287288Signature s = Signature.getInstance("MD5WithRSA", ks.getProvider());289s.initSign(pkey);290System.out.println("initialized signature object with key");291s.update("hello".getBytes());292System.out.println("signature object updated with [hello] bytes");293294byte[] signed = s.sign();295System.out.println("received signature " + signed.length +296" bytes in length");297298Signature v = Signature.getInstance("MD5WithRSA", ks.getProvider());299v.initVerify(ks.getCertificate(alias));300v.update("hello".getBytes());301v.verify(signed);302System.out.println("signature verified");303System.out.println("test " + testnum++ + " passed");304305return testnum;306}307308private static int copy(int testnum) throws Exception {309310if (ks == null) {311ks = KeyStore.getInstance(KS_TYPE, provider);312ks.load(null, tokenPwd);313}314315KeyFactory kf = KeyFactory.getInstance("RSA", provider);316PrivateKey pkSession = (PrivateKey)kf.translateKey(pk3);317System.out.println("pkSession = " + pkSession);318ks.setKeyEntry("pkSession", pkSession, null, chain3);319320KeyStore.PrivateKeyEntry pke =321(KeyStore.PrivateKeyEntry)ks.getEntry("pkSession", null);322System.out.println("pkSession = " + pke.getPrivateKey());323Certificate[] chain = pke.getCertificateChain();324if (chain.length != chain3.length) {325throw new SecurityException("received chain not correct length");326}327for (int i = 0; i < chain.length; i++) {328if (!chain[i].equals(chain3[i])) {329throw new SecurityException("received chain not equal");330}331}332333System.out.println("test " + testnum++ + " passed");334335return testnum;336}337338private static void list() throws Exception {339int testnum = 1;340341ks = KeyStore.getInstance(KS_TYPE, provider);342343// check instance344if (ks.getProvider() instanceof java.security.AuthProvider) {345System.out.println("keystore provider instance of AuthProvider");346System.out.println("test " + testnum++ + " passed");347} else {348throw new SecurityException("did not get AuthProvider KeyStore");349}350351// load352ks.load(null, tokenPwd);353System.out.println("test " + testnum++ + " passed");354355// aliases356Enumeration enu = ks.aliases();357int count = 0;358while (enu.hasMoreElements()) {359count++;360System.out.println("alias " +361count +362" = " +363(String)enu.nextElement());364}365}366367private static void module() throws Exception {368369// perform Security.addProvider of P11 provider370ProviderLoader.go(System.getProperty("CUSTOM_P11_CONFIG"));371372String KS_PROVIDER = "SunPKCS11-" + System.getProperty("TOKEN");373374KeyStoreLoginModule m = new KeyStoreLoginModule();375Subject s = new Subject();376Map options = new HashMap();377options.put("keyStoreURL", "NONE");378options.put("keyStoreType", KS_TYPE);379options.put("keyStoreProvider", KS_PROVIDER);380options.put("debug", "true");381m.initialize(s, new TextCallbackHandler(), new HashMap(), options);382m.login();383m.commit();384System.out.println("authenticated subject = " + s);385m.logout();386System.out.println("authenticated subject = " + s);387}388389/**390* SCA1000 does not handle extended secret key tests391* . Blowfish (CKR_TEMPLATE_INCOMPLETE)392* . AES (CKR_TEMPLATE_INCOMPLETE)393* . RC4 (CKR_ATTRIBUTE_TYPE_INVALID)394* so do this instead395*/396private static int skey(int testnum) throws Exception {397if (ks == null) {398ks = KeyStore.getInstance(KS_TYPE, provider);399ks.load(null, tokenPwd);400}401402// delete all old aliases403Enumeration enu = ks.aliases();404int count = 0;405while (enu.hasMoreElements()) {406String next = (String)enu.nextElement();407ks.deleteEntry(next);408System.out.println("deleted entry for: " + next);409}410411// set good ske 1412ks.setKeyEntry("sk1", sk1, null, null);413System.out.println("test " + testnum++ + " passed");414415// set good ske 2416ks.setKeyEntry("sk2", sk2, null, null);417System.out.println("test " + testnum++ + " passed");418419// getEntry good ske 1420KeyStore.SecretKeyEntry ske =421(KeyStore.SecretKeyEntry)ks.getEntry("sk1", null);422if ("DES".equals(ske.getSecretKey().getAlgorithm())) {423System.out.println("test " + testnum++ + " passed");424} else {425throw new SecurityException426("expected DES, got " + ske.getSecretKey().getAlgorithm());427}428429// getEntry good ske 2430ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null);431if ("DESede".equals(ske.getSecretKey().getAlgorithm())) {432System.out.println("test " + testnum++ + " passed");433} else {434throw new SecurityException435("expected DESede, got " + ske.getSecretKey().getAlgorithm());436}437438// getKey good ske 1439SecretKey skey = (SecretKey)ks.getKey("sk1", null);440if ("DES".equals(skey.getAlgorithm())) {441System.out.println("test " + testnum++ + " passed");442} else {443throw new SecurityException444("expected DES, got " + skey.getAlgorithm());445}446447// getKey good ske 2448skey = (SecretKey)ks.getKey("sk2", null);449if ("DESede".equals(skey.getAlgorithm())) {450System.out.println("test " + testnum++ + " passed");451} else {452throw new SecurityException453("expected DESede, got " + skey.getAlgorithm());454}455456// aliases457enu = ks.aliases();458count = 0;459while (enu.hasMoreElements()) {460count++;461System.out.println("alias " +462count +463" = " +464(String)enu.nextElement());465}466if (count == 2) {467System.out.println("test " + testnum++ + " passed");468} else {469throw new SecurityException("expected 2 aliases");470}471472// size473if (ks.size() == 2) {474System.out.println("test " + testnum++ + " passed");475} else {476throw new SecurityException("expected size 2");477}478479// isCertificateEntry sk1480if (!ks.isCertificateEntry("sk1")) {481System.out.println("test " + testnum++ + " passed");482} else {483throw new SecurityException("expected ske");484}485486// isKeyEntry sk1487if (ks.isKeyEntry("sk1")) {488System.out.println("test " + testnum++ + " passed");489} else {490throw new SecurityException("expected ske");491}492493// entryInstanceOf sk2494if (ks.entryInstanceOf("sk2", KeyStore.SecretKeyEntry.class)) {495System.out.println("test " + testnum++ + " passed");496} else {497throw new SecurityException("expected ske");498}499500return testnum;501}502503private static int setAttribute(int testnum) throws Exception {504505if (ks == null) {506ks = KeyStore.getInstance(KS_TYPE, provider);507ks.load(null, tokenPwd);508}509510if (!ks.containsAlias("pk1")) {511// set good pke 1512ks.setKeyEntry("pk1", pk1, null, chain1);513System.out.println("test " + testnum++ + " passed");514}515516// delete all old aliases except pk1517Enumeration enu = ks.aliases();518int count = 0;519while (enu.hasMoreElements()) {520String next = (String)enu.nextElement();521if (!"pk1".equals(next)) {522ks.deleteEntry(next);523System.out.println("deleted entry for: " + next);524}525}526527KeyStore.PrivateKeyEntry pke =528(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);529System.out.println("pk1 = " + pke.getPrivateKey());530Certificate[] chain = pke.getCertificateChain();531if (chain.length != chain1.length) {532throw new SecurityException("received chain not correct length");533}534for (int i = 0; i < chain.length; i++) {535if (!chain[i].equals(chain1[i])) {536throw new SecurityException("received chain not equal");537}538}539System.out.println("test " + testnum++ + " passed");540541/**542* test change alias only543*/544545// test C_SetAttribute546PrivateKey pkey = pke.getPrivateKey();547ks.setEntry("pk1SA",548new KeyStore.PrivateKeyEntry(pkey, chain1),549null);550System.out.println("test " + testnum++ + " passed");551552// aliases553enu = ks.aliases();554count = 0;555String newAlias = null;556while (enu.hasMoreElements()) {557count++;558newAlias = (String)enu.nextElement();559System.out.println("alias " +560count +561" = " +562newAlias);563}564if (count == 1 && "pk1SA".equals(newAlias)) {565System.out.println("test " + testnum++ + " passed");566} else {567throw new SecurityException("expected 1 alias");568}569570// size571if (ks.size() == 1) {572System.out.println("test " + testnum++ + " passed");573} else {574throw new SecurityException("expected size 1");575}576577pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);578if (pke != null) {579throw new SecurityException("expected not to find pk1");580}581System.out.println("test " + testnum++ + " passed");582583pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null);584System.out.println("pk1SA = " + pke.getPrivateKey());585chain = pke.getCertificateChain();586if (chain.length != chain1.length) {587throw new SecurityException("received chain not correct length");588}589for (int i = 0; i < chain.length; i++) {590if (!chain[i].equals(chain1[i])) {591throw new SecurityException("received chain not equal");592}593}594System.out.println("test " + testnum++ + " passed");595596/**597* test change cert chain598*/599600pkey = pke.getPrivateKey();601ks.setEntry("pk1SA-2",602new KeyStore.PrivateKeyEntry(pkey, chain4),603null);604System.out.println("test " + testnum++ + " passed");605606// aliases607enu = ks.aliases();608count = 0;609newAlias = null;610while (enu.hasMoreElements()) {611count++;612newAlias = (String)enu.nextElement();613System.out.println("alias " +614count +615" = " +616newAlias);617}618if (count == 1 && "pk1SA-2".equals(newAlias)) {619System.out.println("test " + testnum++ + " passed");620} else {621throw new SecurityException("expected 1 alias");622}623624// size625if (ks.size() == 1) {626System.out.println("test " + testnum++ + " passed");627} else {628throw new SecurityException("expected size 1");629}630631pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null);632if (pke != null) {633throw new SecurityException("expected not to find pk1SA");634}635System.out.println("test " + testnum++ + " passed");636637pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA-2", null);638System.out.println("pk1SA-2 = " + pke.getPrivateKey());639chain = pke.getCertificateChain();640if (chain.length != chain4.length) {641throw new SecurityException("received chain not correct length");642}643for (int i = 0; i < chain.length; i++) {644if (!chain[i].equals(chain4[i])) {645throw new SecurityException("received chain not equal");646}647}648System.out.println("test " + testnum++ + " passed");649650return testnum;651}652653private static int pkey(int testnum) throws Exception {654655if (ks == null) {656ks = KeyStore.getInstance(KS_TYPE, provider);657ks.load(null, tokenPwd);658System.out.println("test " + testnum++ + " passed");659}660661// check instance662if (ks.getProvider() instanceof java.security.AuthProvider) {663System.out.println("keystore provider instance of AuthProvider");664System.out.println("test " + testnum++ + " passed");665} else {666throw new SecurityException("did not get AuthProvider KeyStore");667}668669// delete all old aliases670Enumeration enu = ks.aliases();671int count = 0;672while (enu.hasMoreElements()) {673String next = (String)enu.nextElement();674ks.deleteEntry(next);675System.out.println("deleted entry for: " + next);676}677678// set good pke 1679ks.setKeyEntry("pk1", pk1, null, chain1);680System.out.println("test " + testnum++ + " passed");681682// set good pke 2683ks.setEntry("pk2",684new KeyStore.PrivateKeyEntry(pk2, chain2),685null);686System.out.println("test " + testnum++ + " passed");687688// getEntry good pke 1689KeyStore.PrivateKeyEntry pke =690(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);691System.out.println("pk1 = " + pke.getPrivateKey());692Certificate[] chain = pke.getCertificateChain();693if (chain.length != chain1.length) {694throw new SecurityException("received chain not correct length");695}696for (int i = 0; i < chain.length; i++) {697if (!chain[i].equals(chain1[i])) {698throw new SecurityException("received chain not equal");699}700}701702// getKey good pke 1703PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null);704System.out.println("pk1 = " + pkey);705if ("RSA".equals(pkey.getAlgorithm())) {706System.out.println("test " + testnum++ + " passed");707} else {708throw new SecurityException709("expected RSA, got " + pkey.getAlgorithm());710}711712// getCertificate chain chain 1713chain = ks.getCertificateChain("pk1");714if (chain.length != chain1.length) {715throw new SecurityException("received chain not correct length");716}717for (int i = 0; i < chain.length; i++) {718if (!chain[i].equals(chain1[i])) {719throw new SecurityException("received chain not equal");720}721}722System.out.println("test " + testnum++ + " passed");723724// getEntry good pke 2725pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null);726if ("RSA".equals(pke.getPrivateKey().getAlgorithm())) {727System.out.println("test " + testnum++ + " passed");728} else {729throw new SecurityException730("expected RSA, got " + pke.getPrivateKey().getAlgorithm());731}732System.out.println("pk2 = " + pke.getPrivateKey());733chain = pke.getCertificateChain();734if (chain.length != chain2.length) {735throw new SecurityException("received chain not correct length");736}737for (int i = 0; i < chain.length; i++) {738if (!chain[i].equals(chain2[i])) {739throw new SecurityException("received chain not equal");740}741}742743// getKey good pke 2744pkey = (PrivateKey)ks.getKey("pk2", null);745if ("RSA".equals(pkey.getAlgorithm())) {746System.out.println("test " + testnum++ + " passed");747} else {748throw new SecurityException749("expected RSA, got " + pkey.getAlgorithm());750}751752// getCertificate chain chain 2753chain = ks.getCertificateChain("pk2");754if (chain.length != chain2.length) {755throw new SecurityException("received chain not correct length");756}757for (int i = 0; i < chain.length; i++) {758if (!chain[i].equals(chain2[i])) {759throw new SecurityException("received chain not equal");760}761}762System.out.println("test " + testnum++ + " passed");763764// aliases765enu = ks.aliases();766count = 0;767while (enu.hasMoreElements()) {768count++;769System.out.println("alias " +770count +771" = " +772(String)enu.nextElement());773}774if (count == 2) {775System.out.println("test " + testnum++ + " passed");776} else {777throw new SecurityException("expected 2 aliases");778}779780// size781if (ks.size() == 2) {782System.out.println("test " + testnum++ + " passed");783} else {784throw new SecurityException("expected size 2");785}786787// getCertificate788if (ks.getCertificate("pk1").equals(chain1[0])) {789System.out.println("test " + testnum++ + " passed");790} else {791throw new SecurityException("expected certificate pk1 end entity");792}793794// containsAlias795if (ks.containsAlias("pk1") && ks.containsAlias("pk2") &&796!ks.containsAlias("foobar") &&797!ks.containsAlias("pk1.2") && !ks.containsAlias("pk2.2")) {798System.out.println("test " + testnum++ + " passed");799} else {800throw new SecurityException("unexpected aliases encountered");801}802803// isKeyEntry804if (ks.isKeyEntry("pk1") && ks.isKeyEntry("pk2") &&805!ks.isKeyEntry("foobar")) {806System.out.println("test " + testnum++ + " passed");807} else {808throw new SecurityException("isKeyEntry failed");809}810811// isCertificateEntry812if (!ks.isCertificateEntry("foobar") &&813!ks.isCertificateEntry("pk1") && !ks.isCertificateEntry("pk2")) {814System.out.println("test " + testnum++ + " passed");815} else {816throw new SecurityException("isCertificateEntry failed");817}818819// getCertificateAlias820if (ks.getCertificateAlias(chain1[0]).equals("pk1") &&821ks.getCertificateAlias(chain2[0]).equals("pk2") &&822ks.getCertificateAlias(randomCert) == null) {823System.out.println("test " + testnum++ + " passed");824} else {825throw new SecurityException("getCertificateAlias failed");826}827828if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class) &&829ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class) &&830!ks.entryInstanceOf("pk1", KeyStore.TrustedCertificateEntry.class) &&831!ks.entryInstanceOf("pk2", KeyStore.TrustedCertificateEntry.class) &&832!ks.entryInstanceOf("foobar", KeyStore.TrustedCertificateEntry.class) &&833!ks.entryInstanceOf("foobar", KeyStore.PrivateKeyEntry.class)) {834System.out.println("test " + testnum++ + " passed");835} else {836throw new SecurityException("entryInstanceOf failed");837}838839ks.deleteEntry("pk2");840if (ks.containsAlias("pk1") && !ks.containsAlias("pk2")) {841System.out.println("test " + testnum++ + " passed");842} else {843throw new SecurityException("deleteEntry failed");844}845846// getEntry good pke 1847pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);848System.out.println("pk1 = " + pke.getPrivateKey());849chain = pke.getCertificateChain();850if (chain.length != chain1.length) {851throw new SecurityException("received chain not correct length");852}853for (int i = 0; i < chain.length; i++) {854if (!chain[i].equals(chain1[i])) {855throw new SecurityException("received chain not equal");856}857}858System.out.println("test " + testnum++ + " passed");859860// aliases861enu = ks.aliases();862count = 0;863while (enu.hasMoreElements()) {864count++;865System.out.println("alias " +866count +867" = " +868(String)enu.nextElement());869}870if (count == 1) {871System.out.println("test " + testnum++ + " passed");872} else {873throw new SecurityException("expected 1 alias");874}875876// size877if (ks.size() == 1) {878System.out.println("test " + testnum++ + " passed");879} else {880throw new SecurityException("expected size 1");881}882883return testnum;884}885886private static int extended(int testnum) throws Exception {887888// setEntry unknown entry type889try {890ks.setEntry("foo", new FooEntry(), null);891throw new SecurityException("setEntry should have failed");892} catch (KeyStoreException kse) {893System.out.println("test " + testnum++ + " passed");894}895896// getEntry random foo897if (ks.getEntry("foo", null) != null) {898throw new SecurityException("expected null entry");899} else {900System.out.println("test " + testnum++ + " passed");901}902903// set good ske 1904ks.setKeyEntry("sk1", sk1, null, null);905System.out.println("test " + testnum++ + " passed");906907// set good ske 2908ks.setKeyEntry("sk2", sk2, null, null);909System.out.println("test " + testnum++ + " passed");910911// set good ske 3912ks.setEntry("sk3",913new KeyStore.SecretKeyEntry(sk3),914null);915System.out.println("test " + testnum++ + " passed");916917// set good ske 4918ks.setEntry("sk4",919new KeyStore.SecretKeyEntry(sk4),920null);921System.out.println("test " + testnum++ + " passed");922923// getEntry good ske 1924KeyStore.SecretKeyEntry ske =925(KeyStore.SecretKeyEntry)ks.getEntry("sk1", null);926if ("DES".equals(ske.getSecretKey().getAlgorithm())) {927System.out.println("test " + testnum++ + " passed");928} else {929throw new SecurityException930("expected DES, got " + ske.getSecretKey().getAlgorithm());931}932933// getEntry good ske 2934ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null);935if ("DESede".equals(ske.getSecretKey().getAlgorithm())) {936System.out.println("test " + testnum++ + " passed");937} else {938throw new SecurityException939("expected DESede, got " + ske.getSecretKey().getAlgorithm());940}941942// getEntry good ske 3943ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk3", null);944if ("AES".equals(ske.getSecretKey().getAlgorithm())) {945System.out.println("test " + testnum++ + " passed");946} else {947throw new SecurityException948("expected AES, got " + ske.getSecretKey().getAlgorithm());949}950951// getEntry good ske 4952ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk4", null);953if ("ARCFOUR".equals(ske.getSecretKey().getAlgorithm())) {954System.out.println("test " + testnum++ + " passed");955} else {956throw new SecurityException957("expected ARCFOUR, got " + ske.getSecretKey().getAlgorithm());958}959960// getKey good ske 1961SecretKey skey = (SecretKey)ks.getKey("sk1", null);962if ("DES".equals(skey.getAlgorithm())) {963System.out.println("test " + testnum++ + " passed");964} else {965throw new SecurityException966("expected DES, got " + skey.getAlgorithm());967}968969// getKey good ske 2970skey = (SecretKey)ks.getKey("sk2", null);971if ("DESede".equals(skey.getAlgorithm())) {972System.out.println("test " + testnum++ + " passed");973} else {974throw new SecurityException975("expected DESede, got " + skey.getAlgorithm());976}977978// getKey good ske 3979skey = (SecretKey)ks.getKey("sk3", null);980if ("AES".equals(skey.getAlgorithm())) {981System.out.println("test " + testnum++ + " passed");982} else {983throw new SecurityException984("expected AES, got " + skey.getAlgorithm());985}986987// getKey good ske 4988skey = (SecretKey)ks.getKey("sk4", null);989if ("ARCFOUR".equals(skey.getAlgorithm())) {990System.out.println("test " + testnum++ + " passed");991} else {992throw new SecurityException993("expected ARCFOUR, got " + skey.getAlgorithm());994}995996// aliases997Enumeration enu = ks.aliases();998int count = 0;999while (enu.hasMoreElements()) {1000count++;1001System.out.println("alias " +1002count +1003" = " +1004(String)enu.nextElement());1005}1006if (count == 5) {1007System.out.println("test " + testnum++ + " passed");1008} else {1009throw new SecurityException("expected 5 aliases");1010}10111012// size1013if (ks.size() == 5) {1014System.out.println("test " + testnum++ + " passed");1015} else {1016throw new SecurityException("expected size 5");1017}10181019// set good pke 21020ks.setEntry("pk2",1021new KeyStore.PrivateKeyEntry(pk2, chain2),1022null);1023System.out.println("test " + testnum++ + " passed");10241025// set good pke 31026ks.setEntry("pk3",1027new KeyStore.PrivateKeyEntry(pk3, chain3),1028null);1029System.out.println("test " + testnum++ + " passed");10301031// getEntry good pke 11032KeyStore.PrivateKeyEntry pke =1033(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);1034System.out.println("pk1 = " + pke.getPrivateKey());1035Certificate[] chain = pke.getCertificateChain();1036if (chain.length != chain1.length) {1037throw new SecurityException("received chain not correct length");1038}1039for (int i = 0; i < chain.length; i++) {1040if (!chain[i].equals(chain1[i])) {1041throw new SecurityException("received chain not equal");1042}1043}10441045// getEntry good pke 21046pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null);1047System.out.println("pk2 = " + pke.getPrivateKey());1048chain = pke.getCertificateChain();1049if (chain.length != chain2.length) {1050throw new SecurityException("received chain not correct length");1051}1052for (int i = 0; i < chain.length; i++) {1053if (!chain[i].equals(chain2[i])) {1054throw new SecurityException("received chain not equal");1055}1056}10571058// getEntry good pke 31059pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk3", null);1060System.out.println("pk3 = " + pke.getPrivateKey());1061chain = pke.getCertificateChain();1062if (chain.length != chain3.length) {1063throw new SecurityException("received chain not correct length");1064}1065for (int i = 0; i < chain.length; i++) {1066if (!chain[i].equals(chain3[i])) {1067throw new SecurityException("received chain not equal");1068}1069}10701071// getKey good pke 11072PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null);1073if ("RSA".equals(pkey.getAlgorithm())) {1074System.out.println("test " + testnum++ + " passed");1075} else {1076throw new SecurityException1077("expected RSA, got " + pkey.getAlgorithm());1078}10791080// getCertificate chain chain 11081chain = ks.getCertificateChain("pk1");1082if (chain.length != chain1.length) {1083throw new SecurityException("received chain not correct length");1084}1085for (int i = 0; i < chain.length; i++) {1086if (!chain[i].equals(chain1[i])) {1087throw new SecurityException("received chain not equal");1088}1089}10901091// getKey good pke 21092pkey = (PrivateKey)ks.getKey("pk2", null);1093if ("RSA".equals(pkey.getAlgorithm())) {1094System.out.println("test " + testnum++ + " passed");1095} else {1096throw new SecurityException1097("expected RSA, got " + pkey.getAlgorithm());1098}10991100// getCertificate chain chain 21101chain = ks.getCertificateChain("pk2");1102if (chain.length != chain2.length) {1103throw new SecurityException("received chain not correct length");1104}1105for (int i = 0; i < chain.length; i++) {1106if (!chain[i].equals(chain2[i])) {1107throw new SecurityException("received chain not equal");1108}1109}11101111// getKey good pke 31112pkey = (PrivateKey)ks.getKey("pk3", null);1113if ("RSA".equals(pkey.getAlgorithm())) {1114System.out.println("test " + testnum++ + " passed");1115} else {1116throw new SecurityException1117("expected RSA, got " + pkey.getAlgorithm());1118}11191120// getCertificate chain chain 31121chain = ks.getCertificateChain("pk3");1122if (chain.length != chain3.length) {1123throw new SecurityException("received chain not correct length");1124}1125for (int i = 0; i < chain.length; i++) {1126if (!chain[i].equals(chain3[i])) {1127throw new SecurityException("received chain not equal");1128}1129}11301131// aliases1132enu = ks.aliases();1133count = 0;1134while (enu.hasMoreElements()) {1135count++;1136System.out.println("alias " +1137count +1138" = " +1139(String)enu.nextElement());1140}1141if (count == 7) {1142System.out.println("test " + testnum++ + " passed");1143} else {1144throw new SecurityException("expected 7 aliases");1145}11461147// size1148if (ks.size() == 7) {1149System.out.println("test " + testnum++ + " passed");1150} else {1151throw new SecurityException("expected size 7");1152}11531154// getCertificate good chain 11155if (ks.getCertificate("pk1").equals(chain1[0])) {1156System.out.println("test " + testnum++ + " passed");1157} else {1158throw new SecurityException("retrieved cert not equal");1159}11601161// getCertificate good chain 31162if (ks.getCertificate("pk3").equals(chain3[0])) {1163System.out.println("test " + testnum++ + " passed");1164} else {1165throw new SecurityException("retrieved cert not equal");1166}11671168// getKey good ske 11169skey = (SecretKey)ks.getKey("sk1", null);1170if ("DES".equals(skey.getAlgorithm())) {1171System.out.println("test " + testnum++ + " passed");1172} else {1173throw new SecurityException1174("expected DES, got " + skey.getAlgorithm());1175}11761177// getKey good ske 41178skey = (SecretKey)ks.getKey("sk4", null);1179if ("ARCFOUR".equals(skey.getAlgorithm())) {1180System.out.println("test " + testnum++ + " passed");1181} else {1182throw new SecurityException1183("expected ARCFOUR, got " + skey.getAlgorithm());1184}11851186// getKey good pke 11187pkey = (PrivateKey)ks.getKey("pk1", null);1188if ("RSA".equals(pkey.getAlgorithm())) {1189System.out.println("test " + testnum++ + " passed");1190} else {1191throw new SecurityException1192("expected RSA, got " + pkey.getAlgorithm());1193}11941195// getKey good pke 31196pkey = (PrivateKey)ks.getKey("pk3", null);1197if ("RSA".equals(pkey.getAlgorithm())) {1198System.out.println("test " + testnum++ + " passed");1199} else {1200throw new SecurityException1201("expected RSA, got " + pkey.getAlgorithm());1202}12031204// contains alias1205if (!ks.containsAlias("pk1") ||1206!ks.containsAlias("pk2") ||1207!ks.containsAlias("pk3") ||1208!ks.containsAlias("sk1") ||1209!ks.containsAlias("sk2") ||1210!ks.containsAlias("sk3") ||1211!ks.containsAlias("sk4")) {1212throw new SecurityException("did not contain all aliases");1213}1214System.out.println("test " + testnum++ + " passed");12151216// getCertificateAlias pk11217if (ks.getCertificateAlias(chain1[0]).equals("pk1")) {1218System.out.println("test " + testnum++ + " passed");1219} else {1220throw new SecurityException("expected cert pk1");1221}12221223// getCertificateAlias pk31224if (ks.getCertificateAlias(chain3[0]).equals("pk3")) {1225System.out.println("test " + testnum++ + " passed");1226} else {1227throw new SecurityException("expected cert pk3");1228}12291230// isCertificateEntry pk11231if (!ks.isCertificateEntry("pk1")) {1232System.out.println("test " + testnum++ + " passed");1233} else {1234throw new SecurityException("expected pke");1235}12361237// isCertificateEntry pk31238if (!ks.isCertificateEntry("pk3")) {1239System.out.println("test " + testnum++ + " passed");1240} else {1241throw new SecurityException("expected pke");1242}12431244// isCertificateEntry sk11245if (!ks.isCertificateEntry("sk1")) {1246System.out.println("test " + testnum++ + " passed");1247} else {1248throw new SecurityException("expected ske");1249}12501251// isCertificateEntry sk41252if (!ks.isCertificateEntry("sk4")) {1253System.out.println("test " + testnum++ + " passed");1254} else {1255throw new SecurityException("expected ske");1256}12571258// isKeyEntry pk11259if (ks.isKeyEntry("pk1")) {1260System.out.println("test " + testnum++ + " passed");1261} else {1262throw new SecurityException("expected pke");1263}12641265// isKeyEntry pk31266if (ks.isKeyEntry("pk3")) {1267System.out.println("test " + testnum++ + " passed");1268} else {1269throw new SecurityException("expected pke");1270}12711272// isKeyEntry sk11273if (ks.isKeyEntry("sk1")) {1274System.out.println("test " + testnum++ + " passed");1275} else {1276throw new SecurityException("expected ske");1277}12781279// isKeyEntry sk41280if (ks.isKeyEntry("sk4")) {1281System.out.println("test " + testnum++ + " passed");1282} else {1283throw new SecurityException("expected ske");1284}12851286// isCertificateEntry random foo1287if (!ks.isCertificateEntry("foo")) {1288System.out.println("test " + testnum++ + " passed");1289} else {1290throw new SecurityException("expected foo");1291}12921293// isKeyEntry random foo1294if (!ks.isKeyEntry("foo")) {1295System.out.println("test " + testnum++ + " passed");1296} else {1297throw new SecurityException("expected foo");1298}12991300// entryInstanceOf pk11301if (!ks.entryInstanceOf1302("pk1", KeyStore.TrustedCertificateEntry.class)) {1303System.out.println("test " + testnum++ + " passed");1304} else {1305throw new SecurityException("expected tce");1306}13071308// entryInstanceOf pk31309if (!ks.entryInstanceOf1310("pk3", KeyStore.TrustedCertificateEntry.class)) {1311System.out.println("test " + testnum++ + " passed");1312} else {1313throw new SecurityException("expected tce");1314}13151316// entryInstanceOf sk11317if (!ks.entryInstanceOf1318("sk1", KeyStore.TrustedCertificateEntry.class)) {1319System.out.println("test " + testnum++ + " passed");1320} else {1321throw new SecurityException("expected tce");1322}13231324// entryInstanceOf sk41325if (!ks.entryInstanceOf1326("sk4", KeyStore.TrustedCertificateEntry.class)) {1327System.out.println("test " + testnum++ + " passed");1328} else {1329throw new SecurityException("expected tce");1330}13311332// entryInstanceOf pk11333if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class)) {1334System.out.println("test " + testnum++ + " passed");1335} else {1336throw new SecurityException("expected pke");1337}13381339// entryInstanceOf pk31340if (ks.entryInstanceOf("pk3", KeyStore.PrivateKeyEntry.class)) {1341System.out.println("test " + testnum++ + " passed");1342} else {1343throw new SecurityException("expected pke");1344}13451346// entryInstanceOf sk11347if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) {1348System.out.println("test " + testnum++ + " passed");1349} else {1350throw new SecurityException("expected pke");1351}13521353// entryInstanceOf sk41354if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) {1355System.out.println("test " + testnum++ + " passed");1356} else {1357throw new SecurityException("expected pke");1358}13591360// entryInstanceOf sk11361if (ks.entryInstanceOf("sk1", KeyStore.SecretKeyEntry.class)) {1362System.out.println("test " + testnum++ + " passed");1363} else {1364throw new SecurityException("expected ske");1365}13661367// entryInstanceOf sk41368if (ks.entryInstanceOf("sk4", KeyStore.SecretKeyEntry.class)) {1369System.out.println("test " + testnum++ + " passed");1370} else {1371throw new SecurityException("expected ske");1372}13731374// entryInstanceOf pk11375if (!ks.entryInstanceOf("pk1", KeyStore.SecretKeyEntry.class)) {1376System.out.println("test " + testnum++ + " passed");1377} else {1378throw new SecurityException("expected ske");1379}13801381// entryInstanceOf pk31382if (!ks.entryInstanceOf("pk3", KeyStore.SecretKeyEntry.class)) {1383System.out.println("test " + testnum++ + " passed");1384} else {1385throw new SecurityException("expected ske");1386}13871388// getEntry random foobar1389if (ks.getEntry("foobar", null) != null) {1390throw new SecurityException("expected null entry");1391} else {1392System.out.println("test " + testnum++ + " passed");1393}13941395// deleteEntry1396ks.deleteEntry("pk1");1397ks.deleteEntry("pk3");1398ks.deleteEntry("sk2");1399ks.deleteEntry("sk3");1400System.out.println("test " + testnum++ + " passed");14011402// aliases1403enu = ks.aliases();1404count = 0;1405while (enu.hasMoreElements()) {1406count++;1407System.out.println("alias " +1408count +1409" = " +1410(String)enu.nextElement());1411}1412if (count == 3) {1413System.out.println("test " + testnum++ + " passed");1414} else {1415throw new SecurityException("expected 3 aliases");1416}14171418// size1419if (ks.size() == 3) {1420System.out.println("test " + testnum++ + " passed");1421} else {1422throw new SecurityException("expected size 6");1423}14241425// entryInstanceOf sk11426if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) {1427System.out.println("test " + testnum++ + " passed");1428} else {1429throw new SecurityException("expected pke");1430}14311432// entryInstanceOf sk41433if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) {1434System.out.println("test " + testnum++ + " passed");1435} else {1436throw new SecurityException("expected pke");1437}14381439// entryInstanceOf pk21440if (ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class)) {1441System.out.println("test " + testnum++ + " passed");1442} else {1443throw new SecurityException("expected pke");1444}1445System.out.println("test " + testnum++ + " passed");14461447return testnum;1448}1449}145014511452