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 package org.apache.harmony.security.tests.support;
     19 
     20 import java.security.InvalidKeyException;
     21 import java.security.KeyStore;
     22 import java.security.NoSuchAlgorithmException;
     23 import java.security.NoSuchProviderException;
     24 import java.security.PrivateKey;
     25 import java.security.Provider;
     26 import java.security.PublicKey;
     27 import java.security.SignatureException;
     28 import java.security.cert.Certificate;
     29 import java.security.cert.CertificateEncodingException;
     30 import java.security.cert.CertificateException;
     31 
     32 import javax.crypto.SecretKey;
     33 
     34 /**
     35  * Support class for KeyStore tests
     36  *
     37  */
     38 
     39 public class KeyStoreTestSupport {
     40 
     41     public static final String srvKeyStore = "KeyStore";
     42 
     43     public static String[] validValues = { "bks", "BKS", "bKS", "Bks", "bKs",
     44             "BkS" };
     45 
     46     public static String defaultType = "bks";
     47 
     48     public static boolean JKSSupported = false;
     49 
     50     public static String defaultProviderName = null;
     51 
     52     public static Provider defaultProvider = null;
     53 
     54     static {
     55         defaultProvider = SpiEngUtils.isSupport(defaultType, srvKeyStore);
     56         JKSSupported = (defaultProvider != null);
     57         defaultProviderName = (JKSSupported ? defaultProvider.getName() : null);
     58     }
     59 
     60     /**
     61      * Additional class to create SecretKey object
     62      */
     63     public static class SKey implements SecretKey {
     64         private String type;
     65 
     66         private byte[] encoded;
     67 
     68         public SKey(String type, byte[] encoded) {
     69             this.type = type;
     70             this.encoded = encoded;
     71         }
     72 
     73         public String getAlgorithm() {
     74             return type;
     75         }
     76 
     77         public byte[] getEncoded() {
     78             return encoded;
     79         }
     80 
     81         public String getFormat() {
     82             return "test";
     83         }
     84     }
     85 
     86     /**
     87      * Additional class to create PrivateKey object
     88      */
     89     public static class MyPrivateKey implements PrivateKey {
     90         private String algorithm;
     91 
     92         private String format;
     93 
     94         private byte[] encoded;
     95 
     96         public MyPrivateKey(String algorithm, String format, byte[] encoded) {
     97             this.algorithm = algorithm;
     98             this.format = format;
     99             this.encoded = encoded;
    100         }
    101 
    102         public String getAlgorithm() {
    103             return algorithm;
    104         }
    105 
    106         public String getFormat() {
    107             return format;
    108         }
    109 
    110         public byte[] getEncoded() {
    111             return encoded;
    112         }
    113     }
    114 
    115     /**
    116      * Additional class to create Certificate and Key objects
    117      */
    118     public static class MCertificate extends Certificate {
    119         private final byte[] encoding;
    120 
    121         private final String type;
    122 
    123         public MCertificate(String type, byte[] encoding) {
    124             super(type);
    125             this.encoding = encoding;
    126             this.type = type;
    127         }
    128 
    129         public byte[] getEncoded() throws CertificateEncodingException {
    130             return encoding.clone();
    131         }
    132 
    133         public void verify(PublicKey key) throws CertificateException,
    134                 NoSuchAlgorithmException, InvalidKeyException,
    135                 NoSuchProviderException, SignatureException {
    136         }
    137 
    138         public void verify(PublicKey key, String sigProvider)
    139                 throws CertificateException, NoSuchAlgorithmException,
    140                 InvalidKeyException, NoSuchProviderException,
    141                 SignatureException {
    142         }
    143 
    144         public String toString() {
    145             return "[MCertificate, type: " + getType() + "]";
    146         }
    147 
    148         public PublicKey getPublicKey() {
    149             return new PublicKey() {
    150                 public String getAlgorithm() {
    151                     return type;
    152                 }
    153 
    154                 public byte[] getEncoded() {
    155                     return encoding;
    156                 }
    157 
    158                 public String getFormat() {
    159                     return "test";
    160                 }
    161             };
    162         }
    163     }
    164 
    165     /**
    166      * Additional class to create ProtectionParameter object
    167      */
    168     public static class ProtPar implements KeyStore.ProtectionParameter {
    169     }
    170 
    171     /**
    172      * Additional class to create KeyStore.Entry object
    173      */
    174     public static class AnotherEntry implements KeyStore.Entry {
    175     }
    176 }
    177 
    178