use of com.android.bundle.CodeTransparencyOuterClass.CodeTransparency in project bundletool by google.
the class ApkTransparencyCheckUtils method checkTransparency.
public static TransparencyCheckResult checkTransparency(ImmutableList<Path> deviceSpecificApks) {
Optional<Path> baseApkPath = getBaseApkPath(deviceSpecificApks);
if (!baseApkPath.isPresent()) {
throw InvalidCommandException.builder().withInternalMessage("The provided list of device specific APKs must either contain a single APK, or, if" + " multiple APK files are present, base.apk file.").build();
}
TransparencyCheckResult.Builder result = TransparencyCheckResult.builder();
ApkSignatureVerifier.Result apkSignatureVerificationResult = ApkSignatureVerifier.verify(deviceSpecificApks);
if (!apkSignatureVerificationResult.verified()) {
return result.errorMessage("Verification failed: " + apkSignatureVerificationResult.getErrorMessage()).build();
}
result.apkSigningKeyCertificateFingerprint(apkSignatureVerificationResult.getApkSigningKeyCertificateFingerprint());
try (ZipFile baseApkFile = ZipUtils.openZipFile(baseApkPath.get())) {
Optional<ZipEntry> transparencyFileEntry = Optional.ofNullable(baseApkFile.getEntry(TRANSPARENCY_FILE_ZIP_ENTRY_NAME));
if (!transparencyFileEntry.isPresent()) {
throw InvalidCommandException.builder().withInternalMessage("Could not verify code transparency because transparency file is not present in the" + " APK.").build();
}
JsonWebSignature jws = CodeTransparencyCryptoUtils.parseJws(ZipUtils.asByteSource(baseApkFile, transparencyFileEntry.get()));
boolean signatureVerified = CodeTransparencyCryptoUtils.verifySignature(jws);
if (!signatureVerified) {
return result.errorMessage("Verification failed because code transparency signature is invalid.").build();
}
result.transparencySignatureVerified(true).transparencyKeyCertificateFingerprint(CodeTransparencyCryptoUtils.getCertificateFingerprint(jws));
CodeTransparency codeTransparencyMetadata = CodeTransparencyFactory.parseFrom(jws.getUnverifiedPayload());
CodeTransparencyVersion.checkVersion(codeTransparencyMetadata);
ImmutableSet<String> pathsToModifiedFiles = getModifiedFiles(codeTransparencyMetadata, deviceSpecificApks);
result.fileContentsVerified(pathsToModifiedFiles.isEmpty());
if (!pathsToModifiedFiles.isEmpty()) {
result.errorMessage("Verification failed because code was modified after code transparency metadata" + " generation. Modified files: " + pathsToModifiedFiles);
}
return result.build();
} catch (IOException e) {
throw new UncheckedIOException("An error occurred when processing the file.", e);
}
}
Aggregations