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 android.annotation.NonNull; 20 import android.net.ipsec.ike.SaProposal; 21 import android.os.PersistableBundle; 22 import android.util.Pair; 23 24 import com.android.server.vcn.util.PersistableBundleUtils; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 import java.util.Objects; 29 30 /** 31 * Abstract utility class to convert SaProposal to/from PersistableBundle. 32 * 33 * @hide 34 */ 35 abstract class SaProposalUtilsBase { 36 static final String ENCRYPT_ALGO_KEY = "ENCRYPT_ALGO_KEY"; 37 static final String INTEGRITY_ALGO_KEY = "INTEGRITY_ALGO_KEY"; 38 static final String DH_GROUP_KEY = "DH_GROUP_KEY"; 39 40 static class EncryptionAlgoKeyLenPair { 41 private static final String ALGO_KEY = "ALGO_KEY"; 42 private static final String KEY_LEN_KEY = "KEY_LEN_KEY"; 43 44 public final int encryptionAlgo; 45 public final int keyLen; 46 EncryptionAlgoKeyLenPair(int encryptionAlgo, int keyLen)47 EncryptionAlgoKeyLenPair(int encryptionAlgo, int keyLen) { 48 this.encryptionAlgo = encryptionAlgo; 49 this.keyLen = keyLen; 50 } 51 EncryptionAlgoKeyLenPair(PersistableBundle in)52 EncryptionAlgoKeyLenPair(PersistableBundle in) { 53 Objects.requireNonNull(in, "PersistableBundle was null"); 54 55 this.encryptionAlgo = in.getInt(ALGO_KEY); 56 this.keyLen = in.getInt(KEY_LEN_KEY); 57 } 58 toPersistableBundle()59 public PersistableBundle toPersistableBundle() { 60 final PersistableBundle result = new PersistableBundle(); 61 62 result.putInt(ALGO_KEY, encryptionAlgo); 63 result.putInt(KEY_LEN_KEY, keyLen); 64 65 return result; 66 } 67 } 68 69 /** 70 * Serializes common info of a SaProposal to a PersistableBundle. 71 * 72 * @hide 73 */ 74 @NonNull toPersistableBundle(SaProposal proposal)75 static PersistableBundle toPersistableBundle(SaProposal proposal) { 76 final PersistableBundle result = new PersistableBundle(); 77 78 final List<EncryptionAlgoKeyLenPair> encryptAlgoKeyLenPairs = new ArrayList<>(); 79 for (Pair<Integer, Integer> pair : proposal.getEncryptionAlgorithms()) { 80 encryptAlgoKeyLenPairs.add(new EncryptionAlgoKeyLenPair(pair.first, pair.second)); 81 } 82 final PersistableBundle encryptionBundle = 83 PersistableBundleUtils.fromList( 84 encryptAlgoKeyLenPairs, EncryptionAlgoKeyLenPair::toPersistableBundle); 85 result.putPersistableBundle(ENCRYPT_ALGO_KEY, encryptionBundle); 86 87 final int[] integrityAlgoIdArray = 88 proposal.getIntegrityAlgorithms().stream().mapToInt(i -> i).toArray(); 89 result.putIntArray(INTEGRITY_ALGO_KEY, integrityAlgoIdArray); 90 91 final int[] dhGroupArray = proposal.getDhGroups().stream().mapToInt(i -> i).toArray(); 92 result.putIntArray(DH_GROUP_KEY, dhGroupArray); 93 94 return result; 95 } 96 } 97