• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "authorization_set.h"
23 #include "keystore.h"
24 #include "keystore_return_types.h"
25 
26 namespace keystore {
27 
28 
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                                            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) = 0;
89 
90     // Generates a key according to the given |key_parameters| and stores it with
91     // the given |key_name|. The [hardware|software]_enforced_characteristics of
92     // the key are provided on success. Returns KM_ERROR_OK on success. Returns
93     // KM_ERROR_OK on success and a Keystore ResponseCode or keymaster_error_t on
94     // failure.
95     virtual KeyStoreNativeReturnCode generateKey(const std::string& key_name,
96                                 const keystore::AuthorizationSet& key_parameters,
97                                 keystore::AuthorizationSet* hardware_enforced_characteristics,
98                                 keystore::AuthorizationSet* software_enforced_characteristics) = 0;
99 
100     // Provides the [hardware|software]_enforced_characteristics of a key
101     // identified by |key_name|. Returns KM_ERROR_OK on success and a Keystore
102     // ResponseCode or keymaster_error_t on failure.
103     virtual KeyStoreNativeReturnCode
104     getKeyCharacteristics(const std::string& key_name,
105                           keystore::AuthorizationSet* hardware_enforced_characteristics,
106                           keystore::AuthorizationSet* software_enforced_characteristics) = 0;
107 
108     // Imports |key_data| in the given |key_format|, applies the given
109     // |key_parameters|, and stores it with the given |key_name|. The
110     // [hardware|software]_enforced_characteristics of the key are provided on
111     // success. Returns KM_ERROR_OK on success and a Keystore ResponseCode or
112     // keymaster_error_t on failure.
113     virtual KeyStoreNativeReturnCode importKey(const std::string& key_name,
114                               const keystore::AuthorizationSet& key_parameters,
115                               KeyFormat key_format, const std::string& key_data,
116                               keystore::AuthorizationSet* hardware_enforced_characteristics,
117                               keystore::AuthorizationSet* software_enforced_characteristics) = 0;
118 
119     // Exports the public key identified by |key_name| to |export_data| using
120     // |export_format|. Returns KM_ERROR_OK on success and a Keystore ResponseCode
121     // or keymaster_error_t on failure.
122     virtual KeyStoreNativeReturnCode exportKey(KeyFormat export_format, const std::string& key_name,
123                               std::string* export_data) = 0;
124 
125     // Deletes the key identified by |key_name|. Returns KM_ERROR_OK on success
126     // and a Keystore ResponseCode or keymaster_error_t on failure.
127     virtual KeyStoreNativeReturnCode deleteKey(const std::string& key_name) = 0;
128 
129     // Deletes all keys owned by the caller. Returns KM_ERROR_OK on success and a
130     // Keystore ResponseCode or keymaster_error_t on failure.
131     virtual KeyStoreNativeReturnCode deleteAllKeys() = 0;
132 
133     // Begins a cryptographic operation (e.g. encrypt, sign) identified by
134     // |purpose| using the key identified by |key_name| and the given
135     // |input_parameters|. On success, any |output_parameters| and an operation
136     // |handle| are populated. Returns KM_ERROR_OK on success and a Keystore
137     // ResponseCode or keymaster_error_t on failure.
138     virtual KeyStoreNativeReturnCode beginOperation(KeyPurpose purpose, const std::string& key_name,
139                                    const keystore::AuthorizationSet& input_parameters,
140                                    keystore::AuthorizationSet* output_parameters,
141                                    uint64_t* handle) = 0;
142 
143     // Continues the operation associated with |handle| using the given
144     // |input_parameters| and |input_data|. On success, the
145     // |num_input_bytes_consumed| and any |output_parameters| are populated. Any
146     // |output_data| will be appended. Returns KM_ERROR_OK on success and a
147     // Keystore ResponseCode or keymaster_error_t on failure.
148     virtual KeyStoreNativeReturnCode updateOperation(uint64_t handle,
149                                     const keystore::AuthorizationSet& input_parameters,
150                                     const std::string& input_data, size_t* num_input_bytes_consumed,
151                                     keystore::AuthorizationSet* output_parameters,
152                                     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 finishOperation(uint64_t handle,
160                                     const keystore::AuthorizationSet& input_parameters,
161                                     const std::string& signature_to_verify,
162                                     keystore::AuthorizationSet* output_parameters,
163                                     std::string* output_data) = 0;
164 
165     // Aborts the operation associated with |handle|. Returns KM_ERROR_OK on
166     // success and a Keystore ResponseCode or keymaster_error_t on failure.
167     virtual KeyStoreNativeReturnCode abortOperation(uint64_t handle) = 0;
168 
169     // Returns true if a key identified by |key_name| exists in the caller's
170     // key store. Returns false if an error occurs.
171     virtual bool doesKeyExist(const std::string& key_name) = 0;
172 
173     // Provides a |key_name_list| containing all existing key names in the
174     // caller's key store starting with |prefix|. Returns true on success.
175     virtual bool listKeys(const std::string& prefix, std::vector<std::string>* key_name_list) = 0;
176 
177   private:
178     DISALLOW_COPY_AND_ASSIGN(KeystoreClient);
179 };
180 
181 }  // namespace keystore
182 
183 #endif  // KEYSTORE_KEYSTORE_CLIENT_H_
184