blob: 72ddfc47257c8cc4016cee81c05c77a5c8c7dec0 [file] [log] [blame]
bigbiff7ba75002020-04-11 20:47:09 -04001/*
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 ANDROID_VOLD_KEYSTORAGE_H
18#define ANDROID_VOLD_KEYSTORAGE_H
19
mauronofrio matarrese79820322020-05-25 19:48:56 +020020#include "Keymaster.h"
bigbiff7ba75002020-04-11 20:47:09 -040021#include "KeyBuffer.h"
mauronofrio matarrese79820322020-05-25 19:48:56 +020022#include <cutils/multiuser.h>
bigbiff7ba75002020-04-11 20:47:09 -040023#include <string>
24
25namespace android {
26namespace vold {
27
28// Represents the information needed to decrypt a disk encryption key.
29// If "token" is nonempty, it is passed in as a required Gatekeeper auth token.
30// If "token" and "secret" are nonempty, "secret" is appended to the application-specific
31// binary needed to unlock.
32// If only "secret" is nonempty, it is used to decrypt in a non-Keymaster process.
33class KeyAuthentication {
34 public:
35 KeyAuthentication(const std::string& t, const std::string& s) : token{t}, secret{s} {};
36
37 bool usesKeymaster() const { return !token.empty() || secret.empty(); };
38
39 const std::string token;
40 const std::string secret;
41};
42
mauronofrio matarrese79820322020-05-25 19:48:56 +020043enum class KeyType {
44 DE_SYS,
45 DE_USER,
46 CE_USER
47};
48
bigbiff7ba75002020-04-11 20:47:09 -040049extern const KeyAuthentication kEmptyAuthentication;
50
51// Checks if path "path" exists.
52bool pathExists(const std::string& path);
53
54bool createSecdiscardable(const std::string& path, std::string* hash);
55bool readSecdiscardable(const std::string& path, std::string* hash);
56
57// Create a directory at the named path, and store "key" in it,
58// in such a way that it can only be retrieved via Keymaster and
59// can be securely deleted.
60// It's safe to move/rename the directory after creation.
61bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBuffer& key);
62
63// Create a directory at the named path, and store "key" in it as storeKey
64// This version creates the key in "tmp_path" then atomically renames "tmp_path"
65// to "key_path" thereby ensuring that the key is either stored entirely or
66// not at all.
67bool storeKeyAtomically(const std::string& key_path, const std::string& tmp_path,
68 const KeyAuthentication& auth, const KeyBuffer& key);
69
70// Retrieve the key from the named directory.
71bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffer* key,
72 bool keepOld = false);
73
74// Securely destroy the key stored in the named directory and delete the directory.
75bool destroyKey(const std::string& dir);
76
77bool runSecdiscardSingle(const std::string& file);
mauronofrio matarrese79820322020-05-25 19:48:56 +020078bool generateWrappedKey(userid_t user_id, KeyType key_type, KeyBuffer* key);
79bool getEphemeralWrappedKey(km::KeyFormat format, KeyBuffer& kmKey, KeyBuffer* key);
bigbiff7ba75002020-04-11 20:47:09 -040080} // namespace vold
81} // namespace android
82
83#endif