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 | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 21 | "android/soong/bazel" |
Colin Cross | 5f692ec | 2019-02-01 16:53:07 -0800 | [diff] [blame] | 22 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | var String = proptools.String |
| 27 | |
| 28 | func init() { |
Paul Duffin | 667893c | 2021-03-09 22:34:13 +0000 | [diff] [blame] | 29 | registerApexKeyBuildComponents(android.InitRegistrationContext) |
| 30 | } |
| 31 | |
| 32 | func registerApexKeyBuildComponents(ctx android.RegistrationContext) { |
| 33 | ctx.RegisterModuleType("apex_key", ApexKeyFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type apexKey struct { |
| 37 | android.ModuleBase |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 38 | android.BazelModuleBase |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 39 | |
| 40 | properties apexKeyProperties |
| 41 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 42 | publicKeyFile android.Path |
| 43 | privateKeyFile android.Path |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 47 | // 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] | 48 | // 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] | 49 | 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 Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 52 | |
| 53 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 54 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 55 | } |
| 56 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 57 | func ApexKeyFactory() android.Module { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 58 | module := &apexKey{} |
| 59 | module.AddProperties(&module.properties) |
Jooyung Han | 8d4a1f0 | 2023-08-23 13:54:08 +0900 | [diff] [blame] | 60 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 61 | android.InitBazelModule(module) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 62 | return module |
| 63 | } |
| 64 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 65 | func (m *apexKey) installable() bool { |
Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 66 | return false |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 69 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 70 | // 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 Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 74 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 75 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 76 | 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] | 77 | // If not found, fall back to the local key pairs |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 78 | if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() { |
| 79 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 80 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 81 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 82 | |
| 83 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 84 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 85 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 86 | 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 Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 89 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 90 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 91 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 92 | 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 Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 94 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 95 | 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] | 96 | 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] | 97 | m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 98 | return |
| 99 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 100 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 101 | |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame] | 102 | type 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 | |
| 113 | func (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 Han | 286957d | 2023-10-30 16:17:56 +0900 | [diff] [blame] | 126 | func apexKeyEntryFor(ctx android.ModuleContext, module android.Module) apexKeyEntry { |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame] | 127 | 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 Han | 286957d | 2023-10-30 16:17:56 +0900 | [diff] [blame] | 156 | func 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 Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 161 | } |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 162 | |
| 163 | // For Bazel / bp2build |
| 164 | |
| 165 | type bazelApexKeyAttributes struct { |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 166 | Public_key bazel.LabelAttribute |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 167 | Public_key_name bazel.StringAttribute |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 168 | |
| 169 | Private_key bazel.LabelAttribute |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 170 | Private_key_name bazel.StringAttribute |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 173 | // ConvertWithBp2build performs conversion apexKey for bp2build |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 174 | func (m *apexKey) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 175 | apexKeyBp2BuildInternal(ctx, m) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 178 | func apexKeyBp2BuildInternal(ctx android.Bp2buildMutatorContext, module *apexKey) { |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 179 | privateKeyLabelAttribute, privateKeyNameAttribute := |
| 180 | android.BazelStringOrLabelFromProp(ctx, module.properties.Private_key) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 181 | |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 182 | publicKeyLabelAttribute, publicKeyNameAttribute := |
| 183 | android.BazelStringOrLabelFromProp(ctx, module.properties.Public_key) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 184 | |
| 185 | attrs := &bazelApexKeyAttributes{ |
Jingwen Chen | 1d87333 | 2022-10-05 06:15:15 +0000 | [diff] [blame] | 186 | Private_key: privateKeyLabelAttribute, |
| 187 | Private_key_name: privateKeyNameAttribute, |
| 188 | |
| 189 | Public_key: publicKeyLabelAttribute, |
| 190 | Public_key_name: publicKeyNameAttribute, |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | props := bazel.BazelTargetModuleProperties{ |
| 194 | Rule_class: "apex_key", |
Cole Faust | 5f90da3 | 2022-04-29 13:37:43 -0700 | [diff] [blame] | 195 | Bzl_load_location: "//build/bazel/rules/apex:apex_key.bzl", |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 198 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) |
Rupert Shuttleworth | eb8c85a | 2021-07-27 07:10:32 -0400 | [diff] [blame] | 199 | } |