blob: a9126c0caa74ba1fb1bd2c2112bab910fda2e24a [file] [log] [blame]
Geremy Condraf1bcca82013-01-07 22:35:24 -08001/*
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
17package com.android.server.pm;
18
Jeff Sharkey9f837a92014-10-24 12:07:24 -070019import android.util.ArrayMap;
dcashman55b10782014-04-09 14:20:38 -070020
Jeff Sharkey9f837a92014-10-24 12:07:24 -070021import com.android.internal.util.ArrayUtils;
Geremy Condraf1bcca82013-01-07 22:35:24 -080022
23public class PackageKeySetData {
24
dcashman55b10782014-04-09 14:20:38 -070025 static final long KEYSET_UNASSIGNED = -1;
26
27 /* KeySet containing all signing keys - superset of the others */
28 private long mProperSigningKeySet;
29
dcashman55b10782014-04-09 14:20:38 -070030 private long[] mUpgradeKeySets;
31
Jeff Sharkey9f837a92014-10-24 12:07:24 -070032 private final ArrayMap<String, Long> mKeySetAliases = new ArrayMap<String, Long>();
Geremy Condraf1bcca82013-01-07 22:35:24 -080033
34 PackageKeySetData() {
dcashman55b10782014-04-09 14:20:38 -070035 mProperSigningKeySet = KEYSET_UNASSIGNED;
Geremy Condraf1bcca82013-01-07 22:35:24 -080036 }
37
38 PackageKeySetData(PackageKeySetData original) {
dcashmand876a1d2014-10-07 14:16:35 -070039 mProperSigningKeySet = original.mProperSigningKeySet;
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -070040 mUpgradeKeySets = ArrayUtils.cloneOrNull(original.mUpgradeKeySets);
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -070041 mKeySetAliases.putAll(original.mKeySetAliases);
Geremy Condraf1bcca82013-01-07 22:35:24 -080042 }
43
dcashman55b10782014-04-09 14:20:38 -070044 protected void setProperSigningKeySet(long ks) {
dcashman55b10782014-04-09 14:20:38 -070045 mProperSigningKeySet = ks;
dcashman55b10782014-04-09 14:20:38 -070046 return;
Geremy Condraf1bcca82013-01-07 22:35:24 -080047 }
48
dcashman55b10782014-04-09 14:20:38 -070049 protected long getProperSigningKeySet() {
50 return mProperSigningKeySet;
51 }
52
dcashman55b10782014-04-09 14:20:38 -070053 protected void addUpgradeKeySet(String alias) {
dcashman8c04fac2015-03-23 11:39:42 -070054 if (alias == null) {
55 return;
56 }
dcashman55b10782014-04-09 14:20:38 -070057
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!");
Geremy Condracdb57892013-03-26 16:22:36 -070065 }
66 }
67
dcashman55b10782014-04-09 14:20:38 -070068 /*
69 * Used only when restoring keyset data from persistent storage. Must
70 * correspond to a defined-keyset.
71 */
dcashman8c04fac2015-03-23 11:39:42 -070072
dcashman55b10782014-04-09 14:20:38 -070073 protected void addUpgradeKeySetById(long ks) {
dcashman8c04fac2015-03-23 11:39:42 -070074 mUpgradeKeySets = ArrayUtils.appendLong(mUpgradeKeySets, ks);
Geremy Condracdb57892013-03-26 16:22:36 -070075 }
76
dcashman55b10782014-04-09 14:20:38 -070077 protected void removeAllUpgradeKeySets() {
78 mUpgradeKeySets = null;
79 return;
Geremy Condraf1bcca82013-01-07 22:35:24 -080080 }
81
dcashman55b10782014-04-09 14:20:38 -070082 protected long[] getUpgradeKeySets() {
83 return mUpgradeKeySets;
84 }
85
Jeff Sharkey9f837a92014-10-24 12:07:24 -070086 protected ArrayMap<String, Long> getAliases() {
Geremy Condraf1bcca82013-01-07 22:35:24 -080087 return mKeySetAliases;
88 }
dcashman55b10782014-04-09 14:20:38 -070089
dcashman8c04fac2015-03-23 11:39:42 -070090 /*
91 * Replace defined keysets with new ones.
92 */
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
105 protected void addDefinedKeySet(long ks, String alias) {
106 mKeySetAliases.put(alias, ks);
107 }
108
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
dcashman55b10782014-04-09 14:20:38 -0700116 protected boolean isUsingDefinedKeySets() {
117
dcashman8c04fac2015-03-23 11:39:42 -0700118 /* should never be the case that mUpgradeKeySets.length == 0 */
119 return (mKeySetAliases.size() > 0);
dcashman55b10782014-04-09 14:20:38 -0700120 }
121
122 protected boolean isUsingUpgradeKeySets() {
123
124 /* should never be the case that mUpgradeKeySets.length == 0 */
125 return (mUpgradeKeySets != null && mUpgradeKeySets.length > 0);
126 }
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700127}