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) |
| 36 | ctx.RegisterSingletonType("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 | keyName string |
| 49 | } |
| 50 | |
| 51 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 52 | // 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] | 53 | // 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] | 54 | 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 Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 57 | |
| 58 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 59 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 62 | func ApexKeyFactory() android.Module { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 63 | module := &apexKey{} |
| 64 | module.AddProperties(&module.properties) |
dimitry | f807167 | 2019-04-11 17:27:11 +0200 | [diff] [blame] | 65 | android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 66 | android.InitBazelModule(module) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 67 | return module |
| 68 | } |
| 69 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 70 | func (m *apexKey) installable() bool { |
Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 71 | return false |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 74 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 75 | // 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 Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 79 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 80 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 81 | 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] | 82 | // If not found, fall back to the local key pairs |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 83 | if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() { |
| 84 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 85 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 86 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 87 | |
| 88 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 89 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 90 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 91 | 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 Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 94 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 95 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 96 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 97 | 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 Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 99 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 100 | 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] | 101 | 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] | 102 | m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 103 | return |
| 104 | } |
| 105 | m.keyName = pubKeyName |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 106 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 107 | |
| 108 | //////////////////////////////////////////////////////////////////////// |
| 109 | // apex_keys_text |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 110 | type apexKeysText struct { |
| 111 | output android.OutputPath |
| 112 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 113 | |
| 114 | func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) { |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 115 | s.output = android.PathForOutput(ctx, "apexkeys.txt") |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 116 | type apexKeyEntry struct { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 117 | name string |
| 118 | presigned bool |
| 119 | publicKey string |
| 120 | privateKey string |
| 121 | containerCertificate string |
| 122 | containerPrivateKey string |
| 123 | partition string |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 124 | signTool string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 125 | } |
| 126 | toString := func(e apexKeyEntry) string { |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 127 | 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 Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 132 | if e.presigned { |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 133 | 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] | 134 | } else { |
Jooyung Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 135 | 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] | 136 | } |
| 137 | } |
| 138 | |
| 139 | apexKeyMap := make(map[string]apexKeyEntry) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 140 | ctx.VisitAllModules(func(module android.Module) { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 141 | if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 142 | pem, key := m.getCertificateAndPrivateKey(ctx) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 143 | apexKeyMap[m.Name()] = apexKeyEntry{ |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 144 | 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 Han | 09c11ad | 2021-10-27 03:45:31 +0900 | [diff] [blame] | 151 | signTool: proptools.String(m.properties.Custom_sign_tool), |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 152 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 153 | } |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 154 | }) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 155 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 156 | // 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 Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 160 | 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 Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 177 | apexKeyMap[m.BaseModuleName()] = entry |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 178 | } |
| 179 | }) |
| 180 | |
| 181 | // iterating over map does not give consistent ordering in golang |
| 182 | var moduleNames []string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 183 | for key, _ := range apexKeyMap { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 184 | moduleNames = append(moduleNames, key) |
| 185 | } |
| 186 | sort.Strings(moduleNames) |
| 187 | |
| 188 | var filecontent strings.Builder |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 189 | for _, name := range moduleNames { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 190 | filecontent.WriteString(toString(apexKeyMap[name])) |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 191 | } |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 192 | android.WriteFileRule(ctx, s.output, filecontent.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 193 | } |
| 194 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 195 | func apexKeysTextFactory() android.Singleton { |
| 196 | return &apexKeysText{} |
| 197 | } |
| 198 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 199 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 200 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 201 | } |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 202 | |
| 203 | // For Bazel / bp2build |
| 204 | |
| 205 | type bazelApexKeyAttributes struct { |
| 206 | Public_key bazel.LabelAttribute |
| 207 | Private_key bazel.LabelAttribute |
| 208 | } |
| 209 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 210 | // ConvertWithBp2build performs conversion apexKey for bp2build |
| 211 | func (m *apexKey) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 212 | apexKeyBp2BuildInternal(ctx, m) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | func 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 Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 236 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 237 | } |