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);
}
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));
}
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;
}
}
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();
}
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;
}
Aggregations