• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 kVendorPatchlevelProp[] = "ro.vendor.build.security_patch";
44 constexpr char kPatchlevelRegex[] = "^([0-9]{4})-([0-9]{2})-([0-9]{2})$";
45 constexpr size_t kYearMatch = 1;
46 constexpr size_t kMonthMatch = 2;
47 constexpr size_t kDayMatch = 3;
48 constexpr size_t kPatchlevelMatchCount = kDayMatch + 1;
49 
match_to_uint32(const char * expression,const regmatch_t & match)50 uint32_t match_to_uint32(const char* expression, const regmatch_t& match) {
51     if (match.rm_so == -1) return 0;
52 
53     size_t len = match.rm_eo - match.rm_so;
54     std::string s(expression + match.rm_so, len);
55     return std::stoul(s);
56 }
57 
wait_and_get_property(const char * prop)58 std::string wait_and_get_property(const char* prop) {
59     std::string prop_value;
60 #ifndef KEYMASTER_UNIT_TEST_BUILD
61     while (!android::base::WaitForPropertyCreation(prop)) {
62         SLOGE("waited 15s for %s, still waiting...", prop);
63     }
64     prop_value = android::base::GetProperty(prop, "" /* default */);
65 #endif
66     return prop_value;
67 }
68 
69 enum class PatchlevelOutput { kYearMonthDay, kYearMonth };
70 
GetPatchlevel(const char * patchlevel_str,PatchlevelOutput detail)71 uint32_t GetPatchlevel(const char* patchlevel_str, PatchlevelOutput detail) {
72     regex_t regex;
73     if (regcomp(&regex, kPatchlevelRegex, REG_EXTENDED) != 0) {
74         ALOGE("Failed to compile platform patchlevel regex! (%s)", kPatchlevelRegex);
75         return 0;
76     }
77 
78     regmatch_t matches[kPatchlevelMatchCount];
79     int not_match = regexec(&regex, patchlevel_str, kPatchlevelMatchCount, matches, 0 /* flags */);
80     regfree(&regex);
81     if (not_match) {
82         ALOGI(" patchlevel string does not match expected format.  Using patchlevel 0");
83         return 0;
84     }
85 
86     uint32_t year = match_to_uint32(patchlevel_str, matches[kYearMatch]);
87     uint32_t month = match_to_uint32(patchlevel_str, matches[kMonthMatch]);
88 
89     if (month < 1 || month > 12) {
90         ALOGE("Invalid patch month %d", month);
91         return 0;
92     }
93 
94     switch (detail) {
95     case PatchlevelOutput::kYearMonthDay: {
96         uint32_t day = match_to_uint32(patchlevel_str, matches[kDayMatch]);
97         if (day < 1 || day > 31) {
98             ALOGE("Invalid patch day %d", day);
99             return 0;
100         }
101         return year * 10000 + month * 100 + day;
102     }
103     case PatchlevelOutput::kYearMonth:
104         return year * 100 + month;
105     }
106     return 0;
107 }
108 
109 }  // anonymous namespace
110 
ConfigureDevice(keymaster2_device_t * dev,uint32_t os_version,uint32_t os_patchlevel)111 keymaster_error_t ConfigureDevice(keymaster2_device_t* dev, uint32_t os_version,
112                                   uint32_t os_patchlevel) {
113     AuthorizationSet config_params(AuthorizationSetBuilder()
114                                        .Authorization(keymaster::TAG_OS_VERSION, os_version)
115                                        .Authorization(keymaster::TAG_OS_PATCHLEVEL, os_patchlevel));
116     return dev->configure(dev, &config_params);
117 }
118 
ConfigureDevice(keymaster2_device_t * dev)119 keymaster_error_t ConfigureDevice(keymaster2_device_t* dev) {
120     return ConfigureDevice(dev, GetOsVersion(), GetOsPatchlevel());
121 }
122 
GetOsVersion(const char * version_str)123 uint32_t GetOsVersion(const char* version_str) {
124     regex_t regex;
125     if (regcomp(&regex, kPlatformVersionRegex, REG_EXTENDED)) {
126         ALOGE("Failed to compile version regex! (%s)", kPlatformVersionRegex);
127         return 0;
128     }
129 
130     regmatch_t matches[kPlatformVersionMatchCount];
131     int not_match =
132         regexec(&regex, version_str, kPlatformVersionMatchCount, matches, 0 /* flags */);
133     regfree(&regex);
134     if (not_match) {
135         ALOGI("Platform version string \"%s\" does not match expected format.  Using version 0.",
136               version_str);
137         return 0;
138     }
139 
140     uint32_t major = match_to_uint32(version_str, matches[kMajorVersionMatch]);
141     uint32_t minor = match_to_uint32(version_str, matches[kMinorVersionMatch]);
142     uint32_t subminor = match_to_uint32(version_str, matches[kSubminorVersionMatch]);
143 
144     return (major * 100 + minor) * 100 + subminor;
145 }
146 
GetOsVersion()147 uint32_t GetOsVersion() {
148     std::string version = wait_and_get_property(kPlatformVersionProp);
149     return GetOsVersion(version.c_str());
150 }
151 
GetOsPatchlevel(const char * patchlevel_str)152 uint32_t GetOsPatchlevel(const char* patchlevel_str) {
153     return GetPatchlevel(patchlevel_str, PatchlevelOutput::kYearMonth);
154 }
155 
GetOsPatchlevel()156 uint32_t GetOsPatchlevel() {
157     std::string patchlevel = wait_and_get_property(kPlatformPatchlevelProp);
158     return GetOsPatchlevel(patchlevel.c_str());
159 }
160 
GetVendorPatchlevel()161 uint32_t GetVendorPatchlevel() {
162     std::string patchlevel = wait_and_get_property(kVendorPatchlevelProp);
163     return GetPatchlevel(patchlevel.c_str(), PatchlevelOutput::kYearMonthDay);
164 }
165 
166 }  // namespace keymaster
167