Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 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 | |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 19 | import android.content.pm.PackageParser; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 20 | import android.util.ArraySet; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 21 | import android.util.Base64; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 22 | import android.util.Slog; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 23 | import android.util.LongSparseArray; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.io.PrintWriter; |
| 27 | import java.security.PublicKey; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 28 | import java.util.Map; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import org.xmlpull.v1.XmlPullParser; |
| 32 | import org.xmlpull.v1.XmlPullParserException; |
| 33 | import org.xmlpull.v1.XmlSerializer; |
| 34 | |
| 35 | /* |
| 36 | * Manages system-wide KeySet state. |
| 37 | */ |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 38 | public class KeySetManagerService { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 39 | |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 40 | 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 46 | |
Kenny Root | 9a995f5 | 2013-07-08 09:33:22 -0700 | [diff] [blame] | 47 | /** 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. */ |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 51 | protected static final long PUBLIC_KEY_NOT_FOUND = -1; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 52 | |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 53 | private final LongSparseArray<KeySetHandle> mKeySets; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 54 | |
| 55 | private final LongSparseArray<PublicKey> mPublicKeys; |
| 56 | |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 57 | protected final LongSparseArray<ArraySet<Long>> mKeySetMapping; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 58 | |
| 59 | private final Map<String, PackageSetting> mPackages; |
| 60 | |
| 61 | private static long lastIssuedKeySetId = 0; |
| 62 | |
| 63 | private static long lastIssuedKeyId = 0; |
| 64 | |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 65 | public KeySetManagerService(Map<String, PackageSetting> packages) { |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 66 | mKeySets = new LongSparseArray<KeySetHandle>(); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 67 | mPublicKeys = new LongSparseArray<PublicKey>(); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 68 | mKeySetMapping = new LongSparseArray<ArraySet<Long>>(); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 69 | mPackages = packages; |
| 70 | } |
| 71 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 72 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 73 | * 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 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 83 | public boolean packageIsSignedByLPr(String packageName, KeySetHandle ks) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 84 | PackageSetting pkg = mPackages.get(packageName); |
| 85 | if (pkg == null) { |
| 86 | throw new NullPointerException("Invalid package name"); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 87 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 88 | if (pkg.keySetData == null) { |
| 89 | throw new NullPointerException("Package has no KeySet data"); |
| 90 | } |
| 91 | long id = getIdByKeySetLPr(ks); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 92 | if (id == KEYSET_NOT_FOUND) { |
| 93 | return false; |
| 94 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 95 | return pkg.keySetData.packageIsSignedBy(id); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 98 | /** |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 99 | * 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 122 | * 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 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 126 | public void addDefinedKeySetToPackageLPw(String packageName, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 127 | ArraySet<PublicKey> keys, String alias) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 128 | if ((packageName == null) || (keys == null) || (alias == null)) { |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 129 | Slog.w(TAG, "Got null argument for a defined keyset, ignoring!"); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 130 | return; |
| 131 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 132 | PackageSetting pkg = mPackages.get(packageName); |
| 133 | if (pkg == null) { |
| 134 | throw new NullPointerException("Unknown package"); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 135 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 136 | // Add to KeySets, then to package |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 137 | KeySetHandle ks = addKeySetLPw(keys); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 138 | long id = getIdByKeySetLPr(ks); |
| 139 | pkg.keySetData.addDefinedKeySet(id, alias); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 142 | /** |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 143 | * 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 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 147 | public void addUpgradeKeySetToPackageLPw(String packageName, String alias) { |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 148 | if ((packageName == null) || (alias == null)) { |
| 149 | Slog.w(TAG, "Got null argument for a defined keyset, ignoring!"); |
| 150 | return; |
| 151 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 152 | PackageSetting pkg = mPackages.get(packageName); |
| 153 | if (pkg == null) { |
| 154 | throw new NullPointerException("Unknown package"); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 155 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 156 | pkg.keySetData.addUpgradeKeySet(alias); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 160 | * Similar to the above, this informs the system that the given package |
| 161 | * was signed by the provided KeySet. |
| 162 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 163 | public void addSigningKeySetToPackageLPw(String packageName, |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 164 | ArraySet<PublicKey> signingKeys) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 165 | if ((packageName == null) || (signingKeys == null)) { |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 166 | Slog.w(TAG, "Got null argument for a signing keyset, ignoring!"); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 167 | return; |
| 168 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 169 | // add the signing KeySet |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 170 | KeySetHandle ks = addKeySetLPw(signingKeys); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 171 | long id = getIdByKeySetLPr(ks); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 172 | ArraySet<Long> publicKeyIds = mKeySetMapping.get(id); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 173 | if (publicKeyIds == null) { |
| 174 | throw new NullPointerException("Got invalid KeySet id"); |
| 175 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 176 | // 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); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 186 | ArraySet<Long> definedKeys = mKeySetMapping.get(keySetID); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 187 | if (publicKeyIds.containsAll(definedKeys)) { |
| 188 | pkg.keySetData.addSigningKeySet(keySetID); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 193 | /** |
| 194 | * Fetches the stable identifier associated with the given KeySet. Returns |
| 195 | * {@link #KEYSET_NOT_FOUND} if the KeySet... wasn't found. |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 196 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 197 | private long getIdByKeySetLPr(KeySetHandle ks) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 198 | for (int keySetIndex = 0; keySetIndex < mKeySets.size(); keySetIndex++) { |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 199 | KeySetHandle value = mKeySets.valueAt(keySetIndex); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 200 | if (ks.equals(value)) { |
| 201 | return mKeySets.keyAt(keySetIndex); |
| 202 | } |
| 203 | } |
| 204 | return KEYSET_NOT_FOUND; |
| 205 | } |
| 206 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 207 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 208 | * Fetches the KeySet corresponding to the given stable identifier. |
| 209 | * |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 210 | * Returns {@link #KEYSET_NOT_FOUND} if the identifier doesn't |
| 211 | * identify a {@link KeySet}. |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 212 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 213 | public KeySetHandle getKeySetByIdLPr(long id) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 214 | return mKeySets.get(id); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 217 | /** |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 218 | * 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 221 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 222 | public KeySetHandle getKeySetByAliasAndPackageNameLPr(String packageName, String alias) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 223 | PackageSetting p = mPackages.get(packageName); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 224 | if (p == null || p.keySetData == null) { |
| 225 | return null; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 226 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 227 | Long keySetId = p.keySetData.getAliases().get(alias); |
| 228 | if (keySetId == null) { |
| 229 | throw new IllegalArgumentException("Unknown KeySet alias: " + alias); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 230 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 231 | return mKeySets.get(keySetId); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 234 | /** |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 235 | * Fetches the {@link PublicKey public keys} which belong to the specified |
| 236 | * KeySet id. |
| 237 | * |
| 238 | * Returns {@code null} if the identifier doesn't |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 239 | * identify a {@link KeySetHandle}. |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 240 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 241 | public ArraySet<PublicKey> getPublicKeysFromKeySetLPr(long id) { |
| 242 | if(mKeySetMapping.get(id) == null) { |
| 243 | return null; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 244 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 245 | ArraySet<PublicKey> mPubKeys = new ArraySet<PublicKey>(); |
| 246 | for (long pkId : mKeySetMapping.get(id)) { |
| 247 | mPubKeys.add(mPublicKeys.get(pkId)); |
| 248 | } |
| 249 | return mPubKeys; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 253 | * Fetches the proper {@link KeySetHandle KeySet} that signed the given |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 254 | * package. |
| 255 | * |
| 256 | * @throws IllegalArgumentException if the package has no keyset data. |
| 257 | * @throws NullPointerException if the package is unknown. |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 258 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 259 | public KeySetHandle getSigningKeySetByPackageNameLPr(String packageName) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 260 | PackageSetting p = mPackages.get(packageName); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 261 | if (p == null |
| 262 | || p.keySetData == null |
| 263 | || p.keySetData.getProperSigningKeySet() |
| 264 | == PackageKeySetData.KEYSET_UNASSIGNED) { |
| 265 | return null; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 266 | } |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 267 | return mKeySets.get(p.keySetData.getProperSigningKeySet()); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 271 | * Fetches all the known {@link KeySetHandle KeySets} that may upgrade the given |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 272 | * package. |
| 273 | * |
| 274 | * @throws IllegalArgumentException if the package has no keyset data. |
| 275 | * @throws NullPointerException if the package is unknown. |
| 276 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 277 | public ArraySet<KeySetHandle> getUpgradeKeySetsByPackageNameLPr(String packageName) { |
| 278 | ArraySet<KeySetHandle> upgradeKeySets = new ArraySet<KeySetHandle>(); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 279 | PackageSetting p = mPackages.get(packageName); |
| 280 | if (p == null) { |
| 281 | throw new NullPointerException("Unknown package"); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 282 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 283 | 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 294 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 295 | * Creates a new KeySet corresponding to the given keys. |
| 296 | * |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 297 | * If the {@link PublicKey PublicKeys} aren't known to the system, this |
| 298 | * adds them. Otherwise, they're deduped. |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 299 | * |
| 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 | * |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 303 | * If the KeySet isn't known to the system, this adds it to all appropriate |
| 304 | * signingKeySets |
| 305 | * |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 306 | * Throws if the provided set is {@code null}. |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 307 | */ |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 308 | private KeySetHandle addKeySetLPw(ArraySet<PublicKey> keys) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 309 | if (keys == null) { |
| 310 | throw new NullPointerException("Provided keys cannot be null"); |
| 311 | } |
| 312 | // add each of the keys in the provided set |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 313 | ArraySet<Long> addedKeyIds = new ArraySet<Long>(keys.size()); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 314 | for (PublicKey k : keys) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 315 | long id = addPublicKeyLPw(k); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 316 | addedKeyIds.add(id); |
| 317 | } |
| 318 | |
| 319 | // check to see if the resulting keyset is new |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 320 | long existingKeySetId = getIdFromKeyIdsLPr(addedKeyIds); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 321 | if (existingKeySetId != KEYSET_NOT_FOUND) { |
| 322 | return mKeySets.get(existingKeySetId); |
| 323 | } |
| 324 | |
| 325 | // create the KeySet object |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 326 | KeySetHandle ks = new KeySetHandle(); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 327 | // get the first unoccupied slot in mKeySets |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 328 | long id = getFreeKeySetIDLPw(); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 329 | // 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); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 333 | // 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) { |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 339 | ArraySet<Long> pSigningKeys = mKeySetMapping.get(pProperSigning); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 340 | if (pSigningKeys.containsAll(addedKeyIds)) { |
| 341 | p.keySetData.addSigningKeySet(id); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 346 | // go home |
| 347 | return ks; |
| 348 | } |
| 349 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 350 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 351 | * Adds the given PublicKey to the system, deduping as it goes. |
| 352 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 353 | private long addPublicKeyLPw(PublicKey key) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 354 | // check if the public key is new |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 355 | long existingKeyId = getIdForPublicKeyLPr(key); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 356 | if (existingKeyId != PUBLIC_KEY_NOT_FOUND) { |
| 357 | return existingKeyId; |
| 358 | } |
| 359 | // if it's new find the first unoccupied slot in the public keys |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 360 | long id = getFreePublicKeyIdLPw(); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 361 | // add the public key to it |
| 362 | mPublicKeys.put(id, key); |
| 363 | // return the stable identifier |
| 364 | return id; |
| 365 | } |
| 366 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 367 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 368 | * 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 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 372 | private long getIdFromKeyIdsLPr(Set<Long> publicKeyIds) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 373 | for (int keyMapIndex = 0; keyMapIndex < mKeySetMapping.size(); keyMapIndex++) { |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 374 | ArraySet<Long> value = mKeySetMapping.valueAt(keyMapIndex); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 375 | if (value.equals(publicKeyIds)) { |
| 376 | return mKeySetMapping.keyAt(keyMapIndex); |
| 377 | } |
| 378 | } |
| 379 | return KEYSET_NOT_FOUND; |
| 380 | } |
| 381 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 382 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 383 | * Finds the stable identifier for a PublicKey or PUBLIC_KEY_NOT_FOUND. |
| 384 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 385 | private long getIdForPublicKeyLPr(PublicKey k) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 386 | 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 Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 397 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 398 | * Gets an unused stable identifier for a KeySet. |
| 399 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 400 | private long getFreeKeySetIDLPw() { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 401 | lastIssuedKeySetId += 1; |
| 402 | return lastIssuedKeySetId; |
| 403 | } |
| 404 | |
Kenny Root | 2042e9d | 2013-07-08 09:31:31 -0700 | [diff] [blame] | 405 | /** |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 406 | * Same as above, but for public keys. |
| 407 | */ |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 408 | private long getFreePublicKeyIdLPw() { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 409 | lastIssuedKeyId += 1; |
| 410 | return lastIssuedKeyId; |
| 411 | } |
| 412 | |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 413 | 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 Gampe | 0888276 | 2015-03-05 13:13:55 -0800 | [diff] [blame] | 417 | final int origDksSize = deletableKeySets.size(); |
| 418 | for (int i = 0; i < origDksSize; i++) { |
| 419 | ArraySet<Long> knownKeys = mKeySetMapping.get(deletableKeySets.valueAt(i)); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 420 | 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 Gampe | 0888276 | 2015-03-05 13:13:55 -0800 | [diff] [blame] | 432 | final int kksSize = knownKeySets.size(); |
| 433 | for (int i = 0; i < kksSize; i++) { |
| 434 | ArraySet<Long> knownKeys = mKeySetMapping.get(knownKeySets.valueAt(i)); |
Geremy Condra | cdb5789 | 2013-03-26 16:22:36 -0700 | [diff] [blame] | 435 | if (knownKeys != null) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 436 | deletableKeys.removeAll(knownKeys); |
Geremy Condra | cdb5789 | 2013-03-26 16:22:36 -0700 | [diff] [blame] | 437 | } |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 438 | } |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 439 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 440 | |
| 441 | // The remaining keys and KeySets are not relied on by any other |
| 442 | // application and so can be safely deleted. |
Andreas Gampe | 0888276 | 2015-03-05 13:13:55 -0800 | [diff] [blame] | 443 | final int dksSize = deletableKeySets.size(); |
| 444 | for (int i = 0; i < dksSize; i++) { |
| 445 | Long ks = deletableKeySets.valueAt(i); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 446 | mKeySets.delete(ks); |
| 447 | mKeySetMapping.delete(ks); |
| 448 | } |
Andreas Gampe | 0888276 | 2015-03-05 13:13:55 -0800 | [diff] [blame] | 449 | final int dkSize = deletableKeys.size(); |
| 450 | for (int i = 0; i < dkSize; i++) { |
| 451 | mPublicKeys.delete(deletableKeys.valueAt(i)); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 452 | } |
| 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 Gampe | 0888276 | 2015-03-05 13:13:55 -0800 | [diff] [blame] | 457 | for (int i = 0; i < dksSize; i++) { |
| 458 | Long ks = deletableKeySets.valueAt(i); |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 459 | 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 465 | } |
| 466 | |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 467 | private void clearPackageKeySetDataLPw(PackageSetting p) { |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 468 | p.keySetData.removeAllSigningKeySets(); |
| 469 | p.keySetData.removeAllUpgradeKeySets(); |
| 470 | p.keySetData.removeAllDefinedKeySets(); |
| 471 | return; |
| 472 | } |
| 473 | |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 474 | private ArraySet<Long> getOriginalKeySetsByPackageNameLPr(String packageName) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 475 | 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 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 482 | ArraySet<Long> knownKeySets = new ArraySet<Long>(); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 483 | knownKeySets.add(p.keySetData.getProperSigningKeySet()); |
| 484 | if (p.keySetData.isUsingDefinedKeySets()) { |
| 485 | for (long ks : p.keySetData.getDefinedKeySets()) { |
| 486 | knownKeySets.add(ks); |
| 487 | } |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 488 | } |
| 489 | return knownKeySets; |
| 490 | } |
| 491 | |
| 492 | public String encodePublicKey(PublicKey k) throws IOException { |
| 493 | return new String(Base64.encode(k.getEncoded(), 0)); |
| 494 | } |
| 495 | |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 496 | 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 Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 524 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 525 | if (printedLabel) { |
| 526 | pw.println(""); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 527 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 528 | printedLabel = false; |
| 529 | if (pkg.keySetData.isUsingDefinedKeySets()) { |
| 530 | for (long keySetId : pkg.keySetData.getDefinedKeySets()) { |
Kenny Root | df0e6ab | 2013-07-03 13:38:31 -0700 | [diff] [blame] | 531 | if (!printedLabel) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 532 | pw.print(" Defined KeySets: "); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 533 | printedLabel = true; |
| 534 | } else { |
| 535 | pw.print(", "); |
| 536 | } |
Kenny Root | df0e6ab | 2013-07-03 13:38:31 -0700 | [diff] [blame] | 537 | pw.print(Long.toString(keySetId)); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 538 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 539 | } |
| 540 | if (printedLabel) { |
| 541 | pw.println(""); |
| 542 | } |
| 543 | printedLabel = false; |
Christopher Tate | 6441bc7 | 2014-09-03 16:11:31 -0700 | [diff] [blame] | 544 | 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 Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 554 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 555 | } |
| 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(", "); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 567 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 568 | pw.print(Long.toString(keySetId)); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 569 | } |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 570 | } |
| 571 | if (printedLabel) { |
| 572 | pw.println(""); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 578 | void writeKeySetManagerServiceLPr(XmlSerializer serializer) throws IOException { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 579 | serializer.startTag(null, "keyset-settings"); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 580 | serializer.attribute(null, "version", Integer.toString(CURRENT_VERSION)); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 581 | 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); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 610 | ArraySet<Long> keys = mKeySetMapping.valueAt(keySetIndex); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 611 | 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; |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 627 | 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()) { |
dcashman | d79fdf1 | 2014-06-20 15:53:06 -0700 | [diff] [blame] | 639 | clearPackageKeySetDataLPw(p); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 640 | } |
| 641 | return; |
| 642 | } |
| 643 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 644 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 645 | 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); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 653 | } 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 657 | } |
| 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 Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 673 | } |
| 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); |
dcashman | 9d2f441 | 2014-06-09 09:27:54 -0700 | [diff] [blame] | 690 | mKeySets.put(currentKeySetId, new KeySetHandle()); |
dcashman | 55b1078 | 2014-04-09 14:20:38 -0700 | [diff] [blame] | 691 | mKeySetMapping.put(currentKeySetId, new ArraySet<Long>()); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 692 | } else if (tagName.equals("key-id")) { |
| 693 | long id = readIdentifierLPw(parser); |
| 694 | mKeySetMapping.get(currentKeySetId).add(id); |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 695 | } |
| 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 Condra | a2d8eae | 2013-06-21 16:59:42 -0700 | [diff] [blame] | 710 | if (pub != null) { |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 711 | mPublicKeys.put(identifier, pub); |
| 712 | } |
| 713 | } |
Ying Wang | fb236b5 | 2013-07-08 10:53:58 -0700 | [diff] [blame] | 714 | } |