1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi.hotspot2;
18 
19 import android.content.Context;
20 import android.net.wifi.hotspot2.PasspointConfiguration;
21 
22 import com.android.server.wifi.Clock;
23 import com.android.server.wifi.WifiCarrierInfoManager;
24 import com.android.server.wifi.WifiKeyStore;
25 import com.android.server.wifi.WifiMetrics;
26 import com.android.server.wifi.WifiNative;
27 
28 import java.security.KeyStore;
29 import java.security.KeyStoreException;
30 import java.security.NoSuchAlgorithmException;
31 
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManagerFactory;
34 
35 /**
36  * Factory class for creating Passpoint related objects. Useful for mocking object creations
37  * in the unit tests.
38  */
39 public class PasspointObjectFactory{
40     /**
41      * Create a PasspointEventHandler instance.
42      *
43      * @param wifiNative Instance of {@link WifiNative}
44      * @param callbacks Instance of {@link PasspointEventHandler.Callbacks}
45      * @return {@link PasspointEventHandler}
46      */
makePasspointEventHandler(WifiNative wifiNative, PasspointEventHandler.Callbacks callbacks)47     public PasspointEventHandler makePasspointEventHandler(WifiNative wifiNative,
48             PasspointEventHandler.Callbacks callbacks) {
49         return new PasspointEventHandler(wifiNative, callbacks);
50     }
51 
52     /**
53      * Create a PasspointProvider instance.
54      *
55      * @param keyStore Instance of {@link WifiKeyStore}
56      * @param config Configuration for the provider
57      * @param providerId Unique identifier for the provider
58      * @param packageName Package name of app adding/updating the {@code config}
59      * @return {@link PasspointProvider}
60      */
makePasspointProvider(PasspointConfiguration config, WifiKeyStore keyStore, WifiCarrierInfoManager wifiCarrierInfoManager, long providerId, int creatorUid, String packageName, boolean isFromSuggestion)61     public PasspointProvider makePasspointProvider(PasspointConfiguration config,
62             WifiKeyStore keyStore, WifiCarrierInfoManager wifiCarrierInfoManager, long providerId,
63             int creatorUid, String packageName, boolean isFromSuggestion) {
64         return new PasspointProvider(config, keyStore, wifiCarrierInfoManager, providerId,
65                 creatorUid, packageName, isFromSuggestion);
66     }
67 
68     /**
69      * Create a {@link PasspointConfigUserStoreData} instance.
70      *
71      * @param keyStore Instance of {@link WifiKeyStore}
72      * @param wifiCarrierInfoManager Instance of {@link WifiCarrierInfoManager}
73      * @param dataSource Passpoint configuration data source
74      * @return {@link PasspointConfigUserStoreData}
75      */
makePasspointConfigUserStoreData(WifiKeyStore keyStore, WifiCarrierInfoManager wifiCarrierInfoManager, PasspointConfigUserStoreData.DataSource dataSource)76     public PasspointConfigUserStoreData makePasspointConfigUserStoreData(WifiKeyStore keyStore,
77             WifiCarrierInfoManager wifiCarrierInfoManager,
78             PasspointConfigUserStoreData.DataSource dataSource) {
79         return new PasspointConfigUserStoreData(keyStore, wifiCarrierInfoManager, dataSource);
80     }
81 
82     /**
83      * Create a {@link PasspointConfigSharedStoreData} instance.
84      * @param dataSource Passpoint configuration data source
85      * @return {@link PasspointConfigSharedStoreData}
86      */
makePasspointConfigSharedStoreData( PasspointConfigSharedStoreData.DataSource dataSource)87     public PasspointConfigSharedStoreData makePasspointConfigSharedStoreData(
88             PasspointConfigSharedStoreData.DataSource dataSource) {
89         return new PasspointConfigSharedStoreData(dataSource);
90     }
91 
92     /**
93      * Create a AnqpCache instance.
94      *
95      * @param clock Instance of {@link Clock}
96      * @return {@link AnqpCache}
97      */
makeAnqpCache(Clock clock)98     public AnqpCache makeAnqpCache(Clock clock) {
99         return new AnqpCache(clock);
100     }
101 
102     /**
103      * Create an instance of {@link ANQPRequestManager}.
104      *
105      * @param handler Instance of {@link PasspointEventHandler}
106      * @param clock Instance of {@link Clock}
107      * @return {@link ANQPRequestManager}
108      */
makeANQPRequestManager(PasspointEventHandler handler, Clock clock)109     public ANQPRequestManager makeANQPRequestManager(PasspointEventHandler handler, Clock clock) {
110         return new ANQPRequestManager(handler, clock);
111     }
112 
113     /**
114      * Create an instance of {@link PasspointProvisioner}.
115      *
116      * @param context Instance of {@link Context}
117      * @param wifiNative Instance of {@link WifiNative}
118      * @param passpointManager Instance of {@link PasspointManager}
119      * @return {@link PasspointProvisioner}
120      */
makePasspointProvisioner(Context context, WifiNative wifiNative, PasspointManager passpointManager, WifiMetrics wifiMetrics)121     public PasspointProvisioner makePasspointProvisioner(Context context, WifiNative wifiNative,
122             PasspointManager passpointManager, WifiMetrics wifiMetrics) {
123         return new PasspointProvisioner(context, wifiNative, this, passpointManager, wifiMetrics);
124     }
125 
126     /**
127      * Create an instance of {@link OsuNetworkConnection}.
128      *
129      * @param context
130      * @return {@link OsuNetworkConnection}
131      */
makeOsuNetworkConnection(Context context)132     public OsuNetworkConnection makeOsuNetworkConnection(Context context) {
133         return new OsuNetworkConnection(context);
134     }
135 
136     /**
137      * Create an instance of {@link OsuServerConnection}.
138      *
139      * @return {@link OsuServerConnection}
140      */
makeOsuServerConnection()141     public OsuServerConnection makeOsuServerConnection() {
142         return new OsuServerConnection(null);
143     }
144 
145 
146     /**
147      * Create an instance of {@link WfaKeyStore}.
148      *
149      * @return WfaKeyStore {@link WfaKeyStore}
150      */
makeWfaKeyStore()151     public WfaKeyStore makeWfaKeyStore() {
152         return new WfaKeyStore();
153     }
154 
155     /**
156      * Create an instance of {@link SSLContext}.
157      *
158      * @param tlsVersion String indicate TLS version
159      * @return SSLContext an instance, corresponding to the TLS version
160      */
getSSLContext(String tlsVersion)161     public SSLContext getSSLContext(String tlsVersion) {
162         SSLContext tlsContext = null;
163         try {
164             tlsContext = SSLContext.getInstance(tlsVersion);
165         } catch (NoSuchAlgorithmException e) {
166             e.printStackTrace();
167         }
168         return tlsContext;
169     }
170 
171     /**
172      * Create an instance of {@link TrustManagerFactory}.
173      *
174      * @param ks KeyStore used to get root certs
175      * @return TrustManagerFactory an instance for root cert validation
176      */
getTrustManagerFactory(KeyStore ks)177     public TrustManagerFactory getTrustManagerFactory(KeyStore ks) {
178         try {
179             TrustManagerFactory trustManagerFactory = TrustManagerFactory
180                     .getInstance(TrustManagerFactory.getDefaultAlgorithm());
181             trustManagerFactory.init(ks);
182             return trustManagerFactory;
183         } catch (NoSuchAlgorithmException | KeyStoreException e) {
184             return null;
185         }
186     }
187 
188     /**
189      * Create an instance of {@link SystemInfo}.
190      *
191      * @param context Instance of {@link Context}
192      * @param wifiNative Instance of {@link WifiNative}
193      * @return {@Link Systeminfo} that is used for getting system related info.
194      */
getSystemInfo(Context context, WifiNative wifiNative)195     public SystemInfo getSystemInfo(Context context, WifiNative wifiNative) {
196         return SystemInfo.getInstance(context, wifiNative);
197     }
198 }
199