KeyUtil.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "KeyUtil.h"
  17. #include <linux/fs.h>
  18. #include <iomanip>
  19. #include <sstream>
  20. #include <string>
  21. #include <openssl/sha.h>
  22. #include <android-base/file.h>
  23. #include <android-base/logging.h>
  24. #include <keyutils.h>
  25. #include "KeyStorage.h"
  26. #include "Utils.h"
  27. namespace android {
  28. namespace vold {
  29. constexpr int FS_AES_256_XTS_KEY_SIZE = 64;
  30. bool randomKey(KeyBuffer* key) {
  31. *key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE);
  32. if (ReadRandomBytes(key->size(), key->data()) != 0) {
  33. // TODO status_t plays badly with PLOG, fix it.
  34. LOG(ERROR) << "Random read failed";
  35. return false;
  36. }
  37. return true;
  38. }
  39. // Get raw keyref - used to make keyname and to pass to ioctl
  40. static std::string generateKeyRef(const uint8_t* key, int length) {
  41. SHA512_CTX c;
  42. SHA512_Init(&c);
  43. SHA512_Update(&c, key, length);
  44. unsigned char key_ref1[SHA512_DIGEST_LENGTH];
  45. SHA512_Final(key_ref1, &c);
  46. SHA512_Init(&c);
  47. SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
  48. unsigned char key_ref2[SHA512_DIGEST_LENGTH];
  49. SHA512_Final(key_ref2, &c);
  50. static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor");
  51. return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE);
  52. }
  53. static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
  54. if (key.size() != FS_AES_256_XTS_KEY_SIZE) {
  55. LOG(ERROR) << "Wrong size key " << key.size();
  56. return false;
  57. }
  58. static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!");
  59. fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  60. fs_key->size = key.size();
  61. memset(fs_key->raw, 0, sizeof(fs_key->raw));
  62. memcpy(fs_key->raw, key.data(), key.size());
  63. return true;
  64. }
  65. static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
  66. static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
  67. std::ostringstream o;
  68. o << prefix << ":";
  69. for (unsigned char i : raw_ref) {
  70. o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
  71. }
  72. return o.str();
  73. }
  74. // Get the keyring we store all keys in
  75. static bool fscryptKeyring(key_serial_t* device_keyring) {
  76. *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
  77. if (*device_keyring == -1) {
  78. PLOG(ERROR) << "Unable to find device keyring";
  79. return false;
  80. }
  81. return true;
  82. }
  83. // Install password into global keyring
  84. // Return raw key reference for use in policy
  85. bool installKey(const KeyBuffer& key, std::string* raw_ref) {
  86. // Place fscrypt_key into automatically zeroing buffer.
  87. KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
  88. fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
  89. if (!fillKey(key, &fs_key)) return false;
  90. *raw_ref = generateKeyRef(fs_key.raw, fs_key.size);
  91. key_serial_t device_keyring;
  92. if (!fscryptKeyring(&device_keyring)) return false;
  93. for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
  94. auto ref = keyname(*name_prefix, *raw_ref);
  95. key_serial_t key_id =
  96. add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
  97. if (key_id == -1) {
  98. PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
  99. return false;
  100. }
  101. LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
  102. << " in process " << getpid();
  103. }
  104. return true;
  105. }
  106. bool evictKey(const std::string& raw_ref) {
  107. key_serial_t device_keyring;
  108. if (!fscryptKeyring(&device_keyring)) return false;
  109. bool success = true;
  110. for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
  111. auto ref = keyname(*name_prefix, raw_ref);
  112. auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
  113. // Unlink the key from the keyring. Prefer unlinking to revoking or
  114. // invalidating, since unlinking is actually no less secure currently, and
  115. // it avoids bugs in certain kernel versions where the keyring key is
  116. // referenced from places it shouldn't be.
  117. if (keyctl_unlink(key_serial, device_keyring) != 0) {
  118. PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
  119. success = false;
  120. } else {
  121. LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
  122. }
  123. }
  124. return success;
  125. }
  126. bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
  127. const std::string& key_path, const std::string& tmp_path,
  128. std::string* key_ref) {
  129. KeyBuffer key;
  130. if (pathExists(key_path)) {
  131. LOG(DEBUG) << "Key exists, using: " << key_path;
  132. if (!retrieveKey(key_path, key_authentication, &key)) return false;
  133. } else {
  134. if (!create_if_absent) {
  135. LOG(ERROR) << "No key found in " << key_path;
  136. return false;
  137. }
  138. LOG(INFO) << "Creating new key in " << key_path;
  139. if (!randomKey(&key)) return false;
  140. if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
  141. }
  142. if (!installKey(key, key_ref)) {
  143. LOG(ERROR) << "Failed to install key in " << key_path;
  144. return false;
  145. }
  146. return true;
  147. }
  148. bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
  149. KeyBuffer* key, bool keepOld) {
  150. if (pathExists(key_path)) {
  151. LOG(DEBUG) << "Key exists, using: " << key_path;
  152. if (!retrieveKey(key_path, kEmptyAuthentication, key, keepOld)) return false;
  153. } else {
  154. if (!create_if_absent) {
  155. LOG(ERROR) << "No key found in " << key_path;
  156. return false;
  157. }
  158. LOG(INFO) << "Creating new key in " << key_path;
  159. if (!randomKey(key)) return false;
  160. if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false;
  161. }
  162. return true;
  163. }
  164. } // namespace vold
  165. } // namespace android