AndroidVerifyBoot
生活随笔
收集整理的這篇文章主要介紹了
AndroidVerifyBoot
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
點(diǎn)擊打開(kāi)鏈接
xref: /build/core/Makefile
如下code 所示,可見(jiàn)511行是在510行的MKBOOTIMG之后即已經(jīng)產(chǎn)生boot.img的情況下才調(diào)用(BOOT_SIGNER)來(lái)為kernel+ramdisk 計(jì)算一個(gè)總的signature.508$(INSTALLED_BOOTIMAGE_TARGET): $(MKBOOTIMG) $(INTERNAL_BOOTIMAGE_FILES) $(BOOT_SIGNER)
509 $(call pretty,"Target boot image: $@")
510 $(hide) $(MKBOOTIMG) $(INTERNAL_BOOTIMAGE_ARGS) $(BOARD_MKBOOTIMG_ARGS) --output $@
511 $(BOOT_SIGNER) /boot $@ $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VERITY_SIGNING_KEY).pk8 $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VERITY_SIGNING_KEY).x509.pem $@
512 $(hide) $(call assert-max-image-size,$@,$(BOARD_BOOTIMAGE_PARTITION_SIZE))
BOOT_SIGNER 是在哪里定義的呢?
xref: /build/core/config.mk
BOOT_SIGNER := $(HOST_OUT_EXECUTABLES)/boot_signer
?/system/extras/verity/Android.mk
72include $(CLEAR_VARS)
73LOCAL_SRC_FILES := boot_signer
74LOCAL_MODULE := boot_signer
75LOCAL_MODULE_CLASS := EXECUTABLES
76LOCAL_IS_HOST_MODULE := true
77LOCAL_MODULE_TAGS := optional
78LOCAL_REQUIRED_MODULES := BootSignature
79include $(BUILD_PREBUILT)
boot_signer是一個(gè)prebuild好的可執(zhí)行檔.
boot_signer.sh 調(diào)用boot.signature.jar 包來(lái)直接調(diào)用bootsignature.java,所以其源碼是bootsignature.java
?public static void main(String[] args) throws Exception {
291 ? ? ? ?Security.addProvider(new BouncyCastleProvider());
292
293 ? ? ? ?if ("-verify".equals(args[0])) {
294 ? ? ? ? ? ?/* args[1] is the path to a signed boot image */
295 ? ? ? ? ? ?verifySignature(args[1]);
296 ? ? ? ?} else {
303 ? ? ? ? ? ?doSignature(args[0], args[1], args[2], args[3], args[4]);
304 ? ? ? ?}
305 ? ?}
這個(gè)函數(shù)可以校驗(yàn)boot.img 也可以計(jì)算boot.img的signature。我們來(lái)看看doSignature
其四個(gè)參數(shù)的意思如下:
?args[0] is the target name, typically /boot
args[1] is the path to a boot image to sign
args[2] is the path to a private key
args[3] is the path to the matching public key certificate
args[4] is the path where to output the signed boot image
public static void doSignature( String target,
221 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String imagePath,
222 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String keyPath,
223 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String certPath,
224 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String outPath) throws Exception {
225
226 ? ? ? ?byte[] image = Utils.read(imagePath);
227 ? ? ? ?int signableSize = getSignableImageSize(image);
228
229 ? ? ? ?if (signableSize < image.length) {
230 ? ? ? ? ? ?System.err.println("NOTE: truncating file " + imagePath +
231 ? ? ? ? ? ? ? ? ? ?" from " + image.length + " to " + signableSize + " bytes");
232 ? ? ? ? ? ?image = Arrays.copyOf(image, signableSize);
233 ? ? ? ?} else if (signableSize > image.length) {
234 ? ? ? ? ? ?throw new IllegalArgumentException("Invalid image: too short, expected " +
235 ? ? ? ? ? ? ? ? ? ?signableSize + " bytes");
236 ? ? ? ?}
237
238 ? ? ? ?BootSignature bootsig = new BootSignature(target, image.length);
239
240 ? ? ? ?X509Certificate cert = Utils.loadPEMCertificate(certPath);
241 ? ? ? ?bootsig.setCertificate(cert);
242
243 ? ? ? ?PrivateKey key = Utils.loadDERPrivateKeyFromFile(keyPath);
244 ? ? ? ?bootsig.setSignature(bootsig.sign(image, key),
245 ? ? ? ? ? ?Utils.getSignatureAlgorithmIdentifier(key));
246
247 ? ? ? ?byte[] encoded_bootsig = bootsig.getEncoded();
248 ? ? ? ?byte[] image_with_metadata = Arrays.copyOf(image, image.length + encoded_bootsig.length);
249
250 ? ? ? ?System.arraycopy(encoded_bootsig, 0, image_with_metadata,
251 ? ? ? ? ? ? ? ?image.length, encoded_bootsig.length);
252
253 ? ? ? ?Utils.write(image_with_metadata, outPath);
254 ? ?}
227行得到boot.img的size 238行new一個(gè)BootSignature。241設(shè)定證書(shū)。243 裝載private key.
總結(jié)
以上是生活随笔為你收集整理的AndroidVerifyBoot的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: little kernel中如何决定ap
- 下一篇: Android 系统当中各种尺寸单位的定