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" |
Spandan Das | 7701d5f | 2024-12-17 17:52:26 +0000 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | ) |
| 24 | |
| 25 | var String = proptools.String |
| 26 | |
| 27 | func init() { |
Paul Duffin | 667893c | 2021-03-09 22:34:13 +0000 | [diff] [blame] | 28 | registerApexKeyBuildComponents(android.InitRegistrationContext) |
| 29 | } |
| 30 | |
| 31 | func registerApexKeyBuildComponents(ctx android.RegistrationContext) { |
| 32 | ctx.RegisterModuleType("apex_key", ApexKeyFactory) |
Spandan Das | 7701d5f | 2024-12-17 17:52:26 +0000 | [diff] [blame] | 33 | ctx.RegisterParallelSingletonModuleType("all_apex_certs", allApexCertsFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type apexKey struct { |
| 37 | android.ModuleBase |
| 38 | |
| 39 | properties apexKeyProperties |
| 40 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 41 | publicKeyFile android.Path |
| 42 | privateKeyFile android.Path |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 46 | // 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] | 47 | // 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] | 48 | Public_key *string `android:"path"` |
| 49 | // Path or module to the private key file in pem format. Used to sign APEXs. |
| 50 | Private_key *string `android:"path"` |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 51 | |
| 52 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 53 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 56 | func ApexKeyFactory() android.Module { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 57 | module := &apexKey{} |
| 58 | module.AddProperties(&module.properties) |
Jooyung Han | 8d4a1f0 | 2023-08-23 13:54:08 +0900 | [diff] [blame] | 59 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 60 | return module |
| 61 | } |
| 62 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 63 | func (m *apexKey) installable() bool { |
Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 64 | return false |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 67 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 68 | // If the keys are from other modules (i.e. :module syntax) respect it. |
| 69 | // Otherwise, try to locate the key files in the default cert dir or |
| 70 | // in the local module dir |
| 71 | if android.SrcIsModule(String(m.properties.Public_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 72 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 73 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 74 | 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] | 75 | // If not found, fall back to the local key pairs |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 76 | if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() { |
| 77 | m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 78 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 79 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 80 | |
| 81 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 82 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 83 | } else { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 84 | m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) |
| 85 | if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() { |
| 86 | m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 87 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 88 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 89 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 90 | pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())] |
| 91 | 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] | 92 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 93 | 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] | 94 | 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] | 95 | m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 96 | return |
| 97 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 98 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 99 | |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame] | 100 | type apexKeyEntry struct { |
| 101 | name string |
| 102 | presigned bool |
| 103 | publicKey string |
| 104 | privateKey string |
| 105 | containerCertificate string |
| 106 | containerPrivateKey string |
| 107 | partition string |
| 108 | signTool string |
| 109 | } |
| 110 | |
| 111 | func (e apexKeyEntry) String() string { |
| 112 | signTool := "" |
| 113 | if e.signTool != "" { |
| 114 | signTool = fmt.Sprintf(" sign_tool=%q", e.signTool) |
| 115 | } |
| 116 | format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n" |
| 117 | if e.presigned { |
| 118 | return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool) |
| 119 | } else { |
| 120 | return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool) |
| 121 | } |
| 122 | } |
| 123 | |
Jooyung Han | 286957d | 2023-10-30 16:17:56 +0900 | [diff] [blame] | 124 | func apexKeyEntryFor(ctx android.ModuleContext, module android.Module) apexKeyEntry { |
Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame] | 125 | switch m := module.(type) { |
| 126 | case *apexBundle: |
| 127 | pem, key := m.getCertificateAndPrivateKey(ctx) |
| 128 | return apexKeyEntry{ |
| 129 | name: m.Name() + ".apex", |
| 130 | presigned: false, |
| 131 | publicKey: m.publicKeyFile.String(), |
| 132 | privateKey: m.privateKeyFile.String(), |
| 133 | containerCertificate: pem.String(), |
| 134 | containerPrivateKey: key.String(), |
| 135 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 136 | signTool: proptools.String(m.properties.Custom_sign_tool), |
| 137 | } |
| 138 | case *Prebuilt: |
| 139 | return apexKeyEntry{ |
| 140 | name: m.InstallFilename(), |
| 141 | presigned: true, |
| 142 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 143 | } |
| 144 | case *ApexSet: |
| 145 | return apexKeyEntry{ |
| 146 | name: m.InstallFilename(), |
| 147 | presigned: true, |
| 148 | partition: m.PartitionTag(ctx.DeviceConfig()), |
| 149 | } |
| 150 | } |
| 151 | panic(fmt.Errorf("unknown type(%t) for apexKeyEntry", module)) |
| 152 | } |
| 153 | |
Jooyung Han | 286957d | 2023-10-30 16:17:56 +0900 | [diff] [blame] | 154 | func writeApexKeys(ctx android.ModuleContext, module android.Module) android.WritablePath { |
| 155 | path := android.PathForModuleOut(ctx, "apexkeys.txt") |
| 156 | entry := apexKeyEntryFor(ctx, module) |
| 157 | android.WriteFileRuleVerbatim(ctx, path, entry.String()) |
| 158 | return path |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 159 | } |
Spandan Das | 7701d5f | 2024-12-17 17:52:26 +0000 | [diff] [blame] | 160 | |
| 161 | var ( |
| 162 | pemToDer = pctx.AndroidStaticRule("pem_to_der", |
| 163 | blueprint.RuleParams{ |
| 164 | Command: `openssl x509 -inform PEM -outform DER -in $in -out $out`, |
| 165 | Description: "Convert certificate from PEM to DER format", |
| 166 | }, |
| 167 | ) |
| 168 | ) |
| 169 | |
| 170 | // all_apex_certs is a singleton module that collects the certs of all apexes in the tree. |
| 171 | // It provides two types of output files |
| 172 | // 1. .pem: This is usually the checked-in x509 certificate in PEM format |
| 173 | // 2. .der: This is DER format of the certificate, and is generated from the PEM certificate using `openssl x509` |
| 174 | func allApexCertsFactory() android.SingletonModule { |
| 175 | m := &allApexCerts{} |
| 176 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 177 | return m |
| 178 | } |
| 179 | |
| 180 | type allApexCerts struct { |
| 181 | android.SingletonModuleBase |
| 182 | } |
| 183 | |
| 184 | func (_ *allApexCerts) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 185 | var certificatesPem android.Paths |
| 186 | ctx.VisitDirectDeps(func(m android.Module) { |
| 187 | if apex, ok := m.(*apexBundle); ok { |
| 188 | pem, _ := apex.getCertificateAndPrivateKey(ctx) |
| 189 | if !android.ExistentPathForSource(ctx, pem.String()).Valid() { |
| 190 | if ctx.Config().AllowMissingDependencies() { |
| 191 | return |
| 192 | } else { |
| 193 | ctx.ModuleErrorf("Path %s is not valid\n", pem.String()) |
| 194 | } |
| 195 | } |
| 196 | certificatesPem = append(certificatesPem, pem) |
| 197 | } |
| 198 | }) |
| 199 | certificatesPem = android.SortedUniquePaths(certificatesPem) // For hermiticity |
| 200 | var certificatesDer android.Paths |
| 201 | for index, certificatePem := range certificatesPem { |
| 202 | certificateDer := android.PathForModuleOut(ctx, fmt.Sprintf("x509.%v.der", index)) |
| 203 | ctx.Build(pctx, android.BuildParams{ |
| 204 | Rule: pemToDer, |
| 205 | Input: certificatePem, |
| 206 | Output: certificateDer, |
| 207 | }) |
| 208 | certificatesDer = append(certificatesDer, certificateDer) |
| 209 | } |
| 210 | ctx.SetOutputFiles(certificatesPem, ".pem") |
| 211 | ctx.SetOutputFiles(certificatesDer, ".der") |
| 212 | } |
| 213 | |
| 214 | func (_ *allApexCerts) GenerateSingletonBuildActions(ctx android.SingletonContext) { |
| 215 | } |