1 /*
2  * Copyright 2020, 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 #define LOG_TAG "android.hardware.security.secureclock-impl"
18 #include <log/log.h>
19 
20 #include "AndroidSecureClock.h"
21 
22 #include <aidl/android/hardware/security/keymint/ErrorCode.h>
23 
24 #include "KeyMintUtils.h"
25 #include <keymaster/android_keymaster.h>
26 #include <keymaster/keymaster_configuration.h>
27 
28 namespace aidl::android::hardware::security::secureclock {
29 
30 using keymaster::GenerateTimestampTokenRequest;
31 using keymaster::GenerateTimestampTokenResponse;
32 using keymint::km_utils::kmBlob2vector;
33 using keymint::km_utils::kmError2ScopedAStatus;
34 
AndroidSecureClock(const std::shared_ptr<keymint::AndroidKeyMintDevice> & keymint)35 AndroidSecureClock::AndroidSecureClock(
36     const std::shared_ptr<keymint::AndroidKeyMintDevice>& keymint)
37     : impl_(keymint->getKeymasterImpl()) {}
38 
~AndroidSecureClock()39 AndroidSecureClock::~AndroidSecureClock() {}
40 
generateTimeStamp(int64_t challenge,TimeStampToken * token)41 ScopedAStatus AndroidSecureClock::generateTimeStamp(int64_t challenge, TimeStampToken* token) {
42     GenerateTimestampTokenRequest request(impl_->message_version());
43     request.challenge = challenge;
44     GenerateTimestampTokenResponse response(request.message_version);
45     impl_->GenerateTimestampToken(request, &response);
46     if (response.error != KM_ERROR_OK) {
47         return kmError2ScopedAStatus(response.error);
48     }
49     token->challenge = response.token.challenge;
50     token->timestamp.milliSeconds = static_cast<int64_t>(response.token.timestamp);
51     token->mac = kmBlob2vector(response.token.mac);
52     return ScopedAStatus::ok();
53 }
54 
55 }  // namespace aidl::android::hardware::security::secureclock
56