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" |
| 19 | "io" |
Jiyong Park | d1e293d | 2019-03-15 02:13:21 +0900 | [diff] [blame] | 20 | "path/filepath" |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 21 | "strings" |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
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() { |
| 31 | android.RegisterModuleType("apex_key", apexKeyFactory) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 32 | android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type apexKey struct { |
| 36 | android.ModuleBase |
| 37 | |
| 38 | properties apexKeyProperties |
| 39 | |
| 40 | public_key_file android.Path |
| 41 | private_key_file android.Path |
Jiyong Park | d1e293d | 2019-03-15 02:13:21 +0900 | [diff] [blame] | 42 | installDir android.OutputPath |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 43 | |
| 44 | keyName string |
| 45 | } |
| 46 | |
| 47 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame^] | 48 | // 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] | 49 | // 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^] | 50 | Public_key *string `android:"path"` |
| 51 | // Path or module to the private key file in pem format. Used to sign APEXs. |
| 52 | Private_key *string `android:"path"` |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 53 | |
| 54 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 55 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func apexKeyFactory() android.Module { |
| 59 | module := &apexKey{} |
| 60 | module.AddProperties(&module.properties) |
Jiyong Park | d1e293d | 2019-03-15 02:13:21 +0900 | [diff] [blame] | 61 | // This module is device-only |
| 62 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 63 | return module |
| 64 | } |
| 65 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 66 | func (m *apexKey) installable() bool { |
| 67 | return m.properties.Installable == nil || proptools.Bool(m.properties.Installable) |
| 68 | } |
| 69 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 70 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame^] | 71 | // If the keys are from other modules (i.e. :module syntax) respect it. |
| 72 | // Otherwise, try to locate the key files in the default cert dir or |
| 73 | // in the local module dir |
| 74 | if android.SrcIsModule(String(m.properties.Public_key)) != "" { |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 75 | m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame^] | 76 | } else { |
| 77 | m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key)) |
| 78 | // If not found, fall back to the local key pairs |
| 79 | if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() { |
| 80 | m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
| 81 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 82 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame^] | 83 | |
| 84 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 85 | m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame^] | 86 | } else { |
| 87 | m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) |
| 88 | if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() { |
| 89 | m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
| 90 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 91 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 92 | |
| 93 | pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())] |
| 94 | privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())] |
| 95 | |
| 96 | if pubKeyName != privKeyName { |
| 97 | ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname", |
| 98 | m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName) |
| 99 | return |
| 100 | } |
| 101 | m.keyName = pubKeyName |
| 102 | |
Jiyong Park | d1e293d | 2019-03-15 02:13:21 +0900 | [diff] [blame] | 103 | m.installDir = android.PathForModuleInstall(ctx, "etc/security/apex") |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 104 | if m.installable() { |
Jiyong Park | d1e293d | 2019-03-15 02:13:21 +0900 | [diff] [blame] | 105 | ctx.InstallFile(m.installDir, m.keyName, m.public_key_file) |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 106 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | func (m *apexKey) AndroidMk() android.AndroidMkData { |
| 110 | return android.AndroidMkData{ |
| 111 | Class: "ETC", |
| 112 | OutputFile: android.OptionalPathForPath(m.public_key_file), |
| 113 | Extra: []android.AndroidMkExtraFunc{ |
| 114 | func(w io.Writer, outputFile android.Path) { |
Jiyong Park | d1e293d | 2019-03-15 02:13:21 +0900 | [diff] [blame] | 115 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", m.installDir.RelPathString())) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 116 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName) |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 117 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable()) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 118 | }, |
| 119 | }, |
| 120 | } |
| 121 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 122 | |
| 123 | //////////////////////////////////////////////////////////////////////// |
| 124 | // apex_keys_text |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 125 | type apexKeysText struct { |
| 126 | output android.OutputPath |
| 127 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 128 | |
| 129 | func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) { |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 130 | s.output = android.PathForOutput(ctx, "apexkeys.txt") |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 131 | var filecontent strings.Builder |
| 132 | ctx.VisitAllModules(func(module android.Module) { |
| 133 | if m, ok := module.(android.Module); ok && !m.Enabled() { |
| 134 | return |
| 135 | } |
| 136 | |
| 137 | if m, ok := module.(*apexBundle); ok { |
| 138 | fmt.Fprintf(&filecontent, |
| 139 | "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n", |
| 140 | m.Name()+".apex", |
| 141 | m.public_key_file.String(), |
| 142 | m.private_key_file.String(), |
| 143 | m.container_certificate_file.String(), |
| 144 | m.container_private_key_file.String()) |
| 145 | } |
| 146 | }) |
| 147 | ctx.Build(pctx, android.BuildParams{ |
| 148 | Rule: android.WriteFile, |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 149 | Description: "apexkeys.txt", |
| 150 | Output: s.output, |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 151 | Args: map[string]string{ |
| 152 | "content": filecontent.String(), |
| 153 | }, |
| 154 | }) |
| 155 | } |
| 156 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 157 | func apexKeysTextFactory() android.Singleton { |
| 158 | return &apexKeysText{} |
| 159 | } |
| 160 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 161 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 162 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 163 | } |