2019-10-28 18:32:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SYSTEM_SECURITY_CREDENTIAL_DATA_H_
|
|
|
|
#define SYSTEM_SECURITY_CREDENTIAL_DATA_H_
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-02-12 04:08:27 +01:00
|
|
|
#include <android/hardware/identity/IIdentityCredential.h>
|
|
|
|
#include <android/hardware/identity/SecureAccessControlProfile.h>
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace security {
|
|
|
|
namespace identity {
|
|
|
|
|
2020-02-12 04:08:27 +01:00
|
|
|
using ::android::hardware::identity::Certificate;
|
|
|
|
using ::android::hardware::identity::IIdentityCredential;
|
|
|
|
using ::android::hardware::identity::SecureAccessControlProfile;
|
2019-10-28 18:32:48 +01:00
|
|
|
using ::std::map;
|
|
|
|
using ::std::optional;
|
|
|
|
using ::std::string;
|
|
|
|
using ::std::tuple;
|
|
|
|
using ::std::vector;
|
|
|
|
|
|
|
|
struct EntryData {
|
|
|
|
EntryData() {}
|
|
|
|
|
|
|
|
uint64_t size = 0;
|
2020-02-12 04:08:27 +01:00
|
|
|
vector<int32_t> accessControlProfileIds;
|
2019-10-28 18:32:48 +01:00
|
|
|
vector<vector<uint8_t>> encryptedChunks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AuthKeyData {
|
|
|
|
AuthKeyData() {}
|
|
|
|
|
|
|
|
vector<uint8_t> certificate;
|
|
|
|
vector<uint8_t> keyBlob;
|
2021-03-04 22:32:43 +01:00
|
|
|
int64_t expirationDateMillisSinceEpoch = 0;
|
2019-10-28 18:32:48 +01:00
|
|
|
vector<uint8_t> staticAuthenticationData;
|
|
|
|
vector<uint8_t> pendingCertificate;
|
|
|
|
vector<uint8_t> pendingKeyBlob;
|
|
|
|
int useCount = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CredentialData : public RefBase {
|
|
|
|
public:
|
|
|
|
CredentialData(const string& dataPath, uid_t ownerUid, const string& name);
|
|
|
|
|
|
|
|
static string calculateCredentialFileName(const string& dataPath, uid_t ownerUid,
|
|
|
|
const string& name);
|
|
|
|
|
|
|
|
static optional<bool> credentialExists(const string& dataPath, uid_t ownerUid,
|
|
|
|
const string& name);
|
|
|
|
|
|
|
|
void setSecureUserId(int64_t secureUserId);
|
|
|
|
|
|
|
|
void setCredentialData(const vector<uint8_t>& credentialData);
|
|
|
|
|
|
|
|
void setAttestationCertificate(const vector<uint8_t>& attestationCertificate);
|
|
|
|
|
|
|
|
void
|
|
|
|
addSecureAccessControlProfile(const SecureAccessControlProfile& secureAccessControlProfile);
|
|
|
|
|
|
|
|
void addEntryData(const string& namespaceName, const string& entryName, const EntryData& data);
|
|
|
|
|
|
|
|
bool saveToDisk() const;
|
|
|
|
|
|
|
|
bool loadFromDisk();
|
|
|
|
|
|
|
|
bool deleteCredential();
|
|
|
|
|
2022-11-14 21:22:24 +01:00
|
|
|
void setAvailableAuthenticationKeys(int keyCount, int maxUsesPerKey,
|
|
|
|
int64_t minValidTimeMillis);
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
|
|
|
int64_t getSecureUserId();
|
|
|
|
|
|
|
|
const vector<uint8_t>& getCredentialData() const;
|
|
|
|
|
|
|
|
const vector<uint8_t>& getAttestationCertificate() const;
|
|
|
|
|
|
|
|
const vector<SecureAccessControlProfile>& getSecureAccessControlProfiles() const;
|
|
|
|
|
|
|
|
bool hasEntryData(const string& namespaceName, const string& entryName) const;
|
|
|
|
|
|
|
|
optional<EntryData> getEntryData(const string& namespaceName, const string& entryName) const;
|
|
|
|
|
|
|
|
const vector<AuthKeyData>& getAuthKeyDatas() const;
|
|
|
|
|
2022-11-14 21:22:24 +01:00
|
|
|
tuple<int /* keyCount */, int /*maxUsersPerKey */, int64_t /* minValidTimeMillis */>
|
|
|
|
getAvailableAuthenticationKeys() const;
|
2020-10-16 17:50:13 +02:00
|
|
|
|
2019-10-28 18:32:48 +01:00
|
|
|
// Returns |nullptr| if a suitable key cannot be found. Otherwise returns
|
|
|
|
// the authentication and increases its use-count.
|
2021-09-11 19:52:17 +02:00
|
|
|
const AuthKeyData* selectAuthKey(bool allowUsingExhaustedKeys, bool allowUsingExpiredKeys,
|
|
|
|
bool incrementUsageCount);
|
2019-10-28 18:32:48 +01:00
|
|
|
|
2020-02-12 04:08:27 +01:00
|
|
|
optional<vector<vector<uint8_t>>>
|
|
|
|
getAuthKeysNeedingCertification(const sp<IIdentityCredential>& halBinder);
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
bool storeStaticAuthenticationData(const vector<uint8_t>& authenticationKey,
|
2020-10-16 17:50:13 +02:00
|
|
|
int64_t expirationDateMillisSinceEpoch,
|
2019-10-28 18:32:48 +01:00
|
|
|
const vector<uint8_t>& staticAuthData);
|
|
|
|
|
|
|
|
private:
|
2020-10-16 17:50:13 +02:00
|
|
|
AuthKeyData* findAuthKey_(bool allowUsingExhaustedKeys, bool allowUsingExpiredKeys);
|
|
|
|
|
2019-10-28 18:32:48 +01:00
|
|
|
// Set by constructor.
|
|
|
|
//
|
|
|
|
string dataPath_;
|
|
|
|
uid_t ownerUid_;
|
|
|
|
string name_;
|
|
|
|
|
|
|
|
// Calculated at construction time, from |dataPath_|, |ownerUid_|, |name_|.
|
|
|
|
string fileName_;
|
|
|
|
|
|
|
|
// Data serialized in CBOR from here:
|
|
|
|
//
|
|
|
|
int64_t secureUserId_;
|
|
|
|
vector<uint8_t> credentialData_;
|
|
|
|
vector<uint8_t> attestationCertificate_;
|
|
|
|
vector<SecureAccessControlProfile> secureAccessControlProfiles_;
|
|
|
|
map<string, EntryData> idToEncryptedChunks_;
|
|
|
|
|
|
|
|
int keyCount_ = 0;
|
|
|
|
int maxUsesPerKey_ = 1;
|
2022-11-14 21:22:24 +01:00
|
|
|
int64_t minValidTimeMillis_ = 0;
|
2019-10-28 18:32:48 +01:00
|
|
|
vector<AuthKeyData> authKeyDatas_; // Always |keyCount_| long.
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace identity
|
|
|
|
} // namespace security
|
|
|
|
} // namespace android
|
|
|
|
|
|
|
|
#endif // SYSTEM_SECURITY_CREDENTIAL_DATA_H_
|