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.IkeSaProposal; 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 IkeSaProposal to/from PersistableBundle. 33 * 34 * @hide 35 */ 36 @VisibleForTesting(visibility = Visibility.PRIVATE) 37 public final class IkeSaProposalUtils extends SaProposalUtilsBase { 38 private static final String PRF_KEY = "PRF_KEY"; 39 40 /** Serializes an IkeSaProposal to a PersistableBundle. */ 41 @NonNull toPersistableBundle(IkeSaProposal proposal)42 public static PersistableBundle toPersistableBundle(IkeSaProposal proposal) { 43 final PersistableBundle result = SaProposalUtilsBase.toPersistableBundle(proposal); 44 45 final int[] prfArray = 46 proposal.getPseudorandomFunctions().stream().mapToInt(i -> i).toArray(); 47 result.putIntArray(PRF_KEY, prfArray); 48 49 return result; 50 } 51 52 /** Constructs an IkeSaProposal by deserializing a PersistableBundle. */ 53 @NonNull fromPersistableBundle(@onNull PersistableBundle in)54 public static IkeSaProposal fromPersistableBundle(@NonNull PersistableBundle in) { 55 Objects.requireNonNull(in, "PersistableBundle was null"); 56 57 final IkeSaProposal.Builder builder = new IkeSaProposal.Builder(); 58 59 final PersistableBundle encryptionBundle = in.getPersistableBundle(ENCRYPT_ALGO_KEY); 60 Objects.requireNonNull(encryptionBundle, "Encryption algo bundle was null"); 61 final List<EncryptionAlgoKeyLenPair> encryptList = 62 PersistableBundleUtils.toList(encryptionBundle, EncryptionAlgoKeyLenPair::new); 63 for (EncryptionAlgoKeyLenPair t : encryptList) { 64 builder.addEncryptionAlgorithm(t.encryptionAlgo, t.keyLen); 65 } 66 67 final int[] integrityAlgoIdArray = in.getIntArray(INTEGRITY_ALGO_KEY); 68 Objects.requireNonNull(integrityAlgoIdArray, "Integrity algo array was null"); 69 for (int algo : integrityAlgoIdArray) { 70 builder.addIntegrityAlgorithm(algo); 71 } 72 73 final int[] dhGroupArray = in.getIntArray(DH_GROUP_KEY); 74 Objects.requireNonNull(dhGroupArray, "DH Group array was null"); 75 for (int dh : dhGroupArray) { 76 builder.addDhGroup(dh); 77 } 78 79 final int[] prfArray = in.getIntArray(PRF_KEY); 80 Objects.requireNonNull(prfArray, "PRF array was null"); 81 for (int prf : prfArray) { 82 builder.addPseudorandomFunction(prf); 83 } 84 85 return builder.build(); 86 } 87 } 88