• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.vpn2;
18 
19 import 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  */
28 class 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 
55     // Helper fields.
56     boolean saveLogin = false;
57 
VpnProfile(String key)58     VpnProfile(String key) {
59         this.key = key;
60     }
61 
decode(String key, byte[] value)62     static VpnProfile decode(String key, byte[] value) {
63         try {
64             if (key == null) {
65                 return null;
66             }
67 
68             String[] values = new String(value, Charsets.UTF_8).split("\0", -1);
69             // Currently it always has 14 fields.
70             if (values.length < 14) {
71                 return null;
72             }
73 
74             VpnProfile profile = new VpnProfile(key);
75             profile.name = values[0];
76             profile.type = Integer.valueOf(values[1]);
77             if (profile.type < 0 || profile.type > TYPE_MAX) {
78                 return null;
79             }
80             profile.server = values[2];
81             profile.username = values[3];
82             profile.password = values[4];
83             profile.dnsServers = values[5];
84             profile.searchDomains = values[6];
85             profile.routes = values[7];
86             profile.mppe = Boolean.valueOf(values[8]);
87             profile.l2tpSecret = values[9];
88             profile.ipsecIdentifier = values[10];
89             profile.ipsecSecret = values[11];
90             profile.ipsecUserCert = values[12];
91             profile.ipsecCaCert = values[13];
92 
93             profile.saveLogin = !profile.username.isEmpty() || !profile.password.isEmpty();
94             return profile;
95         } catch (Exception e) {
96             // ignore
97         }
98         return null;
99     }
100 
encode()101     byte[] encode() {
102         StringBuilder builder = new StringBuilder(name);
103         builder.append('\0').append(type);
104         builder.append('\0').append(server);
105         builder.append('\0').append(saveLogin ? username : "");
106         builder.append('\0').append(saveLogin ? password : "");
107         builder.append('\0').append(dnsServers);
108         builder.append('\0').append(searchDomains);
109         builder.append('\0').append(routes);
110         builder.append('\0').append(mppe);
111         builder.append('\0').append(l2tpSecret);
112         builder.append('\0').append(ipsecIdentifier);
113         builder.append('\0').append(ipsecSecret);
114         builder.append('\0').append(ipsecUserCert);
115         builder.append('\0').append(ipsecCaCert);
116         return builder.toString().getBytes(Charsets.UTF_8);
117     }
118 }
119