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