CCF
Loading...
Searching...
No Matches
quote_generation.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6#include "ccf/pal/snp_ioctl.h"
7#include "ds/files.h"
8
9#include <nlohmann/json.hpp>
10#include <string>
11
12namespace ccf::pal
13{
14 static std::string virtual_attestation_path(const std::string& suffix)
15 {
16 return fmt::format("ccf_virtual_attestation.{}.{}", ::getpid(), suffix);
17 };
18
19 static void emit_virtual_measurement()
20 {
21 const auto package_path = std::filesystem::canonical("/proc/self/exe");
23 std::ifstream f(package_path, std::ios::binary | std::ios::ate);
24 if (!f)
25 {
26 throw std::runtime_error(fmt::format(
27 "Cannot emit virtual measurement: Cannot open file {}", package_path));
28 }
29
30 const size_t size = f.tellg();
31 f.seekg(0, std::ios::beg);
32
33 static constexpr size_t buf_size = 4096;
34 char buf[buf_size];
35
36 size_t handled = 0;
37 while (handled < size)
38 {
39 const auto this_read = std::min(size - handled, buf_size);
40 f.read(buf, this_read);
41
42 hasher->update_hash({(const uint8_t*)buf, this_read});
43
44 handled += this_read;
45 }
46
47 const auto package_hash = hasher->finalise();
48
49 auto j = nlohmann::json::object();
50
51 j["measurement"] = "Insecure hard-coded virtual measurement v1";
52 j["host_data"] = package_hash.hex_str();
53
54 files::dump(j.dump(2), virtual_attestation_path("measurement"));
55 }
56
57 static void generate_virtual_quote(
58 PlatformAttestationReportData& report_data,
59 RetrieveEndorsementCallback endorsement_cb,
60 const snp::EndorsementsServers& endorsements_servers = {})
61 {
62 auto quote = files::slurp_json(virtual_attestation_path("measurement"));
63 quote["report_data"] = ccf::crypto::b64_from_raw(report_data.data);
64
65 files::dump(quote.dump(2), virtual_attestation_path("attestation"));
66
67 auto dumped_quote = quote.dump();
68 std::vector<uint8_t> quote_vec(dumped_quote.begin(), dumped_quote.end());
69
70 endorsement_cb(
72 .quote = quote_vec,
73 .endorsements = {},
74 .uvm_endorsements = {},
75 .endorsed_tcb = {}},
76 {});
77 }
78
79 static void generate_snp_quote(
80 PlatformAttestationReportData& report_data,
81 RetrieveEndorsementCallback endorsement_cb,
82 const snp::EndorsementsServers& endorsements_servers = {})
83 {
84 QuoteInfo node_quote_info = {};
85 node_quote_info.format = QuoteFormat::amd_sev_snp_v1;
86 auto attestation = snp::get_attestation(report_data);
87
88 node_quote_info.quote = attestation->get_raw();
89
90 if (endorsement_cb != nullptr)
91 {
92 endorsement_cb(
93 node_quote_info,
94 snp::make_endorsement_endpoint_configuration(
95 attestation->get(), endorsements_servers));
96 }
97 }
98
99 static void generate_quote(
100 PlatformAttestationReportData& report_data,
101 RetrieveEndorsementCallback endorsement_cb,
102 const snp::EndorsementsServers& endorsements_servers = {})
103 {
104 switch (ccf::pal::platform)
105 {
107 {
108 generate_snp_quote(report_data, endorsement_cb, endorsements_servers);
109 break;
110 }
111
113 {
114 generate_virtual_quote(
115 report_data, endorsement_cb, endorsements_servers);
116 break;
117 }
118
119 default:
120 {
121 throw std::logic_error(fmt::format(
122 "Unsupported platform for quote generation: {}", ccf::pal::platform));
123 }
124 }
125 }
126}
std::string b64_from_raw(const uint8_t *data, size_t size)
Definition base64.cpp:41
std::shared_ptr< ISha256Hash > make_incremental_sha256()
Definition hash.cpp:46
std::vector< EndorsementsServer > EndorsementsServers
Definition attestation_sev_snp_endorsements.h:87
Definition attestation.h:20
std::function< void(const QuoteInfo &quote_info, const snp::EndorsementEndpointsConfiguration &config)> RetrieveEndorsementCallback
Definition attestation.h:26