01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.java.security;
19:
20: import java.io.ByteArrayInputStream;
21: import java.security.KeyPair;
22: import java.security.KeyPairGenerator;
23: import java.security.KeyStore;
24: import java.security.PrivateKey;
25: import java.security.cert.Certificate;
26: import java.security.cert.CertificateFactory;
27: import java.security.cert.X509Certificate;
28:
29: import junit.framework.TestCase;
30:
31: public class KeyStorePrivateKeyEntryTest extends TestCase {
32:
33: public void testGetCertificateChain() throws Exception {
34:
35: String certificateData = "-----BEGIN CERTIFICATE-----\n"
36: + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
37: + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
38: + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
39: + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
40: + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
41: + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
42: + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
43: + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
44: + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
45: + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
46: + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
47: + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
48: + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
49: + "-----END CERTIFICATE-----\n";
50:
51: ByteArrayInputStream certArray = new ByteArrayInputStream(
52: certificateData.getBytes());
53: CertificateFactory cf = CertificateFactory.getInstance("X.509");
54: Certificate certificate = cf.generateCertificate(certArray);
55: assertTrue(certificate instanceof X509Certificate);
56:
57: String algorithm = certificate.getPublicKey().getAlgorithm();
58: KeyPairGenerator keyPairGenerator = KeyPairGenerator
59: .getInstance(algorithm);
60: KeyPair keyPair = keyPairGenerator.generateKeyPair();
61: PrivateKey privateKey = keyPair.getPrivate();
62:
63: // If all the certificate in the chain is X509Certificate,
64: // KeyStore.PrivateKeyEntry will return a X509Certificate array.
65: KeyStore.PrivateKeyEntry privateKeyEntry = new KeyStore.PrivateKeyEntry(
66: privateKey, new Certificate[] { certificate });
67: Certificate[] chain = privateKeyEntry.getCertificateChain();
68: assertTrue(chain instanceof X509Certificate[]);
69:
70: }
71: }
|