Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include <gatekeeper/soft_gatekeeper.h> |
| 17 | |
| 18 | #include "SoftGateKeeperDevice.h" |
| 19 | |
| 20 | namespace android { |
| 21 | |
| 22 | int SoftGateKeeperDevice::enroll(uint32_t uid, |
| 23 | const uint8_t *current_password_handle, uint32_t current_password_handle_length, |
| 24 | const uint8_t *current_password, uint32_t current_password_length, |
| 25 | const uint8_t *desired_password, uint32_t desired_password_length, |
| 26 | uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) { |
| 27 | |
| 28 | if (enrolled_password_handle == NULL || enrolled_password_handle_length == NULL || |
| 29 | desired_password == NULL || desired_password_length == 0) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | // Current password and current password handle go together |
| 33 | if (current_password_handle == NULL || current_password_handle_length == 0 || |
| 34 | current_password == NULL || current_password_length == 0) { |
| 35 | current_password_handle = NULL; |
| 36 | current_password_handle_length = 0; |
| 37 | current_password = NULL; |
| 38 | current_password_length = 0; |
| 39 | } |
| 40 | |
| 41 | SizedBuffer desired_password_buffer(desired_password_length); |
| 42 | memcpy(desired_password_buffer.buffer.get(), desired_password, desired_password_length); |
| 43 | |
| 44 | SizedBuffer current_password_handle_buffer(current_password_handle_length); |
| 45 | if (current_password_handle) { |
| 46 | memcpy(current_password_handle_buffer.buffer.get(), current_password_handle, |
| 47 | current_password_handle_length); |
| 48 | } |
| 49 | |
| 50 | SizedBuffer current_password_buffer(current_password_length); |
| 51 | if (current_password) { |
| 52 | memcpy(current_password_buffer.buffer.get(), current_password, current_password_length); |
| 53 | } |
| 54 | |
| 55 | EnrollRequest request(uid, ¤t_password_handle_buffer, &desired_password_buffer, |
| 56 | ¤t_password_buffer); |
| 57 | EnrollResponse response; |
| 58 | |
| 59 | impl_->Enroll(request, &response); |
| 60 | |
| 61 | if (response.error != ERROR_NONE) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | *enrolled_password_handle = response.enrolled_password_handle.buffer.release(); |
| 65 | *enrolled_password_handle_length = response.enrolled_password_handle.length; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int SoftGateKeeperDevice::verify(uint32_t uid, |
| 70 | uint64_t challenge, const uint8_t *enrolled_password_handle, |
| 71 | uint32_t enrolled_password_handle_length, const uint8_t *provided_password, |
| 72 | uint32_t provided_password_length, uint8_t **auth_token, uint32_t *auth_token_length) { |
| 73 | |
| 74 | if (enrolled_password_handle == NULL || |
| 75 | provided_password == NULL) { |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | SizedBuffer password_handle_buffer(enrolled_password_handle_length); |
| 80 | memcpy(password_handle_buffer.buffer.get(), enrolled_password_handle, |
| 81 | enrolled_password_handle_length); |
| 82 | SizedBuffer provided_password_buffer(provided_password_length); |
| 83 | memcpy(provided_password_buffer.buffer.get(), provided_password, provided_password_length); |
| 84 | |
| 85 | VerifyRequest request(uid, challenge, &password_handle_buffer, &provided_password_buffer); |
| 86 | VerifyResponse response; |
| 87 | |
| 88 | impl_->Verify(request, &response); |
| 89 | |
| 90 | if (response.error != ERROR_NONE) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | if (auth_token != NULL && auth_token_length != NULL) { |
| 94 | *auth_token = response.auth_token.buffer.release(); |
| 95 | *auth_token_length = response.auth_token.length; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | } // namespace android |