Path: blob/master/test/jdk/sun/security/pkcs11/KeyStore/Basic.java
41152 views
/*1* Copyright (c) 2003, 2021, 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/* @test24* @bug 493818525* @summary KeyStore support for NSS cert/key databases26* To run manually:27* set environment variable:28* <token> [activcard|ibutton|nss|sca1000]29* <command> [list|basic]30*31* Note:32* . 'list' lists the token aliases33* . 'basic' does not run with activcard,34* @library /test/lib ..35* @run testng/othervm Basic36*/3738import java.io.*;39import java.nio.file.Path;40import java.util.*;4142import java.security.KeyStore;43import java.security.KeyStoreException;44import java.security.KeyFactory;45import java.security.PrivateKey;46import java.security.Provider;47import java.security.Signature;48import java.security.Security;4950import java.security.cert.*;51import java.security.spec.*;52import java.security.interfaces.*;5354import javax.crypto.SecretKey;5556import javax.security.auth.Subject;5758import com.sun.security.auth.module.*;59import com.sun.security.auth.callback.*;60import org.testng.annotations.BeforeClass;61import org.testng.annotations.Test;626364public class Basic extends PKCS11Test {6566private static final Path TEST_DATA_PATH = Path.of(BASE)67.resolve("BasicData");68private static final String DIR = TEST_DATA_PATH.toString();69private static char[] tokenPwd;70private static final char[] ibuttonPwd =71new char[0];72private static final char[] activcardPwd =73new char[] { '1', '1', '2', '2', '3', '3' };74private static final char[] nssPwd =75new char[] { 't', 'e', 's', 't', '1', '2' };76private static final char[] solarisPwd =77new char[] { 'p', 'i', 'n' };78private static final char[] sca1000Pwd =79new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };80private static final char[] sPwd = { 'f', 'o', 'o' };8182private static SecretKey sk1;83private static SecretKey sk2;84private static SecretKey sk3;85private static SecretKey sk4;8687private static RSAPrivateCrtKey pk1;88private static PrivateKey pk2;89private static PrivateKey pk3;9091private static Certificate[] chain1;92private static Certificate[] chain2;93private static Certificate[] chain3;94private static Certificate[] chain4;9596private static X509Certificate randomCert;9798private static KeyStore ks;99private static final String KS_TYPE = "PKCS11";100private static Provider provider;101102@BeforeClass103public void setUp() throws Exception {104copyNssCertKeyToClassesDir();105setCommonSystemProps();106System.setProperty("CUSTOM_P11_CONFIG",107TEST_DATA_PATH.resolve("p11-nss.txt").toString());108System.setProperty("TOKEN", "nss");109System.setProperty("TEST", "basic");110}111112@Test113public void testBasic() throws Exception {114String[] args = {"sm", "Basic.policy"};115main(new Basic(), args);116}117118private static class FooEntry implements KeyStore.Entry { }119120private static class P11SecretKey implements SecretKey {121String alg;122int length;123public P11SecretKey(String alg, int length) {124this.alg = alg;125this.length = length;126}127public String getAlgorithm() { return alg; }128public String getFormat() { return "raw"; }129public byte[] getEncoded() { return new byte[length/8]; }130}131132public void main(Provider p) throws Exception {133134this.provider = p;135136// get private keys137KeyFactory kf = KeyFactory.getInstance("RSA");138KeyFactory dsaKf = KeyFactory.getInstance("DSA", "SUN");139140ObjectInputStream ois1 = new ObjectInputStream141(new FileInputStream(new File(DIR, "pk1.key")));142byte[] keyBytes = (byte[])ois1.readObject();143ois1.close();144PrivateKey tmpKey =145kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));146pk1 = (RSAPrivateCrtKey)tmpKey;147148ObjectInputStream ois2 = new ObjectInputStream149(new FileInputStream(new File(DIR, "pk2.key")));150keyBytes = (byte[])ois2.readObject();151ois2.close();152pk2 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));153154ObjectInputStream ois3 = new ObjectInputStream155(new FileInputStream(new File(DIR, "pk3.key")));156keyBytes = (byte[])ois3.readObject();157pk3 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));158ois3.close();159160// get cert chains for private keys161CertificateFactory cf = CertificateFactory.getInstance("X.509", "SUN");162Certificate caCert = cf.generateCertificate163(new FileInputStream(new File(DIR, "ca.cert")));164Certificate ca2Cert = cf.generateCertificate165(new FileInputStream(new File(DIR, "ca2.cert")));166Certificate pk1cert = cf.generateCertificate167(new FileInputStream(new File(DIR, "pk1.cert")));168Certificate pk1cert2 = cf.generateCertificate169(new FileInputStream(new File(DIR, "pk1.cert2")));170Certificate pk2cert = cf.generateCertificate171(new FileInputStream(new File(DIR, "pk2.cert")));172Certificate pk3cert = cf.generateCertificate173(new FileInputStream(new File(DIR, "pk3.cert")));174chain1 = new Certificate[] { pk1cert, caCert };175chain2 = new Certificate[] { pk2cert, caCert };176chain3 = new Certificate[] { pk3cert, caCert };177chain4 = new Certificate[] { pk1cert2, ca2Cert };178179// create secret keys180sk1 = new P11SecretKey("DES", 64);181sk2 = new P11SecretKey("DESede", 192);182sk3 = new P11SecretKey("AES", 128);183sk4 = new P11SecretKey("RC4", 128);184185// read randomCert186randomCert = (X509Certificate)cf.generateCertificate187(new FileInputStream(new File(DIR, "random.cert")));188189doTest();190}191192private static void doTest() throws Exception {193194String token = System.getProperty("TOKEN");195String test = System.getProperty("TEST");196197if (token == null || token.length() == 0) {198throw new Exception("token arg required");199}200if (test == null || test.length() == 0) {201throw new Exception("test arg required");202}203204if ("ibutton".equals(token)) {205tokenPwd = ibuttonPwd;206} else if ("activcard".equals(token)) {207tokenPwd = activcardPwd;208} else if ("nss".equals(token)) {209tokenPwd = nssPwd;210} else if ("sca1000".equals(token)) {211tokenPwd = sca1000Pwd;212} else if ("solaris".equals(token)) {213tokenPwd = solarisPwd;214}215216if ("list".equals(test)) {217Basic.list();218} else if ("basic".equals(test)) {219220int testnum = 1;221222if ("ibutton".equals(token)) {223// pkey and setAttribute224testnum = Basic.pkey(testnum);225testnum = Basic.setAttribute(testnum);226} else if ("activcard".equals(token)) {227// sign228testnum = Basic.signAlias(testnum, null);229} else if ("nss".equals(token)) {230// setAttribute, pkey, sign231testnum = Basic.setAttribute(testnum);232testnum = Basic.pkey(testnum);233testnum = Basic.sign(testnum);234testnum = Basic.copy(testnum);235} else if ("solaris".equals(token)) {236testnum = Basic.setAttribute(testnum);237testnum = Basic.pkey(testnum);238testnum = Basic.sign(testnum);239testnum = Basic.skey(testnum);240testnum = Basic.copy(testnum);241} else if ("sca1000".equals(token)) {242// setAttribute, pkey, sign, skey, copy243testnum = Basic.setAttribute(testnum);244testnum = Basic.pkey(testnum);245testnum = Basic.sign(testnum);246testnum = Basic.skey(testnum);247testnum = Basic.copy(testnum);248}249250} else if ("pkey".equals(test)) {251Basic.pkey(1);252} else if ("skey".equals(test)) {253Basic.skey(1);254} else if ("setAttribute".equals(test)) {255Basic.setAttribute(1);256} else if ("copy".equals(test)) {257Basic.copy(1);258} else if ("sign".equals(test)) {259Basic.sign(1);260} else if ("module".equals(test)) {261Basic.module();262} else if ("nss-extended".equals(test)) {263264// this only works if NSS_TEST is set to true in P11KeyStore.java265266int testnum = 1;267testnum = Basic.setAttribute(testnum);268testnum = Basic.pkey(testnum);269testnum = Basic.sign(testnum);270testnum = Basic.extended(testnum);271} else {272System.out.println("unrecognized command");273}274}275276private static int sign(int testnum) throws Exception {277if (ks == null) {278ks = KeyStore.getInstance(KS_TYPE, provider);279ks.load(null, tokenPwd);280}281if (!ks.containsAlias("pk1")) {282ks.setKeyEntry("pk1", pk1, null, chain1);283}284System.out.println("test " + testnum++ + " passed");285286return signAlias(testnum, "pk1");287}288289private static int signAlias(int testnum, String alias) throws Exception {290291if (ks == null) {292ks = KeyStore.getInstance(KS_TYPE, provider);293ks.load(null, tokenPwd);294}295296if (alias == null) {297Enumeration enu = ks.aliases();298if (enu.hasMoreElements()) {299alias = (String)enu.nextElement();300}301}302303PrivateKey pkey = (PrivateKey)ks.getKey(alias, null);304if ("RSA".equals(pkey.getAlgorithm())) {305System.out.println("got [" + alias + "] signing key: " + pkey);306} else {307throw new SecurityException308("expected RSA, got " + pkey.getAlgorithm());309}310311Signature s = Signature.getInstance("MD5WithRSA", ks.getProvider());312s.initSign(pkey);313System.out.println("initialized signature object with key");314s.update("hello".getBytes());315System.out.println("signature object updated with [hello] bytes");316317byte[] signed = s.sign();318System.out.println("received signature " + signed.length +319" bytes in length");320321Signature v = Signature.getInstance("MD5WithRSA", ks.getProvider());322v.initVerify(ks.getCertificate(alias));323v.update("hello".getBytes());324v.verify(signed);325System.out.println("signature verified");326System.out.println("test " + testnum++ + " passed");327328return testnum;329}330331private static int copy(int testnum) throws Exception {332333if (ks == null) {334ks = KeyStore.getInstance(KS_TYPE, provider);335ks.load(null, tokenPwd);336}337338KeyFactory kf = KeyFactory.getInstance("RSA", provider);339PrivateKey pkSession = (PrivateKey)kf.translateKey(pk3);340System.out.println("pkSession = " + pkSession);341ks.setKeyEntry("pkSession", pkSession, null, chain3);342343KeyStore.PrivateKeyEntry pke =344(KeyStore.PrivateKeyEntry)ks.getEntry("pkSession", null);345System.out.println("pkSession = " + pke.getPrivateKey());346Certificate[] chain = pke.getCertificateChain();347if (chain.length != chain3.length) {348throw new SecurityException("received chain not correct length");349}350for (int i = 0; i < chain.length; i++) {351if (!chain[i].equals(chain3[i])) {352throw new SecurityException("received chain not equal");353}354}355356System.out.println("test " + testnum++ + " passed");357358return testnum;359}360361private static void list() throws Exception {362int testnum = 1;363364ks = KeyStore.getInstance(KS_TYPE, provider);365366// check instance367if (ks.getProvider() instanceof java.security.AuthProvider) {368System.out.println("keystore provider instance of AuthProvider");369System.out.println("test " + testnum++ + " passed");370} else {371throw new SecurityException("did not get AuthProvider KeyStore");372}373374// load375ks.load(null, tokenPwd);376System.out.println("test " + testnum++ + " passed");377378// aliases379Enumeration enu = ks.aliases();380int count = 0;381while (enu.hasMoreElements()) {382count++;383System.out.println("alias " +384count +385" = " +386(String)enu.nextElement());387}388}389390private static void module() throws Exception {391392// perform Security.addProvider of P11 provider393Security.addProvider(getSunPKCS11(System.getProperty("CUSTOM_P11_CONFIG")));394395String KS_PROVIDER = "SunPKCS11-" + System.getProperty("TOKEN");396397KeyStoreLoginModule m = new KeyStoreLoginModule();398Subject s = new Subject();399Map<String, String> options = new HashMap<>();400options.put("keyStoreURL", "NONE");401options.put("keyStoreType", KS_TYPE);402options.put("keyStoreProvider", KS_PROVIDER);403options.put("debug", "true");404m.initialize(s, new TextCallbackHandler(), new HashMap<>(), options);405m.login();406m.commit();407System.out.println("authenticated subject = " + s);408m.logout();409System.out.println("authenticated subject = " + s);410}411412/**413* SCA1000 does not handle extended secret key tests414* . Blowfish (CKR_TEMPLATE_INCOMPLETE)415* . AES (CKR_TEMPLATE_INCOMPLETE)416* . RC4 (CKR_ATTRIBUTE_TYPE_INVALID)417* so do this instead418*/419private static int skey(int testnum) throws Exception {420if (ks == null) {421ks = KeyStore.getInstance(KS_TYPE, provider);422ks.load(null, tokenPwd);423}424425// delete all old aliases426Enumeration enu = ks.aliases();427int count = 0;428while (enu.hasMoreElements()) {429String next = (String)enu.nextElement();430ks.deleteEntry(next);431System.out.println("deleted entry for: " + next);432}433434// set good ske 1435ks.setKeyEntry("sk1", sk1, null, null);436System.out.println("test " + testnum++ + " passed");437438// set good ske 2439ks.setKeyEntry("sk2", sk2, null, null);440System.out.println("test " + testnum++ + " passed");441442// getEntry good ske 1443KeyStore.SecretKeyEntry ske =444(KeyStore.SecretKeyEntry)ks.getEntry("sk1", null);445if ("DES".equals(ske.getSecretKey().getAlgorithm())) {446System.out.println("test " + testnum++ + " passed");447} else {448throw new SecurityException449("expected DES, got " + ske.getSecretKey().getAlgorithm());450}451452// getEntry good ske 2453ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null);454if ("DESede".equals(ske.getSecretKey().getAlgorithm())) {455System.out.println("test " + testnum++ + " passed");456} else {457throw new SecurityException458("expected DESede, got " + ske.getSecretKey().getAlgorithm());459}460461// getKey good ske 1462SecretKey skey = (SecretKey)ks.getKey("sk1", null);463if ("DES".equals(skey.getAlgorithm())) {464System.out.println("test " + testnum++ + " passed");465} else {466throw new SecurityException467("expected DES, got " + skey.getAlgorithm());468}469470// getKey good ske 2471skey = (SecretKey)ks.getKey("sk2", null);472if ("DESede".equals(skey.getAlgorithm())) {473System.out.println("test " + testnum++ + " passed");474} else {475throw new SecurityException476("expected DESede, got " + skey.getAlgorithm());477}478479// aliases480enu = ks.aliases();481count = 0;482while (enu.hasMoreElements()) {483count++;484System.out.println("alias " +485count +486" = " +487(String)enu.nextElement());488}489if (count == 2) {490System.out.println("test " + testnum++ + " passed");491} else {492throw new SecurityException("expected 2 aliases");493}494495// size496if (ks.size() == 2) {497System.out.println("test " + testnum++ + " passed");498} else {499throw new SecurityException("expected size 2");500}501502// isCertificateEntry sk1503if (!ks.isCertificateEntry("sk1")) {504System.out.println("test " + testnum++ + " passed");505} else {506throw new SecurityException("expected ske");507}508509// isKeyEntry sk1510if (ks.isKeyEntry("sk1")) {511System.out.println("test " + testnum++ + " passed");512} else {513throw new SecurityException("expected ske");514}515516// entryInstanceOf sk2517if (ks.entryInstanceOf("sk2", KeyStore.SecretKeyEntry.class)) {518System.out.println("test " + testnum++ + " passed");519} else {520throw new SecurityException("expected ske");521}522523return testnum;524}525526private static int setAttribute(int testnum) throws Exception {527528if (ks == null) {529ks = KeyStore.getInstance(KS_TYPE, provider);530ks.load(null, tokenPwd);531}532533if (!ks.containsAlias("pk1")) {534// set good pke 1535ks.setKeyEntry("pk1", pk1, null, chain1);536System.out.println("test " + testnum++ + " passed");537}538539// delete all old aliases except pk1540Enumeration enu = ks.aliases();541int count = 0;542while (enu.hasMoreElements()) {543String next = (String)enu.nextElement();544if (!"pk1".equals(next)) {545ks.deleteEntry(next);546System.out.println("deleted entry for: " + next);547}548}549550KeyStore.PrivateKeyEntry pke =551(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);552System.out.println("pk1 = " + pke.getPrivateKey());553Certificate[] chain = pke.getCertificateChain();554if (chain.length != chain1.length) {555throw new SecurityException("received chain not correct length");556}557for (int i = 0; i < chain.length; i++) {558if (!chain[i].equals(chain1[i])) {559throw new SecurityException("received chain not equal");560}561}562System.out.println("test " + testnum++ + " passed");563564/**565* test change alias only566*/567568// test C_SetAttribute569PrivateKey pkey = pke.getPrivateKey();570ks.setEntry("pk1SA",571new KeyStore.PrivateKeyEntry(pkey, chain1),572null);573System.out.println("test " + testnum++ + " passed");574575// aliases576enu = ks.aliases();577count = 0;578String newAlias = null;579while (enu.hasMoreElements()) {580count++;581newAlias = (String)enu.nextElement();582System.out.println("alias " +583count +584" = " +585newAlias);586}587if (count == 1 && "pk1SA".equals(newAlias)) {588System.out.println("test " + testnum++ + " passed");589} else {590throw new SecurityException("expected 1 alias");591}592593// size594if (ks.size() == 1) {595System.out.println("test " + testnum++ + " passed");596} else {597throw new SecurityException("expected size 1");598}599600pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);601if (pke != null) {602throw new SecurityException("expected not to find pk1");603}604System.out.println("test " + testnum++ + " passed");605606pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null);607System.out.println("pk1SA = " + pke.getPrivateKey());608chain = pke.getCertificateChain();609if (chain.length != chain1.length) {610throw new SecurityException("received chain not correct length");611}612for (int i = 0; i < chain.length; i++) {613if (!chain[i].equals(chain1[i])) {614throw new SecurityException("received chain not equal");615}616}617System.out.println("test " + testnum++ + " passed");618619/**620* test change cert chain621*/622623pkey = pke.getPrivateKey();624ks.setEntry("pk1SA-2",625new KeyStore.PrivateKeyEntry(pkey, chain4),626null);627System.out.println("test " + testnum++ + " passed");628629// aliases630enu = ks.aliases();631count = 0;632newAlias = null;633while (enu.hasMoreElements()) {634count++;635newAlias = (String)enu.nextElement();636System.out.println("alias " +637count +638" = " +639newAlias);640}641if (count == 1 && "pk1SA-2".equals(newAlias)) {642System.out.println("test " + testnum++ + " passed");643} else {644throw new SecurityException("expected 1 alias");645}646647// size648if (ks.size() == 1) {649System.out.println("test " + testnum++ + " passed");650} else {651throw new SecurityException("expected size 1");652}653654pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null);655if (pke != null) {656throw new SecurityException("expected not to find pk1SA");657}658System.out.println("test " + testnum++ + " passed");659660pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA-2", null);661System.out.println("pk1SA-2 = " + pke.getPrivateKey());662chain = pke.getCertificateChain();663if (chain.length != chain4.length) {664throw new SecurityException("received chain not correct length");665}666for (int i = 0; i < chain.length; i++) {667if (!chain[i].equals(chain4[i])) {668throw new SecurityException("received chain not equal");669}670}671System.out.println("test " + testnum++ + " passed");672673return testnum;674}675676private static int pkey(int testnum) throws Exception {677678if (ks == null) {679ks = KeyStore.getInstance(KS_TYPE, provider);680ks.load(null, tokenPwd);681System.out.println("test " + testnum++ + " passed");682}683684// check instance685if (ks.getProvider() instanceof java.security.AuthProvider) {686System.out.println("keystore provider instance of AuthProvider");687System.out.println("test " + testnum++ + " passed");688} else {689throw new SecurityException("did not get AuthProvider KeyStore");690}691692// delete all old aliases693Enumeration enu = ks.aliases();694int count = 0;695while (enu.hasMoreElements()) {696String next = (String)enu.nextElement();697ks.deleteEntry(next);698System.out.println("deleted entry for: " + next);699}700701// set good pke 1702ks.setKeyEntry("pk1", pk1, null, chain1);703System.out.println("test " + testnum++ + " passed");704705// set good pke 2706ks.setEntry("pk2",707new KeyStore.PrivateKeyEntry(pk2, chain2),708null);709System.out.println("test " + testnum++ + " passed");710711// getEntry good pke 1712KeyStore.PrivateKeyEntry pke =713(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);714System.out.println("pk1 = " + pke.getPrivateKey());715Certificate[] chain = pke.getCertificateChain();716if (chain.length != chain1.length) {717throw new SecurityException("received chain not correct length");718}719for (int i = 0; i < chain.length; i++) {720if (!chain[i].equals(chain1[i])) {721throw new SecurityException("received chain not equal");722}723}724725// getKey good pke 1726PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null);727System.out.println("pk1 = " + pkey);728if ("RSA".equals(pkey.getAlgorithm())) {729System.out.println("test " + testnum++ + " passed");730} else {731throw new SecurityException732("expected RSA, got " + pkey.getAlgorithm());733}734735// getCertificate chain chain 1736chain = ks.getCertificateChain("pk1");737if (chain.length != chain1.length) {738throw new SecurityException("received chain not correct length");739}740for (int i = 0; i < chain.length; i++) {741if (!chain[i].equals(chain1[i])) {742throw new SecurityException("received chain not equal");743}744}745System.out.println("test " + testnum++ + " passed");746747// getEntry good pke 2748pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null);749if ("RSA".equals(pke.getPrivateKey().getAlgorithm())) {750System.out.println("test " + testnum++ + " passed");751} else {752throw new SecurityException753("expected RSA, got " + pke.getPrivateKey().getAlgorithm());754}755System.out.println("pk2 = " + pke.getPrivateKey());756chain = pke.getCertificateChain();757if (chain.length != chain2.length) {758throw new SecurityException("received chain not correct length");759}760for (int i = 0; i < chain.length; i++) {761if (!chain[i].equals(chain2[i])) {762throw new SecurityException("received chain not equal");763}764}765766// getKey good pke 2767pkey = (PrivateKey)ks.getKey("pk2", null);768if ("RSA".equals(pkey.getAlgorithm())) {769System.out.println("test " + testnum++ + " passed");770} else {771throw new SecurityException772("expected RSA, got " + pkey.getAlgorithm());773}774775// getCertificate chain chain 2776chain = ks.getCertificateChain("pk2");777if (chain.length != chain2.length) {778throw new SecurityException("received chain not correct length");779}780for (int i = 0; i < chain.length; i++) {781if (!chain[i].equals(chain2[i])) {782throw new SecurityException("received chain not equal");783}784}785System.out.println("test " + testnum++ + " passed");786787// aliases788enu = ks.aliases();789count = 0;790while (enu.hasMoreElements()) {791count++;792System.out.println("alias " +793count +794" = " +795(String)enu.nextElement());796}797if (count == 2) {798System.out.println("test " + testnum++ + " passed");799} else {800throw new SecurityException("expected 2 aliases");801}802803// size804if (ks.size() == 2) {805System.out.println("test " + testnum++ + " passed");806} else {807throw new SecurityException("expected size 2");808}809810// getCertificate811if (ks.getCertificate("pk1").equals(chain1[0])) {812System.out.println("test " + testnum++ + " passed");813} else {814throw new SecurityException("expected certificate pk1 end entity");815}816817// containsAlias818if (ks.containsAlias("pk1") && ks.containsAlias("pk2") &&819!ks.containsAlias("foobar") &&820!ks.containsAlias("pk1.2") && !ks.containsAlias("pk2.2")) {821System.out.println("test " + testnum++ + " passed");822} else {823throw new SecurityException("unexpected aliases encountered");824}825826// isKeyEntry827if (ks.isKeyEntry("pk1") && ks.isKeyEntry("pk2") &&828!ks.isKeyEntry("foobar")) {829System.out.println("test " + testnum++ + " passed");830} else {831throw new SecurityException("isKeyEntry failed");832}833834// isCertificateEntry835if (!ks.isCertificateEntry("foobar") &&836!ks.isCertificateEntry("pk1") && !ks.isCertificateEntry("pk2")) {837System.out.println("test " + testnum++ + " passed");838} else {839throw new SecurityException("isCertificateEntry failed");840}841842// getCertificateAlias843if (ks.getCertificateAlias(chain1[0]).equals("pk1") &&844ks.getCertificateAlias(chain2[0]).equals("pk2") &&845ks.getCertificateAlias(randomCert) == null) {846System.out.println("test " + testnum++ + " passed");847} else {848throw new SecurityException("getCertificateAlias failed");849}850851if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class) &&852ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class) &&853!ks.entryInstanceOf("pk1", KeyStore.TrustedCertificateEntry.class) &&854!ks.entryInstanceOf("pk2", KeyStore.TrustedCertificateEntry.class) &&855!ks.entryInstanceOf("foobar", KeyStore.TrustedCertificateEntry.class) &&856!ks.entryInstanceOf("foobar", KeyStore.PrivateKeyEntry.class)) {857System.out.println("test " + testnum++ + " passed");858} else {859throw new SecurityException("entryInstanceOf failed");860}861862ks.deleteEntry("pk2");863if (ks.containsAlias("pk1") && !ks.containsAlias("pk2")) {864System.out.println("test " + testnum++ + " passed");865} else {866throw new SecurityException("deleteEntry failed");867}868869// getEntry good pke 1870pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);871System.out.println("pk1 = " + pke.getPrivateKey());872chain = pke.getCertificateChain();873if (chain.length != chain1.length) {874throw new SecurityException("received chain not correct length");875}876for (int i = 0; i < chain.length; i++) {877if (!chain[i].equals(chain1[i])) {878throw new SecurityException("received chain not equal");879}880}881System.out.println("test " + testnum++ + " passed");882883// aliases884enu = ks.aliases();885count = 0;886while (enu.hasMoreElements()) {887count++;888System.out.println("alias " +889count +890" = " +891(String)enu.nextElement());892}893if (count == 1) {894System.out.println("test " + testnum++ + " passed");895} else {896throw new SecurityException("expected 1 alias");897}898899// size900if (ks.size() == 1) {901System.out.println("test " + testnum++ + " passed");902} else {903throw new SecurityException("expected size 1");904}905906return testnum;907}908909private static int extended(int testnum) throws Exception {910911// setEntry unknown entry type912try {913ks.setEntry("foo", new FooEntry(), null);914throw new SecurityException("setEntry should have failed");915} catch (KeyStoreException kse) {916System.out.println("test " + testnum++ + " passed");917}918919// getEntry random foo920if (ks.getEntry("foo", null) != null) {921throw new SecurityException("expected null entry");922} else {923System.out.println("test " + testnum++ + " passed");924}925926// set good ske 1927ks.setKeyEntry("sk1", sk1, null, null);928System.out.println("test " + testnum++ + " passed");929930// set good ske 2931ks.setKeyEntry("sk2", sk2, null, null);932System.out.println("test " + testnum++ + " passed");933934// set good ske 3935ks.setEntry("sk3",936new KeyStore.SecretKeyEntry(sk3),937null);938System.out.println("test " + testnum++ + " passed");939940// set good ske 4941ks.setEntry("sk4",942new KeyStore.SecretKeyEntry(sk4),943null);944System.out.println("test " + testnum++ + " passed");945946// getEntry good ske 1947KeyStore.SecretKeyEntry ske =948(KeyStore.SecretKeyEntry)ks.getEntry("sk1", null);949if ("DES".equals(ske.getSecretKey().getAlgorithm())) {950System.out.println("test " + testnum++ + " passed");951} else {952throw new SecurityException953("expected DES, got " + ske.getSecretKey().getAlgorithm());954}955956// getEntry good ske 2957ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null);958if ("DESede".equals(ske.getSecretKey().getAlgorithm())) {959System.out.println("test " + testnum++ + " passed");960} else {961throw new SecurityException962("expected DESede, got " + ske.getSecretKey().getAlgorithm());963}964965// getEntry good ske 3966ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk3", null);967if ("AES".equals(ske.getSecretKey().getAlgorithm())) {968System.out.println("test " + testnum++ + " passed");969} else {970throw new SecurityException971("expected AES, got " + ske.getSecretKey().getAlgorithm());972}973974// getEntry good ske 4975ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk4", null);976if ("ARCFOUR".equals(ske.getSecretKey().getAlgorithm())) {977System.out.println("test " + testnum++ + " passed");978} else {979throw new SecurityException980("expected ARCFOUR, got " + ske.getSecretKey().getAlgorithm());981}982983// getKey good ske 1984SecretKey skey = (SecretKey)ks.getKey("sk1", null);985if ("DES".equals(skey.getAlgorithm())) {986System.out.println("test " + testnum++ + " passed");987} else {988throw new SecurityException989("expected DES, got " + skey.getAlgorithm());990}991992// getKey good ske 2993skey = (SecretKey)ks.getKey("sk2", null);994if ("DESede".equals(skey.getAlgorithm())) {995System.out.println("test " + testnum++ + " passed");996} else {997throw new SecurityException998("expected DESede, got " + skey.getAlgorithm());999}10001001// getKey good ske 31002skey = (SecretKey)ks.getKey("sk3", null);1003if ("AES".equals(skey.getAlgorithm())) {1004System.out.println("test " + testnum++ + " passed");1005} else {1006throw new SecurityException1007("expected AES, got " + skey.getAlgorithm());1008}10091010// getKey good ske 41011skey = (SecretKey)ks.getKey("sk4", null);1012if ("ARCFOUR".equals(skey.getAlgorithm())) {1013System.out.println("test " + testnum++ + " passed");1014} else {1015throw new SecurityException1016("expected ARCFOUR, got " + skey.getAlgorithm());1017}10181019// aliases1020Enumeration enu = ks.aliases();1021int count = 0;1022while (enu.hasMoreElements()) {1023count++;1024System.out.println("alias " +1025count +1026" = " +1027(String)enu.nextElement());1028}1029if (count == 5) {1030System.out.println("test " + testnum++ + " passed");1031} else {1032throw new SecurityException("expected 5 aliases");1033}10341035// size1036if (ks.size() == 5) {1037System.out.println("test " + testnum++ + " passed");1038} else {1039throw new SecurityException("expected size 5");1040}10411042// set good pke 21043ks.setEntry("pk2",1044new KeyStore.PrivateKeyEntry(pk2, chain2),1045null);1046System.out.println("test " + testnum++ + " passed");10471048// set good pke 31049ks.setEntry("pk3",1050new KeyStore.PrivateKeyEntry(pk3, chain3),1051null);1052System.out.println("test " + testnum++ + " passed");10531054// getEntry good pke 11055KeyStore.PrivateKeyEntry pke =1056(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);1057System.out.println("pk1 = " + pke.getPrivateKey());1058Certificate[] chain = pke.getCertificateChain();1059if (chain.length != chain1.length) {1060throw new SecurityException("received chain not correct length");1061}1062for (int i = 0; i < chain.length; i++) {1063if (!chain[i].equals(chain1[i])) {1064throw new SecurityException("received chain not equal");1065}1066}10671068// getEntry good pke 21069pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null);1070System.out.println("pk2 = " + pke.getPrivateKey());1071chain = pke.getCertificateChain();1072if (chain.length != chain2.length) {1073throw new SecurityException("received chain not correct length");1074}1075for (int i = 0; i < chain.length; i++) {1076if (!chain[i].equals(chain2[i])) {1077throw new SecurityException("received chain not equal");1078}1079}10801081// getEntry good pke 31082pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk3", null);1083System.out.println("pk3 = " + pke.getPrivateKey());1084chain = pke.getCertificateChain();1085if (chain.length != chain3.length) {1086throw new SecurityException("received chain not correct length");1087}1088for (int i = 0; i < chain.length; i++) {1089if (!chain[i].equals(chain3[i])) {1090throw new SecurityException("received chain not equal");1091}1092}10931094// getKey good pke 11095PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null);1096if ("RSA".equals(pkey.getAlgorithm())) {1097System.out.println("test " + testnum++ + " passed");1098} else {1099throw new SecurityException1100("expected RSA, got " + pkey.getAlgorithm());1101}11021103// getCertificate chain chain 11104chain = ks.getCertificateChain("pk1");1105if (chain.length != chain1.length) {1106throw new SecurityException("received chain not correct length");1107}1108for (int i = 0; i < chain.length; i++) {1109if (!chain[i].equals(chain1[i])) {1110throw new SecurityException("received chain not equal");1111}1112}11131114// getKey good pke 21115pkey = (PrivateKey)ks.getKey("pk2", null);1116if ("RSA".equals(pkey.getAlgorithm())) {1117System.out.println("test " + testnum++ + " passed");1118} else {1119throw new SecurityException1120("expected RSA, got " + pkey.getAlgorithm());1121}11221123// getCertificate chain chain 21124chain = ks.getCertificateChain("pk2");1125if (chain.length != chain2.length) {1126throw new SecurityException("received chain not correct length");1127}1128for (int i = 0; i < chain.length; i++) {1129if (!chain[i].equals(chain2[i])) {1130throw new SecurityException("received chain not equal");1131}1132}11331134// getKey good pke 31135pkey = (PrivateKey)ks.getKey("pk3", null);1136if ("RSA".equals(pkey.getAlgorithm())) {1137System.out.println("test " + testnum++ + " passed");1138} else {1139throw new SecurityException1140("expected RSA, got " + pkey.getAlgorithm());1141}11421143// getCertificate chain chain 31144chain = ks.getCertificateChain("pk3");1145if (chain.length != chain3.length) {1146throw new SecurityException("received chain not correct length");1147}1148for (int i = 0; i < chain.length; i++) {1149if (!chain[i].equals(chain3[i])) {1150throw new SecurityException("received chain not equal");1151}1152}11531154// aliases1155enu = ks.aliases();1156count = 0;1157while (enu.hasMoreElements()) {1158count++;1159System.out.println("alias " +1160count +1161" = " +1162(String)enu.nextElement());1163}1164if (count == 7) {1165System.out.println("test " + testnum++ + " passed");1166} else {1167throw new SecurityException("expected 7 aliases");1168}11691170// size1171if (ks.size() == 7) {1172System.out.println("test " + testnum++ + " passed");1173} else {1174throw new SecurityException("expected size 7");1175}11761177// getCertificate good chain 11178if (ks.getCertificate("pk1").equals(chain1[0])) {1179System.out.println("test " + testnum++ + " passed");1180} else {1181throw new SecurityException("retrieved cert not equal");1182}11831184// getCertificate good chain 31185if (ks.getCertificate("pk3").equals(chain3[0])) {1186System.out.println("test " + testnum++ + " passed");1187} else {1188throw new SecurityException("retrieved cert not equal");1189}11901191// getKey good ske 11192skey = (SecretKey)ks.getKey("sk1", null);1193if ("DES".equals(skey.getAlgorithm())) {1194System.out.println("test " + testnum++ + " passed");1195} else {1196throw new SecurityException1197("expected DES, got " + skey.getAlgorithm());1198}11991200// getKey good ske 41201skey = (SecretKey)ks.getKey("sk4", null);1202if ("ARCFOUR".equals(skey.getAlgorithm())) {1203System.out.println("test " + testnum++ + " passed");1204} else {1205throw new SecurityException1206("expected ARCFOUR, got " + skey.getAlgorithm());1207}12081209// getKey good pke 11210pkey = (PrivateKey)ks.getKey("pk1", null);1211if ("RSA".equals(pkey.getAlgorithm())) {1212System.out.println("test " + testnum++ + " passed");1213} else {1214throw new SecurityException1215("expected RSA, got " + pkey.getAlgorithm());1216}12171218// getKey good pke 31219pkey = (PrivateKey)ks.getKey("pk3", null);1220if ("RSA".equals(pkey.getAlgorithm())) {1221System.out.println("test " + testnum++ + " passed");1222} else {1223throw new SecurityException1224("expected RSA, got " + pkey.getAlgorithm());1225}12261227// contains alias1228if (!ks.containsAlias("pk1") ||1229!ks.containsAlias("pk2") ||1230!ks.containsAlias("pk3") ||1231!ks.containsAlias("sk1") ||1232!ks.containsAlias("sk2") ||1233!ks.containsAlias("sk3") ||1234!ks.containsAlias("sk4")) {1235throw new SecurityException("did not contain all aliases");1236}1237System.out.println("test " + testnum++ + " passed");12381239// getCertificateAlias pk11240if (ks.getCertificateAlias(chain1[0]).equals("pk1")) {1241System.out.println("test " + testnum++ + " passed");1242} else {1243throw new SecurityException("expected cert pk1");1244}12451246// getCertificateAlias pk31247if (ks.getCertificateAlias(chain3[0]).equals("pk3")) {1248System.out.println("test " + testnum++ + " passed");1249} else {1250throw new SecurityException("expected cert pk3");1251}12521253// isCertificateEntry pk11254if (!ks.isCertificateEntry("pk1")) {1255System.out.println("test " + testnum++ + " passed");1256} else {1257throw new SecurityException("expected pke");1258}12591260// isCertificateEntry pk31261if (!ks.isCertificateEntry("pk3")) {1262System.out.println("test " + testnum++ + " passed");1263} else {1264throw new SecurityException("expected pke");1265}12661267// isCertificateEntry sk11268if (!ks.isCertificateEntry("sk1")) {1269System.out.println("test " + testnum++ + " passed");1270} else {1271throw new SecurityException("expected ske");1272}12731274// isCertificateEntry sk41275if (!ks.isCertificateEntry("sk4")) {1276System.out.println("test " + testnum++ + " passed");1277} else {1278throw new SecurityException("expected ske");1279}12801281// isKeyEntry pk11282if (ks.isKeyEntry("pk1")) {1283System.out.println("test " + testnum++ + " passed");1284} else {1285throw new SecurityException("expected pke");1286}12871288// isKeyEntry pk31289if (ks.isKeyEntry("pk3")) {1290System.out.println("test " + testnum++ + " passed");1291} else {1292throw new SecurityException("expected pke");1293}12941295// isKeyEntry sk11296if (ks.isKeyEntry("sk1")) {1297System.out.println("test " + testnum++ + " passed");1298} else {1299throw new SecurityException("expected ske");1300}13011302// isKeyEntry sk41303if (ks.isKeyEntry("sk4")) {1304System.out.println("test " + testnum++ + " passed");1305} else {1306throw new SecurityException("expected ske");1307}13081309// isCertificateEntry random foo1310if (!ks.isCertificateEntry("foo")) {1311System.out.println("test " + testnum++ + " passed");1312} else {1313throw new SecurityException("expected foo");1314}13151316// isKeyEntry random foo1317if (!ks.isKeyEntry("foo")) {1318System.out.println("test " + testnum++ + " passed");1319} else {1320throw new SecurityException("expected foo");1321}13221323// entryInstanceOf pk11324if (!ks.entryInstanceOf1325("pk1", KeyStore.TrustedCertificateEntry.class)) {1326System.out.println("test " + testnum++ + " passed");1327} else {1328throw new SecurityException("expected tce");1329}13301331// entryInstanceOf pk31332if (!ks.entryInstanceOf1333("pk3", KeyStore.TrustedCertificateEntry.class)) {1334System.out.println("test " + testnum++ + " passed");1335} else {1336throw new SecurityException("expected tce");1337}13381339// entryInstanceOf sk11340if (!ks.entryInstanceOf1341("sk1", KeyStore.TrustedCertificateEntry.class)) {1342System.out.println("test " + testnum++ + " passed");1343} else {1344throw new SecurityException("expected tce");1345}13461347// entryInstanceOf sk41348if (!ks.entryInstanceOf1349("sk4", KeyStore.TrustedCertificateEntry.class)) {1350System.out.println("test " + testnum++ + " passed");1351} else {1352throw new SecurityException("expected tce");1353}13541355// entryInstanceOf pk11356if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class)) {1357System.out.println("test " + testnum++ + " passed");1358} else {1359throw new SecurityException("expected pke");1360}13611362// entryInstanceOf pk31363if (ks.entryInstanceOf("pk3", KeyStore.PrivateKeyEntry.class)) {1364System.out.println("test " + testnum++ + " passed");1365} else {1366throw new SecurityException("expected pke");1367}13681369// entryInstanceOf sk11370if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) {1371System.out.println("test " + testnum++ + " passed");1372} else {1373throw new SecurityException("expected pke");1374}13751376// entryInstanceOf sk41377if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) {1378System.out.println("test " + testnum++ + " passed");1379} else {1380throw new SecurityException("expected pke");1381}13821383// entryInstanceOf sk11384if (ks.entryInstanceOf("sk1", KeyStore.SecretKeyEntry.class)) {1385System.out.println("test " + testnum++ + " passed");1386} else {1387throw new SecurityException("expected ske");1388}13891390// entryInstanceOf sk41391if (ks.entryInstanceOf("sk4", KeyStore.SecretKeyEntry.class)) {1392System.out.println("test " + testnum++ + " passed");1393} else {1394throw new SecurityException("expected ske");1395}13961397// entryInstanceOf pk11398if (!ks.entryInstanceOf("pk1", KeyStore.SecretKeyEntry.class)) {1399System.out.println("test " + testnum++ + " passed");1400} else {1401throw new SecurityException("expected ske");1402}14031404// entryInstanceOf pk31405if (!ks.entryInstanceOf("pk3", KeyStore.SecretKeyEntry.class)) {1406System.out.println("test " + testnum++ + " passed");1407} else {1408throw new SecurityException("expected ske");1409}14101411// getEntry random foobar1412if (ks.getEntry("foobar", null) != null) {1413throw new SecurityException("expected null entry");1414} else {1415System.out.println("test " + testnum++ + " passed");1416}14171418// deleteEntry1419ks.deleteEntry("pk1");1420ks.deleteEntry("pk3");1421ks.deleteEntry("sk2");1422ks.deleteEntry("sk3");1423System.out.println("test " + testnum++ + " passed");14241425// aliases1426enu = ks.aliases();1427count = 0;1428while (enu.hasMoreElements()) {1429count++;1430System.out.println("alias " +1431count +1432" = " +1433(String)enu.nextElement());1434}1435if (count == 3) {1436System.out.println("test " + testnum++ + " passed");1437} else {1438throw new SecurityException("expected 3 aliases");1439}14401441// size1442if (ks.size() == 3) {1443System.out.println("test " + testnum++ + " passed");1444} else {1445throw new SecurityException("expected size 6");1446}14471448// entryInstanceOf sk11449if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) {1450System.out.println("test " + testnum++ + " passed");1451} else {1452throw new SecurityException("expected pke");1453}14541455// entryInstanceOf sk41456if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) {1457System.out.println("test " + testnum++ + " passed");1458} else {1459throw new SecurityException("expected pke");1460}14611462// entryInstanceOf pk21463if (ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class)) {1464System.out.println("test " + testnum++ + " passed");1465} else {1466throw new SecurityException("expected pke");1467}1468System.out.println("test " + testnum++ + " passed");14691470return testnum;1471}1472}147314741475