blob: 1622c65e657244218132acb2d1118879cd53c407 [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"
Jiyong Parkff1458f2018-10-12 21:49:38 +090019
20 "android/soong/android"
Spandan Das7701d5f2024-12-17 17:52:26 +000021 "github.com/google/blueprint"
Jiyong Parkff1458f2018-10-12 21:49:38 +090022 "github.com/google/blueprint/proptools"
23)
24
25var String = proptools.String
26
27func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000028 registerApexKeyBuildComponents(android.InitRegistrationContext)
29}
30
31func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Spandan Das7701d5f2024-12-17 17:52:26 +000033 ctx.RegisterParallelSingletonModuleType("all_apex_certs", allApexCertsFactory)
Jiyong Parkff1458f2018-10-12 21:49:38 +090034}
35
36type apexKey struct {
37 android.ModuleBase
38
39 properties apexKeyProperties
40
Jaewoong Jung18aefc12020-12-21 09:11:10 -080041 publicKeyFile android.Path
42 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090043}
44
45type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090046 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090047 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090048 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 Park50d99202018-12-27 13:32:34 +090051
52 // Whether this key is installable to one of the partitions. Defualt: true.
53 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090054}
55
Jiyong Parkd1063c12019-07-17 20:08:41 +090056func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090057 module := &apexKey{}
58 module.AddProperties(&module.properties)
Jooyung Han8d4a1f02023-08-23 13:54:08 +090059 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkff1458f2018-10-12 21:49:38 +090060 return module
61}
62
Jiyong Park50d99202018-12-27 13:32:34 +090063func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090064 return false
Jiyong Park50d99202018-12-27 13:32:34 +090065}
66
Jiyong Parkff1458f2018-10-12 21:49:38 +090067func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090068 // 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 Jung18aefc12020-12-21 09:11:10 -080072 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090073 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080074 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090075 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080076 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
77 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090078 }
Jiyong Park9335a262018-12-24 11:31:58 +090079 }
Jiyong Park67882562019-03-21 01:11:21 +090080
81 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080082 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090083 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080084 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 Park67882562019-03-21 01:11:21 +090087 }
Jiyong Park9335a262018-12-24 11:31:58 +090088 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090089
Jaewoong Jung18aefc12020-12-21 09:11:10 -080090 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 Parkff1458f2018-10-12 21:49:38 +090092
Jaewoong Jung939ebd52019-03-26 15:07:36 -070093 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +090094 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
Jaewoong Jung18aefc12020-12-21 09:11:10 -080095 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +090096 return
97 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090098}
Jiyong Park0ca3ce82019-02-18 15:25:04 +090099
Jooyung Han2cf35e72023-10-30 11:17:16 +0900100type 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
111func (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 Han286957d2023-10-30 16:17:56 +0900124func apexKeyEntryFor(ctx android.ModuleContext, module android.Module) apexKeyEntry {
Jooyung Han2cf35e72023-10-30 11:17:16 +0900125 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 Han286957d2023-10-30 16:17:56 +0900154func 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 Park0ca3ce82019-02-18 15:25:04 +0900159}
Spandan Das7701d5f2024-12-17 17:52:26 +0000160
161var (
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`
174func allApexCertsFactory() android.SingletonModule {
175 m := &allApexCerts{}
176 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
177 return m
178}
179
180type allApexCerts struct {
181 android.SingletonModuleBase
182}
183
184func (_ *allApexCerts) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Dasef1a1fd2025-01-03 19:43:38 +0000185 var avbpubkeys android.Paths
Spandan Das7701d5f2024-12-17 17:52:26 +0000186 var certificatesPem android.Paths
187 ctx.VisitDirectDeps(func(m android.Module) {
188 if apex, ok := m.(*apexBundle); ok {
189 pem, _ := apex.getCertificateAndPrivateKey(ctx)
190 if !android.ExistentPathForSource(ctx, pem.String()).Valid() {
191 if ctx.Config().AllowMissingDependencies() {
192 return
193 } else {
194 ctx.ModuleErrorf("Path %s is not valid\n", pem.String())
195 }
196 }
197 certificatesPem = append(certificatesPem, pem)
Spandan Dasef1a1fd2025-01-03 19:43:38 +0000198 // avbpubkey for signing the apex payload
199 avbpubkeys = append(avbpubkeys, apex.publicKeyFile)
Spandan Das7701d5f2024-12-17 17:52:26 +0000200 }
201 })
202 certificatesPem = android.SortedUniquePaths(certificatesPem) // For hermiticity
Spandan Dasef1a1fd2025-01-03 19:43:38 +0000203 avbpubkeys = android.SortedUniquePaths(avbpubkeys) // For hermiticity
Spandan Das7701d5f2024-12-17 17:52:26 +0000204 var certificatesDer android.Paths
205 for index, certificatePem := range certificatesPem {
206 certificateDer := android.PathForModuleOut(ctx, fmt.Sprintf("x509.%v.der", index))
207 ctx.Build(pctx, android.BuildParams{
208 Rule: pemToDer,
209 Input: certificatePem,
210 Output: certificateDer,
211 })
212 certificatesDer = append(certificatesDer, certificateDer)
213 }
214 ctx.SetOutputFiles(certificatesPem, ".pem")
215 ctx.SetOutputFiles(certificatesDer, ".der")
Spandan Dasef1a1fd2025-01-03 19:43:38 +0000216 ctx.SetOutputFiles(avbpubkeys, ".avbpubkey")
Spandan Das7701d5f2024-12-17 17:52:26 +0000217}
218
219func (_ *allApexCerts) GenerateSingletonBuildActions(ctx android.SingletonContext) {
220}