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");
37 BOTAN_ARG_CHECK(!m_identity.empty(),
"PSK importer identity must not be empty");
41 BOTAN_ARG_CHECK(m_identity.size() + m_context.size() + 8 <= std::numeric_limits<uint16_t>::max(),
42 "PSK importer identity + context too long for a TLS PSK identity");
46 BOTAN_ARG_CHECK(version == Protocol_Version::TLS_V13,
"PSK importer is only defined for TLS 1.3");
48 "PSK importer target hash must be SHA-256 or SHA-384");
52 const size_t binder_length = (target_hash ==
"SHA-256") ? 32 : 48;
53 BOTAN_ARG_CHECK(m_identity.size() + m_context.size() + 8 + 11 + binder_length <= 65535,
54 "PSK importer identity + context too long for a TLS PSK extension");
59 const uint16_t target_kdf = (target_hash ==
"SHA-256") ? uint16_t(0x0001) : uint16_t(0x0002);
64 const auto id_len =
static_cast<uint16_t
>(m_identity.size());
65 const auto ctx_len =
static_cast<uint16_t
>(m_context.size());
74 hash_fn->update(imported_identity);
75 const auto identity_hash = hash_fn->final_stdvec();
78 const size_t psk_hash_len = hash_fn->output_length();
81 const std::vector<uint8_t> salt(psk_hash_len, 0);
82 const auto epskx = hkdf_extract->derive_key(psk_hash_len, m_key, salt, {});
101 const std::string target_hash_str(target_hash);
103 const size_t target_hash_len = target_hash_fn->output_length();
104 const auto expand_out_len =
static_cast<uint16_t
>(target_hash_len);
108 const std::array<uint8_t, 17> prefixed_label = {
109 't',
'l',
's',
'1',
'3',
' ',
'd',
'e',
'r',
'i',
'v',
'e',
'd',
' ',
'p',
's',
'k'};
113 const auto prefixed_label_len =
static_cast<uint8_t
>(prefixed_label.size());
114 const auto identity_hash_len =
static_cast<uint8_t
>(identity_hash.size());
122 hkdf_expand->derive_key(ipskx, epskx, hkdf_label, {});
124 const std::string wire_identity(imported_identity.begin(), imported_identity.end());
125 return ExternalPSK(wire_identity, target_hash_str, std::move(ipskx),
true);