summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gatekeeperd/Android.mk3
-rw-r--r--gatekeeperd/IGateKeeperService.cpp52
-rw-r--r--gatekeeperd/IGateKeeperService.h35
-rw-r--r--gatekeeperd/SoftGateKeeper.h127
-rw-r--r--gatekeeperd/SoftGateKeeperDevice.cpp22
-rw-r--r--gatekeeperd/SoftGateKeeperDevice.h5
-rw-r--r--gatekeeperd/gatekeeperd.cpp31
-rw-r--r--gatekeeperd/tests/Android.mk29
-rw-r--r--gatekeeperd/tests/gatekeeper_test.cpp203
9 files changed, 464 insertions, 43 deletions
diff --git a/gatekeeperd/Android.mk b/gatekeeperd/Android.mk
index be3d6fc24..ce050aee7 100644
--- a/gatekeeperd/Android.mk
+++ b/gatekeeperd/Android.mk
@@ -25,9 +25,12 @@ LOCAL_SHARED_LIBRARIES := \
25 libgatekeeper \ 25 libgatekeeper \
26 liblog \ 26 liblog \
27 libhardware \ 27 libhardware \
28 libbase \
28 libutils \ 29 libutils \
29 libcrypto \ 30 libcrypto \
30 libkeystore_binder 31 libkeystore_binder
31LOCAL_STATIC_LIBRARIES := libscrypt_static 32LOCAL_STATIC_LIBRARIES := libscrypt_static
32LOCAL_C_INCLUDES := external/scrypt/lib/crypto 33LOCAL_C_INCLUDES := external/scrypt/lib/crypto
33include $(BUILD_EXECUTABLE) 34include $(BUILD_EXECUTABLE)
35
36include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/gatekeeperd/IGateKeeperService.cpp b/gatekeeperd/IGateKeeperService.cpp
index f5bbbf1f2..95fbfd10b 100644
--- a/gatekeeperd/IGateKeeperService.cpp
+++ b/gatekeeperd/IGateKeeperService.cpp
@@ -50,18 +50,25 @@ status_t BnGateKeeperService::onTransact(
50 50
51 uint8_t *out = NULL; 51 uint8_t *out = NULL;
52 uint32_t outSize = 0; 52 uint32_t outSize = 0;
53 status_t ret = enroll(uid, currentPasswordHandle, currentPasswordHandleSize, 53 int ret = enroll(uid, currentPasswordHandle, currentPasswordHandleSize,
54 currentPassword, currentPasswordSize, desiredPassword, 54 currentPassword, currentPasswordSize, desiredPassword,
55 desiredPasswordSize, &out, &outSize); 55 desiredPasswordSize, &out, &outSize);
56 56
57 reply->writeNoException(); 57 reply->writeNoException();
58 if (ret == NO_ERROR && outSize > 0 && out != NULL) { 58 reply->writeInt32(1);
59 if (ret == 0 && outSize > 0 && out != NULL) {
60 reply->writeInt32(GATEKEEPER_RESPONSE_OK);
61 reply->writeInt32(0);
62 reply->writeInt32(outSize);
59 reply->writeInt32(outSize); 63 reply->writeInt32(outSize);
60 void *buf = reply->writeInplace(outSize); 64 void *buf = reply->writeInplace(outSize);
61 memcpy(buf, out, outSize); 65 memcpy(buf, out, outSize);
62 free(out); 66 delete[] out;
67 } else if (ret > 0) {
68 reply->writeInt32(GATEKEEPER_RESPONSE_RETRY);
69 reply->writeInt32(ret);
63 } else { 70 } else {
64 reply->writeInt32(-1); 71 reply->writeInt32(GATEKEEPER_RESPONSE_ERROR);
65 } 72 }
66 return NO_ERROR; 73 return NO_ERROR;
67 } 74 }
@@ -78,10 +85,23 @@ status_t BnGateKeeperService::onTransact(
78 static_cast<const uint8_t *>(data.readInplace(currentPasswordSize)); 85 static_cast<const uint8_t *>(data.readInplace(currentPasswordSize));
79 if (!currentPassword) currentPasswordSize = 0; 86 if (!currentPassword) currentPasswordSize = 0;
80 87
81 status_t ret = verify(uid, (uint8_t *) currentPasswordHandle, 88 bool request_reenroll = false;
82 currentPasswordHandleSize, (uint8_t *) currentPassword, currentPasswordSize); 89 int ret = verify(uid, (uint8_t *) currentPasswordHandle,
90 currentPasswordHandleSize, (uint8_t *) currentPassword, currentPasswordSize,
91 &request_reenroll);
92
83 reply->writeNoException(); 93 reply->writeNoException();
84 reply->writeInt32(ret == NO_ERROR ? 1 : 0); 94 reply->writeInt32(1);
95 if (ret == 0) {
96 reply->writeInt32(GATEKEEPER_RESPONSE_OK);
97 reply->writeInt32(request_reenroll ? 1 : 0);
98 reply->writeInt32(0); // no payload returned from this call
99 } else if (ret > 0) {
100 reply->writeInt32(GATEKEEPER_RESPONSE_RETRY);
101 reply->writeInt32(ret);
102 } else {
103 reply->writeInt32(GATEKEEPER_RESPONSE_ERROR);
104 }
85 return NO_ERROR; 105 return NO_ERROR;
86 } 106 }
87 case VERIFY_CHALLENGE: { 107 case VERIFY_CHALLENGE: {
@@ -101,17 +121,25 @@ status_t BnGateKeeperService::onTransact(
101 121
102 uint8_t *out = NULL; 122 uint8_t *out = NULL;
103 uint32_t outSize = 0; 123 uint32_t outSize = 0;
104 status_t ret = verifyChallenge(uid, challenge, (uint8_t *) currentPasswordHandle, 124 bool request_reenroll = false;
125 int ret = verifyChallenge(uid, challenge, (uint8_t *) currentPasswordHandle,
105 currentPasswordHandleSize, (uint8_t *) currentPassword, currentPasswordSize, 126 currentPasswordHandleSize, (uint8_t *) currentPassword, currentPasswordSize,
106 &out, &outSize); 127 &out, &outSize, &request_reenroll);
107 reply->writeNoException(); 128 reply->writeNoException();
108 if (ret == NO_ERROR && outSize > 0 && out != NULL) { 129 reply->writeInt32(1);
130 if (ret == 0 && outSize > 0 && out != NULL) {
131 reply->writeInt32(GATEKEEPER_RESPONSE_OK);
132 reply->writeInt32(request_reenroll ? 1 : 0);
133 reply->writeInt32(outSize);
109 reply->writeInt32(outSize); 134 reply->writeInt32(outSize);
110 void *buf = reply->writeInplace(outSize); 135 void *buf = reply->writeInplace(outSize);
111 memcpy(buf, out, outSize); 136 memcpy(buf, out, outSize);
112 free(out); 137 delete[] out;
138 } else if (ret > 0) {
139 reply->writeInt32(GATEKEEPER_RESPONSE_RETRY);
140 reply->writeInt32(ret);
113 } else { 141 } else {
114 reply->writeInt32(-1); 142 reply->writeInt32(GATEKEEPER_RESPONSE_ERROR);
115 } 143 }
116 return NO_ERROR; 144 return NO_ERROR;
117 } 145 }
diff --git a/gatekeeperd/IGateKeeperService.h b/gatekeeperd/IGateKeeperService.h
index a7773187e..f070486cd 100644
--- a/gatekeeperd/IGateKeeperService.h
+++ b/gatekeeperd/IGateKeeperService.h
@@ -35,6 +35,12 @@ public:
35 CLEAR_SECURE_USER_ID = IBinder::FIRST_CALL_TRANSACTION + 4, 35 CLEAR_SECURE_USER_ID = IBinder::FIRST_CALL_TRANSACTION + 4,
36 }; 36 };
37 37
38 enum {
39 GATEKEEPER_RESPONSE_OK = 0,
40 GATEKEEPER_RESPONSE_RETRY = 1,
41 GATEKEEPER_RESPONSE_ERROR = -1,
42 };
43
38 // DECLARE_META_INTERFACE - C++ client interface not needed 44 // DECLARE_META_INTERFACE - C++ client interface not needed
39 static const android::String16 descriptor; 45 static const android::String16 descriptor;
40 virtual const android::String16& getInterfaceDescriptor() const; 46 virtual const android::String16& getInterfaceDescriptor() const;
@@ -43,8 +49,13 @@ public:
43 49
44 /** 50 /**
45 * Enrolls a password with the GateKeeper. Returns 0 on success, negative on failure. 51 * Enrolls a password with the GateKeeper. Returns 0 on success, negative on failure.
52 * Returns:
53 * - 0 on success
54 * - A timestamp T > 0 if the call has failed due to throttling and should not
55 * be reattempted until T milliseconds have elapsed
56 * - -1 on failure
46 */ 57 */
47 virtual status_t enroll(uint32_t uid, 58 virtual int enroll(uint32_t uid,
48 const uint8_t *current_password_handle, uint32_t current_password_handle_length, 59 const uint8_t *current_password_handle, uint32_t current_password_handle_length,
49 const uint8_t *current_password, uint32_t current_password_length, 60 const uint8_t *current_password, uint32_t current_password_length,
50 const uint8_t *desired_password, uint32_t desired_password_length, 61 const uint8_t *desired_password, uint32_t desired_password_length,
@@ -52,21 +63,29 @@ public:
52 63
53 /** 64 /**
54 * Verifies a password previously enrolled with the GateKeeper. 65 * Verifies a password previously enrolled with the GateKeeper.
55 * Returns 0 on success, negative on failure. 66 * Returns:
67 * - 0 on success
68 * - A timestamp T > 0 if the call has failed due to throttling and should not
69 * be reattempted until T milliseconds have elapsed
70 * - -1 on failure
56 */ 71 */
57 virtual status_t verify(uint32_t uid, const uint8_t *enrolled_password_handle, 72 virtual int verify(uint32_t uid, const uint8_t *enrolled_password_handle,
58 uint32_t enrolled_password_handle_length, 73 uint32_t enrolled_password_handle_length,
59 const uint8_t *provided_password, uint32_t provided_password_length) = 0; 74 const uint8_t *provided_password, uint32_t provided_password_length,
75 bool *request_reenroll) = 0;
60 76
61 /** 77 /**
62 * Verifies a password previously enrolled with the GateKeeper. 78 * Verifies a password previously enrolled with the GateKeeper.
63 * Returns 0 on success, negative on failure. 79 * Returns:
80 * - 0 on success
81 * - A timestamp T > 0 if the call has failed due to throttling and should not
82 * be reattempted until T milliseconds have elapsed
83 * - -1 on failure
64 */ 84 */
65 virtual status_t verifyChallenge(uint32_t uid, uint64_t challenge, 85 virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
66 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, 86 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
67 const uint8_t *provided_password, uint32_t provided_password_length, 87 const uint8_t *provided_password, uint32_t provided_password_length,
68 uint8_t **auth_token, uint32_t *auth_token_length) = 0; 88 uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) = 0;
69
70 /** 89 /**
71 * Returns the secure user ID for the provided android user 90 * Returns the secure user ID for the provided android user
72 */ 91 */
diff --git a/gatekeeperd/SoftGateKeeper.h b/gatekeeperd/SoftGateKeeper.h
new file mode 100644
index 000000000..1ae45e633
--- /dev/null
+++ b/gatekeeperd/SoftGateKeeper.h
@@ -0,0 +1,127 @@
1/*
2 * Copyright 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 */
17
18#ifndef SOFT_GATEKEEPER_H_
19#define SOFT_GATEKEEPER_H_
20
21extern "C" {
22#include <openssl/rand.h>
23#include <crypto_scrypt.h>
24}
25
26#include <UniquePtr.h>
27#include <gatekeeper/gatekeeper.h>
28#include <iostream>
29#include <unordered_map>
30
31namespace gatekeeper {
32
33
34class SoftGateKeeper : public GateKeeper {
35public:
36 static const uint32_t SIGNATURE_LENGTH_BYTES = 32;
37
38 // scrypt params
39 static const uint64_t N = 16384;
40 static const uint32_t r = 8;
41 static const uint32_t p = 1;
42
43 static const int MAX_UINT_32_CHARS = 11;
44
45 SoftGateKeeper() {
46 key_.reset(new uint8_t[SIGNATURE_LENGTH_BYTES]);
47 memset(key_.get(), 0, SIGNATURE_LENGTH_BYTES);
48 }
49
50 virtual ~SoftGateKeeper() {
51 }
52
53 virtual bool GetAuthTokenKey(const uint8_t **auth_token_key,
54 uint32_t *length) const {
55 if (auth_token_key == NULL || length == NULL) return false;
56 *auth_token_key = const_cast<const uint8_t *>(key_.get());
57 *length = SIGNATURE_LENGTH_BYTES;
58 return true;
59 }
60
61 virtual void GetPasswordKey(const uint8_t **password_key, uint32_t *length) {
62 if (password_key == NULL || length == NULL) return;
63 *password_key = const_cast<const uint8_t *>(key_.get());
64 *length = SIGNATURE_LENGTH_BYTES;
65 }
66
67 virtual void ComputePasswordSignature(uint8_t *signature, uint32_t signature_length,
68 const uint8_t *, uint32_t, const uint8_t *password,
69 uint32_t password_length, salt_t salt) const {
70 if (signature == NULL) return;
71 crypto_scrypt(password, password_length, reinterpret_cast<uint8_t *>(&salt),
72 sizeof(salt), N, r, p, signature, signature_length);
73 }
74
75 virtual void GetRandom(void *random, uint32_t requested_length) const {
76 if (random == NULL) return;
77 RAND_pseudo_bytes((uint8_t *) random, requested_length);
78 }
79
80 virtual void ComputeSignature(uint8_t *signature, uint32_t signature_length,
81 const uint8_t *, uint32_t, const uint8_t *, const uint32_t) const {
82 if (signature == NULL) return;
83 memset(signature, 0, signature_length);
84 }
85
86 virtual uint64_t GetMillisecondsSinceBoot() const {
87 struct timespec time;
88 int res = clock_gettime(CLOCK_BOOTTIME, &time);
89 if (res < 0) return 0;
90 return (time.tv_sec * 1000) + (time.tv_nsec / 1000 / 1000);
91 }
92
93 virtual bool IsHardwareBacked() const {
94 return false;
95 }
96
97 virtual bool GetFailureRecord(uint32_t uid, secure_id_t user_id, failure_record_t *record) {
98 failure_record_t *stored = &failure_map_[uid];
99 if (user_id != stored->secure_user_id) {
100 stored->secure_user_id = user_id;
101 stored->last_checked_timestamp = 0;
102 stored->failure_counter = 0;
103 }
104 memcpy(record, stored, sizeof(*record));
105 return true;
106 }
107
108 virtual void ClearFailureRecord(uint32_t uid, secure_id_t user_id) {
109 failure_record_t *stored = &failure_map_[uid];
110 stored->secure_user_id = user_id;
111 stored->last_checked_timestamp = 0;
112 stored->failure_counter = 0;
113 }
114
115 virtual bool WriteFailureRecord(uint32_t uid, failure_record_t *record) {
116 failure_map_[uid] = *record;
117 return true;
118 }
119
120private:
121 UniquePtr<uint8_t> key_;
122 std::unordered_map<uint32_t, failure_record_t> failure_map_;
123};
124}
125
126#endif // SOFT_GATEKEEPER_H_
127
diff --git a/gatekeeperd/SoftGateKeeperDevice.cpp b/gatekeeperd/SoftGateKeeperDevice.cpp
index b96bf8d23..f5e2ce631 100644
--- a/gatekeeperd/SoftGateKeeperDevice.cpp
+++ b/gatekeeperd/SoftGateKeeperDevice.cpp
@@ -13,8 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16#include <gatekeeper/soft_gatekeeper.h> 16#include "SoftGateKeeper.h"
17
18#include "SoftGateKeeperDevice.h" 17#include "SoftGateKeeperDevice.h"
19 18
20namespace android { 19namespace android {
@@ -58,8 +57,11 @@ int SoftGateKeeperDevice::enroll(uint32_t uid,
58 57
59 impl_->Enroll(request, &response); 58 impl_->Enroll(request, &response);
60 59
61 if (response.error != ERROR_NONE) 60 if (response.error == ERROR_RETRY) {
61 return response.retry_timeout;
62 } else if (response.error != ERROR_NONE) {
62 return -EINVAL; 63 return -EINVAL;
64 }
63 65
64 *enrolled_password_handle = response.enrolled_password_handle.buffer.release(); 66 *enrolled_password_handle = response.enrolled_password_handle.buffer.release();
65 *enrolled_password_handle_length = response.enrolled_password_handle.length; 67 *enrolled_password_handle_length = response.enrolled_password_handle.length;
@@ -69,7 +71,8 @@ int SoftGateKeeperDevice::enroll(uint32_t uid,
69int SoftGateKeeperDevice::verify(uint32_t uid, 71int SoftGateKeeperDevice::verify(uint32_t uid,
70 uint64_t challenge, const uint8_t *enrolled_password_handle, 72 uint64_t challenge, const uint8_t *enrolled_password_handle,
71 uint32_t enrolled_password_handle_length, const uint8_t *provided_password, 73 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) { 74 uint32_t provided_password_length, uint8_t **auth_token, uint32_t *auth_token_length,
75 bool *request_reenroll) {
73 76
74 if (enrolled_password_handle == NULL || 77 if (enrolled_password_handle == NULL ||
75 provided_password == NULL) { 78 provided_password == NULL) {
@@ -87,14 +90,21 @@ int SoftGateKeeperDevice::verify(uint32_t uid,
87 90
88 impl_->Verify(request, &response); 91 impl_->Verify(request, &response);
89 92
90 if (response.error != ERROR_NONE) 93 if (response.error == ERROR_RETRY) {
91 return -EINVAL; 94 return response.retry_timeout;
95 } else if (response.error != ERROR_NONE) {
96 return -EINVAL;
97 }
92 98
93 if (auth_token != NULL && auth_token_length != NULL) { 99 if (auth_token != NULL && auth_token_length != NULL) {
94 *auth_token = response.auth_token.buffer.release(); 100 *auth_token = response.auth_token.buffer.release();
95 *auth_token_length = response.auth_token.length; 101 *auth_token_length = response.auth_token.length;
96 } 102 }
97 103
104 if (request_reenroll != NULL) {
105 *request_reenroll = response.request_reenroll;
106 }
107
98 return 0; 108 return 0;
99} 109}
100} // namespace android 110} // namespace android
diff --git a/gatekeeperd/SoftGateKeeperDevice.h b/gatekeeperd/SoftGateKeeperDevice.h
index c0b504737..51a85113b 100644
--- a/gatekeeperd/SoftGateKeeperDevice.h
+++ b/gatekeeperd/SoftGateKeeperDevice.h
@@ -17,7 +17,8 @@
17#ifndef SOFT_GATEKEEPER_DEVICE_H_ 17#ifndef SOFT_GATEKEEPER_DEVICE_H_
18#define SOFT_GATEKEEPER_DEVICE_H_ 18#define SOFT_GATEKEEPER_DEVICE_H_
19 19
20#include <gatekeeper/soft_gatekeeper.h> 20#include "SoftGateKeeper.h"
21
21#include <UniquePtr.h> 22#include <UniquePtr.h>
22 23
23using namespace gatekeeper; 24using namespace gatekeeper;
@@ -65,7 +66,7 @@ public:
65 int verify(uint32_t uid, uint64_t challenge, 66 int verify(uint32_t uid, uint64_t challenge,
66 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, 67 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
67 const uint8_t *provided_password, uint32_t provided_password_length, 68 const uint8_t *provided_password, uint32_t provided_password_length,
68 uint8_t **auth_token, uint32_t *auth_token_length); 69 uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll);
69private: 70private:
70 UniquePtr<GateKeeper> impl_; 71 UniquePtr<GateKeeper> impl_;
71}; 72};
diff --git a/gatekeeperd/gatekeeperd.cpp b/gatekeeperd/gatekeeperd.cpp
index 72c7ba2c2..ad16fa5dd 100644
--- a/gatekeeperd/gatekeeperd.cpp
+++ b/gatekeeperd/gatekeeperd.cpp
@@ -102,7 +102,7 @@ public:
102 } 102 }
103 } 103 }
104 104
105 virtual status_t enroll(uint32_t uid, 105 virtual int enroll(uint32_t uid,
106 const uint8_t *current_password_handle, uint32_t current_password_handle_length, 106 const uint8_t *current_password_handle, uint32_t current_password_handle_length,
107 const uint8_t *current_password, uint32_t current_password_length, 107 const uint8_t *current_password, uint32_t current_password_length,
108 const uint8_t *desired_password, uint32_t desired_password_length, 108 const uint8_t *desired_password, uint32_t desired_password_length,
@@ -132,29 +132,29 @@ public:
132 enrolled_password_handle, enrolled_password_handle_length); 132 enrolled_password_handle, enrolled_password_handle_length);
133 } 133 }
134 134
135 if (ret >= 0) { 135 if (ret == 0) {
136 gatekeeper::password_handle_t *handle = 136 gatekeeper::password_handle_t *handle =
137 reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle); 137 reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle);
138 store_sid(uid, handle->user_id); 138 store_sid(uid, handle->user_id);
139 return NO_ERROR;
140 } 139 }
141 return UNKNOWN_ERROR; 140
141 return ret;
142 } 142 }
143 143
144 virtual status_t verify(uint32_t uid, 144 virtual int verify(uint32_t uid,
145 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, 145 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
146 const uint8_t *provided_password, uint32_t provided_password_length) { 146 const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) {
147 uint8_t *auth_token; 147 uint8_t *auth_token;
148 uint32_t auth_token_length; 148 uint32_t auth_token_length;
149 return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length, 149 return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length,
150 provided_password, provided_password_length, 150 provided_password, provided_password_length,
151 &auth_token, &auth_token_length); 151 &auth_token, &auth_token_length, request_reenroll);
152 } 152 }
153 153
154 virtual status_t verifyChallenge(uint32_t uid, uint64_t challenge, 154 virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
155 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, 155 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
156 const uint8_t *provided_password, uint32_t provided_password_length, 156 const uint8_t *provided_password, uint32_t provided_password_length,
157 uint8_t **auth_token, uint32_t *auth_token_length) { 157 uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) {
158 IPCThreadState* ipc = IPCThreadState::self(); 158 IPCThreadState* ipc = IPCThreadState::self();
159 const int calling_pid = ipc->getCallingPid(); 159 const int calling_pid = ipc->getCallingPid();
160 const int calling_uid = ipc->getCallingUid(); 160 const int calling_uid = ipc->getCallingUid();
@@ -170,14 +170,16 @@ public:
170 if (device) { 170 if (device) {
171 ret = device->verify(device, uid, challenge, 171 ret = device->verify(device, uid, challenge,
172 enrolled_password_handle, enrolled_password_handle_length, 172 enrolled_password_handle, enrolled_password_handle_length,
173 provided_password, provided_password_length, auth_token, auth_token_length); 173 provided_password, provided_password_length, auth_token, auth_token_length,
174 request_reenroll);
174 } else { 175 } else {
175 ret = soft_device->verify(uid, challenge, 176 ret = soft_device->verify(uid, challenge,
176 enrolled_password_handle, enrolled_password_handle_length, 177 enrolled_password_handle, enrolled_password_handle_length,
177 provided_password, provided_password_length, auth_token, auth_token_length); 178 provided_password, provided_password_length, auth_token, auth_token_length,
179 request_reenroll);
178 } 180 }
179 181
180 if (ret >= 0 && *auth_token != NULL && *auth_token_length > 0) { 182 if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) {
181 // TODO: cache service? 183 // TODO: cache service?
182 sp<IServiceManager> sm = defaultServiceManager(); 184 sp<IServiceManager> sm = defaultServiceManager();
183 sp<IBinder> binder = sm->getService(String16("android.security.keystore")); 185 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
@@ -192,13 +194,12 @@ public:
192 } 194 }
193 } 195 }
194 196
195 if (ret >= 0) { 197 if (ret == 0) {
196 maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>( 198 maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>(
197 enrolled_password_handle)->user_id); 199 enrolled_password_handle)->user_id);
198 return NO_ERROR;
199 } 200 }
200 201
201 return UNKNOWN_ERROR; 202 return ret;
202 } 203 }
203 204
204 virtual uint64_t getSecureUserId(uint32_t uid) { 205 virtual uint64_t getSecureUserId(uint32_t uid) {
diff --git a/gatekeeperd/tests/Android.mk b/gatekeeperd/tests/Android.mk
new file mode 100644
index 000000000..f56eeb251
--- /dev/null
+++ b/gatekeeperd/tests/Android.mk
@@ -0,0 +1,29 @@
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
17LOCAL_PATH:= $(call my-dir)
18
19include $(CLEAR_VARS)
20LOCAL_MODULE := gatekeeperd-unit-tests
21LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
22LOCAL_CFLAGS += -g -Wall -Werror -std=gnu++11 -Wno-missing-field-initializers
23LOCAL_SHARED_LIBRARIES := libgatekeeper libcrypto
24LOCAL_STATIC_LIBRARIES := libscrypt_static
25LOCAL_C_INCLUDES := external/scrypt/lib/crypto
26LOCAL_SRC_FILES := \
27 gatekeeper_test.cpp \
28include $(BUILD_NATIVE_TEST)
29
diff --git a/gatekeeperd/tests/gatekeeper_test.cpp b/gatekeeperd/tests/gatekeeper_test.cpp
new file mode 100644
index 000000000..c5e7087f1
--- /dev/null
+++ b/gatekeeperd/tests/gatekeeper_test.cpp
@@ -0,0 +1,203 @@
1/*
2 * Copyright 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
17#include <gtest/gtest.h>
18#include <UniquePtr.h>
19#include <iostream>
20
21#include <gatekeeper/soft_gatekeeper.h>
22#include <hardware/hw_auth_token.h>
23
24using ::gatekeeper::SizedBuffer;
25using ::testing::Test;
26using ::gatekeeper::EnrollRequest;
27using ::gatekeeper::EnrollResponse;
28using ::gatekeeper::VerifyRequest;
29using ::gatekeeper::VerifyResponse;
30using ::gatekeeper::SoftGateKeeper;
31using ::gatekeeper::secure_id_t;
32
33static void do_enroll(SoftGateKeeper &gatekeeper, EnrollResponse *response) {
34 SizedBuffer password;
35
36 password.buffer.reset(new uint8_t[16]);
37 password.length = 16;
38 memset(password.buffer.get(), 0, 16);
39 EnrollRequest request(0, NULL, &password, NULL);
40
41 gatekeeper.Enroll(request, response);
42}
43
44TEST(GateKeeperTest, EnrollSuccess) {
45 SoftGateKeeper gatekeeper;
46 EnrollResponse response;
47 do_enroll(gatekeeper, &response);
48 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
49}
50
51TEST(GateKeeperTest, EnrollBogusData) {
52 SoftGateKeeper gatekeeper;
53 SizedBuffer password;
54 EnrollResponse response;
55
56 EnrollRequest request(0, NULL, &password, NULL);
57
58 gatekeeper.Enroll(request, &response);
59
60 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_INVALID, response.error);
61}
62
63TEST(GateKeeperTest, VerifySuccess) {
64 SoftGateKeeper gatekeeper;
65 SizedBuffer provided_password;
66 EnrollResponse enroll_response;
67
68 provided_password.buffer.reset(new uint8_t[16]);
69 provided_password.length = 16;
70 memset(provided_password.buffer.get(), 0, 16);
71
72 do_enroll(gatekeeper, &enroll_response);
73 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
74 VerifyRequest request(0, 1, &enroll_response.enrolled_password_handle,
75 &provided_password);
76 VerifyResponse response;
77
78 gatekeeper.Verify(request, &response);
79
80 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
81
82 hw_auth_token_t *auth_token =
83 reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get());
84
85 ASSERT_EQ((uint32_t) HW_AUTH_PASSWORD, auth_token->authenticator_type);
86 ASSERT_EQ((uint64_t) 1, auth_token->challenge);
87 ASSERT_NE(~((uint32_t) 0), auth_token->timestamp);
88 ASSERT_NE((uint64_t) 0, auth_token->user_id);
89 ASSERT_NE((uint64_t) 0, auth_token->authenticator_id);
90}
91
92TEST(GateKeeperTest, TrustedReEnroll) {
93 SoftGateKeeper gatekeeper;
94 SizedBuffer provided_password;
95 EnrollResponse enroll_response;
96 SizedBuffer password_handle;
97
98 // do_enroll enrolls an all 0 password
99 provided_password.buffer.reset(new uint8_t[16]);
100 provided_password.length = 16;
101 memset(provided_password.buffer.get(), 0, 16);
102 do_enroll(gatekeeper, &enroll_response);
103 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
104
105 // keep a copy of the handle
106 password_handle.buffer.reset(new uint8_t[enroll_response.enrolled_password_handle.length]);
107 password_handle.length = enroll_response.enrolled_password_handle.length;
108 memcpy(password_handle.buffer.get(), enroll_response.enrolled_password_handle.buffer.get(),
109 password_handle.length);
110
111 // verify first password
112 VerifyRequest request(0, 0, &enroll_response.enrolled_password_handle,
113 &provided_password);
114 VerifyResponse response;
115 gatekeeper.Verify(request, &response);
116 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
117 hw_auth_token_t *auth_token =
118 reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get());
119
120 secure_id_t secure_id = auth_token->user_id;
121
122 // enroll new password
123 provided_password.buffer.reset(new uint8_t[16]);
124 provided_password.length = 16;
125 memset(provided_password.buffer.get(), 0, 16);
126 SizedBuffer password;
127 password.buffer.reset(new uint8_t[16]);
128 memset(password.buffer.get(), 1, 16);
129 password.length = 16;
130 EnrollRequest enroll_request(0, &password_handle, &password, &provided_password);
131 gatekeeper.Enroll(enroll_request, &enroll_response);
132 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
133
134 // verify new password
135 password.buffer.reset(new uint8_t[16]);
136 memset(password.buffer.get(), 1, 16);
137 password.length = 16;
138 VerifyRequest new_request(0, 0, &enroll_response.enrolled_password_handle,
139 &password);
140 gatekeeper.Verify(new_request, &response);
141 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
142 ASSERT_EQ(secure_id,
143 reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get())->user_id);
144}
145
146
147TEST(GateKeeperTest, UntrustedReEnroll) {
148 SoftGateKeeper gatekeeper;
149 SizedBuffer provided_password;
150 EnrollResponse enroll_response;
151
152 // do_enroll enrolls an all 0 password
153 provided_password.buffer.reset(new uint8_t[16]);
154 provided_password.length = 16;
155 memset(provided_password.buffer.get(), 0, 16);
156 do_enroll(gatekeeper, &enroll_response);
157 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
158
159 // verify first password
160 VerifyRequest request(0, 0, &enroll_response.enrolled_password_handle,
161 &provided_password);
162 VerifyResponse response;
163 gatekeeper.Verify(request, &response);
164 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
165 hw_auth_token_t *auth_token =
166 reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get());
167
168 secure_id_t secure_id = auth_token->user_id;
169
170 // enroll new password
171 SizedBuffer password;
172 password.buffer.reset(new uint8_t[16]);
173 memset(password.buffer.get(), 1, 16);
174 password.length = 16;
175 EnrollRequest enroll_request(0, NULL, &password, NULL);
176 gatekeeper.Enroll(enroll_request, &enroll_response);
177 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
178
179 // verify new password
180 password.buffer.reset(new uint8_t[16]);
181 memset(password.buffer.get(), 1, 16);
182 password.length = 16;
183 VerifyRequest new_request(0, 0, &enroll_response.enrolled_password_handle,
184 &password);
185 gatekeeper.Verify(new_request, &response);
186 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
187 ASSERT_NE(secure_id,
188 reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get())->user_id);
189}
190
191
192TEST(GateKeeperTest, VerifyBogusData) {
193 SoftGateKeeper gatekeeper;
194 SizedBuffer provided_password;
195 SizedBuffer password_handle;
196 VerifyResponse response;
197
198 VerifyRequest request(0, 0, &provided_password, &password_handle);
199
200 gatekeeper.Verify(request, &response);
201
202 ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_INVALID, response.error);
203}