blob: 58824e5f405b4e88f2a986501fd78443941e2af6 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test 1.1, 03/06/24
26 * @bug 4850376
27 * @summary Provide generic storage KeyStore storage facilities
28 */
29
30import java.security.KeyStore;
31import java.security.PrivateKey;
32import java.security.PublicKey;
33import java.security.Principal;
34import java.security.InvalidKeyException;
35import java.security.NoSuchAlgorithmException;
36import java.security.NoSuchProviderException;
37import java.security.SignatureException;
38import java.security.cert.Certificate;
39import java.security.cert.X509Certificate;
40import java.security.cert.CertificateException;
41import java.security.cert.CertificateExpiredException;
42import java.security.cert.CertificateEncodingException;
43import java.security.cert.CertificateNotYetValidException;
44import java.security.cert.X509Certificate;
45import java.util.Date;
46import java.util.Set;
47import java.util.HashSet;
48import java.math.BigInteger;
49import javax.security.auth.x500.X500Principal;
50
51public class DefaultEntryType {
52
53 private static class PrivKey1 implements PrivateKey {
54 public String getAlgorithm() { return ("matching_alg"); }
55 public String getFormat() { return "privkey1"; }
56 public byte[] getEncoded() { return (byte[])null; }
57 }
58
59 private static class PubKey1 implements PublicKey {
60 public String getAlgorithm() { return ("non_matching_alg"); }
61 public String getFormat() { return "pubkey1"; }
62 public byte[] getEncoded() { return (byte[])null; }
63 }
64
65 private static class PubKey2 implements PublicKey {
66 public String getAlgorithm() { return ("matching_alg"); }
67 public String getFormat() { return "pubkey2"; }
68 public byte[] getEncoded() { return (byte[])null; }
69 }
70
71 private static class Cert extends Certificate {
72 public Cert() { super("cert"); }
73 public byte[] getEncoded()
74 throws CertificateEncodingException { return (byte[])null; }
75 public void verify(PublicKey key)
76 throws CertificateException, NoSuchAlgorithmException,
77 InvalidKeyException, NoSuchProviderException,
78 SignatureException { }
79 public void verify(PublicKey key, String sigProvider)
80 throws CertificateException, NoSuchAlgorithmException,
81 InvalidKeyException, NoSuchProviderException,
82 SignatureException { }
83 public String toString() { return "cert"; }
84 public PublicKey getPublicKey() { return new PubKey1(); }
85 }
86
87 private static class X509Cert extends X509Certificate {
88 public byte[] getEncoded()
89 throws CertificateEncodingException { return (byte[])null; }
90 public void verify(PublicKey key)
91 throws CertificateException, NoSuchAlgorithmException,
92 InvalidKeyException, NoSuchProviderException,
93 SignatureException { }
94 public void verify(PublicKey key, String sigProvider)
95 throws CertificateException, NoSuchAlgorithmException,
96 InvalidKeyException, NoSuchProviderException,
97 SignatureException { }
98 public String toString() { return "x509cert"; }
99 public PublicKey getPublicKey() { return new PubKey2(); }
100
101 public void checkValidity()
102 throws CertificateExpiredException,
103 CertificateNotYetValidException { }
104 public void checkValidity(java.util.Date date)
105 throws CertificateExpiredException,
106 CertificateNotYetValidException { }
107 public int getVersion() { return 1; }
108 public BigInteger getSerialNumber() { return new BigInteger("5", 10); }
109 public Principal getIssuerDN()
110 { return new X500Principal("cn=x509cert"); }
111 public X500Principal getIssuerX500Principal()
112 { return new X500Principal("cn=x509cert"); }
113 public Principal getSubjectDN()
114 { return new X500Principal("cn=x509cert"); }
115 public X500Principal getSubjectX500Principal()
116 { return new X500Principal("cn=x509cert"); }
117 public Date getNotBefore() { return new Date(); }
118 public Date getNotAfter() { return new Date(); }
119 public byte[] getTBSCertificate() throws CertificateEncodingException
120 { return (byte[])null; }
121 public byte[] getSignature() { return (byte[])null; }
122 public String getSigAlgName() { return "x509cert"; }
123 public String getSigAlgOID() { return "x509cert"; }
124 public byte[] getSigAlgParams() { return (byte[])null; }
125
126 public boolean[] getIssuerUniqueID() { return (boolean[])null; }
127 public boolean[] getSubjectUniqueID() { return (boolean[])null; }
128 public boolean[] getKeyUsage() { return (boolean[]) null; }
129 public int getBasicConstraints() { return 1; }
130
131 public boolean hasUnsupportedCriticalExtension() { return true; }
132 public Set getCriticalExtensionOIDs() { return new HashSet(); }
133 public Set getNonCriticalExtensionOIDs() { return new HashSet(); }
134 public byte[] getExtensionValue(String oid) { return (byte[])null; }
135 }
136
137 public static void main(String[] args) throws Exception {
138 testPrivateKeyEntry();
139 testSecretKeyEntry();
140 testTrustedCertificateEntry();
141 }
142
143 private static void testPrivateKeyEntry() throws Exception {
144 // TEST null private key
145 try {
146 Certificate[] chain = new Certificate[0];
147 KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry
148 (null, chain);
149 throw new SecurityException("test 1 failed");
150 } catch (NullPointerException npe) {
151 // good
152 System.out.println("test 1 passed");
153 }
154
155 // TEST null chain
156 try {
157 KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry
158 (new PrivKey1(), null);
159 throw new SecurityException("test 2 failed");
160 } catch (NullPointerException npe) {
161 // good
162 System.out.println("test 2 passed");
163 }
164
165 // TEST empty chain
166 try {
167 Certificate[] chain = new Certificate[0];
168 KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry
169 (new PrivKey1(), chain);
170 throw new SecurityException("test 3 failed");
171 } catch (IllegalArgumentException npe) {
172 // good
173 System.out.println("test 3 passed");
174 }
175
176 // TEST non-homogenous chain
177 try {
178 Certificate[] chain = new Certificate[2];
179 chain[0] = new Cert();
180 chain[1] = new X509Cert();
181 KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry
182 (new PrivKey1(), chain);
183 throw new SecurityException("test 4 failed");
184 } catch (IllegalArgumentException npe) {
185 // good
186 System.out.println("test 4 passed");
187 }
188
189 // TEST non matching algorithms
190 try {
191 Certificate[] chain = new Certificate[1];
192 chain[0] = new Cert();
193 KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry
194 (new PrivKey1(), chain);
195 throw new SecurityException("test 5 failed");
196 } catch (IllegalArgumentException npe) {
197 // good
198 System.out.println("test 5 passed");
199 }
200
201 // TEST correct behavior
202 Certificate[] chain = new Certificate[2];
203 chain[0] = new X509Cert();
204 chain[1] = new X509Cert();
205 PrivateKey pkey = new PrivKey1();
206 KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry
207 (pkey, chain);
208 Certificate[] gotChain = pke.getCertificateChain();
209 if (gotChain instanceof X509Certificate[]) {
210 System.out.println("test 6 passed");
211 } else {
212 throw new SecurityException("test 6 failed");
213 }
214
215 if (gotChain.length == 2 &&
216 gotChain[0] == chain[0] &&
217 gotChain[1] == chain[1]) {
218 System.out.println("test 7 passed");
219 } else {
220 throw new SecurityException("test 7 failed");
221 }
222
223 if (pke.getPrivateKey() == pkey) {
224 System.out.println("test 8 passed");
225 } else {
226 throw new SecurityException("test 8 failed");
227 }
228 }
229
230 private static void testSecretKeyEntry() throws Exception {
231 }
232
233 private static void testTrustedCertificateEntry() throws Exception {
234 }
235}