Search in sources :

Example 21 with DataSource

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

the class ApkSignerTest method testSignApk_stampBlock_noStampGenerated.

@Test
public void testSignApk_stampBlock_noStampGenerated() throws Exception {
    List<ApkSigner.SignerConfig> signersList = Collections.singletonList(getDefaultSignerConfigFromResources(FIRST_RSA_2048_SIGNER_RESOURCE_NAME));
    File signedApkFile = sign("original.apk", new ApkSigner.Builder(signersList).setV1SigningEnabled(true).setV2SigningEnabled(true).setV3SigningEnabled(true));
    try (RandomAccessFile f = new RandomAccessFile(signedApkFile, "r")) {
        DataSource signedApk = DataSources.asDataSource(f, 0, f.length());
        ApkUtils.ZipSections zipSections = ApkUtils.findZipSections(signedApk);
        ApkSigningBlockUtils.Result result = new ApkSigningBlockUtils.Result(ApkSigningBlockUtils.VERSION_SOURCE_STAMP);
        assertThrows(ApkSigningBlockUtils.SignatureNotFoundException.class, () -> ApkSigningBlockUtils.findSignature(signedApk, zipSections, ApkSigningBlockUtils.VERSION_SOURCE_STAMP, result));
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ApkUtils(com.android.apksig.apk.ApkUtils) ApkSigningBlockUtils(com.android.apksig.internal.apk.ApkSigningBlockUtils) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) DataSource(com.android.apksig.util.DataSource) Test(org.junit.Test)

Example 22 with DataSource

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

the class ApkSignerTest method testSignApk_stampFile.

@Test
public void testSignApk_stampFile() throws Exception {
    List<ApkSigner.SignerConfig> signers = Collections.singletonList(getDefaultSignerConfigFromResources(FIRST_RSA_2048_SIGNER_RESOURCE_NAME));
    ApkSigner.SignerConfig sourceStampSigner = getDefaultSignerConfigFromResources(SECOND_RSA_2048_SIGNER_RESOURCE_NAME);
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    messageDigest.update(sourceStampSigner.getCertificates().get(0).getEncoded());
    byte[] expectedStampCertificateDigest = messageDigest.digest();
    File signedApkFile = sign("original.apk", new ApkSigner.Builder(signers).setV1SigningEnabled(true).setSourceStampSignerConfig(sourceStampSigner));
    try (RandomAccessFile f = new RandomAccessFile(signedApkFile, "r")) {
        DataSource signedApk = DataSources.asDataSource(f, 0, f.length());
        ApkUtils.ZipSections zipSections = findZipSections(signedApk);
        List<CentralDirectoryRecord> cdRecords = V1SchemeVerifier.parseZipCentralDirectory(signedApk, zipSections);
        CentralDirectoryRecord stampCdRecord = null;
        for (CentralDirectoryRecord cdRecord : cdRecords) {
            if (SOURCE_STAMP_CERTIFICATE_HASH_ZIP_ENTRY_NAME.equals(cdRecord.getName())) {
                stampCdRecord = cdRecord;
                break;
            }
        }
        assertNotNull(stampCdRecord);
        byte[] actualStampCertificateDigest = LocalFileRecord.getUncompressedData(signedApk, stampCdRecord, zipSections.getZipCentralDirectoryOffset());
        assertArrayEquals(expectedStampCertificateDigest, actualStampCertificateDigest);
    }
}
Also used : CentralDirectoryRecord(com.android.apksig.internal.zip.CentralDirectoryRecord) DataSource(com.android.apksig.util.DataSource) RandomAccessFile(java.io.RandomAccessFile) ApkUtils(com.android.apksig.apk.ApkUtils) MessageDigest(java.security.MessageDigest) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 23 with DataSource

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

the class ApkSignerTest method getSignatureInfoFromApk.

private static SignatureInfo getSignatureInfoFromApk(File apkFile, int signatureVersionId, int signatureVersionBlockId) throws IOException, ZipFormatException, ApkSigningBlockUtils.SignatureNotFoundException {
    try (RandomAccessFile f = new RandomAccessFile(apkFile, "r")) {
        DataSource apk = DataSources.asDataSource(f, 0, f.length());
        ApkUtils.ZipSections zipSections = ApkUtils.findZipSections(apk);
        ApkSigningBlockUtils.Result result = new ApkSigningBlockUtils.Result(signatureVersionId);
        return ApkSigningBlockUtils.findSignature(apk, zipSections, signatureVersionBlockId, result);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ApkUtils(com.android.apksig.apk.ApkUtils) ApkSigningBlockUtils(com.android.apksig.internal.apk.ApkSigningBlockUtils) DataSource(com.android.apksig.util.DataSource)

Example 24 with DataSource

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

the class ApkSignerTest method signGolden.

private static void signGolden(String inResourceName, File outFile, ApkSigner.Builder apkSignerBuilder) throws Exception {
    DataSource in = DataSources.asDataSource(ByteBuffer.wrap(Resources.toByteArray(ApkSigner.class, inResourceName)));
    apkSignerBuilder.setInputApk(in).setOutputApk(outFile);
    File outFileIdSig = new File(outFile.getCanonicalPath() + ".idsig");
    apkSignerBuilder.setV4SignatureOutputFile(outFileIdSig);
    apkSignerBuilder.setV4ErrorReportingEnabled(true);
    apkSignerBuilder.build().sign();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) DataSource(com.android.apksig.util.DataSource)

Example 25 with DataSource

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

the class FileChannelDataSourceTest method testFeedsCorrectData_whenFilePartiallyReadFromBeginning.

@Test
public void testFeedsCorrectData_whenFilePartiallyReadFromBeginning() throws Exception {
    byte[] fullFileContent = createFileContent(1024 * 1024 + 987654);
    RandomAccessFile raf = createRaf(fullFileContent);
    DataSource rafDataSource = new FileChannelDataSource(raf.getChannel());
    ByteArrayDataSink dataSink = new ByteArrayDataSink();
    int bytesToFeed = 1024 * 1024 + 12345;
    rafDataSource.feed(0, bytesToFeed, dataSink);
    byte[] expectedBytes = Arrays.copyOf(fullFileContent, bytesToFeed);
    byte[] resultBytes = getDataSinkBytes(dataSink);
    assertArrayEquals(expectedBytes, resultBytes);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) 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