blob: ffd4f24bccad4542c23ca6ebf734efc033237d07 [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;
Shawn Willden2cb22a42021-02-19 07:50:33 -070033using ::keymaster::Buffer;
Shawn Willden815e8962020-12-11 13:05:27 +000034using ::keymaster::FinishOperationRequest;
35using ::keymaster::FinishOperationResponse;
Shawn Willden2cb22a42021-02-19 07:50:33 -070036using ::keymaster::TAG_ASSOCIATED_DATA;
Shawn Willden815e8962020-12-11 13:05:27 +000037using ::keymaster::UpdateOperationRequest;
38using ::keymaster::UpdateOperationResponse;
Janis Danisevskis9d64bc22021-01-05 10:32:57 -080039using secureclock::TimeStampToken;
Shawn Willden96d4e8c2020-12-07 17:03:54 -070040using namespace km_utils;
Shawn Willden815e8962020-12-11 13:05:27 +000041
42AndroidKeyMintOperation::AndroidKeyMintOperation(
43 const shared_ptr<::keymaster::AndroidKeymaster> implementation,
44 keymaster_operation_handle_t opHandle)
45 : impl_(std::move(implementation)), opHandle_(opHandle) {}
46
47AndroidKeyMintOperation::~AndroidKeyMintOperation() {
48 if (opHandle_ != 0) {
49 abort();
50 }
51}
52
Shawn Willden2cb22a42021-02-19 07:50:33 -070053ScopedAStatus
54AndroidKeyMintOperation::updateAad(const vector<uint8_t>& input,
55 const optional<HardwareAuthToken>& /* authToken */,
56 const optional<TimeStampToken>& /* timestampToken */) {
Shawn Willden950eb0b2021-01-06 19:15:29 +000057 UpdateOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +000058 request.op_handle = opHandle_;
Shawn Willden2cb22a42021-02-19 07:50:33 -070059 request.additional_params.push_back(TAG_ASSOCIATED_DATA, input.data(), input.size());
Shawn Willden815e8962020-12-11 13:05:27 +000060
Shawn Willden950eb0b2021-01-06 19:15:29 +000061 UpdateOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +000062 impl_->UpdateOperation(request, &response);
63
Shawn Willden815e8962020-12-11 13:05:27 +000064 return kmError2ScopedAStatus(response.error);
65}
66
Shawn Willden2cb22a42021-02-19 07:50:33 -070067ScopedAStatus AndroidKeyMintOperation::update(const vector<uint8_t>& input,
Shawn Willden815e8962020-12-11 13:05:27 +000068 const optional<HardwareAuthToken>& /* authToken */,
Janis Danisevskis9d64bc22021-01-05 10:32:57 -080069 const optional<TimeStampToken>&
Shawn Willden2cb22a42021-02-19 07:50:33 -070070 /* timestampToken */,
Shawn Willden815e8962020-12-11 13:05:27 +000071 vector<uint8_t>* output) {
Shawn Willden2cb22a42021-02-19 07:50:33 -070072 if (!output) return kmError2ScopedAStatus(KM_ERROR_OUTPUT_PARAMETER_NULL);
Shawn Willden815e8962020-12-11 13:05:27 +000073
Shawn Willden2cb22a42021-02-19 07:50:33 -070074 UpdateOperationRequest request(impl_->message_version());
75 request.op_handle = opHandle_;
76 request.input.Reinitialize(input.data(), input.size());
77
78 UpdateOperationResponse response(impl_->message_version());
79 impl_->UpdateOperation(request, &response);
80
81 if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error);
82 if (response.input_consumed != request.input.buffer_size()) {
83 return kmError2ScopedAStatus(KM_ERROR_UNKNOWN_ERROR);
84 }
85
86 *output = kmBuffer2vector(response.output);
87 return ScopedAStatus::ok();
88}
89
90ScopedAStatus
91AndroidKeyMintOperation::finish(const optional<vector<uint8_t>>& input, //
92 const optional<vector<uint8_t>>& signature, //
93 const optional<HardwareAuthToken>& /* authToken */,
94 const optional<TimeStampToken>& /* timestampToken */,
95 const optional<vector<uint8_t>>& /* confirmationToken */,
96 vector<uint8_t>* output) {
97
98 if (!output) {
Shawn Willden815e8962020-12-11 13:05:27 +000099 return ScopedAStatus(AStatus_fromServiceSpecificError(
100 static_cast<int32_t>(ErrorCode::OUTPUT_PARAMETER_NULL)));
101 }
102
Shawn Willden950eb0b2021-01-06 19:15:29 +0000103 FinishOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000104 request.op_handle = opHandle_;
Shawn Willden2cb22a42021-02-19 07:50:33 -0700105 if (input) request.input.Reinitialize(input->data(), input->size());
106 if (signature) request.signature.Reinitialize(signature->data(), signature->size());
Shawn Willden815e8962020-12-11 13:05:27 +0000107
Shawn Willden950eb0b2021-01-06 19:15:29 +0000108 FinishOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000109 impl_->FinishOperation(request, &response);
110 opHandle_ = 0;
111
Shawn Willden2cb22a42021-02-19 07:50:33 -0700112 if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error);
Shawn Willden815e8962020-12-11 13:05:27 +0000113
Shawn Willden2cb22a42021-02-19 07:50:33 -0700114 *output = kmBuffer2vector(response.output);
115 return ScopedAStatus::ok();
Shawn Willden815e8962020-12-11 13:05:27 +0000116}
117
118ScopedAStatus AndroidKeyMintOperation::abort() {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000119 AbortOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000120 request.op_handle = opHandle_;
121
Shawn Willden950eb0b2021-01-06 19:15:29 +0000122 AbortOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000123 impl_->AbortOperation(request, &response);
124 opHandle_ = 0;
125
126 return kmError2ScopedAStatus(response.error);
127}
128
129} // namespace aidl::android::hardware::security::keymint