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 #pragma once
18 
19 #include <utility>
20 
21 #include <openssl/ecdsa.h>
22 
23 #include <hardware/keymaster1.h>
24 #include <keymaster/android_keymaster_utils.h>
25 #include <keymaster/contexts/soft_keymaster_context.h>
26 #include <keymaster/km_openssl/ec_key.h>
27 #include <keymaster/km_openssl/ec_key_factory.h>
28 #include <keymaster/logger.h>
29 
30 #include "keymaster1_engine.h"
31 
32 namespace keymaster {
33 
34 /**
35  * EcdsaKeymaster1KeyFactory is a KeyFactory that creates and loads keys which are actually backed
36  * by a hardware keymaster1 module, but which does not support all keymaster1 digests.  During
37  * generation or import any unsupported digests in the key description are silently replaced with
38  * KM_DIGEST_NONE.
39  */
40 class EcdsaKeymaster1KeyFactory : public EcKeyFactory {
41   public:
42     EcdsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker& blob_maker,
43                               const KeymasterContext& context,  //
44                               const Keymaster1Engine* engine);
45 
46     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
47                                   UniquePtr<Key> attest_key,            //
48                                   const KeymasterBlob& issuer_subject,  //
49                                   KeymasterKeyBlob* key_blob,           //
50                                   AuthorizationSet* hw_enforced,        //
51                                   AuthorizationSet* sw_enforced,
52                                   CertificateChain* cert_chain) const override;
53 
54     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
55                                 keymaster_key_format_t input_key_material_format,
56                                 const KeymasterKeyBlob& input_key_material,
57                                 UniquePtr<Key> attest_key,  //
58                                 const KeymasterBlob& issuer_subject,
59                                 KeymasterKeyBlob* output_key_blob,  //
60                                 AuthorizationSet* hw_enforced,      //
61                                 AuthorizationSet* sw_enforced,
62                                 CertificateChain* cert_chain) const override;
63 
64     keymaster_error_t LoadKey(KeymasterKeyBlob&& key_material,
65                               const AuthorizationSet& additional_params,
66                               AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
67                               UniquePtr<Key>* key) const override;
68 
69     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
70 
71   private:
72     const Keymaster1Engine* engine_;
73 
74     std::unique_ptr<OperationFactory> sign_factory_;
75     std::unique_ptr<OperationFactory> verify_factory_;
76 };
77 
78 class EcdsaKeymaster1Key : public EcKey {
79   public:
EcdsaKeymaster1Key(EC_KEY * ecdsa_key,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,const KeyFactory * key_factory)80     EcdsaKeymaster1Key(EC_KEY* ecdsa_key, AuthorizationSet&& hw_enforced,
81                        AuthorizationSet&& sw_enforced, const KeyFactory* key_factory)
82         : EcKey(ecdsa_key, std::move(hw_enforced), std::move(sw_enforced), key_factory) {}
83 };
84 
85 }  // namespace keymaster
86