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