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 | |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 105 | // ////////////////////////////////////////////////////////////////////// |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 106 | // apex_keys_text |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 107 | type apexKeysText struct { |
| 108 | output android.OutputPath |
| 109 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 110 | |
| 111 | func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) { |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 112 | s.output = android.PathForOutput(ctx, "apexkeys.txt") |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 113 | type apexKeyEntry struct { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 114 | name string |
| 115 | presigned bool |
| 116 | publicKey string |
| 117 | privateKey string |
| 118 | containerCertificate string |
| 119 | containerPrivateKey string |
| 120 | partition string |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 121 | signTool string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 122 | } |
| 123 | toString := func(e apexKeyEntry) string { |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 124 | signTool := "" |
| 125 | if e.signTool != "" { |
| 126 | signTool = fmt.Sprintf(" sign_tool=%q", e.signTool) |
| 127 | } |
| 128 | format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n" |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 129 | if e.presigned { |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 130 | return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 131 | } else { |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 132 | return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | apexKeyMap := make(map[string]apexKeyEntry) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 137 | ctx.VisitAllModules(func(module android.Module) { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 138 | if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 139 | pem, key := m.getCertificateAndPrivateKey(ctx) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 140 | apexKeyMap[m.Name()] = apexKeyEntry{ |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 141 | 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()), |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 148 | signTool: proptools.String(m.properties.Custom_sign_tool), |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 149 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 150 | } |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 151 | }) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 152 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 153 | // Find prebuilts and let them override apexBundle if they are preferred |
| 154 | ctx.VisitAllModules(func(module android.Module) { |
| 155 | if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() && |
| 156 | m.Prebuilt().UsePrebuilt() { |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 157 | apexKeyMap[m.BaseModuleName()] = apexKeyEntry{ |
| 158 | name: m.InstallFilename(), |
| 159 | presigned: true, |
| 160 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 161 | } |
| 162 | } |
| 163 | }) |
| 164 | |
| 165 | // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass |
| 166 | // so that apex_set are not overridden by prebuilts. |
| 167 | ctx.VisitAllModules(func(module android.Module) { |
| 168 | if m, ok := module.(*ApexSet); ok && m.Enabled() { |
| 169 | entry := apexKeyEntry{ |
| 170 | name: m.InstallFilename(), |
| 171 | presigned: true, |
| 172 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 173 | } |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 174 | apexKeyMap[m.BaseModuleName()] = entry |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 175 | } |
| 176 | }) |
| 177 | |
| 178 | // iterating over map does not give consistent ordering in golang |
| 179 | var moduleNames []string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 180 | for key, _ := range apexKeyMap { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 181 | moduleNames = append(moduleNames, key) |
| 182 | } |
| 183 | sort.Strings(moduleNames) |
| 184 | |
| 185 | var filecontent strings.Builder |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 186 | for _, name := range moduleNames { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 187 | filecontent.WriteString(toString(apexKeyMap[name])) |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 188 | } |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 189 | android.WriteFileRule(ctx, s.output, filecontent.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 192 | func apexKeysTextFactory() android.Singleton { |
| 193 | return &apexKeysText{} |
| 194 | } |
| 195 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 196 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 197 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 198 | } |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 199 | |
| 200 | // For Bazel / bp2build |
| 201 | |
| 202 | type bazelApexKeyAttributes struct { |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 203 | Public_key bazel.LabelAttribute |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 204 | Public_key_name bazel.StringAttribute |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 205 | |
| 206 | Private_key bazel.LabelAttribute |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 207 | Private_key_name bazel.StringAttribute |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 210 | // ConvertWithBp2build performs conversion apexKey for bp2build |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 211 | func (m *apexKey) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 212 | apexKeyBp2BuildInternal(ctx, m) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 215 | func apexKeyBp2BuildInternal(ctx android.Bp2buildMutatorContext, module *apexKey) { |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 216 | privateKeyLabelAttribute, privateKeyNameAttribute := |
| 217 | android.BazelStringOrLabelFromProp(ctx, module.properties.Private_key) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 218 | |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 219 | publicKeyLabelAttribute, publicKeyNameAttribute := |
| 220 | android.BazelStringOrLabelFromProp(ctx, module.properties.Public_key) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 221 | |
| 222 | attrs := &bazelApexKeyAttributes{ |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 223 | Private_key: privateKeyLabelAttribute, |
| 224 | Private_key_name: privateKeyNameAttribute, |
| 225 | |
| 226 | Public_key: publicKeyLabelAttribute, |
| 227 | Public_key_name: publicKeyNameAttribute, |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | props := bazel.BazelTargetModuleProperties{ |
| 231 | Rule_class: "apex_key", |
Cole Faust | 5f90da3 | 2022-04-29 13:37:43 -0700 | [diff] [blame] | 232 | Bzl_load_location: "//build/bazel/rules/apex:apex_key.bzl", |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 235 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 236 | } |