blob: 32a7ce1773f10028a3b83fc74f63f75e5be06e39 [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"
Jiyong Park4d277042019-04-23 18:00:10 +090019 "sort"
Jiyong Park0ca3ce82019-02-18 15:25:04 +090020 "strings"
Jiyong Parkff1458f2018-10-12 21:49:38 +090021
22 "android/soong/android"
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -040023 "android/soong/bazel"
Colin Cross5f692ec2019-02-01 16:53:07 -080024
Jiyong Parkff1458f2018-10-12 21:49:38 +090025 "github.com/google/blueprint/proptools"
26)
27
28var String = proptools.String
29
30func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000031 registerApexKeyBuildComponents(android.InitRegistrationContext)
32}
33
34func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
36 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -040037
38 android.RegisterBp2BuildMutator("apex_key", ApexKeyBp2Build)
Jiyong Parkff1458f2018-10-12 21:49:38 +090039}
40
41type apexKey struct {
42 android.ModuleBase
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040043 android.BazelModuleBase
Jiyong Parkff1458f2018-10-12 21:49:38 +090044
45 properties apexKeyProperties
46
Jaewoong Jung18aefc12020-12-21 09:11:10 -080047 publicKeyFile android.Path
48 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090049
50 keyName string
51}
52
53type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090054 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090055 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090056 Public_key *string `android:"path"`
57 // Path or module to the private key file in pem format. Used to sign APEXs.
58 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090059
60 // Whether this key is installable to one of the partitions. Defualt: true.
61 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090062}
63
Jiyong Parkd1063c12019-07-17 20:08:41 +090064func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090065 module := &apexKey{}
66 module.AddProperties(&module.properties)
dimitryf8071672019-04-11 17:27:11 +020067 android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040068 android.InitBazelModule(module)
Jiyong Parkff1458f2018-10-12 21:49:38 +090069 return module
70}
71
Jiyong Park50d99202018-12-27 13:32:34 +090072func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090073 return false
Jiyong Park50d99202018-12-27 13:32:34 +090074}
75
Jiyong Parkff1458f2018-10-12 21:49:38 +090076func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090077 // If the keys are from other modules (i.e. :module syntax) respect it.
78 // Otherwise, try to locate the key files in the default cert dir or
79 // in the local module dir
80 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080081 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090082 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080083 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090084 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080085 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
86 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090087 }
Jiyong Park9335a262018-12-24 11:31:58 +090088 }
Jiyong Park67882562019-03-21 01:11:21 +090089
90 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080091 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090092 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080093 m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
94 if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
95 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090096 }
Jiyong Park9335a262018-12-24 11:31:58 +090097 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090098
Jaewoong Jung18aefc12020-12-21 09:11:10 -080099 pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
100 privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
Jiyong Parkff1458f2018-10-12 21:49:38 +0900101
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700102 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +0900103 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800104 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900105 return
106 }
107 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +0900108}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900109
110////////////////////////////////////////////////////////////////////////
111// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900112type apexKeysText struct {
113 output android.OutputPath
114}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900115
116func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900117 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900118 type apexKeyEntry struct {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800119 name string
120 presigned bool
121 publicKey string
122 privateKey string
123 containerCertificate string
124 containerPrivateKey string
125 partition string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900126 }
127 toString := func(e apexKeyEntry) string {
Colin Crosscf371cc2020-11-13 11:48:42 -0800128 format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q\n"
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900129 if e.presigned {
130 return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition)
131 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800132 return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900133 }
134 }
135
136 apexKeyMap := make(map[string]apexKeyEntry)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900137 ctx.VisitAllModules(func(module android.Module) {
Jiyong Park4d277042019-04-23 18:00:10 +0900138 if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900139 pem, key := m.getCertificateAndPrivateKey(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900140 apexKeyMap[m.Name()] = apexKeyEntry{
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800141 name: m.Name() + ".apex",
142 presigned: false,
143 publicKey: m.publicKeyFile.String(),
144 privateKey: m.privateKeyFile.String(),
145 containerCertificate: pem.String(),
146 containerPrivateKey: key.String(),
147 partition: m.PartitionTag(ctx.DeviceConfig()),
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900148 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900149 }
Jiyong Park4d277042019-04-23 18:00:10 +0900150 })
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900151
Jiyong Park4d277042019-04-23 18:00:10 +0900152 // Find prebuilts and let them override apexBundle if they are preferred
153 ctx.VisitAllModules(func(module android.Module) {
154 if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
155 m.Prebuilt().UsePrebuilt() {
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900156 apexKeyMap[m.BaseModuleName()] = apexKeyEntry{
157 name: m.InstallFilename(),
158 presigned: true,
159 partition: m.PartitionTag(ctx.DeviceConfig()),
160 }
161 }
162 })
163
164 // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass
165 // so that apex_set are not overridden by prebuilts.
166 ctx.VisitAllModules(func(module android.Module) {
167 if m, ok := module.(*ApexSet); ok && m.Enabled() {
168 entry := apexKeyEntry{
169 name: m.InstallFilename(),
170 presigned: true,
171 partition: m.PartitionTag(ctx.DeviceConfig()),
172 }
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900173 apexKeyMap[m.BaseModuleName()] = entry
Jiyong Park4d277042019-04-23 18:00:10 +0900174 }
175 })
176
177 // iterating over map does not give consistent ordering in golang
178 var moduleNames []string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900179 for key, _ := range apexKeyMap {
Jiyong Park4d277042019-04-23 18:00:10 +0900180 moduleNames = append(moduleNames, key)
181 }
182 sort.Strings(moduleNames)
183
184 var filecontent strings.Builder
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900185 for _, name := range moduleNames {
Colin Crosscf371cc2020-11-13 11:48:42 -0800186 filecontent.WriteString(toString(apexKeyMap[name]))
Jiyong Park4d277042019-04-23 18:00:10 +0900187 }
Colin Crosscf371cc2020-11-13 11:48:42 -0800188 android.WriteFileRule(ctx, s.output, filecontent.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900189}
190
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900191func apexKeysTextFactory() android.Singleton {
192 return &apexKeysText{}
193}
194
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900195func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
196 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900197}
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400198
199// For Bazel / bp2build
200
201type bazelApexKeyAttributes struct {
202 Public_key bazel.LabelAttribute
203 Private_key bazel.LabelAttribute
204}
205
206type bazelApexKey struct {
207 android.BazelTargetModuleBase
208 bazelApexKeyAttributes
209}
210
211func BazelApexKeyFactory() android.Module {
212 module := &bazelApexKey{}
213 module.AddProperties(&module.bazelApexKeyAttributes)
214 android.InitBazelTargetModule(module)
215 return module
216}
217
218func ApexKeyBp2Build(ctx android.TopDownMutatorContext) {
219 module, ok := ctx.Module().(*apexKey)
220 if !ok {
221 // Not an APEX key
222 return
223 }
224 if !module.ConvertWithBp2build(ctx) {
225 return
226 }
227 if ctx.ModuleType() != "apex_key" {
228 return
229 }
230
231 apexKeyBp2BuildInternal(ctx, module)
232}
233
234func apexKeyBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexKey) {
235 var privateKeyLabelAttribute bazel.LabelAttribute
236 if module.properties.Private_key != nil {
237 privateKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Private_key))
238 }
239
240 var publicKeyLabelAttribute bazel.LabelAttribute
241 if module.properties.Public_key != nil {
242 publicKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Public_key))
243 }
244
245 attrs := &bazelApexKeyAttributes{
246 Private_key: privateKeyLabelAttribute,
247 Public_key: publicKeyLabelAttribute,
248 }
249
250 props := bazel.BazelTargetModuleProperties{
251 Rule_class: "apex_key",
252 Bzl_load_location: "//build/bazel/rules:apex_key.bzl",
253 }
254
255 ctx.CreateBazelTargetModule(BazelApexKeyFactory, module.Name(), props, attrs)
256}
257
258func (m *bazelApexKey) Name() string {
259 return m.BaseModuleName()
260}
261
262func (m *bazelApexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {}