1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
#pragma once
#include <string>
#include <cstring> // for std::strlen, and std::memset
#include <ostream>
#include <openvpn/common/strneq.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/buffer/bufstr.hpp>
namespace openvpn {
/**
@brief A string-like type that clears the buffer contents on delete
*/
class SafeString
{
static constexpr size_t INITIAL_CAPACITY = 32;
static constexpr unsigned int BUF_FLAGS = BufAllocFlags::DESTRUCT_ZERO | BufAllocFlags::GROW;
public:
SafeString()
{
}
SafeString(const char *str, const size_t size)
: data(size + 1, BUF_FLAGS)
{
if (size == std::numeric_limits<size_t>::max())
OPENVPN_BUFFER_THROW(buffer_overflow)
data.write((unsigned char *)str, size);
trail();
}
SafeString(const char *str)
: SafeString(str, std::strlen(str))
{
}
SafeString(const std::string &str)
: SafeString(str.c_str(), str.length())
{
}
const char *c_str() const
{
if (data.defined())
return (const char *)data.c_data();
else
return "";
}
// Note: unsafe because of conversion to std::string
std::string to_string() const
{
return buf_to_string(data);
}
size_t length() const
{
return data.size();
}
bool empty() const
{
return !length();
}
char &operator[](size_t pos)
{
return *reinterpret_cast<char *>(data.index(pos));
}
const char &operator[](size_t pos) const
{
return *reinterpret_cast<const char *>(data.c_index(pos));
}
bool operator==(const char *str) const
{
return !operator!=(str);
}
bool operator!=(const char *str) const
{
return crypto::str_neq(str, c_str());
}
bool operator==(const std::string &str) const
{
return !operator!=(str);
}
bool operator!=(const std::string &str) const
{
return crypto::str_neq(str.c_str(), c_str());
}
SafeString &operator+=(char c)
{
alloc();
data.push_back((unsigned char)c);
trail();
return *this;
}
SafeString &operator+=(const char *s)
{
return append(s);
}
SafeString &operator+=(const SafeString &str)
{
return append(str);
}
SafeString &append(const char *s)
{
alloc();
data.write((unsigned char *)s, std::strlen(s));
trail();
return *this;
}
SafeString &append(const SafeString &str)
{
alloc();
data.append(str.data);
trail();
return *this;
}
SafeString &append(const SafeString &str, size_t subpos, size_t sublen)
{
alloc();
data.append(str.data.range(subpos, sublen));
trail();
return *this;
}
void reserve(const size_t n)
{
if (data.allocated())
data.reserve(n + 1);
else
data.init(n + 1, BUF_FLAGS);
}
void wipe()
{
data.clear();
}
private:
void alloc()
{
if (!data.allocated())
data.init(INITIAL_CAPACITY, BUF_FLAGS);
}
void trail()
{
data.set_trailer(0);
}
BufferAllocated data;
};
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits> &operator<<(std::basic_ostream<Elem, Traits> &os,
const SafeString &ss)
{
os << ss.c_str();
return os;
}
} // namespace openvpn
|