blob: 7e98d2b67496d906fa6efdafae5d3b09e83ebbb4 [file] [log] [blame]
Jiyong Parkff1458f2018-10-12 21:49:38 +09001// 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
15package apex
16
17import (
18 "fmt"
19 "io"
20
21 "android/soong/android"
22 "github.com/google/blueprint/proptools"
23)
24
25var String = proptools.String
26
27func init() {
28 android.RegisterModuleType("apex_key", apexKeyFactory)
29}
30
31type apexKey struct {
32 android.ModuleBase
33
34 properties apexKeyProperties
35
36 public_key_file android.Path
37 private_key_file android.Path
38
39 keyName string
40}
41
42type apexKeyProperties struct {
43 // Path to the public key file in avbpubkey format. Installed to the device.
44 // Base name of the file is used as the ID for the key.
45 Public_key *string
46 // Path to the private key file in pem format. Used to sign APEXs.
47 Private_key *string
Jiyong Park50d99202018-12-27 13:32:34 +090048
49 // Whether this key is installable to one of the partitions. Defualt: true.
50 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090051}
52
53func apexKeyFactory() android.Module {
54 module := &apexKey{}
55 module.AddProperties(&module.properties)
56 android.InitAndroidModule(module)
57 return module
58}
59
Jiyong Park50d99202018-12-27 13:32:34 +090060func (m *apexKey) installable() bool {
61 return m.properties.Installable == nil || proptools.Bool(m.properties.Installable)
62}
63
Jiyong Parkff1458f2018-10-12 21:49:38 +090064func (m *apexKey) DepsMutator(ctx android.BottomUpMutatorContext) {
65}
66
67func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park9335a262018-12-24 11:31:58 +090068 if ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild() {
69 // Flattened APEXes are not signed
70 return
71 }
72
73 m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
74 m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
75
76 // If not found, fall back to the local key pairs
77 if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
78 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
79 }
80 if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
81 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
82 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090083
84 pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
85 privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
86
87 if pubKeyName != privKeyName {
88 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
89 m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName)
90 return
91 }
92 m.keyName = pubKeyName
93
Jiyong Park50d99202018-12-27 13:32:34 +090094 if m.installable() {
95 ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file)
96 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090097}
98
99func (m *apexKey) AndroidMk() android.AndroidMkData {
100 return android.AndroidMkData{
101 Class: "ETC",
102 OutputFile: android.OptionalPathForPath(m.public_key_file),
103 Extra: []android.AndroidMkExtraFunc{
104 func(w io.Writer, outputFile android.Path) {
105 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex")
106 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName)
Jiyong Park50d99202018-12-27 13:32:34 +0900107 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable())
Jiyong Parkff1458f2018-10-12 21:49:38 +0900108 },
109 },
110 }
111}