Search in sources :

Example 31 with ApkFormatException

use of com.android.apksig.apk.ApkFormatException in project apksig by venshine.

the class V2SchemeVerifier method parseSigner.

/**
 * Parses the provided signer block and populates the {@code result}.
 *
 * <p>This verifies signatures over {@code signed-data} contained in this block but does not
 * verify the integrity of the rest of the APK. To facilitate APK integrity verification, this
 * method adds the {@code contentDigestsToVerify}. These digests can then be used to verify the
 * integrity of the APK.
 *
 * <p>This method adds one or more errors to the {@code result} if a verification error is
 * expected to be encountered on an Android platform version in the
 * {@code [minSdkVersion, maxSdkVersion]} range.
 */
private static void parseSigner(ByteBuffer signerBlock, CertificateFactory certFactory, ApkSigningBlockUtils.Result.SignerInfo result, Set<ContentDigestAlgorithm> contentDigestsToVerify, Map<Integer, String> supportedApkSigSchemeNames, Set<Integer> foundApkSigSchemeIds, int minSdkVersion, int maxSdkVersion) throws ApkFormatException, NoSuchAlgorithmException {
    ByteBuffer signedData = ApkSigningBlockUtils.getLengthPrefixedSlice(signerBlock);
    byte[] signedDataBytes = new byte[signedData.remaining()];
    signedData.get(signedDataBytes);
    signedData.flip();
    result.signedData = signedDataBytes;
    ByteBuffer signatures = ApkSigningBlockUtils.getLengthPrefixedSlice(signerBlock);
    byte[] publicKeyBytes = ApkSigningBlockUtils.readLengthPrefixedByteArray(signerBlock);
    // Parse the signatures block and identify supported signatures
    int signatureCount = 0;
    List<ApkSigningBlockUtils.SupportedSignature> supportedSignatures = new ArrayList<>(1);
    while (signatures.hasRemaining()) {
        signatureCount++;
        try {
            ByteBuffer signature = ApkSigningBlockUtils.getLengthPrefixedSlice(signatures);
            int sigAlgorithmId = signature.getInt();
            byte[] sigBytes = ApkSigningBlockUtils.readLengthPrefixedByteArray(signature);
            result.signatures.add(new ApkSigningBlockUtils.Result.SignerInfo.Signature(sigAlgorithmId, sigBytes));
            SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.findById(sigAlgorithmId);
            if (signatureAlgorithm == null) {
                result.addWarning(Issue.V2_SIG_UNKNOWN_SIG_ALGORITHM, sigAlgorithmId);
                continue;
            }
            supportedSignatures.add(new ApkSigningBlockUtils.SupportedSignature(signatureAlgorithm, sigBytes));
        } catch (ApkFormatException | BufferUnderflowException e) {
            result.addError(Issue.V2_SIG_MALFORMED_SIGNATURE, signatureCount);
            return;
        }
    }
    if (result.signatures.isEmpty()) {
        result.addError(Issue.V2_SIG_NO_SIGNATURES);
        return;
    }
    // Verify signatures over signed-data block using the public key
    List<ApkSigningBlockUtils.SupportedSignature> signaturesToVerify = null;
    try {
        signaturesToVerify = ApkSigningBlockUtils.getSignaturesToVerify(supportedSignatures, minSdkVersion, maxSdkVersion);
    } catch (ApkSigningBlockUtils.NoSupportedSignaturesException e) {
        result.addError(Issue.V2_SIG_NO_SUPPORTED_SIGNATURES, e);
        return;
    }
    for (ApkSigningBlockUtils.SupportedSignature signature : signaturesToVerify) {
        SignatureAlgorithm signatureAlgorithm = signature.algorithm;
        String jcaSignatureAlgorithm = signatureAlgorithm.getJcaSignatureAlgorithmAndParams().getFirst();
        AlgorithmParameterSpec jcaSignatureAlgorithmParams = signatureAlgorithm.getJcaSignatureAlgorithmAndParams().getSecond();
        String keyAlgorithm = signatureAlgorithm.getJcaKeyAlgorithm();
        PublicKey publicKey;
        try {
            publicKey = KeyFactory.getInstance(keyAlgorithm).generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        } catch (Exception e) {
            result.addError(Issue.V2_SIG_MALFORMED_PUBLIC_KEY, e);
            return;
        }
        try {
            Signature sig = Signature.getInstance(jcaSignatureAlgorithm);
            sig.initVerify(publicKey);
            if (jcaSignatureAlgorithmParams != null) {
                sig.setParameter(jcaSignatureAlgorithmParams);
            }
            signedData.position(0);
            sig.update(signedData);
            byte[] sigBytes = signature.signature;
            if (!sig.verify(sigBytes)) {
                result.addError(Issue.V2_SIG_DID_NOT_VERIFY, signatureAlgorithm);
                return;
            }
            result.verifiedSignatures.put(signatureAlgorithm, sigBytes);
            contentDigestsToVerify.add(signatureAlgorithm.getContentDigestAlgorithm());
        } catch (InvalidKeyException | InvalidAlgorithmParameterException | SignatureException e) {
            result.addError(Issue.V2_SIG_VERIFY_EXCEPTION, signatureAlgorithm, e);
            return;
        }
    }
    // At least one signature over signedData has verified. We can now parse signed-data.
    signedData.position(0);
    ByteBuffer digests = ApkSigningBlockUtils.getLengthPrefixedSlice(signedData);
    ByteBuffer certificates = ApkSigningBlockUtils.getLengthPrefixedSlice(signedData);
    ByteBuffer additionalAttributes = ApkSigningBlockUtils.getLengthPrefixedSlice(signedData);
    // Parse the certificates block
    int certificateIndex = -1;
    while (certificates.hasRemaining()) {
        certificateIndex++;
        byte[] encodedCert = ApkSigningBlockUtils.readLengthPrefixedByteArray(certificates);
        X509Certificate certificate;
        try {
            certificate = X509CertificateUtils.generateCertificate(encodedCert, certFactory);
        } catch (CertificateException e) {
            result.addError(Issue.V2_SIG_MALFORMED_CERTIFICATE, certificateIndex, certificateIndex + 1, e);
            return;
        }
        // Wrap the cert so that the result's getEncoded returns exactly the original encoded
        // form. Without this, getEncoded may return a different form from what was stored in
        // the signature. This is because some X509Certificate(Factory) implementations
        // re-encode certificates.
        certificate = new GuaranteedEncodedFormX509Certificate(certificate, encodedCert);
        result.certs.add(certificate);
    }
    if (result.certs.isEmpty()) {
        result.addError(Issue.V2_SIG_NO_CERTIFICATES);
        return;
    }
    X509Certificate mainCertificate = result.certs.get(0);
    byte[] certificatePublicKeyBytes;
    try {
        certificatePublicKeyBytes = ApkSigningBlockUtils.encodePublicKey(mainCertificate.getPublicKey());
    } catch (InvalidKeyException e) {
        System.out.println("Caught an exception encoding the public key: " + e);
        e.printStackTrace();
        certificatePublicKeyBytes = mainCertificate.getPublicKey().getEncoded();
    }
    if (!Arrays.equals(publicKeyBytes, certificatePublicKeyBytes)) {
        result.addError(Issue.V2_SIG_PUBLIC_KEY_MISMATCH_BETWEEN_CERTIFICATE_AND_SIGNATURES_RECORD, ApkSigningBlockUtils.toHex(certificatePublicKeyBytes), ApkSigningBlockUtils.toHex(publicKeyBytes));
        return;
    }
    // Parse the digests block
    int digestCount = 0;
    while (digests.hasRemaining()) {
        digestCount++;
        try {
            ByteBuffer digest = ApkSigningBlockUtils.getLengthPrefixedSlice(digests);
            int sigAlgorithmId = digest.getInt();
            byte[] digestBytes = ApkSigningBlockUtils.readLengthPrefixedByteArray(digest);
            result.contentDigests.add(new ApkSigningBlockUtils.Result.SignerInfo.ContentDigest(sigAlgorithmId, digestBytes));
        } catch (ApkFormatException | BufferUnderflowException e) {
            result.addError(Issue.V2_SIG_MALFORMED_DIGEST, digestCount);
            return;
        }
    }
    List<Integer> sigAlgsFromSignaturesRecord = new ArrayList<>(result.signatures.size());
    for (ApkSigningBlockUtils.Result.SignerInfo.Signature signature : result.signatures) {
        sigAlgsFromSignaturesRecord.add(signature.getAlgorithmId());
    }
    List<Integer> sigAlgsFromDigestsRecord = new ArrayList<>(result.contentDigests.size());
    for (ApkSigningBlockUtils.Result.SignerInfo.ContentDigest digest : result.contentDigests) {
        sigAlgsFromDigestsRecord.add(digest.getSignatureAlgorithmId());
    }
    if (!sigAlgsFromSignaturesRecord.equals(sigAlgsFromDigestsRecord)) {
        result.addError(Issue.V2_SIG_SIG_ALG_MISMATCH_BETWEEN_SIGNATURES_AND_DIGESTS_RECORDS, sigAlgsFromSignaturesRecord, sigAlgsFromDigestsRecord);
        return;
    }
    // Parse the additional attributes block.
    int additionalAttributeCount = 0;
    Set<Integer> supportedApkSigSchemeIds = supportedApkSigSchemeNames.keySet();
    Set<Integer> supportedExpectedApkSigSchemeIds = new HashSet<>(1);
    while (additionalAttributes.hasRemaining()) {
        additionalAttributeCount++;
        try {
            ByteBuffer attribute = ApkSigningBlockUtils.getLengthPrefixedSlice(additionalAttributes);
            int id = attribute.getInt();
            byte[] value = ByteBufferUtils.toByteArray(attribute);
            result.additionalAttributes.add(new ApkSigningBlockUtils.Result.SignerInfo.AdditionalAttribute(id, value));
            switch(id) {
                case V2SchemeConstants.STRIPPING_PROTECTION_ATTR_ID:
                    // stripping protection added when signing with a newer scheme
                    int foundId = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getInt();
                    if (supportedApkSigSchemeIds.contains(foundId)) {
                        supportedExpectedApkSigSchemeIds.add(foundId);
                    } else {
                        result.addWarning(Issue.V2_SIG_UNKNOWN_APK_SIG_SCHEME_ID, result.index, foundId);
                    }
                    break;
                default:
                    result.addWarning(Issue.V2_SIG_UNKNOWN_ADDITIONAL_ATTRIBUTE, id);
            }
        } catch (ApkFormatException | BufferUnderflowException e) {
            result.addError(Issue.V2_SIG_MALFORMED_ADDITIONAL_ATTRIBUTE, additionalAttributeCount);
            return;
        }
    }
    // make sure that all known IDs indicated in stripping protection have already verified
    for (int id : supportedExpectedApkSigSchemeIds) {
        if (!foundApkSigSchemeIds.contains(id)) {
            String apkSigSchemeName = supportedApkSigSchemeNames.get(id);
            result.addError(Issue.V2_SIG_MISSING_APK_SIG_REFERENCED, result.index, apkSigSchemeName);
        }
    }
}
Also used : GuaranteedEncodedFormX509Certificate(com.android.apksig.internal.util.GuaranteedEncodedFormX509Certificate) ArrayList(java.util.ArrayList) SignatureAlgorithm(com.android.apksig.internal.apk.SignatureAlgorithm) CertificateException(java.security.cert.CertificateException) SignatureException(java.security.SignatureException) ApkSigningBlockUtils(com.android.apksig.internal.apk.ApkSigningBlockUtils) ApkFormatException(com.android.apksig.apk.ApkFormatException) BufferUnderflowException(java.nio.BufferUnderflowException) HashSet(java.util.HashSet) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ApkFormatException(com.android.apksig.apk.ApkFormatException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BufferUnderflowException(java.nio.BufferUnderflowException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) X509Certificate(java.security.cert.X509Certificate) GuaranteedEncodedFormX509Certificate(com.android.apksig.internal.util.GuaranteedEncodedFormX509Certificate) Signature(java.security.Signature) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 32 with ApkFormatException

use of com.android.apksig.apk.ApkFormatException in project apksig by venshine.

the class SourceStampVerifier method parseStampAttributes.

private static void parseStampAttributes(ByteBuffer stampAttributeData, X509Certificate sourceStampCertificate, ApkSignerInfo result) throws ApkFormatException {
    ByteBuffer stampAttributes = getLengthPrefixedSlice(stampAttributeData);
    int stampAttributeCount = 0;
    while (stampAttributes.hasRemaining()) {
        stampAttributeCount++;
        try {
            ByteBuffer attribute = getLengthPrefixedSlice(stampAttributes);
            int id = attribute.getInt();
            byte[] value = ByteBufferUtils.toByteArray(attribute);
            if (id == SourceStampConstants.PROOF_OF_ROTATION_ATTR_ID) {
                readStampCertificateLineage(value, sourceStampCertificate, result);
            } else {
                result.addWarning(ApkVerificationIssue.SOURCE_STAMP_UNKNOWN_ATTRIBUTE, id);
            }
        } catch (ApkFormatException | BufferUnderflowException e) {
            result.addWarning(ApkVerificationIssue.SOURCE_STAMP_MALFORMED_ATTRIBUTE, stampAttributeCount);
            return;
        }
    }
}
Also used : ApkFormatException(com.android.apksig.apk.ApkFormatException) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 33 with ApkFormatException

use of com.android.apksig.apk.ApkFormatException in project apksig by venshine.

the class V1SourceStampVerifier method verify.

/**
 * Verifies the provided APK's SourceStamp signatures and outputs the results into the provided
 * {@code result}. APK is considered verified only if there are no errors reported in the {@code
 * result}. See {@link #verify(DataSource, ApkUtils.ZipSections, byte[], Map, int, int)} for
 * more information about the contract of this method.
 */
private static void verify(ByteBuffer sourceStampBlock, byte[] sourceStampCertificateDigest, Map<ContentDigestAlgorithm, byte[]> apkContentDigests, int minSdkVersion, int maxSdkVersion, ApkSigningBlockUtils.Result result) throws NoSuchAlgorithmException {
    ApkSigningBlockUtils.Result.SignerInfo signerInfo = new ApkSigningBlockUtils.Result.SignerInfo();
    result.signers.add(signerInfo);
    try {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        ByteBuffer sourceStampBlockData = ApkSigningBlockUtils.getLengthPrefixedSlice(sourceStampBlock);
        byte[] digestBytes = encodeAsSequenceOfLengthPrefixedPairsOfIntAndLengthPrefixedBytes(getApkDigests(apkContentDigests));
        SourceStampVerifier.verifyV1SourceStamp(sourceStampBlockData, certFactory, signerInfo, digestBytes, sourceStampCertificateDigest, minSdkVersion, maxSdkVersion);
        result.verified = !result.containsErrors() && !result.containsWarnings();
    } catch (CertificateException e) {
        throw new IllegalStateException("Failed to obtain X.509 CertificateFactory", e);
    } catch (ApkFormatException | BufferUnderflowException e) {
        signerInfo.addWarning(ApkVerifier.Issue.SOURCE_STAMP_MALFORMED_SIGNATURE);
    }
}
Also used : ApkFormatException(com.android.apksig.apk.ApkFormatException) CertificateException(java.security.cert.CertificateException) ApkSigningBlockUtils(com.android.apksig.internal.apk.ApkSigningBlockUtils) CertificateFactory(java.security.cert.CertificateFactory) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 34 with ApkFormatException

use of com.android.apksig.apk.ApkFormatException in project apksig by venshine.

the class V3SigningCertificateLineage method readSigningCertificateLineage.

/**
 * Deserializes the binary representation of an {@link V3SigningCertificateLineage}. Also
 * verifies that the structure is well-formed, e.g. that the signature for each node is from its
 * parent.
 */
public static List<SigningCertificateNode> readSigningCertificateLineage(ByteBuffer inputBytes) throws IOException {
    List<SigningCertificateNode> result = new ArrayList<>();
    int nodeCount = 0;
    if (inputBytes == null || !inputBytes.hasRemaining()) {
        return null;
    }
    ApkSigningBlockUtils.checkByteOrderLittleEndian(inputBytes);
    // FORMAT (little endian):
    // * uint32: version code
    // * sequence of length-prefixed (uint32): nodes
    // * length-prefixed bytes: signed data
    // * length-prefixed bytes: certificate
    // * uint32: signature algorithm id
    // * uint32: flags
    // * uint32: signature algorithm id (used by to sign next cert in lineage)
    // * length-prefixed bytes: signature over above signed data
    X509Certificate lastCert = null;
    int lastSigAlgorithmId = 0;
    try {
        int version = inputBytes.getInt();
        if (version != CURRENT_VERSION) {
            // we only have one version to worry about right now, so just check it
            throw new IllegalArgumentException("Encoded SigningCertificateLineage has a version" + " different than any of which we are aware");
        }
        HashSet<X509Certificate> certHistorySet = new HashSet<>();
        while (inputBytes.hasRemaining()) {
            nodeCount++;
            ByteBuffer nodeBytes = getLengthPrefixedSlice(inputBytes);
            ByteBuffer signedData = getLengthPrefixedSlice(nodeBytes);
            int flags = nodeBytes.getInt();
            int sigAlgorithmId = nodeBytes.getInt();
            SignatureAlgorithm sigAlgorithm = SignatureAlgorithm.findById(lastSigAlgorithmId);
            byte[] signature = readLengthPrefixedByteArray(nodeBytes);
            if (lastCert != null) {
                // Use previous level cert to verify current level
                String jcaSignatureAlgorithm = sigAlgorithm.getJcaSignatureAlgorithmAndParams().getFirst();
                AlgorithmParameterSpec jcaSignatureAlgorithmParams = sigAlgorithm.getJcaSignatureAlgorithmAndParams().getSecond();
                PublicKey publicKey = lastCert.getPublicKey();
                Signature sig = Signature.getInstance(jcaSignatureAlgorithm);
                sig.initVerify(publicKey);
                if (jcaSignatureAlgorithmParams != null) {
                    sig.setParameter(jcaSignatureAlgorithmParams);
                }
                sig.update(signedData);
                if (!sig.verify(signature)) {
                    throw new SecurityException("Unable to verify signature of certificate #" + nodeCount + " using " + jcaSignatureAlgorithm + " when verifying" + " V3SigningCertificateLineage object");
                }
            }
            signedData.rewind();
            byte[] encodedCert = readLengthPrefixedByteArray(signedData);
            int signedSigAlgorithm = signedData.getInt();
            if (lastCert != null && lastSigAlgorithmId != signedSigAlgorithm) {
                throw new SecurityException("Signing algorithm ID mismatch for certificate #" + nodeBytes + " when verifying V3SigningCertificateLineage object");
            }
            lastCert = X509CertificateUtils.generateCertificate(encodedCert);
            lastCert = new GuaranteedEncodedFormX509Certificate(lastCert, encodedCert);
            if (certHistorySet.contains(lastCert)) {
                throw new SecurityException("Encountered duplicate entries in " + "SigningCertificateLineage at certificate #" + nodeCount + ".  All " + "signing certificates should be unique");
            }
            certHistorySet.add(lastCert);
            lastSigAlgorithmId = sigAlgorithmId;
            result.add(new SigningCertificateNode(lastCert, SignatureAlgorithm.findById(signedSigAlgorithm), SignatureAlgorithm.findById(sigAlgorithmId), signature, flags));
        }
    } catch (ApkFormatException | BufferUnderflowException e) {
        throw new IOException("Failed to parse V3SigningCertificateLineage object", e);
    } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | SignatureException e) {
        throw new SecurityException("Failed to verify signature over signed data for certificate #" + nodeCount + " when parsing V3SigningCertificateLineage object", e);
    } catch (CertificateException e) {
        throw new SecurityException("Failed to decode certificate #" + nodeCount + " when parsing V3SigningCertificateLineage object", e);
    }
    return result;
}
Also used : GuaranteedEncodedFormX509Certificate(com.android.apksig.internal.util.GuaranteedEncodedFormX509Certificate) ArrayList(java.util.ArrayList) SignatureAlgorithm(com.android.apksig.internal.apk.SignatureAlgorithm) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) ApkFormatException(com.android.apksig.apk.ApkFormatException) BufferUnderflowException(java.nio.BufferUnderflowException) HashSet(java.util.HashSet) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PublicKey(java.security.PublicKey) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer) X509Certificate(java.security.cert.X509Certificate) GuaranteedEncodedFormX509Certificate(com.android.apksig.internal.util.GuaranteedEncodedFormX509Certificate) Signature(java.security.Signature) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 35 with ApkFormatException

use of com.android.apksig.apk.ApkFormatException in project apksig by venshine.

the class ApkSigningBlockUtilsLite method readLengthPrefixedByteArray.

public static byte[] readLengthPrefixedByteArray(ByteBuffer buf) throws ApkFormatException {
    int len = buf.getInt();
    if (len < 0) {
        throw new ApkFormatException("Negative length");
    } else if (len > buf.remaining()) {
        throw new ApkFormatException("Underflow while reading length-prefixed value. Length: " + len + ", available: " + buf.remaining());
    }
    byte[] result = new byte[len];
    buf.get(result);
    return result;
}
Also used : ApkFormatException(com.android.apksig.apk.ApkFormatException)

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