keymaster_configuration.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <keymaster/keymaster_configuration.h>
  17. #include <regex>
  18. #include <string>
  19. #include <regex.h>
  20. #define LOG_TAG "keymaster"
  21. #include <android-base/properties.h>
  22. #include <log/log.h>
  23. #include <keymaster/authorization_set.h>
  24. namespace keymaster {
  25. namespace {
  26. constexpr char kPlatformVersionProp[] = "ro.build.version.release";
  27. constexpr char kPlatformVersionRegex[] = "^([0-9]{1,2})(\\.([0-9]{1,2}))?(\\.([0-9]{1,2}))?";
  28. constexpr size_t kMajorVersionMatch = 1;
  29. constexpr size_t kMinorVersionMatch = 3;
  30. constexpr size_t kSubminorVersionMatch = 5;
  31. constexpr size_t kPlatformVersionMatchCount = kSubminorVersionMatch + 1;
  32. constexpr char kPlatformPatchlevelProp[] = "ro.build.version.security_patch";
  33. constexpr char kPlatformPatchlevelRegex[] = "^([0-9]{4})-([0-9]{2})-[0-9]{2}$";
  34. constexpr size_t kYearMatch = 1;
  35. constexpr size_t kMonthMatch = 2;
  36. constexpr size_t kPlatformPatchlevelMatchCount = kMonthMatch + 1;
  37. uint32_t match_to_uint32(const char* expression, const regmatch_t& match) {
  38. if (match.rm_so == -1)
  39. return 0;
  40. size_t len = match.rm_eo - match.rm_so;
  41. std::string s(expression + match.rm_so, len);
  42. return std::stoul(s);
  43. }
  44. std::string wait_and_get_property(const char* prop) {
  45. std::string prop_value;
  46. #ifndef KEYMASTER_UNIT_TEST_BUILD
  47. while (!android::base::WaitForPropertyCreation(prop)) {
  48. SLOGE("waited 15s for %s, still waiting...", prop);
  49. }
  50. prop_value = android::base::GetProperty(prop, "" /* default */);
  51. #endif
  52. return prop_value;
  53. }
  54. } // anonymous namespace
  55. keymaster_error_t ConfigureDevice(keymaster2_device_t* dev, uint32_t os_version,
  56. uint32_t os_patchlevel) {
  57. AuthorizationSet config_params(AuthorizationSetBuilder()
  58. .Authorization(keymaster::TAG_OS_VERSION, os_version)
  59. .Authorization(keymaster::TAG_OS_PATCHLEVEL, os_patchlevel));
  60. return dev->configure(dev, &config_params);
  61. }
  62. keymaster_error_t ConfigureDevice(keymaster2_device_t* dev) {
  63. return ConfigureDevice(dev, GetOsVersion(), GetOsPatchlevel());
  64. }
  65. uint32_t GetOsVersion(const char* version_str) {
  66. regex_t regex;
  67. if (regcomp(&regex, kPlatformVersionRegex, REG_EXTENDED)) {
  68. ALOGE("Failed to compile version regex! (%s)", kPlatformVersionRegex);
  69. return 0;
  70. }
  71. regmatch_t matches[kPlatformVersionMatchCount];
  72. int not_match =
  73. regexec(&regex, version_str, kPlatformVersionMatchCount, matches, 0 /* flags */);
  74. regfree(&regex);
  75. if (not_match) {
  76. ALOGI("Platform version string does not match expected format. Using version 0.");
  77. return 0;
  78. }
  79. uint32_t major = match_to_uint32(version_str, matches[kMajorVersionMatch]);
  80. uint32_t minor = match_to_uint32(version_str, matches[kMinorVersionMatch]);
  81. uint32_t subminor = match_to_uint32(version_str, matches[kSubminorVersionMatch]);
  82. return (major * 100 + minor) * 100 + subminor;
  83. }
  84. uint32_t GetOsVersion() {
  85. std::string version = wait_and_get_property(kPlatformVersionProp);
  86. return GetOsVersion(version.c_str());
  87. }
  88. uint32_t GetOsPatchlevel(const char* patchlevel_str) {
  89. regex_t regex;
  90. if (regcomp(&regex, kPlatformPatchlevelRegex, REG_EXTENDED) != 0) {
  91. ALOGE("Failed to compile platform patchlevel regex! (%s)", kPlatformPatchlevelRegex);
  92. return 0;
  93. }
  94. regmatch_t matches[kPlatformPatchlevelMatchCount];
  95. int not_match =
  96. regexec(&regex, patchlevel_str, kPlatformPatchlevelMatchCount, matches, 0 /* flags */);
  97. regfree(&regex);
  98. if (not_match) {
  99. ALOGI("Platform patchlevel string does not match expected format. Using patchlevel 0");
  100. return 0;
  101. }
  102. uint32_t year = match_to_uint32(patchlevel_str, matches[kYearMatch]);
  103. uint32_t month = match_to_uint32(patchlevel_str, matches[kMonthMatch]);
  104. if (month < 1 || month > 12) {
  105. ALOGE("Invalid patch month %d", month);
  106. return 0;
  107. }
  108. return year * 100 + month;
  109. }
  110. uint32_t GetOsPatchlevel() {
  111. std::string patchlevel = wait_and_get_property(kPlatformPatchlevelProp);
  112. return GetOsPatchlevel(patchlevel.c_str());
  113. }
  114. } // namespace keymaster