1/*
2 * Copyright (C) 2015 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 android.util.ArraySet;
20import android.util.LongSparseArray;
21
22import java.lang.reflect.Field;
23import java.security.PublicKey;
24
25public class KeySetUtils {
26
27    public static PublicKey getPubKey(KeySetManagerService ksms, long pkId)
28            throws NoSuchFieldException, IllegalAccessException {
29        Field pkField = ksms.getClass().getDeclaredField("mPublicKeys");
30        pkField.setAccessible(true);
31        LongSparseArray<KeySetManagerService.PublicKeyHandle> mPublicKeys =
32            (LongSparseArray<KeySetManagerService.PublicKeyHandle>) pkField.get(ksms);
33        KeySetManagerService.PublicKeyHandle pkh = mPublicKeys.get(pkId);
34        if (pkh == null) {
35            return null;
36        } else {
37            return pkh.getKey();
38        }
39    }
40
41    public static int getPubKeyRefCount(KeySetManagerService ksms, long pkId)
42            throws NoSuchFieldException, IllegalAccessException {
43        Field pkField = ksms.getClass().getDeclaredField("mPublicKeys");
44        pkField.setAccessible(true);
45        LongSparseArray<KeySetManagerService.PublicKeyHandle> mPublicKeys =
46            (LongSparseArray<KeySetManagerService.PublicKeyHandle>) pkField.get(ksms);
47        KeySetManagerService.PublicKeyHandle pkh = mPublicKeys.get(pkId);
48        if (pkh == null) {
49            return 0;
50        } else {
51            return pkh.getRefCountLPr();
52        }
53    }
54
55    public static int getKeySetRefCount(KeySetManagerService ksms, long keysetId)
56            throws NoSuchFieldException, IllegalAccessException {
57        Field ksField = ksms.getClass().getDeclaredField("mKeySets");
58        ksField.setAccessible(true);
59        LongSparseArray<KeySetHandle> mKeySets =
60            (LongSparseArray<KeySetHandle>) ksField.get(ksms);
61        KeySetHandle ksh = mKeySets.get(keysetId);
62        if (ksh == null) {
63            return 0;
64        } else {
65            return ksh.getRefCountLPr();
66        }
67    }
68
69    public static LongSparseArray<ArraySet<Long>> getKeySetMapping(KeySetManagerService ksms)
70            throws NoSuchFieldException, IllegalAccessException {
71        Field ksField = ksms.getClass().getDeclaredField("mKeySetMapping");
72        ksField.setAccessible(true);
73        return (LongSparseArray<ArraySet<Long>>) ksField.get(ksms);
74    }
75
76    public static Long getLastIssuedKeyId(KeySetManagerService ksms)
77            throws NoSuchFieldException, IllegalAccessException {
78        Field ksField = ksms.getClass().getDeclaredField("lastIssuedKeyId");
79        ksField.setAccessible(true);
80        return (Long) ksField.get(ksms);
81    }
82
83    public static Long getLastIssuedKeySetId(KeySetManagerService ksms)
84            throws NoSuchFieldException, IllegalAccessException {
85        Field ksField = ksms.getClass().getDeclaredField("lastIssuedKeySetId");
86        ksField.setAccessible(true);
87        return (Long) ksField.get(ksms);
88    }
89}
90