001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.io.IOException;
024: import java.security.KeyStore;
025: import java.security.KeyStoreException;
026: import java.security.Provider;
027: import java.security.Security;
028: import java.security.SignatureException;
029: import java.security.Principal;
030: import java.security.PublicKey;
031: import java.security.InvalidKeyException;
032: import java.security.NoSuchProviderException;
033: import java.security.NoSuchAlgorithmException;
034: import java.security.cert.Certificate;
035: import java.security.cert.CertificateException;
036: import java.security.cert.CertificateEncodingException;
037: import java.security.cert.CertificateExpiredException;
038: import java.security.cert.CertificateNotYetValidException;
039: import java.security.cert.X509Certificate;
040: import java.util.Date;
041: import java.util.Set;
042: import java.math.BigInteger;
043:
044: import javax.crypto.KeyGenerator;
045: import javax.crypto.SecretKey;
046:
047: import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
048: import org.apache.harmony.security.tests.support.MyLoadStoreParams;
049: import org.apache.harmony.security.tests.support.SpiEngUtils;
050:
051: import junit.framework.TestCase;
052:
053: /**
054: * Tests for <code>KeyStore</code> constructor and methods
055: *
056: */
057:
058: public class KeyStoreTest extends TestCase {
059:
060: private static final String KeyStoreProviderClass = "org.apache.harmony.security.tests.support.MyKeyStore";
061:
062: private static final String defaultType = "KeyStore";
063:
064: public static boolean KSSupported = false;
065:
066: public static String defaultProviderName = null;
067:
068: public static Provider defaultProvider = null;
069:
070: private static String NotSupportMsg = "Default KeyStore type is not supported";
071:
072: Provider mProv;
073:
074: public KeyStore[] createKS() throws Exception {
075: assertTrue(NotSupportMsg, KSSupported);
076: KeyStore[] kpg = new KeyStore[3];
077:
078: kpg[0] = KeyStore.getInstance(defaultType);
079: kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
080: kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
081: return kpg;
082: }
083:
084: protected void setUp() throws Exception {
085: super .setUp();
086: mProv = (new SpiEngUtils()).new MyProvider("MyKSProvider",
087: "Testing provider", KeyStoreTestSupport.srvKeyStore
088: .concat(".").concat(defaultType),
089: KeyStoreProviderClass);
090: Security.insertProviderAt(mProv, 2);
091: defaultProvider = SpiEngUtils.isSupport(defaultType,
092: KeyStoreTestSupport.srvKeyStore);
093: KSSupported = (defaultProvider != null);
094: defaultProviderName = (KSSupported ? defaultProvider.getName()
095: : null);
096: }
097:
098: /*
099: * @see TestCase#tearDown()
100: */
101: protected void tearDown() throws Exception {
102: super .tearDown();
103: Security.removeProvider(mProv.getName());
104: }
105:
106: /**
107: * Test for <code>load(LoadStoreParameter param)</code>
108: * <code>store(LoadStoreParameter param)</code>
109: * methods
110: * Assertions: throw IllegalArgumentException if param is null;
111: */
112: public void testLoadStore02() throws Exception {
113: assertTrue(NotSupportMsg, KSSupported);
114:
115: KeyStore[] kss = createKS();
116: assertNotNull("KeyStore objects were not created", kss);
117:
118: for (int i = 0; i < kss.length; i++) {
119: try {
120: kss[i].load(null);
121: fail("IOException or IllegalArgumentException should be thrown for null parameter");
122: } catch (IOException e) {
123: } catch (IllegalArgumentException e) {
124: }
125: kss[i].load(null, null);
126: try {
127: kss[i].store(null);
128: fail("IOException or IllegalArgumentException should be thrown for null parameter");
129: } catch (IOException e) {
130: } catch (IllegalArgumentException e) {
131: }
132: }
133: KeyStore.LoadStoreParameter lParam = new MyLoadStoreParams(
134: new KeyStore.PasswordProtection(new char[0]));
135: for (int i = 0; i < kss.length; i++) {
136: kss[i].load(lParam);
137: assertEquals("Incorrect result", kss[i].size(), 0);
138: kss[i].store(lParam);
139: }
140: }
141:
142: /**
143: * Test for <code>setKeyEntry(String alias, byte[] key, Certificate[] chain)</code>
144: * method
145: * Assertion: stores KeyEntry.
146: */
147: public void testSetKeyEntry() throws Exception {
148: assertTrue(NotSupportMsg, KSSupported);
149:
150: KeyStore[] kss = createKS();
151: assertNotNull("KeyStore objects were not created", kss);
152: byte[] kk = { (byte) 1, (byte) 2, (byte) 127, (byte) 77 };
153: String alias = "keyEntry";
154: char[] pwd = new char[0];
155: byte[] res;
156: Certificate certs[] = {
157: new KeyStoreTestSupport.MCertificate(alias, kk),
158: new KeyStoreTestSupport.MCertificate(alias, kk) };
159: for (int i = 0; i < kss.length; i++) {
160: kss[i].load(null, null);
161: try {
162: kss[i].setKeyEntry("proba", null, null);
163: fail("KeyStoreException must be thrown");
164: } catch (KeyStoreException e) {
165: }
166: kss[i].setKeyEntry(alias, kk, certs);
167: res = kss[i].getKey(alias, pwd).getEncoded();
168: assertEquals(kk.length, res.length);
169: for (int j = 0; j < res.length; j++) {
170: assertEquals(res[j], kk[j]);
171: }
172: assertEquals(kss[i].getCertificateChain(alias).length,
173: certs.length);
174: kss[i].setKeyEntry(alias, kk, null);
175: res = kss[i].getKey(alias, pwd).getEncoded();
176: assertEquals(kk.length, res.length);
177: for (int j = 0; j < res.length; j++) {
178: assertEquals(res[j], kk[j]);
179: }
180: assertNull(kss[i].getCertificateChain(alias));
181: }
182: }
183:
184: /**
185: * Test for <code>getDefaultType()</code> method Assertion: returns
186: * default security key store type or "jks" string
187: */
188: public void testKeyStore01() {
189: String propName = "keystore.type";
190: String defKSType = Security.getProperty(propName);
191: String dType = KeyStore.getDefaultType();
192: String resType = defKSType;
193: if (resType == null) {
194: resType = defaultType;
195: }
196: assertNotNull("Default type have not be null", dType);
197: assertEquals("Incorrect default type", dType, resType);
198:
199: if (defKSType == null) {
200: Security.setProperty(propName, defaultType);
201: dType = KeyStore.getDefaultType();
202: resType = Security.getProperty(propName);
203: assertNotNull("Incorrect default type", resType);
204: assertNotNull("Default type have not be null", dType);
205: assertEquals("Incorrect default type", dType, resType);
206: }
207: }
208:
209: /**
210: * Test for <code>getInstance(String type)</code> method
211: * Assertion:
212: * throws NullPointerException when type is null
213: * throws KeyStoreException when type is not available
214: *
215: */
216: public void testKeyStore02() throws KeyStoreException {
217: String[] invalidValues = SpiEngUtils.invalidValues;
218: try {
219: KeyStore.getInstance(null);
220: fail("NullPointerException must be thrown when type is null");
221: } catch (NullPointerException e) {
222: }
223: for (int i = 0; i < invalidValues.length; i++) {
224: try {
225: KeyStore.getInstance(invalidValues[i]);
226: fail("KeyStoreException must be thrown (type: ".concat(
227: invalidValues[i]).concat(" )"));
228: } catch (KeyStoreException e) {
229: }
230: }
231: }
232:
233: /**
234: * @test java.security.KeyStore.PasswordProtection.getPassword()
235: */
236: public void testKeyStorePPGetPassword() {
237: // Regression for HARMONY-1539
238: // no exception expected
239: assertNull(new KeyStore.PasswordProtection(null).getPassword());
240: char[] password = new char[] { 'a', 'b', 'c' };
241: KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(
242: password);
243: assertNotSame(pp.getPassword(), password);
244: assertSame(pp.getPassword(), pp.getPassword());
245:
246: }
247:
248: /*
249: * @tests java.security.KeyStoreSpi.engineEntryInstanceOf(String, Class<? extends Entry>)
250: */
251: public void testEngineEntryInstanceOf() throws Exception {
252: //Regression for HARMONY-615
253:
254: // create a KeyStore
255: KeyStore keyStore = KeyStore.getInstance(KeyStore
256: .getDefaultType());
257: keyStore.load(null, "pwd".toCharArray());
258:
259: // generate a key
260: KeyGenerator keyGen = KeyGenerator.getInstance("DES");
261: keyGen.init(56);
262: SecretKey secretKey = keyGen.generateKey();
263:
264: // put the key into keystore
265: String alias = "alias";
266: keyStore.setKeyEntry(alias, secretKey, "pwd".toCharArray(),
267: null);
268:
269: // check if it is a secret key
270: assertTrue(keyStore.entryInstanceOf(alias,
271: KeyStore.SecretKeyEntry.class));
272:
273: // check if it is NOT a private key
274: assertFalse(keyStore.entryInstanceOf(alias,
275: KeyStore.PrivateKeyEntry.class));
276: }
277:
278: /**
279: * @tests java.security.KeyStore.TrustedCertificateEntry.toString()
280: */
281: public void testKeyStoreTCToString() {
282: // Regression for HARMONY-1542
283: // no exception expected
284: class TestX509Certificate extends X509Certificate {
285: private static final long serialVersionUID = 1L;
286:
287: public void checkValidity()
288: throws CertificateExpiredException,
289: CertificateNotYetValidException {
290: }
291:
292: public void checkValidity(Date p)
293: throws CertificateExpiredException,
294: CertificateNotYetValidException {
295: }
296:
297: public int getVersion() {
298: return 0;
299: }
300:
301: public BigInteger getSerialNumber() {
302: return null;
303: }
304:
305: public Principal getIssuerDN() {
306: return null;
307: }
308:
309: public Principal getSubjectDN() {
310: return null;
311: }
312:
313: public Date getNotBefore() {
314: return null;
315: }
316:
317: public Date getNotAfter() {
318: return null;
319: }
320:
321: public byte[] getTBSCertificate()
322: throws CertificateEncodingException {
323: return null;
324: }
325:
326: public byte[] getSignature() {
327: return null;
328: }
329:
330: public String getSigAlgName() {
331: return null;
332: }
333:
334: public String getSigAlgOID() {
335: return null;
336: }
337:
338: public byte[] getSigAlgParams() {
339: return null;
340: }
341:
342: public boolean[] getIssuerUniqueID() {
343: return null;
344: }
345:
346: public boolean[] getSubjectUniqueID() {
347: return null;
348: }
349:
350: public boolean[] getKeyUsage() {
351: return null;
352: }
353:
354: public int getBasicConstraints() {
355: return 0;
356: }
357:
358: public byte[] getEncoded()
359: throws CertificateEncodingException {
360: return null;
361: }
362:
363: public void verify(PublicKey p)
364: throws CertificateException,
365: NoSuchAlgorithmException, InvalidKeyException,
366: NoSuchProviderException, SignatureException {
367: }
368:
369: public void verify(PublicKey p0, String p1)
370: throws CertificateException,
371: NoSuchAlgorithmException, InvalidKeyException,
372: NoSuchProviderException, SignatureException {
373: }
374:
375: public String toString() {
376: return null;
377: }
378:
379: public PublicKey getPublicKey() {
380: return null;
381: }
382:
383: public boolean hasUnsupportedCriticalExtension() {
384: return false;
385: }
386:
387: public Set getCriticalExtensionOIDs() {
388: return null;
389: }
390:
391: public Set getNonCriticalExtensionOIDs() {
392: return null;
393: }
394:
395: public byte[] getExtensionValue(String p) {
396: return null;
397: }
398: }
399: assertNotNull(new KeyStore.TrustedCertificateEntry(
400: new TestX509Certificate()).toString());
401: }
402: }
|