blob: b886bd7dbd4134736a2446c0c9c38f29948008f3 [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,
David Drysdale44a18b72022-04-27 14:03:34 +010053 const optional<HardwareAuthToken>& authToken,
Shawn Willden2cb22a42021-02-19 07:50:33 -070054 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());
David Drysdale44a18b72022-04-27 14:03:34 +010058 if (authToken) {
59 auto tokenAsVec(authToken2AidlVec(*authToken));
60 request.additional_params.push_back(keymaster::TAG_AUTH_TOKEN, tokenAsVec.data(),
61 tokenAsVec.size());
62 }
Shawn Willden815e8962020-12-11 13:05:27 +000063
Shawn Willden950eb0b2021-01-06 19:15:29 +000064 UpdateOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +000065 impl_->UpdateOperation(request, &response);
66
Shawn Willden815e8962020-12-11 13:05:27 +000067 return kmError2ScopedAStatus(response.error);
68}
69
Shawn Willden2cb22a42021-02-19 07:50:33 -070070ScopedAStatus AndroidKeyMintOperation::update(const vector<uint8_t>& input,
David Drysdale44a18b72022-04-27 14:03:34 +010071 const optional<HardwareAuthToken>& authToken,
Janis Danisevskis9d64bc22021-01-05 10:32:57 -080072 const optional<TimeStampToken>&
Shawn Willden2cb22a42021-02-19 07:50:33 -070073 /* timestampToken */,
Shawn Willden815e8962020-12-11 13:05:27 +000074 vector<uint8_t>* output) {
Shawn Willden2cb22a42021-02-19 07:50:33 -070075 if (!output) return kmError2ScopedAStatus(KM_ERROR_OUTPUT_PARAMETER_NULL);
Shawn Willden815e8962020-12-11 13:05:27 +000076
Shawn Willden2cb22a42021-02-19 07:50:33 -070077 UpdateOperationRequest request(impl_->message_version());
78 request.op_handle = opHandle_;
79 request.input.Reinitialize(input.data(), input.size());
David Drysdale44a18b72022-04-27 14:03:34 +010080 if (authToken) {
81 auto tokenAsVec(authToken2AidlVec(*authToken));
82 request.additional_params.push_back(keymaster::TAG_AUTH_TOKEN, tokenAsVec.data(),
83 tokenAsVec.size());
84 }
Shawn Willden2cb22a42021-02-19 07:50:33 -070085
86 UpdateOperationResponse response(impl_->message_version());
87 impl_->UpdateOperation(request, &response);
88
89 if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error);
90 if (response.input_consumed != request.input.buffer_size()) {
91 return kmError2ScopedAStatus(KM_ERROR_UNKNOWN_ERROR);
92 }
93
94 *output = kmBuffer2vector(response.output);
95 return ScopedAStatus::ok();
96}
97
98ScopedAStatus
99AndroidKeyMintOperation::finish(const optional<vector<uint8_t>>& input, //
100 const optional<vector<uint8_t>>& signature, //
David Drysdale44a18b72022-04-27 14:03:34 +0100101 const optional<HardwareAuthToken>& authToken,
Shawn Willden2cb22a42021-02-19 07:50:33 -0700102 const optional<TimeStampToken>& /* timestampToken */,
103 const optional<vector<uint8_t>>& /* confirmationToken */,
104 vector<uint8_t>* output) {
105
106 if (!output) {
Shawn Willden815e8962020-12-11 13:05:27 +0000107 return ScopedAStatus(AStatus_fromServiceSpecificError(
108 static_cast<int32_t>(ErrorCode::OUTPUT_PARAMETER_NULL)));
109 }
110
Shawn Willden950eb0b2021-01-06 19:15:29 +0000111 FinishOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000112 request.op_handle = opHandle_;
Shawn Willden2cb22a42021-02-19 07:50:33 -0700113 if (input) request.input.Reinitialize(input->data(), input->size());
114 if (signature) request.signature.Reinitialize(signature->data(), signature->size());
David Drysdale44a18b72022-04-27 14:03:34 +0100115 if (authToken) {
116 auto tokenAsVec(authToken2AidlVec(*authToken));
117 request.additional_params.push_back(keymaster::TAG_AUTH_TOKEN, tokenAsVec.data(),
118 tokenAsVec.size());
119 }
Shawn Willden815e8962020-12-11 13:05:27 +0000120
Shawn Willden950eb0b2021-01-06 19:15:29 +0000121 FinishOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000122 impl_->FinishOperation(request, &response);
123 opHandle_ = 0;
124
Shawn Willden2cb22a42021-02-19 07:50:33 -0700125 if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error);
Shawn Willden815e8962020-12-11 13:05:27 +0000126
Shawn Willden2cb22a42021-02-19 07:50:33 -0700127 *output = kmBuffer2vector(response.output);
128 return ScopedAStatus::ok();
Shawn Willden815e8962020-12-11 13:05:27 +0000129}
130
131ScopedAStatus AndroidKeyMintOperation::abort() {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000132 AbortOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000133 request.op_handle = opHandle_;
134
Shawn Willden950eb0b2021-01-06 19:15:29 +0000135 AbortOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000136 impl_->AbortOperation(request, &response);
137 opHandle_ = 0;
138
139 return kmError2ScopedAStatus(response.error);
140}
141
142} // namespace aidl::android::hardware::security::keymint