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 Keymaster1Engine * engine)27 RsaKeymaster1KeyFactory::RsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker* blob_maker,
28                                                  const Keymaster1Engine* engine)
29     : RsaKeyFactory(blob_maker), engine_(engine),
30       sign_factory_(new RsaKeymaster1OperationFactory(KM_PURPOSE_SIGN, engine)),
31       decrypt_factory_(new RsaKeymaster1OperationFactory(KM_PURPOSE_DECRYPT, engine)),
32       // For pubkey ops we can use the normal operation factories.
33       verify_factory_(new RsaVerificationOperationFactory),
34       encrypt_factory_(new RsaEncryptionOperationFactory) {}
35 
is_supported(uint32_t digest)36 static bool is_supported(uint32_t digest) {
37     return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
38 }
39 
UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet & key_description,AuthorizationSet * new_description)40 static void UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet& key_description,
41                                                  AuthorizationSet* new_description) {
42     bool have_unsupported_digests = false;
43     bool have_digest_none = false;
44     bool have_pad_none = false;
45     bool have_padding_requiring_digest = 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         if (entry.tag == TAG_PADDING) {
59             switch (entry.enumerated) {
60             case KM_PAD_RSA_PSS:
61             case KM_PAD_RSA_OAEP:
62                 have_padding_requiring_digest = true;
63                 break;
64             case KM_PAD_NONE:
65                 have_pad_none = true;
66                 break;
67             }
68         }
69     }
70 
71     if (have_unsupported_digests && !have_digest_none) {
72         LOG_I("Adding KM_DIGEST_NONE to key authorization, to enable software digesting", 0);
73         new_description->push_back(TAG_DIGEST, KM_DIGEST_NONE);
74     }
75 
76     if (have_unsupported_digests && have_padding_requiring_digest && !have_pad_none) {
77         LOG_I("Adding KM_PAD_NONE to key authorization, to enable PSS or OAEP software padding", 0);
78         new_description->push_back(TAG_PADDING, KM_PAD_NONE);
79     }
80 }
81 
GenerateKey(const AuthorizationSet & key_description,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const82 keymaster_error_t RsaKeymaster1KeyFactory::GenerateKey(const AuthorizationSet& key_description,
83                                                        KeymasterKeyBlob* key_blob,
84                                                        AuthorizationSet* hw_enforced,
85                                                        AuthorizationSet* sw_enforced) const {
86     AuthorizationSet key_params_copy;
87     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
88     return engine_->GenerateKey(key_params_copy, key_blob, hw_enforced, sw_enforced);
89 }
90 
ImportKey(const AuthorizationSet & key_description,keymaster_key_format_t input_key_material_format,const KeymasterKeyBlob & input_key_material,KeymasterKeyBlob * output_key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced) const91 keymaster_error_t RsaKeymaster1KeyFactory::ImportKey(
92     const AuthorizationSet& key_description, keymaster_key_format_t input_key_material_format,
93     const KeymasterKeyBlob& input_key_material, KeymasterKeyBlob* output_key_blob,
94     AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
95     AuthorizationSet key_params_copy;
96     UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
97     return engine_->ImportKey(key_params_copy, input_key_material_format, input_key_material,
98                               output_key_blob, hw_enforced, sw_enforced);
99 }
100 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet & additional_params,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const101 keymaster_error_t RsaKeymaster1KeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
102                                                    const AuthorizationSet& additional_params,
103                                                    AuthorizationSet&& hw_enforced,
104                                                    AuthorizationSet&& sw_enforced,
105                                                    UniquePtr<Key>* key) const {
106     if (!key)
107         return KM_ERROR_OUTPUT_PARAMETER_NULL;
108 
109     keymaster_error_t error;
110     RSA_Ptr rsa(engine_->BuildRsaKey(key_material, additional_params, &error));
111     if (!rsa.get())
112         return error;
113 
114     key->reset(new (std::nothrow)
115                    RsaKeymaster1Key(rsa.release(), move(hw_enforced), move(sw_enforced), this));
116     if (!(*key))
117         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
118 
119     (*key)->key_material() = move(key_material);
120     return KM_ERROR_OK;
121 }
122 
GetOperationFactory(keymaster_purpose_t purpose) const123 OperationFactory* RsaKeymaster1KeyFactory::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     case KM_PURPOSE_ENCRYPT:
130         return encrypt_factory_.get();
131     case KM_PURPOSE_DECRYPT:
132         return decrypt_factory_.get();
133     case KM_PURPOSE_DERIVE_KEY:
134     case KM_PURPOSE_WRAP:
135         break;
136     }
137     return nullptr;
138 }
139 
140 }  // namespace keymaster
141