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 public 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 
SKey(String type, byte[] encoded)67         public SKey(String type, byte[] encoded) {
68             this.type = type;
69             this.encoded = encoded;
70         }
71 
getAlgorithm()72         public String getAlgorithm() {
73             return type;
74         }
75 
getEncoded()76         public byte[] getEncoded() {
77             return encoded;
78         }
79 
getFormat()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 
MyPrivateKey(String algorithm, String format, byte[] encoded)95         public MyPrivateKey(String algorithm, String format, byte[] encoded) {
96             this.algorithm = algorithm;
97             this.format = format;
98             this.encoded = encoded;
99         }
100 
getAlgorithm()101         public String getAlgorithm() {
102             return algorithm;
103         }
104 
getFormat()105         public String getFormat() {
106             return format;
107         }
108 
getEncoded()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 
MCertificate(String type, byte[] encoding)122         public MCertificate(String type, byte[] encoding) {
123             super(type);
124             this.encoding = encoding;
125             this.type = type;
126         }
127 
getEncoded()128         public byte[] getEncoded() throws CertificateEncodingException {
129             return encoding.clone();
130         }
131 
verify(PublicKey key)132         public void verify(PublicKey key) throws CertificateException,
133                 NoSuchAlgorithmException, InvalidKeyException,
134                 NoSuchProviderException, SignatureException {
135         }
136 
verify(PublicKey key, String sigProvider)137         public void verify(PublicKey key, String sigProvider)
138                 throws CertificateException, NoSuchAlgorithmException,
139                 InvalidKeyException, NoSuchProviderException,
140                 SignatureException {
141         }
142 
toString()143         public String toString() {
144             return "[MCertificate, type: " + getType() + "]";
145         }
146 
getPublicKey()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