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