blob: 24844b7a77cb319d366991d435ab0d9cc3c93051 [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
Janis Danisevskisccfff102017-05-01 11:02:51 -070027#include <memory>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070028
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010029#include <keystore/authorization_set.h>
30
31#include "blob.h"
32
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070033size_t readFully(int fd, uint8_t* data, size_t size);
34size_t writeFully(int fd, uint8_t* data, size_t size);
35
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010036void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070037
38/**
39 * Returns the app ID (in the Android multi-user sense) for the current
40 * UNIX UID.
41 */
42uid_t get_app_id(uid_t uid);
43
44/**
45 * Returns the user ID (in the Android multi-user sense) for the current
46 * UNIX UID.
47 */
48uid_t get_user_id(uid_t uid);
49
50struct EVP_PKEY_Delete {
51 void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
52};
Janis Danisevskisccfff102017-05-01 11:02:51 -070053typedef std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070054
55struct PKCS8_PRIV_KEY_INFO_Delete {
56 void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); }
57};
Janis Danisevskisccfff102017-05-01 11:02:51 -070058typedef std::unique_ptr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070059
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010060namespace keystore {
61
62inline static hidl_vec<uint8_t> blob2hidlVec(const Blob& blob) {
63 hidl_vec<uint8_t> result;
64 result.setToExternal(const_cast<uint8_t*>(blob.getValue()), blob.getLength());
65 return result;
66}
67
68} // namespace keystore
69
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070070#endif // KEYSTORE_KEYSTORE_UTILS_H_