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 #ifndef SYSTEM_KEYMASTER_RSA_KEYMASTER1_KEY_H_
18 #define SYSTEM_KEYMASTER_RSA_KEYMASTER1_KEY_H_
19 
20 #include <openssl/rsa.h>
21 
22 #include <keymaster/km_openssl/rsa_key.h>
23 #include <keymaster/km_openssl/rsa_key_factory.h>
24 #include <keymaster/operation.h>
25 
26 #include "keymaster1_engine.h"
27 
28 namespace keymaster {
29 
30 /**
31  * RsaKeymaster1KeyFactory is a KeyFactory that creates and loads keys which are actually backed by
32  * a hardware keymaster1 module, but which does not support all keymaster1 digests.  If unsupported
33  * digests are found during generation or import, KM_DIGEST_NONE is added to the key description,
34  * then the operations handle the unsupported digests in software.
35  *
36  * If unsupported digests are requested and KM_PAD_RSA_PSS or KM_PAD_RSA_OAEP is also requested, but
37  * KM_PAD_NONE is not present KM_PAD_NONE will be added to the description, to allow for
38  * software padding as well as software digesting.
39  */
40 class RsaKeymaster1KeyFactory : public RsaKeyFactory {
41   public:
42     RsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker* blob_maker,
43                             const Keymaster1Engine* engine);
44 
45     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
46                                   KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
47                                   AuthorizationSet* sw_enforced) const override;
48 
49     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
50                                 keymaster_key_format_t input_key_material_format,
51                                 const KeymasterKeyBlob& input_key_material,
52                                 KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
53                                 AuthorizationSet* sw_enforced) const override;
54 
55     keymaster_error_t LoadKey(KeymasterKeyBlob&& key_material,
56                               const AuthorizationSet& additional_params,
57                               AuthorizationSet&& hw_enforced,
58                               AuthorizationSet&& sw_enforced,
59                               UniquePtr<Key>* key) const override;
60 
61     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
62 
63   private:
64     const Keymaster1Engine* engine_;
65 
66     std::unique_ptr<OperationFactory> sign_factory_;
67     std::unique_ptr<OperationFactory> decrypt_factory_;
68     std::unique_ptr<OperationFactory> verify_factory_;
69     std::unique_ptr<OperationFactory> encrypt_factory_;
70 };
71 
72 class RsaKeymaster1Key : public RsaKey {
73   public:
RsaKeymaster1Key(RSA * rsa_key,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,const KeyFactory * key_factory)74     RsaKeymaster1Key(RSA* rsa_key, AuthorizationSet&& hw_enforced,
75                      AuthorizationSet&& sw_enforced,
76                      const KeyFactory* key_factory)
77         : RsaKey(rsa_key, move(hw_enforced), move(sw_enforced), key_factory) {}
78 };
79 
80 }  // namespace keymaster
81 
82 #endif  // SYSTEM_KEYMASTER_RSA_KEYMASTER1_KEY_H_
83