1 /*
2  * Copyright 2014 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_ANDROID_KEYMASTER_H_
18 #define SYSTEM_KEYMASTER_ANDROID_KEYMASTER_H_
19 
20 #include <keymaster/android_keymaster_messages.h>
21 #include <keymaster/authorization_set.h>
22 
23 namespace keymaster {
24 
25 class Key;
26 class KeyFactory;
27 class KeymasterContext;
28 class OperationTable;
29 
30 /**
31  * This is the reference implementation of Keymaster.  In addition to acting as a reference for
32  * other Keymaster implementers to check their assumptions against, it is used by Keystore as the
33  * default implementation when no secure implementation is available, and may be installed and
34  * executed in secure hardware as a secure implementation.
35  *
36  * Note that this class doesn't actually implement the Keymaster HAL interface, instead it
37  * implements an alternative API which is similar to and based upon the HAL, but uses C++ "message"
38  * classes which support serialization.
39  *
40  * For non-secure, pure software implementation there is a HAL translation layer that converts the
41  * HAL's parameters to and from the message representations, which are then passed in to this
42  * API.
43  *
44  * For secure implementation there is another HAL translation layer that serializes the messages to
45  * the TEE. In the TEE implementation there's another component which deserializes the messages,
46  * extracts the relevant parameters and calls this API.
47  */
48 class AndroidKeymaster {
49   public:
50     AndroidKeymaster(KeymasterContext* context, size_t operation_table_size);
51     virtual ~AndroidKeymaster();
52 
53     void GetVersion(const GetVersionRequest& request, GetVersionResponse* response);
54     void SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
55                              SupportedAlgorithmsResponse* response);
56     void SupportedBlockModes(const SupportedBlockModesRequest& request,
57                              SupportedBlockModesResponse* response);
58     void SupportedPaddingModes(const SupportedPaddingModesRequest& request,
59                                SupportedPaddingModesResponse* response);
60     void SupportedDigests(const SupportedDigestsRequest& request,
61                           SupportedDigestsResponse* response);
62     void SupportedImportFormats(const SupportedImportFormatsRequest& request,
63                                 SupportedImportFormatsResponse* response);
64     void SupportedExportFormats(const SupportedExportFormatsRequest& request,
65                                 SupportedExportFormatsResponse* response);
66 
67     void AddRngEntropy(const AddEntropyRequest& request, AddEntropyResponse* response);
68     void GenerateKey(const GenerateKeyRequest& request, GenerateKeyResponse* response);
69     void GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
70                                GetKeyCharacteristicsResponse* response);
71     void ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response);
72     void ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response);
73     void AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response);
74     void UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response);
75     void DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response);
76     void DeleteAllKeys(const DeleteAllKeysRequest& request, DeleteAllKeysResponse* response);
77     void BeginOperation(const BeginOperationRequest& request, BeginOperationResponse* response);
78     void UpdateOperation(const UpdateOperationRequest& request, UpdateOperationResponse* response);
79     void FinishOperation(const FinishOperationRequest& request, FinishOperationResponse* response);
80     void AbortOperation(const AbortOperationRequest& request, AbortOperationResponse* response);
81 
82     bool has_operation(keymaster_operation_handle_t op_handle) const;
83 
84   private:
85     keymaster_error_t LoadKey(const keymaster_key_blob_t& key_blob,
86                               const AuthorizationSet& additional_params,
87                               AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced,
88                               const KeyFactory** factory, UniquePtr<Key>* key);
89 
90     UniquePtr<KeymasterContext> context_;
91     UniquePtr<OperationTable> operation_table_;
92 };
93 
94 }  // namespace keymaster
95 
96 #endif  //  SYSTEM_KEYMASTER_ANDROID_KEYMASTER_H_
97