blob: 5ae96f6086c6d359b2e4748f777fc4f854ae21ca [file] [log] [blame]
Kenny Rootd0c4f2b2019-12-04 07:54:20 -08001/*
2 * Copyright (C) 2019 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 <android-base/file.h>
18#include <android-base/logging.h>
19#include <android-base/unique_fd.h>
20
21#include "HadamardUtils.h"
22#include "rebootescrow-impl/RebootEscrow.h"
23
24namespace aidl {
25namespace android {
26namespace hardware {
27namespace rebootescrow {
28
29using ::android::base::unique_fd;
30
31ndk::ScopedAStatus RebootEscrow::storeKey(const std::vector<int8_t>& kek) {
Kenny Root7e6f5f92020-01-24 17:36:47 -080032 int rawFd = TEMP_FAILURE_RETRY(::open(devicePath_.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Kenny Rootd0c4f2b2019-12-04 07:54:20 -080033 unique_fd fd(rawFd);
34 if (fd.get() < 0) {
35 LOG(WARNING) << "Could not open reboot escrow device";
36 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
37 }
38
39 std::vector<uint8_t> ukek(kek.begin(), kek.end());
40 auto encoded = hadamard::EncodeKey(ukek);
41
42 if (!::android::base::WriteFully(fd, encoded.data(), encoded.size())) {
43 LOG(WARNING) << "Could not write data fully to character device";
44 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
45 }
46
47 return ndk::ScopedAStatus::ok();
48}
49
50ndk::ScopedAStatus RebootEscrow::retrieveKey(std::vector<int8_t>* _aidl_return) {
Kenny Root7e6f5f92020-01-24 17:36:47 -080051 int rawFd = TEMP_FAILURE_RETRY(::open(devicePath_.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Kenny Rootd0c4f2b2019-12-04 07:54:20 -080052 unique_fd fd(rawFd);
53 if (fd.get() < 0) {
54 LOG(WARNING) << "Could not open reboot escrow device";
55 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
56 }
57
58 std::string encodedString;
59 if (!::android::base::ReadFdToString(fd, &encodedString)) {
60 LOG(WARNING) << "Could not read device to string";
61 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
62 }
63
64 std::vector<uint8_t> encodedBytes(encodedString.begin(), encodedString.end());
65 auto keyBytes = hadamard::DecodeKey(encodedBytes);
66
67 std::vector<int8_t> signedKeyBytes(keyBytes.begin(), keyBytes.end());
68 *_aidl_return = signedKeyBytes;
69 return ndk::ScopedAStatus::ok();
70}
71
72} // namespace rebootescrow
73} // namespace hardware
74} // namespace android
75} // namespace aidl