| 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" | 
|  | 20 |  | 
|  | 21 | "android/soong/android" | 
| 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() { | 
|  | 29 | android.RegisterModuleType("apex_key", apexKeyFactory) | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | type apexKey struct { | 
|  | 33 | android.ModuleBase | 
|  | 34 |  | 
|  | 35 | properties apexKeyProperties | 
|  | 36 |  | 
|  | 37 | public_key_file  android.Path | 
|  | 38 | private_key_file android.Path | 
|  | 39 |  | 
|  | 40 | keyName string | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | type apexKeyProperties struct { | 
|  | 44 | // Path to the public key file in avbpubkey format. Installed to the device. | 
|  | 45 | // Base name of the file is used as the ID for the key. | 
|  | 46 | Public_key *string | 
|  | 47 | // Path to the private key file in pem format. Used to sign APEXs. | 
|  | 48 | Private_key *string | 
| Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 49 |  | 
|  | 50 | // Whether this key is installable to one of the partitions. Defualt: true. | 
|  | 51 | Installable *bool | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
|  | 54 | func apexKeyFactory() android.Module { | 
|  | 55 | module := &apexKey{} | 
|  | 56 | module.AddProperties(&module.properties) | 
|  | 57 | android.InitAndroidModule(module) | 
|  | 58 | return module | 
|  | 59 | } | 
|  | 60 |  | 
| Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 61 | func (m *apexKey) installable() bool { | 
|  | 62 | return m.properties.Installable == nil || proptools.Bool(m.properties.Installable) | 
|  | 63 | } | 
|  | 64 |  | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 65 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 66 | m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key)) | 
|  | 67 | m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) | 
|  | 68 |  | 
|  | 69 | // If not found, fall back to the local key pairs | 
|  | 70 | if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() { | 
|  | 71 | m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) | 
|  | 72 | } | 
|  | 73 | if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() { | 
|  | 74 | m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) | 
|  | 75 | } | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 76 |  | 
|  | 77 | pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())] | 
|  | 78 | privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())] | 
|  | 79 |  | 
|  | 80 | if pubKeyName != privKeyName { | 
|  | 81 | ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname", | 
|  | 82 | m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName) | 
|  | 83 | return | 
|  | 84 | } | 
|  | 85 | m.keyName = pubKeyName | 
|  | 86 |  | 
| Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 87 | if m.installable() { | 
|  | 88 | ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file) | 
|  | 89 | } | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | func (m *apexKey) AndroidMk() android.AndroidMkData { | 
|  | 93 | return android.AndroidMkData{ | 
|  | 94 | Class:      "ETC", | 
|  | 95 | OutputFile: android.OptionalPathForPath(m.public_key_file), | 
|  | 96 | Extra: []android.AndroidMkExtraFunc{ | 
|  | 97 | func(w io.Writer, outputFile android.Path) { | 
|  | 98 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex") | 
|  | 99 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName) | 
| Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 100 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable()) | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 101 | }, | 
|  | 102 | }, | 
|  | 103 | } | 
|  | 104 | } |