blob: 2405e98429fb390fdc7de26436d220399445cbe1 [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 Parkff1458f2018-10-12 21:49:38 +090019
20 "android/soong/android"
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -040021 "android/soong/bazel"
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() {
Paul Duffin667893c2021-03-09 22:34:13 +000029 registerApexKeyBuildComponents(android.InitRegistrationContext)
30}
31
32func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Jiyong Parkff1458f2018-10-12 21:49:38 +090034}
35
36type apexKey struct {
37 android.ModuleBase
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040038 android.BazelModuleBase
Jiyong Parkff1458f2018-10-12 21:49:38 +090039
40 properties apexKeyProperties
41
Jaewoong Jung18aefc12020-12-21 09:11:10 -080042 publicKeyFile android.Path
43 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090044}
45
46type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090047 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090048 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090049 Public_key *string `android:"path"`
50 // Path or module to the private key file in pem format. Used to sign APEXs.
51 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090052
53 // Whether this key is installable to one of the partitions. Defualt: true.
54 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090055}
56
Jiyong Parkd1063c12019-07-17 20:08:41 +090057func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090058 module := &apexKey{}
59 module.AddProperties(&module.properties)
Jooyung Han8d4a1f02023-08-23 13:54:08 +090060 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040061 android.InitBazelModule(module)
Jiyong Parkff1458f2018-10-12 21:49:38 +090062 return module
63}
64
Jiyong Park50d99202018-12-27 13:32:34 +090065func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090066 return false
Jiyong Park50d99202018-12-27 13:32:34 +090067}
68
Jiyong Parkff1458f2018-10-12 21:49:38 +090069func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090070 // If the keys are from other modules (i.e. :module syntax) respect it.
71 // Otherwise, try to locate the key files in the default cert dir or
72 // in the local module dir
73 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080074 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090075 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080076 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090077 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080078 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
79 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090080 }
Jiyong Park9335a262018-12-24 11:31:58 +090081 }
Jiyong Park67882562019-03-21 01:11:21 +090082
83 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080084 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090085 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080086 m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
87 if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
88 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090089 }
Jiyong Park9335a262018-12-24 11:31:58 +090090 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090091
Jaewoong Jung18aefc12020-12-21 09:11:10 -080092 pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
93 privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
Jiyong Parkff1458f2018-10-12 21:49:38 +090094
Jaewoong Jung939ebd52019-03-26 15:07:36 -070095 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +090096 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 -080097 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +090098 return
99 }
Jiyong Parkff1458f2018-10-12 21:49:38 +0900100}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900101
Jooyung Han2cf35e72023-10-30 11:17:16 +0900102type apexKeyEntry struct {
103 name string
104 presigned bool
105 publicKey string
106 privateKey string
107 containerCertificate string
108 containerPrivateKey string
109 partition string
110 signTool string
111}
112
113func (e apexKeyEntry) String() string {
114 signTool := ""
115 if e.signTool != "" {
116 signTool = fmt.Sprintf(" sign_tool=%q", e.signTool)
117 }
118 format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n"
119 if e.presigned {
120 return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool)
121 } else {
122 return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool)
123 }
124}
125
Jooyung Han286957d2023-10-30 16:17:56 +0900126func apexKeyEntryFor(ctx android.ModuleContext, module android.Module) apexKeyEntry {
Jooyung Han2cf35e72023-10-30 11:17:16 +0900127 switch m := module.(type) {
128 case *apexBundle:
129 pem, key := m.getCertificateAndPrivateKey(ctx)
130 return apexKeyEntry{
131 name: m.Name() + ".apex",
132 presigned: false,
133 publicKey: m.publicKeyFile.String(),
134 privateKey: m.privateKeyFile.String(),
135 containerCertificate: pem.String(),
136 containerPrivateKey: key.String(),
137 partition: m.PartitionTag(ctx.DeviceConfig()),
138 signTool: proptools.String(m.properties.Custom_sign_tool),
139 }
140 case *Prebuilt:
141 return apexKeyEntry{
142 name: m.InstallFilename(),
143 presigned: true,
144 partition: m.PartitionTag(ctx.DeviceConfig()),
145 }
146 case *ApexSet:
147 return apexKeyEntry{
148 name: m.InstallFilename(),
149 presigned: true,
150 partition: m.PartitionTag(ctx.DeviceConfig()),
151 }
152 }
153 panic(fmt.Errorf("unknown type(%t) for apexKeyEntry", module))
154}
155
Jooyung Han286957d2023-10-30 16:17:56 +0900156func writeApexKeys(ctx android.ModuleContext, module android.Module) android.WritablePath {
157 path := android.PathForModuleOut(ctx, "apexkeys.txt")
158 entry := apexKeyEntryFor(ctx, module)
159 android.WriteFileRuleVerbatim(ctx, path, entry.String())
160 return path
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900161}
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400162
163// For Bazel / bp2build
164
165type bazelApexKeyAttributes struct {
Jingwen Chen1d873332022-10-05 06:15:15 +0000166 Public_key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +0000167 Public_key_name bazel.StringAttribute
Jingwen Chen1d873332022-10-05 06:15:15 +0000168
169 Private_key bazel.LabelAttribute
Jingwen Chen6817bbb2022-10-14 09:56:07 +0000170 Private_key_name bazel.StringAttribute
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400171}
172
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400173// ConvertWithBp2build performs conversion apexKey for bp2build
Chris Parsons637458d2023-09-19 20:09:00 +0000174func (m *apexKey) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400175 apexKeyBp2BuildInternal(ctx, m)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400176}
177
Chris Parsons637458d2023-09-19 20:09:00 +0000178func apexKeyBp2BuildInternal(ctx android.Bp2buildMutatorContext, module *apexKey) {
Jingwen Chen6817bbb2022-10-14 09:56:07 +0000179 privateKeyLabelAttribute, privateKeyNameAttribute :=
180 android.BazelStringOrLabelFromProp(ctx, module.properties.Private_key)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400181
Jingwen Chen6817bbb2022-10-14 09:56:07 +0000182 publicKeyLabelAttribute, publicKeyNameAttribute :=
183 android.BazelStringOrLabelFromProp(ctx, module.properties.Public_key)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400184
185 attrs := &bazelApexKeyAttributes{
Jingwen Chen1d873332022-10-05 06:15:15 +0000186 Private_key: privateKeyLabelAttribute,
187 Private_key_name: privateKeyNameAttribute,
188
189 Public_key: publicKeyLabelAttribute,
190 Public_key_name: publicKeyNameAttribute,
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400191 }
192
193 props := bazel.BazelTargetModuleProperties{
194 Rule_class: "apex_key",
Cole Faust5f90da32022-04-29 13:37:43 -0700195 Bzl_load_location: "//build/bazel/rules/apex:apex_key.bzl",
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400196 }
197
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000198 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400199}