20 std::span<const uint8_t> identity,
21 std::span<const uint8_t> context,
22 std::string_view hash) :
23 m_key(key.begin(), key.end()),
24 m_identity(identity.begin(), identity.end()),
25 m_context(context.begin(), context.end()),
27 BOTAN_ARG_CHECK(m_hash ==
"SHA-256" || m_hash ==
"SHA-384",
"PSK importer hash must be SHA-256 or SHA-384");
36 BOTAN_ARG_CHECK(!m_identity.empty(),
"PSK importer identity must not be empty");
40 BOTAN_ARG_CHECK(m_identity.size() + m_context.size() + 8 <= std::numeric_limits<uint16_t>::max(),
41 "PSK importer identity + context too long for a TLS PSK identity");
45 BOTAN_ARG_CHECK(version == Protocol_Version::TLS_V13,
"PSK importer is only defined for TLS 1.3");
47 "PSK importer target hash must be SHA-256 or SHA-384");
52 const uint16_t target_kdf = (target_hash ==
"SHA-256") ? uint16_t(0x0001) : uint16_t(0x0002);
57 const auto id_len =
static_cast<uint16_t
>(m_identity.size());
58 const auto ctx_len =
static_cast<uint16_t
>(m_context.size());
67 hash_fn->update(imported_identity);
68 const auto identity_hash = hash_fn->final_stdvec();
71 const size_t psk_hash_len = hash_fn->output_length();
74 const std::vector<uint8_t> salt(psk_hash_len, 0);
75 const auto epskx = hkdf_extract->derive_key(psk_hash_len, m_key, salt, {});
94 const std::string target_hash_str(target_hash);
96 const size_t target_hash_len = target_hash_fn->output_length();
97 const auto expand_out_len =
static_cast<uint16_t
>(target_hash_len);
101 const std::array<uint8_t, 17> prefixed_label = {
102 't',
'l',
's',
'1',
'3',
' ',
'd',
'e',
'r',
'i',
'v',
'e',
'd',
' ',
'p',
's',
'k'};
106 const auto prefixed_label_len =
static_cast<uint8_t
>(prefixed_label.size());
107 const auto identity_hash_len =
static_cast<uint8_t
>(identity_hash.size());
115 hkdf_expand->derive_key(ipskx, epskx, hkdf_label, {});
117 const std::string wire_identity(imported_identity.begin(), imported_identity.end());
118 return ExternalPSK(wire_identity, target_hash_str, std::move(ipskx),
true);