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