Skip to content

Commit 433db19

Browse files
committed
TLS final
1 parent f91229b commit 433db19

9 files changed

+79
-25
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ Network error log:
109109
By default the miner will donate 1% of the hashpower (1 minute in 100 minutes) to my pool. If you want to change that, edit **donate-level.h** before you build the binaries.
110110

111111
If you want to donate directly to support further development, here is my wallet
112-
* 4581HhZkQHgZrZjKeCfCJxZff9E3xCgHGF25zABZz7oR71TnbbgiS7sK9jveE6Dx6uMs2LwszDuvQJgRZQotdpHt1fTdDhk
113-
114112

113+
```
114+
4581HhZkQHgZrZjKeCfCJxZff9E3xCgHGF25zABZz7oR71TnbbgiS7sK9jveE6Dx6uMs2LwszDuvQJgRZQotdpHt1fTdDhk
115+
```
115116

116117
#### PGP Key
117118
```

config.txt

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979

8080
/*
8181
* TLS Settings
82+
* If you need real security, make sure tls_secure_algo is enabled (otherwise MITM attack can downgrade encryption
83+
* to trivially breakable stuff like DES and MD5), and verify the server's fingerprint through a trusted channel.
84+
*
8285
* use_tls - This option will make us connect using Transport Layer Security.
8386
* tls_secure_algo - Use only secure algorithms. This will make us quit with an error if we can't negotiate a secure algo.
8487
* tls_fingerprint - Server's SHA256 fingerprint. If this string is non-empty then we will check the server's cert against it.

donate-level.h

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Example of how it works for the default setting of 1.0:
77
* You miner will mine into your usual pool for 99 minutes, then switch to the developer's pool for 1.0 minute.
88
* Switching is instant, and only happens after a successful connection, so you never loose any hashes.
9+
*
10+
* If you plan on changing this setting to 0.0 please consider making a one off donation to my wallet:
11+
* 4581HhZkQHgZrZjKeCfCJxZff9E3xCgHGF25zABZz7oR71TnbbgiS7sK9jveE6Dx6uMs2LwszDuvQJgRZQotdpHt1fTdDhk
12+
*
913
*/
1014

1115
constexpr double fDevDonationLevel = 1.0 / 100.0;

executor.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ void executor::on_switch_pool(size_t pool_id)
313313
// If it fails, it fails, we carry on on the usr pool
314314
// as we never receive further events
315315
printer::inst()->print_msg(L1, "Connecting to dev pool...");
316-
if(!pool->connect("donate.xmr-stak.net:3333", error))
316+
const char* dev_pool_addr = jconf::inst()->GetTlsSetting() ? "donate.xmr-stak.net:6666" : "donate.xmr-stak.net:3333";
317+
if(!pool->connect(dev_pool_addr, error))
317318
printer::inst()->print_msg(L1, "Error connecting to dev pool. Staying with user pool.");
318319
}
319320
else
@@ -349,8 +350,8 @@ void executor::ex_main()
349350
telem = new telemetry(pvThreads->size());
350351

351352
current_pool_id = usr_pool_id;
352-
usr_pool = new jpsock(usr_pool_id);
353-
dev_pool = new jpsock(dev_pool_id);
353+
usr_pool = new jpsock(usr_pool_id, jconf::inst()->GetTlsSetting());
354+
dev_pool = new jpsock(dev_pool_id, jconf::inst()->GetTlsSetting());
354355

355356
ex_event ev;
356357
std::thread clock_thd(&executor::ex_clock_thd, this);

executor.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class executor
2727
inline void push_event(ex_event&& ev) { oEventQ.push(std::move(ev)); }
2828
void push_timed_event(ex_event&& ev, size_t sec);
2929

30+
constexpr static size_t invalid_pool_id = 0;
31+
constexpr static size_t dev_pool_id = 1;
32+
constexpr static size_t usr_pool_id = 2;
33+
3034
private:
3135
struct timed_event
3236
{
@@ -43,12 +47,6 @@ class executor
4347
// We will divide up this period according to the config setting
4448
constexpr static size_t iDevDonatePeriod = 100 * 60;
4549

46-
constexpr static size_t invalid_pool_id = 0;
47-
constexpr static size_t dev_pool_id = 1;
48-
constexpr static size_t usr_pool_id = 2;
49-
50-
//std::atomic<size_t> iDevDisconnectCountdown;
51-
//std::atomic<size_t> iReconnectCountdown;
5250
std::list<timed_event> lTimedEvents;
5351
std::mutex timed_event_mutex;
5452
thdq<ex_event> oEventQ;

jconf.h

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class jconf
3232

3333
slow_mem_cfg GetSlowMemSetting();
3434

35+
bool GetTlsSetting();
36+
bool TlsSecureAlgos();
37+
const char* GetTlsFingerprint();
38+
3539
const char* GetPoolAddress();
3640
const char* GetPoolPwd();
3741
const char* GetWalletAddress();

jpsock.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct jpsock::opq_json_val
8585
opq_json_val(const Value* val) : val(val) {}
8686
};
8787

88-
jpsock::jpsock(size_t id) : pool_id(id)
88+
jpsock::jpsock(size_t id, bool tls) : pool_id(id)
8989
{
9090
sock_init();
9191

@@ -95,8 +95,10 @@ jpsock::jpsock(size_t id) : pool_id(id)
9595

9696
prv = new opaque_private(bJsonCallMem, bJsonRecvMem, bJsonParseMem);
9797

98-
//sck = new plain_socket(this);
99-
sck = new tls_socket(this);
98+
if(tls)
99+
sck = new tls_socket(this);
100+
else
101+
sck = new plain_socket(this);
100102

101103
oRecvThd = nullptr;
102104
bRunning = false;

jpsock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class base_socket;
2424
class jpsock
2525
{
2626
public:
27-
jpsock(size_t id);
27+
jpsock(size_t id, bool tls);
2828
~jpsock();
2929

3030
bool connect(const char* sAddr, std::string& sConnectError);

socket.cpp

+51-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "socket.h"
22
#include "jpsock.h"
33
#include "jconf.h"
4+
#include "console.h"
5+
#include "executor.h"
46

57
#include <openssl/ssl.h>
68
#include <openssl/err.h>
@@ -169,7 +171,10 @@ void tls_socket::init_ctx()
169171
if(ctx == nullptr)
170172
return;
171173

172-
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION);
174+
if(jconf::inst()->TlsSecureAlgos())
175+
{
176+
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_COMPRESSION);
177+
}
173178
}
174179

175180
bool tls_socket::set_hostname(const char* sAddr)
@@ -203,11 +208,15 @@ bool tls_socket::set_hostname(const char* sAddr)
203208
return false;
204209
}
205210

206-
/*if(SSL_set_cipher_list(ssl, "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4") != 1)
211+
if(jconf::inst()->TlsSecureAlgos())
207212
{
208-
print_error();
209-
return false;
210-
}*/
213+
if(SSL_set_cipher_list(ssl, "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4:!SHA1") != 1)
214+
{
215+
print_error();
216+
return false;
217+
}
218+
}
219+
211220
return true;
212221
}
213222

@@ -241,18 +250,50 @@ bool tls_socket::connect()
241250
if(digest == nullptr)
242251
{
243252
print_error();
244-
false;
253+
return false;
245254
}
246255

247256
if(X509_digest(cert, digest, md, &dlen) != 1)
248257
{
258+
X509_free(cert);
249259
print_error();
250-
false;
260+
return false;
251261
}
252262

253-
for(size_t i=0; i < dlen; i++)
254-
printf("%.2X:", md[i]);
255-
printf("\n");
263+
if(pCallback->pool_id != executor::dev_pool_id)
264+
{
265+
//Base64 encode digest
266+
BIO *bmem, *b64;
267+
b64 = BIO_new(BIO_f_base64());
268+
bmem = BIO_new(BIO_s_mem());
269+
270+
BIO_puts(bmem, "SHA256:");
271+
b64 = BIO_push(b64, bmem);
272+
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
273+
BIO_write(b64, md, dlen);
274+
BIO_flush(b64);
275+
276+
const char* conf_md = jconf::inst()->GetTlsFingerprint();
277+
char *b64_md = nullptr;
278+
size_t b64_len = BIO_get_mem_data(bmem, &b64_md);
279+
280+
if(strlen(conf_md) == 0)
281+
{
282+
printer::inst()->print_msg(L1, "Server fingerprint: %.*s", (int)b64_len, b64_md);
283+
}
284+
else if(strncmp(b64_md, conf_md, b64_len) != 0)
285+
{
286+
printer::inst()->print_msg(L0, "FINGERPRINT FAILED CHECK: %.*s was given, %s was configured",
287+
(int)b64_len, b64_md, conf_md);
288+
289+
pCallback->set_socket_error("FINGERPRINT FAILED CHECK");
290+
BIO_free_all(b64);
291+
X509_free(cert);
292+
return false;
293+
}
294+
295+
BIO_free_all(b64);
296+
}
256297

257298
X509_free(cert);
258299
return true;

0 commit comments

Comments
 (0)