use of com.android.apksig.apk.ApkFormatException in project AppManager by MuntashirAkon.
the class ScannerViewModel method loadApkVerifierResult.
private void loadApkVerifierResult() {
waitForFile();
try {
// TODO: 26/5/21 Add v4 verification
ApkVerifier.Builder builder = new ApkVerifier.Builder(apkFile);
ApkVerifier apkVerifier = builder.build();
ApkVerifier.Result apkVerifierResult = apkVerifier.verify();
this.apkVerifierResult.postValue(apkVerifierResult);
} catch (IOException | ApkFormatException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
use of com.android.apksig.apk.ApkFormatException in project AppManager by MuntashirAkon.
the class PackageUtils method getSignerInfo.
@Nullable
private static SignerInfo getSignerInfo(@NonNull File apkFile) {
ApkVerifier.Builder builder = new ApkVerifier.Builder(apkFile);
ApkVerifier apkVerifier = builder.build();
try {
return new SignerInfo(apkVerifier.verify());
} catch (CertificateEncodingException | IOException | ApkFormatException | NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
use of com.android.apksig.apk.ApkFormatException in project bundletool by google.
the class ApkSigner method signApk.
public void signApk(Path apkPath, ModuleSplit split) {
if (!signingConfigProvider.isPresent()) {
return;
}
ApksigSigningConfiguration signingConfig = signingConfigProvider.get().getSigningConfiguration(ApkDescription.fromModuleSplit(split));
try (TempDirectory tempDirectory = new TempDirectory(getClass().getSimpleName())) {
Path signedApkPath = tempDirectory.getPath().resolve("signed.apk");
com.android.apksig.ApkSigner.Builder apkSigner = new com.android.apksig.ApkSigner.Builder(signingConfig.getSignerConfigs().stream().map(ApkSigner::convertToApksigSignerConfig).collect(toImmutableList())).setInputApk(apkPath.toFile()).setOutputApk(signedApkPath.toFile()).setV1SigningEnabled(signingConfig.getV1SigningEnabled()).setV2SigningEnabled(signingConfig.getV2SigningEnabled()).setV3SigningEnabled(signingConfig.getV3SigningEnabled()).setOtherSignersSignaturesPreserved(false).setMinSdkVersion(split.getAndroidManifest().getEffectiveMinSdkVersion());
signingConfig.getSigningCertificateLineage().ifPresent(apkSigner::setSigningCertificateLineage);
sourceStampSigningConfig.map(SigningConfiguration::getSignerConfig).map(ApkSigner::convertToApksigSignerConfig).ifPresent(apkSigner::setSourceStampSignerConfig);
apkSigner.build().sign();
Files.move(signedApkPath, apkPath, REPLACE_EXISTING);
} catch (IOException | ApkFormatException | NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw CommandExecutionException.builder().withCause(e).withInternalMessage("Unable to sign APK.").build();
}
}
use of com.android.apksig.apk.ApkFormatException in project bundletool by google.
the class BuildApksCommand method getLineageFromInputFile.
/**
* Extracts the Signing Certificate Lineage from the provided lineage or APK file.
*/
private static SigningCertificateLineage getLineageFromInputFile(File inputLineageFile) {
try (RandomAccessFile f = new RandomAccessFile(inputLineageFile, "r")) {
if (f.length() < 4) {
throw CommandExecutionException.builder().withInternalMessage("The input file is not a valid lineage file.").build();
}
DataSource apk = DataSources.asDataSource(f);
int magicValue = apk.getByteBuffer(0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();
if (magicValue == SigningCertificateLineage.MAGIC) {
return SigningCertificateLineage.readFromFile(inputLineageFile);
} else if (magicValue == ZIP_MAGIC) {
return SigningCertificateLineage.readFromApkFile(inputLineageFile);
} else {
throw CommandExecutionException.builder().withInternalMessage("The input file is not a valid lineage file.").build();
}
} catch (IOException | ApkFormatException | IllegalArgumentException e) {
throw CommandExecutionException.builder().withCause(e).build();
}
}
use of com.android.apksig.apk.ApkFormatException in project apksig by venshine.
the class ApkSigner method parseZipCentralDirectory.
private static List<CentralDirectoryRecord> parseZipCentralDirectory(ByteBuffer cd, ApkUtils.ZipSections apkSections) throws ApkFormatException {
long cdOffset = apkSections.getZipCentralDirectoryOffset();
int expectedCdRecordCount = apkSections.getZipCentralDirectoryRecordCount();
List<CentralDirectoryRecord> cdRecords = new ArrayList<>(expectedCdRecordCount);
Set<String> entryNames = new HashSet<>(expectedCdRecordCount);
for (int i = 0; i < expectedCdRecordCount; i++) {
CentralDirectoryRecord cdRecord;
int offsetInsideCd = cd.position();
try {
cdRecord = CentralDirectoryRecord.getRecord(cd);
} catch (ZipFormatException e) {
throw new ApkFormatException("Malformed ZIP Central Directory record #" + (i + 1) + " at file offset " + (cdOffset + offsetInsideCd), e);
}
String entryName = cdRecord.getName();
if (!entryNames.add(entryName)) {
throw new ApkFormatException("Multiple ZIP entries with the same name: " + entryName);
}
cdRecords.add(cdRecord);
}
if (cd.hasRemaining()) {
throw new ApkFormatException("Unused space at the end of ZIP Central Directory: " + cd.remaining() + " bytes starting at file offset " + (cdOffset + cd.position()));
}
return cdRecords;
}
Aggregations