blob: aa63932763756a9c68e5b49023fd0488f6c911c3 [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
Geremy Condraf1bcca82013-01-07 22:35:24 -080019import android.content.pm.PackageParser;
dcashman55b10782014-04-09 14:20:38 -070020import android.util.ArraySet;
Geremy Condraf1bcca82013-01-07 22:35:24 -080021import android.util.Base64;
dcashman55b10782014-04-09 14:20:38 -070022import android.util.Slog;
Geremy Condraf1bcca82013-01-07 22:35:24 -080023import android.util.LongSparseArray;
24
25import java.io.IOException;
26import java.io.PrintWriter;
27import java.security.PublicKey;
Geremy Condraf1bcca82013-01-07 22:35:24 -080028import java.util.Map;
29import java.util.Set;
30
31import org.xmlpull.v1.XmlPullParser;
32import org.xmlpull.v1.XmlPullParserException;
33import org.xmlpull.v1.XmlSerializer;
34
35/*
36 * Manages system-wide KeySet state.
37 */
dcashman55b10782014-04-09 14:20:38 -070038public class KeySetManagerService {
Geremy Condraf1bcca82013-01-07 22:35:24 -080039
dcashman55b10782014-04-09 14:20:38 -070040 static final String TAG = "KeySetManagerService";
41
42 /* original keysets implementation had no versioning info, so this is the first */
43 public static final int FIRST_VERSION = 1;
44
45 public static final int CURRENT_VERSION = FIRST_VERSION;
Geremy Condraf1bcca82013-01-07 22:35:24 -080046
Kenny Root9a995f52013-07-08 09:33:22 -070047 /** Sentinel value returned when a {@code KeySet} is not found. */
48 public static final long KEYSET_NOT_FOUND = -1;
49
50 /** Sentinel value returned when public key is not found. */
dcashman55b10782014-04-09 14:20:38 -070051 protected static final long PUBLIC_KEY_NOT_FOUND = -1;
Geremy Condraf1bcca82013-01-07 22:35:24 -080052
dcashman9d2f4412014-06-09 09:27:54 -070053 private final LongSparseArray<KeySetHandle> mKeySets;
Geremy Condraf1bcca82013-01-07 22:35:24 -080054
55 private final LongSparseArray<PublicKey> mPublicKeys;
56
dcashmand79fdf12014-06-20 15:53:06 -070057 protected final LongSparseArray<ArraySet<Long>> mKeySetMapping;
Geremy Condraf1bcca82013-01-07 22:35:24 -080058
59 private final Map<String, PackageSetting> mPackages;
60
61 private static long lastIssuedKeySetId = 0;
62
63 private static long lastIssuedKeyId = 0;
64
dcashman55b10782014-04-09 14:20:38 -070065 public KeySetManagerService(Map<String, PackageSetting> packages) {
dcashman9d2f4412014-06-09 09:27:54 -070066 mKeySets = new LongSparseArray<KeySetHandle>();
Geremy Condraf1bcca82013-01-07 22:35:24 -080067 mPublicKeys = new LongSparseArray<PublicKey>();
dcashmand79fdf12014-06-20 15:53:06 -070068 mKeySetMapping = new LongSparseArray<ArraySet<Long>>();
Geremy Condraf1bcca82013-01-07 22:35:24 -080069 mPackages = packages;
70 }
71
Kenny Root2042e9d2013-07-08 09:31:31 -070072 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -080073 * Determine if a package is signed by the given KeySet.
74 *
75 * Returns false if the package was not signed by all the
76 * keys in the KeySet.
77 *
78 * Returns true if the package was signed by at least the
79 * keys in the given KeySet.
80 *
81 * Note that this can return true for multiple KeySets.
82 */
dcashman9d2f4412014-06-09 09:27:54 -070083 public boolean packageIsSignedByLPr(String packageName, KeySetHandle ks) {
dcashmand79fdf12014-06-20 15:53:06 -070084 PackageSetting pkg = mPackages.get(packageName);
85 if (pkg == null) {
86 throw new NullPointerException("Invalid package name");
Geremy Condraf1bcca82013-01-07 22:35:24 -080087 }
dcashmand79fdf12014-06-20 15:53:06 -070088 if (pkg.keySetData == null) {
89 throw new NullPointerException("Package has no KeySet data");
90 }
91 long id = getIdByKeySetLPr(ks);
dcashman9d2f4412014-06-09 09:27:54 -070092 if (id == KEYSET_NOT_FOUND) {
93 return false;
94 }
dcashmand79fdf12014-06-20 15:53:06 -070095 return pkg.keySetData.packageIsSignedBy(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -080096 }
97
Kenny Root2042e9d2013-07-08 09:31:31 -070098 /**
dcashman9d2f4412014-06-09 09:27:54 -070099 * Determine if a package is signed by the given KeySet.
100 *
101 * Returns false if the package was not signed by all the
102 * keys in the KeySet, or if the package was signed by keys
103 * not in the KeySet.
104 *
105 * Note that this can return only for one KeySet.
106 */
107 public boolean packageIsSignedByExactlyLPr(String packageName, KeySetHandle ks) {
108 PackageSetting pkg = mPackages.get(packageName);
109 if (pkg == null) {
110 throw new NullPointerException("Invalid package name");
111 }
112 if (pkg.keySetData == null
113 || pkg.keySetData.getProperSigningKeySet()
114 == PackageKeySetData.KEYSET_UNASSIGNED) {
115 throw new NullPointerException("Package has no KeySet data");
116 }
117 long id = getIdByKeySetLPr(ks);
118 return pkg.keySetData.getProperSigningKeySet() == id;
119 }
120
121 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800122 * This informs the system that the given package has defined a KeySet
123 * in its manifest that a) contains the given keys and b) is named
124 * alias by that package.
125 */
dcashmand79fdf12014-06-20 15:53:06 -0700126 public void addDefinedKeySetToPackageLPw(String packageName,
dcashman9d2f4412014-06-09 09:27:54 -0700127 ArraySet<PublicKey> keys, String alias) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800128 if ((packageName == null) || (keys == null) || (alias == null)) {
dcashman55b10782014-04-09 14:20:38 -0700129 Slog.w(TAG, "Got null argument for a defined keyset, ignoring!");
Geremy Condraf1bcca82013-01-07 22:35:24 -0800130 return;
131 }
dcashmand79fdf12014-06-20 15:53:06 -0700132 PackageSetting pkg = mPackages.get(packageName);
133 if (pkg == null) {
134 throw new NullPointerException("Unknown package");
Geremy Condraf1bcca82013-01-07 22:35:24 -0800135 }
dcashmand79fdf12014-06-20 15:53:06 -0700136 // Add to KeySets, then to package
dcashman9d2f4412014-06-09 09:27:54 -0700137 KeySetHandle ks = addKeySetLPw(keys);
dcashmand79fdf12014-06-20 15:53:06 -0700138 long id = getIdByKeySetLPr(ks);
139 pkg.keySetData.addDefinedKeySet(id, alias);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800140 }
141
Kenny Root2042e9d2013-07-08 09:31:31 -0700142 /**
dcashman55b10782014-04-09 14:20:38 -0700143 * This informs the system that the given package has defined a KeySet
144 * alias in its manifest to be an upgradeKeySet. This must be called
145 * after all of the defined KeySets have been added.
146 */
dcashmand79fdf12014-06-20 15:53:06 -0700147 public void addUpgradeKeySetToPackageLPw(String packageName, String alias) {
dcashman55b10782014-04-09 14:20:38 -0700148 if ((packageName == null) || (alias == null)) {
149 Slog.w(TAG, "Got null argument for a defined keyset, ignoring!");
150 return;
151 }
dcashmand79fdf12014-06-20 15:53:06 -0700152 PackageSetting pkg = mPackages.get(packageName);
153 if (pkg == null) {
154 throw new NullPointerException("Unknown package");
dcashman55b10782014-04-09 14:20:38 -0700155 }
dcashmand79fdf12014-06-20 15:53:06 -0700156 pkg.keySetData.addUpgradeKeySet(alias);
dcashman55b10782014-04-09 14:20:38 -0700157 }
158
159 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800160 * Similar to the above, this informs the system that the given package
161 * was signed by the provided KeySet.
162 */
dcashmand79fdf12014-06-20 15:53:06 -0700163 public void addSigningKeySetToPackageLPw(String packageName,
dcashman9d2f4412014-06-09 09:27:54 -0700164 ArraySet<PublicKey> signingKeys) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800165 if ((packageName == null) || (signingKeys == null)) {
dcashman55b10782014-04-09 14:20:38 -0700166 Slog.w(TAG, "Got null argument for a signing keyset, ignoring!");
Geremy Condraf1bcca82013-01-07 22:35:24 -0800167 return;
168 }
dcashmand79fdf12014-06-20 15:53:06 -0700169 // add the signing KeySet
dcashman9d2f4412014-06-09 09:27:54 -0700170 KeySetHandle ks = addKeySetLPw(signingKeys);
dcashmand79fdf12014-06-20 15:53:06 -0700171 long id = getIdByKeySetLPr(ks);
dcashman9d2f4412014-06-09 09:27:54 -0700172 ArraySet<Long> publicKeyIds = mKeySetMapping.get(id);
dcashmand79fdf12014-06-20 15:53:06 -0700173 if (publicKeyIds == null) {
174 throw new NullPointerException("Got invalid KeySet id");
175 }
dcashmand79fdf12014-06-20 15:53:06 -0700176 // attach it to the package
177 PackageSetting pkg = mPackages.get(packageName);
178 if (pkg == null) {
179 throw new NullPointerException("No such package!");
180 }
181 pkg.keySetData.setProperSigningKeySet(id);
182 // for each KeySet which is a subset of the one above, add the
183 // KeySet id to the package's signing KeySets
184 for (int keySetIndex = 0; keySetIndex < mKeySets.size(); keySetIndex++) {
185 long keySetID = mKeySets.keyAt(keySetIndex);
dcashman9d2f4412014-06-09 09:27:54 -0700186 ArraySet<Long> definedKeys = mKeySetMapping.get(keySetID);
dcashmand79fdf12014-06-20 15:53:06 -0700187 if (publicKeyIds.containsAll(definedKeys)) {
188 pkg.keySetData.addSigningKeySet(keySetID);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800189 }
190 }
191 }
192
Kenny Root2042e9d2013-07-08 09:31:31 -0700193 /**
194 * Fetches the stable identifier associated with the given KeySet. Returns
195 * {@link #KEYSET_NOT_FOUND} if the KeySet... wasn't found.
Geremy Condraf1bcca82013-01-07 22:35:24 -0800196 */
dcashman9d2f4412014-06-09 09:27:54 -0700197 private long getIdByKeySetLPr(KeySetHandle ks) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800198 for (int keySetIndex = 0; keySetIndex < mKeySets.size(); keySetIndex++) {
dcashman9d2f4412014-06-09 09:27:54 -0700199 KeySetHandle value = mKeySets.valueAt(keySetIndex);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800200 if (ks.equals(value)) {
201 return mKeySets.keyAt(keySetIndex);
202 }
203 }
204 return KEYSET_NOT_FOUND;
205 }
206
Kenny Root2042e9d2013-07-08 09:31:31 -0700207 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800208 * Fetches the KeySet corresponding to the given stable identifier.
209 *
Kenny Root2042e9d2013-07-08 09:31:31 -0700210 * Returns {@link #KEYSET_NOT_FOUND} if the identifier doesn't
211 * identify a {@link KeySet}.
Geremy Condraf1bcca82013-01-07 22:35:24 -0800212 */
dcashman9d2f4412014-06-09 09:27:54 -0700213 public KeySetHandle getKeySetByIdLPr(long id) {
dcashmand79fdf12014-06-20 15:53:06 -0700214 return mKeySets.get(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800215 }
216
Kenny Root2042e9d2013-07-08 09:31:31 -0700217 /**
dcashman9d2f4412014-06-09 09:27:54 -0700218 * Fetches the {@link KeySetHandle} that a given package refers to by the
219 * provided alias. Returns null if the package is unknown or does not have a
220 * KeySet corresponding to that alias.
Geremy Condraf1bcca82013-01-07 22:35:24 -0800221 */
dcashman9d2f4412014-06-09 09:27:54 -0700222 public KeySetHandle getKeySetByAliasAndPackageNameLPr(String packageName, String alias) {
dcashmand79fdf12014-06-20 15:53:06 -0700223 PackageSetting p = mPackages.get(packageName);
dcashman9d2f4412014-06-09 09:27:54 -0700224 if (p == null || p.keySetData == null) {
225 return null;
Geremy Condraf1bcca82013-01-07 22:35:24 -0800226 }
dcashman9d2f4412014-06-09 09:27:54 -0700227 Long keySetId = p.keySetData.getAliases().get(alias);
228 if (keySetId == null) {
229 throw new IllegalArgumentException("Unknown KeySet alias: " + alias);
dcashmand79fdf12014-06-20 15:53:06 -0700230 }
dcashmand79fdf12014-06-20 15:53:06 -0700231 return mKeySets.get(keySetId);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800232 }
233
Kenny Root2042e9d2013-07-08 09:31:31 -0700234 /**
dcashman55b10782014-04-09 14:20:38 -0700235 * Fetches the {@link PublicKey public keys} which belong to the specified
236 * KeySet id.
237 *
238 * Returns {@code null} if the identifier doesn't
dcashman9d2f4412014-06-09 09:27:54 -0700239 * identify a {@link KeySetHandle}.
dcashman55b10782014-04-09 14:20:38 -0700240 */
dcashmand79fdf12014-06-20 15:53:06 -0700241 public ArraySet<PublicKey> getPublicKeysFromKeySetLPr(long id) {
242 if(mKeySetMapping.get(id) == null) {
243 return null;
dcashman55b10782014-04-09 14:20:38 -0700244 }
dcashmand79fdf12014-06-20 15:53:06 -0700245 ArraySet<PublicKey> mPubKeys = new ArraySet<PublicKey>();
246 for (long pkId : mKeySetMapping.get(id)) {
247 mPubKeys.add(mPublicKeys.get(pkId));
248 }
249 return mPubKeys;
dcashman55b10782014-04-09 14:20:38 -0700250 }
251
252 /**
dcashman9d2f4412014-06-09 09:27:54 -0700253 * Fetches the proper {@link KeySetHandle KeySet} that signed the given
dcashman55b10782014-04-09 14:20:38 -0700254 * package.
255 *
256 * @throws IllegalArgumentException if the package has no keyset data.
257 * @throws NullPointerException if the package is unknown.
Geremy Condraf1bcca82013-01-07 22:35:24 -0800258 */
dcashman9d2f4412014-06-09 09:27:54 -0700259 public KeySetHandle getSigningKeySetByPackageNameLPr(String packageName) {
dcashmand79fdf12014-06-20 15:53:06 -0700260 PackageSetting p = mPackages.get(packageName);
dcashman9d2f4412014-06-09 09:27:54 -0700261 if (p == null
262 || p.keySetData == null
263 || p.keySetData.getProperSigningKeySet()
264 == PackageKeySetData.KEYSET_UNASSIGNED) {
265 return null;
dcashman55b10782014-04-09 14:20:38 -0700266 }
dcashman9d2f4412014-06-09 09:27:54 -0700267 return mKeySets.get(p.keySetData.getProperSigningKeySet());
dcashman55b10782014-04-09 14:20:38 -0700268 }
269
270 /**
dcashman9d2f4412014-06-09 09:27:54 -0700271 * Fetches all the known {@link KeySetHandle KeySets} that may upgrade the given
dcashman55b10782014-04-09 14:20:38 -0700272 * package.
273 *
274 * @throws IllegalArgumentException if the package has no keyset data.
275 * @throws NullPointerException if the package is unknown.
276 */
dcashman9d2f4412014-06-09 09:27:54 -0700277 public ArraySet<KeySetHandle> getUpgradeKeySetsByPackageNameLPr(String packageName) {
278 ArraySet<KeySetHandle> upgradeKeySets = new ArraySet<KeySetHandle>();
dcashmand79fdf12014-06-20 15:53:06 -0700279 PackageSetting p = mPackages.get(packageName);
280 if (p == null) {
281 throw new NullPointerException("Unknown package");
Geremy Condraf1bcca82013-01-07 22:35:24 -0800282 }
dcashmand79fdf12014-06-20 15:53:06 -0700283 if (p.keySetData == null) {
284 throw new IllegalArgumentException("Package has no keySet data");
285 }
286 if (p.keySetData.isUsingUpgradeKeySets()) {
287 for (long l : p.keySetData.getUpgradeKeySets()) {
288 upgradeKeySets.add(mKeySets.get(l));
289 }
290 }
291 return upgradeKeySets;
Geremy Condraf1bcca82013-01-07 22:35:24 -0800292 }
293
Kenny Root2042e9d2013-07-08 09:31:31 -0700294 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800295 * Creates a new KeySet corresponding to the given keys.
296 *
Kenny Root2042e9d2013-07-08 09:31:31 -0700297 * If the {@link PublicKey PublicKeys} aren't known to the system, this
298 * adds them. Otherwise, they're deduped.
Geremy Condraf1bcca82013-01-07 22:35:24 -0800299 *
300 * If the KeySet isn't known to the system, this adds that and creates the
301 * mapping to the PublicKeys. If it is known, then it's deduped.
302 *
dcashman55b10782014-04-09 14:20:38 -0700303 * If the KeySet isn't known to the system, this adds it to all appropriate
304 * signingKeySets
305 *
Kenny Root2042e9d2013-07-08 09:31:31 -0700306 * Throws if the provided set is {@code null}.
Geremy Condraf1bcca82013-01-07 22:35:24 -0800307 */
dcashman9d2f4412014-06-09 09:27:54 -0700308 private KeySetHandle addKeySetLPw(ArraySet<PublicKey> keys) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800309 if (keys == null) {
310 throw new NullPointerException("Provided keys cannot be null");
311 }
312 // add each of the keys in the provided set
dcashmand79fdf12014-06-20 15:53:06 -0700313 ArraySet<Long> addedKeyIds = new ArraySet<Long>(keys.size());
Geremy Condraf1bcca82013-01-07 22:35:24 -0800314 for (PublicKey k : keys) {
dcashmand79fdf12014-06-20 15:53:06 -0700315 long id = addPublicKeyLPw(k);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800316 addedKeyIds.add(id);
317 }
318
319 // check to see if the resulting keyset is new
dcashmand79fdf12014-06-20 15:53:06 -0700320 long existingKeySetId = getIdFromKeyIdsLPr(addedKeyIds);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800321 if (existingKeySetId != KEYSET_NOT_FOUND) {
322 return mKeySets.get(existingKeySetId);
323 }
324
325 // create the KeySet object
dcashman9d2f4412014-06-09 09:27:54 -0700326 KeySetHandle ks = new KeySetHandle();
Geremy Condraf1bcca82013-01-07 22:35:24 -0800327 // get the first unoccupied slot in mKeySets
dcashmand79fdf12014-06-20 15:53:06 -0700328 long id = getFreeKeySetIDLPw();
Geremy Condraf1bcca82013-01-07 22:35:24 -0800329 // add the KeySet object to it
330 mKeySets.put(id, ks);
331 // add the stable key ids to the mapping
332 mKeySetMapping.put(id, addedKeyIds);
dcashman55b10782014-04-09 14:20:38 -0700333 // add this KeySet id to all packages which are signed by it
334 for (String pkgName : mPackages.keySet()) {
335 PackageSetting p = mPackages.get(pkgName);
336 if (p.keySetData != null) {
337 long pProperSigning = p.keySetData.getProperSigningKeySet();
338 if (pProperSigning != PackageKeySetData.KEYSET_UNASSIGNED) {
dcashman9d2f4412014-06-09 09:27:54 -0700339 ArraySet<Long> pSigningKeys = mKeySetMapping.get(pProperSigning);
dcashman55b10782014-04-09 14:20:38 -0700340 if (pSigningKeys.containsAll(addedKeyIds)) {
341 p.keySetData.addSigningKeySet(id);
342 }
343 }
344 }
345 }
Geremy Condraf1bcca82013-01-07 22:35:24 -0800346 // go home
347 return ks;
348 }
349
Kenny Root2042e9d2013-07-08 09:31:31 -0700350 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800351 * Adds the given PublicKey to the system, deduping as it goes.
352 */
dcashmand79fdf12014-06-20 15:53:06 -0700353 private long addPublicKeyLPw(PublicKey key) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800354 // check if the public key is new
dcashmand79fdf12014-06-20 15:53:06 -0700355 long existingKeyId = getIdForPublicKeyLPr(key);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800356 if (existingKeyId != PUBLIC_KEY_NOT_FOUND) {
357 return existingKeyId;
358 }
359 // if it's new find the first unoccupied slot in the public keys
dcashmand79fdf12014-06-20 15:53:06 -0700360 long id = getFreePublicKeyIdLPw();
Geremy Condraf1bcca82013-01-07 22:35:24 -0800361 // add the public key to it
362 mPublicKeys.put(id, key);
363 // return the stable identifier
364 return id;
365 }
366
Kenny Root2042e9d2013-07-08 09:31:31 -0700367 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800368 * Finds the stable identifier for a KeySet based on a set of PublicKey stable IDs.
369 *
370 * Returns KEYSET_NOT_FOUND if there isn't one.
371 */
dcashmand79fdf12014-06-20 15:53:06 -0700372 private long getIdFromKeyIdsLPr(Set<Long> publicKeyIds) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800373 for (int keyMapIndex = 0; keyMapIndex < mKeySetMapping.size(); keyMapIndex++) {
dcashman9d2f4412014-06-09 09:27:54 -0700374 ArraySet<Long> value = mKeySetMapping.valueAt(keyMapIndex);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800375 if (value.equals(publicKeyIds)) {
376 return mKeySetMapping.keyAt(keyMapIndex);
377 }
378 }
379 return KEYSET_NOT_FOUND;
380 }
381
Kenny Root2042e9d2013-07-08 09:31:31 -0700382 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800383 * Finds the stable identifier for a PublicKey or PUBLIC_KEY_NOT_FOUND.
384 */
dcashmand79fdf12014-06-20 15:53:06 -0700385 private long getIdForPublicKeyLPr(PublicKey k) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800386 String encodedPublicKey = new String(k.getEncoded());
387 for (int publicKeyIndex = 0; publicKeyIndex < mPublicKeys.size(); publicKeyIndex++) {
388 PublicKey value = mPublicKeys.valueAt(publicKeyIndex);
389 String encodedExistingKey = new String(value.getEncoded());
390 if (encodedPublicKey.equals(encodedExistingKey)) {
391 return mPublicKeys.keyAt(publicKeyIndex);
392 }
393 }
394 return PUBLIC_KEY_NOT_FOUND;
395 }
396
Kenny Root2042e9d2013-07-08 09:31:31 -0700397 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800398 * Gets an unused stable identifier for a KeySet.
399 */
dcashmand79fdf12014-06-20 15:53:06 -0700400 private long getFreeKeySetIDLPw() {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800401 lastIssuedKeySetId += 1;
402 return lastIssuedKeySetId;
403 }
404
Kenny Root2042e9d2013-07-08 09:31:31 -0700405 /**
Geremy Condraf1bcca82013-01-07 22:35:24 -0800406 * Same as above, but for public keys.
407 */
dcashmand79fdf12014-06-20 15:53:06 -0700408 private long getFreePublicKeyIdLPw() {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800409 lastIssuedKeyId += 1;
410 return lastIssuedKeyId;
411 }
412
dcashmand79fdf12014-06-20 15:53:06 -0700413 public void removeAppKeySetDataLPw(String packageName) {
414 // Get the package's known keys and KeySets
415 ArraySet<Long> deletableKeySets = getOriginalKeySetsByPackageNameLPr(packageName);
416 ArraySet<Long> deletableKeys = new ArraySet<Long>();
Andreas Gampe08882762015-03-05 13:13:55 -0800417 final int origDksSize = deletableKeySets.size();
418 for (int i = 0; i < origDksSize; i++) {
419 ArraySet<Long> knownKeys = mKeySetMapping.get(deletableKeySets.valueAt(i));
dcashmand79fdf12014-06-20 15:53:06 -0700420 if (knownKeys != null) {
421 deletableKeys.addAll(knownKeys);
422 }
423 }
424
425 // Now remove the keys and KeySets on which any other package relies
426 for (String pkgName : mPackages.keySet()) {
427 if (pkgName.equals(packageName)) {
428 continue;
429 }
430 ArraySet<Long> knownKeySets = getOriginalKeySetsByPackageNameLPr(pkgName);
431 deletableKeySets.removeAll(knownKeySets);
Andreas Gampe08882762015-03-05 13:13:55 -0800432 final int kksSize = knownKeySets.size();
433 for (int i = 0; i < kksSize; i++) {
434 ArraySet<Long> knownKeys = mKeySetMapping.get(knownKeySets.valueAt(i));
Geremy Condracdb57892013-03-26 16:22:36 -0700435 if (knownKeys != null) {
dcashmand79fdf12014-06-20 15:53:06 -0700436 deletableKeys.removeAll(knownKeys);
Geremy Condracdb57892013-03-26 16:22:36 -0700437 }
Geremy Condraf1bcca82013-01-07 22:35:24 -0800438 }
Geremy Condraf1bcca82013-01-07 22:35:24 -0800439 }
dcashmand79fdf12014-06-20 15:53:06 -0700440
441 // The remaining keys and KeySets are not relied on by any other
442 // application and so can be safely deleted.
Andreas Gampe08882762015-03-05 13:13:55 -0800443 final int dksSize = deletableKeySets.size();
444 for (int i = 0; i < dksSize; i++) {
445 Long ks = deletableKeySets.valueAt(i);
dcashmand79fdf12014-06-20 15:53:06 -0700446 mKeySets.delete(ks);
447 mKeySetMapping.delete(ks);
448 }
Andreas Gampe08882762015-03-05 13:13:55 -0800449 final int dkSize = deletableKeys.size();
450 for (int i = 0; i < dkSize; i++) {
451 mPublicKeys.delete(deletableKeys.valueAt(i));
dcashmand79fdf12014-06-20 15:53:06 -0700452 }
453
454 // Now remove the deleted KeySets from each package's signingKeySets
455 for (String pkgName : mPackages.keySet()) {
456 PackageSetting p = mPackages.get(pkgName);
Andreas Gampe08882762015-03-05 13:13:55 -0800457 for (int i = 0; i < dksSize; i++) {
458 Long ks = deletableKeySets.valueAt(i);
dcashmand79fdf12014-06-20 15:53:06 -0700459 p.keySetData.removeSigningKeySet(ks);
460 }
461 }
462 // Finally, remove all KeySets from the original package
463 PackageSetting p = mPackages.get(packageName);
464 clearPackageKeySetDataLPw(p);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800465 }
466
dcashmand79fdf12014-06-20 15:53:06 -0700467 private void clearPackageKeySetDataLPw(PackageSetting p) {
dcashman55b10782014-04-09 14:20:38 -0700468 p.keySetData.removeAllSigningKeySets();
469 p.keySetData.removeAllUpgradeKeySets();
470 p.keySetData.removeAllDefinedKeySets();
471 return;
472 }
473
dcashmand79fdf12014-06-20 15:53:06 -0700474 private ArraySet<Long> getOriginalKeySetsByPackageNameLPr(String packageName) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800475 PackageSetting p = mPackages.get(packageName);
476 if (p == null) {
477 throw new NullPointerException("Unknown package");
478 }
479 if (p.keySetData == null) {
480 throw new IllegalArgumentException("Package has no keySet data");
481 }
dcashmand79fdf12014-06-20 15:53:06 -0700482 ArraySet<Long> knownKeySets = new ArraySet<Long>();
dcashman55b10782014-04-09 14:20:38 -0700483 knownKeySets.add(p.keySetData.getProperSigningKeySet());
484 if (p.keySetData.isUsingDefinedKeySets()) {
485 for (long ks : p.keySetData.getDefinedKeySets()) {
486 knownKeySets.add(ks);
487 }
Geremy Condraf1bcca82013-01-07 22:35:24 -0800488 }
489 return knownKeySets;
490 }
491
492 public String encodePublicKey(PublicKey k) throws IOException {
493 return new String(Base64.encode(k.getEncoded(), 0));
494 }
495
dcashmand79fdf12014-06-20 15:53:06 -0700496 public void dumpLPr(PrintWriter pw, String packageName,
497 PackageManagerService.DumpState dumpState) {
498 boolean printedHeader = false;
499 for (Map.Entry<String, PackageSetting> e : mPackages.entrySet()) {
500 String keySetPackage = e.getKey();
501 if (packageName != null && !packageName.equals(keySetPackage)) {
502 continue;
503 }
504 if (!printedHeader) {
505 if (dumpState.onTitlePrinted())
506 pw.println();
507 pw.println("Key Set Manager:");
508 printedHeader = true;
509 }
510 PackageSetting pkg = e.getValue();
511 pw.print(" ["); pw.print(keySetPackage); pw.println("]");
512 if (pkg.keySetData != null) {
513 boolean printedLabel = false;
514 for (Map.Entry<String, Long> entry : pkg.keySetData.getAliases().entrySet()) {
515 if (!printedLabel) {
516 pw.print(" KeySets Aliases: ");
517 printedLabel = true;
518 } else {
519 pw.print(", ");
520 }
521 pw.print(entry.getKey());
522 pw.print('=');
523 pw.print(Long.toString(entry.getValue()));
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700524 }
dcashmand79fdf12014-06-20 15:53:06 -0700525 if (printedLabel) {
526 pw.println("");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700527 }
dcashmand79fdf12014-06-20 15:53:06 -0700528 printedLabel = false;
529 if (pkg.keySetData.isUsingDefinedKeySets()) {
530 for (long keySetId : pkg.keySetData.getDefinedKeySets()) {
Kenny Rootdf0e6ab2013-07-03 13:38:31 -0700531 if (!printedLabel) {
dcashmand79fdf12014-06-20 15:53:06 -0700532 pw.print(" Defined KeySets: ");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700533 printedLabel = true;
534 } else {
535 pw.print(", ");
536 }
Kenny Rootdf0e6ab2013-07-03 13:38:31 -0700537 pw.print(Long.toString(keySetId));
Geremy Condraf1bcca82013-01-07 22:35:24 -0800538 }
dcashmand79fdf12014-06-20 15:53:06 -0700539 }
540 if (printedLabel) {
541 pw.println("");
542 }
543 printedLabel = false;
Christopher Tate6441bc72014-09-03 16:11:31 -0700544 final long[] signingKeySets = pkg.keySetData.getSigningKeySets();
545 if (signingKeySets != null) {
546 for (long keySetId : signingKeySets) {
547 if (!printedLabel) {
548 pw.print(" Signing KeySets: ");
549 printedLabel = true;
550 } else {
551 pw.print(", ");
552 }
553 pw.print(Long.toString(keySetId));
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700554 }
dcashmand79fdf12014-06-20 15:53:06 -0700555 }
556 if (printedLabel) {
557 pw.println("");
558 }
559 printedLabel = false;
560 if (pkg.keySetData.isUsingUpgradeKeySets()) {
561 for (long keySetId : pkg.keySetData.getUpgradeKeySets()) {
562 if (!printedLabel) {
563 pw.print(" Upgrade KeySets: ");
564 printedLabel = true;
565 } else {
566 pw.print(", ");
dcashman55b10782014-04-09 14:20:38 -0700567 }
dcashmand79fdf12014-06-20 15:53:06 -0700568 pw.print(Long.toString(keySetId));
dcashman55b10782014-04-09 14:20:38 -0700569 }
dcashmand79fdf12014-06-20 15:53:06 -0700570 }
571 if (printedLabel) {
572 pw.println("");
Geremy Condraf1bcca82013-01-07 22:35:24 -0800573 }
574 }
575 }
576 }
577
dcashman55b10782014-04-09 14:20:38 -0700578 void writeKeySetManagerServiceLPr(XmlSerializer serializer) throws IOException {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800579 serializer.startTag(null, "keyset-settings");
dcashman55b10782014-04-09 14:20:38 -0700580 serializer.attribute(null, "version", Integer.toString(CURRENT_VERSION));
Geremy Condraf1bcca82013-01-07 22:35:24 -0800581 writePublicKeysLPr(serializer);
582 writeKeySetsLPr(serializer);
583 serializer.startTag(null, "lastIssuedKeyId");
584 serializer.attribute(null, "value", Long.toString(lastIssuedKeyId));
585 serializer.endTag(null, "lastIssuedKeyId");
586 serializer.startTag(null, "lastIssuedKeySetId");
587 serializer.attribute(null, "value", Long.toString(lastIssuedKeySetId));
588 serializer.endTag(null, "lastIssuedKeySetId");
589 serializer.endTag(null, "keyset-settings");
590 }
591
592 void writePublicKeysLPr(XmlSerializer serializer) throws IOException {
593 serializer.startTag(null, "keys");
594 for (int pKeyIndex = 0; pKeyIndex < mPublicKeys.size(); pKeyIndex++) {
595 long id = mPublicKeys.keyAt(pKeyIndex);
596 PublicKey key = mPublicKeys.valueAt(pKeyIndex);
597 String encodedKey = encodePublicKey(key);
598 serializer.startTag(null, "public-key");
599 serializer.attribute(null, "identifier", Long.toString(id));
600 serializer.attribute(null, "value", encodedKey);
601 serializer.endTag(null, "public-key");
602 }
603 serializer.endTag(null, "keys");
604 }
605
606 void writeKeySetsLPr(XmlSerializer serializer) throws IOException {
607 serializer.startTag(null, "keysets");
608 for (int keySetIndex = 0; keySetIndex < mKeySetMapping.size(); keySetIndex++) {
609 long id = mKeySetMapping.keyAt(keySetIndex);
dcashman9d2f4412014-06-09 09:27:54 -0700610 ArraySet<Long> keys = mKeySetMapping.valueAt(keySetIndex);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800611 serializer.startTag(null, "keyset");
612 serializer.attribute(null, "identifier", Long.toString(id));
613 for (long keyId : keys) {
614 serializer.startTag(null, "key-id");
615 serializer.attribute(null, "identifier", Long.toString(keyId));
616 serializer.endTag(null, "key-id");
617 }
618 serializer.endTag(null, "keyset");
619 }
620 serializer.endTag(null, "keysets");
621 }
622
623 void readKeySetsLPw(XmlPullParser parser)
624 throws XmlPullParserException, IOException {
625 int type;
626 long currentKeySetId = 0;
dcashman55b10782014-04-09 14:20:38 -0700627 int outerDepth = parser.getDepth();
628 String recordedVersion = parser.getAttributeValue(null, "version");
629 if (recordedVersion == null || Integer.parseInt(recordedVersion) != CURRENT_VERSION) {
630 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
631 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
632 // Our version is different than the one which generated the old keyset data.
633 // We don't want any of the old data, but we must advance the parser
634 continue;
635 }
636 // The KeySet information read previously from packages.xml is invalid.
637 // Destroy it all.
638 for (PackageSetting p : mPackages.values()) {
dcashmand79fdf12014-06-20 15:53:06 -0700639 clearPackageKeySetDataLPw(p);
dcashman55b10782014-04-09 14:20:38 -0700640 }
641 return;
642 }
643 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
644 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800645 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
646 continue;
647 }
648 final String tagName = parser.getName();
649 if (tagName.equals("keys")) {
650 readKeysLPw(parser);
651 } else if (tagName.equals("keysets")) {
652 readKeySetListLPw(parser);
dcashman55b10782014-04-09 14:20:38 -0700653 } else if (tagName.equals("lastIssuedKeyId")) {
654 lastIssuedKeyId = Long.parseLong(parser.getAttributeValue(null, "value"));
655 } else if (tagName.equals("lastIssuedKeySetId")) {
656 lastIssuedKeySetId = Long.parseLong(parser.getAttributeValue(null, "value"));
Geremy Condraf1bcca82013-01-07 22:35:24 -0800657 }
658 }
659 }
660
661 void readKeysLPw(XmlPullParser parser)
662 throws XmlPullParserException, IOException {
663 int outerDepth = parser.getDepth();
664 int type;
665 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
666 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
667 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
668 continue;
669 }
670 final String tagName = parser.getName();
671 if (tagName.equals("public-key")) {
672 readPublicKeyLPw(parser);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800673 }
674 }
675 }
676
677 void readKeySetListLPw(XmlPullParser parser)
678 throws XmlPullParserException, IOException {
679 int outerDepth = parser.getDepth();
680 int type;
681 long currentKeySetId = 0;
682 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
683 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
684 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
685 continue;
686 }
687 final String tagName = parser.getName();
688 if (tagName.equals("keyset")) {
689 currentKeySetId = readIdentifierLPw(parser);
dcashman9d2f4412014-06-09 09:27:54 -0700690 mKeySets.put(currentKeySetId, new KeySetHandle());
dcashman55b10782014-04-09 14:20:38 -0700691 mKeySetMapping.put(currentKeySetId, new ArraySet<Long>());
Geremy Condraf1bcca82013-01-07 22:35:24 -0800692 } else if (tagName.equals("key-id")) {
693 long id = readIdentifierLPw(parser);
694 mKeySetMapping.get(currentKeySetId).add(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800695 }
696 }
697 }
698
699 long readIdentifierLPw(XmlPullParser parser)
700 throws XmlPullParserException {
701 return Long.parseLong(parser.getAttributeValue(null, "identifier"));
702 }
703
704 void readPublicKeyLPw(XmlPullParser parser)
705 throws XmlPullParserException {
706 String encodedID = parser.getAttributeValue(null, "identifier");
707 long identifier = Long.parseLong(encodedID);
708 String encodedPublicKey = parser.getAttributeValue(null, "value");
709 PublicKey pub = PackageParser.parsePublicKey(encodedPublicKey);
Geremy Condraa2d8eae2013-06-21 16:59:42 -0700710 if (pub != null) {
Geremy Condraf1bcca82013-01-07 22:35:24 -0800711 mPublicKeys.put(identifier, pub);
712 }
713 }
Ying Wangfb236b52013-07-08 10:53:58 -0700714}