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.java.security;
19
20import java.io.ByteArrayInputStream;
21import java.io.UnsupportedEncodingException;
22import java.security.KeyPair;
23import java.security.KeyPairGenerator;
24import java.security.KeyStore;
25import java.security.PrivateKey;
26import java.security.cert.Certificate;
27import java.security.cert.CertificateFactory;
28import java.security.cert.X509Certificate;
29
30import junit.framework.TestCase;
31
32public class KeyStorePrivateKeyEntryTest extends TestCase {
33
34    public void testGetCertificateChain() throws Exception {
35
36        String certificateData = "-----BEGIN CERTIFICATE-----\n"
37                + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
38                + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
39                + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
40                + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
41                + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
42                + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
43                + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
44                + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
45                + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
46                + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
47                + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
48                + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
49                + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
50                + "-----END CERTIFICATE-----\n";
51
52        ByteArrayInputStream certArray;
53        {
54            try {
55                certArray = new ByteArrayInputStream(
56                        certificateData.getBytes("UTF-8"));
57            } catch (UnsupportedEncodingException e) {
58                throw new RuntimeException(e.getMessage());
59            }
60        }
61        CertificateFactory cf = CertificateFactory.getInstance("X.509");
62        Certificate certificate = cf.generateCertificate(certArray);
63        assertTrue(certificate instanceof X509Certificate);
64
65        String algorithm = certificate.getPublicKey().getAlgorithm();
66        KeyPairGenerator keyPairGenerator = KeyPairGenerator
67                .getInstance(algorithm);
68        KeyPair keyPair = keyPairGenerator.generateKeyPair();
69        PrivateKey privateKey = keyPair.getPrivate();
70
71        // If all the certificate in the chain is X509Certificate,
72        // KeyStore.PrivateKeyEntry will return a X509Certificate array.
73        KeyStore.PrivateKeyEntry privateKeyEntry = new KeyStore.PrivateKeyEntry(
74                privateKey, new Certificate[] { certificate });
75        Certificate[] chain = privateKeyEntry.getCertificateChain();
76        assertTrue(chain instanceof X509Certificate[]);
77
78    }
79}
80