13883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher/*
23883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  Licensed to the Apache Software Foundation (ASF) under one or more
33883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  contributor license agreements.  See the NOTICE file distributed with
43883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  this work for additional information regarding copyright ownership.
53883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  The ASF licenses this file to You under the Apache License, Version 2.0
63883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  (the "License"); you may not use this file except in compliance with
73883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  the License.  You may obtain a copy of the License at
83883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *
93883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *     http://www.apache.org/licenses/LICENSE-2.0
103883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *
113883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  Unless required by applicable law or agreed to in writing, software
123883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  distributed under the License is distributed on an "AS IS" BASIS,
133883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  See the License for the specific language governing permissions and
153883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher *  limitations under the License.
163883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher */
173883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher
183883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopherpackage org.apache.harmony.xnet.provider.jsse;
19
20import java.io.InputStream;
21import java.security.KeyStore;
22import java.security.SecureRandom;
23
24import javax.net.ssl.KeyManagerFactory;
25import javax.net.ssl.SSLContext;
26import javax.net.ssl.TrustManagerFactory;
27
28import tests.support.resource.Support_Resources;
29
30/**
31 * JSSETestData
32 */
33public class JSSETestData {
34
35    private static Exception initException;
36
37    // the password to the store
38    public static final char[] KS_PASSWORD = "password".toCharArray();
39
40    private static SSLContext context;
41    private static KeyStore keyStore;
42    private static SSLParameters sslParameters;
43
44    static {
45        try {
46            String ksDefaultType = KeyStore.getDefaultType();
47            InputStream is = Support_Resources.getResourceStream(
48                    "key_store." + ksDefaultType.toLowerCase());
49
50            keyStore = KeyStore.getInstance(ksDefaultType);
51            keyStore.load(is, KS_PASSWORD);
52
53            KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
54            kmf.init(keyStore, KS_PASSWORD);
55
56            TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
57            tmf.init(keyStore);
58
59            sslParameters = new SSLParameters(kmf.getKeyManagers(), tmf
60                    .getTrustManagers(), new SecureRandom(),
61                    new SSLSessionContextImpl(), new SSLSessionContextImpl());
62
63            context = SSLContext.getInstance("TLSv1");
64            context.init(kmf.getKeyManagers(),
65                    tmf.getTrustManagers(), new SecureRandom());
66        } catch (Exception e) {
67            e.printStackTrace();
68            initException = e;
69        }
70    }
71
72    public static SSLContext getContext() throws Exception {
73        if (initException != null) {
74            throw initException;
75        }
76        return context;
77    }
78
79    public static SSLParameters getSSLParameters() throws Exception {
80        if (initException != null) {
81            throw initException;
82        }
83        return sslParameters;
84    }
85
86    public static KeyStore getKeyStore() throws Exception {
87        if (initException != null) {
88            throw initException;
89        }
90        return keyStore;
91    }
92}
93