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.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