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