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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#include "Globals.h"
#include "mbedTLS++/SslConfig.h"
#include "mbedTLS++/CryptoKey.h"
#include "mbedTLS++/EntropyContext.h"
#include "mbedTLS++/RootCA.h"
// This allows us to debug SSL and certificate problems, but produce way too much output,
// so it's disabled until someone needs it
// #define ENABLE_SSL_DEBUG_MSG
#if !defined(NDEBUG) && defined(ENABLE_SSL_DEBUG_MSG)
#include "mbedtls/debug.h"
namespace
{
void SSLDebugMessage(void * a_UserParam, int a_Level, const char * a_Filename, int a_LineNo, const char * a_Text)
{
if (a_Level > 3)
{
// Don't want the trace messages
return;
}
// Remove the terminating LF:
size_t len = strlen(a_Text) - 1;
while ((len > 0) && (a_Text[len] <= 32))
{
len--;
}
AString Text(a_Text, len + 1);
LOGD("SSL (%d): %s", a_Level, Text.c_str());
}
int SSLVerifyCert(void * a_This, mbedtls_x509_crt * a_Crt, int a_Depth, uint32_t * a_Flags)
{
char buf[1024];
UNUSED(a_This);
LOG("Verify requested for (Depth %d):", a_Depth);
mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", a_Crt);
LOG("%s", buf);
uint32_t Flags = *a_Flags;
if ((Flags & MBEDTLS_X509_BADCERT_EXPIRED) != 0)
{
LOG(" ! server certificate has expired");
}
if ((Flags & MBEDTLS_X509_BADCERT_REVOKED) != 0)
{
LOG(" ! server certificate has been revoked");
}
if ((Flags & MBEDTLS_X509_BADCERT_CN_MISMATCH) != 0)
{
LOG(" ! CN mismatch");
}
if ((Flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) != 0)
{
LOG(" ! self-signed or not signed by a trusted CA");
}
if ((Flags & MBEDTLS_X509_BADCRL_NOT_TRUSTED) != 0)
{
LOG(" ! CRL not trusted");
}
if ((Flags & MBEDTLS_X509_BADCRL_EXPIRED) != 0)
{
LOG(" ! CRL expired");
}
if ((Flags & MBEDTLS_X509_BADCERT_OTHER) != 0)
{
LOG(" ! other (unknown) flag");
}
if (Flags == 0)
{
LOG(" This certificate has no flags");
}
return 0;
}
}
#endif // !defined(NDEBUG) && defined(ENABLE_SSL_DEBUG_MSG)
////////////////////////////////////////////////////////////////////////////////
// cSslConfig:
cSslConfig::cSslConfig()
{
mbedtls_ssl_config_init(&m_Config);
}
cSslConfig::~cSslConfig()
{
mbedtls_ssl_config_free(&m_Config);
}
int cSslConfig::InitDefaults(const bool a_IsClient)
{
return mbedtls_ssl_config_defaults(
&m_Config,
a_IsClient ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT
);
}
void cSslConfig::SetAuthMode(const eSslAuthMode a_AuthMode)
{
const int Mode = [=]()
{
switch (a_AuthMode)
{
case eSslAuthMode::None: return MBEDTLS_SSL_VERIFY_NONE;
case eSslAuthMode::Optional: return MBEDTLS_SSL_VERIFY_OPTIONAL;
case eSslAuthMode::Required: return MBEDTLS_SSL_VERIFY_REQUIRED;
case eSslAuthMode::Unset: return MBEDTLS_SSL_VERIFY_UNSET;
}
UNREACHABLE("Unsupported SSL auth mode");
}();
mbedtls_ssl_conf_authmode(&m_Config, Mode);
}
void cSslConfig::SetRng(cCtrDrbgContextPtr a_CtrDrbg)
{
ASSERT(a_CtrDrbg != nullptr);
m_CtrDrbg = std::move(a_CtrDrbg);
mbedtls_ssl_conf_rng(&m_Config, mbedtls_ctr_drbg_random, &m_CtrDrbg->m_CtrDrbg);
}
void cSslConfig::SetDebugCallback(cDebugCallback a_CallbackFun, void * a_CallbackData)
{
mbedtls_ssl_conf_dbg(&m_Config, a_CallbackFun, a_CallbackData);
}
void cSslConfig::SetOwnCert(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnCertPrivKey)
{
ASSERT(a_OwnCert != nullptr);
ASSERT(a_OwnCertPrivKey != nullptr);
// Make sure we have the cert stored for later, mbedTLS only uses the cert later on
m_OwnCert = std::move(a_OwnCert);
m_OwnCertPrivKey = std::move(a_OwnCertPrivKey);
// Set into the config:
mbedtls_ssl_conf_own_cert(&m_Config, m_OwnCert->GetInternal(), m_OwnCertPrivKey->GetInternal());
}
void cSslConfig::SetVerifyCallback(cVerifyCallback a_CallbackFun, void * a_CallbackData)
{
mbedtls_ssl_conf_verify(&m_Config, a_CallbackFun, a_CallbackData);
}
void cSslConfig::SetCipherSuites(std::vector<int> a_CipherSuites)
{
m_CipherSuites = std::move(a_CipherSuites);
m_CipherSuites.push_back(0); // Must be null terminated
mbedtls_ssl_conf_ciphersuites(&m_Config, m_CipherSuites.data());
}
void cSslConfig::SetCACerts(cX509CertPtr a_CACert)
{
m_CACerts = std::move(a_CACert);
mbedtls_ssl_conf_ca_chain(&m_Config, m_CACerts->GetInternal(), nullptr);
}
std::shared_ptr<cSslConfig> cSslConfig::MakeDefaultConfig(bool a_IsClient)
{
auto Ret = std::make_shared<cSslConfig>();
Ret->InitDefaults(a_IsClient);
{
auto CtrDrbg = std::make_shared<cCtrDrbgContext>();
CtrDrbg->Initialize("Cuberite", 8);
Ret->SetRng(std::move(CtrDrbg));
}
Ret->SetAuthMode(eSslAuthMode::Required);
Ret->SetCACerts(GetCACerts());
#ifndef NDEBUG
#ifdef ENABLE_SSL_DEBUG_MSG
Ret->SetDebugCallback(&SSLDebugMessage, nullptr);
Ret->SetVerifyCallback(SSLVerifyCert, nullptr);
mbedtls_debug_set_threshold(2);
#endif
/*
// Set ciphersuite to the easiest one to decode, so that the connection can be wireshark-decoded:
Ret->SetCipherSuites(
{
MBEDTLS_TLS_RSA_WITH_RC4_128_MD5,
MBEDTLS_TLS_RSA_WITH_RC4_128_SHA,
MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA
}
);
*/
#endif
return Ret;
}
std::shared_ptr<const cSslConfig> cSslConfig::GetDefaultClientConfig()
{
static const std::shared_ptr<const cSslConfig> ClientConfig = MakeDefaultConfig(true);
return ClientConfig;
}
std::shared_ptr<const cSslConfig> cSslConfig::GetDefaultServerConfig()
{
static const std::shared_ptr<const cSslConfig> ServerConfig = MakeDefaultConfig(false);
return ServerConfig;
}
|