1 /*
2  * Copyright 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <keymaster/legacy_support/ec_keymaster1_key.h>
18 
19 #include <memory>
20 #include <utility>
21 
22 #include <keymaster/km_openssl/ecdsa_operation.h>
23 #include <keymaster/logger.h>
24 
25 #include "ecdsa_keymaster1_operation.h"
26 
27 using std::unique_ptr;
28 
29 namespace keymaster {
30 
EcdsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker & blob_maker,const KeymasterContext & context,const Keymaster1Engine * engine)31 EcdsaKeymaster1KeyFactory::EcdsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker& blob_maker,
32                                                      const KeymasterContext& context,
33                                                      const Keymaster1Engine* engine)
34     : EcKeyFactory(blob_maker, context), engine_(engine),
35       sign_factory_(new (std::nothrow) EcdsaKeymaster1OperationFactory(KM_PURPOSE_SIGN, engine)),
36       // For pubkey ops we can use the normal operation factories.
37       verify_factory_(new (std::nothrow) EcdsaVerifyOperationFactory) {}
38 
is_supported(uint32_t digest)39 static bool is_supported(uint32_t digest) {
40     return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
41 }
42 
UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet & key_description,AuthorizationSet * new_description)43 static void UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet& key_description,
44                                                  AuthorizationSet* new_description) {
45     bool have_unsupported_digests = false;
46     bool have_digest_none = false;
47     for (const keymaster_key_param_t& entry : key_description) {
48         new_description->push_back(entry);
49 
50         if (entry.tag == TAG_DIGEST) {
51             if (entry.enumerated == KM_DIGEST_NONE) {
52                 have_digest_none = true;
53             } else if (!is_supported(entry.enumerated)) {
54                 LOG_D("Found request for unsupported digest %u", entry.enumerated);
55                 have_unsupported_digests = true;
56             }
57         }
58     }
59 
60     if (have_unsupported_digests && !have_digest_none) {
61         LOG_I("Adding KM_DIGEST_NONE to key authorization, to enable software digesting");
62         new_description->push_back(TAG_DIGEST, KM_DIGEST_NONE);
63     }
64 }
65 
GenerateKey(const AuthorizationSet & key_description,UniquePtr<Key>,const KeymasterBlob &,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced,CertificateChain *) const66 keymaster_error_t EcdsaKeymaster1KeyFactory::GenerateKey(const AuthorizationSet& key_description,
67                                                          UniquePtr<Key> /* attest_key */,
68                                                          const KeymasterBlob& /* issuer_subject */,
69                                                          KeymasterKeyBlob* key_blob,
70                                                          AuthorizationSet* hw_enforced,
71                                                          AuthorizationSet* sw_enforced,
72                                                          CertificateChain* /* cert_chain */) const {
73     AuthorizationSet key_params_copy;
74     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
75 
76     keymaster_ec_curve_t ec_curve;
77     uint32_t key_size;
78     keymaster_error_t error = GetCurveAndSize(key_description, &ec_curve, &key_size);
79     if (error != KM_ERROR_OK) {
80         return error;
81     } else if (!key_description.Contains(TAG_KEY_SIZE, key_size)) {
82         key_params_copy.push_back(TAG_KEY_SIZE, key_size);
83     }
84     return engine_->GenerateKey(key_params_copy, key_blob, hw_enforced, sw_enforced);
85 }
86 
87 keymaster_error_t
ImportKey(const AuthorizationSet & key_description,keymaster_key_format_t input_key_material_format,const KeymasterKeyBlob & input_key_material,UniquePtr<Key>,const KeymasterBlob &,KeymasterKeyBlob * output_key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced,CertificateChain *) const88 EcdsaKeymaster1KeyFactory::ImportKey(const AuthorizationSet& key_description,           //
89                                      keymaster_key_format_t input_key_material_format,  //
90                                      const KeymasterKeyBlob& input_key_material,        //
91                                      UniquePtr<Key> /* attest_key */,                   //
92                                      const KeymasterBlob& /* issuer_subject */,
93                                      KeymasterKeyBlob* output_key_blob,  //
94                                      AuthorizationSet* hw_enforced,      //
95                                      AuthorizationSet* sw_enforced,
96                                      CertificateChain* /* cert_chain */) const {
97     AuthorizationSet key_params_copy;
98     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
99     return engine_->ImportKey(key_params_copy, input_key_material_format, input_key_material,
100                               output_key_blob, hw_enforced, sw_enforced);
101 }
102 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet & additional_params,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const103 keymaster_error_t EcdsaKeymaster1KeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
104                                                      const AuthorizationSet& additional_params,
105                                                      AuthorizationSet&& hw_enforced,
106                                                      AuthorizationSet&& sw_enforced,
107                                                      UniquePtr<Key>* key) const {
108     if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
109 
110     keymaster_error_t error;
111     unique_ptr<EC_KEY, EC_KEY_Delete> ecdsa(
112         engine_->BuildEcKey(key_material, additional_params, &error));
113     if (!ecdsa) return error;
114 
115     key->reset(new (std::nothrow)
116                    EcdsaKeymaster1Key(ecdsa.release(), std::move(hw_enforced),
117                                       std::move(sw_enforced), this));
118     if (!(*key)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
119 
120     (*key)->key_material() = std::move(key_material);
121     return KM_ERROR_OK;
122 }
123 
124 OperationFactory*
GetOperationFactory(keymaster_purpose_t purpose) const125 EcdsaKeymaster1KeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
126     switch (purpose) {
127     case KM_PURPOSE_SIGN:
128         return sign_factory_.get();
129     case KM_PURPOSE_VERIFY:
130         return verify_factory_.get();
131     default:
132         return nullptr;
133     }
134 }
135 
136 }  // namespace keymaster
137