OpenVPN 3 Core Library
Loading...
Searching...
No Matches
psid.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// A 64-bit session ID, used by ProtoContext.
13
14#ifndef OPENVPN_SSL_PSID_H
15#define OPENVPN_SSL_PSID_H
16
17#include <string>
18#include <cstring>
19
23
24namespace openvpn {
25
27{
28 public:
29 enum
30 {
31 SIZE = 8
32 };
33
34 constexpr ProtoSessionID()
35 : defined_(false), id_{}
36 {
37 }
38
39 void reset()
40 {
41 defined_ = false;
42 std::memset(id_, 0, SIZE);
43 }
44
45 template <typename BufType> // so it can take a Buffer or a ConstBuffer
46 explicit ProtoSessionID(BufType &buf)
47 {
48 buf.read(id_, SIZE);
49 defined_ = true;
50 }
51
53 {
54 rng.rand_bytes(id_, SIZE);
55 defined_ = true;
56 }
57
58 template <typename BufType> // so it can take a Buffer or a ConstBuffer
59 void read(BufType &buf)
60 {
61 buf.read(id_, SIZE);
62 defined_ = true;
63 }
64
65 void write(Buffer &buf) const
66 {
67 buf.write(id_, SIZE);
68 }
69
70 void prepend(Buffer &buf) const
71 {
72 buf.prepend(id_, SIZE);
73 }
74
75 // returned buffer is only valid for *this lifetime
76 const Buffer get_buf() const
77 {
78 if (defined_)
79 {
80 return PsidBuf(const_cast<Buffer::type>(id_));
81 }
82 return Buffer();
83 }
84
85 constexpr bool defined() const
86 {
87 return defined_;
88 }
89
90 bool match(const ProtoSessionID &other) const
91 {
92 return defined_ && other.defined_ && !crypto::memneq(id_, other.id_, SIZE);
93 }
94
95 std::string str() const
96 {
97 return render_hex(id_, SIZE);
98 }
99
100 private:
101 // access protected ctor to use Buffer w/o memcpy
103 {
104 // T* data, const size_t offset, const size_t size, const size_t capacity
106 : Buffer(id, 0, SIZE, SIZE)
107 {
108 }
109 };
110
112 unsigned char id_[SIZE];
113};
114} // namespace openvpn
115
116#endif // OPENVPN_SSL_PSID_H
void prepend(const T *data, const size_t size)
Prepend data to the buffer.
Definition buffer.hpp:1575
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1563
bool match(const ProtoSessionID &other) const
Definition psid.hpp:90
std::string str() const
Definition psid.hpp:95
const Buffer get_buf() const
Definition psid.hpp:76
void write(Buffer &buf) const
Definition psid.hpp:65
constexpr bool defined() const
Definition psid.hpp:85
void read(BufType &buf)
Definition psid.hpp:59
void randomize(StrongRandomAPI &rng)
Definition psid.hpp:52
unsigned char id_[SIZE]
Definition psid.hpp:112
void prepend(Buffer &buf) const
Definition psid.hpp:70
constexpr ProtoSessionID()
Definition psid.hpp:34
ProtoSessionID(BufType &buf)
Definition psid.hpp:46
virtual void rand_bytes(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes.
Abstract base class for cryptographically strong random number generators.
Definition randapi.hpp:228
bool memneq(const void *a, const void *b, size_t size)
Definition memneq.hpp:79
BufferType< unsigned char > Buffer
Definition buffer.hpp:1895
std::string render_hex(const unsigned char *data, size_t size, const bool caps=false)
Definition hexstr.hpp:135
PsidBuf(typename Buffer::type id)
Definition psid.hpp:105