key_store_service.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 KEYSTORE_KEYSTORE_SERVICE_H_
  17. #define KEYSTORE_KEYSTORE_SERVICE_H_
  18. #include <android/security/keystore/BnKeystoreService.h>
  19. #include "auth_token_table.h"
  20. #include "confirmation_manager.h"
  21. #include "KeyStore.h"
  22. #include "keystore_keymaster_enforcement.h"
  23. #include "operation.h"
  24. #include "permissions.h"
  25. #include <keystore/ExportResult.h>
  26. #include <keystore/KeyCharacteristics.h>
  27. #include <keystore/KeymasterArguments.h>
  28. #include <keystore/KeymasterBlob.h>
  29. #include <keystore/KeymasterCertificateChain.h>
  30. #include <keystore/OperationResult.h>
  31. #include <keystore/keystore_return_types.h>
  32. #include <mutex>
  33. namespace keystore {
  34. // Class provides implementation for generated BnKeystoreService.h based on
  35. // gen/aidl/android/security/BnKeystoreService.h generated from
  36. // java/android/security/IKeystoreService.aidl Note that all generated methods return binder::Status
  37. // and use last arguments to send actual result to the caller. Private methods don't need to handle
  38. // binder::Status. Input parameters cannot be null unless annotated with @nullable in .aidl file.
  39. class KeyStoreService : public android::security::keystore::BnKeystoreService {
  40. public:
  41. explicit KeyStoreService(sp<KeyStore> keyStore) : mKeyStore(keyStore) {}
  42. virtual ~KeyStoreService() = default;
  43. void binderDied(const android::wp<android::IBinder>& who);
  44. ::android::binder::Status getState(int32_t userId, int32_t* _aidl_return) override;
  45. ::android::binder::Status get(const ::android::String16& name, int32_t uid,
  46. ::std::vector<uint8_t>* _aidl_return) override;
  47. ::android::binder::Status insert(const ::android::String16& name,
  48. const ::std::vector<uint8_t>& item, int32_t uid, int32_t flags,
  49. int32_t* _aidl_return) override;
  50. ::android::binder::Status del(const ::android::String16& name, int32_t uid,
  51. int32_t* _aidl_return) override;
  52. ::android::binder::Status exist(const ::android::String16& name, int32_t uid,
  53. int32_t* _aidl_return) override;
  54. ::android::binder::Status list(const ::android::String16& namePrefix, int32_t uid,
  55. ::std::vector<::android::String16>* _aidl_return) override;
  56. ::android::binder::Status listUidsOfAuthBoundKeys(std::vector<::std::string>* uids,
  57. int32_t* _aidl_return) override;
  58. ::android::binder::Status reset(int32_t* _aidl_return) override;
  59. ::android::binder::Status onUserPasswordChanged(int32_t userId,
  60. const ::android::String16& newPassword,
  61. int32_t* _aidl_return) override;
  62. ::android::binder::Status lock(int32_t userId, int32_t* _aidl_return) override;
  63. ::android::binder::Status unlock(int32_t userId, const ::android::String16& userPassword,
  64. int32_t* _aidl_return) override;
  65. ::android::binder::Status isEmpty(int32_t userId, int32_t* _aidl_return) override;
  66. ::android::binder::Status grant(const ::android::String16& name, int32_t granteeUid,
  67. ::android::String16* _aidl_return) override;
  68. ::android::binder::Status ungrant(const ::android::String16& name, int32_t granteeUid,
  69. int32_t* _aidl_return) override;
  70. ::android::binder::Status getmtime(const ::android::String16& name, int32_t uid,
  71. int64_t* _aidl_return) override;
  72. ::android::binder::Status is_hardware_backed(const ::android::String16& string,
  73. int32_t* _aidl_return) override;
  74. ::android::binder::Status clear_uid(int64_t uid, int32_t* _aidl_return) override;
  75. ::android::binder::Status
  76. addRngEntropy(const ::android::sp<::android::security::keystore::IKeystoreResponseCallback>& cb,
  77. const ::std::vector<uint8_t>& data, int32_t flags,
  78. int32_t* _aidl_return) override;
  79. ::android::binder::Status generateKey(
  80. const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
  81. const ::android::String16& alias,
  82. const ::android::security::keymaster::KeymasterArguments& arguments,
  83. const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t flags,
  84. int32_t* _aidl_return) override;
  85. ::android::binder::Status getKeyCharacteristics(
  86. const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
  87. const ::android::String16& alias,
  88. const ::android::security::keymaster::KeymasterBlob& clientId,
  89. const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
  90. int32_t* _aidl_return) override;
  91. ::android::binder::Status importKey(
  92. const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
  93. const ::android::String16& alias,
  94. const ::android::security::keymaster::KeymasterArguments& arguments, int32_t format,
  95. const ::std::vector<uint8_t>& keyData, int32_t uid, int32_t flags,
  96. int32_t* _aidl_return) override;
  97. ::android::binder::Status
  98. exportKey(const ::android::sp<::android::security::keystore::IKeystoreExportKeyCallback>& cb,
  99. const ::android::String16& alias, int32_t format,
  100. const ::android::security::keymaster::KeymasterBlob& clientId,
  101. const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
  102. int32_t* _aidl_return) override;
  103. ::android::binder::Status
  104. begin(const ::android::sp<::android::security::keystore::IKeystoreOperationResultCallback>& cb,
  105. const ::android::sp<::android::IBinder>& appToken, const ::android::String16& alias,
  106. int32_t purpose, bool pruneable,
  107. const ::android::security::keymaster::KeymasterArguments& params,
  108. const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t* _aidl_return) override;
  109. ::android::binder::Status
  110. update(const ::android::sp<::android::security::keystore::IKeystoreOperationResultCallback>& cb,
  111. const ::android::sp<::android::IBinder>& token,
  112. const ::android::security::keymaster::KeymasterArguments& params,
  113. const ::std::vector<uint8_t>& input, int32_t* _aidl_return) override;
  114. ::android::binder::Status
  115. finish(const ::android::sp<::android::security::keystore::IKeystoreOperationResultCallback>& cb,
  116. const ::android::sp<::android::IBinder>& token,
  117. const ::android::security::keymaster::KeymasterArguments& params,
  118. const ::std::vector<uint8_t>& signature, const ::std::vector<uint8_t>& entropy,
  119. int32_t* _aidl_return) override;
  120. ::android::binder::Status
  121. abort(const ::android::sp<::android::security::keystore::IKeystoreResponseCallback>& cb,
  122. const ::android::sp<::android::IBinder>& token, int32_t* _aidl_return) override;
  123. ::android::binder::Status addAuthToken(const ::std::vector<uint8_t>& authToken,
  124. int32_t* _aidl_return) override;
  125. ::android::binder::Status onUserAdded(int32_t userId, int32_t parentId,
  126. int32_t* _aidl_return) override;
  127. ::android::binder::Status onUserRemoved(int32_t userId, int32_t* _aidl_return) override;
  128. ::android::binder::Status attestKey(
  129. const ::android::sp<::android::security::keystore::IKeystoreCertificateChainCallback>& cb,
  130. const ::android::String16& alias,
  131. const ::android::security::keymaster::KeymasterArguments& params,
  132. int32_t* _aidl_return) override;
  133. ::android::binder::Status attestDeviceIds(
  134. const ::android::sp<::android::security::keystore::IKeystoreCertificateChainCallback>& cb,
  135. const ::android::security::keymaster::KeymasterArguments& params,
  136. int32_t* _aidl_return) override;
  137. ::android::binder::Status onDeviceOffBody(int32_t* _aidl_return) override;
  138. ::android::binder::Status importWrappedKey(
  139. const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
  140. const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey,
  141. const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey,
  142. const ::android::security::keymaster::KeymasterArguments& params, int64_t rootSid,
  143. int64_t fingerprintSid, int32_t* _aidl_return) override;
  144. ::android::binder::Status presentConfirmationPrompt(
  145. const ::android::sp<::android::IBinder>& listener, const ::android::String16& promptText,
  146. const ::std::vector<uint8_t>& extraData, const ::android::String16& locale,
  147. int32_t uiOptionsAsFlags, int32_t* _aidl_return) override;
  148. ::android::binder::Status
  149. cancelConfirmationPrompt(const ::android::sp<::android::IBinder>& listener,
  150. int32_t* _aidl_return) override;
  151. ::android::binder::Status isConfirmationPromptSupported(bool* _aidl_return) override;
  152. ::android::binder::Status onKeyguardVisibilityChanged(bool isShowing, int32_t userId,
  153. int32_t* _aidl_return) override;
  154. private:
  155. static const int32_t UID_SELF = -1;
  156. /**
  157. * Get the effective target uid for a binder operation that takes an
  158. * optional uid as the target.
  159. */
  160. uid_t getEffectiveUid(int32_t targetUid);
  161. /**
  162. * Check if the caller of the current binder method has the required
  163. * permission and if acting on other uids the grants to do so.
  164. */
  165. bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
  166. /**
  167. * Check if the caller of the current binder method has the required
  168. * permission and the target uid is the caller or the caller is system.
  169. */
  170. bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
  171. /**
  172. * Check if the caller of the current binder method has the required
  173. * permission or the target of the operation is the caller's uid. This is
  174. * for operation where the permission is only for cross-uid activity and all
  175. * uids are allowed to act on their own (ie: clearing all entries for a
  176. * given uid).
  177. */
  178. bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
  179. /**
  180. * Helper method to check that the caller has the required permission as
  181. * well as the keystore is in the unlocked state if checkUnlocked is true.
  182. *
  183. * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
  184. * otherwise the state of keystore when not unlocked and checkUnlocked is
  185. * true.
  186. */
  187. KeyStoreServiceReturnCode checkBinderPermissionAndKeystoreState(perm_t permission,
  188. int32_t targetUid = -1,
  189. bool checkUnlocked = true);
  190. bool isKeystoreUnlocked(State state);
  191. /**
  192. * Check that all keymaster_key_param_t's provided by the application are
  193. * allowed. Any parameter that keystore adds itself should be disallowed here.
  194. */
  195. bool checkAllowedOperationParams(const hidl_vec<KeyParameter>& params);
  196. void addLegacyBeginParams(const android::String16& name, AuthorizationSet* params);
  197. KeyStoreServiceReturnCode doLegacySignVerify(const android::String16& name,
  198. const hidl_vec<uint8_t>& data,
  199. hidl_vec<uint8_t>* out,
  200. const hidl_vec<uint8_t>& signature,
  201. KeyPurpose purpose);
  202. /**
  203. * Adds a Confirmation Token to the key parameters if needed.
  204. */
  205. void appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics,
  206. std::vector<KeyParameter>* params);
  207. sp<KeyStore> mKeyStore;
  208. /**
  209. * This mutex locks keystore operations from concurrent execution.
  210. * The keystore service has always been conceptually single threaded. Even with the introduction
  211. * of keymaster workers, it was assumed that the dispatcher thread executes exclusively on
  212. * certain code paths. With the introduction of wifi Keystore service in the keystore process
  213. * this assumption no longer holds as the hwbinder thread servicing this interface makes
  214. * functions (rather than IPC) calls into keystore. This mutex protects the keystore logic
  215. * from concurrent execution.
  216. */
  217. std::mutex keystoreServiceMutex_;
  218. };
  219. }; // namespace keystore
  220. #endif // KEYSTORE_KEYSTORE_SERVICE_H_