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 #ifndef KEYSTORE_KEYSTORE_UTILS_H_
18 #define KEYSTORE_KEYSTORE_UTILS_H_
19
20 #include <stdint.h>
21
22 #include <vector>
23
24 #include <openssl/evp.h>
25 #include <openssl/pem.h>
26
27 #include <hardware/keymaster_defs.h>
28
29 #include <UniquePtr.h>
30
31 #include <keystore/authorization_set.h>
32
33 #include "blob.h"
34
35 size_t readFully(int fd, uint8_t* data, size_t size);
36 size_t writeFully(int fd, uint8_t* data, size_t size);
37
38 void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params);
39
40 /**
41 * Returns the app ID (in the Android multi-user sense) for the current
42 * UNIX UID.
43 */
44 uid_t get_app_id(uid_t uid);
45
46 /**
47 * Returns the user ID (in the Android multi-user sense) for the current
48 * UNIX UID.
49 */
50 uid_t get_user_id(uid_t uid);
51
52 struct EVP_PKEY_Delete {
operatorEVP_PKEY_Delete53 void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
54 };
55 typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
56
57 struct PKCS8_PRIV_KEY_INFO_Delete {
operatorPKCS8_PRIV_KEY_INFO_Delete58 void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); }
59 };
60 typedef UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO;
61
62 namespace keystore {
63
blob2hidlVec(const Blob & blob)64 inline static hidl_vec<uint8_t> blob2hidlVec(const Blob& blob) {
65 hidl_vec<uint8_t> result;
66 result.setToExternal(const_cast<uint8_t*>(blob.getValue()), blob.getLength());
67 return result;
68 }
69
70 } // namespace keystore
71
72 #endif // KEYSTORE_KEYSTORE_UTILS_H_
73