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 <memory>
30
31#include <keystore/authorization_set.h>
32
33#include "blob.h"
34
35size_t readFully(int fd, uint8_t* data, size_t size);
36size_t writeFully(int fd, uint8_t* data, size_t size);
37
38void 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 */
44uid_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 */
50uid_t get_user_id(uid_t uid);
51
52struct EVP_PKEY_Delete {
53    void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
54};
55typedef std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
56
57struct PKCS8_PRIV_KEY_INFO_Delete {
58    void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); }
59};
60typedef std::unique_ptr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO;
61
62namespace keystore {
63
64inline 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