1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.security.tests.java.security; 19 20import java.io.ByteArrayInputStream; 21import java.io.FileNotFoundException; 22import java.io.IOException; 23import java.io.InputStream; 24import java.io.OutputStream; 25import java.io.UnsupportedEncodingException; 26import java.security.Key; 27import java.security.KeyPair; 28import java.security.KeyPairGenerator; 29import java.security.KeyStore; 30import java.security.KeyStoreException; 31import java.security.KeyStoreSpi; 32import java.security.NoSuchAlgorithmException; 33import java.security.Provider; 34import java.security.UnrecoverableKeyException; 35import java.security.KeyStore.Builder; 36import java.security.KeyStore.PasswordProtection; 37import java.security.KeyStore.ProtectionParameter; 38import java.security.cert.Certificate; 39import java.security.cert.CertificateException; 40import java.security.cert.CertificateFactory; 41import java.util.Date; 42import java.util.Enumeration; 43 44import junit.framework.TestCase; 45 46public class KeyStore3Test extends TestCase { 47 48 private KeyStore mockKeyStore; 49 50 private KeyPair keyPair; 51 52 private Certificate certificate; 53 54 public KeyStore3Test() throws Exception { 55 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); 56 keyPair = keyPairGenerator.generateKeyPair(); 57 58 String certificateData = "-----BEGIN CERTIFICATE-----\n" 59 + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" 60 + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" 61 + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" 62 + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" 63 + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" 64 + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" 65 + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" 66 + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" 67 + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" 68 + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" 69 + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" 70 + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" 71 + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" 72 + "-----END CERTIFICATE-----\n"; 73 74 ByteArrayInputStream certArray; 75 { 76 try { 77 certArray = new ByteArrayInputStream(certificateData.getBytes("UTF-8")); 78 } catch (UnsupportedEncodingException e) { 79 throw new RuntimeException(e.getMessage()); 80 } 81 } 82 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 83 certificate = cf.generateCertificate(certArray); 84 } 85 86 public void test_load() throws Exception { 87 // No exception should be thrown out. 88 mockKeyStore.load(null); 89 } 90 91 public void test_store() throws Exception { 92 try { 93 mockKeyStore.store(null); 94 fail("should throw KeyStoreException: not initialized"); 95 } catch (KeyStoreException e) { 96 // expected 97 } 98 99 // No exception should be thrown out. 100 mockKeyStore.load(null, null); 101 mockKeyStore.store(null); 102 } 103 104 public void test_setKeyEntry_null() throws Exception { 105 mockKeyStore.load(null, null); 106 // No exception should be thrown out. 107 mockKeyStore.setKeyEntry(null, null, null, null); 108 } 109 110 public void test_setKeyEntry_key_is_null() throws Exception { 111 mockKeyStore.load(null, null); 112 // No exception should be thrown out. 113 mockKeyStore.setKeyEntry("Alias", null, null, new Certificate[] { certificate }); 114 } 115 116 public void test_setKeyEntry_key_is_private() throws Exception { 117 mockKeyStore.load(null, null); 118 Key key = keyPair.getPrivate(); 119 try { 120 mockKeyStore.setKeyEntry("Alias", key, null, null); 121 fail("should throw IllegalArgumentException"); 122 } catch (IllegalArgumentException e) { 123 // expected 124 } 125 126 try { 127 mockKeyStore.setKeyEntry("Alias", key, null, 128 new Certificate[0]); 129 fail("should throw IllegalArgumentException"); 130 } catch (IllegalArgumentException e) { 131 // expected 132 } 133 134 mockKeyStore.setKeyEntry("Alias", key, null, new Certificate[] { certificate }); 135 } 136 137 public void test_setKeyEntry_key_is_public() throws Exception { 138 mockKeyStore.load(null, null); 139 Key key = keyPair.getPublic(); 140 mockKeyStore.setKeyEntry("Alias1", key, null, null); 141 mockKeyStore.setKeyEntry("Alias2", key, null, 142 new Certificate[0]); 143 mockKeyStore.setKeyEntry("Alias3", key, null, new Certificate[] { certificate }); 144 } 145 146 public void test_setCertificateEntry_null() throws Exception { 147 mockKeyStore.load(null, null); 148 149 mockKeyStore.setCertificateEntry(null, null); 150 151 mockKeyStore.setCertificateEntry(null, certificate); 152 153 mockKeyStore.setCertificateEntry("Alias", null); 154 } 155 156 public void test_store_null() throws Exception { 157 mockKeyStore.load(null, null); 158 mockKeyStore.store(null, null); 159 } 160 161 public void test_getKeyStore() throws KeyStoreException, 162 NoSuchAlgorithmException, CertificateException, 163 FileNotFoundException, IOException { 164 165 String alias = "BKS"; 166 char[] pwd = new char[] { '1', '2', '3', '4', '5', '6' }; 167 InputStream fis = KeyStore2Test.class 168 .getResourceAsStream("builderimpl.ks"); 169 KeyStore ks = KeyStore.getInstance(alias); 170 ks.load(fis, pwd); 171 Builder b = Builder.newInstance(ks, new PasswordProtection(pwd)); 172 KeyStore firstKeyStore = b.getKeyStore(); 173 ProtectionParameter firstProtParameter = b 174 .getProtectionParameter(alias); 175 assertSame(firstKeyStore, b.getKeyStore()); 176 assertSame(firstProtParameter, b.getProtectionParameter(alias)); 177 178 b = Builder.newInstance(alias, ks.getProvider(), 179 new KeyStore.PasswordProtection(pwd)); 180 firstKeyStore = b.getKeyStore(); 181 firstProtParameter = b.getProtectionParameter(alias); 182 assertNotSame(firstKeyStore, b.getKeyStore()); 183 assertSame(firstProtParameter, b.getProtectionParameter(alias)); 184 } 185 186 protected void setUp() throws Exception { 187 super.setUp(); 188 mockKeyStore = new MyKeyStore(new MyKeyStoreSpi(), null, "MyKeyStore"); 189 } 190 191 private static class MyKeyStore extends KeyStore { 192 193 public MyKeyStore(KeyStoreSpi keyStoreSpi, Provider provider, 194 String type) { 195 super(keyStoreSpi, provider, type); 196 } 197 } 198 199 private static class MyKeyStoreSpi extends KeyStoreSpi { 200 201 public Enumeration<String> engineAliases() { 202 return null; 203 } 204 205 public boolean engineContainsAlias(String arg0) { 206 return false; 207 } 208 209 public void engineDeleteEntry(String arg0) throws KeyStoreException { 210 } 211 212 public Certificate engineGetCertificate(String arg0) { 213 return null; 214 } 215 216 public String engineGetCertificateAlias(Certificate arg0) { 217 return null; 218 } 219 220 public Certificate[] engineGetCertificateChain(String arg0) { 221 return null; 222 } 223 224 public Date engineGetCreationDate(String arg0) { 225 return null; 226 } 227 228 public Key engineGetKey(String arg0, char[] arg1) 229 throws NoSuchAlgorithmException, UnrecoverableKeyException { 230 return null; 231 } 232 233 public boolean engineIsCertificateEntry(String arg0) { 234 return false; 235 } 236 237 public boolean engineIsKeyEntry(String arg0) { 238 return false; 239 } 240 241 public void engineLoad(InputStream arg0, char[] arg1) 242 throws IOException, NoSuchAlgorithmException, 243 CertificateException { 244 return; 245 } 246 247 public void engineSetCertificateEntry(String arg0, Certificate arg1) 248 throws KeyStoreException { 249 return; 250 } 251 252 public void engineSetKeyEntry(String arg0, byte[] arg1, 253 Certificate[] arg2) throws KeyStoreException { 254 return; 255 } 256 257 public void engineSetKeyEntry(String arg0, Key arg1, char[] arg2, 258 Certificate[] arg3) throws KeyStoreException { 259 return; 260 } 261 262 public int engineSize() { 263 return 0; 264 } 265 266 public void engineStore(KeyStore.LoadStoreParameter param) { 267 return; 268 } 269 270 public void engineStore(OutputStream arg0, char[] arg1) 271 throws IOException, NoSuchAlgorithmException, 272 CertificateException { 273 return; 274 } 275 } 276 277} 278 279