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" |
Colin Cross | 5f692ec | 2019-02-01 16:53:07 -0800 | [diff] [blame] | 23 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | var String = proptools.String |
| 28 | |
| 29 | func init() { |
Paul Duffin | 667893c | 2021-03-09 22:34:13 +0000 | [diff] [blame] | 30 | registerApexKeyBuildComponents(android.InitRegistrationContext) |
| 31 | } |
| 32 | |
| 33 | func registerApexKeyBuildComponents(ctx android.RegistrationContext) { |
| 34 | ctx.RegisterModuleType("apex_key", ApexKeyFactory) |
| 35 | ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type apexKey struct { |
| 39 | android.ModuleBase |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame^] | 40 | android.BazelModuleBase |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 41 | |
| 42 | properties apexKeyProperties |
| 43 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 44 | publicKeyFile android.Path |
| 45 | privateKeyFile android.Path |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 46 | |
| 47 | keyName string |
| 48 | } |
| 49 | |
| 50 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 51 | // 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] | 52 | // 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] | 53 | Public_key *string `android:"path"` |
| 54 | // Path or module to the private key file in pem format. Used to sign APEXs. |
| 55 | Private_key *string `android:"path"` |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 56 | |
| 57 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 58 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 61 | func ApexKeyFactory() android.Module { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 62 | module := &apexKey{} |
| 63 | module.AddProperties(&module.properties) |
dimitry | f807167 | 2019-04-11 17:27:11 +0200 | [diff] [blame] | 64 | android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame^] | 65 | android.InitBazelModule(module) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 66 | return module |
| 67 | } |
| 68 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 69 | func (m *apexKey) installable() bool { |
Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 70 | return false |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 73 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 74 | // If the keys are from other modules (i.e. :module syntax) respect it. |
| 75 | // Otherwise, try to locate the key files in the default cert dir or |
| 76 | // in the local module dir |
| 77 | if android.SrcIsModule(String(m.properties.Public_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 78 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 79 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 80 | 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] | 81 | // If not found, fall back to the local key pairs |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 82 | if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() { |
| 83 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 84 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 85 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 86 | |
| 87 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 88 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 89 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 90 | m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) |
| 91 | if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() { |
| 92 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 93 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 94 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 95 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 96 | pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())] |
| 97 | 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] | 98 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 99 | 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] | 100 | 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] | 101 | m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 102 | return |
| 103 | } |
| 104 | m.keyName = pubKeyName |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 105 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 106 | |
| 107 | //////////////////////////////////////////////////////////////////////// |
| 108 | // apex_keys_text |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 109 | type apexKeysText struct { |
| 110 | output android.OutputPath |
| 111 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 112 | |
| 113 | func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) { |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 114 | s.output = android.PathForOutput(ctx, "apexkeys.txt") |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 115 | type apexKeyEntry struct { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 116 | name string |
| 117 | presigned bool |
| 118 | publicKey string |
| 119 | privateKey string |
| 120 | containerCertificate string |
| 121 | containerPrivateKey string |
| 122 | partition string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 123 | } |
| 124 | toString := func(e apexKeyEntry) string { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 125 | format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q\n" |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 126 | if e.presigned { |
| 127 | return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition) |
| 128 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 129 | return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | apexKeyMap := make(map[string]apexKeyEntry) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 134 | ctx.VisitAllModules(func(module android.Module) { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 135 | if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() { |
Jiyong Park | b81b990 | 2020-11-24 19:51:18 +0900 | [diff] [blame] | 136 | pem, key := m.getCertificateAndPrivateKey(ctx) |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 137 | apexKeyMap[m.Name()] = apexKeyEntry{ |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 138 | name: m.Name() + ".apex", |
| 139 | presigned: false, |
| 140 | publicKey: m.publicKeyFile.String(), |
| 141 | privateKey: m.privateKeyFile.String(), |
| 142 | containerCertificate: pem.String(), |
| 143 | containerPrivateKey: key.String(), |
| 144 | partition: m.PartitionTag(ctx.DeviceConfig()), |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 145 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 146 | } |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 147 | }) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 148 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 149 | // Find prebuilts and let them override apexBundle if they are preferred |
| 150 | ctx.VisitAllModules(func(module android.Module) { |
| 151 | if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() && |
| 152 | m.Prebuilt().UsePrebuilt() { |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 153 | apexKeyMap[m.BaseModuleName()] = apexKeyEntry{ |
| 154 | name: m.InstallFilename(), |
| 155 | presigned: true, |
| 156 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 157 | } |
| 158 | } |
| 159 | }) |
| 160 | |
| 161 | // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass |
| 162 | // so that apex_set are not overridden by prebuilts. |
| 163 | ctx.VisitAllModules(func(module android.Module) { |
| 164 | if m, ok := module.(*ApexSet); ok && m.Enabled() { |
| 165 | entry := apexKeyEntry{ |
| 166 | name: m.InstallFilename(), |
| 167 | presigned: true, |
| 168 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 169 | } |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 170 | apexKeyMap[m.BaseModuleName()] = entry |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 171 | } |
| 172 | }) |
| 173 | |
| 174 | // iterating over map does not give consistent ordering in golang |
| 175 | var moduleNames []string |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 176 | for key, _ := range apexKeyMap { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 177 | moduleNames = append(moduleNames, key) |
| 178 | } |
| 179 | sort.Strings(moduleNames) |
| 180 | |
| 181 | var filecontent strings.Builder |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 182 | for _, name := range moduleNames { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 183 | filecontent.WriteString(toString(apexKeyMap[name])) |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 184 | } |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 185 | android.WriteFileRule(ctx, s.output, filecontent.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 186 | } |
| 187 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 188 | func apexKeysTextFactory() android.Singleton { |
| 189 | return &apexKeysText{} |
| 190 | } |
| 191 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 192 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 193 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 194 | } |