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