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
17package com.android.server.pm;
18
19import com.android.internal.util.ArrayUtils;
20
21import java.util.HashMap;
22import java.util.Map;
23
24public class PackageKeySetData {
25
26    static final long KEYSET_UNASSIGNED = -1;
27
28    /* KeySet containing all signing keys - superset of the others */
29    private long mProperSigningKeySet;
30
31    private long[] mSigningKeySets;
32
33    private long[] mUpgradeKeySets;
34
35    private long[] mDefinedKeySets;
36
37    private final Map<String, Long> mKeySetAliases = new HashMap<String, Long>();
38
39    PackageKeySetData() {
40        mProperSigningKeySet = KEYSET_UNASSIGNED;
41    }
42
43    PackageKeySetData(PackageKeySetData original) {
44        mProperSigningKeySet = original.mProperSigningKeySet;
45        mSigningKeySets = ArrayUtils.cloneOrNull(original.mSigningKeySets);
46        mUpgradeKeySets = ArrayUtils.cloneOrNull(original.mUpgradeKeySets);
47        mDefinedKeySets = ArrayUtils.cloneOrNull(original.mDefinedKeySets);
48        mKeySetAliases.putAll(original.mKeySetAliases);
49    }
50
51    protected void setProperSigningKeySet(long ks) {
52        if (ks == mProperSigningKeySet) {
53
54            /* nothing to change */
55            return;
56        }
57
58        /* otherwise, our current signing keysets are likely invalid */
59        removeAllSigningKeySets();
60        mProperSigningKeySet = ks;
61        addSigningKeySet(ks);
62        return;
63    }
64
65    protected long getProperSigningKeySet() {
66        return mProperSigningKeySet;
67    }
68
69    protected void addSigningKeySet(long ks) {
70        mSigningKeySets = ArrayUtils.appendLong(mSigningKeySets, ks);
71    }
72
73    protected void removeSigningKeySet(long ks) {
74        mSigningKeySets = ArrayUtils.removeLong(mSigningKeySets, ks);
75    }
76
77    protected void addUpgradeKeySet(String alias) {
78
79        /* must have previously been defined */
80        Long ks = mKeySetAliases.get(alias);
81        if (ks != null) {
82            mUpgradeKeySets = ArrayUtils.appendLong(mUpgradeKeySets, ks);
83        } else {
84            throw new IllegalArgumentException("Upgrade keyset alias " + alias
85                    + "does not refer to a defined keyset alias!");
86        }
87    }
88
89    /*
90     * Used only when restoring keyset data from persistent storage.  Must
91     * correspond to a defined-keyset.
92     */
93    protected void addUpgradeKeySetById(long ks) {
94        mSigningKeySets = ArrayUtils.appendLong(mSigningKeySets, ks);
95    }
96
97    protected void addDefinedKeySet(long ks, String alias) {
98        mDefinedKeySets = ArrayUtils.appendLong(mDefinedKeySets, ks);
99        mKeySetAliases.put(alias, ks);
100    }
101
102    protected void removeAllSigningKeySets() {
103        mProperSigningKeySet = KEYSET_UNASSIGNED;
104        mSigningKeySets = null;
105        return;
106    }
107
108    protected void removeAllUpgradeKeySets() {
109        mUpgradeKeySets = null;
110        return;
111    }
112
113    protected void removeAllDefinedKeySets() {
114        mDefinedKeySets = null;
115        mKeySetAliases.clear();
116        return;
117    }
118
119    protected boolean packageIsSignedBy(long ks) {
120        return ArrayUtils.contains(mSigningKeySets, ks);
121    }
122
123    protected long[] getSigningKeySets() {
124        return mSigningKeySets;
125    }
126
127    protected long[] getUpgradeKeySets() {
128        return mUpgradeKeySets;
129    }
130
131    protected long[] getDefinedKeySets() {
132        return mDefinedKeySets;
133    }
134
135    protected Map<String, Long> getAliases() {
136        return mKeySetAliases;
137    }
138
139    protected boolean isUsingDefinedKeySets() {
140
141        /* should never be the case that mDefinedKeySets.length == 0 */
142        return (mDefinedKeySets != null && mDefinedKeySets.length > 0);
143    }
144
145    protected boolean isUsingUpgradeKeySets() {
146
147        /* should never be the case that mUpgradeKeySets.length == 0 */
148        return (mUpgradeKeySets != null && mUpgradeKeySets.length > 0);
149    }
150}
151