Android-cuttlefish cvd tool
tpm_keymaster_context.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020 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#pragma once
17
18#include <map>
19#include <vector>
20
21#include <keymaster/keymaster_context.h>
22#include <keymaster/km_openssl/attestation_record.h>
23
25
26namespace cuttlefish {
27
28class TpmAttestationRecordContext;
29class TpmResourceManager;
30class TpmKeyBlobMaker;
31class TpmRandomSource;
32class TpmRemoteProvisioningContext;
33
40class TpmKeymasterContext : public keymaster::KeymasterContext {
41 private:
43 keymaster::KeymasterEnforcement& enforcement_;
44 std::unique_ptr<TpmKeyBlobMaker> key_blob_maker_;
45 std::unique_ptr<TpmRandomSource> random_source_;
46 std::unique_ptr<TpmAttestationRecordContext> attestation_context_;
47 std::unique_ptr<TpmRemoteProvisioningContext> remote_provisioning_context_;
48 std::map<keymaster_algorithm_t, std::unique_ptr<keymaster::KeyFactory>>
50 std::vector<keymaster_algorithm_t> supported_algorithms_;
51 uint32_t os_version_;
53 std::optional<uint32_t> vendor_patchlevel_;
54 std::optional<uint32_t> boot_patchlevel_;
55 std::optional<std::string> bootloader_state_;
56 std::optional<std::string> verified_boot_state_;
57 std::optional<std::vector<uint8_t>> vbmeta_digest_;
58
59 public:
60 TpmKeymasterContext(TpmResourceManager&, keymaster::KeymasterEnforcement&);
62
63 keymaster::KmVersion GetKmVersion() const override {
64 return attestation_context_->GetKmVersion();
65 }
66
67 keymaster_error_t SetSystemVersion(uint32_t os_version,
68 uint32_t os_patchlevel) override;
69 void GetSystemVersion(uint32_t* os_version,
70 uint32_t* os_patchlevel) const override;
71
72 const keymaster::KeyFactory* GetKeyFactory(
73 keymaster_algorithm_t algorithm) const override;
74 keymaster::OperationFactory* GetOperationFactory(
75 keymaster_algorithm_t algorithm,
76 keymaster_purpose_t purpose) const override;
77 const keymaster_algorithm_t* GetSupportedAlgorithms(
78 size_t* algorithms_count) const override;
79
80 keymaster_error_t UpgradeKeyBlob(
81 const keymaster::KeymasterKeyBlob& key_to_upgrade,
82 const keymaster::AuthorizationSet& upgrade_params,
83 keymaster::KeymasterKeyBlob* upgraded_key) const override;
84
85 keymaster_error_t ParseKeyBlob(
86 const keymaster::KeymasterKeyBlob& blob,
87 const keymaster::AuthorizationSet& additional_params,
88 keymaster::UniquePtr<keymaster::Key>* key) const override;
89
90 keymaster_error_t AddRngEntropy(const uint8_t* buf,
91 size_t length) const override;
92
93 keymaster::KeymasterEnforcement* enforcement_policy() override;
94
95 keymaster::AttestationContext* attestation_context() override {
96 return attestation_context_.get();
97 }
98
99 keymaster::CertificateChain GenerateAttestation(
100 const keymaster::Key& key,
101 const keymaster::AuthorizationSet& attest_params,
102 keymaster::UniquePtr<keymaster::Key> attest_key,
103 const keymaster::KeymasterBlob& issuer_subject,
104 keymaster_error_t* error) const override;
105
106 keymaster::CertificateChain GenerateSelfSignedCertificate(
107 const keymaster::Key& key, const keymaster::AuthorizationSet& cert_params,
108 bool fake_signature, keymaster_error_t* error) const override;
109
110 keymaster_error_t UnwrapKey(
111 const keymaster::KeymasterKeyBlob& wrapped_key_blob,
112 const keymaster::KeymasterKeyBlob& wrapping_key_blob,
113 const keymaster::AuthorizationSet& wrapping_key_params,
114 const keymaster::KeymasterKeyBlob& masking_key,
115 keymaster::AuthorizationSet* wrapped_key_params,
116 keymaster_key_format_t* wrapped_key_format,
117 keymaster::KeymasterKeyBlob* wrapped_key_material) const override;
118
119 keymaster_error_t CheckConfirmationToken(
120 const std::uint8_t* input_data, size_t input_data_size,
121 const uint8_t confirmation_token[keymaster::kConfirmationTokenSize])
122 const;
123
124 keymaster::RemoteProvisioningContext* GetRemoteProvisioningContext()
125 const override;
126
127 keymaster_error_t SetVerifiedBootInfo(
128 std::string_view verified_boot_state, std::string_view bootloader_state,
129 const std::vector<uint8_t>& vbmeta_digest) override;
130
131 keymaster_error_t SetVendorPatchlevel(uint32_t vendor_patchlevel) override;
132 keymaster_error_t SetBootPatchlevel(uint32_t boot_patchlevel) override;
133 std::optional<uint32_t> GetVendorPatchlevel() const override;
134 std::optional<uint32_t> GetBootPatchlevel() const override;
135
136 keymaster_error_t SetAttestationIds(
137 const keymaster::SetAttestationIdsRequest& request) override {
138 return attestation_context_->SetAttestationIds(request);
139 }
140
141 keymaster_error_t SetAttestationIdsKM3(
142 const keymaster::SetAttestationIdsKM3Request& request) override {
143 return attestation_context_->SetAttestationIdsKM3(request);
144 }
145};
146
147} // namespace cuttlefish
Definition: tpm_keymaster_context.h:40
std::optional< std::vector< uint8_t > > vbmeta_digest_
Definition: tpm_keymaster_context.h:57
std::unique_ptr< TpmRandomSource > random_source_
Definition: tpm_keymaster_context.h:45
keymaster_error_t CheckConfirmationToken(const std::uint8_t *input_data, size_t input_data_size, const uint8_t confirmation_token[keymaster::kConfirmationTokenSize]) const
Definition: tpm_keymaster_context.cpp:576
uint32_t os_patchlevel_
Definition: tpm_keymaster_context.h:52
keymaster_error_t SetVendorPatchlevel(uint32_t vendor_patchlevel) override
Definition: tpm_keymaster_context.cpp:648
std::map< keymaster_algorithm_t, std::unique_ptr< keymaster::KeyFactory > > key_factories_
Definition: tpm_keymaster_context.h:49
std::optional< uint32_t > GetVendorPatchlevel() const override
Definition: tpm_keymaster_context.cpp:678
uint32_t os_version_
Definition: tpm_keymaster_context.h:51
void GetSystemVersion(uint32_t *os_version, uint32_t *os_patchlevel) const override
Definition: tpm_keymaster_context.cpp:115
std::optional< uint32_t > boot_patchlevel_
Definition: tpm_keymaster_context.h:54
std::optional< std::string > bootloader_state_
Definition: tpm_keymaster_context.h:55
keymaster_error_t SetVerifiedBootInfo(std::string_view verified_boot_state, std::string_view bootloader_state, const std::vector< uint8_t > &vbmeta_digest) override
Definition: tpm_keymaster_context.cpp:614
keymaster_error_t SetBootPatchlevel(uint32_t boot_patchlevel) override
Definition: tpm_keymaster_context.cpp:663
std::vector< keymaster_algorithm_t > supported_algorithms_
Definition: tpm_keymaster_context.h:50
keymaster::OperationFactory * GetOperationFactory(keymaster_algorithm_t algorithm, keymaster_purpose_t purpose) const override
Definition: tpm_keymaster_context.cpp:131
keymaster_error_t SetAttestationIds(const keymaster::SetAttestationIdsRequest &request) override
Definition: tpm_keymaster_context.h:136
keymaster::CertificateChain GenerateAttestation(const keymaster::Key &key, const keymaster::AuthorizationSet &attest_params, keymaster::UniquePtr< keymaster::Key > attest_key, const keymaster::KeymasterBlob &issuer_subject, keymaster_error_t *error) const override
Definition: tpm_keymaster_context.cpp:281
const keymaster_algorithm_t * GetSupportedAlgorithms(size_t *algorithms_count) const override
Definition: tpm_keymaster_context.cpp:147
keymaster_error_t ParseKeyBlob(const keymaster::KeymasterKeyBlob &blob, const keymaster::AuthorizationSet &additional_params, keymaster::UniquePtr< keymaster::Key > *key) const override
Definition: tpm_keymaster_context.cpp:233
keymaster::RemoteProvisioningContext * GetRemoteProvisioningContext() const override
Definition: tpm_keymaster_context.cpp:599
keymaster::AttestationContext * attestation_context() override
Definition: tpm_keymaster_context.h:95
std::unique_ptr< TpmAttestationRecordContext > attestation_context_
Definition: tpm_keymaster_context.h:46
keymaster_error_t SetSystemVersion(uint32_t os_version, uint32_t os_patchlevel) override
Definition: tpm_keymaster_context.cpp:105
keymaster_error_t SetAttestationIdsKM3(const keymaster::SetAttestationIdsKM3Request &request) override
Definition: tpm_keymaster_context.h:141
std::unique_ptr< TpmKeyBlobMaker > key_blob_maker_
Definition: tpm_keymaster_context.h:44
const keymaster::KeyFactory * GetKeyFactory(keymaster_algorithm_t algorithm) const override
Definition: tpm_keymaster_context.cpp:121
std::unique_ptr< TpmRemoteProvisioningContext > remote_provisioning_context_
Definition: tpm_keymaster_context.h:47
keymaster::KeymasterEnforcement & enforcement_
Definition: tpm_keymaster_context.h:43
TpmResourceManager & resource_manager_
Definition: tpm_keymaster_context.h:42
keymaster_error_t UnwrapKey(const keymaster::KeymasterKeyBlob &wrapped_key_blob, const keymaster::KeymasterKeyBlob &wrapping_key_blob, const keymaster::AuthorizationSet &wrapping_key_params, const keymaster::KeymasterKeyBlob &masking_key, keymaster::AuthorizationSet *wrapped_key_params, keymaster_key_format_t *wrapped_key_format, keymaster::KeymasterKeyBlob *wrapped_key_material) const override
Definition: tpm_keymaster_context.cpp:357
keymaster::KmVersion GetKmVersion() const override
Definition: tpm_keymaster_context.h:63
keymaster_error_t UpgradeKeyBlob(const keymaster::KeymasterKeyBlob &key_to_upgrade, const keymaster::AuthorizationSet &upgrade_params, keymaster::KeymasterKeyBlob *upgraded_key) const override
Definition: tpm_keymaster_context.cpp:182
keymaster::CertificateChain GenerateSelfSignedCertificate(const keymaster::Key &key, const keymaster::AuthorizationSet &cert_params, bool fake_signature, keymaster_error_t *error) const override
Definition: tpm_keymaster_context.cpp:332
TpmKeymasterContext(TpmResourceManager &, keymaster::KeymasterEnforcement &)
Definition: tpm_keymaster_context.cpp:77
std::optional< uint32_t > vendor_patchlevel_
Definition: tpm_keymaster_context.h:53
keymaster::KeymasterEnforcement * enforcement_policy() override
Definition: tpm_keymaster_context.cpp:274
std::optional< std::string > verified_boot_state_
Definition: tpm_keymaster_context.h:56
std::optional< uint32_t > GetBootPatchlevel() const override
Definition: tpm_keymaster_context.cpp:682
keymaster_error_t AddRngEntropy(const uint8_t *buf, size_t length) const override
Definition: tpm_keymaster_context.cpp:269
Definition: tpm_resource_manager.h:50
#define error(format, args...)
Definition: fec_private.h:201
Definition: alloc_utils.cpp:23