Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Danisevskis | 9d64bc2 | 2021-01-05 10:32:57 -0800 | [diff] [blame] | 23 | #include <aidl/android/hardware/security/secureclock/ISecureClock.h> |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 24 | |
| 25 | #include <keymaster/android_keymaster.h> |
| 26 | |
| 27 | #include "KeyMintUtils.h" |
| 28 | |
| 29 | namespace aidl::android::hardware::security::keymint { |
| 30 | |
| 31 | using ::keymaster::AbortOperationRequest; |
| 32 | using ::keymaster::AbortOperationResponse; |
| 33 | using ::keymaster::FinishOperationRequest; |
| 34 | using ::keymaster::FinishOperationResponse; |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 35 | using ::keymaster::TAG_ASSOCIATED_DATA; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 36 | using ::keymaster::UpdateOperationRequest; |
| 37 | using ::keymaster::UpdateOperationResponse; |
Janis Danisevskis | 9d64bc2 | 2021-01-05 10:32:57 -0800 | [diff] [blame] | 38 | using secureclock::TimeStampToken; |
Shawn Willden | 9a3792e | 2021-04-08 09:38:14 -0600 | [diff] [blame^] | 39 | using namespace km_utils; // NOLINT(google-build-using-namespace) |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 40 | |
| 41 | AndroidKeyMintOperation::AndroidKeyMintOperation( |
Shawn Willden | 9a3792e | 2021-04-08 09:38:14 -0600 | [diff] [blame^] | 42 | shared_ptr<::keymaster::AndroidKeymaster> implementation, keymaster_operation_handle_t opHandle) |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 43 | : impl_(std::move(implementation)), opHandle_(opHandle) {} |
| 44 | |
| 45 | AndroidKeyMintOperation::~AndroidKeyMintOperation() { |
| 46 | if (opHandle_ != 0) { |
| 47 | abort(); |
| 48 | } |
| 49 | } |
| 50 | |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 51 | ScopedAStatus |
| 52 | AndroidKeyMintOperation::updateAad(const vector<uint8_t>& input, |
| 53 | const optional<HardwareAuthToken>& /* authToken */, |
| 54 | const optional<TimeStampToken>& /* timestampToken */) { |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 55 | UpdateOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 56 | request.op_handle = opHandle_; |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 57 | request.additional_params.push_back(TAG_ASSOCIATED_DATA, input.data(), input.size()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 58 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 59 | UpdateOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 60 | impl_->UpdateOperation(request, &response); |
| 61 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 62 | return kmError2ScopedAStatus(response.error); |
| 63 | } |
| 64 | |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 65 | ScopedAStatus AndroidKeyMintOperation::update(const vector<uint8_t>& input, |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 66 | const optional<HardwareAuthToken>& /* authToken */, |
Janis Danisevskis | 9d64bc2 | 2021-01-05 10:32:57 -0800 | [diff] [blame] | 67 | const optional<TimeStampToken>& |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 68 | /* timestampToken */, |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 69 | vector<uint8_t>* output) { |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 70 | if (!output) return kmError2ScopedAStatus(KM_ERROR_OUTPUT_PARAMETER_NULL); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 71 | |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 72 | 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 | |
| 88 | ScopedAStatus |
| 89 | AndroidKeyMintOperation::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 Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 97 | return ScopedAStatus(AStatus_fromServiceSpecificError( |
| 98 | static_cast<int32_t>(ErrorCode::OUTPUT_PARAMETER_NULL))); |
| 99 | } |
| 100 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 101 | FinishOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 102 | request.op_handle = opHandle_; |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 103 | if (input) request.input.Reinitialize(input->data(), input->size()); |
| 104 | if (signature) request.signature.Reinitialize(signature->data(), signature->size()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 105 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 106 | FinishOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 107 | impl_->FinishOperation(request, &response); |
| 108 | opHandle_ = 0; |
| 109 | |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 110 | if (response.error != KM_ERROR_OK) return kmError2ScopedAStatus(response.error); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 111 | |
Shawn Willden | 2cb22a4 | 2021-02-19 07:50:33 -0700 | [diff] [blame] | 112 | *output = kmBuffer2vector(response.output); |
| 113 | return ScopedAStatus::ok(); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | ScopedAStatus AndroidKeyMintOperation::abort() { |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 117 | AbortOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 118 | request.op_handle = opHandle_; |
| 119 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 120 | AbortOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 121 | impl_->AbortOperation(request, &response); |
| 122 | opHandle_ = 0; |
| 123 | |
| 124 | return kmError2ScopedAStatus(response.error); |
| 125 | } |
| 126 | |
| 127 | } // namespace aidl::android::hardware::security::keymint |