| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 47 |
EncryptionAlgoKeyLenPair(int encryptionAlgo, int keyLen) { |
| 48 |
this.encryptionAlgo = encryptionAlgo; |
| 49 |
this.keyLen = keyLen; |
| 50 |
} |
| 51 |
|
| 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 |
|
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
@NonNull |
| 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 |
} |