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 <stdint.h>
19
20#include <map>
21#include <memory>
22#include <optional>
23#include <string>
24#include <string_view>
25#include <vector>
26
27#include <keymaster/keymaster_context.h>
28#include <keymaster/km_openssl/attestation_record.h>
29
31
32namespace cuttlefish {
33
34class TpmAttestationRecordContext;
35class TpmResourceManager;
36class TpmKeyBlobMaker;
37class TpmRandomSource;
38class TpmRemoteProvisioningContext;
39
46class TpmKeymasterContext : public keymaster::KeymasterContext {
47 private:
49 keymaster::KeymasterEnforcement& enforcement_;
50 std::unique_ptr<TpmKeyBlobMaker> key_blob_maker_;
51 std::unique_ptr<TpmRandomSource> random_source_;
52 std::unique_ptr<TpmAttestationRecordContext> attestation_context_;
53 std::unique_ptr<TpmRemoteProvisioningContext> remote_provisioning_context_;
54 std::map<keymaster_algorithm_t, std::unique_ptr<keymaster::KeyFactory>>
56 std::vector<keymaster_algorithm_t> supported_algorithms_;
57 uint32_t os_version_;
59 std::optional<uint32_t> vendor_patchlevel_;
60 std::optional<uint32_t> boot_patchlevel_;
61 std::optional<std::string> bootloader_state_;
62 std::optional<std::string> verified_boot_state_;
63 std::optional<std::vector<uint8_t>> vbmeta_digest_;
64
65 public:
66 TpmKeymasterContext(TpmResourceManager&, keymaster::KeymasterEnforcement&);
68
69 keymaster::KmVersion GetKmVersion() const override {
70 return attestation_context_->GetKmVersion();
71 }
72
73 keymaster_error_t SetSystemVersion(uint32_t os_version,
74 uint32_t os_patchlevel) override;
75 void GetSystemVersion(uint32_t* os_version,
76 uint32_t* os_patchlevel) const override;
77
78 const keymaster::KeyFactory* GetKeyFactory(
79 keymaster_algorithm_t algorithm) const override;
80 keymaster::OperationFactory* GetOperationFactory(
81 keymaster_algorithm_t algorithm,
82 keymaster_purpose_t purpose) const override;
83 const keymaster_algorithm_t* GetSupportedAlgorithms(
84 size_t* algorithms_count) const override;
85
86 keymaster_error_t UpgradeKeyBlob(
87 const keymaster::KeymasterKeyBlob& key_to_upgrade,
88 const keymaster::AuthorizationSet& upgrade_params,
89 keymaster::KeymasterKeyBlob* upgraded_key) const override;
90
91 keymaster_error_t ParseKeyBlob(
92 const keymaster::KeymasterKeyBlob& blob,
93 const keymaster::AuthorizationSet& additional_params,
94 keymaster::UniquePtr<keymaster::Key>* key) const override;
95
96 keymaster_error_t AddRngEntropy(const uint8_t* buf,
97 size_t length) const override;
98
99 keymaster::KeymasterEnforcement* enforcement_policy() override;
100
101 keymaster::AttestationContext* attestation_context() override {
102 return attestation_context_.get();
103 }
104
105 keymaster::CertificateChain GenerateAttestation(
106 const keymaster::Key& key,
107 const keymaster::AuthorizationSet& attest_params,
108 keymaster::UniquePtr<keymaster::Key> attest_key,
109 const keymaster::KeymasterBlob& issuer_subject,
110 keymaster_error_t* error) const override;
111
112 keymaster::CertificateChain GenerateSelfSignedCertificate(
113 const keymaster::Key& key, const keymaster::AuthorizationSet& cert_params,
114 bool fake_signature, keymaster_error_t* error) const override;
115
116 keymaster_error_t UnwrapKey(
117 const keymaster::KeymasterKeyBlob& wrapped_key_blob,
118 const keymaster::KeymasterKeyBlob& wrapping_key_blob,
119 const keymaster::AuthorizationSet& wrapping_key_params,
120 const keymaster::KeymasterKeyBlob& masking_key,
121 keymaster::AuthorizationSet* wrapped_key_params,
122 keymaster_key_format_t* wrapped_key_format,
123 keymaster::KeymasterKeyBlob* wrapped_key_material) const override;
124
125 keymaster_error_t CheckConfirmationToken(
126 const uint8_t* input_data, size_t input_data_size,
127 const uint8_t confirmation_token[keymaster::kConfirmationTokenSize])
128 const;
129
130 keymaster::RemoteProvisioningContext* GetRemoteProvisioningContext()
131 const override;
132
133 keymaster_error_t SetVerifiedBootInfo(
134 std::string_view verified_boot_state, std::string_view bootloader_state,
135 const std::vector<uint8_t>& vbmeta_digest) override;
136
137 keymaster_error_t SetVendorPatchlevel(uint32_t vendor_patchlevel) override;
138 keymaster_error_t SetBootPatchlevel(uint32_t boot_patchlevel) override;
139 std::optional<uint32_t> GetVendorPatchlevel() const override;
140 std::optional<uint32_t> GetBootPatchlevel() const override;
141
142 keymaster_error_t SetAttestationIds(
143 const keymaster::SetAttestationIdsRequest& request) override {
144 return attestation_context_->SetAttestationIds(request);
145 }
146
147 keymaster_error_t SetAttestationIdsKM3(
148 const keymaster::SetAttestationIdsKM3Request& request) override {
149 return attestation_context_->SetAttestationIdsKM3(request);
150 }
151};
152
153} // namespace cuttlefish
Definition: tpm_keymaster_context.h:46
std::optional< std::vector< uint8_t > > vbmeta_digest_
Definition: tpm_keymaster_context.h:63
std::unique_ptr< TpmRandomSource > random_source_
Definition: tpm_keymaster_context.h:51
uint32_t os_patchlevel_
Definition: tpm_keymaster_context.h:58
keymaster_error_t SetVendorPatchlevel(uint32_t vendor_patchlevel) override
Definition: tpm_keymaster_context.cpp:655
std::map< keymaster_algorithm_t, std::unique_ptr< keymaster::KeyFactory > > key_factories_
Definition: tpm_keymaster_context.h:55
std::optional< uint32_t > GetVendorPatchlevel() const override
Definition: tpm_keymaster_context.cpp:685
uint32_t os_version_
Definition: tpm_keymaster_context.h:57
void GetSystemVersion(uint32_t *os_version, uint32_t *os_patchlevel) const override
Definition: tpm_keymaster_context.cpp:123
std::optional< uint32_t > boot_patchlevel_
Definition: tpm_keymaster_context.h:60
std::optional< std::string > bootloader_state_
Definition: tpm_keymaster_context.h:61
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:621
keymaster_error_t SetBootPatchlevel(uint32_t boot_patchlevel) override
Definition: tpm_keymaster_context.cpp:670
std::vector< keymaster_algorithm_t > supported_algorithms_
Definition: tpm_keymaster_context.h:56
keymaster::OperationFactory * GetOperationFactory(keymaster_algorithm_t algorithm, keymaster_purpose_t purpose) const override
Definition: tpm_keymaster_context.cpp:139
keymaster_error_t SetAttestationIds(const keymaster::SetAttestationIdsRequest &request) override
Definition: tpm_keymaster_context.h:142
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:289
const keymaster_algorithm_t * GetSupportedAlgorithms(size_t *algorithms_count) const override
Definition: tpm_keymaster_context.cpp:155
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:241
keymaster::RemoteProvisioningContext * GetRemoteProvisioningContext() const override
Definition: tpm_keymaster_context.cpp:606
keymaster::AttestationContext * attestation_context() override
Definition: tpm_keymaster_context.h:101
std::unique_ptr< TpmAttestationRecordContext > attestation_context_
Definition: tpm_keymaster_context.h:52
keymaster_error_t SetSystemVersion(uint32_t os_version, uint32_t os_patchlevel) override
Definition: tpm_keymaster_context.cpp:113
keymaster_error_t SetAttestationIdsKM3(const keymaster::SetAttestationIdsKM3Request &request) override
Definition: tpm_keymaster_context.h:147
std::unique_ptr< TpmKeyBlobMaker > key_blob_maker_
Definition: tpm_keymaster_context.h:50
keymaster_error_t CheckConfirmationToken(const uint8_t *input_data, size_t input_data_size, const uint8_t confirmation_token[keymaster::kConfirmationTokenSize]) const
Definition: tpm_keymaster_context.cpp:584
const keymaster::KeyFactory * GetKeyFactory(keymaster_algorithm_t algorithm) const override
Definition: tpm_keymaster_context.cpp:129
std::unique_ptr< TpmRemoteProvisioningContext > remote_provisioning_context_
Definition: tpm_keymaster_context.h:53
keymaster::KeymasterEnforcement & enforcement_
Definition: tpm_keymaster_context.h:49
TpmResourceManager & resource_manager_
Definition: tpm_keymaster_context.h:48
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:365
keymaster::KmVersion GetKmVersion() const override
Definition: tpm_keymaster_context.h:69
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:190
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:340
TpmKeymasterContext(TpmResourceManager &, keymaster::KeymasterEnforcement &)
Definition: tpm_keymaster_context.cpp:85
std::optional< uint32_t > vendor_patchlevel_
Definition: tpm_keymaster_context.h:59
keymaster::KeymasterEnforcement * enforcement_policy() override
Definition: tpm_keymaster_context.cpp:282
std::optional< std::string > verified_boot_state_
Definition: tpm_keymaster_context.h:62
std::optional< uint32_t > GetBootPatchlevel() const override
Definition: tpm_keymaster_context.cpp:689
keymaster_error_t AddRngEntropy(const uint8_t *buf, size_t length) const override
Definition: tpm_keymaster_context.cpp:277
Definition: tpm_resource_manager.h:51
#define key
Definition: dict.c:55
#define error(format, args...)
Definition: fec_private.h:201
Definition: alloc_driver.h:20