@Test (expected = IllegalArgumentException.class) public void givenAnEmptyContainerPath_whenVerifyContainerAgainstCertificate_thenThrows() { testContainerSignatureVerifier .verifyContainerSignatureAgainstCertificate(EMPTY_CONTAINER_PATH, trustedCertificate); }
boolean verifyContainerSignatureAgainstCertificate( @NonNull String containerPath, @NonNull X509Certificate trustedCertificate) { checkNotNull(containerPath); checkArgument( !containerPath.isEmpty() && (new File(containerPath).exists()), "The container path must be associated to an existing file"); checkNotNull(trustedCertificate); Optional<String> optionalExtension = extractExtensionFromFilePath(containerPath); if (!optionalExtension.isPresent()) { return false; } String extension = optionalExtension.get(); boolean signatureCheckIsSuccessful = false; if (extension.equals(APK_EXTENSION)) { PackageInfo mPackageInfo = packageManager.getPackageArchiveInfo(containerPath, PackageManager.GET_SIGNATURES); if (mPackageInfo != null) { Signature[] signatures = mPackageInfo.signatures; if (signatures != null) { for (Signature sign : signatures) { if (sign != null) { X509Certificate certFromSign; InputStream inStream = null; try { inStream = new ByteArrayInputStream(sign.toByteArray()); certFromSign = (X509Certificate) certificateFactory.generateCertificate(inStream); certFromSign.checkValidity(); if (certFromSign.equals(trustedCertificate)) signatureCheckIsSuccessful = true; } catch (CertificateException e) { } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted signature is associated with the source archive."); } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted container was found."); } } if (extension.equals(JAR_EXTENSION) || (extension.equals(APK_EXTENSION) && signatureCheckIsSuccessful)) { JarFile containerToVerify = null; try { containerToVerify = new JarFile(containerPath); verifyJARContainer(containerToVerify, trustedCertificate); signatureCheckIsSuccessful = true; } catch (Exception e) { signatureCheckIsSuccessful = false; } finally { if (containerToVerify != null) try { containerToVerify.close(); } catch (IOException e) { e.printStackTrace(); } } } return signatureCheckIsSuccessful; }
ContainerSignatureVerifier { boolean verifyContainerSignatureAgainstCertificate( @NonNull String containerPath, @NonNull X509Certificate trustedCertificate) { checkNotNull(containerPath); checkArgument( !containerPath.isEmpty() && (new File(containerPath).exists()), "The container path must be associated to an existing file"); checkNotNull(trustedCertificate); Optional<String> optionalExtension = extractExtensionFromFilePath(containerPath); if (!optionalExtension.isPresent()) { return false; } String extension = optionalExtension.get(); boolean signatureCheckIsSuccessful = false; if (extension.equals(APK_EXTENSION)) { PackageInfo mPackageInfo = packageManager.getPackageArchiveInfo(containerPath, PackageManager.GET_SIGNATURES); if (mPackageInfo != null) { Signature[] signatures = mPackageInfo.signatures; if (signatures != null) { for (Signature sign : signatures) { if (sign != null) { X509Certificate certFromSign; InputStream inStream = null; try { inStream = new ByteArrayInputStream(sign.toByteArray()); certFromSign = (X509Certificate) certificateFactory.generateCertificate(inStream); certFromSign.checkValidity(); if (certFromSign.equals(trustedCertificate)) signatureCheckIsSuccessful = true; } catch (CertificateException e) { } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted signature is associated with the source archive."); } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted container was found."); } } if (extension.equals(JAR_EXTENSION) || (extension.equals(APK_EXTENSION) && signatureCheckIsSuccessful)) { JarFile containerToVerify = null; try { containerToVerify = new JarFile(containerPath); verifyJARContainer(containerToVerify, trustedCertificate); signatureCheckIsSuccessful = true; } catch (Exception e) { signatureCheckIsSuccessful = false; } finally { if (containerToVerify != null) try { containerToVerify.close(); } catch (IOException e) { e.printStackTrace(); } } } return signatureCheckIsSuccessful; } }
ContainerSignatureVerifier { boolean verifyContainerSignatureAgainstCertificate( @NonNull String containerPath, @NonNull X509Certificate trustedCertificate) { checkNotNull(containerPath); checkArgument( !containerPath.isEmpty() && (new File(containerPath).exists()), "The container path must be associated to an existing file"); checkNotNull(trustedCertificate); Optional<String> optionalExtension = extractExtensionFromFilePath(containerPath); if (!optionalExtension.isPresent()) { return false; } String extension = optionalExtension.get(); boolean signatureCheckIsSuccessful = false; if (extension.equals(APK_EXTENSION)) { PackageInfo mPackageInfo = packageManager.getPackageArchiveInfo(containerPath, PackageManager.GET_SIGNATURES); if (mPackageInfo != null) { Signature[] signatures = mPackageInfo.signatures; if (signatures != null) { for (Signature sign : signatures) { if (sign != null) { X509Certificate certFromSign; InputStream inStream = null; try { inStream = new ByteArrayInputStream(sign.toByteArray()); certFromSign = (X509Certificate) certificateFactory.generateCertificate(inStream); certFromSign.checkValidity(); if (certFromSign.equals(trustedCertificate)) signatureCheckIsSuccessful = true; } catch (CertificateException e) { } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted signature is associated with the source archive."); } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted container was found."); } } if (extension.equals(JAR_EXTENSION) || (extension.equals(APK_EXTENSION) && signatureCheckIsSuccessful)) { JarFile containerToVerify = null; try { containerToVerify = new JarFile(containerPath); verifyJARContainer(containerToVerify, trustedCertificate); signatureCheckIsSuccessful = true; } catch (Exception e) { signatureCheckIsSuccessful = false; } finally { if (containerToVerify != null) try { containerToVerify.close(); } catch (IOException e) { e.printStackTrace(); } } } return signatureCheckIsSuccessful; } ContainerSignatureVerifier(
            @NonNull PackageManager packageManager,
            @NonNull CertificateFactory certificateFactory); }
ContainerSignatureVerifier { boolean verifyContainerSignatureAgainstCertificate( @NonNull String containerPath, @NonNull X509Certificate trustedCertificate) { checkNotNull(containerPath); checkArgument( !containerPath.isEmpty() && (new File(containerPath).exists()), "The container path must be associated to an existing file"); checkNotNull(trustedCertificate); Optional<String> optionalExtension = extractExtensionFromFilePath(containerPath); if (!optionalExtension.isPresent()) { return false; } String extension = optionalExtension.get(); boolean signatureCheckIsSuccessful = false; if (extension.equals(APK_EXTENSION)) { PackageInfo mPackageInfo = packageManager.getPackageArchiveInfo(containerPath, PackageManager.GET_SIGNATURES); if (mPackageInfo != null) { Signature[] signatures = mPackageInfo.signatures; if (signatures != null) { for (Signature sign : signatures) { if (sign != null) { X509Certificate certFromSign; InputStream inStream = null; try { inStream = new ByteArrayInputStream(sign.toByteArray()); certFromSign = (X509Certificate) certificateFactory.generateCertificate(inStream); certFromSign.checkValidity(); if (certFromSign.equals(trustedCertificate)) signatureCheckIsSuccessful = true; } catch (CertificateException e) { } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted signature is associated with the source archive."); } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted container was found."); } } if (extension.equals(JAR_EXTENSION) || (extension.equals(APK_EXTENSION) && signatureCheckIsSuccessful)) { JarFile containerToVerify = null; try { containerToVerify = new JarFile(containerPath); verifyJARContainer(containerToVerify, trustedCertificate); signatureCheckIsSuccessful = true; } catch (Exception e) { signatureCheckIsSuccessful = false; } finally { if (containerToVerify != null) try { containerToVerify.close(); } catch (IOException e) { e.printStackTrace(); } } } return signatureCheckIsSuccessful; } ContainerSignatureVerifier(
            @NonNull PackageManager packageManager,
            @NonNull CertificateFactory certificateFactory);  }
ContainerSignatureVerifier { boolean verifyContainerSignatureAgainstCertificate( @NonNull String containerPath, @NonNull X509Certificate trustedCertificate) { checkNotNull(containerPath); checkArgument( !containerPath.isEmpty() && (new File(containerPath).exists()), "The container path must be associated to an existing file"); checkNotNull(trustedCertificate); Optional<String> optionalExtension = extractExtensionFromFilePath(containerPath); if (!optionalExtension.isPresent()) { return false; } String extension = optionalExtension.get(); boolean signatureCheckIsSuccessful = false; if (extension.equals(APK_EXTENSION)) { PackageInfo mPackageInfo = packageManager.getPackageArchiveInfo(containerPath, PackageManager.GET_SIGNATURES); if (mPackageInfo != null) { Signature[] signatures = mPackageInfo.signatures; if (signatures != null) { for (Signature sign : signatures) { if (sign != null) { X509Certificate certFromSign; InputStream inStream = null; try { inStream = new ByteArrayInputStream(sign.toByteArray()); certFromSign = (X509Certificate) certificateFactory.generateCertificate(inStream); certFromSign.checkValidity(); if (certFromSign.equals(trustedCertificate)) signatureCheckIsSuccessful = true; } catch (CertificateException e) { } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted signature is associated with the source archive."); } } else { Log.i(TAG_CONTAINER_SIGNATURE_VERIFIER, "An invalid/corrupted container was found."); } } if (extension.equals(JAR_EXTENSION) || (extension.equals(APK_EXTENSION) && signatureCheckIsSuccessful)) { JarFile containerToVerify = null; try { containerToVerify = new JarFile(containerPath); verifyJARContainer(containerToVerify, trustedCertificate); signatureCheckIsSuccessful = true; } catch (Exception e) { signatureCheckIsSuccessful = false; } finally { if (containerToVerify != null) try { containerToVerify.close(); } catch (IOException e) { e.printStackTrace(); } } } return signatureCheckIsSuccessful; } ContainerSignatureVerifier(
            @NonNull PackageManager packageManager,
            @NonNull CertificateFactory certificateFactory);   }