1/*
2 * Copyright (C) 2011 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
17package com.android.settings.vpn2;
18
19import java.nio.charset.Charsets;
20
21/**
22 * Parcel-like entity class for VPN profiles. To keep things simple, all
23 * fields are package private. Methods are provided for serialization, so
24 * storage can be implemented easily. Two rules are set for this class.
25 * First, all fields must be kept non-null. Second, always make a copy
26 * using clone() before modifying.
27 */
28class VpnProfile implements Cloneable {
29    // Match these constants with R.array.vpn_types.
30    static final int TYPE_PPTP = 0;
31    static final int TYPE_L2TP_IPSEC_PSK = 1;
32    static final int TYPE_L2TP_IPSEC_RSA = 2;
33    static final int TYPE_IPSEC_XAUTH_PSK = 3;
34    static final int TYPE_IPSEC_XAUTH_RSA = 4;
35    static final int TYPE_IPSEC_HYBRID_RSA = 5;
36    static final int TYPE_MAX = 5;
37
38    // Entity fields.
39    final String key;           // -1
40    String name = "";           // 0
41    int type = TYPE_PPTP;       // 1
42    String server = "";         // 2
43    String username = "";       // 3
44    String password = "";       // 4
45    String dnsServers = "";     // 5
46    String searchDomains = "";  // 6
47    String routes = "";         // 7
48    boolean mppe = true;        // 8
49    String l2tpSecret = "";     // 9
50    String ipsecIdentifier = "";// 10
51    String ipsecSecret = "";    // 11
52    String ipsecUserCert = "";  // 12
53    String ipsecCaCert = "";    // 13
54    String ipsecServerCert = "";// 14
55
56    // Helper fields.
57    boolean saveLogin = false;
58
59    VpnProfile(String key) {
60        this.key = key;
61    }
62
63    static VpnProfile decode(String key, byte[] value) {
64        try {
65            if (key == null) {
66                return null;
67            }
68
69            String[] values = new String(value, Charsets.UTF_8).split("\0", -1);
70            // There can be 14 or 15 values in ICS MR1.
71            if (values.length < 14 || values.length > 15) {
72                return null;
73            }
74
75            VpnProfile profile = new VpnProfile(key);
76            profile.name = values[0];
77            profile.type = Integer.valueOf(values[1]);
78            if (profile.type < 0 || profile.type > TYPE_MAX) {
79                return null;
80            }
81            profile.server = values[2];
82            profile.username = values[3];
83            profile.password = values[4];
84            profile.dnsServers = values[5];
85            profile.searchDomains = values[6];
86            profile.routes = values[7];
87            profile.mppe = Boolean.valueOf(values[8]);
88            profile.l2tpSecret = values[9];
89            profile.ipsecIdentifier = values[10];
90            profile.ipsecSecret = values[11];
91            profile.ipsecUserCert = values[12];
92            profile.ipsecCaCert = values[13];
93            profile.ipsecServerCert = (values.length > 14) ? values[14] : "";
94
95            profile.saveLogin = !profile.username.isEmpty() || !profile.password.isEmpty();
96            return profile;
97        } catch (Exception e) {
98            // ignore
99        }
100        return null;
101    }
102
103    byte[] encode() {
104        StringBuilder builder = new StringBuilder(name);
105        builder.append('\0').append(type);
106        builder.append('\0').append(server);
107        builder.append('\0').append(saveLogin ? username : "");
108        builder.append('\0').append(saveLogin ? password : "");
109        builder.append('\0').append(dnsServers);
110        builder.append('\0').append(searchDomains);
111        builder.append('\0').append(routes);
112        builder.append('\0').append(mppe);
113        builder.append('\0').append(l2tpSecret);
114        builder.append('\0').append(ipsecIdentifier);
115        builder.append('\0').append(ipsecSecret);
116        builder.append('\0').append(ipsecUserCert);
117        builder.append('\0').append(ipsecCaCert);
118        builder.append('\0').append(ipsecServerCert);
119        return builder.toString().getBytes(Charsets.UTF_8);
120    }
121}
122