|
| 1 | +#include "vcek_cache.h" |
| 2 | + |
| 3 | +#include <curl/curl.h> |
| 4 | +#include <nlohmann/json.hpp> |
| 5 | + |
| 6 | +#include <stdexcept> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace accless::attestation::snp { |
| 10 | +namespace { |
| 11 | + |
| 12 | +constexpr const char *THIM_URL = |
| 13 | + "http://169.254.169.254/metadata/THIM/amd/certification"; |
| 14 | +constexpr const char *THIM_METADATA_HEADER = "Metadata:true"; |
| 15 | + |
| 16 | +struct VcekCache { |
| 17 | + std::once_flag once; |
| 18 | + std::string vcekCert; |
| 19 | + std::string certChain; |
| 20 | + std::string bundle; |
| 21 | + std::string error; |
| 22 | +}; |
| 23 | + |
| 24 | +VcekCache g_cache; |
| 25 | + |
| 26 | +size_t curlWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata) { |
| 27 | + auto *out = static_cast<std::string *>(userdata); |
| 28 | + if (!out) |
| 29 | + return 0; |
| 30 | + const size_t total = size * nmemb; |
| 31 | + out->append(ptr, total); |
| 32 | + return total; |
| 33 | +} |
| 34 | + |
| 35 | +// Perform one-time VCEK fetch, but *never throw*. If unavailable, returns empty |
| 36 | +// PEM strings. |
| 37 | +void initVcekCache() { |
| 38 | + CURL *curl = curl_easy_init(); |
| 39 | + if (!curl) { |
| 40 | + g_cache.error = "failed to init curl for VCEK fetch"; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + std::string response; |
| 45 | + char errbuf[CURL_ERROR_SIZE] = {0}; |
| 46 | + |
| 47 | + curl_easy_setopt(curl, CURLOPT_URL, THIM_URL); |
| 48 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 49 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback); |
| 50 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); |
| 51 | + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); |
| 52 | + curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 500L); // do not block |
| 53 | + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 300L); |
| 54 | + |
| 55 | + // Add Metadata:true header |
| 56 | + struct curl_slist *headers = nullptr; |
| 57 | + headers = curl_slist_append(headers, THIM_METADATA_HEADER); |
| 58 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 59 | + |
| 60 | + CURLcode res = curl_easy_perform(curl); |
| 61 | + long status = 0; |
| 62 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); |
| 63 | + |
| 64 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, nullptr); |
| 65 | + curl_slist_free_all(headers); |
| 66 | + curl_easy_cleanup(curl); |
| 67 | + |
| 68 | + // IMDS unreachable → not a CVM → silently return empty values |
| 69 | + if (res != CURLE_OK) { |
| 70 | + g_cache.error = std::string("VCEK fetch failed: curl error: ") + |
| 71 | + (errbuf[0] ? errbuf : curl_easy_strerror(res)); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (status != 200) { |
| 76 | + g_cache.error = |
| 77 | + "VCEK fetch failed: HTTP status " + std::to_string(status); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Parse JSON. |
| 82 | + try { |
| 83 | + auto json = nlohmann::json::parse(response); |
| 84 | + |
| 85 | + g_cache.vcekCert = json.value("vcekCert", ""); |
| 86 | + g_cache.certChain = json.value("certificateChain", ""); |
| 87 | + |
| 88 | + // Normalize newlines |
| 89 | + if (!g_cache.vcekCert.empty() && g_cache.vcekCert.back() != '\n') |
| 90 | + g_cache.vcekCert.push_back('\n'); |
| 91 | + |
| 92 | + if (!g_cache.certChain.empty() && g_cache.certChain.back() != '\n') |
| 93 | + g_cache.certChain.push_back('\n'); |
| 94 | + |
| 95 | + g_cache.bundle = g_cache.vcekCert + g_cache.certChain; |
| 96 | + } catch (const std::exception &e) { |
| 97 | + g_cache.error = std::string("VCEK fetch JSON parse error: ") + e.what(); |
| 98 | + // Leave empty certs |
| 99 | + return; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void ensureInitialized() { std::call_once(g_cache.once, initVcekCache); } |
| 104 | + |
| 105 | +} // namespace |
| 106 | + |
| 107 | +// --- Public API --- |
| 108 | + |
| 109 | +const std::string &getVcekPemBundle() { |
| 110 | + ensureInitialized(); |
| 111 | + return g_cache.bundle; |
| 112 | +} |
| 113 | + |
| 114 | +const std::string &getVcekCertPem() { |
| 115 | + ensureInitialized(); |
| 116 | + return g_cache.vcekCert; |
| 117 | +} |
| 118 | + |
| 119 | +const std::string &getVcekChainPem() { |
| 120 | + ensureInitialized(); |
| 121 | + return g_cache.certChain; |
| 122 | +} |
| 123 | +} // namespace accless::attestation::snp |
0 commit comments