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