blob: 17f6b330f1297d81c6aeb01453e2745253e95fa2 [file] [log] [blame]
Shawn Willden815e8962020-12-11 13:05:27 +00001/*
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.keymint-impl"
18#include <log/log.h>
19
20#include "AndroidKeyMintOperation.h"
21
22#include <aidl/android/hardware/security/keymint/ErrorCode.h>
Janis Danisevskis9d64bc22021-01-05 10:32:57 -080023#include <aidl/android/hardware/security/secureclock/ISecureClock.h>
Shawn Willden815e8962020-12-11 13:05:27 +000024
25#include <keymaster/android_keymaster.h>
26
27#include "KeyMintUtils.h"
28
29namespace aidl::android::hardware::security::keymint {
30
31using ::keymaster::AbortOperationRequest;
32using ::keymaster::AbortOperationResponse;
33using ::keymaster::FinishOperationRequest;
34using ::keymaster::FinishOperationResponse;
Shawn Willden2cb22a42021-02-19 07:50:33 -070035using ::keymaster::TAG_ASSOCIATED_DATA;
Shawn Willden815e8962020-12-11 13:05:27 +000036using ::keymaster::UpdateOperationRequest;
37using ::keymaster::UpdateOperationResponse;
Janis Danisevskis9d64bc22021-01-05 10:32:57 -080038using secureclock::TimeStampToken;
Shawn Willden9a3792e2021-04-08 09:38:14 -060039using namespace km_utils; // NOLINT(google-build-using-namespace)
Shawn Willden815e8962020-12-11 13:05:27 +000040
41AndroidKeyMintOperation::AndroidKeyMintOperation(
Shawn Willden9a3792e2021-04-08 09:38:14 -060042 shared_ptr<::keymaster::AndroidKeymaster> implementation, keymaster_operation_handle_t opHandle)
Shawn Willden815e8962020-12-11 13:05:27 +000043 : impl_(std::move(implementation)), opHandle_(opHandle) {}
44
45AndroidKeyMintOperation::~AndroidKeyMintOperation() {
46 if (opHandle_ != 0) {
47 abort();
48 }
49}
50
Shawn Willden2cb22a42021-02-19 07:50:33 -070051ScopedAStatus
52AndroidKeyMintOperation::updateAad(const vector<uint8_t>& input,
53 const optional<HardwareAuthToken>& /* authToken */,
54 const optional<TimeStampToken>& /* timestampToken */) {
Shawn Willden950eb0b2021-01-06 19:15:29 +000055 UpdateOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +000056 request.op_handle = opHandle_;
Shawn Willden2cb22a42021-02-19 07:50:33 -070057 request.additional_params.push_back(TAG_ASSOCIATED_DATA, input.data(), input.size());
Shawn Willden815e8962020-12-11 13:05:27 +000058
Shawn Willden950eb0b2021-01-06 19:15:29 +000059 UpdateOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +000060 impl_->UpdateOperation(request, &response);
61
Shawn Willden815e8962020-12-11 13:05:27 +000062 return kmError2ScopedAStatus(response.error);
63}
64
Shawn Willden2cb22a42021-02-19 07:50:33 -070065ScopedAStatus AndroidKeyMintOperation::update(const vector<uint8_t>& input,
Shawn Willden815e8962020-12-11 13:05:27 +000066 const optional<HardwareAuthToken>& /* authToken */,
Janis Danisevskis9d64bc22021-01-05 10:32:57 -080067 const optional<TimeStampToken>&
Shawn Willden2cb22a42021-02-19 07:50:33 -070068 /* timestampToken */,
Shawn Willden815e8962020-12-11 13:05:27 +000069 vector<uint8_t>* output) {
Shawn Willden2cb22a42021-02-19 07:50:33 -070070 if (!output) return kmError2ScopedAStatus(KM_ERROR_OUTPUT_PARAMETER_NULL);
Shawn Willden815e8962020-12-11 13:05:27 +000071
Shawn Willden2cb22a42021-02-19 07:50:33 -070072 UpdateOperationRequest request(impl_->message_version());
73 request.op_handle = opHandle_;
74 request.input.Reinitialize(input.data(), input.size());
75
76 UpdateOperationResponse response(impl_->message_version());
77 impl_->UpdateOperation(request, &response);
78
79 if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error);
80 if (response.input_consumed != request.input.buffer_size()) {
81 return kmError2ScopedAStatus(KM_ERROR_UNKNOWN_ERROR);
82 }
83
84 *output = kmBuffer2vector(response.output);
85 return ScopedAStatus::ok();
86}
87
88ScopedAStatus
89AndroidKeyMintOperation::finish(const optional<vector<uint8_t>>& input, //
90 const optional<vector<uint8_t>>& signature, //
91 const optional<HardwareAuthToken>& /* authToken */,
92 const optional<TimeStampToken>& /* timestampToken */,
93 const optional<vector<uint8_t>>& /* confirmationToken */,
94 vector<uint8_t>* output) {
95
96 if (!output) {
Shawn Willden815e8962020-12-11 13:05:27 +000097 return ScopedAStatus(AStatus_fromServiceSpecificError(
98 static_cast<int32_t>(ErrorCode::OUTPUT_PARAMETER_NULL)));
99 }
100
Shawn Willden950eb0b2021-01-06 19:15:29 +0000101 FinishOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000102 request.op_handle = opHandle_;
Shawn Willden2cb22a42021-02-19 07:50:33 -0700103 if (input) request.input.Reinitialize(input->data(), input->size());
104 if (signature) request.signature.Reinitialize(signature->data(), signature->size());
Shawn Willden815e8962020-12-11 13:05:27 +0000105
Shawn Willden950eb0b2021-01-06 19:15:29 +0000106 FinishOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000107 impl_->FinishOperation(request, &response);
108 opHandle_ = 0;
109
Shawn Willden2cb22a42021-02-19 07:50:33 -0700110 if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error);
Shawn Willden815e8962020-12-11 13:05:27 +0000111
Shawn Willden2cb22a42021-02-19 07:50:33 -0700112 *output = kmBuffer2vector(response.output);
113 return ScopedAStatus::ok();
Shawn Willden815e8962020-12-11 13:05:27 +0000114}
115
116ScopedAStatus AndroidKeyMintOperation::abort() {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000117 AbortOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000118 request.op_handle = opHandle_;
119
Shawn Willden950eb0b2021-01-06 19:15:29 +0000120 AbortOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000121 impl_->AbortOperation(request, &response);
122 opHandle_ = 0;
123
124 return kmError2ScopedAStatus(response.error);
125}
126
127} // namespace aidl::android::hardware::security::keymint