Botan 3.12.0
Crypto and TLS for C&
tls_psk_13.h
Go to the documentation of this file.
1/**
2 * TLS 1.3 Preshared Key identity and importer
3 * (C) 2023 Jack Lloyd
4 * 2023 René Meusel - Rohde & Schwarz Cybersecurity
5 * 2023 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
6 * 2025,2026 Jack Lloyd
7 *
8 * Botan is released under the Simplified BSD License (see license.txt)
9 */
10
11#ifndef BOTAN_TLS_PSK_13_H_
12#define BOTAN_TLS_PSK_13_H_
13
14#include <botan/strong_type.h>
15#include <botan/tls_external_psk.h>
16#include <botan/tls_session.h> // TODO remove this dep
17#include <botan/tls_version.h>
18#include <botan/types.h>
19#include <chrono>
20#include <span>
21#include <string>
22#include <string_view>
23#include <vector>
24
25namespace Botan::TLS {
26
27/// @brief holds a PSK identity as used in TLS 1.3
29
30/**
31 * Represents a TLS 1.3 PSK identity as found in the Preshared Key extension
32 * with an opaque identity and an associated (obfuscated) ticket age. The latter
33 * is not applicable for externally provided PSKs.
34 */
36 public:
37 /**
38 * Construct from information provided in the peer's ClientHello
39 */
40 PskIdentity(std::vector<uint8_t> identity, const uint32_t obfuscated_age) :
41 m_identity(std::move(identity)), m_obfuscated_age(obfuscated_age) {}
42
43 /**
44 * Construct from a session stored by the client
45 */
46 PskIdentity(Opaque_Session_Handle identity, std::chrono::milliseconds age, uint32_t ticket_age_add);
47
48 /**
49 * Construct from an externally provided PSK in the client
50 */
52
53 const std::vector<uint8_t>& identity() const { return m_identity; }
54
55 std::string identity_as_string() const;
56
57 /**
58 * If this represents a PSK for session resumption, it returns the
59 * session's age given the de-obfuscation parameter @p ticket_age_add. For
60 * externally provided PSKs this method does not provide any meaningful
61 * information.
62 */
63 std::chrono::milliseconds age(uint32_t ticket_age_add) const;
64
65 uint32_t obfuscated_age() const { return m_obfuscated_age; }
66
67 private:
68 std::vector<uint8_t> m_identity;
69 uint32_t m_obfuscated_age;
70};
71
72/**
73 * Botan 3.0.0 used the class name "Ticket". In Botan 3.1.0 we decided to
74 * re-name it to the more generic term "PskIdentity" to better reflect its dual
75 * use case for resumption and externally provided PSKs.
76 */
77BOTAN_DEPRECATED("Use PskIdentity") typedef PskIdentity Ticket;
78
79/**
80 * RFC 9258 PSK Importer.
81 *
82 * Holds the base key material and identity for a pre-shared key and
83 * derives imported PSKs for specific TLS protocol versions and cipher
84 * suite hash algorithms using the PSK importer mechanism
85 */
87 public:
88 /**
89 * @param key the base pre-shared key
90 * @param identity the external PSK identity
91 * @param context optional importer context
92 * @param hash the hash algorithm provisioned with this PSK ("SHA-256" or "SHA-384")
93 * which defaults to SHA-256 due to RFC 9258's "If the EPSK does not have [...]
94 * an associated hash function, SHA-256 SHOULD be used."
95 */
96 PSKImporter(std::span<const uint8_t> key,
97 std::span<const uint8_t> identity,
98 std::span<const uint8_t> context,
99 std::string_view hash = "SHA-256");
100
101 /**
102 * Derive an imported PSK for the given target protocol version and
103 * cipher suite hash algorithm.
104 *
105 * @param version target TLS protocol version (must be TLS 1.3)
106 * @param target_hash hash algorithm of the target cipher suite ("SHA-256" or "SHA-384")
107 * @return an ExternalPSK ready for use in a TLS 1.3 handshake
108 */
109 ExternalPSK derive_imported_psk(Protocol_Version version, std::string_view target_hash) const;
110
111 private:
113 std::vector<uint8_t> m_identity;
114 std::vector<uint8_t> m_context;
115 std::string m_hash;
116};
117
118} // namespace Botan::TLS
119
120#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
PSKImporter(std::span< const uint8_t > key, std::span< const uint8_t > identity, std::span< const uint8_t > context, std::string_view hash="SHA-256")
ExternalPSK derive_imported_psk(Protocol_Version version, std::string_view target_hash) const
PskIdentity(std::vector< uint8_t > identity, const uint32_t obfuscated_age)
Definition tls_psk_13.h:40
uint32_t obfuscated_age() const
Definition tls_psk_13.h:65
const std::vector< uint8_t > & identity() const
Definition tls_psk_13.h:53
Strong< std::string, struct PresharedKeyID_ > PresharedKeyID
holds a PSK identity as used in TLS 1.3
Definition tls_psk_13.h:28
PskIdentity Ticket
Definition tls_psk_13.h:77
Strong< std::vector< uint8_t >, struct Opaque_Session_Handle_ > Opaque_Session_Handle
holds an opaque session handle as used in TLS 1.3 that could be either a ticket for stateless resumpt...
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68