blob: 6d1032d35759a9c30d7d849f1ef6fc791f4def1b [file] [log] [blame]
Jiyong Parkff1458f2018-10-12 21:49:38 +09001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
19 "io"
20
21 "android/soong/android"
Colin Cross5f692ec2019-02-01 16:53:07 -080022
Jiyong Parkff1458f2018-10-12 21:49:38 +090023 "github.com/google/blueprint/proptools"
24)
25
26var String = proptools.String
27
28func init() {
29 android.RegisterModuleType("apex_key", apexKeyFactory)
30}
31
32type apexKey struct {
33 android.ModuleBase
34
35 properties apexKeyProperties
36
37 public_key_file android.Path
38 private_key_file android.Path
39
40 keyName string
41}
42
43type apexKeyProperties struct {
44 // Path to the public key file in avbpubkey format. Installed to the device.
45 // Base name of the file is used as the ID for the key.
46 Public_key *string
47 // Path to the private key file in pem format. Used to sign APEXs.
48 Private_key *string
Jiyong Park50d99202018-12-27 13:32:34 +090049
50 // Whether this key is installable to one of the partitions. Defualt: true.
51 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090052}
53
54func apexKeyFactory() android.Module {
55 module := &apexKey{}
56 module.AddProperties(&module.properties)
57 android.InitAndroidModule(module)
58 return module
59}
60
Jiyong Park50d99202018-12-27 13:32:34 +090061func (m *apexKey) installable() bool {
62 return m.properties.Installable == nil || proptools.Bool(m.properties.Installable)
63}
64
Jiyong Parkff1458f2018-10-12 21:49:38 +090065func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park9335a262018-12-24 11:31:58 +090066 if ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild() {
67 // Flattened APEXes are not signed
68 return
69 }
70
71 m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
72 m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
73
74 // If not found, fall back to the local key pairs
75 if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
76 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
77 }
78 if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
79 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
80 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090081
82 pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
83 privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
84
85 if pubKeyName != privKeyName {
86 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
87 m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName)
88 return
89 }
90 m.keyName = pubKeyName
91
Jiyong Park50d99202018-12-27 13:32:34 +090092 if m.installable() {
93 ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file)
94 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090095}
96
97func (m *apexKey) AndroidMk() android.AndroidMkData {
98 return android.AndroidMkData{
99 Class: "ETC",
100 OutputFile: android.OptionalPathForPath(m.public_key_file),
101 Extra: []android.AndroidMkExtraFunc{
102 func(w io.Writer, outputFile android.Path) {
103 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex")
104 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName)
Jiyong Park50d99202018-12-27 13:32:34 +0900105 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable())
Jiyong Parkff1458f2018-10-12 21:49:38 +0900106 },
107 },
108 }
109}