Botan 3.9.0
Crypto and TLS for C&
Botan::SecureQueue Class Referencefinal

#include <secqueue.h>

Inheritance diagram for Botan::SecureQueue:
Botan::Fanout_Filter Botan::DataSource Botan::Filter

Public Member Functions

bool attachable () override
bool check_available (size_t n) override
size_t discard_next (size_t N)
bool empty () const
virtual void end_msg ()
bool end_of_data () const override
size_t get_bytes_read () const override
virtual std::string id () const
std::string name () const override
SecureQueueoperator= (const SecureQueue &other)
SecureQueueoperator= (SecureQueue &&other)=delete
size_t peek (uint8_t output[], size_t length, size_t offset=0) const override
size_t peek_byte (uint8_t &out) const
size_t read (uint8_t output[], size_t length) override
std::optional< uint8_t > read_byte ()
size_t read_byte (uint8_t &out)
 SecureQueue ()
 SecureQueue (const SecureQueue &other)
 SecureQueue (SecureQueue &&other)=delete
size_t size () const
virtual void start_msg ()
void write (const uint8_t input[], size_t length) override
 ~SecureQueue () override

Protected Member Functions

void attach (Filter *f)
void incr_owns ()
virtual void send (const uint8_t in[], size_t length)
void send (std::span< const uint8_t > in)
void send (std::span< const uint8_t > in, size_t length)
void send (uint8_t in)
void set_next (Filter *f[], size_t n)
void set_port (size_t n)

Detailed Description

A queue that knows how to zeroize itself

Definition at line 20 of file secqueue.h.

Constructor & Destructor Documentation

◆ SecureQueue() [1/3]

Botan::SecureQueue::SecureQueue ( SecureQueue && other)
delete

◆ SecureQueue() [2/3]

Botan::SecureQueue::SecureQueue ( )

SecureQueue default constructor (creates empty queue)

Definition at line 69 of file secqueue.cpp.

69 : m_bytes_read(0) {
70 set_next(nullptr, 0);
71 m_head = m_tail = new SecureQueueNode; // NOLINT(*-owning-memory)
72}
void set_next(Filter *f[], size_t n)
Definition filter.h:156

References Botan::Fanout_Filter::set_next().

◆ SecureQueue() [3/3]

Botan::SecureQueue::SecureQueue ( const SecureQueue & other)

SecureQueue copy constructor

Parameters
otherthe queue to copy

Definition at line 77 of file secqueue.cpp.

77 : Fanout_Filter(), m_bytes_read(0) {
78 set_next(nullptr, 0);
79
80 m_head = m_tail = new SecureQueueNode; // NOLINT(*-owning-memory)
81 SecureQueueNode* temp = input.m_head;
82 while(temp != nullptr) {
83 write(&temp->m_buffer[temp->m_start], temp->m_end - temp->m_start);
84 temp = temp->m_next;
85 }
86}
friend class Fanout_Filter
Definition filter.h:107
void write(const uint8_t input[], size_t length) override
Definition secqueue.cpp:123

References SecureQueue(), Botan::Fanout_Filter::set_next(), and write().

◆ ~SecureQueue()

Botan::SecureQueue::~SecureQueue ( )
inlineoverride

Definition at line 64 of file secqueue.h.

64{ destroy(); }

Member Function Documentation

◆ attach()

void Botan::Fanout_Filter::attach ( Filter * f)
inlineprotectedinherited

Definition at line 158 of file filter.h.

158{ Filter::attach(f); }

References Botan::Filter::Filter().

Referenced by Botan::Chain::Chain(), and Botan::Chain::Chain().

◆ attachable()

bool Botan::SecureQueue::attachable ( )
inlineoverridevirtual

Check whether this filter is an attachable filter.

Returns
true if this filter is attachable, false otherwise

Reimplemented from Botan::Filter.

Definition at line 42 of file secqueue.h.

42{ return false; }

◆ check_available()

