xref: /aosp_15_r20/system/keymaster/contexts/keymaster1_passthrough_context.cpp (revision 789431f29546679ab5188a97751fb38e3018d44d)
1 /*
2 **
3 ** Copyright 2017, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include <keymaster/contexts/keymaster1_passthrough_context.h>
19 
20 #include <utility>
21 
22 #include <keymaster/contexts/soft_attestation_cert.h>
23 #include <keymaster/key_blob_utils/integrity_assured_key_blob.h>
24 #include <keymaster/key_blob_utils/ocb_utils.h>
25 #include <keymaster/key_blob_utils/software_keyblobs.h>
26 #include <keymaster/km_openssl/aes_key.h>
27 #include <keymaster/km_openssl/attestation_utils.h>
28 #include <keymaster/km_openssl/hmac_key.h>
29 #include <keymaster/km_version.h>
30 #include <keymaster/legacy_support/ec_keymaster1_key.h>
31 #include <keymaster/legacy_support/keymaster1_engine.h>
32 #include <keymaster/legacy_support/keymaster1_legacy_support.h>
33 #include <keymaster/legacy_support/keymaster_passthrough_engine.h>
34 #include <keymaster/legacy_support/keymaster_passthrough_key.h>
35 #include <keymaster/legacy_support/rsa_keymaster1_key.h>
36 
37 namespace keymaster {
38 
Keymaster1PassthroughContext(KmVersion version,keymaster1_device_t * dev)39 Keymaster1PassthroughContext::Keymaster1PassthroughContext(KmVersion version,
40                                                            keymaster1_device_t* dev)
41     : SoftAttestationContext(version), device_(dev),
42       pt_engine_(KeymasterPassthroughEngine::createInstance(dev)),
43       km1_engine_(new (std::nothrow) Keymaster1Engine(dev)) {}
44 
SetSystemVersion(uint32_t os_version,uint32_t os_patchlevel)45 keymaster_error_t Keymaster1PassthroughContext::SetSystemVersion(uint32_t os_version,
46                                                                  uint32_t os_patchlevel) {
47     os_version_ = os_version;
48     os_patchlevel_ = os_patchlevel;
49     return KM_ERROR_OK;
50 }
51 
GetSystemVersion(uint32_t * os_version,uint32_t * os_patchlevel) const52 void Keymaster1PassthroughContext::GetSystemVersion(uint32_t* os_version,
53                                                     uint32_t* os_patchlevel) const {
54     if (os_version) *os_version = os_version_;
55     if (os_patchlevel) *os_patchlevel = os_patchlevel_;
56 }
57 
GetKeyFactory(keymaster_algorithm_t algorithm) const58 KeyFactory* Keymaster1PassthroughContext::GetKeyFactory(keymaster_algorithm_t algorithm) const {
59     auto& result = factories_[algorithm];
60     if (!result) {
61         switch (algorithm) {
62         case KM_ALGORITHM_RSA:
63             result.reset(new (std::nothrow) Keymaster1ArbitrationFactory<RsaKeymaster1KeyFactory>(
64                 pt_engine_.get(), KM_ALGORITHM_RSA, device_, *this /* blob_maker */,
65                 *this /* context */, km1_engine_.get()));
66             break;
67         case KM_ALGORITHM_EC:
68             result.reset(new (std::nothrow) Keymaster1ArbitrationFactory<EcdsaKeymaster1KeyFactory>(
69                 pt_engine_.get(), KM_ALGORITHM_EC, device_, *this /* blob_maker */,
70                 *this /* context */, km1_engine_.get()));
71             break;
72         case KM_ALGORITHM_AES:
73             result.reset(new (std::nothrow) Keymaster1ArbitrationFactory<AesKeyFactory>(
74                 pt_engine_.get(), KM_ALGORITHM_AES, device_, *this /* blob_maker */,
75                 *this /* random_source */));
76             break;
77         case KM_ALGORITHM_HMAC:
78             result.reset(new (std::nothrow) Keymaster1ArbitrationFactory<HmacKeyFactory>(
79                 pt_engine_.get(), KM_ALGORITHM_HMAC, device_, *this /* blob_maker */,
80                 *this /* random_source */));
81             break;
82         case KM_ALGORITHM_TRIPLE_DES:
83             // Not supported by KM1.
84             return nullptr;
85         }
86     }
87     return result.get();
88 }
89 OperationFactory*
GetOperationFactory(keymaster_algorithm_t algorithm,keymaster_purpose_t purpose) const90 Keymaster1PassthroughContext::GetOperationFactory(keymaster_algorithm_t algorithm,
91                                                   keymaster_purpose_t purpose) const {
92     auto keyfactory = GetKeyFactory(algorithm);
93     return keyfactory->GetOperationFactory(purpose);
94 }
95 keymaster_algorithm_t*
GetSupportedAlgorithms(size_t * algorithms_count) const96 Keymaster1PassthroughContext::GetSupportedAlgorithms(size_t* algorithms_count) const {
97     if (algorithms_count) *algorithms_count = 0;
98     return nullptr;
99 }
100 
101 keymaster_error_t
UpgradeKeyBlob(const KeymasterKeyBlob & key_to_upgrade,const AuthorizationSet & upgrade_params,KeymasterKeyBlob * upgraded_key) const102 Keymaster1PassthroughContext::UpgradeKeyBlob(const KeymasterKeyBlob& key_to_upgrade,
103                                              const AuthorizationSet& upgrade_params,
104                                              KeymasterKeyBlob* upgraded_key) const {
105 
106     UniquePtr<Key> key;
107     keymaster_error_t error = ParseKeyBlob(key_to_upgrade, upgrade_params, &key);
108     if (error != KM_ERROR_OK) return error;
109 
110     if (key->hw_enforced().Contains(TAG_PURPOSE) &&
111         !key->hw_enforced().Contains(TAG_OS_PATCHLEVEL)) {
112         return KM_ERROR_INVALID_ARGUMENT;
113     }
114 
115     return UpgradeSoftKeyBlob(key, os_version_, os_patchlevel_, upgrade_params, upgraded_key);
116 }
117 
118 static keymaster_error_t
parseKeymaster1HwBlob(const keymaster1_device_t * device,const KeymasterKeyBlob & blob,const AuthorizationSet & additional_params,KeymasterKeyBlob * key_material,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced)119 parseKeymaster1HwBlob(const keymaster1_device_t* device, const KeymasterKeyBlob& blob,
120                       const AuthorizationSet& additional_params, KeymasterKeyBlob* key_material,
121                       AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) {
122     keymaster_blob_t client_id = {nullptr, 0};
123     keymaster_blob_t app_data = {nullptr, 0};
124     keymaster_blob_t* client_id_ptr = nullptr;
125     keymaster_blob_t* app_data_ptr = nullptr;
126     if (additional_params.GetTagValue(TAG_APPLICATION_ID, &client_id)) client_id_ptr = &client_id;
127     if (additional_params.GetTagValue(TAG_APPLICATION_DATA, &app_data)) app_data_ptr = &app_data;
128 
129     // Get key characteristics, which incidentally verifies that the HW recognizes the key.
130     keymaster_key_characteristics_t* characteristics;
131     keymaster_error_t error = device->get_key_characteristics(device, &blob, client_id_ptr,
132                                                               app_data_ptr, &characteristics);
133     if (error != KM_ERROR_OK) return error;
134 
135     UniquePtr<keymaster_key_characteristics_t, Characteristics_Delete> characteristics_deleter(
136         characteristics);
137 
138     hw_enforced->Reinitialize(characteristics->hw_enforced);
139     sw_enforced->Reinitialize(characteristics->sw_enforced);
140     *key_material = blob;
141     return KM_ERROR_OK;
142 }
143 
144 keymaster_error_t
ParseKeyBlob(const KeymasterKeyBlob & blob,const AuthorizationSet & additional_params,UniquePtr<Key> * key) const145 Keymaster1PassthroughContext::ParseKeyBlob(const KeymasterKeyBlob& blob,
146                                            const AuthorizationSet& additional_params,
147                                            UniquePtr<Key>* key) const {
148     AuthorizationSet hw_enforced;
149     AuthorizationSet sw_enforced;
150     KeymasterKeyBlob key_material;
151 
152     AuthorizationSet hidden;
153     keymaster_error_t error =
154         BuildHiddenAuthorizations(additional_params, &hidden, softwareRootOfTrust);
155     if (error != KM_ERROR_OK) return error;
156 
157     // Assume it's an integrity-assured blob (new software-only blob).
158     error =
159         DeserializeIntegrityAssuredBlob(blob, hidden, &key_material, &hw_enforced, &sw_enforced);
160     if (error != KM_ERROR_INVALID_KEY_BLOB && error != KM_ERROR_OK) return error;
161 
162     if (error == KM_ERROR_INVALID_KEY_BLOB) {
163         error = parseKeymaster1HwBlob(km1_engine_->device(), blob, additional_params, &key_material,
164                                       &hw_enforced, &sw_enforced);
165         if (error != KM_ERROR_OK) return error;
166     }
167 
168     // GetKeyFactory
169     keymaster_algorithm_t algorithm;
170     if (!hw_enforced.GetTagValue(TAG_ALGORITHM, &algorithm) &&
171         !sw_enforced.GetTagValue(TAG_ALGORITHM, &algorithm)) {
172         return KM_ERROR_INVALID_ARGUMENT;
173     }
174     auto factory = GetKeyFactory(algorithm);
175 
176     return factory->LoadKey(std::move(key_material), additional_params, std::move(hw_enforced),
177                             std::move(sw_enforced), key);
178 }
179 
DeleteKey(const KeymasterKeyBlob & blob) const180 keymaster_error_t Keymaster1PassthroughContext::DeleteKey(const KeymasterKeyBlob& blob) const {
181     // HACK. Due to a bug with Qualcomm's Keymaster implementation, which causes the device to
182     // reboot if we pass it a key blob it doesn't understand, we need to check for software
183     // keys.  If it looks like a software key there's nothing to do so we just return.
184     // Can be removed once b/33385206 is fixed
185     KeymasterKeyBlob key_material;
186     AuthorizationSet hw_enforced, sw_enforced;
187     keymaster_error_t error = DeserializeIntegrityAssuredBlob_NoHmacCheck(
188         blob, &key_material, &hw_enforced, &sw_enforced);
189     if (error == KM_ERROR_OK) {
190         return KM_ERROR_OK;
191     }
192 
193     error = km1_engine_->DeleteKey(blob);
194     if (error == KM_ERROR_INVALID_KEY_BLOB) {
195         // Some implementations diagnose invalid keys.
196         // However, all care we about is that the key blob, as supplied, is not usable after the
197         // call.
198         return KM_ERROR_OK;
199     }
200     return error;
201 }
202 
DeleteAllKeys() const203 keymaster_error_t Keymaster1PassthroughContext::DeleteAllKeys() const {
204     return km1_engine_->DeleteAllKeys();
205 }
206 
AddRngEntropy(const uint8_t * buf,size_t length) const207 keymaster_error_t Keymaster1PassthroughContext::AddRngEntropy(const uint8_t* buf,
208                                                               size_t length) const {
209     return device_->add_rng_entropy(device_, buf, length);
210 }
211 
enforcement_policy()212 KeymasterEnforcement* Keymaster1PassthroughContext::enforcement_policy() {
213     return nullptr;
214 }
215 
CreateKeyBlob(const AuthorizationSet & key_description,const keymaster_key_origin_t origin,const KeymasterKeyBlob & key_material,KeymasterKeyBlob * blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const216 keymaster_error_t Keymaster1PassthroughContext::CreateKeyBlob(
217     const AuthorizationSet& key_description, const keymaster_key_origin_t origin,
218     const KeymasterKeyBlob& key_material, KeymasterKeyBlob* blob, AuthorizationSet* hw_enforced,
219     AuthorizationSet* sw_enforced) const {
220     keymaster_error_t error =
221         SetKeyBlobAuthorizations(key_description, origin, os_version_, os_patchlevel_, hw_enforced,
222                                  sw_enforced, GetKmVersion());
223     if (error != KM_ERROR_OK) return error;
224 
225     AuthorizationSet hidden;
226     error = BuildHiddenAuthorizations(key_description, &hidden, softwareRootOfTrust);
227     if (error != KM_ERROR_OK) return error;
228 
229     return SerializeIntegrityAssuredBlob(key_material, hidden, *hw_enforced, *sw_enforced, blob);
230 }
231 
GenerateAttestation(const Key & key,const AuthorizationSet & attest_params,UniquePtr<Key>,const KeymasterBlob &,keymaster_error_t * error) const232 CertificateChain Keymaster1PassthroughContext::GenerateAttestation(
233     const Key& key, const AuthorizationSet& attest_params, UniquePtr<Key> /* attest_key */,
234     const KeymasterBlob& /* issuer_subject */, keymaster_error_t* error) const {
235     keymaster_algorithm_t key_algorithm;
236     if (!key.authorizations().GetTagValue(TAG_ALGORITHM, &key_algorithm)) {
237         *error = KM_ERROR_UNKNOWN_ERROR;
238         return {};
239     }
240 
241     if ((key_algorithm != KM_ALGORITHM_RSA && key_algorithm != KM_ALGORITHM_EC)) {
242         *error = KM_ERROR_INCOMPATIBLE_ALGORITHM;
243         return {};
244     }
245 
246     // We have established that the given key has the correct algorithm, and because this is the
247     // SoftKeymasterContext we can assume that the Key is an AsymmetricKey. So we can downcast.
248     const AsymmetricKey& asymmetric_key = static_cast<const AsymmetricKey&>(key);
249 
250     return generate_attestation(asymmetric_key, attest_params, {} /* attest_key */,
251                                 *this /* AttestationContext */, error);
252 }
253 
UnwrapKey(const KeymasterKeyBlob &,const KeymasterKeyBlob &,const AuthorizationSet &,const KeymasterKeyBlob &,AuthorizationSet *,keymaster_key_format_t *,KeymasterKeyBlob *) const254 keymaster_error_t Keymaster1PassthroughContext::UnwrapKey(
255     const KeymasterKeyBlob&, const KeymasterKeyBlob&, const AuthorizationSet&,
256     const KeymasterKeyBlob&, AuthorizationSet*, keymaster_key_format_t*, KeymasterKeyBlob*) const {
257     return KM_ERROR_UNIMPLEMENTED;
258 }
259 
260 }  // namespace keymaster
261