12d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales/*
22d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * Copyright (C) 2015 The Android Open Source Project
32d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales *
42d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * Licensed under the Apache License, Version 2.0 (the "License");
52d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * you may not use this file except in compliance with the License.
62d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * You may obtain a copy of the License at
72d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales *
82d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales *      http://www.apache.org/licenses/LICENSE-2.0
92d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales *
102d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * Unless required by applicable law or agreed to in writing, software
112d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * distributed under the License is distributed on an "AS IS" BASIS,
122d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * See the License for the specific language governing permissions and
142d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales * limitations under the License.
152d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales */
162d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
172d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#define LOG_TAG "gatekeeperd"
182d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
192d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include "IGateKeeperService.h"
202d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
216a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <errno.h>
226a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <stdint.h>
236a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <inttypes.h>
246a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <fcntl.h>
256a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <unistd.h>
266a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
272d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <cutils/log.h>
282d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <utils/Log.h>
292d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
302d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <binder/IPCThreadState.h>
312d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <binder/IServiceManager.h>
322d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <binder/PermissionCache.h>
332d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <utils/String16.h>
34835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales#include <utils/Log.h>
352d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
362d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <keystore/IKeystoreService.h>
372ae8b4c28fc95022b2075e77c73959e51e01b6ceAndres Morales#include <keystore/keystore.h> // For error code
386a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <gatekeeper/password_handle.h> // for password_handle_t
392d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales#include <hardware/gatekeeper.h>
406a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales#include <hardware/hw_auth_token.h>
412d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
4233dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales#include "SoftGateKeeperDevice.h"
431cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales#include "IUserManager.h"
4433dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales
452d08dce0beedcfc63b2a837045d1be7d49157555Andres Moralesnamespace android {
462d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
472d08dce0beedcfc63b2a837045d1be7d49157555Andres Moralesstatic const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
482d08dce0beedcfc63b2a837045d1be7d49157555Andres Moralesstatic const String16 DUMP_PERMISSION("android.permission.DUMP");
492d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
502d08dce0beedcfc63b2a837045d1be7d49157555Andres Moralesclass GateKeeperProxy : public BnGateKeeperService {
512d08dce0beedcfc63b2a837045d1be7d49157555Andres Moralespublic:
522d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    GateKeeperProxy() {
532d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        int ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &module);
54fef908e5a50a4026bb94edabb8f500a959b9ed0eAndres Morales        device = NULL;
55fef908e5a50a4026bb94edabb8f500a959b9ed0eAndres Morales
5633dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        if (ret < 0) {
5733dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales            ALOGW("falling back to software GateKeeper");
5833dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales            soft_device.reset(new SoftGateKeeperDevice());
5933dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        } else {
6033dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales            ret = gatekeeper_open(module, &device);
6133dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales            if (ret < 0)
6233dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                LOG_ALWAYS_FATAL_IF(ret < 0, "Unable to open GateKeeper HAL");
6333dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        }
643c2086dabdb87e027412bb4405279128a321e184Andres Morales
653c2086dabdb87e027412bb4405279128a321e184Andres Morales        if (mark_cold_boot()) {
663c2086dabdb87e027412bb4405279128a321e184Andres Morales            ALOGI("cold boot: clearing state");
673c2086dabdb87e027412bb4405279128a321e184Andres Morales            if (device != NULL && device->delete_all_users != NULL) {
683c2086dabdb87e027412bb4405279128a321e184Andres Morales                device->delete_all_users(device);
693c2086dabdb87e027412bb4405279128a321e184Andres Morales            }
703c2086dabdb87e027412bb4405279128a321e184Andres Morales        }
712d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    }
722d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
732d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    virtual ~GateKeeperProxy() {
7433dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        if (device) gatekeeper_close(device);
752d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    }
762d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
776a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    void store_sid(uint32_t uid, uint64_t sid) {
786a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        char filename[21];
796a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        sprintf(filename, "%u", uid);
806a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
816a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        if (fd < 0) {
82dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales            ALOGE("could not open file: %s: %s", filename, strerror(errno));
836a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales            return;
846a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        }
856a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        write(fd, &sid, sizeof(sid));
866a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        close(fd);
876a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    }
886a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
893c2086dabdb87e027412bb4405279128a321e184Andres Morales    bool mark_cold_boot() {
903c2086dabdb87e027412bb4405279128a321e184Andres Morales        const char *filename = ".coldboot";
913c2086dabdb87e027412bb4405279128a321e184Andres Morales        if (access(filename, F_OK) == -1) {
923c2086dabdb87e027412bb4405279128a321e184Andres Morales            int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
933c2086dabdb87e027412bb4405279128a321e184Andres Morales            if (fd < 0) {
943c2086dabdb87e027412bb4405279128a321e184Andres Morales                ALOGE("could not open file: %s : %s", filename, strerror(errno));
953c2086dabdb87e027412bb4405279128a321e184Andres Morales                return false;
963c2086dabdb87e027412bb4405279128a321e184Andres Morales            }
973c2086dabdb87e027412bb4405279128a321e184Andres Morales            close(fd);
983c2086dabdb87e027412bb4405279128a321e184Andres Morales            return true;
993c2086dabdb87e027412bb4405279128a321e184Andres Morales        }
1003c2086dabdb87e027412bb4405279128a321e184Andres Morales        return false;
1013c2086dabdb87e027412bb4405279128a321e184Andres Morales    }
1023c2086dabdb87e027412bb4405279128a321e184Andres Morales
1036a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    void maybe_store_sid(uint32_t uid, uint64_t sid) {
1046a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        char filename[21];
1056a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        sprintf(filename, "%u", uid);
1066a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        if (access(filename, F_OK) == -1) {
1076a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales            store_sid(uid, sid);
1086a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        }
1096a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    }
1106a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
1116a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    uint64_t read_sid(uint32_t uid) {
1126a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        char filename[21];
1136a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        uint64_t sid;
1146a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        sprintf(filename, "%u", uid);
1156a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        int fd = open(filename, O_RDONLY);
1166a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        if (fd < 0) return 0;
1176a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        read(fd, &sid, sizeof(sid));
1180b0435ea87f87b31652a2c6272c1f77438bfcdf1Andres Morales        close(fd);
1196a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        return sid;
1206a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    }
1216a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
122dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales    void clear_sid(uint32_t uid) {
123dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales        char filename[21];
124dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales        sprintf(filename, "%u", uid);
125dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales        if (remove(filename) < 0) {
126dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales            ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
127dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales            store_sid(uid, 0);
128dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales        }
129dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales    }
130dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales
131ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales    virtual int enroll(uint32_t uid,
1322d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            const uint8_t *current_password_handle, uint32_t current_password_handle_length,
1332d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            const uint8_t *current_password, uint32_t current_password_length,
1342d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            const uint8_t *desired_password, uint32_t desired_password_length,
1352d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) {
1362d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        IPCThreadState* ipc = IPCThreadState::self();
1372d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        const int calling_pid = ipc->getCallingPid();
1382d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        const int calling_uid = ipc->getCallingUid();
1392d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
1402d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            return PERMISSION_DENIED;
1412d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        }
1422d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
1432d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        // need a desired password to enroll
1442d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        if (desired_password_length == 0) return -EINVAL;
14533dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales
14633dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        int ret;
14733dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        if (device) {
148835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales            const gatekeeper::password_handle_t *handle =
149835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle);
150835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales
1517f6dcf657680aaf51dcd5d41c32c89254779b2a8Andres Morales            if (handle != NULL && handle->version != 0 && !handle->hardware_backed) {
152835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                // handle is being re-enrolled from a software version. HAL probably won't accept
153835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                // the handle as valid, so we nullify it and enroll from scratch
154835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                current_password_handle = NULL;
155835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                current_password_handle_length = 0;
156835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                current_password = NULL;
157835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                current_password_length = 0;
158835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales            }
159835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales
160835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales            ret = device->enroll(device, uid, current_password_handle, current_password_handle_length,
16133dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    current_password, current_password_length,
16233dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    desired_password, desired_password_length,
16333dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    enrolled_password_handle, enrolled_password_handle_length);
16433dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        } else {
16533dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales            ret = soft_device->enroll(uid,
16633dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    current_password_handle, current_password_handle_length,
16733dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    current_password, current_password_length,
16833dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    desired_password, desired_password_length,
16933dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                    enrolled_password_handle, enrolled_password_handle_length);
17033dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        }
17133dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales
172ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales        if (ret == 0) {
1736a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales            gatekeeper::password_handle_t *handle =
1746a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales                    reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle);
1756a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales            store_sid(uid, handle->user_id);
176531e3e83c28a2fdb2270b26b2bf0333ed6e30682Andres Morales            bool rr;
177531e3e83c28a2fdb2270b26b2bf0333ed6e30682Andres Morales
178531e3e83c28a2fdb2270b26b2bf0333ed6e30682Andres Morales            // immediately verify this password so we don't ask the user to enter it again
179531e3e83c28a2fdb2270b26b2bf0333ed6e30682Andres Morales            // if they just created it.
180531e3e83c28a2fdb2270b26b2bf0333ed6e30682Andres Morales            verify(uid, *enrolled_password_handle, sizeof(password_handle_t), desired_password,
181531e3e83c28a2fdb2270b26b2bf0333ed6e30682Andres Morales                    desired_password_length, &rr);
1826a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        }
183ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales
184ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales        return ret;
1852d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    }
1862d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
187ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales    virtual int verify(uint32_t uid,
1882d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
189ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales            const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) {
190c828ae87768f3539cefadb7e485b877995918299Andres Morales        uint8_t *auth_token;
191c828ae87768f3539cefadb7e485b877995918299Andres Morales        uint32_t auth_token_length;
192c828ae87768f3539cefadb7e485b877995918299Andres Morales        return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length,
193c828ae87768f3539cefadb7e485b877995918299Andres Morales                provided_password, provided_password_length,
194ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales                &auth_token, &auth_token_length, request_reenroll);
195c828ae87768f3539cefadb7e485b877995918299Andres Morales    }
196c828ae87768f3539cefadb7e485b877995918299Andres Morales
197ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales    virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
198c828ae87768f3539cefadb7e485b877995918299Andres Morales            const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
199c828ae87768f3539cefadb7e485b877995918299Andres Morales            const uint8_t *provided_password, uint32_t provided_password_length,
200ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales            uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) {
2012d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        IPCThreadState* ipc = IPCThreadState::self();
2022d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        const int calling_pid = ipc->getCallingPid();
2032d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        const int calling_uid = ipc->getCallingUid();
2042d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
2052d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            return PERMISSION_DENIED;
2062d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        }
2072d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
2082d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        // can't verify if we're missing either param
2092d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        if ((enrolled_password_handle_length | provided_password_length) == 0)
2102d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            return -EINVAL;
2112d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
21233dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        int ret;
21333dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        if (device) {
214835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales            const gatekeeper::password_handle_t *handle =
215835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle);
2167f6dcf657680aaf51dcd5d41c32c89254779b2a8Andres Morales            // handle version 0 does not have hardware backed flag, and thus cannot be upgraded to
2177f6dcf657680aaf51dcd5d41c32c89254779b2a8Andres Morales            // a HAL if there was none before
2187f6dcf657680aaf51dcd5d41c32c89254779b2a8Andres Morales            if (handle->version == 0 || handle->hardware_backed) {
219835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                ret = device->verify(device, uid, challenge,
220835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    enrolled_password_handle, enrolled_password_handle_length,
221835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    provided_password, provided_password_length, auth_token, auth_token_length,
222835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    request_reenroll);
223835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales            } else {
224835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                // upgrade scenario, a HAL has been added to this device where there was none before
225835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                SoftGateKeeperDevice soft_dev;
226835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                ret = soft_dev.verify(uid, challenge,
227835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    enrolled_password_handle, enrolled_password_handle_length,
228835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    provided_password, provided_password_length, auth_token, auth_token_length,
229835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    request_reenroll);
230835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales
231835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                if (ret == 0) {
232835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    // success! re-enroll with HAL
233835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                    *request_reenroll = true;
234835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales                }
235835d96eae59aefeaa32fda3218246db51bf22fabAndres Morales            }
23633dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        } else {
23733dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales            ret = soft_device->verify(uid, challenge,
23833dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales                enrolled_password_handle, enrolled_password_handle_length,
239ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales                provided_password, provided_password_length, auth_token, auth_token_length,
240ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales                request_reenroll);
24133dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales        }
2422d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
243ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales        if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) {
2442d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            // TODO: cache service?
2452d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            sp<IServiceManager> sm = defaultServiceManager();
2462d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
2472d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
2482d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            if (service != NULL) {
2492ae8b4c28fc95022b2075e77c73959e51e01b6ceAndres Morales                status_t ret = service->addAuthToken(*auth_token, *auth_token_length);
2502ae8b4c28fc95022b2075e77c73959e51e01b6ceAndres Morales                if (ret != ResponseCode::NO_ERROR) {
2512ae8b4c28fc95022b2075e77c73959e51e01b6ceAndres Morales                    ALOGE("Falure sending auth token to KeyStore: %d", ret);
2522d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales                }
2532d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            } else {
2542d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales                ALOGE("Unable to communicate with KeyStore");
2552d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            }
2562d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        }
2572d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
258ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales        if (ret == 0) {
2596a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales            maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>(
2606a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales                        enrolled_password_handle)->user_id);
2616a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        }
2626a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
263ae242929da80d88a7db223984ec9baa5fd5949e6Andres Morales        return ret;
2646a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    }
2656a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
2666a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    virtual uint64_t getSecureUserId(uint32_t uid) {
2671cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales        uint64_t sid = read_sid(uid);
2681cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales         if (sid == 0) {
2691cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            // might be a work profile, look up the parent
2701cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            sp<IServiceManager> sm = defaultServiceManager();
2711cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            sp<IBinder> binder = sm->getService(String16("user"));
2721cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            sp<IUserManager> um = interface_cast<IUserManager>(binder);
2731cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            int32_t parent = um->getCredentialOwnerProfile(uid);
2741cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            if (parent < 0) {
2751cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales                return 0;
2761cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            } else if (parent != (int32_t) uid) {
2771cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales                return read_sid(parent);
2781cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales            }
2791cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales        }
2801cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales        return sid;
2811cf7d259e856f589657c597d46f6f53d1dbf3faeAndres Morales
2822d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    }
2832d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
2847c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales    virtual void clearSecureUserId(uint32_t uid) {
2857c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales        IPCThreadState* ipc = IPCThreadState::self();
2867c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales        const int calling_pid = ipc->getCallingPid();
2877c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales        const int calling_uid = ipc->getCallingUid();
2887c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales        if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
2897c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales            ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
2907c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales            return;
2917c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales        }
292dcb3fbdaa4bf5eca7b6a07689871e4e8f631070cAndres Morales        clear_sid(uid);
2933c2086dabdb87e027412bb4405279128a321e184Andres Morales
2943c2086dabdb87e027412bb4405279128a321e184Andres Morales        if (device != NULL && device->delete_user != NULL) {
2953c2086dabdb87e027412bb4405279128a321e184Andres Morales            device->delete_user(device, uid);
2963c2086dabdb87e027412bb4405279128a321e184Andres Morales        }
2977c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales    }
2987c9c3bc9c2d3f98ff839f73dc76750dc23693eaeAndres Morales
2992d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    virtual status_t dump(int fd, const Vector<String16> &) {
3002d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        IPCThreadState* ipc = IPCThreadState::self();
3012d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        const int pid = ipc->getCallingPid();
3022d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        const int uid = ipc->getCallingUid();
3032d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
3042d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            return PERMISSION_DENIED;
3052d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        }
3062d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
3072d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        if (device == NULL) {
3082d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            const char *result = "Device not available";
3092d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            write(fd, result, strlen(result) + 1);
3102d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        } else {
3112d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            const char *result = "OK";
3122d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            write(fd, result, strlen(result) + 1);
3132d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        }
3142d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
3152d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        return NO_ERROR;
3162d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    }
3172d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
3182d08dce0beedcfc63b2a837045d1be7d49157555Andres Moralesprivate:
3192d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    gatekeeper_device_t *device;
32033dfdc7bbc48911a8a5324b16b05e492ae653d76Andres Morales    UniquePtr<SoftGateKeeperDevice> soft_device;
3212d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    const hw_module_t *module;
3222d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales};
3232d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales}// namespace android
3242d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
3256a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Moralesint main(int argc, char* argv[]) {
3262d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    ALOGI("Starting gatekeeperd...");
3276a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    if (argc < 2) {
3286a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        ALOGE("A directory must be specified!");
3296a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        return 1;
3306a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    }
3316a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    if (chdir(argv[1]) == -1) {
3326a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        ALOGE("chdir: %s: %s", argv[1], strerror(errno));
3336a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales        return 1;
3346a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales    }
3356a49c2fa4371cad600f4a96da3d1644df862d2a5Andres Morales
3362d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    android::sp<android::IServiceManager> sm = android::defaultServiceManager();
3372d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
3382d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    android::status_t ret = sm->addService(
3392d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales            android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
3402d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    if (ret != android::OK) {
3412d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        ALOGE("Couldn't register binder service!");
3422d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales        return -1;
3432d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    }
3442d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales
3452d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    /*
3462d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales     * We're the only thread in existence, so we're just going to process
3472d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales     * Binder transaction as a single-threaded program.
3482d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales     */
3492d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    android::IPCThreadState::self()->joinThreadPool();
3502d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales    return 0;
3512d08dce0beedcfc63b2a837045d1be7d49157555Andres Morales}
352