Home | History | Annotate | Download | only in keystore
      1 /*
      2  * Copyright (C) 2016 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 #define LOG_TAG "keystore"
     18 
     19 #include "keystore_utils.h"
     20 
     21 #include <errno.h>
     22 #include <string.h>
     23 #include <unistd.h>
     24 
     25 #include <cutils/log.h>
     26 #include <private/android_filesystem_config.h>
     27 #include <private/android_logger.h>
     28 
     29 #include <log/log_event_list.h>
     30 
     31 #include <keystore/keymaster_types.h>
     32 #include <keystore/keystore_client.h>
     33 
     34 #include "blob.h"
     35 
     36 size_t readFully(int fd, uint8_t* data, size_t size) {
     37     size_t remaining = size;
     38     while (remaining > 0) {
     39         ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining));
     40         if (n <= 0) {
     41             return size - remaining;
     42         }
     43         data += n;
     44         remaining -= n;
     45     }
     46     return size;
     47 }
     48 
     49 size_t writeFully(int fd, uint8_t* data, size_t size) {
     50     size_t remaining = size;
     51     while (remaining > 0) {
     52         ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining));
     53         if (n < 0) {
     54             ALOGW("write failed: %s", strerror(errno));
     55             return size - remaining;
     56         }
     57         data += n;
     58         remaining -= n;
     59     }
     60     if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) {
     61         ALOGW("fsync failed: %s", strerror(errno));
     62         return -1;
     63     }
     64     return size;
     65 }
     66 
     67 void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params) {
     68     using namespace keystore;
     69     params->push_back(TAG_PURPOSE, KeyPurpose::SIGN);
     70     params->push_back(TAG_PURPOSE, KeyPurpose::VERIFY);
     71     params->push_back(TAG_PURPOSE, KeyPurpose::ENCRYPT);
     72     params->push_back(TAG_PURPOSE, KeyPurpose::DECRYPT);
     73     params->push_back(TAG_PADDING, PaddingMode::NONE);
     74     if (keyType == EVP_PKEY_RSA) {
     75         params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_SIGN);
     76         params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_ENCRYPT);
     77         params->push_back(TAG_PADDING, PaddingMode::RSA_PSS);
     78         params->push_back(TAG_PADDING, PaddingMode::RSA_OAEP);
     79     }
     80     params->push_back(TAG_DIGEST, Digest::NONE);
     81     params->push_back(TAG_DIGEST, Digest::MD5);
     82     params->push_back(TAG_DIGEST, Digest::SHA1);
     83     params->push_back(TAG_DIGEST, Digest::SHA_2_224);
     84     params->push_back(TAG_DIGEST, Digest::SHA_2_256);
     85     params->push_back(TAG_DIGEST, Digest::SHA_2_384);
     86     params->push_back(TAG_DIGEST, Digest::SHA_2_512);
     87     params->push_back(TAG_NO_AUTH_REQUIRED);
     88     params->push_back(TAG_ORIGINATION_EXPIRE_DATETIME, LLONG_MAX);
     89     params->push_back(TAG_USAGE_EXPIRE_DATETIME, LLONG_MAX);
     90     params->push_back(TAG_ACTIVE_DATETIME, 0);
     91 }
     92 
     93 uid_t get_app_id(uid_t uid) {
     94     return uid % AID_USER;
     95 }
     96 
     97 uid_t get_user_id(uid_t uid) {
     98     return uid / AID_USER;
     99 }
    100 
    101 void log_key_integrity_violation(const char* name, uid_t uid) {
    102     if (!__android_log_security()) return;
    103     android_log_event_list(SEC_TAG_KEY_INTEGRITY_VIOLATION)
    104         << name << int32_t(uid) << LOG_ID_SECURITY;
    105 }
    106 
    107 namespace keystore {
    108 
    109 hidl_vec<uint8_t> blob2hidlVec(const Blob& blob) {
    110     hidl_vec<uint8_t> result;
    111     result.setToExternal(const_cast<uint8_t*>(blob.getValue()), blob.getLength());
    112     return result;
    113 }
    114 
    115 SecurityLevel flagsToSecurityLevel(int32_t flags) {
    116     switch (flags & (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX)) {
    117     case KEYSTORE_FLAG_FALLBACK:
    118     // treating Strongbox flag as "don't care" if Fallback is set
    119     case (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX):
    120         return SecurityLevel::SOFTWARE;
    121     case KEYSTORE_FLAG_STRONGBOX:
    122         return SecurityLevel::STRONGBOX;
    123     default:
    124         return SecurityLevel::TRUSTED_ENVIRONMENT;
    125     }
    126 }
    127 
    128 uint32_t securityLevelToFlags(SecurityLevel secLevel) {
    129     switch (secLevel) {
    130     case SecurityLevel::SOFTWARE:
    131         return KEYSTORE_FLAG_FALLBACK;
    132     case SecurityLevel::STRONGBOX:
    133         return KEYSTORE_FLAG_STRONGBOX;
    134     default:
    135         return 0;
    136     }
    137 }
    138 
    139 }  // namespace keystore
    140