• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 
17 #include "trusty_keymaster_enforcement.h"
18 
19 #include <openssl/hmac.h>
20 #include <trusty/time.h>
21 
22 #include <inttypes.h>
23 
24 #include <hardware/hw_auth_token.h>
25 #include <keymaster/android_keymaster_utils.h>
26 #include <keymaster/km_openssl/openssl_err.h>
27 
28 #include "trusty_keymaster_context.h"
29 
30 namespace keymaster {
31 
SecurityLevel() const32 keymaster_security_level_t TrustyKeymasterEnforcement::SecurityLevel() const {
33     return KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
34 }
35 
auth_token_timed_out(const hw_auth_token_t & token,uint32_t timeout_seconds) const36 bool TrustyKeymasterEnforcement::auth_token_timed_out(
37         const hw_auth_token_t& token,
38         uint32_t timeout_seconds) const {
39     uint64_t token_timestamp_millis = ntoh(token.timestamp);
40     uint64_t timeout_millis = static_cast<uint64_t>(timeout_seconds) * 1000;
41     uint64_t millis_since_boot = milliseconds_since_boot();
42     return (millis_since_boot >= token_timestamp_millis &&
43             (millis_since_boot - token_timestamp_millis) > timeout_millis);
44 }
45 
get_current_time_ms() const46 uint64_t TrustyKeymasterEnforcement::get_current_time_ms() const {
47     return milliseconds_since_boot();
48 }
49 
min(size_t a,size_t b)50 inline size_t min(size_t a, size_t b) {
51     return a < b ? a : b;
52 }
53 
ValidateTokenSignature(const hw_auth_token_t & token) const54 bool TrustyKeymasterEnforcement::ValidateTokenSignature(
55         const hw_auth_token_t& token) const {
56     keymaster_key_blob_t auth_token_key;
57     keymaster_error_t error = context_->GetAuthTokenKey(&auth_token_key);
58     if (error != KM_ERROR_OK)
59         return false;
60 
61     // Signature covers entire token except HMAC field.
62     const uint8_t* hash_data = reinterpret_cast<const uint8_t*>(&token);
63     size_t hash_data_length =
64             reinterpret_cast<const uint8_t*>(&token.hmac) - hash_data;
65 
66     uint8_t computed_hash[EVP_MAX_MD_SIZE];
67     unsigned int computed_hash_length;
68     if (!HMAC(EVP_sha256(), auth_token_key.key_material,
69               auth_token_key.key_material_size, hash_data, hash_data_length,
70               computed_hash, &computed_hash_length)) {
71         LOG_S("Error %d computing token signature",
72               TranslateLastOpenSslError());
73         return false;
74     }
75 
76     return 0 == memcmp_s(computed_hash, token.hmac,
77                          min(sizeof(token.hmac), computed_hash_length));
78 }
79 
milliseconds_since_boot() const80 uint64_t TrustyKeymasterEnforcement::milliseconds_since_boot() const {
81     int rv;
82     int64_t secure_time_ns = 0;
83     rv = trusty_gettime(0, &secure_time_ns);
84     if (rv || secure_time_ns < 0) {
85         LOG_S("Error getting time. Error: %d, time: %" PRId64, rv,
86               secure_time_ns);
87         secure_time_ns =
88                 0xFFFFFFFFFFFFFFFFL;  // UINT64_MAX isn't defined (b/22120972)
89     }
90     return static_cast<uint64_t>(secure_time_ns) / 1000 / 1000;
91 }
92 
93 }  // namespace keymaster
94