Search in sources :

Example 66 with Signature

use of android.content.pm.Signature in project platform_frameworks_base by android.

the class PackageSignatures method writeXml.

void writeXml(XmlSerializer serializer, String tagName, ArrayList<Signature> pastSignatures) throws IOException {
    if (mSignatures == null) {
        return;
    }
    serializer.startTag(null, tagName);
    serializer.attribute(null, "count", Integer.toString(mSignatures.length));
    for (int i = 0; i < mSignatures.length; i++) {
        serializer.startTag(null, "cert");
        final Signature sig = mSignatures[i];
        final int sigHash = sig.hashCode();
        final int numPast = pastSignatures.size();
        int j;
        for (j = 0; j < numPast; j++) {
            Signature pastSig = pastSignatures.get(j);
            if (pastSig.hashCode() == sigHash && pastSig.equals(sig)) {
                serializer.attribute(null, "index", Integer.toString(j));
                break;
            }
        }
        if (j >= numPast) {
            pastSignatures.add(sig);
            serializer.attribute(null, "index", Integer.toString(numPast));
            serializer.attribute(null, "key", sig.toCharsString());
        }
        serializer.endTag(null, "cert");
    }
    serializer.endTag(null, tagName);
}
Also used : Signature(android.content.pm.Signature)

Example 67 with Signature

use of android.content.pm.Signature in project platform_frameworks_base by android.

the class KeySetManagerServiceTest method testPublicKeyCertReprEquiv.

/* test equivalence of PackageManager cert encoding and PackageParser manifest keys */
public void testPublicKeyCertReprEquiv() throws CertificateException {
    PublicKey keyA = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyA);
    PublicKey keyB = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyB);
    PublicKey keyC = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyC);
    Signature sigA = new Signature(KeySetStrings.ctsKeySetCertA);
    Signature sigB = new Signature(KeySetStrings.ctsKeySetCertB);
    Signature sigC = new Signature(KeySetStrings.ctsKeySetCertC);
    assertNotNull(keyA);
    assertNotNull(keyB);
    assertNotNull(keyC);
    assertEquals(keyA, sigA.getPublicKey());
    assertEquals(keyB, sigB.getPublicKey());
    assertEquals(keyC, sigC.getPublicKey());
    byte[] bArrayPk = keyA.getEncoded();
    byte[] bArrayCert = sigA.getPublicKey().getEncoded();
    assertEquals(bArrayPk.length, bArrayCert.length);
    assertEquals(true, ArrayUtils.equals(bArrayPk, bArrayCert, bArrayPk.length));
    bArrayPk = keyB.getEncoded();
    bArrayCert = sigB.getPublicKey().getEncoded();
    assertEquals(bArrayPk.length, bArrayCert.length);
    assertEquals(true, ArrayUtils.equals(bArrayPk, bArrayCert, bArrayPk.length));
    bArrayPk = keyC.getEncoded();
    bArrayCert = sigC.getPublicKey().getEncoded();
    assertEquals(bArrayPk.length, bArrayCert.length);
    assertEquals(true, ArrayUtils.equals(bArrayPk, bArrayCert, bArrayPk.length));
}
Also used : PublicKey(java.security.PublicKey) Signature(android.content.pm.Signature)

Example 68 with Signature

use of android.content.pm.Signature in project Conversations by siacs.

the class ExceptionHelper method checkForCrash.

public static boolean checkForCrash(ConversationActivity activity, final XmppConnectionService service) {
    try {
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        boolean neverSend = preferences.getBoolean("never_send", false);
        if (neverSend || Config.BUG_REPORTS == null) {
            return false;
        }
        List<Account> accounts = service.getAccounts();
        Account account = null;
        for (int i = 0; i < accounts.size(); ++i) {
            if (!accounts.get(i).isOptionSet(Account.OPTION_DISABLED)) {
                account = accounts.get(i);
                break;
            }
        }
        if (account == null) {
            return false;
        }
        final Account finalAccount = account;
        FileInputStream file = activity.openFileInput("stacktrace.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(file);
        BufferedReader stacktrace = new BufferedReader(inputStreamReader);
        final StringBuilder report = new StringBuilder();
        PackageManager pm = activity.getPackageManager();
        PackageInfo packageInfo;
        try {
            packageInfo = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES);
            report.append("Version: " + packageInfo.versionName + '\n');
            report.append("Last Update: " + DATE_FORMATs.format(new Date(packageInfo.lastUpdateTime)) + '\n');
            Signature[] signatures = packageInfo.signatures;
            if (signatures != null && signatures.length >= 1) {
                report.append("SHA-1: " + CryptoHelper.getFingerprintCert(packageInfo.signatures[0].toByteArray()) + "\n");
            }
            report.append('\n');
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        String line;
        while ((line = stacktrace.readLine()) != null) {
            report.append(line);
            report.append('\n');
        }
        file.close();
        activity.deleteFile("stacktrace.txt");
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(activity.getString(R.string.crash_report_title));
        builder.setMessage(activity.getText(R.string.crash_report_message));
        builder.setPositiveButton(activity.getText(R.string.send_now), new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(Config.LOGTAG, "using account=" + finalAccount.getJid().toBareJid() + " to send in stack trace");
                Conversation conversation = null;
                try {
                    conversation = service.findOrCreateConversation(finalAccount, Jid.fromString(Config.BUG_REPORTS), false);
                } catch (final InvalidJidException ignored) {
                }
                Message message = new Message(conversation, report.toString(), Message.ENCRYPTION_NONE);
                service.sendMessage(message);
            }
        });
        builder.setNegativeButton(activity.getText(R.string.send_never), new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                preferences.edit().putBoolean("never_send", true).apply();
            }
        });
        builder.create().show();
        return true;
    } catch (final IOException ignored) {
        return false;
    }
}
Also used : AlertDialog(android.app.AlertDialog) Account(eu.siacs.conversations.entities.Account) Message(eu.siacs.conversations.entities.Message) DialogInterface(android.content.DialogInterface) Conversation(eu.siacs.conversations.entities.Conversation) PackageManager(android.content.pm.PackageManager) InputStreamReader(java.io.InputStreamReader) SharedPreferences(android.content.SharedPreferences) PackageInfo(android.content.pm.PackageInfo) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Signature(android.content.pm.Signature) BufferedReader(java.io.BufferedReader) OnClickListener(android.content.DialogInterface.OnClickListener)

