• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
24 namespace aidl {
25 namespace android {
26 namespace hardware {
27 namespace rebootescrow {
28 
29 using ::android::base::unique_fd;
30 
storeKey(const std::vector<int8_t> & kek)31 ndk::ScopedAStatus RebootEscrow::storeKey(const std::vector<int8_t>& kek) {
32     int rawFd = TEMP_FAILURE_RETRY(::open(devicePath_.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
33     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 
retrieveKey(std::vector<int8_t> * _aidl_return)50 ndk::ScopedAStatus RebootEscrow::retrieveKey(std::vector<int8_t>* _aidl_return) {
51     int rawFd = TEMP_FAILURE_RETRY(::open(devicePath_.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
52     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::vector<uint8_t> encodedBytes(hadamard::OUTPUT_SIZE_BYTES);
59     if (!::android::base::ReadFully(fd, &encodedBytes[0], encodedBytes.size())) {
60         LOG(WARNING) << "Could not read device";
61         return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
62     }
63 
64     auto keyBytes = hadamard::DecodeKey(encodedBytes);
65 
66     std::vector<int8_t> signedKeyBytes(keyBytes.begin(), keyBytes.end());
67     *_aidl_return = signedKeyBytes;
68     return ndk::ScopedAStatus::ok();
69 }
70 
71 }  // namespace rebootescrow
72 }  // namespace hardware
73 }  // namespace android
74 }  // namespace aidl
75