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