bool Botan::SecureQueue::check_available ( size_t n)
inlineoverridevirtual

Implements Botan::DataSource.

Definition at line 35 of file secqueue.h.

35{ return n <= size(); }
size_t size() const
Definition secqueue.cpp:195

References size().

◆ discard_next()

size_t Botan::DataSource::discard_next ( size_t N)
inherited

Discard the next N bytes of the data

Parameters
Nthe number of bytes to discard
Returns
number of bytes actually discarded

Definition at line 53 of file data_src.cpp.

53 {
54 uint8_t buf[64] = {0};
55 size_t discarded = 0;
56
57 while(n > 0) {
58 const size_t got = this->read(buf, std::min(n, sizeof(buf)));
59 discarded += got;
60 n -= got;
61
62 if(got == 0) {
63 break;
64 }
65 }
66
67 return discarded;
68}
virtual size_t read(uint8_t out[], size_t length)=0

References read().

◆ empty()

bool Botan::SecureQueue::empty ( ) const

Definition at line 213 of file secqueue.cpp.

213 {
214 return (size() == 0);
215}

References size().

◆ end_msg()

virtual void Botan::Filter::end_msg ( )
inlinevirtualinherited

Notify that the current message is finished; flush buffers and do end-of-message processing (if any).

Reimplemented in Botan::Base64_Decoder, Botan::Base64_Encoder, Botan::DataSink_Stream, Botan::Hex_Decoder, and Botan::Hex_Encoder.

Definition at line 47 of file filter.h.

47 { /* default empty */
48 }

◆ end_of_data()

bool Botan::SecureQueue::end_of_data ( ) const
overridevirtual

Test whether the source still has data that can be read.

Returns
true if there is no more data to read, false otherwise

Implements Botan::DataSource.

Definition at line 209 of file secqueue.cpp.

209 {
210 return (size() == 0);
211}

References size().

◆ get_bytes_read()

size_t Botan::SecureQueue::get_bytes_read ( ) const
overridevirtual

Return how many bytes have been read so far.

Implements Botan::DataSource.

Definition at line 188 of file secqueue.cpp.

188 {
189 return m_bytes_read;
190}

Referenced by Botan::Output_Buffers::get_bytes_read(), and operator=().

◆ id()

virtual std::string Botan::DataSource::id ( ) const
inlinevirtualinherited

return the id of this data source

Returns
std::string representing the id of this data source

Reimplemented in Botan::DataSource_Stream.

Definition at line 63 of file data_src.h.

63{ return ""; }

◆ incr_owns()

void Botan::Fanout_Filter::incr_owns ( )
inlineprotectedinherited

Increment the number of filters past us that we own

Definition at line 152 of file filter.h.

152{ ++m_filter_owns; }

Referenced by Botan::Chain::Chain(), and Botan::Chain::Chain().

◆ name()

std::string Botan::SecureQueue::name ( ) const
inlineoverridevirtual
Returns
descriptive name for this filter

Implements Botan::Filter.

Definition at line 23 of file secqueue.h.

23{ return "Queue"; }

◆ operator=() [1/2]

SecureQueue & Botan::SecureQueue::operator= ( const SecureQueue & other)

SecureQueue assignment

Parameters
otherthe queue to copy

Definition at line 104 of file secqueue.cpp.

104 {
105 if(this == &input) {
106 return *this;
107 }
108
109 destroy();
110 m_bytes_read = input.get_bytes_read();
111 m_head = m_tail = new SecureQueueNode; // NOLINT(*-owning-memory)
112 SecureQueueNode* temp = input.m_head;
113 while(temp != nullptr) {
114 write(&temp->m_buffer[temp->m_start], temp->m_end - temp->m_start);
115 temp = temp->m_next;
116 }
117 return (*this);
118}

References get_bytes_read(), SecureQueue(), and write().

◆ operator=() [2/2]

