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 import android.os.Handler;
22 
23 import com.android.server.wifi.Clock;
24 import com.android.server.wifi.WifiCarrierInfoManager;
25 import com.android.server.wifi.WifiInjector;
26 import com.android.server.wifi.WifiKeyStore;
27 import com.android.server.wifi.WifiMetrics;
28 import com.android.server.wifi.WifiNative;
29 
30 import java.security.KeyStore;
31 import java.security.KeyStoreException;
32 import java.security.NoSuchAlgorithmException;
33 
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.TrustManagerFactory;
36 
37 /**
38  * Factory class for creating Passpoint related objects. Useful for mocking object creations
39  * in the unit tests.
40  */
41 public class PasspointObjectFactory{
42     /**
43      * Create a PasspointEventHandler instance.
44      *
45      * @param wifiNative Instance of {@link WifiNative}
46      * @param callbacks Instance of {@link PasspointEventHandler.Callbacks}
47      * @return {@link PasspointEventHandler}
48      */
makePasspointEventHandler(WifiInjector wifiInjector, PasspointEventHandler.Callbacks callbacks)49     public PasspointEventHandler makePasspointEventHandler(WifiInjector wifiInjector,
50             PasspointEventHandler.Callbacks callbacks) {
51         return new PasspointEventHandler(wifiInjector, callbacks);
52     }
53 
54     /**
55      * Create a PasspointProvider instance.
56      *
57      * @param config Configuration for the provider
58      * @param keyStore Instance of {@link WifiKeyStore}
59      * @param wifiCarrierInfoManager Instance of {@link WifiCarrierInfoManager}
60      * @param providerId Unique identifier for the provider
61      * @param creatorUid Creator UID
62      * @param packageName Package name of app adding/updating the {@code config}
63      * @param isFromSuggestion True if originated from a suggestion
64      * @param clock Instance of {@link Clock}
65      * @return {@link PasspointProvider}
66      */
makePasspointProvider(PasspointConfiguration config, WifiKeyStore keyStore, WifiCarrierInfoManager wifiCarrierInfoManager, long providerId, int creatorUid, String packageName, boolean isFromSuggestion, Clock clock)67     public PasspointProvider makePasspointProvider(PasspointConfiguration config,
68             WifiKeyStore keyStore, WifiCarrierInfoManager wifiCarrierInfoManager, long providerId,
69             int creatorUid, String packageName, boolean isFromSuggestion, Clock clock) {
70         return new PasspointProvider(config, keyStore, wifiCarrierInfoManager, providerId,
71                 creatorUid, packageName, isFromSuggestion, clock);
72     }
73 
74     /**
75      * Create a {@link PasspointConfigUserStoreData} instance.
76      *
77      * @param keyStore Instance of {@link WifiKeyStore}
78      * @param wifiCarrierInfoManager Instance of {@link WifiCarrierInfoManager}
79      * @param dataSource Passpoint configuration data source
80      * @param clock Instance of {@link Clock}
81      * @return {@link PasspointConfigUserStoreData}
82      */
makePasspointConfigUserStoreData(WifiKeyStore keyStore, WifiCarrierInfoManager wifiCarrierInfoManager, PasspointConfigUserStoreData.DataSource dataSource, Clock clock)83     public PasspointConfigUserStoreData makePasspointConfigUserStoreData(WifiKeyStore keyStore,
84             WifiCarrierInfoManager wifiCarrierInfoManager,
85             PasspointConfigUserStoreData.DataSource dataSource, Clock clock) {
86         return new PasspointConfigUserStoreData(keyStore, wifiCarrierInfoManager, dataSource,
87                 clock);
88     }
89 
90     /**
91      * Create a {@link PasspointConfigSharedStoreData} instance.
92      * @param dataSource Passpoint configuration data source
93      * @return {@link PasspointConfigSharedStoreData}
94      */
makePasspointConfigSharedStoreData( PasspointConfigSharedStoreData.DataSource dataSource)95     public PasspointConfigSharedStoreData makePasspointConfigSharedStoreData(
96             PasspointConfigSharedStoreData.DataSource dataSource) {
97         return new PasspointConfigSharedStoreData(dataSource);
98     }
99 
100     /**
101      * Create a AnqpCache instance.
102      *
103      * @param clock Instance of {@link Clock}
104      * @return {@link AnqpCache}
105      */
makeAnqpCache(Clock clock)106     public AnqpCache makeAnqpCache(Clock clock) {
107         return new AnqpCache(clock);
108     }
109 
110     /**
111      * Create an instance of {@link ANQPRequestManager}.
112      *
113      * @param passpointEventHandler Instance of {@link PasspointEventHandler}
114      * @param clock                 Instance of {@link Clock}
115      * @return {@link ANQPRequestManager}
116      */
makeANQPRequestManager(PasspointEventHandler passpointEventHandler, Clock clock, WifiInjector wifiInjector, Handler handler)117     public ANQPRequestManager makeANQPRequestManager(PasspointEventHandler passpointEventHandler,
118             Clock clock, WifiInjector wifiInjector, Handler handler) {
119         return new ANQPRequestManager(passpointEventHandler, clock, wifiInjector, handler);
120     }
121 
122     /**
123      * Create an instance of {@link PasspointProvisioner}.
124      *
125      * @param context Instance of {@link Context}
126      * @param wifiNative Instance of {@link WifiNative}
127      * @param passpointManager Instance of {@link PasspointManager}
128      * @return {@link PasspointProvisioner}
129      */
makePasspointProvisioner(Context context, WifiNative wifiNative, PasspointManager passpointManager, WifiMetrics wifiMetrics)130     public PasspointProvisioner makePasspointProvisioner(Context context, WifiNative wifiNative,
131             PasspointManager passpointManager, WifiMetrics wifiMetrics) {
132         return new PasspointProvisioner(context, wifiNative, this, passpointManager, wifiMetrics);
133     }
134 
135     /**
136      * Create an instance of {@link OsuNetworkConnection}.
137      *
138      * @param context
139      * @return {@link OsuNetworkConnection}
140      */
makeOsuNetworkConnection(Context context)141     public OsuNetworkConnection makeOsuNetworkConnection(Context context) {
142         return new OsuNetworkConnection(context);
143     }
144 
145     /**
146      * Create an instance of {@link OsuServerConnection}.
147      *
148      * @return {@link OsuServerConnection}
149      */
makeOsuServerConnection()150     public OsuServerConnection makeOsuServerConnection() {
151         return new OsuServerConnection(null);
152     }
153 
154 
155     /**
156      * Create an instance of {@link WfaKeyStore}.
157      *
158      * @return WfaKeyStore {@link WfaKeyStore}
159      */
makeWfaKeyStore()160     public WfaKeyStore makeWfaKeyStore() {
161         return new WfaKeyStore();
162     }
163 
164     /**
165      * Create an instance of {@link SSLContext}.
166      *
167      * @param tlsVersion String indicate TLS version
168      * @return SSLContext an instance, corresponding to the TLS version
169      */
getSSLContext(String tlsVersion)170     public SSLContext getSSLContext(String tlsVersion) {
171         SSLContext tlsContext = null;
172         try {
173             tlsContext = SSLContext.getInstance(tlsVersion);
174         } catch (NoSuchAlgorithmException e) {
175             e.printStackTrace();
176         }
177         return tlsContext;
178     }
179 
180     /**
181      * Create an instance of {@link TrustManagerFactory}.
182      *
183      * @param ks KeyStore used to get root certs
184      * @return TrustManagerFactory an instance for root cert validation
185      */
getTrustManagerFactory(KeyStore ks)186     public TrustManagerFactory getTrustManagerFactory(KeyStore ks) {
187         try {
188             TrustManagerFactory trustManagerFactory = TrustManagerFactory
189                     .getInstance(TrustManagerFactory.getDefaultAlgorithm());
190             trustManagerFactory.init(ks);
191             return trustManagerFactory;
192         } catch (NoSuchAlgorithmException | KeyStoreException e) {
193             return null;
194         }
195     }
196 
197     /**
198      * Create an instance of {@link SystemInfo}.
199      *
200      * @param context Instance of {@link Context}
201      * @param wifiNative Instance of {@link WifiNative}
202      * @return {@Link Systeminfo} that is used for getting system related info.
203      */
getSystemInfo(Context context, WifiNative wifiNative)204     public SystemInfo getSystemInfo(Context context, WifiNative wifiNative) {
205         return SystemInfo.getInstance(context, wifiNative);
206     }
207 }
208