#ifndef CHROMEOS_ASH_COMPONENTS_KCER_CHAPS_HIGH_LEVEL_CHAPS_CLIENT_H_
#define CHROMEOS_ASH_COMPONENTS_KCER_CHAPS_HIGH_LEVEL_CHAPS_CLIENT_H_
#include <stdint.h>
#include <vector>
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/sequence_checker.h"
#include "chromeos/ash/components/kcer/attributes.pb.h"
#include "chromeos/ash/components/kcer/chaps/session_chaps_client.h"
#include "chromeos/constants/pkcs11_definitions.h"
#include "third_party/cros_system_api/constants/pkcs11_custom_attributes.h"
#include "third_party/cros_system_api/dbus/chaps/dbus-constants.h"
namespace kcer {
COMPONENT_EXPORT(KCER)
void AddAttribute(chaps::AttributeList& attr_list,
chromeos::PKCS11_CK_ATTRIBUTE_TYPE type,
base::span<const uint8_t> data);
template <typename T>
COMPONENT_EXPORT(KCER)
base::span<const uint8_t> MakeSpan(T* value) {
static_assert(std::is_integral_v<T>);
return base::as_bytes(UNSAFE_TODO(base::span<T>(value, 1u)));
}
class HighLevelChapsClient {
public:
using GetAttributeValueCallback =
base::OnceCallback<void(chaps::AttributeList attributes,
uint32_t result_code)>;
enum class AttributeId : uint32_t {
kModulus = chromeos::PKCS11_CKA_MODULUS,
kPublicExponent = chromeos::PKCS11_CKA_PUBLIC_EXPONENT,
kEcPoint = chromeos::PKCS11_CKA_EC_POINT,
kPkcs11Id = chromeos::PKCS11_CKA_ID,
kLabel = chromeos::PKCS11_CKA_LABEL,
kKeyType = chromeos::PKCS11_CKA_KEY_TYPE,
kValue = chromeos::PKCS11_CKA_VALUE,
kKeyInSoftware = chaps::kKeyInSoftwareAttribute,
kKeyPermissions = pkcs11_custom_attributes::kCkaChromeOsKeyPermissions,
kCertProvisioningId =
pkcs11_custom_attributes::kCkaChromeOsBuiltinProvisioningProfileId,
};
HighLevelChapsClient() = default;
virtual ~HighLevelChapsClient() = default;
virtual void GetMechanismList(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::GetMechanismListCallback callback) = 0;
virtual void CreateObject(
SessionChapsClient::SlotId slot_id,
const chaps::AttributeList& attributes,
SessionChapsClient::CreateObjectCallback callback) = 0;
virtual void DestroyObject(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
SessionChapsClient::DestroyObjectCallback callback) = 0;
virtual void DestroyObjectsWithRetries(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
SessionChapsClient::DestroyObjectCallback callback) = 0;
virtual void GetAttributeValue(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
std::vector<AttributeId> attribute_ids,
HighLevelChapsClient::GetAttributeValueCallback callback) = 0;
virtual void SetAttributeValue(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
const chaps::AttributeList& attributes,
SessionChapsClient::SetAttributeValueCallback callback) = 0;
virtual void SetAttributeValue(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
const chaps::AttributeList& attributes,
SessionChapsClient::SetAttributeValueCallback callback) = 0;
virtual void FindObjects(
SessionChapsClient::SlotId slot_id,
const chaps::AttributeList& attributes,
SessionChapsClient::FindObjectsCallback callback) = 0;
virtual void Sign(SessionChapsClient::SlotId slot_id,
uint64_t mechanism_type,
const std::vector<uint8_t>& mechanism_parameter,
SessionChapsClient::ObjectHandle key_handle,
std::vector<uint8_t> data,
SessionChapsClient::SignCallback callback) = 0;
virtual void GenerateKeyPair(
SessionChapsClient::SlotId slot_id,
uint64_t mechanism_type,
const std::vector<uint8_t>& mechanism_parameter,
const chaps::AttributeList& public_key_attributes,
const chaps::AttributeList& private_key_attributes,
SessionChapsClient::GenerateKeyPairCallback callback) = 0;
};
class COMPONENT_EXPORT(KCER) HighLevelChapsClientImpl
: public HighLevelChapsClient {
public:
explicit HighLevelChapsClientImpl(SessionChapsClient* session_chaps_client);
~HighLevelChapsClientImpl() override;
void GetMechanismList(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::GetMechanismListCallback callback) override;
void CreateObject(SessionChapsClient::SlotId slot_id,
const chaps::AttributeList& attributes,
SessionChapsClient::CreateObjectCallback callback) override;
void DestroyObject(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
SessionChapsClient::DestroyObjectCallback callback) override;
void DestroyObjectsWithRetries(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
SessionChapsClient::DestroyObjectCallback callback) override;
void GetAttributeValue(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
std::vector<AttributeId> attribute_ids,
HighLevelChapsClient::GetAttributeValueCallback callback) override;
void SetAttributeValue(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
const chaps::AttributeList& attributes,
SessionChapsClient::SetAttributeValueCallback callback) override;
void SetAttributeValue(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
const chaps::AttributeList& attributes,
SessionChapsClient::SetAttributeValueCallback callback) override;
void FindObjects(SessionChapsClient::SlotId slot_id,
const chaps::AttributeList& attributes,
SessionChapsClient::FindObjectsCallback callback) override;
void Sign(SessionChapsClient::SlotId slot_id,
uint64_t mechanism_type,
const std::vector<uint8_t>& mechanism_parameter,
SessionChapsClient::ObjectHandle key_handle,
std::vector<uint8_t> data,
SessionChapsClient::SignCallback callback) override;
void GenerateKeyPair(
SessionChapsClient::SlotId slot_id,
uint64_t mechanism_type,
const std::vector<uint8_t>& mechanism_parameter,
const chaps::AttributeList& public_key_attributes,
const chaps::AttributeList& private_key_attributes,
SessionChapsClient::GenerateKeyPairCallback callback) override;
void SetSessionChapsClientForTesting(
SessionChapsClient* session_chaps_client);
private:
void DestroyObjectsWithRetriesImpl(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
std::vector<SessionChapsClient::ObjectHandle> failed_handles,
uint32_t last_error,
int retries_left,
SessionChapsClient::DestroyObjectCallback callback);
void DestroyObjectsWithRetriesHandleOneResult(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
std::vector<SessionChapsClient::ObjectHandle> failed_handles,
uint32_t last_error,
int retries_left,
SessionChapsClient::DestroyObjectCallback callback,
uint32_t result_code);
void DidGetAttributeValue(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
HighLevelChapsClient::GetAttributeValueCallback callback,
std::vector<uint8_t> attributes,
uint32_t result_code);
void DidGetAttributeLength(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
HighLevelChapsClient::GetAttributeValueCallback callback,
std::vector<uint8_t> attributes,
uint32_t result_code);
void DidGetAttributeValueWithLength(
SessionChapsClient::SlotId slot_id,
SessionChapsClient::ObjectHandle object_handle,
HighLevelChapsClient::GetAttributeValueCallback callback,
std::vector<uint8_t> attributes,
uint32_t result_code);
void SetAttributeValueImpl(
SessionChapsClient::SlotId slot_id,
std::vector<SessionChapsClient::ObjectHandle> object_handles,
const chaps::AttributeList& attributes,
SessionChapsClient::SetAttributeValueCallback callback,
uint32_t last_error,
uint32_t new_result_code);
SEQUENCE_CHECKER(sequence_checker_);
const raw_ptr<SessionChapsClient> session_chaps_client_;
base::WeakPtrFactory<HighLevelChapsClientImpl> weak_factory_{this};
};
}
#endif