SecureQueue & Botan::SecureQueue::operator= ( SecureQueue && other)
delete

References SecureQueue().

◆ peek()

size_t Botan::SecureQueue::peek ( uint8_t out[],
size_t length,
size_t peek_offset = 0 ) const
overridevirtual

Read from the source but do not modify the internal offset. Consecutive calls to peek() will return portions of the source starting at the same position.

Parameters
outthe byte array to write the output to
lengththe length of the byte array out
peek_offsetthe offset into the stream to read at
Returns
length in bytes that was actually read and put into out

Implements Botan::DataSource.

Definition at line 161 of file secqueue.cpp.

161 {
162 SecureQueueNode* current = m_head;
163
164 while(offset > 0 && current != nullptr) {
165 if(offset >= current->size()) {
166 offset -= current->size();
167 current = current->m_next;
168 } else {
169 break;
170 }
171 }
172
173 size_t got = 0;
174 while(length > 0 && current != nullptr) {
175 const size_t n = current->peek(output, length, offset);
176 offset = 0;
177 output += n;
178 got += n;
179 length -= n;
180 current = current->m_next;
181 }
182 return got;
183}

Referenced by Botan::Output_Buffers::peek().

◆ peek_byte()

size_t Botan::DataSource::peek_byte ( uint8_t & out) const
inherited

Peek at one byte.

Parameters
outan output byte
Returns
length in bytes that was actually read and put into out

Definition at line 46 of file data_src.cpp.

46 {
47 return peek(&out, 1, 0);
48}
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0

References peek().

Referenced by Botan::ASN1::maybe_BER().

◆ read()

size_t Botan::SecureQueue::read ( uint8_t out[],
size_t length )
overridevirtual

Read from the source. Moves the internal offset so that every call to read will return a new portion of the source.

Parameters
outthe byte array to write the result to
lengththe length of the byte array out
Returns
length in bytes that was actually read and put into out

Implements Botan::DataSource.

Definition at line 141 of file secqueue.cpp.

141 {
142 size_t got = 0;
143 while(length > 0 && m_head != nullptr) {
144 const size_t n = m_head->read(output, length);
145 output += n;
146 got += n;
147 length -= n;
148 if(m_head->size() == 0) {
149 SecureQueueNode* holder = m_head->m_next;
150 delete m_head; // NOLINT(*-owning-memory)
151 m_head = holder;
152 }
153 }
154 m_bytes_read += got;
155 return got;
156}

Referenced by Botan::Output_Buffers::read().

◆ read_byte() [1/2]

std::optional< uint8_t > Botan::DataSource::read_byte ( )
inherited

Read one byte.

Returns nullopt if no further bytes are available

Definition at line 34 of file data_src.cpp.

34 {
35 uint8_t b = 0;
36 if(this->read(&b, 1) == 1) {
37 return b;
38 } else {
39 return {};
40 }
41}

References read().

◆ read_byte() [2/2]

size_t Botan::DataSource::read_byte ( uint8_t & out)
inherited

Read one byte.

Parameters
outthe byte to read to
Returns
length in bytes that was actually read and put into out

Definition at line 27 of file data_src.cpp.

27 {
28 return read(&out, 1);
29}

References read().

Referenced by Botan::PEM_Code::decode(), and Botan::ASN1::maybe_BER().

◆ send() [1/4]

void Botan::Filter::send ( const uint8_t in[],
size_t length )
protectedvirtualinherited
Parameters
insome input for the filter
lengththe length of in

Definition at line 30 of file filter.cpp.

30 {
31 if(length == 0) {
32 return;
33 }
34
35 bool nothing_attached = true;
36 for(size_t j = 0; j != total_ports(); ++j) {
37 if(m_next[j] != nullptr) {
38 if(!m_write_queue.empty()) {
39 m_next[j]->write(m_write_queue.data(), m_write_queue.size());
40 }
41 m_next[j]->write(input, length);
42 nothing_attached = false;
43 }
44 }
45
46 if(nothing_attached) {
47 m_write_queue += std::make_pair(input, length);
48 } else {
49 m_write_queue.clear();
50 }
51}

