CertPinInstallReceiverTestpublic class CertPinInstallReceiverTest extends android.test.AndroidTestCase Tests for {@link com.android.server.CertPinInstallReceiver} |
Fields Summary |
---|
private static final String | TAG | private static final String | PINLIST_ROOT | public static final String | PINLIST_CONTENT_PATH | public static final String | PINLIST_METADATA_PATH | public static final String | PINLIST_CONTENT_URL_KEY | public static final String | PINLIST_METADATA_URL_KEY | public static final String | PINLIST_CERTIFICATE_KEY | public static final String | PINLIST_VERSION_KEY | private static final String | EXTRA_CONTENT_PATH | private static final String | EXTRA_REQUIRED_HASH | private static final String | EXTRA_SIGNATURE | private static final String | EXTRA_VERSION_NUMBER | public static final String | TEST_CERT | public static final String | TEST_KEY |
Methods Summary |
---|
private java.security.cert.X509Certificate | createCertificate()
byte[] derCert = Base64.decode(TEST_CERT.getBytes(), Base64.DEFAULT);
InputStream istream = new ByteArrayInputStream(derCert);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return (X509Certificate) cf.generateCertificate(istream);
| private java.security.PrivateKey | createKey()
byte[] derKey = Base64.decode(TEST_KEY.getBytes(), Base64.DEFAULT);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(derKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (PrivateKey) keyFactory.generatePrivate(keySpec);
| private java.lang.String | createSignature(java.lang.String content, java.lang.String version, java.lang.String requiredHash)
Signature signer = Signature.getInstance("SHA512withRSA");
signer.initSign(createKey());
signer.update(content.trim().getBytes());
signer.update(version.trim().getBytes());
signer.update(requiredHash.getBytes());
String sig = new String(Base64.encode(signer.sign(), Base64.DEFAULT));
assertEquals(true,
verifySignature(content, version, requiredHash, sig, createCertificate()));
return sig;
| private static java.lang.String | getCurrentHash(java.lang.String content)
if (content == null) {
return "0";
}
MessageDigest dgst = MessageDigest.getInstance("SHA512");
byte[] encoded = content.getBytes();
byte[] fingerprint = dgst.digest(encoded);
return IntegralToString.bytesToHexString(fingerprint, false);
| private static java.lang.String | getHashOfCurrentContent()
String content = IoUtils.readFileAsString("/data/misc/keychain/pins");
return getCurrentHash(content);
| private java.lang.String | getNextVersion()
int currentVersion = Integer.parseInt(readCurrentVersion());
return Integer.toString(currentVersion + 1);
| private java.lang.String | makeTemporaryContentFile(java.lang.String content)
FileOutputStream fw = mContext.openFileOutput("content.txt", mContext.MODE_WORLD_READABLE);
fw.write(content.getBytes(), 0, content.length());
fw.close();
return mContext.getFilesDir() + "/content.txt";
| private void | overrideCert(java.lang.String value)
overrideSettings(PINLIST_CERTIFICATE_KEY, value);
| private void | overrideSettings(java.lang.String key, java.lang.String value)
assertTrue(Settings.Secure.putString(mContext.getContentResolver(), key, value));
Thread.sleep(1000);
| private java.lang.String | readCurrentVersion()
return IoUtils.readFileAsString("/data/misc/keychain/metadata/version");
| private java.lang.String | readPins()
return IoUtils.readFileAsString(PINLIST_CONTENT_PATH);
| private java.lang.String | runTest(java.lang.String cert, java.lang.String content, java.lang.String version, java.lang.String required, java.lang.String sig)
Log.e(TAG, "started test");
overrideCert(cert);
String contentPath = makeTemporaryContentFile(content);
sendIntent(contentPath, version, required, sig);
Thread.sleep(1000);
return readPins();
| private java.lang.String | runTestWithoutSig(java.lang.String cert, java.lang.String content, java.lang.String version, java.lang.String required)
String sig = createSignature(content, version, required);
return runTest(cert, content, version, required, sig);
| private void | sendIntent(java.lang.String contentPath, java.lang.String version, java.lang.String required, java.lang.String sig)
Intent i = new Intent();
i.setAction("android.intent.action.UPDATE_PINS");
i.putExtra(EXTRA_CONTENT_PATH, contentPath);
i.putExtra(EXTRA_VERSION_NUMBER, version);
i.putExtra(EXTRA_REQUIRED_HASH, required);
i.putExtra(EXTRA_SIGNATURE, sig);
mContext.sendBroadcast(i);
| public void | testBadRequiredHashFails()
runTestWithoutSig(TEST_CERT, "blahblahblah", getNextVersion(), getHashOfCurrentContent());
assertEquals("blahblahblah", runTestWithoutSig(TEST_CERT, "cdefg", getNextVersion(), "0"));
Log.e(TAG, "started testOverwritePinList");
| public void | testBadSignatureFails()
Log.e(TAG, "started testOverwritePinList");
String text = "blahblah";
runTestWithoutSig(TEST_CERT, text, getNextVersion(), getHashOfCurrentContent());
assertEquals(text, runTest(TEST_CERT, "bcdef", getNextVersion(), getCurrentHash(text), ""));
Log.e(TAG, "started testOverwritePinList");
| public void | testBadVersionFails()
String text = "blahblahblahblah";
String version = getNextVersion();
runTestWithoutSig(TEST_CERT, text, version, getHashOfCurrentContent());
assertEquals(text, runTestWithoutSig(TEST_CERT, "defgh", version, getCurrentHash(text)));
Log.e(TAG, "started testOverwritePinList");
| public void | testOverrideRequiredHash()
runTestWithoutSig(TEST_CERT, "blahblahblah", getNextVersion(), getHashOfCurrentContent());
assertEquals("blahblahblah", runTestWithoutSig(TEST_CERT, "cdefg", "NONE", "0"));
Log.e(TAG, "started testOverwritePinList");
| public void | testOverwritePinlist()
Log.e(TAG, "started testOverwritePinList");
assertEquals("abcde", runTestWithoutSig(TEST_CERT, "abcde", getNextVersion(), getHashOfCurrentContent()));
Log.e(TAG, "started testOverwritePinList");
| public boolean | verifySignature(java.lang.String content, java.lang.String version, java.lang.String requiredPrevious, java.lang.String signature, java.security.cert.X509Certificate cert)
Signature signer = Signature.getInstance("SHA512withRSA");
signer.initVerify(cert);
signer.update(content.trim().getBytes());
signer.update(version.trim().getBytes());
signer.update(requiredPrevious.trim().getBytes());
return signer.verify(Base64.decode(signature.getBytes(), Base64.DEFAULT));
|
|