#include #include #include #include #include namespace microbu_v2x::pki { namespace { bool same_or_newer(unsigned old_seq, unsigned next_seq, const ByteBuffer& old, const ByteBuffer& next) { if (next_seq == old_seq) return old == next; auto generation_time = [](const ByteBuffer& bytes) { vanetza::security::v3::SecuredMessage message; if (!message.decode(bytes)) return decltype(message.generation_time()) {}; return message.generation_time(); }; const auto old_time = generation_time(old); const auto next_time = generation_time(next); if (!old_time || !next_time || *next_time < *old_time) return false; // TS 102 941 clause 6.3.4 wraps ctlSequence at 255. Authenticated time // distinguishes a wrapped full list from replay of an earlier cycle. return next_seq > old_seq || *next_time > *old_time; } bool decode_certificate(const ByteBuffer& bytes, Certificate& cert) { return cert.decode(bytes) && cert.encode() == bytes; } } ByteBuffer encode_snapshot(const TrustSnapshot& snapshot) { ByteBuffer bytes {'T', 'S', 'N', 1}; for (const auto* blob : {&snapshot.tlm, &snapshot.rca, &snapshot.ectl, &snapshot.ctl, &snapshot.crl}) { if (blob->empty() || blob->size() > 32768) return {}; bytes.push_back(blob->size() >> 8); bytes.push_back(blob->size() & 255); bytes.insert(bytes.end(), blob->begin(), blob->end()); } for (int shift = 24; shift >= 0; shift -= 8) bytes.push_back(snapshot.deadline >> shift); return bytes.size() <= 60000 ? bytes : ByteBuffer {}; } bool decode_snapshot(const ByteBuffer& bytes, TrustSnapshot& out) { if (bytes.size() < 8 || bytes.size() > 60000 || !std::equal(bytes.begin(), bytes.begin() + 4, ByteBuffer {'T','S','N',1}.begin())) return false; TrustSnapshot parsed; std::size_t pos = 4; for (auto* blob : {&parsed.tlm, &parsed.rca, &parsed.ectl, &parsed.ctl, &parsed.crl}) { if (pos + 2 > bytes.size()) return false; const std::size_t length = (bytes[pos] << 8) | bytes[pos + 1]; pos += 2; if (!length || length > 32768 || length > bytes.size() - pos) return false; blob->assign(bytes.begin() + pos, bytes.begin() + pos + length); pos += length; } if (pos + 4 != bytes.size()) return false; for (int i = 0; i < 4; ++i) parsed.deadline = (parsed.deadline << 8) | bytes[pos++]; out = std::move(parsed); return true; } bool validate_update(vanetza::security::Backend& backend, vanetza::Clock::time_point now, const ByteBuffer& pinned_tlm, const TrustSnapshot* previous, const TrustSnapshot& proposed, TrustSnapshot& accepted) { Certificate tlm, rca; if (!decode_certificate(proposed.tlm, tlm) || !decode_certificate(proposed.rca, rca) || !tlm.valid_at_timepoint(now) || !rca.valid_at_timepoint(now)) return false; const auto time = vanetza::security::v2::convert_time32(now); for (const auto* wire : {&proposed.ectl, &proposed.ctl, &proposed.crl}) { vanetza::security::v3::SecuredMessage msg; if (!msg.decode(*wire) || !msg.generation_time() || *msg.generation_time() > vanetza::security::v2::convert_time64(now)) return false; } // TS 102 941 clause 6.3.6: authenticate every object, releasing decoded // certificate graphs between lists so live reception can continue during updates. std::optional old_ectl_sequence, old_ctl_sequence; std::optional old_crl; std::vector> old_authorities; if (previous) { { Certificate old_tlm; if (!decode_certificate(previous->tlm, old_tlm)) return false; const auto old_ectl = parse_tlm_ctl(backend, previous->ectl, old_tlm); if (!old_ectl) return false; old_ectl_sequence = old_ectl->sequence; if (proposed.tlm != previous->tlm && std::none_of(old_ectl->tlms.begin(), old_ectl->tlms.end(), [&](const auto& c) { return c.encode() == proposed.tlm; })) return false; } Certificate old_rca; if (!decode_certificate(previous->rca, old_rca)) return false; { const auto old_ctl = parse_rca_ctl(backend, previous->ctl, old_rca); if (!old_ctl) return false; old_ctl_sequence = old_ctl->sequence; for (const auto* list : {&old_ctl->ea, &old_ctl->aa}) for (const auto& cert : *list) { const auto id = cert.calculate_digest(); if (!id) return false; old_authorities.emplace_back(*id, microbu_v2x::security::validity_bounds(cert).end_validity); } } old_crl = parse_crl(backend, previous->crl, old_rca); if (!old_crl) return false; } else if (pinned_tlm.empty() || proposed.tlm != pinned_tlm) return false; Time32 deadline = std::min(microbu_v2x::security::validity_bounds(tlm).end_validity, microbu_v2x::security::validity_bounds(rca).end_validity); { const auto ectl = parse_tlm_ctl(backend, proposed.ectl, tlm); if (!ectl || !ectl->full || ectl->next_update <= time || std::none_of(ectl->roots.begin(), ectl->roots.end(), [&](const auto& c) { return c.encode() == proposed.rca; })) return false; deadline = std::min(deadline, ectl->next_update); if (previous && proposed.tlm == previous->tlm && !same_or_newer(*old_ectl_sequence, ectl->sequence, previous->ectl, proposed.ectl)) return false; } { const auto ctl = parse_rca_ctl(backend, proposed.ctl, rca); if (!ctl || !ctl->full || ctl->next_update <= time) return false; deadline = std::min(deadline, ctl->next_update); if (previous && proposed.rca == previous->rca && !same_or_newer(*old_ctl_sequence, ctl->sequence, previous->ctl, proposed.ctl)) return false; for (const auto* list : {&ctl->ea, &ctl->aa}) for (const auto& cert : *list) { if (!cert.valid_at_timepoint(now)) return false; deadline = std::min(deadline, microbu_v2x::security::validity_bounds(cert).end_validity); } } const auto crl = parse_crl(backend, proposed.crl, rca); if (!crl || crl->next_update <= time || crl->this_update > time || crl->next_update <= crl->this_update) return false; deadline = std::min(deadline, crl->next_update); if (previous && proposed.rca == previous->rca) { if (crl->this_update < old_crl->this_update || (crl->this_update == old_crl->this_update && proposed.crl != previous->crl)) return false; // Revocation is permanent until the known certificate expires. // Unknown removed entries cannot be proven expired and fail closed. for (const auto& id : old_crl->revoked) { if (std::find(crl->revoked.begin(), crl->revoked.end(), id) != crl->revoked.end()) continue; const auto known = std::find_if(old_authorities.begin(), old_authorities.end(), [&](const auto& item) { return item.first == id; }); if (known == old_authorities.end() || known->second > time) return false; } } accepted = proposed; accepted.deadline = deadline; return true; } bool snapshot_credentials(vanetza::security::Backend& backend, const TrustSnapshot& state, microbu_v2x::security::StoredCredentials& credentials, microbu_v2x::security::TrustConfiguration* trust) { Certificate tlm, rca; if (!decode_certificate(state.tlm, tlm) || !decode_certificate(state.rca, rca)) return false; const auto crl = parse_crl(backend, state.crl, rca); if (!crl) return false; // TS 102 941 clauses 6.3.1 and 6.3.6: ECTL anchors apply to reception. // The selected home RCA determines credential management and its CTL. std::vector roots, authorities; { const auto ectl = parse_tlm_ctl(backend, state.ectl, tlm); if (!ectl || !ectl->full || std::none_of(ectl->roots.begin(), ectl->roots.end(), [&](const auto& c) { return c.encode() == state.rca; })) return false; for (const auto& cert : ectl->roots) roots.push_back(cert.encode()); } { const auto ctl = parse_rca_ctl(backend, state.ctl, rca); if (!ctl || !ctl->full) return false; for (const auto* list : {&ctl->ea, &ctl->aa}) for (const auto& cert : *list) authorities.push_back(cert.encode()); } credentials.roots = std::move(roots); credentials.authorities = std::move(authorities); if (trust) apply(*crl, rca, *trust); return true; } }