• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <keymaster/km_openssl/ecdsa_operation.h>
22 #include <keymaster/logger.h>
23 
24 #include "ecdsa_keymaster1_operation.h"
25 
26 using std::unique_ptr;
27 
28 namespace keymaster {
29 
EcdsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker & blob_maker,const KeymasterContext & context,const Keymaster1Engine * engine)30 EcdsaKeymaster1KeyFactory::EcdsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker& blob_maker,
31                                                      const KeymasterContext& context,
32                                                      const Keymaster1Engine* engine)
33     : EcKeyFactory(blob_maker, context), engine_(engine),
34       sign_factory_(new EcdsaKeymaster1OperationFactory(KM_PURPOSE_SIGN, engine)),
35       // For pubkey ops we can use the normal operation factories.
36       verify_factory_(new EcdsaVerifyOperationFactory) {}
37 
is_supported(uint32_t digest)38 static bool is_supported(uint32_t digest) {
39     return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
40 }
41 
UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet & key_description,AuthorizationSet * new_description)42 static void UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet& key_description,
43                                                  AuthorizationSet* new_description) {
44     bool have_unsupported_digests = false;
45     bool have_digest_none = false;
46     for (const keymaster_key_param_t& entry : key_description) {
47         new_description->push_back(entry);
48 
49         if (entry.tag == TAG_DIGEST) {
50             if (entry.enumerated == KM_DIGEST_NONE) {
51                 have_digest_none = true;
52             } else if (!is_supported(entry.enumerated)) {
53                 LOG_D("Found request for unsupported digest %u", entry.enumerated);
54                 have_unsupported_digests = true;
55             }
56         }
57     }
58 
59     if (have_unsupported_digests && !have_digest_none) {
60         LOG_I("Adding KM_DIGEST_NONE to key authorization, to enable software digesting", 0);
61         new_description->push_back(TAG_DIGEST, KM_DIGEST_NONE);
62     }
63 }
64 
GenerateKey(const AuthorizationSet & key_description,UniquePtr<Key>,const KeymasterBlob &,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced,CertificateChain *) const65 keymaster_error_t EcdsaKeymaster1KeyFactory::GenerateKey(const AuthorizationSet& key_description,
66                                                          UniquePtr<Key> /* attest_key */,
67                                                          const KeymasterBlob& /* issuer_subject */,
68                                                          KeymasterKeyBlob* key_blob,
69                                                          AuthorizationSet* hw_enforced,
70                                                          AuthorizationSet* sw_enforced,
71                                                          CertificateChain* /* cert_chain */) const {
72     AuthorizationSet key_params_copy;
73     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
74 
75     keymaster_ec_curve_t ec_curve;
76     uint32_t key_size;
77     keymaster_error_t error = GetCurveAndSize(key_description, &ec_curve, &key_size);
78     if (error != KM_ERROR_OK) {
79         return error;
80     } else if (!key_description.Contains(TAG_KEY_SIZE, key_size)) {
81         key_params_copy.push_back(TAG_KEY_SIZE, key_size);
82     }
83     return engine_->GenerateKey(key_params_copy, key_blob, hw_enforced, sw_enforced);
84 }
85 
86 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 *) const87 EcdsaKeymaster1KeyFactory::ImportKey(const AuthorizationSet& key_description,           //
88                                      keymaster_key_format_t input_key_material_format,  //
89                                      const KeymasterKeyBlob& input_key_material,        //
90                                      UniquePtr<Key> /* attest_key */,                   //
91                                      const KeymasterBlob& /* issuer_subject */,
92                                      KeymasterKeyBlob* output_key_blob,  //
93                                      AuthorizationSet* hw_enforced,      //
94                                      AuthorizationSet* sw_enforced,
95                                      CertificateChain* /* cert_chain */) const {
96     AuthorizationSet key_params_copy;
97     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
98     return engine_->ImportKey(key_params_copy, input_key_material_format, input_key_material,
99                               output_key_blob, hw_enforced, sw_enforced);
100 }
101 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet & additional_params,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const102 keymaster_error_t EcdsaKeymaster1KeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
103                                                      const AuthorizationSet& additional_params,
104                                                      AuthorizationSet&& hw_enforced,
105                                                      AuthorizationSet&& sw_enforced,
106                                                      UniquePtr<Key>* key) const {
107     if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
108 
109     keymaster_error_t error;
110     unique_ptr<EC_KEY, EC_KEY_Delete> ecdsa(
111         engine_->BuildEcKey(key_material, additional_params, &error));
112     if (!ecdsa) return error;
113 
114     key->reset(new (std::nothrow)
115                    EcdsaKeymaster1Key(ecdsa.release(), move(hw_enforced), move(sw_enforced), this));
116     if (!(*key)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
117 
118     (*key)->key_material() = move(key_material);
119     return KM_ERROR_OK;
120 }
121 
122 OperationFactory*
GetOperationFactory(keymaster_purpose_t purpose) const123 EcdsaKeymaster1KeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
124     switch (purpose) {
125     case KM_PURPOSE_SIGN:
126         return sign_factory_.get();
127     case KM_PURPOSE_VERIFY:
128         return verify_factory_.get();
129     default:
130         return nullptr;
131     }
132 }
133 
134 }  // namespace keymaster
135