1 /*
2  * Copyright (C) 2019 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 android.net;
18 
19 import static android.net.PlatformVpnProfile.TYPE_IKEV2_IPSEC_PSK;
20 import static android.net.PlatformVpnProfile.TYPE_IKEV2_IPSEC_RSA;
21 import static android.net.PlatformVpnProfile.TYPE_IKEV2_IPSEC_USER_PASS;
22 
23 import android.annotation.IntDef;
24 import android.annotation.NonNull;
25 
26 import com.android.internal.net.VpnProfile;
27 
28 import java.io.IOException;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.security.GeneralSecurityException;
32 
33 /**
34  * PlatformVpnProfile represents a configuration for a platform-based VPN implementation.
35  *
36  * <p>Platform-based VPNs allow VPN applications to provide configuration and authentication options
37  * to leverage the Android OS' implementations of well-defined control plane (authentication, key
38  * negotiation) and data plane (per-packet encryption) protocols to simplify the creation of VPN
39  * tunnels. In contrast, {@link VpnService} based VPNs must implement both the control and data
40  * planes on a per-app basis.
41  *
42  * @see Ikev2VpnProfile
43  */
44 public abstract class PlatformVpnProfile {
45     /**
46      * Alias to platform VPN related types from VpnProfile, for API use.
47      *
48      * @hide
49      */
50     @Retention(RetentionPolicy.SOURCE)
51     @IntDef({
52         TYPE_IKEV2_IPSEC_USER_PASS,
53         TYPE_IKEV2_IPSEC_PSK,
54         TYPE_IKEV2_IPSEC_RSA,
55     })
56     public static @interface PlatformVpnType {}
57 
58     public static final int TYPE_IKEV2_IPSEC_USER_PASS = VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS;
59     public static final int TYPE_IKEV2_IPSEC_PSK = VpnProfile.TYPE_IKEV2_IPSEC_PSK;
60     public static final int TYPE_IKEV2_IPSEC_RSA = VpnProfile.TYPE_IKEV2_IPSEC_RSA;
61 
62     /** @hide */
63     public static final int MAX_MTU_DEFAULT = 1360;
64 
65     /** @hide */
66     @PlatformVpnType protected final int mType;
67 
68     /** @hide */
PlatformVpnProfile(@latformVpnType int type)69     PlatformVpnProfile(@PlatformVpnType int type) {
70         mType = type;
71     }
72     /** Returns the profile integer type. */
73     @PlatformVpnType
getType()74     public final int getType() {
75         return mType;
76     }
77 
78     /** Returns a type string describing the VPN profile type */
79     @NonNull
getTypeString()80     public final String getTypeString() {
81         switch (mType) {
82             case TYPE_IKEV2_IPSEC_USER_PASS:
83                 return "IKEv2/IPsec Username/Password";
84             case TYPE_IKEV2_IPSEC_PSK:
85                 return "IKEv2/IPsec Preshared key";
86             case TYPE_IKEV2_IPSEC_RSA:
87                 return "IKEv2/IPsec RSA Digital Signature";
88             default:
89                 return "Unknown VPN profile type";
90         }
91     }
92 
93     /** @hide */
94     @NonNull
toVpnProfile()95     public abstract VpnProfile toVpnProfile() throws IOException, GeneralSecurityException;
96 
97     /** @hide */
98     @NonNull
fromVpnProfile(@onNull VpnProfile profile)99     public static PlatformVpnProfile fromVpnProfile(@NonNull VpnProfile profile)
100             throws IOException, GeneralSecurityException {
101         switch (profile.type) {
102             case TYPE_IKEV2_IPSEC_USER_PASS: // fallthrough
103             case TYPE_IKEV2_IPSEC_PSK: // fallthrough
104             case TYPE_IKEV2_IPSEC_RSA:
105                 return Ikev2VpnProfile.fromVpnProfile(profile);
106             default:
107                 throw new IllegalArgumentException("Unknown VPN Profile type");
108         }
109     }
110 }
111