1/*
2 * Copyright (C) 2016 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 android.util;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.pm.PackageInfo;
22import android.content.pm.PackageManager;
23import android.content.pm.Signature;
24
25import java.security.MessageDigest;
26import java.security.NoSuchAlgorithmException;
27
28/**
29 * Helper functions applicable to packages.
30 * @hide
31 */
32public final class PackageUtils {
33    private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
34
35    private PackageUtils() {
36        /* hide constructor */
37    }
38
39    /**
40     * Computes the SHA256 digest of the signing cert for a package.
41     * @param packageManager The package manager.
42     * @param packageName The package for which to generate the digest.
43     * @param userId The user for which to generate the digest.
44     * @return The digest or null if the package does not exist for this user.
45     */
46    public static @Nullable String computePackageCertSha256Digest(
47            @NonNull PackageManager packageManager,
48            @NonNull String packageName, int userId) {
49        final PackageInfo packageInfo;
50        try {
51            packageInfo = packageManager.getPackageInfoAsUser(packageName,
52                    PackageManager.GET_SIGNATURES, userId);
53        } catch (PackageManager.NameNotFoundException e) {
54            return null;
55        }
56        return computeCertSha256Digest(packageInfo.signatures[0]);
57    }
58
59    /**
60     * Computes the SHA256 digest of a cert.
61     * @param signature The signature.
62     * @return The digest or null if an error occurs.
63     */
64    public static @Nullable String computeCertSha256Digest(@NonNull Signature signature) {
65        return computeSha256Digest(signature.toByteArray());
66    }
67
68    /**
69     * Computes the SHA256 digest of some data.
70     * @param data The data.
71     * @return The digest or null if an error occurs.
72     */
73    public static @Nullable String computeSha256Digest(@NonNull byte[] data) {
74        MessageDigest messageDigest;
75        try {
76            messageDigest = MessageDigest.getInstance("SHA256");
77        } catch (NoSuchAlgorithmException e) {
78            /* can't happen */
79            return null;
80        }
81
82        messageDigest.update(data);
83
84        final byte[] digest = messageDigest.digest();
85        final int digestLength = digest.length;
86        final int charCount = 2 * digestLength;
87
88        final char[] chars = new char[charCount];
89        for (int i = 0; i < digestLength; i++) {
90            final int byteHex = digest[i] & 0xFF;
91            chars[i * 2] = HEX_ARRAY[byteHex >>> 4];
92            chars[i * 2 + 1] = HEX_ARRAY[byteHex & 0x0F];
93        }
94        return new String(chars);
95    }
96}
97