1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.json.JSONObject; 23 24 import com.android.internal.net.LegacyVpnInfo; 25 import com.android.internal.net.VpnConfig; 26 import com.android.internal.net.VpnProfile; 27 import com.android.internal.util.ArrayUtils; 28 import com.google.android.collect.Lists; 29 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 30 import com.googlecode.android_scripting.rpc.Rpc; 31 import com.googlecode.android_scripting.rpc.RpcParameter; 32 33 import android.app.Service; 34 import android.content.Context; 35 import android.net.IConnectivityManager; 36 import android.os.RemoteException; 37 import android.os.ServiceManager; 38 import android.security.Credentials; 39 import android.security.KeyStore; 40 import com.googlecode.android_scripting.facade.CertInstallerHelper; 41 42 /** 43 * Access NFC functions. 44 */ 45 public class VpnFacade extends RpcReceiver { 46 47 private final Service mService; 48 private final IConnectivityManager mConService; 49 private CertInstallerHelper mCertHelper; 50 VpnFacade(FacadeManager manager)51 public VpnFacade(FacadeManager manager) { 52 super(manager); 53 mService = manager.getService(); 54 mCertHelper = new CertInstallerHelper(); 55 mConService = IConnectivityManager.Stub 56 .asInterface(ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); 57 } 58 loadVpnProfiles(KeyStore keyStore, int... excludeTypes)59 static List<VpnProfile> loadVpnProfiles(KeyStore keyStore, int... excludeTypes) { 60 final ArrayList<VpnProfile> result = Lists.newArrayList(); 61 62 for (String key : keyStore.list(Credentials.VPN)) { 63 final VpnProfile profile = VpnProfile.decode(key, keyStore.get(Credentials.VPN + key)); 64 if (profile != null && !ArrayUtils.contains(excludeTypes, profile.type)) { 65 result.add(profile); 66 } 67 } 68 return result; 69 } 70 genLegacyVpnProfile(JSONObject vpnProfileJson)71 private VpnProfile genLegacyVpnProfile(JSONObject vpnProfileJson) { 72 VpnProfile vp = new VpnProfile(vpnProfileJson.optString("key", "")); 73 vp.name = vpnProfileJson.optString("name", ""); 74 vp.type = vpnProfileJson.optInt("type", VpnProfile.TYPE_PPTP); 75 vp.server = vpnProfileJson.optString("server", ""); 76 vp.username = vpnProfileJson.optString("username", ""); 77 vp.password = vpnProfileJson.optString("password", ""); 78 vp.dnsServers = vpnProfileJson.optString("dnsServers", ""); 79 vp.searchDomains = vpnProfileJson.optString("searchDomains", ""); 80 vp.routes = vpnProfileJson.optString("routes", ""); 81 vp.mppe = vpnProfileJson.optBoolean("mppe", true); 82 vp.l2tpSecret = vpnProfileJson.optString("l2tpSecret", ""); 83 vp.ipsecIdentifier = vpnProfileJson.optString("ipsecIdentifier", ""); 84 vp.ipsecSecret = vpnProfileJson.optString("ipsecSecret", ""); 85 vp.ipsecUserCert = vpnProfileJson.optString("ipsecUserCert", ""); 86 vp.ipsecCaCert = vpnProfileJson.optString("ipsecCaCert", ""); 87 vp.ipsecServerCert = vpnProfileJson.optString("ipsecServerCert", ""); 88 vp.saveLogin = vpnProfileJson.optBoolean("saveLogin", false); 89 return vp; 90 } 91 92 @Rpc(description = "Start legacy VPN with a profile.") vpnStartLegacyVpn(@pcParametername = "vpnProfile") JSONObject vpnProfile)93 public void vpnStartLegacyVpn(@RpcParameter(name = "vpnProfile") JSONObject vpnProfile) 94 throws RemoteException { 95 VpnProfile profile = genLegacyVpnProfile(vpnProfile); 96 mConService.startLegacyVpn(profile); 97 } 98 99 @Rpc(description = "Stop the current legacy VPN connection.") vpnStopLegacyVpn()100 public void vpnStopLegacyVpn() throws RemoteException { 101 mConService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, mService.getUserId()); 102 } 103 104 @Rpc(description = "Get the info object of the currently active legacy VPN connection.") vpnGetLegacyVpnInfo()105 public LegacyVpnInfo vpnGetLegacyVpnInfo() throws RemoteException { 106 return mConService.getLegacyVpnInfo(mService.getUserId()); 107 } 108 109 @Override shutdown()110 public void shutdown() { 111 } 112 113 @Rpc(description = "Install certificate for RSA VPNs.") installCertificate(@pcParametername = "vpnProfile") JSONObject vpnProfile, @RpcParameter(name = "certFile") String certFile, @RpcParameter(name = "password") String password)114 public void installCertificate(@RpcParameter(name = "vpnProfile") JSONObject vpnProfile, 115 @RpcParameter(name = "certFile") String certFile, 116 @RpcParameter(name = "password") String password) 117 throws RemoteException { 118 VpnProfile profile = genLegacyVpnProfile(vpnProfile); 119 mCertHelper.installCertificate(profile, certFile, password); 120 } 121 } 122