Referenced by Botan::Base64_Decoder::end_msg(), Botan::Base64_Encoder::end_msg(), Botan::Hex_Decoder::end_msg(), Botan::Hex_Encoder::end_msg(), operator=(), send(), Botan::Base64_Decoder::write(), Botan::Chain::write(), Botan::Fork::write(), and Botan::Hex_Decoder::write().

◆ send() [2/4]

void Botan::Filter::send ( std::span< const uint8_t > in)
inlineprotectedinherited
Parameters
insome input for the filter

Definition at line 78 of file filter.h.

78{ send(in.data(), in.size()); }
virtual void send(const uint8_t in[], size_t length)
Definition filter.cpp:30

References send().

Referenced by send().

◆ send() [3/4]

void Botan::Filter::send ( std::span< const uint8_t > in,
size_t length )
protectedinherited
Parameters
insome input for the filter
lengththe number of bytes of in to send

This previously took a std::vector, for which the length field (allowing using just a prefix of the vector) somewhat made sense. It makes less sense now that we are using a span here; you can just use first to get a prefix.

Definition at line 22 of file filter.cpp.

22 {
23 BOTAN_ASSERT_NOMSG(length <= in.size());
24 send(in.data(), length);
25}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75

References BOTAN_ASSERT_NOMSG, and send().

◆ send() [4/4]

void Botan::Filter::send ( uint8_t in)
inlineprotectedinherited
Parameters
insome input for the filter

Definition at line 73 of file filter.h.

73{ send(&in, 1); }

References send().

Referenced by send().

◆ set_next()

void Botan::Fanout_Filter::set_next ( Filter * f[],
size_t n )
inlineprotectedinherited

Definition at line 156 of file filter.h.

156{ Filter::set_next(f, n); }

References Botan::Filter::Filter().

Referenced by Botan::Fork::Fork(), Botan::Fork::Fork(), Botan::SecureQueue::SecureQueue(), and Botan::SecureQueue::SecureQueue().

◆ set_port()

void Botan::Fanout_Filter::set_port ( size_t n)
inlineprotectedinherited

Definition at line 154 of file filter.h.

154{ Filter::set_port(n); }

Referenced by Botan::Fork::set_port().

◆ size()

size_t Botan::SecureQueue::size ( ) const
Returns
number of bytes available in the queue

Definition at line 195 of file secqueue.cpp.

195 {
196 SecureQueueNode* current = m_head;
197 size_t count = 0;
198
199 while(current != nullptr) {
200 count += current->size();
201 current = current->m_next;
202 }
203 return count;
204}

Referenced by check_available(), empty(), end_of_data(), and Botan::Output_Buffers::remaining().

◆ start_msg()

virtual void Botan::Filter::start_msg ( )
inlinevirtualinherited

Start a new message. Must be closed by end_msg() before another message can be started.

Definition at line 40 of file filter.h.

40 { /* default empty */
41 }

◆ write()

void Botan::SecureQueue::write ( const uint8_t input[],
size_t length )
overridevirtual

Write a portion of a message to this filter.

Parameters
inputthe input as a byte array
lengththe length of the byte array input

Implements Botan::Filter.

Definition at line 123 of file secqueue.cpp.

123 {
124 if(m_head == nullptr) {
125 m_head = m_tail = new SecureQueueNode; // NOLINT(*-owning-memory)
126 }
127 while(length > 0) {
128 const size_t n = m_tail->write(input, length);
129 input += n;
130 length -= n;
131 if(length > 0) {
132 m_tail->m_next = new SecureQueueNode; // NOLINT(*-owning-memory)
133 m_tail = m_tail->m_next;
134 }
135 }
136}

Referenced by operator=(), and SecureQueue().


The documentation for this class was generated from the following files: