KeyStorage.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2016 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. #ifndef ANDROID_VOLD_KEYSTORAGE_H
  17. #define ANDROID_VOLD_KEYSTORAGE_H
  18. #include "KeyBuffer.h"
  19. #include <string>
  20. namespace android {
  21. namespace vold {
  22. // Represents the information needed to decrypt a disk encryption key.
  23. // If "token" is nonempty, it is passed in as a required Gatekeeper auth token.
  24. // If "token" and "secret" are nonempty, "secret" is appended to the application-specific
  25. // binary needed to unlock.
  26. // If only "secret" is nonempty, it is used to decrypt in a non-Keymaster process.
  27. class KeyAuthentication {
  28. public:
  29. KeyAuthentication(const std::string& t, const std::string& s) : token{t}, secret{s} {};
  30. bool usesKeymaster() const { return !token.empty() || secret.empty(); };
  31. const std::string token;
  32. const std::string secret;
  33. };
  34. extern const KeyAuthentication kEmptyAuthentication;
  35. // Checks if path "path" exists.
  36. bool pathExists(const std::string& path);
  37. bool createSecdiscardable(const std::string& path, std::string* hash);
  38. bool readSecdiscardable(const std::string& path, std::string* hash);
  39. // Create a directory at the named path, and store "key" in it,
  40. // in such a way that it can only be retrieved via Keymaster and
  41. // can be securely deleted.
  42. // It's safe to move/rename the directory after creation.
  43. bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBuffer& key);
  44. // Create a directory at the named path, and store "key" in it as storeKey
  45. // This version creates the key in "tmp_path" then atomically renames "tmp_path"
  46. // to "key_path" thereby ensuring that the key is either stored entirely or
  47. // not at all.
  48. bool storeKeyAtomically(const std::string& key_path, const std::string& tmp_path,
  49. const KeyAuthentication& auth, const KeyBuffer& key);
  50. // Retrieve the key from the named directory.
  51. bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffer* key,
  52. bool keepOld = false);
  53. // Securely destroy the key stored in the named directory and delete the directory.
  54. bool destroyKey(const std::string& dir);
  55. bool runSecdiscardSingle(const std::string& file);
  56. } // namespace vold
  57. } // namespace android
  58. #endif