Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2018 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* The keystore refuses to allow the root user to supply auth tokens, so |
| 20 | * we write the auth token to a file in TWRP and run a separate service |
| 21 | * (this) that runs as the system user to add the auth token. TWRP waits |
| 22 | * for /auth_token to be deleted and also looks for /auth_error to check |
| 23 | * for errors. TWRP will error out after a while if /auth_token does not |
| 24 | * get deleted. */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string> |
| 28 | |
| 29 | #include <keystore/IKeystoreService.h> |
| 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <binder/IServiceManager.h> |
| 32 | |
| 33 | #include <keystore/keystore.h> |
| 34 | #include <keystore/authorization_set.h> |
| 35 | |
| 36 | #define LOG_TAG "keystore_auth" |
| 37 | |
| 38 | using namespace android; |
| 39 | |
| 40 | void create_error_file() { |
| 41 | FILE* error_file = fopen("/auth_error", "wb"); |
| 42 | if (error_file == NULL) { |
| 43 | printf("Failed to open /auth_error\n"); |
| 44 | ALOGE("Failed to open /auth_error\n"); |
| 45 | return; |
| 46 | } |
| 47 | fwrite("1", 1, 1, error_file); |
| 48 | fclose(error_file); |
| 49 | unlink("/auth_token"); |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char *argv[]) { |
| 53 | unlink("/auth_error"); |
| 54 | FILE* auth_file = fopen("/auth_token", "rb"); |
| 55 | if (auth_file == NULL) { |
| 56 | printf("Failed to open /auth_token\n"); |
| 57 | ALOGE("Failed to open /auth_token\n"); |
| 58 | create_error_file(); |
| 59 | return -1; |
| 60 | } |
| 61 | // Get the file size |
| 62 | fseek(auth_file, 0, SEEK_END); |
| 63 | int size = ftell(auth_file); |
| 64 | fseek(auth_file, 0, SEEK_SET); |
| 65 | uint8_t auth_token[size]; |
| 66 | fread(auth_token , sizeof(uint8_t), size, auth_file); |
| 67 | fclose(auth_file); |
| 68 | // First get the keystore service |
| 69 | sp<IServiceManager> sm = defaultServiceManager(); |
| 70 | sp<IBinder> binder = sm->getService(String16("android.security.keystore")); |
| 71 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
| 72 | if (service == NULL) { |
| 73 | printf("error: could not connect to keystore service\n"); |
| 74 | ALOGE("error: could not connect to keystore service\n"); |
| 75 | create_error_file(); |
| 76 | return -2; |
| 77 | } |
| 78 | ::keystore::KeyStoreServiceReturnCode auth_result = service->addAuthToken(auth_token, size); |
| 79 | if (!auth_result.isOk()) { |
| 80 | // The keystore checks the uid of the calling process and will return a permission denied on this operation for user 0 |
| 81 | printf("keystore error adding auth token\n"); |
| 82 | ALOGE("keystore error adding auth token\n"); |
| 83 | create_error_file(); |
| 84 | return -3; |
| 85 | } |
| 86 | printf("successfully added auth token to keystore\n"); |
| 87 | ALOGD("successfully added auth token to keystore\n"); |
| 88 | unlink("/auth_token"); |
| 89 | return 0; |
| 90 | } |