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/contexts/soft_keymaster_context.h>
18 #include <keymaster/legacy_support/rsa_keymaster1_key.h>
19 
20 #include <keymaster/km_openssl/openssl_utils.h>
21 #include <keymaster/logger.h>
22 
23 #include "rsa_keymaster1_operation.h"
24 
25 namespace keymaster {
26 
RsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker & blob_maker,const KeymasterContext & context,const Keymaster1Engine * engine)27 RsaKeymaster1KeyFactory::RsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker& blob_maker,
28                                                  const KeymasterContext& context,
29                                                  const Keymaster1Engine* engine)
30     : RsaKeyFactory(blob_maker, context), engine_(engine),
31       sign_factory_(new RsaKeymaster1OperationFactory(KM_PURPOSE_SIGN, engine)),
32       decrypt_factory_(new RsaKeymaster1OperationFactory(KM_PURPOSE_DECRYPT, engine)),
33       // For pubkey ops we can use the normal operation factories.
34       verify_factory_(new RsaVerificationOperationFactory),
35       encrypt_factory_(new RsaEncryptionOperationFactory) {}
36 
is_supported(uint32_t digest)37 static bool is_supported(uint32_t digest) {
38     return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
39 }
40 
UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet & key_description,AuthorizationSet * new_description)41 static void UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet& key_description,
42                                                  AuthorizationSet* new_description) {
43     bool have_unsupported_digests = false;
44     bool have_digest_none = false;
45     bool have_pad_none = false;
46     bool have_padding_requiring_digest = 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         if (entry.tag == TAG_PADDING) {
60             switch (entry.enumerated) {
61             case KM_PAD_RSA_PSS:
62             case KM_PAD_RSA_OAEP:
63                 have_padding_requiring_digest = true;
64                 break;
65             case KM_PAD_NONE:
66                 have_pad_none = true;
67                 break;
68             }
69         }
70     }
71 
72     if (have_unsupported_digests && !have_digest_none) {
73         LOG_I("Adding KM_DIGEST_NONE to key authorization, to enable software digesting", 0);
74         new_description->push_back(TAG_DIGEST, KM_DIGEST_NONE);
75     }
76 
77     if (have_unsupported_digests && have_padding_requiring_digest && !have_pad_none) {
78         LOG_I("Adding KM_PAD_NONE to key authorization, to enable PSS or OAEP software padding", 0);
79         new_description->push_back(TAG_PADDING, KM_PAD_NONE);
80     }
81 }
82 
GenerateKey(const AuthorizationSet & key_description,UniquePtr<Key>,const KeymasterBlob &,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced,CertificateChain *) const83 keymaster_error_t RsaKeymaster1KeyFactory::GenerateKey(const AuthorizationSet& key_description,
84                                                        UniquePtr<Key> /* attest_key */,
85                                                        const KeymasterBlob& /* issuer_subject */,
86                                                        KeymasterKeyBlob* key_blob,
87                                                        AuthorizationSet* hw_enforced,
88                                                        AuthorizationSet* sw_enforced,
89                                                        CertificateChain* /* cert_chain */) const {
90     AuthorizationSet key_params_copy;
91     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
92     return engine_->GenerateKey(key_params_copy, key_blob, hw_enforced, sw_enforced);
93 }
94 
95 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 *) const96 RsaKeymaster1KeyFactory::ImportKey(const AuthorizationSet& key_description,
97                                    keymaster_key_format_t input_key_material_format,
98                                    const KeymasterKeyBlob& input_key_material,
99                                    UniquePtr<Key> /* attest_key */,
100                                    const KeymasterBlob& /* issuer_subject */,
101                                    KeymasterKeyBlob* output_key_blob,  //
102                                    AuthorizationSet* hw_enforced,      //
103                                    AuthorizationSet* sw_enforced,
104                                    CertificateChain* /* cert_chain */) const {
105     AuthorizationSet key_params_copy;
106     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
107     return engine_->ImportKey(key_params_copy, input_key_material_format, input_key_material,
108                               output_key_blob, hw_enforced, sw_enforced);
109 }
110 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet & additional_params,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const111 keymaster_error_t RsaKeymaster1KeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
112                                                    const AuthorizationSet& additional_params,
113                                                    AuthorizationSet&& hw_enforced,
114                                                    AuthorizationSet&& sw_enforced,
115                                                    UniquePtr<Key>* key) const {
116     if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
117 
118     keymaster_error_t error;
119     RSA_Ptr rsa(engine_->BuildRsaKey(key_material, additional_params, &error));
120     if (!rsa.get()) return error;
121 
122     key->reset(new (std::nothrow)
123                    RsaKeymaster1Key(rsa.release(), move(hw_enforced), move(sw_enforced), this));
124     if (!(*key)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
125 
126     (*key)->key_material() = move(key_material);
127     return KM_ERROR_OK;
128 }
129 
GetOperationFactory(keymaster_purpose_t purpose) const130 OperationFactory* RsaKeymaster1KeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
131     switch (purpose) {
132     case KM_PURPOSE_SIGN:
133         return sign_factory_.get();
134     case KM_PURPOSE_VERIFY:
135         return verify_factory_.get();
136     case KM_PURPOSE_ENCRYPT:
137         return encrypt_factory_.get();
138     case KM_PURPOSE_DECRYPT:
139         return decrypt_factory_.get();
140     case KM_PURPOSE_DERIVE_KEY:
141     case KM_PURPOSE_WRAP:
142     case KM_PURPOSE_AGREE_KEY:
143     case KM_PURPOSE_ATTEST_KEY:
144         break;
145     }
146     return nullptr;
147 }
148 
149 }  // namespace keymaster
150