| 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" | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 19 |  | 
 | 20 | 	"android/soong/android" | 
 | 21 | 	"github.com/google/blueprint/proptools" | 
 | 22 | ) | 
 | 23 |  | 
 | 24 | var String = proptools.String | 
 | 25 |  | 
 | 26 | func init() { | 
| Paul Duffin | 667893c | 2021-03-09 22:34:13 +0000 | [diff] [blame] | 27 | 	registerApexKeyBuildComponents(android.InitRegistrationContext) | 
 | 28 | } | 
 | 29 |  | 
 | 30 | func registerApexKeyBuildComponents(ctx android.RegistrationContext) { | 
 | 31 | 	ctx.RegisterModuleType("apex_key", ApexKeyFactory) | 
| 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 |  | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 39 | 	publicKeyFile  android.Path | 
 | 40 | 	privateKeyFile android.Path | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 41 | } | 
 | 42 |  | 
 | 43 | type apexKeyProperties struct { | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 44 | 	// 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] | 45 | 	// 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] | 46 | 	Public_key *string `android:"path"` | 
 | 47 | 	// Path or module to the private key file in pem format. Used to sign APEXs. | 
 | 48 | 	Private_key *string `android:"path"` | 
| 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 |  | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 54 | func ApexKeyFactory() android.Module { | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 55 | 	module := &apexKey{} | 
 | 56 | 	module.AddProperties(&module.properties) | 
| Jooyung Han | 8d4a1f0 | 2023-08-23 13:54:08 +0900 | [diff] [blame] | 57 | 	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 58 | 	return module | 
 | 59 | } | 
 | 60 |  | 
| Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 61 | func (m *apexKey) installable() bool { | 
| Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 62 | 	return false | 
| Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 63 | } | 
 | 64 |  | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 65 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 66 | 	// If the keys are from other modules (i.e. :module syntax) respect it. | 
 | 67 | 	// Otherwise, try to locate the key files in the default cert dir or | 
 | 68 | 	// in the local module dir | 
 | 69 | 	if android.SrcIsModule(String(m.properties.Public_key)) != "" { | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 70 | 		m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 71 | 	} else { | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 72 | 		m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key)) | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 73 | 		// If not found, fall back to the local key pairs | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 74 | 		if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() { | 
 | 75 | 			m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 76 | 		} | 
| Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 77 | 	} | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 78 |  | 
 | 79 | 	if android.SrcIsModule(String(m.properties.Private_key)) != "" { | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 80 | 		m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 81 | 	} else { | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 82 | 		m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) | 
 | 83 | 		if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() { | 
 | 84 | 			m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) | 
| Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 85 | 		} | 
| Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 86 | 	} | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 87 |  | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 88 | 	pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())] | 
 | 89 | 	privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())] | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 90 |  | 
| Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 91 | 	if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName { | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 92 | 		ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname", | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 93 | 			m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName) | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 94 | 		return | 
 | 95 | 	} | 
| Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 96 | } | 
| Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 97 |  | 
| Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame] | 98 | type apexKeyEntry struct { | 
 | 99 | 	name                 string | 
 | 100 | 	presigned            bool | 
 | 101 | 	publicKey            string | 
 | 102 | 	privateKey           string | 
 | 103 | 	containerCertificate string | 
 | 104 | 	containerPrivateKey  string | 
 | 105 | 	partition            string | 
 | 106 | 	signTool             string | 
 | 107 | } | 
 | 108 |  | 
 | 109 | func (e apexKeyEntry) String() string { | 
 | 110 | 	signTool := "" | 
 | 111 | 	if e.signTool != "" { | 
 | 112 | 		signTool = fmt.Sprintf(" sign_tool=%q", e.signTool) | 
 | 113 | 	} | 
 | 114 | 	format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n" | 
 | 115 | 	if e.presigned { | 
 | 116 | 		return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool) | 
 | 117 | 	} else { | 
 | 118 | 		return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool) | 
 | 119 | 	} | 
 | 120 | } | 
 | 121 |  | 
| Jooyung Han | 286957d | 2023-10-30 16:17:56 +0900 | [diff] [blame] | 122 | func apexKeyEntryFor(ctx android.ModuleContext, module android.Module) apexKeyEntry { | 
| Jooyung Han | 2cf35e7 | 2023-10-30 11:17:16 +0900 | [diff] [blame] | 123 | 	switch m := module.(type) { | 
 | 124 | 	case *apexBundle: | 
 | 125 | 		pem, key := m.getCertificateAndPrivateKey(ctx) | 
 | 126 | 		return apexKeyEntry{ | 
 | 127 | 			name:                 m.Name() + ".apex", | 
 | 128 | 			presigned:            false, | 
 | 129 | 			publicKey:            m.publicKeyFile.String(), | 
 | 130 | 			privateKey:           m.privateKeyFile.String(), | 
 | 131 | 			containerCertificate: pem.String(), | 
 | 132 | 			containerPrivateKey:  key.String(), | 
 | 133 | 			partition:            m.PartitionTag(ctx.DeviceConfig()), | 
 | 134 | 			signTool:             proptools.String(m.properties.Custom_sign_tool), | 
 | 135 | 		} | 
 | 136 | 	case *Prebuilt: | 
 | 137 | 		return apexKeyEntry{ | 
 | 138 | 			name:      m.InstallFilename(), | 
 | 139 | 			presigned: true, | 
 | 140 | 			partition: m.PartitionTag(ctx.DeviceConfig()), | 
 | 141 | 		} | 
 | 142 | 	case *ApexSet: | 
 | 143 | 		return apexKeyEntry{ | 
 | 144 | 			name:      m.InstallFilename(), | 
 | 145 | 			presigned: true, | 
 | 146 | 			partition: m.PartitionTag(ctx.DeviceConfig()), | 
 | 147 | 		} | 
 | 148 | 	} | 
 | 149 | 	panic(fmt.Errorf("unknown type(%t) for apexKeyEntry", module)) | 
 | 150 | } | 
 | 151 |  | 
| Jooyung Han | 286957d | 2023-10-30 16:17:56 +0900 | [diff] [blame] | 152 | func writeApexKeys(ctx android.ModuleContext, module android.Module) android.WritablePath { | 
 | 153 | 	path := android.PathForModuleOut(ctx, "apexkeys.txt") | 
 | 154 | 	entry := apexKeyEntryFor(ctx, module) | 
 | 155 | 	android.WriteFileRuleVerbatim(ctx, path, entry.String()) | 
 | 156 | 	return path | 
| Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 157 | } |