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
28 #include <keystore/authorization_set.h>
29 #include <keystore/keystore_client.h>
30 #include <keystore/IKeystoreService.h>
31
readFully(int fd,uint8_t * data,size_t size)32 size_t readFully(int fd, uint8_t* data, size_t size) {
33 size_t remaining = size;
34 while (remaining > 0) {
35 ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining));
36 if (n <= 0) {
37 return size - remaining;
38 }
39 data += n;
40 remaining -= n;
41 }
42 return size;
43 }
44
writeFully(int fd,uint8_t * data,size_t size)45 size_t writeFully(int fd, uint8_t* data, size_t size) {
46 size_t remaining = size;
47 while (remaining > 0) {
48 ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining));
49 if (n < 0) {
50 ALOGW("write failed: %s", strerror(errno));
51 return size - remaining;
52 }
53 data += n;
54 remaining -= n;
55 }
56 if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) {
57 ALOGW("fsync failed: %s", strerror(errno));
58 return -1;
59 }
60 return size;
61 }
62
add_legacy_key_authorizations(int keyType,keystore::AuthorizationSet * params)63 void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params) {
64 using namespace keystore;
65 params->push_back(TAG_PURPOSE, KeyPurpose::SIGN);
66 params->push_back(TAG_PURPOSE, KeyPurpose::VERIFY);
67 params->push_back(TAG_PURPOSE, KeyPurpose::ENCRYPT);
68 params->push_back(TAG_PURPOSE, KeyPurpose::DECRYPT);
69 params->push_back(TAG_PADDING, PaddingMode::NONE);
70 if (keyType == EVP_PKEY_RSA) {
71 params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_SIGN);
72 params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_ENCRYPT);
73 params->push_back(TAG_PADDING, PaddingMode::RSA_PSS);
74 params->push_back(TAG_PADDING, PaddingMode::RSA_OAEP);
75 }
76 params->push_back(TAG_DIGEST, Digest::NONE);
77 params->push_back(TAG_DIGEST, Digest::MD5);
78 params->push_back(TAG_DIGEST, Digest::SHA1);
79 params->push_back(TAG_DIGEST, Digest::SHA_2_224);
80 params->push_back(TAG_DIGEST, Digest::SHA_2_256);
81 params->push_back(TAG_DIGEST, Digest::SHA_2_384);
82 params->push_back(TAG_DIGEST, Digest::SHA_2_512);
83 params->push_back(TAG_ALL_USERS);
84 params->push_back(TAG_NO_AUTH_REQUIRED);
85 params->push_back(TAG_ORIGINATION_EXPIRE_DATETIME, LLONG_MAX);
86 params->push_back(TAG_USAGE_EXPIRE_DATETIME, LLONG_MAX);
87 params->push_back(TAG_ACTIVE_DATETIME, 0);
88 }
89
get_app_id(uid_t uid)90 uid_t get_app_id(uid_t uid) {
91 return uid % AID_USER;
92 }
93
get_user_id(uid_t uid)94 uid_t get_user_id(uid_t uid) {
95 return uid / AID_USER;
96 }
97