|
| 1 | +/* |
| 2 | + * Copyright 2018-present Facebook, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | + * not use this file except in compliance with the License. You may obtain |
| 6 | + * a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.facebook.buck.android; |
| 18 | + |
| 19 | +import com.android.apksig.ApkSigner; |
| 20 | +import com.android.sdklib.build.ApkCreationException; |
| 21 | +import com.facebook.buck.io.filesystem.ProjectFilesystem; |
| 22 | +import com.facebook.buck.step.ExecutionContext; |
| 23 | +import com.facebook.buck.step.Step; |
| 24 | +import com.facebook.buck.step.StepExecutionResult; |
| 25 | +import com.facebook.buck.step.StepExecutionResults; |
| 26 | +import com.google.common.base.Preconditions; |
| 27 | +import java.io.File; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.security.KeyStore; |
| 30 | +import java.security.KeyStoreException; |
| 31 | +import java.security.PrivateKey; |
| 32 | +import java.security.cert.Certificate; |
| 33 | +import java.security.cert.X509Certificate; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.List; |
| 37 | +import java.util.function.Supplier; |
| 38 | + |
| 39 | +/** Use Google apksigner to v1/v2/v3 sign the final APK */ |
| 40 | +class ApkSignerStep implements Step { |
| 41 | + |
| 42 | + private final ProjectFilesystem filesystem; |
| 43 | + private final Path inputApkPath; |
| 44 | + private final Path outputApkPath; |
| 45 | + private final Supplier<KeystoreProperties> keystorePropertiesSupplier; |
| 46 | + private final boolean isRedexBuild; |
| 47 | + |
| 48 | + public ApkSignerStep( |
| 49 | + ProjectFilesystem filesystem, |
| 50 | + Path inputApkPath, |
| 51 | + Path outputApkPath, |
| 52 | + Supplier<KeystoreProperties> keystorePropertiesSupplier, |
| 53 | + boolean isRedexBuild) { |
| 54 | + this.filesystem = filesystem; |
| 55 | + this.inputApkPath = inputApkPath; |
| 56 | + this.outputApkPath = outputApkPath; |
| 57 | + this.keystorePropertiesSupplier = keystorePropertiesSupplier; |
| 58 | + this.isRedexBuild = isRedexBuild; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public StepExecutionResult execute(ExecutionContext context) { |
| 63 | + try { |
| 64 | + List<ApkSigner.SignerConfig> signerConfigs = getSignerConfigs(); |
| 65 | + File inputApkFile = filesystem.getPathForRelativePath(inputApkPath).toFile(); |
| 66 | + File outputApkFile = filesystem.getPathForRelativePath(outputApkPath).toFile(); |
| 67 | + signApkFile(inputApkFile, outputApkFile, signerConfigs); |
| 68 | + } catch (Exception e) { |
| 69 | + context.logError(e, "Error when signing APK at: %s.", outputApkPath); |
| 70 | + return StepExecutionResults.ERROR; |
| 71 | + } |
| 72 | + return StepExecutionResults.SUCCESS; |
| 73 | + } |
| 74 | + |
| 75 | + /** Sign the APK using Google's {@link com.android.apksig.ApkSigner} */ |
| 76 | + private void signApkFile( |
| 77 | + File inputApk, File outputApk, List<ApkSigner.SignerConfig> signerConfigs) |
| 78 | + throws ApkCreationException { |
| 79 | + ApkSigner.Builder apkSignerBuilder = new ApkSigner.Builder(signerConfigs); |
| 80 | + // For non-redex build, apkSignerBuilder can look up minimum SDK version from |
| 81 | + // AndroidManifest.xml. Redex build does not have AndroidManifest.xml, so we |
| 82 | + // manually set it here. |
| 83 | + if (isRedexBuild) { |
| 84 | + apkSignerBuilder.setMinSdkVersion(1); |
| 85 | + } |
| 86 | + try { |
| 87 | + apkSignerBuilder |
| 88 | + .setV1SigningEnabled(true) |
| 89 | + .setV2SigningEnabled(true) |
| 90 | + .setV3SigningEnabled(false) |
| 91 | + .setInputApk(inputApk) |
| 92 | + .setOutputApk(outputApk) |
| 93 | + .build() |
| 94 | + .sign(); |
| 95 | + } catch (Exception e) { |
| 96 | + throw new ApkCreationException(e, "Failed to sign APK"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private List<ApkSigner.SignerConfig> getSignerConfigs() throws KeyStoreException { |
| 101 | + KeystoreProperties keystoreProperties = keystorePropertiesSupplier.get(); |
| 102 | + Path keystorePath = keystoreProperties.getKeystore(); |
| 103 | + char[] keystorePassword = keystoreProperties.getStorepass().toCharArray(); |
| 104 | + String keyAlias = keystoreProperties.getAlias(); |
| 105 | + char[] keyPassword = keystoreProperties.getKeypass().toCharArray(); |
| 106 | + KeyStore keystore = loadKeyStore(keystorePath, keystorePassword); |
| 107 | + PrivateKey key = loadPrivateKey(keystore, keyAlias, keyPassword); |
| 108 | + List<X509Certificate> certs = loadCertificates(keystore, keyAlias); |
| 109 | + ApkSigner.SignerConfig signerConfig = |
| 110 | + new ApkSigner.SignerConfig.Builder("CERT", key, certs).build(); |
| 111 | + List<ApkSigner.SignerConfig> configs = new ArrayList<>(Arrays.asList(signerConfig)); |
| 112 | + return configs; |
| 113 | + } |
| 114 | + |
| 115 | + private KeyStore loadKeyStore(Path keystorePath, char[] keystorePassword) |
| 116 | + throws KeyStoreException { |
| 117 | + try { |
| 118 | + String ksType = KeyStore.getDefaultType(); |
| 119 | + KeyStore keystore = KeyStore.getInstance(ksType); |
| 120 | + keystore.load(filesystem.getInputStreamForRelativePath(keystorePath), keystorePassword); |
| 121 | + return keystore; |
| 122 | + } catch (Exception e) { |
| 123 | + throw new KeyStoreException("Failed to load keystore from " + keystorePath); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private PrivateKey loadPrivateKey(KeyStore keystore, String keyAlias, char[] keyPassword) |
| 128 | + throws KeyStoreException { |
| 129 | + PrivateKey key; |
| 130 | + try { |
| 131 | + key = (PrivateKey) keystore.getKey(keyAlias, keyPassword); |
| 132 | + // key can be null if alias/password is incorrect. |
| 133 | + Preconditions.checkNotNull(key); |
| 134 | + } catch (Exception e) { |
| 135 | + throw new KeyStoreException( |
| 136 | + "Failed to load private key \"" + keyAlias + "\" from " + keystore); |
| 137 | + } |
| 138 | + return key; |
| 139 | + } |
| 140 | + |
| 141 | + private List<X509Certificate> loadCertificates(KeyStore keystore, String keyAlias) |
| 142 | + throws KeyStoreException { |
| 143 | + Certificate[] certChain = keystore.getCertificateChain(keyAlias); |
| 144 | + if ((certChain == null) || (certChain.length == 0)) { |
| 145 | + throw new KeyStoreException( |
| 146 | + keystore + " entry \"" + keyAlias + "\" does not contain certificates"); |
| 147 | + } |
| 148 | + List<X509Certificate> certs = new ArrayList<>(certChain.length); |
| 149 | + for (Certificate cert : certChain) { |
| 150 | + certs.add((X509Certificate) cert); |
| 151 | + } |
| 152 | + return certs; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String getShortName() { |
| 157 | + return "apk_signer"; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public String getDescription(ExecutionContext context) { |
| 162 | + return getShortName(); |
| 163 | + } |
| 164 | +} |
0 commit comments