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