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