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