This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Support V2 signing on local debug version of fb4a. - Compile and include apksigner.jar from (Google's Android Open Source](https://fburl.com/0c6z5fof) to buck aosp respository - Add an ApkSignerStep to ApkBuilder to support v1/v2/v3 signing on APK, using apksigner.jar Reviewed By: styurin fbshipit-source-id: 2bc1b7dd37
- Loading branch information
1 parent
1e2d7d4
commit cf9c2ad
Showing
14 changed files
with
464 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright 2018-present Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.facebook.buck.android; | ||
|
||
import com.android.apksig.ApkSigner; | ||
import com.android.sdklib.build.ApkCreationException; | ||
import com.facebook.buck.io.filesystem.ProjectFilesystem; | ||
import com.facebook.buck.step.ExecutionContext; | ||
import com.facebook.buck.step.Step; | ||
import com.facebook.buck.step.StepExecutionResult; | ||
import com.facebook.buck.step.StepExecutionResults; | ||
import com.google.common.base.Preconditions; | ||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.PrivateKey; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
/** Use Google apksigner to v1/v2/v3 sign the final APK */ | ||
class ApkSignerStep implements Step { | ||
|
||
private final ProjectFilesystem filesystem; | ||
private final Path inputApkPath; | ||
private final Path outputApkPath; | ||
private final Supplier<KeystoreProperties> keystorePropertiesSupplier; | ||
private final boolean isRedexBuild; | ||
|
||
public ApkSignerStep( | ||
ProjectFilesystem filesystem, | ||
Path inputApkPath, | ||
Path outputApkPath, | ||
Supplier<KeystoreProperties> keystorePropertiesSupplier, | ||
boolean isRedexBuild) { | ||
this.filesystem = filesystem; | ||
this.inputApkPath = inputApkPath; | ||
this.outputApkPath = outputApkPath; | ||
this.keystorePropertiesSupplier = keystorePropertiesSupplier; | ||
this.isRedexBuild = isRedexBuild; | ||
} | ||
|
||
@Override | ||
public StepExecutionResult execute(ExecutionContext context) { | ||
try { | ||
List<ApkSigner.SignerConfig> signerConfigs = getSignerConfigs(); | ||
File inputApkFile = filesystem.getPathForRelativePath(inputApkPath).toFile(); | ||
File outputApkFile = filesystem.getPathForRelativePath(outputApkPath).toFile(); | ||
signApkFile(inputApkFile, outputApkFile, signerConfigs); | ||
} catch (Exception e) { | ||
context.logError(e, "Error when signing APK at: %s.", outputApkPath); | ||
return StepExecutionResults.ERROR; | ||
} | ||
return StepExecutionResults.SUCCESS; | ||
} | ||
|
||
/** Sign the APK using Google's {@link com.android.apksig.ApkSigner} */ | ||
private void signApkFile( | ||
File inputApk, File outputApk, List<ApkSigner.SignerConfig> signerConfigs) | ||
throws ApkCreationException { | ||
ApkSigner.Builder apkSignerBuilder = new ApkSigner.Builder(signerConfigs); | ||
// For non-redex build, apkSignerBuilder can look up minimum SDK version from | ||
// AndroidManifest.xml. Redex build does not have AndroidManifest.xml, so we | ||
// manually set it here. | ||
if (isRedexBuild) { | ||
apkSignerBuilder.setMinSdkVersion(1); | ||
} | ||
try { | ||
apkSignerBuilder | ||
.setV1SigningEnabled(true) | ||
.setV2SigningEnabled(true) | ||
.setV3SigningEnabled(false) | ||
.setInputApk(inputApk) | ||
.setOutputApk(outputApk) | ||
.build() | ||
.sign(); | ||
} catch (Exception e) { | ||
throw new ApkCreationException(e, "Failed to sign APK"); | ||
} | ||
} | ||
|
||
private List<ApkSigner.SignerConfig> getSignerConfigs() throws KeyStoreException { | ||
KeystoreProperties keystoreProperties = keystorePropertiesSupplier.get(); | ||
Path keystorePath = keystoreProperties.getKeystore(); | ||
char[] keystorePassword = keystoreProperties.getStorepass().toCharArray(); | ||
String keyAlias = keystoreProperties.getAlias(); | ||
char[] keyPassword = keystoreProperties.getKeypass().toCharArray(); | ||
KeyStore keystore = loadKeyStore(keystorePath, keystorePassword); | ||
PrivateKey key = loadPrivateKey(keystore, keyAlias, keyPassword); | ||
List<X509Certificate> certs = loadCertificates(keystore, keyAlias); | ||
ApkSigner.SignerConfig signerConfig = | ||
new ApkSigner.SignerConfig.Builder("CERT", key, certs).build(); | ||
List<ApkSigner.SignerConfig> configs = new ArrayList<>(Arrays.asList(signerConfig)); | ||
return configs; | ||
} | ||
|
||
private KeyStore loadKeyStore(Path keystorePath, char[] keystorePassword) | ||
throws KeyStoreException { | ||
try { | ||
String ksType = KeyStore.getDefaultType(); | ||
KeyStore keystore = KeyStore.getInstance(ksType); | ||
keystore.load(filesystem.getInputStreamForRelativePath(keystorePath), keystorePassword); | ||
return keystore; | ||
} catch (Exception e) { | ||
throw new KeyStoreException("Failed to load keystore from " + keystorePath); | ||
} | ||
} | ||
|
||
private PrivateKey loadPrivateKey(KeyStore keystore, String keyAlias, char[] keyPassword) | ||
throws KeyStoreException { | ||
PrivateKey key; | ||
try { | ||
key = (PrivateKey) keystore.getKey(keyAlias, keyPassword); | ||
// key can be null if alias/password is incorrect. | ||
Preconditions.checkNotNull(key); | ||
} catch (Exception e) { | ||
throw new KeyStoreException( | ||
"Failed to load private key \"" + keyAlias + "\" from " + keystore); | ||
} | ||
return key; | ||
} | ||
|
||
private List<X509Certificate> loadCertificates(KeyStore keystore, String keyAlias) | ||
throws KeyStoreException { | ||
Certificate[] certChain = keystore.getCertificateChain(keyAlias); | ||
if ((certChain == null) || (certChain.length == 0)) { | ||
throw new KeyStoreException( | ||
keystore + " entry \"" + keyAlias + "\" does not contain certificates"); | ||
} | ||
List<X509Certificate> certs = new ArrayList<>(certChain.length); | ||
for (Certificate cert : certChain) { | ||
certs.add((X509Certificate) cert); | ||
} | ||
return certs; | ||
} | ||
|
||
@Override | ||
public String getShortName() { | ||
return "apk_signer"; | ||
} | ||
|
||
@Override | ||
public String getDescription(ExecutionContext context) { | ||
return getShortName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
java_library(required_for_source_only_abi = True, | ||
name = "aosp", | ||
srcs = glob(["src/**/*.java"]), | ||
licenses = [ | ||
java_library( | ||
required_for_source_only_abi=True, | ||
name="aosp", | ||
srcs=glob(["src/**/*.java"]), | ||
licenses=[ | ||
"NOTICE", | ||
"LICENSE", | ||
], | ||
visibility = [ | ||
"PUBLIC", | ||
], | ||
deps = [ | ||
visibility=["PUBLIC"], | ||
deps=[ | ||
"//third-party/java/android:sdklib", | ||
"//third-party/java/gson:gson", | ||
"//third-party/java/guava:guava", | ||
], | ||
) | ||
|
||
prebuilt_jar( | ||
name="apksig", | ||
binary_jar="apksig.jar", | ||
source_jar="apksig-sources.jar", | ||
licenses=[ | ||
"LICENSE", | ||
], | ||
visibility=["PUBLIC"], | ||
deps=[], | ||
) |
Oops, something went wrong.