• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.06
/src/lib/tls/msg_session_ticket.cpp
1
/*
2
* Session Tickets
3
* (C) 2012 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/tls_messages.h>
9

10
#include <botan/rng.h>
11
#include <botan/tls_callbacks.h>
12
#include <botan/tls_session.h>
13
#include <botan/tls_session_manager.h>
14
#include <botan/internal/loadstor.h>
15
#include <botan/internal/tls_handshake_hash.h>
16
#include <botan/internal/tls_handshake_io.h>
17
#include <botan/internal/tls_reader.h>
18

19
#include <botan/tls_exceptn.h>
20

21
#include <span>
22

23
namespace Botan::TLS {
24

25
New_Session_Ticket_12::New_Session_Ticket_12(Handshake_IO& io,
389✔
26
                                             Handshake_Hash& hash,
27
                                             Session_Ticket ticket,
28
                                             std::chrono::seconds lifetime) :
389✔
29
      m_ticket_lifetime_hint(lifetime), m_ticket(std::move(ticket)) {
389✔
30
   hash.update(io.send(*this));
778✔
31
}
389✔
32

33
New_Session_Ticket_12::New_Session_Ticket_12(Handshake_IO& io, Handshake_Hash& hash) { hash.update(io.send(*this)); }
×
34

35
New_Session_Ticket_12::New_Session_Ticket_12(const std::vector<uint8_t>& buf) {
601✔
36
   if(buf.size() < 6) {
601✔
37
      throw Decoding_Error("Session ticket message too short to be valid");
1✔
38
   }
39

40
   TLS_Data_Reader reader("SessionTicket", buf);
600✔
41

42
   m_ticket_lifetime_hint = std::chrono::seconds(reader.get_uint32_t());
600✔
43
   m_ticket = Session_Ticket(reader.get_range<uint8_t>(2, 0, 65535));
600✔
44
   reader.assert_done();
599✔
45
}
601✔
46

47
namespace {
48

49
template <typename lifetime_t = uint32_t>
50
void store_lifetime(std::span<uint8_t> sink, std::chrono::seconds lifetime) {
267✔
51
   BOTAN_ARG_CHECK(lifetime.count() >= 0 && lifetime.count() <= std::numeric_limits<lifetime_t>::max(),
267✔
52
                   "Ticket lifetime is out of range");
53
   store_be(static_cast<lifetime_t>(lifetime.count()), sink.data());
267✔
54
}
267✔
55

56
}  // namespace
57

58
std::vector<uint8_t> New_Session_Ticket_12::serialize() const {
389✔
59
   std::vector<uint8_t> buf(4);
389✔
60
   store_be(static_cast<uint32_t>(m_ticket_lifetime_hint.count()), buf.data());
389✔
61
   append_tls_length_value(buf, m_ticket.get(), 2);
389✔
62
   return buf;
389✔
63
}
×
64

65
#if defined(BOTAN_HAS_TLS_13)
66

67
New_Session_Ticket_13::New_Session_Ticket_13(Ticket_Nonce nonce,
266✔
68
                                             const Session& session,
69
                                             const Session_Handle& handle,
70
                                             Callbacks& callbacks) :
266✔
71
      m_ticket_lifetime_hint(session.lifetime_hint()),
266✔
72
      m_ticket_age_add(session.session_age_add()),
266✔
73
      m_ticket_nonce(std::move(nonce)),
266✔
74
      m_handle(handle.opaque_handle()) {
266✔
75
   callbacks.tls_modify_extensions(m_extensions, Connection_Side::Server, type());
266✔
76
}
266✔
77

78
New_Session_Ticket_13::New_Session_Ticket_13(const std::vector<uint8_t>& buf, Connection_Side from) {
549✔
79
   TLS_Data_Reader reader("New_Session_Ticket_13", buf);
549✔
80

81
   m_ticket_lifetime_hint = std::chrono::seconds(reader.get_uint32_t());
549✔
82

83
   // RFC 8446 4.6.1
84
   //    Servers MUST NOT use any value [of ticket_lifetime] greater than 604800
85
   //    seconds (7 days).
86
   if(m_ticket_lifetime_hint > std::chrono::days(7)) {
1,098✔
87
      throw TLS_Exception(Alert::IllegalParameter, "Received a session ticket with lifetime longer than one week.");
×
88
   }
89

90
   m_ticket_age_add = reader.get_uint32_t();
549✔
91
   m_ticket_nonce = Ticket_Nonce(reader.get_tls_length_value(1));
1,098✔
92
   m_handle = Opaque_Session_Handle(reader.get_tls_length_value(2));
1,098✔
93

94
   m_extensions.deserialize(reader, from, type());
549✔
95

96
   // RFC 8446 4.6.1
97
   //    The sole extension currently defined for NewSessionTicket is
98
   //    "early_data", indicating that the ticket may be used to send 0-RTT
99
   //    data [...]. Clients MUST ignore unrecognized extensions.
100
   if(m_extensions.contains_implemented_extensions_other_than({Extension_Code::EarlyData})) {
1,098✔
101
      throw TLS_Exception(Alert::IllegalParameter, "NewSessionTicket message contained unexpected extension");
×
102
   }
103

104
   reader.assert_done();
549✔
105
}
549✔
106

107
std::optional<uint32_t> New_Session_Ticket_13::early_data_byte_limit() const {
549✔
108
   if(!m_extensions.has<EarlyDataIndication>()) {
549✔
109
      return std::nullopt;
548✔
110
   }
111

112
   const EarlyDataIndication* ext = m_extensions.get<EarlyDataIndication>();
1✔
113
   BOTAN_ASSERT_NOMSG(ext->max_early_data_size().has_value());
1✔
114
   return ext->max_early_data_size().value();
1✔
115
}
116

117
std::vector<uint8_t> New_Session_Ticket_13::serialize() const {
267✔
118
   std::vector<uint8_t> result(8);
267✔
119

120
   store_lifetime(std::span(result.data(), 4), m_ticket_lifetime_hint);
267✔
121
   store_be(m_ticket_age_add, result.data() + 4);
267✔
122
   append_tls_length_value(result, m_ticket_nonce.get(), 1);
267✔
123
   append_tls_length_value(result, m_handle.get(), 2);
267✔
124

125
   // TODO: re-evaluate this construction when reworking message marshalling
126
   if(m_extensions.size() == 0) {
267✔
127
      result.push_back(0x00);
265✔
128
      result.push_back(0x00);
265✔
129
   } else {
130
      result += m_extensions.serialize(Connection_Side::Server);
4✔
131
   }
132

133
   return result;
267✔
134
}
×
135

136
#endif
137

138
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc