Search in sources :

Example 16 with ApkFormatException

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();
    }
}
Also used : ApkVerifier(com.android.apksig.ApkVerifier) ApkFormatException(com.android.apksig.apk.ApkFormatException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 17 with ApkFormatException

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;
    }
}
Also used : SignerInfo(io.github.muntashirakon.AppManager.apk.signing.SignerInfo) ApkVerifier(com.android.apksig.ApkVerifier) ApkFormatException(com.android.apksig.apk.ApkFormatException) SpannableStringBuilder(android.text.SpannableStringBuilder) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Nullable(androidx.annotation.Nullable)

Example 18 with ApkFormatException

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();
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ApksigSigningConfiguration(com.android.tools.build.bundletool.model.ApksigSigningConfiguration) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) ApksigSigningConfiguration(com.android.tools.build.bundletool.model.ApksigSigningConfiguration) ApkFormatException(com.android.apksig.apk.ApkFormatException)

Example 19 with ApkFormatException

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();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ApkFormatException(com.android.apksig.apk.ApkFormatException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) DataSource(com.android.apksig.util.DataSource)

Example 20 with ApkFormatException

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;
}
Also used : CentralDirectoryRecord(com.android.apksig.internal.zip.CentralDirectoryRecord) ApkFormatException(com.android.apksig.apk.ApkFormatException) ArrayList(java.util.ArrayList) ZipFormatException(com.android.apksig.zip.ZipFormatException) HashSet(java.util.HashSet)

Aggregations

ApkFormatException (com.android.apksig.apk.ApkFormatException)36 IOException (java.io.IOException)18 ByteBuffer (java.nio.ByteBuffer)17 ArrayList (java.util.ArrayList)15 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 BufferUnderflowException (java.nio.BufferUnderflowException)12 ZipFormatException (com.android.apksig.zip.ZipFormatException)11 CertificateException (java.security.cert.CertificateException)11 ApkSigningBlockUtils (com.android.apksig.internal.apk.ApkSigningBlockUtils)9 CentralDirectoryRecord (com.android.apksig.internal.zip.CentralDirectoryRecord)9 X509Certificate (java.security.cert.X509Certificate)9 InvalidKeyException (java.security.InvalidKeyException)7 SignatureException (java.security.SignatureException)7 CertificateFactory (java.security.cert.CertificateFactory)7 SignatureAlgorithm (com.android.apksig.internal.apk.SignatureAlgorithm)6 GuaranteedEncodedFormX509Certificate (com.android.apksig.internal.util.GuaranteedEncodedFormX509Certificate)6 HashSet (java.util.HashSet)6 ApkSigResult (com.android.apksig.internal.apk.ApkSigResult)5 DataSource (com.android.apksig.util.DataSource)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)5