keystore_main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2009 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. #define LOG_TAG "keystore"
  17. #include <android-base/logging.h>
  18. #include <android/hidl/manager/1.1/IServiceManager.h>
  19. #include <android/security/keystore/IKeystoreService.h>
  20. #include <android/system/wifi/keystore/1.0/IKeystore.h>
  21. #include <binder/IPCThreadState.h>
  22. #include <binder/IServiceManager.h>
  23. #include <hidl/HidlTransportSupport.h>
  24. #include <keymasterV4_0/Keymaster3.h>
  25. #include <keymasterV4_0/Keymaster4.h>
  26. #include <utils/StrongPointer.h>
  27. #include <wifikeystorehal/keystore.h>
  28. #include <keystore/keystore_hidl_support.h>
  29. #include <keystore/keystore_return_types.h>
  30. #include "KeyStore.h"
  31. #include "key_store_service.h"
  32. #include "legacy_keymaster_device_wrapper.h"
  33. #include "permissions.h"
  34. /* KeyStore is a secured storage for key-value pairs. In this implementation,
  35. * each file stores one key-value pair. Keys are encoded in file names, and
  36. * values are encrypted with checksums. The encryption key is protected by a
  37. * user-defined password. To keep things simple, buffers are always larger than
  38. * the maximum space we needed, so boundary checks on buffers are omitted. */
  39. using ::android::sp;
  40. using ::android::hardware::configureRpcThreadpool;
  41. using ::android::system::wifi::keystore::V1_0::IKeystore;
  42. using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
  43. using ::android::hidl::manager::V1_1::IServiceManager;
  44. using ::android::hardware::hidl_string;
  45. using ::android::hardware::hidl_vec;
  46. using ::android::hardware::keymaster::V4_0::SecurityLevel;
  47. using ::android::hardware::keymaster::V4_0::HmacSharingParameters;
  48. using ::android::hardware::keymaster::V4_0::ErrorCode;
  49. using ::keystore::keymaster::support::Keymaster;
  50. using ::keystore::keymaster::support::Keymaster3;
  51. using ::keystore::keymaster::support::Keymaster4;
  52. using keystore::KeymasterDevices;
  53. template <typename Wrapper>
  54. KeymasterDevices enumerateKeymasterDevices(IServiceManager* serviceManager) {
  55. KeymasterDevices result;
  56. serviceManager->listByInterface(
  57. Wrapper::WrappedIKeymasterDevice::descriptor, [&](const hidl_vec<hidl_string>& names) {
  58. auto try_get_device = [&](const auto& name, bool fail_silent) {
  59. auto device = Wrapper::WrappedIKeymasterDevice::getService(name);
  60. if (fail_silent && !device) return;
  61. CHECK(device) << "Failed to get service for \""
  62. << Wrapper::WrappedIKeymasterDevice::descriptor
  63. << "\" with interface name \"" << name << "\"";
  64. sp<Keymaster> kmDevice(new Wrapper(device, name));
  65. auto halVersion = kmDevice->halVersion();
  66. SecurityLevel securityLevel = halVersion.securityLevel;
  67. LOG(INFO) << "found " << Wrapper::WrappedIKeymasterDevice::descriptor
  68. << " with interface name " << name << " and seclevel "
  69. << toString(securityLevel);
  70. CHECK(static_cast<uint32_t>(securityLevel) < result.size())
  71. << "Security level of \"" << Wrapper::WrappedIKeymasterDevice::descriptor
  72. << "\" with interface name \"" << name << "\" out of range";
  73. auto& deviceSlot = result[securityLevel];
  74. if (deviceSlot) {
  75. if (!fail_silent) {
  76. LOG(WARNING) << "Implementation of \""
  77. << Wrapper::WrappedIKeymasterDevice::descriptor
  78. << "\" with interface name \"" << name
  79. << "\" and security level: " << toString(securityLevel)
  80. << " Masked by other implementation of Keymaster";
  81. }
  82. } else {
  83. deviceSlot = kmDevice;
  84. }
  85. };
  86. bool has_default = false;
  87. for (auto& n : names) {
  88. try_get_device(n, false);
  89. if (n == "default") has_default = true;
  90. }
  91. // Make sure that we always check the default device. If we enumerate only what is
  92. // known to hwservicemanager, we miss a possible passthrough HAL.
  93. if (!has_default) {
  94. try_get_device("default", true /* fail_silent */);
  95. }
  96. });
  97. return result;
  98. }
  99. KeymasterDevices initializeKeymasters() {
  100. auto serviceManager = android::hidl::manager::V1_1::IServiceManager::getService();
  101. CHECK(serviceManager.get()) << "Failed to get ServiceManager";
  102. auto result = enumerateKeymasterDevices<Keymaster4>(serviceManager.get());
  103. auto softKeymaster = result[SecurityLevel::SOFTWARE];
  104. if (!result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
  105. result = enumerateKeymasterDevices<Keymaster3>(serviceManager.get());
  106. }
  107. if (softKeymaster) result[SecurityLevel::SOFTWARE] = softKeymaster;
  108. if (result[SecurityLevel::SOFTWARE] && !result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
  109. LOG(WARNING) << "No secure Keymaster implementation found, but device offers insecure"
  110. " Keymaster HAL. Using as default.";
  111. result[SecurityLevel::TRUSTED_ENVIRONMENT] = result[SecurityLevel::SOFTWARE];
  112. result[SecurityLevel::SOFTWARE] = nullptr;
  113. }
  114. if (!result[SecurityLevel::SOFTWARE]) {
  115. auto fbdev = android::keystore::makeSoftwareKeymasterDevice();
  116. CHECK(fbdev.get()) << "Unable to create Software Keymaster Device";
  117. result[SecurityLevel::SOFTWARE] = new Keymaster3(fbdev, "Software");
  118. }
  119. return result;
  120. }
  121. int main(int argc, char* argv[]) {
  122. android::OtherSystemServiceLoopRun();
  123. using android::hardware::hidl_string;
  124. CHECK(argc >= 2) << "A directory must be specified!";
  125. CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno);
  126. auto kmDevices = initializeKeymasters();
  127. CHECK(kmDevices[SecurityLevel::SOFTWARE]) << "Missing software Keymaster device";
  128. CHECK(kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT])
  129. << "Error no viable keymaster device found";
  130. CHECK(configure_selinux() != -1) << "Failed to configure SELinux.";
  131. auto halVersion = kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]->halVersion();
  132. // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import
  133. // or generation of keys. The fallback device is only used for legacy keys present on the
  134. // device.
  135. SecurityLevel minimalAllowedSecurityLevelForNewKeys =
  136. halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE;
  137. android::sp<keystore::KeyStore> keyStore(
  138. new keystore::KeyStore(kmDevices, minimalAllowedSecurityLevelForNewKeys));
  139. keyStore->initialize();
  140. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  141. android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(keyStore);
  142. service->setRequestingSid(true);
  143. android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
  144. CHECK(ret == android::OK) << "Couldn't register binder service!";
  145. /**
  146. * Register the wifi keystore HAL service to run in passthrough mode.
  147. * This will spawn off a new thread which will service the HIDL
  148. * transactions.
  149. */
  150. configureRpcThreadpool(1, false /* callerWillJoin */);
  151. android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
  152. android::status_t err = wifiKeystoreHalService->registerAsService();
  153. CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err;
  154. /*
  155. * This thread is just going to process Binder transactions.
  156. */
  157. android::IPCThreadState::self()->joinThreadPool();
  158. return 1;
  159. }