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; |
| 35 | using ::keymaster::UpdateOperationRequest; |
| 36 | using ::keymaster::UpdateOperationResponse; |
Janis Danisevskis | 9d64bc2 | 2021-01-05 10:32:57 -0800 | [diff] [blame^] | 37 | using secureclock::TimeStampToken; |
Shawn Willden | 96d4e8c | 2020-12-07 17:03:54 -0700 | [diff] [blame] | 38 | using namespace km_utils; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 39 | |
| 40 | AndroidKeyMintOperation::AndroidKeyMintOperation( |
| 41 | const shared_ptr<::keymaster::AndroidKeymaster> implementation, |
| 42 | keymaster_operation_handle_t opHandle) |
| 43 | : impl_(std::move(implementation)), opHandle_(opHandle) {} |
| 44 | |
| 45 | AndroidKeyMintOperation::~AndroidKeyMintOperation() { |
| 46 | if (opHandle_ != 0) { |
| 47 | abort(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | ScopedAStatus AndroidKeyMintOperation::update(const optional<KeyParameterArray>& params, |
| 52 | const optional<vector<uint8_t>>& input, |
| 53 | const optional<HardwareAuthToken>& /* authToken */, |
Janis Danisevskis | 9d64bc2 | 2021-01-05 10:32:57 -0800 | [diff] [blame^] | 54 | const optional<TimeStampToken>& |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 55 | /* verificationToken */, |
| 56 | optional<KeyParameterArray>* updatedParams, |
| 57 | optional<ByteArray>* output, int32_t* inputConsumed) { |
| 58 | if (!updatedParams || !output || !inputConsumed) { |
| 59 | return kmError2ScopedAStatus(KM_ERROR_OUTPUT_PARAMETER_NULL); |
| 60 | } |
| 61 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 62 | UpdateOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 63 | request.op_handle = opHandle_; |
| 64 | if (input) { |
| 65 | request.input.Reinitialize(input->data(), input->size()); |
| 66 | } |
| 67 | |
| 68 | if (params) { |
| 69 | request.additional_params.Reinitialize(KmParamSet(params->params)); |
| 70 | } |
| 71 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 72 | UpdateOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 73 | impl_->UpdateOperation(request, &response); |
| 74 | |
| 75 | *inputConsumed = 0; |
| 76 | if (response.error == KM_ERROR_OK) { |
| 77 | *inputConsumed = response.input_consumed; |
| 78 | |
| 79 | updatedParams->emplace(); |
| 80 | (*updatedParams)->params = kmParamSet2Aidl(response.output_params); |
| 81 | |
| 82 | output->emplace(); |
| 83 | (*output)->data = kmBuffer2vector(response.output); |
| 84 | |
| 85 | return ScopedAStatus::ok(); |
| 86 | } |
| 87 | |
| 88 | opHandle_ = 0; |
| 89 | return kmError2ScopedAStatus(response.error); |
| 90 | } |
| 91 | |
| 92 | ScopedAStatus AndroidKeyMintOperation::finish(const optional<KeyParameterArray>& params, |
| 93 | const optional<vector<uint8_t>>& input, |
| 94 | const optional<vector<uint8_t>>& signature, |
| 95 | const optional<HardwareAuthToken>& /* authToken */, |
Janis Danisevskis | 9d64bc2 | 2021-01-05 10:32:57 -0800 | [diff] [blame^] | 96 | const optional<TimeStampToken>& |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 97 | /* verificationToken */, |
| 98 | optional<KeyParameterArray>* updatedParams, |
| 99 | vector<uint8_t>* output) { |
| 100 | |
| 101 | if (!updatedParams || !output) { |
| 102 | return ScopedAStatus(AStatus_fromServiceSpecificError( |
| 103 | static_cast<int32_t>(ErrorCode::OUTPUT_PARAMETER_NULL))); |
| 104 | } |
| 105 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 106 | FinishOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 107 | request.op_handle = opHandle_; |
| 108 | |
| 109 | if (input) { |
| 110 | request.input.Reinitialize(input->data(), input->size()); |
| 111 | } |
| 112 | |
| 113 | if (signature) { |
| 114 | request.signature.Reinitialize(signature->data(), signature->size()); |
| 115 | } |
| 116 | |
| 117 | if (params) { |
| 118 | request.additional_params.Reinitialize(KmParamSet(params->params)); |
| 119 | } |
| 120 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 121 | FinishOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 122 | impl_->FinishOperation(request, &response); |
| 123 | opHandle_ = 0; |
| 124 | |
| 125 | if (response.error == KM_ERROR_OK) { |
| 126 | updatedParams->emplace(); |
| 127 | (*updatedParams)->params = kmParamSet2Aidl(response.output_params); |
| 128 | |
| 129 | *output = kmBuffer2vector(response.output); |
| 130 | return ScopedAStatus::ok(); |
| 131 | } |
| 132 | |
| 133 | return kmError2ScopedAStatus(response.error); |
| 134 | } |
| 135 | |
| 136 | ScopedAStatus AndroidKeyMintOperation::abort() { |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 137 | AbortOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 138 | request.op_handle = opHandle_; |
| 139 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 140 | AbortOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 141 | impl_->AbortOperation(request, &response); |
| 142 | opHandle_ = 0; |
| 143 | |
| 144 | return kmError2ScopedAStatus(response.error); |
| 145 | } |
| 146 | |
| 147 | } // namespace aidl::android::hardware::security::keymint |