Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 19 | "sort" |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 20 | "strings" |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 21 | |
| 22 | "android/soong/android" |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 23 | "android/soong/bazel" |
Colin Cross | 5f692ec | 2019-02-01 16:53:07 -0800 | [diff] [blame] | 24 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
| 26 | ) |
| 27 | |
| 28 | var String = proptools.String |
| 29 | |
| 30 | func init() { |
Paul Duffin | 667893c | 2021-03-09 22:34:13 +0000 | [diff] [blame] | 31 | registerApexKeyBuildComponents(android.InitRegistrationContext) |
| 32 | } |
| 33 | |
| 34 | func registerApexKeyBuildComponents(ctx android.RegistrationContext) { |
| 35 | ctx.RegisterModuleType("apex_key", ApexKeyFactory) |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 36 | ctx.RegisterParallelSingletonType("apex_keys_text", apexKeysTextFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type apexKey struct { |
| 40 | android.ModuleBase |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 41 | android.BazelModuleBase |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 42 | |
| 43 | properties apexKeyProperties |
| 44 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 45 | publicKeyFile android.Path |
| 46 | privateKeyFile android.Path |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 50 | // Path or module to the public key file in avbpubkey format. Installed to the device. |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 51 | // Base name of the file is used as the ID for the key. |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 52 | Public_key *string `android:"path"` |
| 53 | // Path or module to the private key file in pem format. Used to sign APEXs. |
| 54 | Private_key *string `android:"path"` |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 55 | |
| 56 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 57 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 60 | func ApexKeyFactory() android.Module { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 61 | module := &apexKey{} |
| 62 | module.AddProperties(&module.properties) |
Jooyung Han | 8d4a1f0 | 2023-08-23 13:54:08 +0900 | [diff] [blame] | 63 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 64 | android.InitBazelModule(module) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 65 | return module |
| 66 | } |
| 67 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 68 | func (m *apexKey) installable() bool { |
Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 69 | return false |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 72 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 73 | // If the keys are from other modules (i.e. :module syntax) respect it. |
| 74 | // Otherwise, try to locate the key files in the default cert dir or |
| 75 | // in the local module dir |
| 76 | if android.SrcIsModule(String(m.properties.Public_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 77 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 78 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 79 | m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 80 | // If not found, fall back to the local key pairs |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 81 | if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() { |
| 82 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 83 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 84 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 85 | |
| 86 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 87 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 88 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 89 | m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) |
| 90 | if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() { |
| 91 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 92 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 93 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 94 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 95 | pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())] |
| 96 | privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())] |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 97 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 98 | if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 99 | ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname", |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 100 | m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 101 | return |
| 102 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 103 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 104 | |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame^] | 105 | type apexKeyEntry struct { |
| 106 | name string |
| 107 | presigned bool |
| 108 | publicKey string |
| 109 | privateKey string |
| 110 | containerCertificate string |
| 111 | containerPrivateKey string |
| 112 | partition string |
| 113 | signTool string |
| 114 | } |
| 115 | |
| 116 | func (e apexKeyEntry) String() string { |
| 117 | signTool := "" |
| 118 | if e.signTool != "" { |
| 119 | signTool = fmt.Sprintf(" sign_tool=%q", e.signTool) |
| 120 | } |
| 121 | format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n" |
| 122 | if e.presigned { |
| 123 | return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool) |
| 124 | } else { |
| 125 | return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func apexKeyEntryFor(ctx android.SingletonContext, module android.Module) apexKeyEntry { |
| 130 | switch m := module.(type) { |
| 131 | case *apexBundle: |
| 132 | pem, key := m.getCertificateAndPrivateKey(ctx) |
| 133 | return apexKeyEntry{ |
| 134 | name: m.Name() + ".apex", |
| 135 | presigned: false, |
| 136 | publicKey: m.publicKeyFile.String(), |
| 137 | privateKey: m.privateKeyFile.String(), |
| 138 | containerCertificate: pem.String(), |
| 139 | containerPrivateKey: key.String(), |
| 140 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 141 | signTool: proptools.String(m.properties.Custom_sign_tool), |
| 142 | } |
| 143 | case *Prebuilt: |
| 144 | return apexKeyEntry{ |
| 145 | name: m.InstallFilename(), |
| 146 | presigned: true, |
| 147 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 148 | } |
| 149 | case *ApexSet: |
| 150 | return apexKeyEntry{ |
| 151 | name: m.InstallFilename(), |
| 152 | presigned: true, |
| 153 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 154 | } |
| 155 | } |
| 156 | panic(fmt.Errorf("unknown type(%t) for apexKeyEntry", module)) |
| 157 | } |
| 158 | |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 159 | // ////////////////////////////////////////////////////////////////////// |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 160 | // apex_keys_text |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 161 | type apexKeysText struct { |
| 162 | output android.OutputPath |
| 163 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 164 | |
| 165 | func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) { |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 166 | s.output = android.PathForOutput(ctx, "apexkeys.txt") |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 167 | |
| 168 | apexKeyMap := make(map[string]apexKeyEntry) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 169 | ctx.VisitAllModules(func(module android.Module) { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 170 | if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() { |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame^] | 171 | apexKeyMap[m.Name()] = apexKeyEntryFor(ctx, m) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 172 | } |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 173 | }) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 174 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 175 | // Find prebuilts and let them override apexBundle if they are preferred |
| 176 | ctx.VisitAllModules(func(module android.Module) { |
| 177 | if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() && |
| 178 | m.Prebuilt().UsePrebuilt() { |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame^] | 179 | apexKeyMap[m.BaseModuleName()] = apexKeyEntryFor(ctx, m) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 180 | } |
| 181 | }) |
| 182 | |
| 183 | // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass |
| 184 | // so that apex_set are not overridden by prebuilts. |
| 185 | ctx.VisitAllModules(func(module android.Module) { |
| 186 | if m, ok := module.(*ApexSet); ok && m.Enabled() { |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame^] | 187 | apexKeyMap[m.BaseModuleName()] = apexKeyEntryFor(ctx, m) |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 188 | } |
| 189 | }) |
| 190 | |
| 191 | // iterating over map does not give consistent ordering in golang |
| 192 | var moduleNames []string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 193 | for key, _ := range apexKeyMap { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 194 | moduleNames = append(moduleNames, key) |
| 195 | } |
| 196 | sort.Strings(moduleNames) |
| 197 | |
| 198 | var filecontent strings.Builder |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 199 | for _, name := range moduleNames { |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame^] | 200 | filecontent.WriteString(apexKeyMap[name].String()) |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 201 | } |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 202 | android.WriteFileRule(ctx, s.output, filecontent.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 203 | } |
| 204 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 205 | func apexKeysTextFactory() android.Singleton { |
| 206 | return &apexKeysText{} |
| 207 | } |
| 208 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 209 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 210 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 211 | } |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 212 | |
| 213 | // For Bazel / bp2build |
| 214 | |
| 215 | type bazelApexKeyAttributes struct { |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 216 | Public_key bazel.LabelAttribute |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 217 | Public_key_name bazel.StringAttribute |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 218 | |
| 219 | Private_key bazel.LabelAttribute |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 220 | Private_key_name bazel.StringAttribute |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 223 | // ConvertWithBp2build performs conversion apexKey for bp2build |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 224 | func (m *apexKey) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 225 | apexKeyBp2BuildInternal(ctx, m) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 228 | func apexKeyBp2BuildInternal(ctx android.Bp2buildMutatorContext, module *apexKey) { |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 229 | privateKeyLabelAttribute, privateKeyNameAttribute := |
| 230 | android.BazelStringOrLabelFromProp(ctx, module.properties.Private_key) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 231 | |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 232 | publicKeyLabelAttribute, publicKeyNameAttribute := |
| 233 | android.BazelStringOrLabelFromProp(ctx, module.properties.Public_key) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 234 | |
| 235 | attrs := &bazelApexKeyAttributes{ |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 236 | Private_key: privateKeyLabelAttribute, |
| 237 | Private_key_name: privateKeyNameAttribute, |
| 238 | |
| 239 | Public_key: publicKeyLabelAttribute, |
| 240 | Public_key_name: publicKeyNameAttribute, |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | props := bazel.BazelTargetModuleProperties{ |
| 244 | Rule_class: "apex_key", |
Cole Faust | 5f90da3 | 2022-04-29 13:37:43 -0700 | [diff] [blame] | 245 | Bzl_load_location: "//build/bazel/rules/apex:apex_key.bzl", |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 248 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 249 | } |