blob: 829410ed2e63b7452573efbe36a0056429d29a82 [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)
Jiyong Parkff1458f2018-10-12 21:49:38 +090037}
38
39type apexKey struct {
40 android.ModuleBase
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040041 android.BazelModuleBase
Jiyong Parkff1458f2018-10-12 21:49:38 +090042
43 properties apexKeyProperties
44
Jaewoong Jung18aefc12020-12-21 09:11:10 -080045 publicKeyFile android.Path
46 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090047
48 keyName string
49}
50
51type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090052 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090053 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090054 Public_key *string `android:"path"`
55 // Path or module to the private key file in pem format. Used to sign APEXs.
56 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090057
58 // Whether this key is installable to one of the partitions. Defualt: true.
59 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090060}
61
Jiyong Parkd1063c12019-07-17 20:08:41 +090062func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090063 module := &apexKey{}
64 module.AddProperties(&module.properties)
dimitryf8071672019-04-11 17:27:11 +020065 android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040066 android.InitBazelModule(module)
Jiyong Parkff1458f2018-10-12 21:49:38 +090067 return module
68}
69
Jiyong Park50d99202018-12-27 13:32:34 +090070func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090071 return false
Jiyong Park50d99202018-12-27 13:32:34 +090072}
73
Jiyong Parkff1458f2018-10-12 21:49:38 +090074func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090075 // If the keys are from other modules (i.e. :module syntax) respect it.
76 // Otherwise, try to locate the key files in the default cert dir or
77 // in the local module dir
78 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080079 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090080 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080081 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090082 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080083 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
84 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090085 }
Jiyong Park9335a262018-12-24 11:31:58 +090086 }
Jiyong Park67882562019-03-21 01:11:21 +090087
88 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080089 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090090 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080091 m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
92 if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
93 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090094 }
Jiyong Park9335a262018-12-24 11:31:58 +090095 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090096
Jaewoong Jung18aefc12020-12-21 09:11:10 -080097 pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
98 privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
Jiyong Parkff1458f2018-10-12 21:49:38 +090099
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700100 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +0900101 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 -0800102 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900103 return
104 }
105 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +0900106}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900107
108////////////////////////////////////////////////////////////////////////
109// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900110type apexKeysText struct {
111 output android.OutputPath
112}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900113
114func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900115 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900116 type apexKeyEntry struct {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800117 name string
118 presigned bool
119 publicKey string
120 privateKey string
121 containerCertificate string
122 containerPrivateKey string
123 partition string
Jooyung Han09c11ad2021-10-27 03:45:31 +0900124 signTool string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900125 }
126 toString := func(e apexKeyEntry) string {
Jooyung Han09c11ad2021-10-27 03:45:31 +0900127 signTool := ""
128 if e.signTool != "" {
129 signTool = fmt.Sprintf(" sign_tool=%q", e.signTool)
130 }
131 format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n"
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900132 if e.presigned {
Jooyung Han09c11ad2021-10-27 03:45:31 +0900133 return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900134 } else {
Jooyung Han09c11ad2021-10-27 03:45:31 +0900135 return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900136 }
137 }
138
139 apexKeyMap := make(map[string]apexKeyEntry)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900140 ctx.VisitAllModules(func(module android.Module) {
Jiyong Park4d277042019-04-23 18:00:10 +0900141 if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900142 pem, key := m.getCertificateAndPrivateKey(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900143 apexKeyMap[m.Name()] = apexKeyEntry{
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800144 name: m.Name() + ".apex",
145 presigned: false,
146 publicKey: m.publicKeyFile.String(),
147 privateKey: m.privateKeyFile.String(),
148 containerCertificate: pem.String(),
149 containerPrivateKey: key.String(),
150 partition: m.PartitionTag(ctx.DeviceConfig()),
Jooyung Han09c11ad2021-10-27 03:45:31 +0900151 signTool: proptools.String(m.properties.Custom_sign_tool),
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900152 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900153 }
Jiyong Park4d277042019-04-23 18:00:10 +0900154 })
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900155
Jiyong Park4d277042019-04-23 18:00:10 +0900156 // Find prebuilts and let them override apexBundle if they are preferred
157 ctx.VisitAllModules(func(module android.Module) {
158 if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
159 m.Prebuilt().UsePrebuilt() {
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900160 apexKeyMap[m.BaseModuleName()] = apexKeyEntry{
161 name: m.InstallFilename(),
162 presigned: true,
163 partition: m.PartitionTag(ctx.DeviceConfig()),
164 }
165 }
166 })
167
168 // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass
169 // so that apex_set are not overridden by prebuilts.
170 ctx.VisitAllModules(func(module android.Module) {
171 if m, ok := module.(*ApexSet); ok && m.Enabled() {
172 entry := apexKeyEntry{
173 name: m.InstallFilename(),
174 presigned: true,
175 partition: m.PartitionTag(ctx.DeviceConfig()),
176 }
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900177 apexKeyMap[m.BaseModuleName()] = entry
Jiyong Park4d277042019-04-23 18:00:10 +0900178 }
179 })
180
181 // iterating over map does not give consistent ordering in golang
182 var moduleNames []string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900183 for key, _ := range apexKeyMap {
Jiyong Park4d277042019-04-23 18:00:10 +0900184 moduleNames = append(moduleNames, key)
185 }
186 sort.Strings(moduleNames)
187
188 var filecontent strings.Builder
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900189 for _, name := range moduleNames {
Colin Crosscf371cc2020-11-13 11:48:42 -0800190 filecontent.WriteString(toString(apexKeyMap[name]))
Jiyong Park4d277042019-04-23 18:00:10 +0900191 }
Colin Crosscf371cc2020-11-13 11:48:42 -0800192 android.WriteFileRule(ctx, s.output, filecontent.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900193}
194
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900195func apexKeysTextFactory() android.Singleton {
196 return &apexKeysText{}
197}
198
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900199func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
200 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900201}
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400202
203// For Bazel / bp2build
204
205type bazelApexKeyAttributes struct {
206 Public_key bazel.LabelAttribute
207 Private_key bazel.LabelAttribute
208}
209
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400210// ConvertWithBp2build performs conversion apexKey for bp2build
211func (m *apexKey) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
212 apexKeyBp2BuildInternal(ctx, m)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400213}
214
215func apexKeyBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexKey) {
216 var privateKeyLabelAttribute bazel.LabelAttribute
217 if module.properties.Private_key != nil {
218 privateKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Private_key))
219 }
220
221 var publicKeyLabelAttribute bazel.LabelAttribute
222 if module.properties.Public_key != nil {
223 publicKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Public_key))
224 }
225
226 attrs := &bazelApexKeyAttributes{
227 Private_key: privateKeyLabelAttribute,
228 Public_key: publicKeyLabelAttribute,
229 }
230
231 props := bazel.BazelTargetModuleProperties{
232 Rule_class: "apex_key",
233 Bzl_load_location: "//build/bazel/rules:apex_key.bzl",
234 }
235
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000236 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400237}