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