1 /* 2 * Copyright (C) 2013 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 com.android.server.pm; 18 19 import android.util.ArrayMap; 20 21 import com.android.internal.util.ArrayUtils; 22 23 public class PackageKeySetData { 24 25 static final long KEYSET_UNASSIGNED = -1; 26 27 /* KeySet containing all signing keys - superset of the others */ 28 private long mProperSigningKeySet; 29 30 private long[] mUpgradeKeySets; 31 32 private final ArrayMap<String, Long> mKeySetAliases = new ArrayMap<String, Long>(); 33 PackageKeySetData()34 PackageKeySetData() { 35 mProperSigningKeySet = KEYSET_UNASSIGNED; 36 } 37 PackageKeySetData(PackageKeySetData original)38 PackageKeySetData(PackageKeySetData original) { 39 mProperSigningKeySet = original.mProperSigningKeySet; 40 mUpgradeKeySets = ArrayUtils.cloneOrNull(original.mUpgradeKeySets); 41 mKeySetAliases.putAll(original.mKeySetAliases); 42 } 43 setProperSigningKeySet(long ks)44 protected void setProperSigningKeySet(long ks) { 45 mProperSigningKeySet = ks; 46 return; 47 } 48 getProperSigningKeySet()49 protected long getProperSigningKeySet() { 50 return mProperSigningKeySet; 51 } 52 addUpgradeKeySet(String alias)53 protected void addUpgradeKeySet(String alias) { 54 if (alias == null) { 55 return; 56 } 57 58 /* must have previously been defined */ 59 Long ks = mKeySetAliases.get(alias); 60 if (ks != null) { 61 mUpgradeKeySets = ArrayUtils.appendLong(mUpgradeKeySets, ks); 62 } else { 63 throw new IllegalArgumentException("Upgrade keyset alias " + alias 64 + "does not refer to a defined keyset alias!"); 65 } 66 } 67 68 /* 69 * Used only when restoring keyset data from persistent storage. Must 70 * correspond to a defined-keyset. 71 */ 72 addUpgradeKeySetById(long ks)73 protected void addUpgradeKeySetById(long ks) { 74 mUpgradeKeySets = ArrayUtils.appendLong(mUpgradeKeySets, ks); 75 } 76 removeAllUpgradeKeySets()77 protected void removeAllUpgradeKeySets() { 78 mUpgradeKeySets = null; 79 return; 80 } 81 getUpgradeKeySets()82 protected long[] getUpgradeKeySets() { 83 return mUpgradeKeySets; 84 } 85 getAliases()86 protected ArrayMap<String, Long> getAliases() { 87 return mKeySetAliases; 88 } 89 90 /* 91 * Replace defined keysets with new ones. 92 */ setAliases(ArrayMap<String, Long> newAliases)93 protected void setAliases(ArrayMap<String, Long> newAliases) { 94 95 /* remove old aliases */ 96 removeAllDefinedKeySets(); 97 98 /* add new ones */ 99 final int newAliasSize = newAliases.size(); 100 for (int i = 0; i < newAliasSize; i++) { 101 mKeySetAliases.put(newAliases.keyAt(i), newAliases.valueAt(i));; 102 } 103 } 104 addDefinedKeySet(long ks, String alias)105 protected void addDefinedKeySet(long ks, String alias) { 106 mKeySetAliases.put(alias, ks); 107 } 108 removeAllDefinedKeySets()109 protected void removeAllDefinedKeySets() { 110 final int aliasSize = mKeySetAliases.size(); 111 for (int i = 0; i < aliasSize; i++) { 112 mKeySetAliases.removeAt(i); 113 } 114 } 115 isUsingDefinedKeySets()116 protected boolean isUsingDefinedKeySets() { 117 118 /* should never be the case that mUpgradeKeySets.length == 0 */ 119 return (mKeySetAliases.size() > 0); 120 } 121 isUsingUpgradeKeySets()122 protected boolean isUsingUpgradeKeySets() { 123 124 /* should never be the case that mUpgradeKeySets.length == 0 */ 125 return (mUpgradeKeySets != null && mUpgradeKeySets.length > 0); 126 } 127 } 128