|
| 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.internal.net; |
| 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 | + * @hide |
| 29 | + */ |
| 30 | +public class VpnProfile implements Cloneable { |
| 31 | + // Match these constants with R.array.vpn_types. |
| 32 | + public static final int TYPE_PPTP = 0; |
| 33 | + public static final int TYPE_L2TP_IPSEC_PSK = 1; |
| 34 | + public static final int TYPE_L2TP_IPSEC_RSA = 2; |
| 35 | + public static final int TYPE_IPSEC_XAUTH_PSK = 3; |
| 36 | + public static final int TYPE_IPSEC_XAUTH_RSA = 4; |
| 37 | + public static final int TYPE_IPSEC_HYBRID_RSA = 5; |
| 38 | + public static final int TYPE_MAX = 5; |
| 39 | + |
| 40 | + // Entity fields. |
| 41 | + public final String key; // -1 |
| 42 | + public String name = ""; // 0 |
| 43 | + public int type = TYPE_PPTP; // 1 |
| 44 | + public String server = ""; // 2 |
| 45 | + public String username = ""; // 3 |
| 46 | + public String password = ""; // 4 |
| 47 | + public String dnsServers = ""; // 5 |
| 48 | + public String searchDomains = ""; // 6 |
| 49 | + public String routes = ""; // 7 |
| 50 | + public boolean mppe = true; // 8 |
| 51 | + public String l2tpSecret = ""; // 9 |
| 52 | + public String ipsecIdentifier = "";// 10 |
| 53 | + public String ipsecSecret = ""; // 11 |
| 54 | + public String ipsecUserCert = ""; // 12 |
| 55 | + public String ipsecCaCert = ""; // 13 |
| 56 | + public String ipsecServerCert = "";// 14 |
| 57 | + |
| 58 | + // Helper fields. |
| 59 | + public boolean saveLogin = false; |
| 60 | + |
| 61 | + public VpnProfile(String key) { |
| 62 | + this.key = key; |
| 63 | + } |
| 64 | + |
| 65 | + public static VpnProfile decode(String key, byte[] value) { |
| 66 | + try { |
| 67 | + if (key == null) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + String[] values = new String(value, Charsets.UTF_8).split("\0", -1); |
| 72 | + // There can be 14 or 15 values in ICS MR1. |
| 73 | + if (values.length < 14 || values.length > 15) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + VpnProfile profile = new VpnProfile(key); |
| 78 | + profile.name = values[0]; |
| 79 | + profile.type = Integer.valueOf(values[1]); |
| 80 | + if (profile.type < 0 || profile.type > TYPE_MAX) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + profile.server = values[2]; |
| 84 | + profile.username = values[3]; |
| 85 | + profile.password = values[4]; |
| 86 | + profile.dnsServers = values[5]; |
| 87 | + profile.searchDomains = values[6]; |
| 88 | + profile.routes = values[7]; |
| 89 | + profile.mppe = Boolean.valueOf(values[8]); |
| 90 | + profile.l2tpSecret = values[9]; |
| 91 | + profile.ipsecIdentifier = values[10]; |
| 92 | + profile.ipsecSecret = values[11]; |
| 93 | + profile.ipsecUserCert = values[12]; |
| 94 | + profile.ipsecCaCert = values[13]; |
| 95 | + profile.ipsecServerCert = (values.length > 14) ? values[14] : ""; |
| 96 | + |
| 97 | + profile.saveLogin = !profile.username.isEmpty() || !profile.password.isEmpty(); |
| 98 | + return profile; |
| 99 | + } catch (Exception e) { |
| 100 | + // ignore |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + public byte[] encode() { |
| 106 | + StringBuilder builder = new StringBuilder(name); |
| 107 | + builder.append('\0').append(type); |
| 108 | + builder.append('\0').append(server); |
| 109 | + builder.append('\0').append(saveLogin ? username : ""); |
| 110 | + builder.append('\0').append(saveLogin ? password : ""); |
| 111 | + builder.append('\0').append(dnsServers); |
| 112 | + builder.append('\0').append(searchDomains); |
| 113 | + builder.append('\0').append(routes); |
| 114 | + builder.append('\0').append(mppe); |
| 115 | + builder.append('\0').append(l2tpSecret); |
| 116 | + builder.append('\0').append(ipsecIdentifier); |
| 117 | + builder.append('\0').append(ipsecSecret); |
| 118 | + builder.append('\0').append(ipsecUserCert); |
| 119 | + builder.append('\0').append(ipsecCaCert); |
| 120 | + builder.append('\0').append(ipsecServerCert); |
| 121 | + return builder.toString().getBytes(Charsets.UTF_8); |
| 122 | + } |
| 123 | +} |
0 commit comments