use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.
the class BuildApksManagerTest method apkWithSourceStamp.
@Test
public void apkWithSourceStamp() throws Exception {
String stampSource = "https://www.example.com";
SigningConfiguration signingConfiguration = SigningConfiguration.builder().setSignerConfig(privateKey, certificate).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withOutputPath(outputFilePath).withSigningConfig(signingConfiguration).withSourceStamp(SourceStamp.builder().setSource(stampSource).setSigningConfiguration(signingConfiguration).build()).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
for (Variant variant : result.getVariantList()) {
for (ApkSet apkSet : variant.getApkSetList()) {
for (ApkDescription apkDescription : apkSet.getApkDescriptionList()) {
File apk = extractFromApkSetFile(apkSetFile, apkDescription.getPath(), outputDir);
ApkVerifier.Result verifierResult = new ApkVerifier.Builder(apk).build().verify();
assertThat(verifierResult.isSourceStampVerified()).isTrue();
assertThat(verifierResult.getSourceStampInfo().getCertificate()).isEqualTo(certificate);
AndroidManifest manifest = extractAndroidManifest(apk, tmpDir);
assertThat(manifest.getMetadataValue(STAMP_SOURCE_METADATA_KEY)).hasValue(stampSource);
try (ZipFile apkZip = new ZipFile(apk)) {
ZipEntry sourceStampCertEntry = apkZip.getEntry("stamp-cert-sha256");
assertNotNull(sourceStampCertEntry);
byte[] sourceStampCertHash = ByteStreams.toByteArray(apkZip.getInputStream(sourceStampCertEntry));
assertThat(sourceStampCertHash).isEqualTo(CertificateHelper.getSha256Bytes(certificate.getEncoded()));
}
}
}
}
}
use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.
the class CheckTransparencyCommandTest method setUp.
@Before
public void setUp() throws Exception {
tmpDir = tmp.getRoot().toPath();
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(/* keysize= */
3072);
KeyPair keyPair = kpg.genKeyPair();
transparencyPrivateKey = keyPair.getPrivate();
transparencyKeyCertificate = CertificateFactory.buildSelfSignedCertificate(keyPair, "CN=CheckTransparencyCommandTest_TransparencyKey");
transparencyKeyCertificatePath = tmpDir.resolve("transparency-public.cert");
Files.write(transparencyKeyCertificatePath, transparencyKeyCertificate.getEncoded());
KeyPair apkSigningKeyPair = kpg.genKeyPair();
apkSigningKeyCertificate = CertificateFactory.buildSelfSignedCertificate(apkSigningKeyPair, "CN=CheckTransparencyCommandTest_ApkSigningKey");
apkSigningKeyCertificatePath = tmpDir.resolve("apk-signing-key-public.cert");
Files.write(apkSigningKeyCertificatePath, apkSigningKeyCertificate.getEncoded());
SigningConfiguration apkSigningConfig = SigningConfiguration.builder().setSignerConfig(SignerConfig.builder().setPrivateKey(apkSigningKeyPair.getPrivate()).setCertificates(ImmutableList.of(apkSigningKeyCertificate)).build()).build();
TestComponent.useTestModule(this, TestModule.builder().withSigningConfig(apkSigningConfig).build());
bundlePath = tmpDir.resolve("bundle.aab");
apkZipPath = tmpDir.resolve("apks.zip");
}
use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.
the class BuildApksCommandTest method buildingViaFlagsAndBuilderHasSameResult_stamp_separateKeystoreAndKeyAlias.
@Test
public void buildingViaFlagsAndBuilderHasSameResult_stamp_separateKeystoreAndKeyAlias() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
BuildApksCommand commandViaFlags = BuildApksCommand.fromFlags(new FlagParser().parse("--bundle=" + bundlePath, "--output=" + outputFilePath, "--aapt2=" + AAPT2_PATH, "--create-stamp=" + true, "--stamp-ks=" + stampKeystorePath, "--stamp-key-alias=" + STAMP_KEY_ALIAS, "--stamp-ks-pass=pass:" + STAMP_KEYSTORE_PASSWORD, "--stamp-key-pass=pass:" + STAMP_KEY_PASSWORD), new PrintStream(output), systemEnvironmentProvider, fakeAdbServer);
SigningConfiguration stampSigningConfiguration = SigningConfiguration.builder().setSignerConfig(stampPrivateKey, stampCertificate).build();
BuildApksCommand.Builder commandViaBuilder = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setSourceStamp(SourceStamp.builder().setSigningConfiguration(stampSigningConfiguration).build()).setAapt2Command(commandViaFlags.getAapt2Command().get()).setExecutorServiceInternal(commandViaFlags.getExecutorService()).setExecutorServiceCreatedByBundleTool(true).setOutputPrintStream(commandViaFlags.getOutputPrintStream().get());
DebugKeystoreUtils.getDebugSigningConfiguration(systemEnvironmentProvider).ifPresent(commandViaBuilder::setSigningConfiguration);
assertThat(commandViaBuilder.build()).isEqualTo(commandViaFlags);
}
use of com.android.tools.build.bundletool.model.SigningConfiguration 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.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.
the class BuildApksCommand method populateSigningConfigurationFromFlags.
private static void populateSigningConfigurationFromFlags(Builder buildApksCommand, ParsedFlags flags, PrintStream out, SystemEnvironmentProvider systemEnvironmentProvider) {
// Signing-related arguments.
Optional<Path> keystorePath = KEYSTORE_FLAG.getValue(flags);
Optional<String> keyAlias = KEY_ALIAS_FLAG.getValue(flags);
Optional<Password> keystorePassword = KEYSTORE_PASSWORD_FLAG.getValue(flags);
Optional<Password> keyPassword = KEY_PASSWORD_FLAG.getValue(flags);
Optional<Integer> minV3RotationApi = MINIMUM_V3_ROTATION_API_VERSION_FLAG.getValue(flags);
Optional<Integer> rotationMinSdkVersion = ROTATION_MINIMUM_SDK_VERSION_FLAG.getValue(flags);
if (keystorePath.isPresent() && keyAlias.isPresent()) {
SignerConfig signerConfig = SignerConfig.extractFromKeystore(keystorePath.get(), keyAlias.get(), keystorePassword, keyPassword);
SigningConfiguration.Builder builder = SigningConfiguration.builder().setSignerConfig(signerConfig).setMinimumV3RotationApiVersion(minV3RotationApi).setRotationMinSdkVersion(rotationMinSdkVersion);
populateLineageFromFlags(builder, flags);
buildApksCommand.setSigningConfiguration(builder.build());
} else if (keystorePath.isPresent() && !keyAlias.isPresent()) {
throw InvalidCommandException.builder().withInternalMessage("Flag --ks-key-alias is required when --ks is set.").build();
} else if (!keystorePath.isPresent() && keyAlias.isPresent()) {
throw InvalidCommandException.builder().withInternalMessage("Flag --ks is required when --ks-key-alias is set.").build();
} else {
// Try to use debug keystore if present.
Optional<SigningConfiguration> debugConfig = DebugKeystoreUtils.getDebugSigningConfiguration(systemEnvironmentProvider);
if (debugConfig.isPresent()) {
out.printf("INFO: The APKs will be signed with the debug keystore found at '%s'.%n", DebugKeystoreUtils.DEBUG_KEYSTORE_CACHE.getUnchecked(systemEnvironmentProvider).get());
buildApksCommand.setSigningConfiguration(debugConfig.get());
} else {
out.println("WARNING: The APKs won't be signed and thus not installable unless you also pass a " + "keystore via the flag --ks. See the command help for more information.");
}
}
}
Aggregations