keystore_keymaster_enforcement.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2015 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_KEYMASTER_ENFORCEMENT_H_
  17. #define KEYSTORE_KEYSTORE_KEYMASTER_ENFORCEMENT_H_
  18. #include <time.h>
  19. #include "keymaster_enforcement.h"
  20. namespace keystore {
  21. /**
  22. * This is a specialization of the KeymasterEnforcement class to be used by Keystore to enforce
  23. * keymaster requirements on all key operation.
  24. */
  25. class KeystoreKeymasterEnforcement : public KeymasterEnforcement {
  26. public:
  27. KeystoreKeymasterEnforcement() : KeymasterEnforcement(64, 64) {}
  28. uint32_t get_current_time() const override {
  29. struct timespec tp;
  30. int err = clock_gettime(CLOCK_MONOTONIC, &tp);
  31. if (err || tp.tv_sec < 0)
  32. return 0;
  33. return static_cast<uint32_t>(tp.tv_sec);
  34. }
  35. bool activation_date_valid(uint64_t activation_date) const override {
  36. time_t now = time(nullptr);
  37. if (now == static_cast<time_t>(-1)) {
  38. // Failed to obtain current time -- fail safe: activation_date hasn't yet occurred.
  39. return false;
  40. } else if (now < 0) {
  41. // Current time is prior to start of the epoch -- activation_date hasn't yet occurred.
  42. return false;
  43. }
  44. // time(NULL) returns seconds since epoch and "loses" milliseconds information. We thus add
  45. // 999 ms to now_date to avoid a situation where an activation_date of up to 999ms in the
  46. // past may still be considered to still be in the future. This can be removed once
  47. // time(NULL) is replaced by a millisecond-precise source of time.
  48. uint64_t now_date = static_cast<uint64_t>(now) * 1000 + 999;
  49. return now_date >= activation_date;
  50. }
  51. bool expiration_date_passed(uint64_t expiration_date) const override {
  52. time_t now = time(nullptr);
  53. if (now == static_cast<time_t>(-1)) {
  54. // Failed to obtain current time -- fail safe: expiration_date has passed.
  55. return true;
  56. } else if (now < 0) {
  57. // Current time is prior to start of the epoch: expiration_date hasn't yet occurred.
  58. return false;
  59. }
  60. // time(NULL) returns seconds since epoch and "loses" milliseconds information. As a result,
  61. // expiration_date of up to 999 ms in the past may still be considered in the future. This
  62. // is OK.
  63. uint64_t now_date = static_cast<uint64_t>(now) * 1000;
  64. return now_date > expiration_date;
  65. }
  66. bool auth_token_timed_out(const HardwareAuthToken&, uint32_t) const {
  67. // Assume the token has not timed out, because AuthTokenTable would not have returned it if
  68. // the timeout were past. Secure hardware will also check timeouts if it supports them.
  69. return false;
  70. }
  71. bool ValidateTokenSignature(const HardwareAuthToken&) const override {
  72. // Non-secure world cannot validate token signatures because it doesn't have access to the
  73. // signing key. Assume the token is good.
  74. return true;
  75. }
  76. bool is_device_locked(int32_t userId) const override {
  77. // If we haven't had a set call for this user yet, assume the device is locked.
  78. if (mIsDeviceLockedForUser.count(userId) == 0) return true;
  79. return mIsDeviceLockedForUser.find(userId)->second;
  80. }
  81. void set_device_locked(bool isLocked, int32_t userId) {
  82. mIsDeviceLockedForUser[userId] = isLocked;
  83. }
  84. private:
  85. std::map<int32_t, bool> mIsDeviceLockedForUser;
  86. };
  87. } // namespace keystore
  88. #endif // KEYSTORE_KEYSTORE_KEYMASTER_ENFORCEMENT_H_