Directory: | ./ |
---|---|
File: | pdserv/src/TLS.cpp |
Date: | 2025-08-17 04:10:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 11 | 78 | 14.1% |
Branches: | 2 | 78 | 2.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Copyright 2016 Richard Hacker (lerichi at gmx dot net) | ||
4 | * | ||
5 | * This file is part of the pdserv library. | ||
6 | * | ||
7 | * The pdserv library is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser General Public License as published | ||
9 | * by the Free Software Foundation, either version 3 of the License, or (at | ||
10 | * your option) any later version. | ||
11 | * | ||
12 | * The pdserv library is distributed in the hope that it will be useful, but | ||
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
15 | * License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public License | ||
18 | * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "TLS.h" | ||
23 | |||
24 | #include "Debug.h" | ||
25 | |||
26 | #include <cstring> | ||
27 | #include <algorithm> | ||
28 | |||
29 | ///////////////////////////////////////////////////////////////////////////// | ||
30 | ✗ | static unsigned char hexchar(char c) | |
31 | { | ||
32 | ✗ | if (c >= 'A' and c <= 'F') | |
33 | ✗ | return c - ('A' - 10); | |
34 | |||
35 | ✗ | if (c >= 'a' and c <= 'f') | |
36 | ✗ | return c - ('a' - 10); | |
37 | |||
38 | ✗ | if (c >= '0' and c <= '9') | |
39 | ✗ | return c - '0'; | |
40 | |||
41 | ✗ | return 0; | |
42 | } | ||
43 | |||
44 | ///////////////////////////////////////////////////////////////////////////// | ||
45 | ///////////////////////////////////////////////////////////////////////////// | ||
46 | ✗ | datum_string::datum_string(size_t len) | |
47 | { | ||
48 | ✗ | reserve(len); | |
49 | } | ||
50 | |||
51 | ///////////////////////////////////////////////////////////////////////////// | ||
52 | ✗ | datum_string::datum_string(const gnutls_datum_t& value) | |
53 | { | ||
54 | ✗ | append(value.data, value.size); | |
55 | } | ||
56 | |||
57 | ///////////////////////////////////////////////////////////////////////////// | ||
58 | ✗ | datum_string::datum_string(const pointer data, size_t len) | |
59 | { | ||
60 | ✗ | append(data, len); | |
61 | } | ||
62 | |||
63 | ///////////////////////////////////////////////////////////////////////////// | ||
64 | ✗ | datum_string::datum_string(const char* hexdata, size_t len) | |
65 | { | ||
66 | ✗ | len = std::min(hexdata ? ::strlen(hexdata) : 0U, len) / 2; | |
67 | |||
68 | ✗ | reserve(len); | |
69 | |||
70 | ✗ | while (len--) { | |
71 | ✗ | append(1, (hexchar(hexdata[0]) << 4) + hexchar(hexdata[1])); | |
72 | ✗ | hexdata += 2; | |
73 | } | ||
74 | } | ||
75 | |||
76 | ///////////////////////////////////////////////////////////////////////////// | ||
77 | ✗ | datum_string::operator std::string() const | |
78 | { | ||
79 | static const char* table = "0123456789abcdef"; | ||
80 | ✗ | std::string s; | |
81 | ✗ | for (const_iterator it = begin(); it != end(); ++it) { | |
82 | ✗ | s.append(1, table[((*it) >> 4) & 0x0F]); | |
83 | ✗ | s.append(1, table[ (*it) & 0x0F]); | |
84 | } | ||
85 | ✗ | return s; | |
86 | } | ||
87 | |||
88 | ///////////////////////////////////////////////////////////////////////////// | ||
89 | ✗ | datum_string::operator gnutls_datum_t() const | |
90 | { | ||
91 | ✗ | gnutls_datum_t res; | |
92 | ✗ | res.size = size(); | |
93 | ✗ | res.data = reinterpret_cast<pointer>(res.size); | |
94 | ✗ | std::copy(begin(), end(), res.data); | |
95 | ✗ | return res; | |
96 | } | ||
97 | |||
98 | ///////////////////////////////////////////////////////////////////////////// | ||
99 | ///////////////////////////////////////////////////////////////////////////// | ||
100 | 157 | TlsSessionDB::TlsSessionDB(pthread::Mutex* mutex, size_t max): | |
101 | 157 | mutex(mutex), maxSize(max) | |
102 | { | ||
103 | 157 | } | |
104 | |||
105 | ///////////////////////////////////////////////////////////////////////////// | ||
106 | ✗ | int TlsSessionDB::store( | |
107 | const gnutls_datum_t& key, const gnutls_datum_t& value) | ||
108 | { | ||
109 | ✗ | if (key.size > TLS_DB_MAX_ENTRY_SIZE | |
110 | ✗ | or value.size > TLS_DB_MAX_ENTRY_SIZE) | |
111 | ✗ | return -1; | |
112 | |||
113 | ✗ | pthread::MutexLock lock(*mutex); | |
114 | |||
115 | ✗ | map_type::iterator it = | |
116 | ✗ | map.insert(std::make_pair(key, value)).first; | |
117 | |||
118 | ✗ | list.remove(it); | |
119 | |||
120 | ✗ | while (list.size() > maxSize) { | |
121 | ✗ | map.erase(*list.begin()); | |
122 | ✗ | list.pop_front(); | |
123 | } | ||
124 | ✗ | list.push_back(it); | |
125 | |||
126 | ✗ | return 0; | |
127 | } | ||
128 | |||
129 | ///////////////////////////////////////////////////////////////////////////// | ||
130 | ✗ | int TlsSessionDB::erase(const gnutls_datum_t& key) | |
131 | { | ||
132 | ✗ | pthread::MutexLock lock(*mutex); | |
133 | |||
134 | ✗ | map_type::iterator it = map.find(key); | |
135 | |||
136 | ✗ | if (it == map.end()) | |
137 | ✗ | return -1; | |
138 | |||
139 | ✗ | list.remove(it); | |
140 | ✗ | map.erase(it); | |
141 | ✗ | return 0; | |
142 | } | ||
143 | |||
144 | ///////////////////////////////////////////////////////////////////////////// | ||
145 | ✗ | gnutls_datum_t TlsSessionDB::retrieve(const gnutls_datum_t& key) | |
146 | { | ||
147 | ✗ | pthread::MutexLock lock(*mutex); | |
148 | |||
149 | ✗ | map_type::const_iterator it = map.find(key); | |
150 | static const gnutls_datum_t res = { NULL, 0 }; | ||
151 | ✗ | return it == map.end() | |
152 | ? res | ||
153 | ✗ | : static_cast<gnutls_datum_t>(it->second); | |
154 | } | ||
155 | |||
156 | 66 | void TlsDeleter::operator()(gnutls_session_t s) | |
157 | { | ||
158 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | if (s) |
159 | 66 | gnutls_deinit(s); | |
160 | 66 | } | |
161 | ✗ | void TlsDeleter::operator()(gnutls_dh_params_t d) | |
162 | { | ||
163 | ✗ | if (d) | |
164 | ✗ | gnutls_dh_params_deinit(d); | |
165 | } | ||
166 | ✗ | void TlsDeleter::operator()(gnutls_priority_t p) | |
167 | { | ||
168 | ✗ | if (p) | |
169 | ✗ | gnutls_priority_deinit(p); | |
170 | } | ||
171 | 27 | void TlsDeleter::operator()(gnutls_x509_crt_t c) | |
172 | { | ||
173 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (c) |
174 | 27 | gnutls_x509_crt_deinit(c); | |
175 | 27 | } | |
176 | ✗ | void TlsDeleter::operator()(gnutls_certificate_credentials_t cc) | |
177 | { | ||
178 | ✗ | if (cc) | |
179 | ✗ | gnutls_certificate_free_credentials(cc); | |
180 | } | ||
181 |