Example 69 with Signature

use of android.content.pm.Signature in project android_frameworks_base by DirtyUnicorns.

the class AccountManagerService method calculatePackageSignatureDigest.

private byte[] calculatePackageSignatureDigest(String callerPkg) {
    MessageDigest digester;
    try {
        digester = MessageDigest.getInstance("SHA-256");
        PackageInfo pkgInfo = mPackageManager.getPackageInfo(callerPkg, PackageManager.GET_SIGNATURES);
        for (Signature sig : pkgInfo.signatures) {
            digester.update(sig.toByteArray());
        }
    } catch (NoSuchAlgorithmException x) {
        Log.wtf(TAG, "SHA-256 should be available", x);
        digester = null;
    } catch (NameNotFoundException e) {
        Log.w(TAG, "Could not find packageinfo for: " + callerPkg);
        digester = null;
    }
    return (digester == null) ? null : digester.digest();
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Signature(android.content.pm.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 70 with Signature

use of android.content.pm.Signature in project android_frameworks_base by DirtyUnicorns.

the class PackageManagerService method compareSignatures.

/**
     * Compares two sets of signatures. Returns:
     * <br />
     * {@link PackageManager#SIGNATURE_NEITHER_SIGNED}: if both signature sets are null,
     * <br />
     * {@link PackageManager#SIGNATURE_FIRST_NOT_SIGNED}: if the first signature set is null,
     * <br />
     * {@link PackageManager#SIGNATURE_SECOND_NOT_SIGNED}: if the second signature set is null,
     * <br />
     * {@link PackageManager#SIGNATURE_MATCH}: if the two signature sets are identical,
     * <br />
     * {@link PackageManager#SIGNATURE_NO_MATCH}: if the two signature sets differ.
     */
static int compareSignatures(Signature[] s1, Signature[] s2) {
    if (s1 == null) {
        return s2 == null ? PackageManager.SIGNATURE_NEITHER_SIGNED : PackageManager.SIGNATURE_FIRST_NOT_SIGNED;
    }
    if (s2 == null) {
        return PackageManager.SIGNATURE_SECOND_NOT_SIGNED;
    }
    if (s1.length != s2.length) {
        return PackageManager.SIGNATURE_NO_MATCH;
    }
    // Since both signature sets are of size 1, we can compare without HashSets.
    if (s1.length == 1) {
        return s1[0].equals(s2[0]) ? PackageManager.SIGNATURE_MATCH : PackageManager.SIGNATURE_NO_MATCH;
    }
    ArraySet<Signature> set1 = new ArraySet<Signature>();
    for (Signature sig : s1) {
        set1.add(sig);
    }
    ArraySet<Signature> set2 = new ArraySet<Signature>();
    for (Signature sig : s2) {
        set2.add(sig);
    }
    // Make sure s2 contains all signatures in s1.
    if (set1.equals(set2)) {
        return PackageManager.SIGNATURE_MATCH;
    }
    return PackageManager.SIGNATURE_NO_MATCH;
}
Also used : ArraySet(android.util.ArraySet) Signature(android.content.pm.Signature)

Aggregations

Signature (android.content.pm.Signature)97 PackageManager (android.content.pm.PackageManager)34 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)31 PackageInfo (android.content.pm.PackageInfo)26 ArrayList (java.util.ArrayList)16 MessageDigest (java.security.MessageDigest)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 ArraySet (android.util.ArraySet)11 IOException (java.io.IOException)8 PublicKey (java.security.PublicKey)8 File (java.io.File)7 Intent (android.content.Intent)6 CertificateException (java.security.cert.CertificateException)6 HashSet (java.util.HashSet)6 ResolveInfo (android.content.pm.ResolveInfo)5 INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)5 IActivityManager (android.app.IActivityManager)4 INotificationManager (android.app.INotificationManager)4 IProcessObserver (android.app.IProcessObserver)4 PackageParser (android.content.pm.PackageParser)4