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 */
16package com.android.settings.vpn2;
17
18import android.content.Context;
19import android.net.ConnectivityManager;
20import android.security.Credentials;
21import android.security.KeyStore;
22
23/**
24 * Utility functions for vpn.
25 *
26 * Keystore methods should only be called in system user
27 */
28public class VpnUtils {
29
30    public static String getLockdownVpn() {
31        final byte[] value = KeyStore.getInstance().get(Credentials.LOCKDOWN_VPN);
32        return value == null ? null : new String(value);
33    }
34
35    public static void clearLockdownVpn(Context context) {
36        KeyStore.getInstance().delete(Credentials.LOCKDOWN_VPN);
37        // Always notify ConnectivityManager after keystore update
38        context.getSystemService(ConnectivityManager.class).updateLockdownVpn();
39    }
40
41    public static void setLockdownVpn(Context context, String lockdownKey) {
42        KeyStore.getInstance().put(Credentials.LOCKDOWN_VPN, lockdownKey.getBytes(),
43                KeyStore.UID_SELF, /* flags */ 0);
44        // Always notify ConnectivityManager after keystore update
45        context.getSystemService(ConnectivityManager.class).updateLockdownVpn();
46    }
47
48    public static boolean isVpnLockdown(String key) {
49        return key.equals(getLockdownVpn());
50    }
51}
52