blob: f970559f018565a5619ce97384f04510c84fa51d [file] [log] [blame]
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001/*
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
Janis Danisevskisccfff102017-05-01 11:02:51 -070029#include <memory>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070030
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010031#include <keystore/authorization_set.h>
32
33#include "blob.h"
34
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070035size_t readFully(int fd, uint8_t* data, size_t size);
36size_t writeFully(int fd, uint8_t* data, size_t size);
37
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010038void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039
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};
Janis Danisevskisccfff102017-05-01 11:02:51 -070055typedef std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070056
57struct PKCS8_PRIV_KEY_INFO_Delete {
58 void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); }
59};
Janis Danisevskisccfff102017-05-01 11:02:51 -070060typedef std::unique_ptr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070061
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010062namespace 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
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070072#endif // KEYSTORE_KEYSTORE_UTILS_H_