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