1 /*
2  * Copyright (C) 2021 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 package android.net.vcn.persistablebundleutils;
17 
18 import android.annotation.NonNull;
19 import android.net.ipsec.ike.IkeSessionParams;
20 import android.net.ipsec.ike.IkeTunnelConnectionParams;
21 import android.net.ipsec.ike.TunnelModeChildSessionParams;
22 import android.os.PersistableBundle;
23 
24 import java.util.Objects;
25 
26 /**
27  * Utility class to convert Tunnel Connection Params to/from PersistableBundle
28  *
29  * @hide
30  */
31 public final class TunnelConnectionParamsUtils {
32     private static final int EXPECTED_BUNDLE_KEY_CNT = 1;
33 
34     private static final String PARAMS_TYPE_IKE = "IKE";
35 
36     /** Serializes an IkeTunnelConnectionParams to a PersistableBundle. */
37     @NonNull
toPersistableBundle(@onNull IkeTunnelConnectionParams params)38     public static PersistableBundle toPersistableBundle(@NonNull IkeTunnelConnectionParams params) {
39         final PersistableBundle result = new PersistableBundle();
40 
41         result.putPersistableBundle(
42                 PARAMS_TYPE_IKE,
43                 IkeTunnelConnectionParamsUtils.serializeIkeParams(
44                         (IkeTunnelConnectionParams) params));
45         return result;
46     }
47 
48     /** Constructs an IkeTunnelConnectionParams by deserializing a PersistableBundle. */
49     @NonNull
fromPersistableBundle(@onNull PersistableBundle in)50     public static IkeTunnelConnectionParams fromPersistableBundle(@NonNull PersistableBundle in) {
51         Objects.requireNonNull(in, "PersistableBundle was null");
52 
53         if (in.keySet().size() != EXPECTED_BUNDLE_KEY_CNT) {
54             throw new IllegalArgumentException(
55                     String.format(
56                             "Expect PersistableBundle to have %d element but found: %s",
57                             EXPECTED_BUNDLE_KEY_CNT, in.keySet()));
58         }
59 
60         if (in.get(PARAMS_TYPE_IKE) != null) {
61             return IkeTunnelConnectionParamsUtils.deserializeIkeParams(
62                     in.getPersistableBundle(PARAMS_TYPE_IKE));
63         }
64 
65         throw new IllegalArgumentException(
66                 "Invalid Tunnel Connection Params type " + in.keySet().iterator().next());
67     }
68 
69     private static final class IkeTunnelConnectionParamsUtils {
70         private static final String IKE_PARAMS_KEY = "IKE_PARAMS_KEY";
71         private static final String CHILD_PARAMS_KEY = "CHILD_PARAMS_KEY";
72 
73         @NonNull
serializeIkeParams( @onNull IkeTunnelConnectionParams ikeParams)74         public static PersistableBundle serializeIkeParams(
75                 @NonNull IkeTunnelConnectionParams ikeParams) {
76             final PersistableBundle result = new PersistableBundle();
77 
78             result.putPersistableBundle(
79                     IKE_PARAMS_KEY,
80                     IkeSessionParamsUtils.toPersistableBundle(ikeParams.getIkeSessionParams()));
81             result.putPersistableBundle(
82                     CHILD_PARAMS_KEY,
83                     TunnelModeChildSessionParamsUtils.toPersistableBundle(
84                             ikeParams.getTunnelModeChildSessionParams()));
85             return result;
86         }
87 
88         @NonNull
deserializeIkeParams( @onNull PersistableBundle in)89         public static IkeTunnelConnectionParams deserializeIkeParams(
90                 @NonNull PersistableBundle in) {
91             final PersistableBundle ikeBundle = in.getPersistableBundle(IKE_PARAMS_KEY);
92             final PersistableBundle childBundle = in.getPersistableBundle(CHILD_PARAMS_KEY);
93             Objects.requireNonNull(ikeBundle, "IkeSessionParams was null");
94             Objects.requireNonNull(ikeBundle, "TunnelModeChildSessionParams was null");
95 
96             final IkeSessionParams ikeParams =
97                     IkeSessionParamsUtils.fromPersistableBundle(ikeBundle);
98             final TunnelModeChildSessionParams childParams =
99                     TunnelModeChildSessionParamsUtils.fromPersistableBundle(childBundle);
100             return new IkeTunnelConnectionParams(ikeParams, childParams);
101         }
102     }
103 }
104