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 18/** 19 * @author Vera Y. Petrashkova 20 */ 21 22package org.apache.harmony.security.tests.support; 23 24import java.io.ByteArrayOutputStream; 25import java.io.IOException; 26import java.io.InputStream; 27import java.io.OutputStream; 28import java.security.Key; 29import java.security.KeyStore; 30import java.security.KeyStoreException; 31import java.security.KeyStoreSpi; 32import java.security.NoSuchAlgorithmException; 33import java.security.UnrecoverableKeyException; 34import java.security.cert.Certificate; 35import java.security.cert.CertificateException; 36import java.util.Date; 37import java.util.Enumeration; 38import java.util.Hashtable; 39 40/** 41 * Additional class for KeyStoreSpi and KeyStore verification 42 */ 43 44public class MyKeyStore extends KeyStoreSpi { 45 private Hashtable Keys = new Hashtable(); 46 47 private Hashtable Cert = new Hashtable(); 48 49 private Hashtable Chain = new Hashtable(); 50 51 private Hashtable Dates = new Hashtable(); 52 53 private Hashtable KeysSL = new Hashtable(); 54 55 private Hashtable CertSL = new Hashtable(); 56 57 private Hashtable ChainSL = new Hashtable(); 58 59 private Hashtable DatesSL = new Hashtable(); 60 61 public Key engineGetKey(String alias, char[] password) 62 throws NoSuchAlgorithmException, UnrecoverableKeyException { 63 if (Keys.containsKey(alias)) { 64 return (Key) Keys.get(alias); 65 } 66 return null; 67 } 68 69 public Certificate[] engineGetCertificateChain(String alias) { 70 if (Chain.containsKey(alias)) { 71 return (Certificate[]) Chain.get(alias); 72 } 73 return null; 74 } 75 76 public Certificate engineGetCertificate(String alias) { 77 if (Cert.containsKey(alias)) { 78 return (Certificate) Cert.get(alias); 79 } 80 return null; 81 } 82 83 public Date engineGetCreationDate(String alias) { 84 if (Dates.containsKey(alias)) { 85 return (Date) Dates.get(alias); 86 } 87 return null; 88 } 89 90 public void engineSetKeyEntry(String alias, Key key, char[] password, 91 Certificate[] chain) throws KeyStoreException { 92 if (Cert.containsKey(alias)) { 93 Cert.remove(alias); 94 } 95 Keys.put(alias, key); 96 if (chain != null) { 97 Chain.put(alias, chain); 98 } 99 Dates.put(alias, new Date()); 100 } 101 102 public void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain) 103 throws KeyStoreException { 104 if (key == null) { 105 throw new KeyStoreException("Not Supported for null key"); 106 } 107 if (Cert.containsKey(alias)) { 108 Cert.remove(alias); 109 } 110 if (Chain.containsKey(alias)) { 111 Chain.remove(alias); 112 } 113 KeyStoreTestSupport.MyPrivateKey keyK = new KeyStoreTestSupport.MyPrivateKey( 114 alias, alias, key); 115 Keys.put(alias, keyK); 116 if (chain != null) { 117 Chain.put(alias, chain); 118 } 119 Dates.put(alias, new Date()); 120 121 } 122 123 public void engineSetCertificateEntry(String alias, Certificate cert) 124 throws KeyStoreException { 125 Cert.put(alias, cert); 126 Dates.put(alias, new Date()); 127 } 128 129 public void engineDeleteEntry(String alias) throws KeyStoreException { 130 if (Keys.containsKey(alias)) { 131 Keys.remove(alias); 132 Chain.remove(alias); 133 return; 134 } 135 if (Cert.containsKey(alias)) { 136 Cert.remove(alias); 137 } 138 } 139 140 public Enumeration engineAliases() { 141 return null; 142 } 143 144 public boolean engineContainsAlias(String alias) { 145 if (Keys.containsKey(alias)) { 146 return true; 147 } 148 if (Cert.containsKey(alias)) { 149 return true; 150 } 151 return false; 152 } 153 154 public int engineSize() { 155 return (Keys.size() + Cert.size()); 156 } 157 158 public boolean engineIsKeyEntry(String alias) { 159 if (Keys.containsKey(alias)) { 160 return true; 161 } 162 return false; 163 } 164 165 public boolean engineIsCertificateEntry(String alias) { 166 if (Cert.containsKey(alias)) { 167 return true; 168 } 169 return false; 170 } 171 172 public String engineGetCertificateAlias(Certificate cert) { 173 return ""; 174 } 175 176 public void engineStore(OutputStream stream, char[] password) 177 throws IOException, NoSuchAlgorithmException, CertificateException { 178 if (!(stream instanceof ByteArrayOutputStream)) { 179 throw new IOException("Incorrect stream"); 180 } 181 String alias; 182 Enumeration e = Keys.keys(); 183 while (e.hasMoreElements()) { 184 alias = (String) e.nextElement(); 185 KeysSL.put(alias, Keys.get(alias)); 186 DatesSL.put(alias, Dates.get(alias)); 187 if (Chain.containsKey(alias)) { 188 ChainSL.put(alias, Chain.get(alias)); 189 } 190 } 191 e = Cert.keys(); 192 while (e.hasMoreElements()) { 193 alias = (String) e.nextElement(); 194 CertSL.put(alias, Cert.get(alias)); 195 DatesSL.put(alias, Dates.get(alias)); 196 } 197 } 198 199 public void engineLoad(InputStream stream, char[] password) 200 throws IOException, NoSuchAlgorithmException, CertificateException { 201 Keys.clear(); 202 Cert.clear(); 203 Chain.clear(); 204 Dates.clear(); 205 String alias; 206 Enumeration e = KeysSL.keys(); 207 while (e.hasMoreElements()) { 208 alias = (String) e.nextElement(); 209 Keys.put(alias, KeysSL.get(alias)); 210 Dates.put(alias, DatesSL.get(alias)); 211 if (ChainSL.containsKey(alias)) { 212 Chain.put(alias, ChainSL.get(alias)); 213 } 214 } 215 e = CertSL.keys(); 216 while (e.hasMoreElements()) { 217 alias = (String) e.nextElement(); 218 Cert.put(alias, CertSL.get(alias)); 219 Dates.put(alias, DatesSL.get(alias)); 220 } 221 } 222 223 public void engineStore(KeyStore.LoadStoreParameter param) 224 throws IOException, NoSuchAlgorithmException, CertificateException { 225 if (param == null) { 226 throw new IOException("param is null"); 227 } 228 } 229 230 public void engineLoad(KeyStore.LoadStoreParameter param) 231 throws IOException, NoSuchAlgorithmException, CertificateException { 232 if (!(param instanceof MyLoadStoreParams)) { 233 throw new IllegalArgumentException("param is not MyLoadStoreParams: " + param); 234 } 235 } 236}