12#ifdef SGX_ATTESTATION_VERIFICATION
13# include <openenclave/attestation/verifier.h>
18#if defined(INSIDE_ENCLAVE) && !defined(VIRTUAL_ENCLAVE)
19# include <openenclave/enclave.h>
20#elif defined(SGX_ATTESTATION_VERIFICATION)
21# include <openenclave/host_verify.h>
26 static void legacy_remove_jwt_public_signing_keys(
32 Tables::Legacy::JWT_PUBLIC_SIGNING_KEY_ISSUER);
35 [&issuer, &keys, &key_issuer](
const auto& k,
const auto& v) {
39 key_issuer->remove(k);
45 static bool check_issuer_constraint(
46 const std::string& issuer,
const std::string& constraint)
55 if (constraint_domain.empty())
67 if (issuer_domain != constraint_domain)
69 const auto pattern =
"." + constraint_domain;
70 return issuer_domain.ends_with(pattern);
76 static void remove_jwt_public_signing_keys(
82 legacy_remove_jwt_public_signing_keys(tx, issuer);
87 keys->foreach([&issuer, &keys](
const auto& k,
const auto& v) {
88 auto it = find_if(v.begin(), v.end(), [&](
const auto& metadata) {
89 return metadata.issuer == issuer;
94 std::vector<OpenIDJWKMetadata> updated(v.begin(), it);
95 updated.insert(updated.end(), ++it, v.end());
99 keys->put(k, updated);
110#ifdef SGX_ATTESTATION_VERIFICATION
111 static oe_result_t oe_verify_attestation_certificate_with_evidence_cb(
112 oe_claim_t* claims,
size_t claims_length,
void* arg)
114 auto claims_map = (std::map<std::string, std::vector<uint8_t>>*)arg;
115 for (
size_t i = 0; i < claims_length; i++)
117 std::string claim_name(claims[i].name);
118 std::vector<uint8_t> claim_value(
119 claims[i].value, claims[i].value + claims[i].value_size);
120 claims_map->emplace(std::move(claim_name), std::move(claim_value));
126 static bool set_jwt_public_signing_keys(
128 const std::string& log_prefix,
130 const JwtIssuerMetadata& issuer_metadata,
131 const JsonWebKeySet& jwks)
136 if (jwks.keys.empty())
141 std::map<std::string, std::vector<uint8_t>> new_keys;
142 std::map<std::string, JwtIssuer> issuer_constraints;
143 for (
auto& jwk : jwks.keys)
145 if (!jwk.kid.has_value())
151 if (!jwk.x5c.has_value() && jwk.x5c->empty())
153 LOG_FAIL_FMT(
"{}: JWKS is invalid (empty x5c)", log_prefix);
157 auto& der_base64 = jwk.x5c.value()[0];
159 auto const& kid = jwk.kid.value();
164 catch (
const std::invalid_argument& e)
167 "{}: Could not parse x5c of key id {}: {}",
174 std::map<std::string, std::vector<uint8_t>> claims;
175 bool has_key_policy_sgx_claims = issuer_metadata.key_policy.has_value() &&
176 issuer_metadata.key_policy.value().sgx_claims.has_value() &&
177 !issuer_metadata.key_policy.value().sgx_claims.value().empty();
180 has_key_policy_sgx_claims)
182#ifdef SGX_ATTESTATION_VERIFICATION
183 oe_verify_attestation_certificate_with_evidence(
186 oe_verify_attestation_certificate_with_evidence_cb,
189 LOG_FAIL_FMT(
"{}: SGX claims not supported", log_prefix);
198 "{}: Skipping JWT signing key with kid {} (not OE "
205 if (has_key_policy_sgx_claims)
207 for (
auto& [claim_name, expected_claim_val_hex] :
208 issuer_metadata.key_policy.value().sgx_claims.value())
210 if (claims.find(claim_name) == claims.end())
213 "{}: JWKS kid {} is missing the {} SGX claim",
219 auto& actual_claim_val = claims[claim_name];
220 auto actual_claim_val_hex = ds::to_hex(actual_claim_val);
221 if (expected_claim_val_hex != actual_claim_val_hex)
224 "{}: JWKS kid {} has a mismatching {} SGX claim: {} != {}",
228 expected_claim_val_hex,
229 actual_claim_val_hex);
239 (std::vector<uint8_t>)der);
241 catch (std::invalid_argument& exc)
244 "{}: JWKS kid {} has an invalid X.509 certificate: {}",
251 LOG_INFO_FMT(
"{}: Storing JWT signing key with kid {}", log_prefix, kid);
252 new_keys.emplace(kid, der);
256 if (!check_issuer_constraint(issuer, *jwk.issuer))
259 "{}: JWKS kid {} with issuer constraint {} fails validation "
268 issuer_constraints.emplace(kid, *jwk.issuer);
272 if (new_keys.empty())
274 LOG_FAIL_FMT(
"{}: no keys left after applying filter", log_prefix);
278 std::set<std::string> existing_kids;
279 keys->foreach([&existing_kids, &issuer_constraints, &issuer](
280 const auto& k,
const auto& v) {
281 if (find_if(v.begin(), v.end(), [&](
const auto& metadata) {
282 return metadata.issuer == issuer;
285 existing_kids.insert(k);
291 for (
auto& [kid, der] : new_keys)
293 OpenIDJWKMetadata value{der, issuer, std::nullopt};
294 const auto it = issuer_constraints.find(kid);
295 if (it != issuer_constraints.end())
297 value.constraint = it->second;
300 if (existing_kids.count(kid))
302 const auto& keys_for_kid = keys->get(kid);
305 keys_for_kid->begin(),
307 [&value](
const auto& metadata) {
308 return metadata.cert == value.cert &&
309 metadata.issuer == value.issuer &&
310 metadata.constraint == value.constraint;
311 }) != keys_for_kid->end())
319 "Save JWT key kid={} issuer={}, constraint={}",
324 auto existing_keys = keys->get(kid);
327 const auto prev = find_if(
328 existing_keys->begin(),
329 existing_keys->end(),
330 [&](
const auto& issuer_with_constraint) {
331 return issuer_with_constraint.issuer == issuer;
334 if (prev != existing_keys->end())
340 existing_keys->push_back(std::move(value));
342 keys->put(kid, *existing_keys);
346 keys->put(kid, std::vector<OpenIDJWKMetadata>{value});
350 for (
auto& kid : existing_kids)
352 if (!new_keys.contains(kid))
354 auto updated = keys->get(kid);
359 [&](
const auto& metadata) { return metadata.issuer == issuer; }),
362 if (updated->empty())
368 keys->put(kid, *updated);
M::Handle * rw(M &m)
Definition tx.h:213
#define LOG_INFO_FMT
Definition logger.h:395
#define LOG_DEBUG_FMT
Definition logger.h:380
#define LOG_FAIL_FMT
Definition logger.h:396
ccf::kv::RawCopySerialisedMap< JwtKeyId, JwtIssuer > JwtPublicSigningKeyIssuer
Definition jwt.h:92
VerifierUniquePtr make_unique_verifier(const std::vector< uint8_t > &cert)
Definition verifier.cpp:13
std::vector< uint8_t > raw_from_b64(const std::string_view &b64_string)
Definition base64.cpp:12
Definition app_interface.h:15
std::vector< uint8_t > Cert
Definition jwt.h:59
ServiceMap< JwtKeyId, std::vector< OpenIDJWKMetadata > > JwtPublicSigningKeys
Definition jwt.h:73
URL parse_url_full(const std::string &url)
Definition http_parser.h:145
std::string host
Definition http_parser.h:138
constexpr oe_result_t OE_OK
Definition virtual_enclave.h:41
int oe_result_t
Definition virtual_enclave.h:40