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 | |
| 19 | import java.util.Arrays; |
| 20 | import java.util.HashMap; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 21 | import java.util.Map; |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 22 | |
| 23 | public class PackageKeySetData { |
| 24 | |
| 25 | private long[] mSigningKeySets; |
| 26 | |
| 27 | private long[] mDefinedKeySets; |
| 28 | |
| 29 | private final Map<String, Long> mKeySetAliases; |
| 30 | |
| 31 | PackageKeySetData() { |
| 32 | mSigningKeySets = new long[0]; |
| 33 | mDefinedKeySets = new long[0]; |
| 34 | mKeySetAliases = new HashMap<String, Long>(); |
| 35 | } |
| 36 | |
| 37 | PackageKeySetData(PackageKeySetData original) { |
| 38 | mSigningKeySets = original.getSigningKeySets().clone(); |
| 39 | mDefinedKeySets = original.getDefinedKeySets().clone(); |
| 40 | mKeySetAliases = new HashMap<String, Long>(); |
| 41 | mKeySetAliases.putAll(original.getAliases()); |
| 42 | } |
| 43 | |
| 44 | public void addSigningKeySet(long ks) { |
| 45 | // deduplicate |
| 46 | for (long knownKeySet : mSigningKeySets) { |
| 47 | if (ks == knownKeySet) { |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | int end = mSigningKeySets.length; |
| 52 | mSigningKeySets = Arrays.copyOf(mSigningKeySets, end + 1); |
| 53 | mSigningKeySets[end] = ks; |
| 54 | } |
| 55 | |
Geremy Condra | cdb5789 | 2013-03-26 16:22:36 -0700 | [diff] [blame] | 56 | public void removeSigningKeySet(long ks) { |
| 57 | if (packageIsSignedBy(ks)) { |
| 58 | long[] keysets = new long[mSigningKeySets.length - 1]; |
| 59 | int index = 0; |
| 60 | for (long signingKeySet : mSigningKeySets) { |
| 61 | if (signingKeySet != ks) { |
| 62 | keysets[index] = signingKeySet; |
| 63 | index += 1; |
| 64 | } |
| 65 | } |
| 66 | mSigningKeySets = keysets; |
| 67 | } |
| 68 | } |
| 69 | |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 70 | public void addDefinedKeySet(long ks, String alias) { |
| 71 | // deduplicate |
| 72 | for (long knownKeySet : mDefinedKeySets) { |
| 73 | if (ks == knownKeySet) { |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | int end = mDefinedKeySets.length; |
| 78 | mDefinedKeySets = Arrays.copyOf(mDefinedKeySets, end + 1); |
| 79 | mDefinedKeySets[end] = ks; |
| 80 | mKeySetAliases.put(alias, ks); |
| 81 | } |
| 82 | |
Geremy Condra | cdb5789 | 2013-03-26 16:22:36 -0700 | [diff] [blame] | 83 | public void removeDefinedKeySet(long ks) { |
| 84 | if (mKeySetAliases.containsValue(ks)) { |
| 85 | long[] keysets = new long[mDefinedKeySets.length - 1]; |
| 86 | int index = 0; |
| 87 | for (long definedKeySet : mDefinedKeySets) { |
| 88 | if (definedKeySet != ks) { |
| 89 | keysets[index] = definedKeySet; |
| 90 | index += 1; |
| 91 | } |
| 92 | } |
| 93 | mDefinedKeySets = keysets; |
| 94 | for (String alias : mKeySetAliases.keySet()) { |
| 95 | if (mKeySetAliases.get(alias) == ks) { |
| 96 | mKeySetAliases.remove(alias); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Geremy Condra | f1bcca8 | 2013-01-07 22:35:24 -0800 | [diff] [blame] | 103 | public boolean packageIsSignedBy(long ks) { |
| 104 | for (long signingKeySet : mSigningKeySets) { |
| 105 | if (ks == signingKeySet) { |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | public long[] getSigningKeySets() { |
| 113 | return mSigningKeySets; |
| 114 | } |
| 115 | |
| 116 | public long[] getDefinedKeySets() { |
| 117 | return mDefinedKeySets; |
| 118 | } |
| 119 | |
| 120 | public Map<String, Long> getAliases() { |
| 121 | return mKeySetAliases; |
| 122 | } |
| 123 | } |