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