| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import static android.net.IpSecAlgorithm.AUTH_AES_CMAC; |
| 20 |
import static android.net.IpSecAlgorithm.AUTH_AES_XCBC; |
| 21 |
import static android.net.IpSecAlgorithm.AUTH_CRYPT_AES_GCM; |
| 22 |
import static android.net.IpSecAlgorithm.AUTH_CRYPT_CHACHA20_POLY1305; |
| 23 |
import static android.net.IpSecAlgorithm.AUTH_HMAC_SHA256; |
| 24 |
import static android.net.IpSecAlgorithm.AUTH_HMAC_SHA384; |
| 25 |
import static android.net.IpSecAlgorithm.AUTH_HMAC_SHA512; |
| 26 |
import static android.net.IpSecAlgorithm.CRYPT_AES_CBC; |
| 27 |
import static android.net.IpSecAlgorithm.CRYPT_AES_CTR; |
| 28 |
|
| 29 |
import static com.android.internal.annotations.VisibleForTesting.Visibility; |
| 30 |
import static com.android.internal.util.Preconditions.checkStringNotEmpty; |
| 31 |
import static com.android.net.module.util.NetworkStackConstants.IPV6_MIN_MTU; |
| 32 |
|
| 33 |
import android.annotation.NonNull; |
| 34 |
import android.annotation.Nullable; |
| 35 |
import android.annotation.RequiresFeature; |
| 36 |
import android.content.pm.PackageManager; |
| 37 |
import android.net.ipsec.ike.IkeDerAsn1DnIdentification; |
| 38 |
import android.net.ipsec.ike.IkeFqdnIdentification; |
| 39 |
import android.net.ipsec.ike.IkeIdentification; |
| 40 |
import android.net.ipsec.ike.IkeIpv4AddrIdentification; |
| 41 |
import android.net.ipsec.ike.IkeIpv6AddrIdentification; |
| 42 |
import android.net.ipsec.ike.IkeKeyIdIdentification; |
| 43 |
import android.net.ipsec.ike.IkeRfc822AddrIdentification; |
| 44 |
import android.net.ipsec.ike.IkeSessionParams; |
| 45 |
import android.net.ipsec.ike.IkeTunnelConnectionParams; |
| 46 |
import android.security.Credentials; |
| 47 |
import android.util.Log; |
| 48 |
|
| 49 |
import com.android.internal.annotations.VisibleForTesting; |
| 50 |
import com.android.internal.net.VpnProfile; |
| 51 |
|
| 52 |
import java.io.IOException; |
| 53 |
import java.nio.charset.StandardCharsets; |
| 54 |
import java.security.GeneralSecurityException; |
| 55 |
import java.security.Key; |
| 56 |
import java.security.KeyFactory; |
| 57 |
import java.security.KeyStore; |
| 58 |
import java.security.NoSuchAlgorithmException; |
| 59 |
import java.security.PrivateKey; |
| 60 |
import java.security.cert.CertificateEncodingException; |
| 61 |
import java.security.cert.CertificateException; |
| 62 |
import java.security.cert.X509Certificate; |
| 63 |
import java.security.spec.InvalidKeySpecException; |
| 64 |
import java.security.spec.PKCS8EncodedKeySpec; |
| 65 |
import java.util.ArrayList; |
| 66 |
import java.util.Arrays; |
| 67 |
import java.util.Base64; |
| 68 |
import java.util.Collections; |
| 69 |
import java.util.List; |
| 70 |
import java.util.Objects; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public final class Ikev2VpnProfile extends PlatformVpnProfile { |
| 83 |
private static final String TAG = Ikev2VpnProfile.class.getSimpleName(); |
| 84 |
|
| 85 |
public static final String PREFIX_KEYSTORE_ALIAS = "KEYSTORE_ALIAS:"; |
| 86 |
|
| 87 |
public static final String PREFIX_INLINE = "INLINE:"; |
| 88 |
|
| 89 |
private static final String ANDROID_KEYSTORE_PROVIDER = "AndroidKeyStore"; |
| 90 |
private static final String MISSING_PARAM_MSG_TMPL = "Required parameter was not provided: %s"; |
| 91 |
private static final String EMPTY_CERT = ""; |
| 92 |
|
| 93 |
|
| 94 |
public static final List<String> DEFAULT_ALGORITHMS; |
| 95 |
|
| 96 |
private static void addAlgorithmIfSupported(List<String> algorithms, String ipSecAlgoName) { |
| 97 |
if (IpSecAlgorithm.getSupportedAlgorithms().contains(ipSecAlgoName)) { |
| 98 |
algorithms.add(ipSecAlgoName); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static { |
| 103 |
final List<String> algorithms = new ArrayList<>(); |
| 104 |
addAlgorithmIfSupported(algorithms, CRYPT_AES_CBC); |
| 105 |
addAlgorithmIfSupported(algorithms, CRYPT_AES_CTR); |
| 106 |
addAlgorithmIfSupported(algorithms, AUTH_HMAC_SHA256); |
| 107 |
addAlgorithmIfSupported(algorithms, AUTH_HMAC_SHA384); |
| 108 |
addAlgorithmIfSupported(algorithms, AUTH_HMAC_SHA512); |
| 109 |
addAlgorithmIfSupported(algorithms, AUTH_AES_XCBC); |
| 110 |
addAlgorithmIfSupported(algorithms, AUTH_AES_CMAC); |
| 111 |
addAlgorithmIfSupported(algorithms, AUTH_CRYPT_AES_GCM); |
| 112 |
addAlgorithmIfSupported(algorithms, AUTH_CRYPT_CHACHA20_POLY1305); |
| 113 |
|
| 114 |
DEFAULT_ALGORITHMS = Collections.unmodifiableList(algorithms); |
| 115 |
} |
| 116 |
|
| 117 |
@Nullable private final String mServerAddr; |
| 118 |
@Nullable private final String mUserIdentity; |
| 119 |
|
| 120 |
|
| 121 |
@Nullable private final byte[] mPresharedKey; |
| 122 |
|
| 123 |
|
| 124 |
@Nullable private final X509Certificate mServerRootCaCert; |
| 125 |
|
| 126 |
|
| 127 |
@Nullable private final String mUsername; |
| 128 |
@Nullable private final String mPassword; |
| 129 |
|
| 130 |
|
| 131 |
@Nullable private final PrivateKey mRsaPrivateKey; |
| 132 |
@Nullable private final X509Certificate mUserCert; |
| 133 |
|
| 134 |
@Nullable private final ProxyInfo mProxyInfo; |
| 135 |
@NonNull private final List<String> mAllowedAlgorithms; |
| 136 |
private final boolean mIsBypassable; |
| 137 |
private final boolean mIsMetered; |
| 138 |
private final int mMaxMtu; |
| 139 |
private final boolean mIsRestrictedToTestNetworks; |
| 140 |
@Nullable private final IkeTunnelConnectionParams mIkeTunConnParams; |
| 141 |
private final boolean mAutomaticNattKeepaliveTimerEnabled; |
| 142 |
private final boolean mAutomaticIpVersionSelectionEnabled; |
| 143 |
|
| 144 |
private Ikev2VpnProfile( |
| 145 |
int type, |
| 146 |
@Nullable String serverAddr, |
| 147 |
@Nullable String userIdentity, |
| 148 |
@Nullable byte[] presharedKey, |
| 149 |
@Nullable X509Certificate serverRootCaCert, |
| 150 |
@Nullable String username, |
| 151 |
@Nullable String password, |
| 152 |
@Nullable PrivateKey rsaPrivateKey, |
| 153 |
@Nullable X509Certificate userCert, |
| 154 |
@Nullable ProxyInfo proxyInfo, |
| 155 |
@NonNull List<String> allowedAlgorithms, |
| 156 |
boolean isBypassable, |
| 157 |
boolean isMetered, |
| 158 |
int maxMtu, |
| 159 |
boolean restrictToTestNetworks, |
| 160 |
boolean excludeLocalRoutes, |
| 161 |
boolean requiresInternetValidation, |
| 162 |
@Nullable IkeTunnelConnectionParams ikeTunConnParams, |
| 163 |
boolean automaticNattKeepaliveTimerEnabled, |
| 164 |
boolean automaticIpVersionSelectionEnabled) { |
| 165 |
super(type, excludeLocalRoutes, requiresInternetValidation); |
| 166 |
|
| 167 |
checkNotNull(allowedAlgorithms, MISSING_PARAM_MSG_TMPL, "Allowed Algorithms"); |
| 168 |
|
| 169 |
mServerAddr = serverAddr; |
| 170 |
mUserIdentity = userIdentity; |
| 171 |
mPresharedKey = |
| 172 |
presharedKey == null ? null : Arrays.copyOf(presharedKey, presharedKey.length); |
| 173 |
mServerRootCaCert = serverRootCaCert; |
| 174 |
mUsername = username; |
| 175 |
mPassword = password; |
| 176 |
mRsaPrivateKey = rsaPrivateKey; |
| 177 |
mUserCert = userCert; |
| 178 |
mProxyInfo = (proxyInfo == null) ? null : new ProxyInfo(proxyInfo); |
| 179 |
|
| 180 |
|
| 181 |
mAllowedAlgorithms = Collections.unmodifiableList(new ArrayList<>(allowedAlgorithms)); |
| 182 |
if (excludeLocalRoutes && !isBypassable) { |
| 183 |
throw new IllegalArgumentException( |
| 184 |
"Vpn must be bypassable if excludeLocalRoutes is set"); |
| 185 |
} |
| 186 |
|
| 187 |
mIsBypassable = isBypassable; |
| 188 |
mIsMetered = isMetered; |
| 189 |
mMaxMtu = maxMtu; |
| 190 |
mIsRestrictedToTestNetworks = restrictToTestNetworks; |
| 191 |
mIkeTunConnParams = ikeTunConnParams; |
| 192 |
mAutomaticNattKeepaliveTimerEnabled = automaticNattKeepaliveTimerEnabled; |
| 193 |
mAutomaticIpVersionSelectionEnabled = automaticIpVersionSelectionEnabled; |
| 194 |
|
| 195 |
validate(); |
| 196 |
} |
| 197 |
|
| 198 |
private void validate() { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if (mMaxMtu < IPV6_MIN_MTU) { |
| 203 |
throw new IllegalArgumentException("Max MTU must be at least" + IPV6_MIN_MTU); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if (mIkeTunConnParams != null) return; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
checkStringNotEmpty(mServerAddr, MISSING_PARAM_MSG_TMPL, "Server Address"); |
| 213 |
checkStringNotEmpty(mUserIdentity, MISSING_PARAM_MSG_TMPL, "User Identity"); |
| 214 |
|
| 215 |
switch (mType) { |
| 216 |
case TYPE_IKEV2_IPSEC_USER_PASS: |
| 217 |
checkNotNull(mUsername, MISSING_PARAM_MSG_TMPL, "Username"); |
| 218 |
checkNotNull(mPassword, MISSING_PARAM_MSG_TMPL, "Password"); |
| 219 |
|
| 220 |
if (mServerRootCaCert != null) checkCert(mServerRootCaCert); |
| 221 |
|
| 222 |
break; |
| 223 |
case TYPE_IKEV2_IPSEC_PSK: |
| 224 |
checkNotNull(mPresharedKey, MISSING_PARAM_MSG_TMPL, "Preshared Key"); |
| 225 |
break; |
| 226 |
case TYPE_IKEV2_IPSEC_RSA: |
| 227 |
checkNotNull(mUserCert, MISSING_PARAM_MSG_TMPL, "User cert"); |
| 228 |
checkNotNull(mRsaPrivateKey, MISSING_PARAM_MSG_TMPL, "RSA Private key"); |
| 229 |
|
| 230 |
checkCert(mUserCert); |
| 231 |
if (mServerRootCaCert != null) checkCert(mServerRootCaCert); |
| 232 |
|
| 233 |
break; |
| 234 |
default: |
| 235 |
throw new IllegalArgumentException("Invalid auth method set"); |
| 236 |
} |
| 237 |
|
| 238 |
validateAllowedAlgorithms(mAllowedAlgorithms); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private static void validateAllowedAlgorithms(@NonNull List<String> algorithmNames) { |
| 251 |
|
| 252 |
if (algorithmNames.contains(IpSecAlgorithm.AUTH_HMAC_MD5) |
| 253 |
|| algorithmNames.contains(IpSecAlgorithm.AUTH_HMAC_SHA1)) { |
| 254 |
throw new IllegalArgumentException("Algorithm not supported for IKEv2 VPN profiles"); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if (hasAeadAlgorithms(algorithmNames) || hasNormalModeAlgorithms(algorithmNames)) { |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
throw new IllegalArgumentException("Algorithm set missing support for Auth, Crypt or both"); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public static boolean hasAeadAlgorithms(@NonNull List<String> algorithmNames) { |
| 271 |
return algorithmNames.contains(IpSecAlgorithm.AUTH_CRYPT_AES_GCM); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public static boolean hasNormalModeAlgorithms(@NonNull List<String> algorithmNames) { |
| 280 |
final boolean hasCrypt = algorithmNames.contains(IpSecAlgorithm.CRYPT_AES_CBC); |
| 281 |
final boolean hasAuth = algorithmNames.contains(IpSecAlgorithm.AUTH_HMAC_SHA256) |
| 282 |
|| algorithmNames.contains(IpSecAlgorithm.AUTH_HMAC_SHA384) |
| 283 |
|| algorithmNames.contains(IpSecAlgorithm.AUTH_HMAC_SHA512); |
| 284 |
|
| 285 |
return hasCrypt && hasAuth; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
@NonNull |
| 290 |
public String getServerAddr() { |
| 291 |
if (mIkeTunConnParams == null) return mServerAddr; |
| 292 |
|
| 293 |
final IkeSessionParams ikeSessionParams = mIkeTunConnParams.getIkeSessionParams(); |
| 294 |
return ikeSessionParams.getServerHostname(); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
@NonNull |
| 299 |
public String getUserIdentity() { |
| 300 |
if (mIkeTunConnParams == null) return mUserIdentity; |
| 301 |
|
| 302 |
final IkeSessionParams ikeSessionParams = mIkeTunConnParams.getIkeSessionParams(); |
| 303 |
return getUserIdentityFromIkeSession(ikeSessionParams); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
@Nullable |
| 313 |
public byte[] getPresharedKey() { |
| 314 |
if (mIkeTunConnParams != null) return null; |
| 315 |
|
| 316 |
return mPresharedKey == null ? null : Arrays.copyOf(mPresharedKey, mPresharedKey.length); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
@Nullable |
| 327 |
public X509Certificate getServerRootCaCert() { |
| 328 |
if (mIkeTunConnParams != null) return null; |
| 329 |
|
| 330 |
return mServerRootCaCert; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
@Nullable |
| 339 |
public String getUsername() { |
| 340 |
if (mIkeTunConnParams != null) return null; |
| 341 |
|
| 342 |
return mUsername; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
@Nullable |
| 352 |
public String getPassword() { |
| 353 |
if (mIkeTunConnParams != null) return null; |
| 354 |
|
| 355 |
return mPassword; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
@Nullable |
| 365 |
public PrivateKey getRsaPrivateKey() { |
| 366 |
if (mIkeTunConnParams != null) return null; |
| 367 |
|
| 368 |
return mRsaPrivateKey; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
@Nullable |
| 376 |
public X509Certificate getUserCert() { |
| 377 |
if (mIkeTunConnParams != null) return null; |
| 378 |
|
| 379 |
return mUserCert; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
@Nullable |
| 384 |
public ProxyInfo getProxyInfo() { |
| 385 |
return mProxyInfo; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
@NonNull |
| 393 |
public List<String> getAllowedAlgorithms() { |
| 394 |
if (mIkeTunConnParams != null) return new ArrayList<>(); |
| 395 |
|
| 396 |
return mAllowedAlgorithms; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
public boolean isBypassable() { |
| 401 |
return mIsBypassable; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
public boolean isMetered() { |
| 406 |
return mIsMetered; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
public int getMaxMtu() { |
| 411 |
return mMaxMtu; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
@Nullable |
| 416 |
public IkeTunnelConnectionParams getIkeTunnelConnectionParams() { |
| 417 |
return mIkeTunConnParams; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public boolean isRestrictedToTestNetworks() { |
| 426 |
return mIsRestrictedToTestNetworks; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
public boolean isAutomaticNattKeepaliveTimerEnabled() { |
| 431 |
return mAutomaticNattKeepaliveTimerEnabled; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
public boolean isAutomaticIpVersionSelectionEnabled() { |
| 436 |
return mAutomaticIpVersionSelectionEnabled; |
| 437 |
} |
| 438 |
|
| 439 |
@Override |
| 440 |
public int hashCode() { |
| 441 |
return Objects.hash( |
| 442 |
mType, |
| 443 |
mServerAddr, |
| 444 |
mUserIdentity, |
| 445 |
Arrays.hashCode(mPresharedKey), |
| 446 |
mServerRootCaCert, |
| 447 |
mUsername, |
| 448 |
mPassword, |
| 449 |
mRsaPrivateKey, |
| 450 |
mUserCert, |
| 451 |
mProxyInfo, |
| 452 |
mAllowedAlgorithms, |
| 453 |
mIsBypassable, |
| 454 |
mIsMetered, |
| 455 |
mMaxMtu, |
| 456 |
mIsRestrictedToTestNetworks, |
| 457 |
mExcludeLocalRoutes, |
| 458 |
mRequiresInternetValidation, |
| 459 |
mIkeTunConnParams, |
| 460 |
mAutomaticNattKeepaliveTimerEnabled, |
| 461 |
mAutomaticIpVersionSelectionEnabled); |
| 462 |
} |
| 463 |
|
| 464 |
@Override |
| 465 |
public boolean equals(@Nullable Object obj) { |
| 466 |
if (!(obj instanceof Ikev2VpnProfile)) { |
| 467 |
return false; |
| 468 |
} |
| 469 |
|
| 470 |
final Ikev2VpnProfile other = (Ikev2VpnProfile) obj; |
| 471 |
return mType == other.mType |
| 472 |
&& Objects.equals(mServerAddr, other.mServerAddr) |
| 473 |
&& Objects.equals(mUserIdentity, other.mUserIdentity) |
| 474 |
&& Arrays.equals(mPresharedKey, other.mPresharedKey) |
| 475 |
&& Objects.equals(mServerRootCaCert, other.mServerRootCaCert) |
| 476 |
&& Objects.equals(mUsername, other.mUsername) |
| 477 |
&& Objects.equals(mPassword, other.mPassword) |
| 478 |
&& Objects.equals(mRsaPrivateKey, other.mRsaPrivateKey) |
| 479 |
&& Objects.equals(mUserCert, other.mUserCert) |
| 480 |
&& Objects.equals(mProxyInfo, other.mProxyInfo) |
| 481 |
&& Objects.equals(mAllowedAlgorithms, other.mAllowedAlgorithms) |
| 482 |
&& mIsBypassable == other.mIsBypassable |
| 483 |
&& mIsMetered == other.mIsMetered |
| 484 |
&& mMaxMtu == other.mMaxMtu |
| 485 |
&& mIsRestrictedToTestNetworks == other.mIsRestrictedToTestNetworks |
| 486 |
&& mExcludeLocalRoutes == other.mExcludeLocalRoutes |
| 487 |
&& mRequiresInternetValidation == other.mRequiresInternetValidation |
| 488 |
&& Objects.equals(mIkeTunConnParams, other.mIkeTunConnParams) |
| 489 |
&& mAutomaticNattKeepaliveTimerEnabled == other.mAutomaticNattKeepaliveTimerEnabled |
| 490 |
&& mAutomaticIpVersionSelectionEnabled == other.mAutomaticIpVersionSelectionEnabled; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
@NonNull |
| 502 |
public VpnProfile toVpnProfile() throws IOException, GeneralSecurityException { |
| 503 |
final VpnProfile profile = new VpnProfile("" , |
| 504 |
mIsRestrictedToTestNetworks, mExcludeLocalRoutes, mRequiresInternetValidation, |
| 505 |
mIkeTunConnParams, mAutomaticNattKeepaliveTimerEnabled, |
| 506 |
mAutomaticIpVersionSelectionEnabled); |
| 507 |
profile.proxy = mProxyInfo; |
| 508 |
profile.isBypassable = mIsBypassable; |
| 509 |
profile.isMetered = mIsMetered; |
| 510 |
profile.maxMtu = mMaxMtu; |
| 511 |
profile.areAuthParamsInline = true; |
| 512 |
profile.saveLogin = true; |
| 513 |
|
| 514 |
if (mIkeTunConnParams != null) { |
| 515 |
profile.type = VpnProfile.TYPE_IKEV2_FROM_IKE_TUN_CONN_PARAMS; |
| 516 |
return profile; |
| 517 |
} |
| 518 |
|
| 519 |
profile.type = mType; |
| 520 |
profile.server = getServerAddr(); |
| 521 |
profile.ipsecIdentifier = getUserIdentity(); |
| 522 |
profile.setAllowedAlgorithms(mAllowedAlgorithms); |
| 523 |
switch (mType) { |
| 524 |
case TYPE_IKEV2_IPSEC_USER_PASS: |
| 525 |
profile.username = mUsername; |
| 526 |
profile.password = mPassword; |
| 527 |
profile.ipsecCaCert = |
| 528 |
mServerRootCaCert == null ? "" : certificateToPemString(mServerRootCaCert); |
| 529 |
break; |
| 530 |
case TYPE_IKEV2_IPSEC_PSK: |
| 531 |
profile.ipsecSecret = encodeForIpsecSecret(mPresharedKey); |
| 532 |
break; |
| 533 |
case TYPE_IKEV2_IPSEC_RSA: |
| 534 |
profile.ipsecUserCert = certificateToPemString(mUserCert); |
| 535 |
profile.ipsecSecret = |
| 536 |
PREFIX_INLINE + encodeForIpsecSecret(mRsaPrivateKey.getEncoded()); |
| 537 |
profile.ipsecCaCert = |
| 538 |
mServerRootCaCert == null ? "" : certificateToPemString(mServerRootCaCert); |
| 539 |
break; |
| 540 |
default: |
| 541 |
throw new IllegalArgumentException("Invalid auth method set"); |
| 542 |
} |
| 543 |
|
| 544 |
return profile; |
| 545 |
} |
| 546 |
|
| 547 |
private static PrivateKey getPrivateKeyFromAndroidKeystore(String alias) { |
| 548 |
try { |
| 549 |
final KeyStore keystore = KeyStore.getInstance(ANDROID_KEYSTORE_PROVIDER); |
| 550 |
keystore.load(null); |
| 551 |
final Key key = keystore.getKey(alias, null); |
| 552 |
if (!(key instanceof PrivateKey)) { |
| 553 |
throw new IllegalStateException( |
| 554 |
"Unexpected key type returned from android keystore."); |
| 555 |
} |
| 556 |
return (PrivateKey) key; |
| 557 |
} catch (Exception e) { |
| 558 |
throw new IllegalStateException("Failed to load key from android keystore.", e); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
@NonNull |
| 570 |
public static Ikev2VpnProfile fromVpnProfile(@NonNull VpnProfile profile) |
| 571 |
throws GeneralSecurityException { |
| 572 |
final Builder builder; |
| 573 |
if (profile.ikeTunConnParams == null) { |
| 574 |
builder = new Builder(profile.server, profile.ipsecIdentifier); |
| 575 |
builder.setAllowedAlgorithms(profile.getAllowedAlgorithms()); |
| 576 |
|
| 577 |
switch (profile.type) { |
| 578 |
case TYPE_IKEV2_IPSEC_USER_PASS: |
| 579 |
builder.setAuthUsernamePassword( |
| 580 |
profile.username, |
| 581 |
profile.password, |
| 582 |
certificateFromPemString(profile.ipsecCaCert)); |
| 583 |
break; |
| 584 |
case TYPE_IKEV2_IPSEC_PSK: |
| 585 |
builder.setAuthPsk(decodeFromIpsecSecret(profile.ipsecSecret)); |
| 586 |
break; |
| 587 |
case TYPE_IKEV2_IPSEC_RSA: |
| 588 |
final PrivateKey key; |
| 589 |
if (profile.ipsecSecret.startsWith(PREFIX_KEYSTORE_ALIAS)) { |
| 590 |
final String alias = |
| 591 |
profile.ipsecSecret.substring(PREFIX_KEYSTORE_ALIAS.length()); |
| 592 |
key = getPrivateKeyFromAndroidKeystore(alias); |
| 593 |
} else if (profile.ipsecSecret.startsWith(PREFIX_INLINE)) { |
| 594 |
key = getPrivateKey(profile.ipsecSecret.substring(PREFIX_INLINE.length())); |
| 595 |
} else { |
| 596 |
throw new IllegalArgumentException("Invalid RSA private key prefix"); |
| 597 |
} |
| 598 |
|
| 599 |
final X509Certificate userCert = |
| 600 |
certificateFromPemString(profile.ipsecUserCert); |
| 601 |
final X509Certificate serverRootCa = |
| 602 |
certificateFromPemString(profile.ipsecCaCert); |
| 603 |
builder.setAuthDigitalSignature(userCert, key, serverRootCa); |
| 604 |
break; |
| 605 |
default: |
| 606 |
throw new IllegalArgumentException("Invalid auth method set"); |
| 607 |
} |
| 608 |
} else { |
| 609 |
builder = new Builder(profile.ikeTunConnParams); |
| 610 |
} |
| 611 |
|
| 612 |
builder.setProxy(profile.proxy); |
| 613 |
builder.setBypassable(profile.isBypassable); |
| 614 |
builder.setMetered(profile.isMetered); |
| 615 |
builder.setMaxMtu(profile.maxMtu); |
| 616 |
if (profile.isRestrictedToTestNetworks) { |
| 617 |
builder.restrictToTestNetworks(); |
| 618 |
} |
| 619 |
|
| 620 |
if (profile.excludeLocalRoutes && !profile.isBypassable) { |
| 621 |
Log.w(TAG, "ExcludeLocalRoutes should only be set in the bypassable VPN"); |
| 622 |
} |
| 623 |
|
| 624 |
builder.setLocalRoutesExcluded(profile.excludeLocalRoutes && profile.isBypassable); |
| 625 |
builder.setRequiresInternetValidation(profile.requiresInternetValidation); |
| 626 |
|
| 627 |
builder.setAutomaticNattKeepaliveTimerEnabled(profile.automaticNattKeepaliveTimerEnabled); |
| 628 |
builder.setAutomaticIpVersionSelectionEnabled(profile.automaticIpVersionSelectionEnabled); |
| 629 |
|
| 630 |
return builder.build(); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public static boolean isValidVpnProfile(@NonNull VpnProfile profile) { |
| 639 |
if (profile.server.isEmpty() || profile.ipsecIdentifier.isEmpty()) { |
| 640 |
return false; |
| 641 |
} |
| 642 |
|
| 643 |
switch (profile.type) { |
| 644 |
case TYPE_IKEV2_IPSEC_USER_PASS: |
| 645 |
if (profile.username.isEmpty() || profile.password.isEmpty()) { |
| 646 |
return false; |
| 647 |
} |
| 648 |
break; |
| 649 |
case TYPE_IKEV2_IPSEC_PSK: |
| 650 |
if (profile.ipsecSecret.isEmpty()) { |
| 651 |
return false; |
| 652 |
} |
| 653 |
break; |
| 654 |
case TYPE_IKEV2_IPSEC_RSA: |
| 655 |
if (profile.ipsecSecret.isEmpty() || profile.ipsecUserCert.isEmpty()) { |
| 656 |
return false; |
| 657 |
} |
| 658 |
break; |
| 659 |
default: |
| 660 |
return false; |
| 661 |
} |
| 662 |
|
| 663 |
return true; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
@NonNull |
| 674 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 675 |
public static String certificateToPemString(@Nullable X509Certificate cert) |
| 676 |
throws IOException, CertificateEncodingException { |
| 677 |
if (cert == null) { |
| 678 |
return EMPTY_CERT; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
return new String(Credentials.convertToPem(cert), StandardCharsets.US_ASCII); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
@Nullable |
| 691 |
private static X509Certificate certificateFromPemString(@Nullable String certStr) |
| 692 |
throws CertificateException { |
| 693 |
if (certStr == null || EMPTY_CERT.equals(certStr)) { |
| 694 |
return null; |
| 695 |
} |
| 696 |
|
| 697 |
try { |
| 698 |
final List<X509Certificate> certs = |
| 699 |
Credentials.convertFromPem(certStr.getBytes(StandardCharsets.US_ASCII)); |
| 700 |
return certs.isEmpty() ? null : certs.get(0); |
| 701 |
} catch (IOException e) { |
| 702 |
throw new CertificateException(e); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
@NonNull |
| 708 |
public static String encodeForIpsecSecret(@NonNull byte[] secret) { |
| 709 |
checkNotNull(secret, MISSING_PARAM_MSG_TMPL, "secret"); |
| 710 |
|
| 711 |
return Base64.getEncoder().encodeToString(secret); |
| 712 |
} |
| 713 |
|
| 714 |
@NonNull |
| 715 |
private static byte[] decodeFromIpsecSecret(@NonNull String encoded) { |
| 716 |
checkNotNull(encoded, MISSING_PARAM_MSG_TMPL, "encoded"); |
| 717 |
|
| 718 |
return Base64.getDecoder().decode(encoded); |
| 719 |
} |
| 720 |
|
| 721 |
@NonNull |
| 722 |
private static PrivateKey getPrivateKey(@NonNull String keyStr) |
| 723 |
throws InvalidKeySpecException, NoSuchAlgorithmException { |
| 724 |
final PKCS8EncodedKeySpec privateKeySpec = |
| 725 |
new PKCS8EncodedKeySpec(decodeFromIpsecSecret(keyStr)); |
| 726 |
final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 727 |
return keyFactory.generatePrivate(privateKeySpec); |
| 728 |
} |
| 729 |
|
| 730 |
private static void checkCert(@NonNull X509Certificate cert) { |
| 731 |
try { |
| 732 |
certificateToPemString(cert); |
| 733 |
} catch (GeneralSecurityException | IOException e) { |
| 734 |
throw new IllegalArgumentException("Certificate could not be encoded"); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
private static @NonNull <T> T checkNotNull( |
| 739 |
final T reference, final String messageTemplate, final Object... messageArgs) { |
| 740 |
return Objects.requireNonNull(reference, String.format(messageTemplate, messageArgs)); |
| 741 |
} |
| 742 |
|
| 743 |
private static void checkBuilderSetter(boolean constructedFromIkeTunConParams, |
| 744 |
@NonNull String field) { |
| 745 |
if (constructedFromIkeTunConParams) { |
| 746 |
throw new IllegalArgumentException( |
| 747 |
field + " can't be set with IkeTunnelConnectionParams builder"); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
@NonNull |
| 752 |
private static String getUserIdentityFromIkeSession(@NonNull IkeSessionParams params) { |
| 753 |
final IkeIdentification ident = params.getLocalIdentification(); |
| 754 |
|
| 755 |
if (ident instanceof IkeKeyIdIdentification) { |
| 756 |
return "@#" + new String(((IkeKeyIdIdentification) ident).keyId); |
| 757 |
} else if (ident instanceof IkeRfc822AddrIdentification) { |
| 758 |
return "@@" + ((IkeRfc822AddrIdentification) ident).rfc822Name; |
| 759 |
} else if (ident instanceof IkeFqdnIdentification) { |
| 760 |
return "@" + ((IkeFqdnIdentification) ident).fqdn; |
| 761 |
} else if (ident instanceof IkeIpv4AddrIdentification) { |
| 762 |
return ((IkeIpv4AddrIdentification) ident).ipv4Address.getHostAddress(); |
| 763 |
} else if (ident instanceof IkeIpv6AddrIdentification) { |
| 764 |
return ((IkeIpv6AddrIdentification) ident).ipv6Address.getHostAddress(); |
| 765 |
} else if (ident instanceof IkeDerAsn1DnIdentification) { |
| 766 |
throw new IllegalArgumentException("Unspported ASN.1 encoded identities"); |
| 767 |
} else { |
| 768 |
throw new IllegalArgumentException("Unknown IkeIdentification to get user identity"); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
@Override |
| 773 |
public String toString() { |
| 774 |
final StringBuilder sb = new StringBuilder("IkeV2VpnProfile ["); |
| 775 |
sb.append(" MaxMtu=" + mMaxMtu); |
| 776 |
if (mIsBypassable) sb.append(" Bypassable"); |
| 777 |
if (mRequiresInternetValidation) sb.append(" RequiresInternetValidation"); |
| 778 |
if (mIsRestrictedToTestNetworks) sb.append(" RestrictedToTestNetworks"); |
| 779 |
if (mAutomaticNattKeepaliveTimerEnabled) sb.append(" AutomaticNattKeepaliveTimerEnabled"); |
| 780 |
if (mAutomaticIpVersionSelectionEnabled) sb.append(" AutomaticIpVersionSelectionEnabled"); |
| 781 |
sb.append("]"); |
| 782 |
return sb.toString(); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
public static final class Builder { |
| 787 |
private int mType = -1; |
| 788 |
@Nullable private final String mServerAddr; |
| 789 |
@Nullable private final String mUserIdentity; |
| 790 |
|
| 791 |
|
| 792 |
@Nullable private byte[] mPresharedKey; |
| 793 |
|
| 794 |
|
| 795 |
@Nullable private X509Certificate mServerRootCaCert; |
| 796 |
|
| 797 |
|
| 798 |
@Nullable private String mUsername; |
| 799 |
@Nullable private String mPassword; |
| 800 |
|
| 801 |
|
| 802 |
@Nullable private PrivateKey mRsaPrivateKey; |
| 803 |
@Nullable private X509Certificate mUserCert; |
| 804 |
|
| 805 |
@Nullable private ProxyInfo mProxyInfo; |
| 806 |
@NonNull private List<String> mAllowedAlgorithms = DEFAULT_ALGORITHMS; |
| 807 |
private boolean mRequiresInternetValidation = false; |
| 808 |
private boolean mIsBypassable = false; |
| 809 |
private boolean mIsMetered = true; |
| 810 |
private int mMaxMtu = PlatformVpnProfile.MAX_MTU_DEFAULT; |
| 811 |
private boolean mIsRestrictedToTestNetworks = false; |
| 812 |
private boolean mExcludeLocalRoutes = false; |
| 813 |
private boolean mAutomaticNattKeepaliveTimerEnabled = false; |
| 814 |
private boolean mAutomaticIpVersionSelectionEnabled = false; |
| 815 |
@Nullable private final IkeTunnelConnectionParams mIkeTunConnParams; |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 824 |
public Builder(@NonNull String serverAddr, @NonNull String identity) { |
| 825 |
checkNotNull(serverAddr, MISSING_PARAM_MSG_TMPL, "serverAddr"); |
| 826 |
checkNotNull(identity, MISSING_PARAM_MSG_TMPL, "identity"); |
| 827 |
|
| 828 |
mServerAddr = serverAddr; |
| 829 |
mUserIdentity = identity; |
| 830 |
|
| 831 |
mIkeTunConnParams = null; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 841 |
public Builder(@NonNull IkeTunnelConnectionParams ikeTunConnParams) { |
| 842 |
checkNotNull(ikeTunConnParams, MISSING_PARAM_MSG_TMPL, "ikeTunConnParams"); |
| 843 |
|
| 844 |
mIkeTunConnParams = ikeTunConnParams; |
| 845 |
mServerAddr = null; |
| 846 |
mUserIdentity = null; |
| 847 |
} |
| 848 |
|
| 849 |
private void resetAuthParams() { |
| 850 |
mPresharedKey = null; |
| 851 |
mServerRootCaCert = null; |
| 852 |
mUsername = null; |
| 853 |
mPassword = null; |
| 854 |
mRsaPrivateKey = null; |
| 855 |
mUserCert = null; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
@NonNull |
| 878 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 879 |
public Builder setAuthUsernamePassword( |
| 880 |
@NonNull String user, |
| 881 |
@NonNull String pass, |
| 882 |
@Nullable X509Certificate serverRootCa) { |
| 883 |
checkNotNull(user, MISSING_PARAM_MSG_TMPL, "user"); |
| 884 |
checkNotNull(pass, MISSING_PARAM_MSG_TMPL, "pass"); |
| 885 |
checkBuilderSetter(mIkeTunConnParams != null, "authUsernamePassword"); |
| 886 |
|
| 887 |
|
| 888 |
if (serverRootCa != null) checkCert(serverRootCa); |
| 889 |
|
| 890 |
resetAuthParams(); |
| 891 |
mUsername = user; |
| 892 |
mPassword = pass; |
| 893 |
mServerRootCaCert = serverRootCa; |
| 894 |
mType = VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS; |
| 895 |
return this; |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
@NonNull |
| 919 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 920 |
public Builder setAuthDigitalSignature( |
| 921 |
@NonNull X509Certificate userCert, |
| 922 |
@NonNull PrivateKey key, |
| 923 |
@Nullable X509Certificate serverRootCa) { |
| 924 |
checkNotNull(userCert, MISSING_PARAM_MSG_TMPL, "userCert"); |
| 925 |
checkNotNull(key, MISSING_PARAM_MSG_TMPL, "key"); |
| 926 |
checkBuilderSetter(mIkeTunConnParams != null, "authDigitalSignature"); |
| 927 |
|
| 928 |
|
| 929 |
checkCert(userCert); |
| 930 |
if (serverRootCa != null) checkCert(serverRootCa); |
| 931 |
|
| 932 |
resetAuthParams(); |
| 933 |
mUserCert = userCert; |
| 934 |
mRsaPrivateKey = key; |
| 935 |
mServerRootCaCert = serverRootCa; |
| 936 |
mType = VpnProfile.TYPE_IKEV2_IPSEC_RSA; |
| 937 |
return this; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
@NonNull |
| 955 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 956 |
public Builder setAuthPsk(@NonNull byte[] psk) { |
| 957 |
checkNotNull(psk, MISSING_PARAM_MSG_TMPL, "psk"); |
| 958 |
checkBuilderSetter(mIkeTunConnParams != null, "authPsk"); |
| 959 |
|
| 960 |
resetAuthParams(); |
| 961 |
mPresharedKey = psk; |
| 962 |
mType = VpnProfile.TYPE_IKEV2_IPSEC_PSK; |
| 963 |
return this; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
@NonNull |
| 980 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 981 |
public Builder setBypassable(boolean isBypassable) { |
| 982 |
mIsBypassable = isBypassable; |
| 983 |
return this; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
@NonNull |
| 995 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 996 |
public Builder setProxy(@Nullable ProxyInfo proxy) { |
| 997 |
mProxyInfo = proxy; |
| 998 |
return this; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
@NonNull |
| 1012 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1013 |
public Builder setMaxMtu(int mtu) { |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
if (mtu < IPV6_MIN_MTU) { |
| 1018 |
throw new IllegalArgumentException("Max MTU must be at least " + IPV6_MIN_MTU); |
| 1019 |
} |
| 1020 |
mMaxMtu = mtu; |
| 1021 |
return this; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
@NonNull |
| 1042 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1043 |
public Builder setRequiresInternetValidation(boolean requiresInternetValidation) { |
| 1044 |
mRequiresInternetValidation = requiresInternetValidation; |
| 1045 |
return this; |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
@NonNull |
| 1063 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1064 |
public Builder setMetered(boolean isMetered) { |
| 1065 |
mIsMetered = isMetered; |
| 1066 |
return this; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
@NonNull |
| 1095 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1096 |
public Builder setAllowedAlgorithms(@NonNull List<String> algorithmNames) { |
| 1097 |
checkNotNull(algorithmNames, MISSING_PARAM_MSG_TMPL, "algorithmNames"); |
| 1098 |
checkBuilderSetter(mIkeTunConnParams != null, "algorithmNames"); |
| 1099 |
validateAllowedAlgorithms(algorithmNames); |
| 1100 |
|
| 1101 |
mAllowedAlgorithms = algorithmNames; |
| 1102 |
return this; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
@NonNull |
| 1115 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1116 |
public Builder restrictToTestNetworks() { |
| 1117 |
mIsRestrictedToTestNetworks = true; |
| 1118 |
return this; |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
@NonNull |
| 1134 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1135 |
public Builder setAutomaticNattKeepaliveTimerEnabled(boolean isEnabled) { |
| 1136 |
mAutomaticNattKeepaliveTimerEnabled = isEnabled; |
| 1137 |
return this; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
@NonNull |
| 1148 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1149 |
public Builder setAutomaticIpVersionSelectionEnabled(boolean isEnabled) { |
| 1150 |
mAutomaticIpVersionSelectionEnabled = isEnabled; |
| 1151 |
return this; |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
@NonNull |
| 1172 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1173 |
public Builder setLocalRoutesExcluded(boolean excludeLocalRoutes) { |
| 1174 |
mExcludeLocalRoutes = excludeLocalRoutes; |
| 1175 |
return this; |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
@NonNull |
| 1184 |
@RequiresFeature(PackageManager.FEATURE_IPSEC_TUNNELS) |
| 1185 |
public Ikev2VpnProfile build() { |
| 1186 |
return new Ikev2VpnProfile( |
| 1187 |
mType, |
| 1188 |
mServerAddr, |
| 1189 |
mUserIdentity, |
| 1190 |
mPresharedKey, |
| 1191 |
mServerRootCaCert, |
| 1192 |
mUsername, |
| 1193 |
mPassword, |
| 1194 |
mRsaPrivateKey, |
| 1195 |
mUserCert, |
| 1196 |
mProxyInfo, |
| 1197 |
mAllowedAlgorithms, |
| 1198 |
mIsBypassable, |
| 1199 |
mIsMetered, |
| 1200 |
mMaxMtu, |
| 1201 |
mIsRestrictedToTestNetworks, |
| 1202 |
mExcludeLocalRoutes, |
| 1203 |
mRequiresInternetValidation, |
| 1204 |
mIkeTunConnParams, |
| 1205 |
mAutomaticNattKeepaliveTimerEnabled, |
| 1206 |
mAutomaticIpVersionSelectionEnabled); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |