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