• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2019 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <base/logging.h>
20 #include <keystore/keystore_client_impl.h>
21 #include <mutex>
22 
23 #include "osi/include/alarm.h"
24 #include "osi/include/allocator.h"
25 #include "osi/include/compat.h"
26 #include "osi/include/config.h"
27 #include "osi/include/log.h"
28 #include "osi/include/osi.h"
29 #include "osi/include/properties.h"
30 
31 namespace bluetooth {
32 /**
33  * Client wrapper to access AndroidKeystore.
34  *
35  * <p>Use to encrypt/decrypt data and store to disk.
36  */
37 class BtifKeystore {
38  public:
39   /**
40    * @param keystore_client injected pre-created client object for keystore
41    */
42   BtifKeystore(keystore::KeystoreClient* keystore_client);
43 
44   /**
45    * Encrypts given data
46    *
47    * <p>Returns a string representation of the encrypted data
48    *
49    * @param data to be encrypted
50    * @param flags for keystore
51    */
52   std::string Encrypt(const std::string& data, int32_t flags);
53 
54   /**
55    * Returns a decrypted string representation of the encrypted data or empty
56    * string on error.
57    *
58    * @param input encrypted data
59    */
60   std::string Decrypt(const std::string& input_filename);
61 
62   /**
63    * Check for existence of keystore key.
64    *
65    * This key can be cleared if a user manually wipes bluetooth storage data
66    * b/133214365
67    */
68   bool DoesKeyExist();
69 
70  private:
71   std::unique_ptr<keystore::KeystoreClient> keystore_client_;
72   std::mutex api_mutex_;
73   keystore::KeyStoreNativeReturnCode GenerateKey(const std::string& name,
74                                                  int32_t flags,
75                                                  bool auth_bound);
76 };
77 
78 }  // namespace bluetooth
79