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 | 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() { |
| 30 | android.RegisterModuleType("apex_key", apexKeyFactory) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 31 | android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type apexKey struct { |
| 35 | android.ModuleBase |
| 36 | |
| 37 | properties apexKeyProperties |
| 38 | |
| 39 | public_key_file android.Path |
| 40 | private_key_file android.Path |
| 41 | |
| 42 | keyName string |
| 43 | } |
| 44 | |
| 45 | type apexKeyProperties struct { |
| 46 | // Path to the public key file in avbpubkey format. Installed to the device. |
| 47 | // Base name of the file is used as the ID for the key. |
| 48 | Public_key *string |
| 49 | // Path to the private key file in pem format. Used to sign APEXs. |
| 50 | Private_key *string |
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 | |
| 56 | func apexKeyFactory() android.Module { |
| 57 | module := &apexKey{} |
| 58 | module.AddProperties(&module.properties) |
| 59 | android.InitAndroidModule(module) |
| 60 | return module |
| 61 | } |
| 62 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 63 | func (m *apexKey) installable() bool { |
| 64 | return m.properties.Installable == nil || proptools.Bool(m.properties.Installable) |
| 65 | } |
| 66 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 67 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 68 | m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key)) |
| 69 | m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) |
| 70 | |
| 71 | // If not found, fall back to the local key pairs |
| 72 | if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() { |
| 73 | m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
| 74 | } |
| 75 | if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() { |
| 76 | m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
| 77 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 78 | |
| 79 | pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())] |
| 80 | privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())] |
| 81 | |
| 82 | if pubKeyName != privKeyName { |
| 83 | ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname", |
| 84 | m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName) |
| 85 | return |
| 86 | } |
| 87 | m.keyName = pubKeyName |
| 88 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 89 | if m.installable() { |
| 90 | ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file) |
| 91 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (m *apexKey) AndroidMk() android.AndroidMkData { |
| 95 | return android.AndroidMkData{ |
| 96 | Class: "ETC", |
| 97 | OutputFile: android.OptionalPathForPath(m.public_key_file), |
| 98 | Extra: []android.AndroidMkExtraFunc{ |
| 99 | func(w io.Writer, outputFile android.Path) { |
| 100 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex") |
| 101 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName) |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 102 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable()) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 103 | }, |
| 104 | }, |
| 105 | } |
| 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 | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 116 | var filecontent strings.Builder |
| 117 | ctx.VisitAllModules(func(module android.Module) { |
| 118 | if m, ok := module.(android.Module); ok && !m.Enabled() { |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | if m, ok := module.(*apexBundle); ok { |
| 123 | fmt.Fprintf(&filecontent, |
| 124 | "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n", |
| 125 | m.Name()+".apex", |
| 126 | m.public_key_file.String(), |
| 127 | m.private_key_file.String(), |
| 128 | m.container_certificate_file.String(), |
| 129 | m.container_private_key_file.String()) |
| 130 | } |
| 131 | }) |
| 132 | ctx.Build(pctx, android.BuildParams{ |
| 133 | Rule: android.WriteFile, |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame^] | 134 | Description: "apexkeys.txt", |
| 135 | Output: s.output, |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 136 | Args: map[string]string{ |
| 137 | "content": filecontent.String(), |
| 138 | }, |
| 139 | }) |
| 140 | } |
| 141 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 142 | func apexKeysTextFactory() android.Singleton { |
| 143 | return &apexKeysText{} |
| 144 | } |
| 145 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame^] | 146 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 147 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 148 | } |