1 // Copyright 2015 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef KEYSTORE_KEYSTORE_CLIENT_H_ 16 #define KEYSTORE_KEYSTORE_CLIENT_H_ 17 18 #include <set> 19 #include <string> 20 #include <vector> 21 22 #include <android-base/macros.h> 23 24 #include "keymaster_types.h" 25 #include "keystore.h" 26 #include "keystore_return_types.h" 27 28 namespace keystore { 29 30 // An abstract class providing a convenient interface to keystore services. This 31 // interface is designed to: 32 // - hide details of the IPC mechanism (e.g. binder) 33 // - use std data types 34 // - encourage the use of keystore::AuthorizationSet[Builder] 35 // - be convenient for native services integrating with keystore 36 // - be safely mocked for unit testing (e.g. pure virtual methods) 37 // 38 // Example usage: 39 // KeystoreClient* keystore = new KeyStoreClientImpl(); 40 // keystore->AddRandomNumberGeneratorEntropy("unpredictable"); 41 // 42 // Notes on error codes: 43 // Keystore binder methods return a variety of values including ResponseCode 44 // values defined in keystore.h, keymaster_error_t values defined in 45 // keymaster_defs.h, or just 0 or -1 (both of which conflict with 46 // keymaster_error_t). The methods in this class converge on a single success 47 // indicator for convenience. KM_ERROR_OK was chosen over ::NO_ERROR for two 48 // reasons: 49 // 1) KM_ERROR_OK is 0, which is a common convention for success, is the gmock 50 // default, and allows error checks like 'if (error) {...'. 51 // 2) Although both pollute the global namespace, KM_ERROR_OK has a prefix per 52 // C convention and hopefully clients can use this interface without 53 // needing to include 'keystore.h' directly. 54 class KeystoreClient { 55 public: 56 KeystoreClient() = default; 57 virtual ~KeystoreClient() = default; 58 59 // Encrypts and authenticates |data| with minimal configuration for local 60 // decryption. If a key identified by |key_name| does not already exist it 61 // will be generated. On success returns true and populates |encrypted_data|. 62 // Note: implementations may generate more than one key but they will always 63 // have |key_name| as a prefix. 64 virtual bool encryptWithAuthentication(const std::string& key_name, const std::string& data, 65 int32_t flags, std::string* encrypted_data) = 0; 66 67 // Decrypts and authenticates |encrypted_data| as output by 68 // EncryptWithAuthentication using the key(s) identified by |key_name|. On 69 // success returns true and populates |data|. 70 virtual bool decryptWithAuthentication(const std::string& key_name, 71 const std::string& encrypted_data, 72 std::string* data) = 0; 73 74 // Performs a Begin/Update/Finish sequence for an operation. The |purpose|, 75 // |key_name|, |input_parameters|, and |output_parameters| are as in 76 // BeginOperation. The |input_data| is as in UpdateOperation. The 77 // |signature_to_verify| and |output_data| are as in FinishOperation. On 78 // success returns true. 79 virtual bool oneShotOperation(KeyPurpose purpose, const std::string& key_name, 80 const keystore::AuthorizationSet& input_parameters, 81 const std::string& input_data, 82 const std::string& signature_to_verify, 83 keystore::AuthorizationSet* output_parameters, 84 std::string* output_data) = 0; 85 86 // Adds |entropy| to the random number generator. Returns KM_ERROR_OK on 87 // success and a Keystore ResponseCode or keymaster_error_t on failure. 88 virtual KeyStoreNativeReturnCode addRandomNumberGeneratorEntropy(const std::string& entropy, 89 int32_t flags) = 0; 90 91 // Generates a key according to the given |key_parameters| and stores it with 92 // the given |key_name|. The [hardware|software]_enforced_characteristics of 93 // the key are provided on success. Returns KM_ERROR_OK on success. Returns 94 // KM_ERROR_OK on success and a Keystore ResponseCode or keymaster_error_t on 95 // failure. 96 virtual KeyStoreNativeReturnCode 97 generateKey(const std::string& key_name, const keystore::AuthorizationSet& key_parameters, 98 int32_t flags, keystore::AuthorizationSet* hardware_enforced_characteristics, 99 keystore::AuthorizationSet* software_enforced_characteristics) = 0; 100 101 // Provides the [hardware|software]_enforced_characteristics of a key 102 // identified by |key_name|. Returns KM_ERROR_OK on success and a Keystore 103 // ResponseCode or keymaster_error_t on failure. 104 virtual KeyStoreNativeReturnCode 105 getKeyCharacteristics(const std::string& key_name, 106 keystore::AuthorizationSet* hardware_enforced_characteristics, 107 keystore::AuthorizationSet* software_enforced_characteristics) = 0; 108 109 // Imports |key_data| in the given |key_format|, applies the given 110 // |key_parameters|, and stores it with the given |key_name|. The 111 // [hardware|software]_enforced_characteristics of the key are provided on 112 // success. Returns KM_ERROR_OK on success and a Keystore ResponseCode or 113 // keymaster_error_t on failure. 114 virtual KeyStoreNativeReturnCode 115 importKey(const std::string& key_name, const keystore::AuthorizationSet& key_parameters, 116 KeyFormat key_format, const std::string& key_data, 117 keystore::AuthorizationSet* hardware_enforced_characteristics, 118 keystore::AuthorizationSet* software_enforced_characteristics) = 0; 119 120 // Exports the public key identified by |key_name| to |export_data| using 121 // |export_format|. Returns KM_ERROR_OK on success and a Keystore ResponseCode 122 // or keymaster_error_t on failure. 123 virtual KeyStoreNativeReturnCode exportKey(KeyFormat export_format, const std::string& key_name, 124 std::string* export_data) = 0; 125 126 // Deletes the key identified by |key_name|. Returns KM_ERROR_OK on success 127 // and a Keystore ResponseCode or keymaster_error_t on failure. 128 virtual KeyStoreNativeReturnCode deleteKey(const std::string& key_name) = 0; 129 130 // Deletes all keys owned by the caller. Returns KM_ERROR_OK on success and a 131 // Keystore ResponseCode or keymaster_error_t on failure. 132 virtual KeyStoreNativeReturnCode deleteAllKeys() = 0; 133 134 // Begins a cryptographic operation (e.g. encrypt, sign) identified by 135 // |purpose| using the key identified by |key_name| and the given 136 // |input_parameters|. On success, any |output_parameters| and an operation 137 // |handle| are populated. Returns KM_ERROR_OK on success and a Keystore 138 // ResponseCode or keymaster_error_t on failure. 139 virtual KeyStoreNativeReturnCode 140 beginOperation(KeyPurpose purpose, const std::string& key_name, 141 const keystore::AuthorizationSet& input_parameters, 142 keystore::AuthorizationSet* output_parameters, uint64_t* handle) = 0; 143 144 // Continues the operation associated with |handle| using the given 145 // |input_parameters| and |input_data|. On success, the 146 // |num_input_bytes_consumed| and any |output_parameters| are populated. Any 147 // |output_data| will be appended. Returns KM_ERROR_OK on success and a 148 // Keystore ResponseCode or keymaster_error_t on failure. 149 virtual KeyStoreNativeReturnCode 150 updateOperation(uint64_t handle, const keystore::AuthorizationSet& input_parameters, 151 const std::string& input_data, size_t* num_input_bytes_consumed, 152 keystore::AuthorizationSet* output_parameters, std::string* output_data) = 0; 153 154 // Finishes the operation associated with |handle| using the given 155 // |input_parameters| and, if necessary, a |signature_to_verify|. On success, 156 // any |output_parameters| are populated and |output_data| is appended. 157 // Returns KM_ERROR_OK on success and a Keystore ResponseCode or 158 // keymaster_error_t on failure. 159 virtual KeyStoreNativeReturnCode 160 finishOperation(uint64_t handle, const keystore::AuthorizationSet& input_parameters, 161 const std::string& signature_to_verify, 162 keystore::AuthorizationSet* output_parameters, std::string* output_data) = 0; 163 164 // Aborts the operation associated with |handle|. Returns KM_ERROR_OK on 165 // success and a Keystore ResponseCode or keymaster_error_t on failure. 166 virtual KeyStoreNativeReturnCode abortOperation(uint64_t handle) = 0; 167 168 // Returns true if a key identified by |key_name| exists in the caller's 169 // key store. Returns false if an error occurs. 170 virtual bool doesKeyExist(const std::string& key_name) = 0; 171 172 // Provides a |key_name_list| containing all existing key names in the 173 // caller's key store starting with |prefix|. Returns true on success. 174 virtual bool listKeys(const std::string& prefix, std::vector<std::string>* key_name_list) = 0; 175 176 private: 177 DISALLOW_COPY_AND_ASSIGN(KeystoreClient); 178 }; 179 180 } // namespace keystore 181 182 #endif // KEYSTORE_KEYSTORE_CLIENT_H_ 183