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 
17 package android.net.vcn.persistablebundleutils;
18 
19 import static com.android.internal.annotations.VisibleForTesting.Visibility;
20 
21 import android.annotation.NonNull;
22 import android.net.ipsec.ike.ChildSaProposal;
23 import android.os.PersistableBundle;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.server.vcn.util.PersistableBundleUtils;
27 
28 import java.util.List;
29 import java.util.Objects;
30 
31 /**
32  * Provides utility methods to convert ChildSaProposal to/from PersistableBundle.
33  *
34  * @hide
35  */
36 @VisibleForTesting(visibility = Visibility.PRIVATE)
37 public final class ChildSaProposalUtils extends SaProposalUtilsBase {
38     /** Serializes a ChildSaProposal to a PersistableBundle. */
39     @NonNull
toPersistableBundle(ChildSaProposal proposal)40     public static PersistableBundle toPersistableBundle(ChildSaProposal proposal) {
41         return SaProposalUtilsBase.toPersistableBundle(proposal);
42     }
43 
44     /** Constructs a ChildSaProposal by deserializing a PersistableBundle. */
45     @NonNull
fromPersistableBundle(@onNull PersistableBundle in)46     public static ChildSaProposal fromPersistableBundle(@NonNull PersistableBundle in) {
47         Objects.requireNonNull(in, "PersistableBundle was null");
48 
49         final ChildSaProposal.Builder builder = new ChildSaProposal.Builder();
50 
51         final PersistableBundle encryptionBundle = in.getPersistableBundle(ENCRYPT_ALGO_KEY);
52         Objects.requireNonNull(encryptionBundle, "Encryption algo bundle was null");
53         final List<EncryptionAlgoKeyLenPair> encryptList =
54                 PersistableBundleUtils.toList(encryptionBundle, EncryptionAlgoKeyLenPair::new);
55         for (EncryptionAlgoKeyLenPair t : encryptList) {
56             builder.addEncryptionAlgorithm(t.encryptionAlgo, t.keyLen);
57         }
58 
59         final int[] integrityAlgoIdArray = in.getIntArray(INTEGRITY_ALGO_KEY);
60         Objects.requireNonNull(integrityAlgoIdArray, "Integrity algo array was null");
61         for (int algo : integrityAlgoIdArray) {
62             builder.addIntegrityAlgorithm(algo);
63         }
64 
65         final int[] dhGroupArray = in.getIntArray(DH_GROUP_KEY);
66         Objects.requireNonNull(dhGroupArray, "DH Group array was null");
67         for (int dh : dhGroupArray) {
68             builder.addDhGroup(dh);
69         }
70 
71         return builder.build();
72     }
73 }
74