Search in sources :

Example 26 with DataSource

use of com.android.apksig.util.DataSource in project apksig by venshine.

the class FileChannelDataSourceTest method testFeedsCorrectData_whenSeveralMbRead.

@Test
public void testFeedsCorrectData_whenSeveralMbRead() throws Exception {
    byte[] fullFileContent = createFileContent(3 * 1024 * 1024 + 987654);
    RandomAccessFile raf = createRaf(fullFileContent);
    DataSource rafDataSource = new FileChannelDataSource(raf.getChannel());
    ByteArrayDataSink dataSink = new ByteArrayDataSink();
    int offset = 23456;
    int bytesToFeed = 2 * 1024 * 1024 + 12345;
    rafDataSource.feed(offset, bytesToFeed, dataSink);
    byte[] expectedBytes = Arrays.copyOfRange(fullFileContent, offset, offset + bytesToFeed);
    byte[] resultBytes = getDataSinkBytes(dataSink);
    assertArrayEquals(expectedBytes, resultBytes);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) DataSource(com.android.apksig.util.DataSource) Test(org.junit.Test)

Example 27 with DataSource

use of com.android.apksig.util.DataSource in project apksig by venshine.

the class SigningCertificateLineageTest method testLineageFromBytesContainsExpectedSigners.

@Test
public void testLineageFromBytesContainsExpectedSigners() throws Exception {
    // This file contains the lineage with the three rsa-2048 signers
    DataSource lineageDataSource = Resources.toDataSource(getClass(), "rsa-2048-lineage-3-signers");
    SigningCertificateLineage lineage = SigningCertificateLineage.readFromBytes(lineageDataSource.getByteBuffer(0, (int) lineageDataSource.size()).array());
    List<SignerConfig> signers = new ArrayList<>(3);
    signers.add(Resources.toLineageSignerConfig(getClass(), FIRST_RSA_2048_SIGNER_RESOURCE_NAME));
    signers.add(Resources.toLineageSignerConfig(getClass(), SECOND_RSA_2048_SIGNER_RESOURCE_NAME));
    signers.add(Resources.toLineageSignerConfig(getClass(), THIRD_RSA_2048_SIGNER_RESOURCE_NAME));
    assertLineageContainsExpectedSigners(lineage, signers);
}
Also used : SignerConfig(com.android.apksig.SigningCertificateLineage.SignerConfig) ArrayList(java.util.ArrayList) DataSource(com.android.apksig.util.DataSource) Test(org.junit.Test)

Example 28 with DataSource

use of com.android.apksig.util.DataSource in project apksig by venshine.

the class SigningCertificateLineageTest method testLineageFromFileContainsExpectedSigners.

@Test
public void testLineageFromFileContainsExpectedSigners() throws Exception {
    // This file contains the lineage with the three rsa-2048 signers
    DataSource lineageDataSource = Resources.toDataSource(getClass(), "rsa-2048-lineage-3-signers");
    SigningCertificateLineage lineage = SigningCertificateLineage.readFromDataSource(lineageDataSource);
    List<SignerConfig> signers = new ArrayList<>(3);
    signers.add(Resources.toLineageSignerConfig(getClass(), FIRST_RSA_2048_SIGNER_RESOURCE_NAME));
    signers.add(Resources.toLineageSignerConfig(getClass(), SECOND_RSA_2048_SIGNER_RESOURCE_NAME));
    signers.add(Resources.toLineageSignerConfig(getClass(), THIRD_RSA_2048_SIGNER_RESOURCE_NAME));
    assertLineageContainsExpectedSigners(lineage, signers);
}
Also used : SignerConfig(com.android.apksig.SigningCertificateLineage.SignerConfig) ArrayList(java.util.ArrayList) DataSource(com.android.apksig.util.DataSource) Test(org.junit.Test)

Example 29 with DataSource

use of com.android.apksig.util.DataSource in project apksig by venshine.

the class SigningCertificateLineageTest method testLineageFromAPKWithNoLineageFails.

@Test
public void testLineageFromAPKWithNoLineageFails() throws Exception {
    // This test verifies that attempting to read the lineage from an APK without a lineage
    // fails.
    // This is a valid APK that has only been signed with the V1 and V2 signature schemes;
    // since the lineage is an attribute in the V3 signature block this test should fail.
    DataSource apkDataSource = Resources.toDataSource(getClass(), "golden-aligned-v1v2-out.apk");
    try {
        SigningCertificateLineage.readFromApkDataSource(apkDataSource);
        fail("A failure should have been reported due to the APK not containing a V3 signing " + "block");
    } catch (IllegalArgumentException expected) {
    }
    // This is a valid APK signed with the V1, V2, and V3 signature schemes, but there is no
    // lineage in the V3 signature block.
    apkDataSource = Resources.toDataSource(getClass(), "golden-aligned-v1v2v3-out.apk");
    try {
        SigningCertificateLineage.readFromApkDataSource(apkDataSource);
        fail("A failure should have been reported due to the APK containing a V3 signing " + "block without the lineage attribute");
    } catch (IllegalArgumentException expected) {
    }
    // This APK is based off the v1v2v3-with-rsa-2048-lineage-3-signers.apk with a bit flip
    // in the lineage attribute ID in the V3 signature block.
    apkDataSource = Resources.toDataSource(getClass(), "v1v2v3-with-rsa-2048-lineage-3-signers-invalid-lineage-attr.apk");
    try {
        SigningCertificateLineage.readFromApkDataSource(apkDataSource);
        fail("A failure should have been reported due to the APK containing a V3 signing " + "block with a modified lineage attribute ID");
    } catch (IllegalArgumentException expected) {
    }
}
Also used : DataSource(com.android.apksig.util.DataSource) Test(org.junit.Test)

Aggregations

DataSource (com.android.apksig.util.DataSource)29 RandomAccessFile (java.io.RandomAccessFile)11 Test (org.junit.Test)10 ByteBuffer (java.nio.ByteBuffer)7 ArrayList (java.util.ArrayList)6 ApkFormatException (com.android.apksig.apk.ApkFormatException)5 ApkSigningBlockUtils (com.android.apksig.internal.apk.ApkSigningBlockUtils)5 IOException (java.io.IOException)5 ApkUtils (com.android.apksig.apk.ApkUtils)4 ByteBufferDataSource (com.android.apksig.internal.util.ByteBufferDataSource)4 SignerConfig (com.android.apksig.SigningCertificateLineage.SignerConfig)3 CentralDirectoryRecord (com.android.apksig.internal.zip.CentralDirectoryRecord)3 DataSink (com.android.apksig.util.DataSink)3 File (java.io.File)3 MessageDigest (java.security.MessageDigest)3 ApkSigningBlockNotFoundException (com.android.apksig.apk.ApkSigningBlockNotFoundException)2 SignatureInfo (com.android.apksig.internal.apk.SignatureInfo)2 ChainedDataSource (com.android.apksig.internal.util.ChainedDataSource)2 Pair (com.android.apksig.internal.util.Pair)2 ZipFormatException (com.android.apksig.zip.ZipFormatException)2