-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepf.cpp
539 lines (465 loc) · 18.1 KB
/
epf.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**
* Copyright (c) 2018 TectroLabs L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file epf.cpp
* @author Andrian Belinski
* @date 06/1/2018
* @version 1.0
*
* @brief feeds the Linux entropy pool with true random bytes retrieved from a remote service
*
* @section DESCRIPTION
*
* This is a small program that feeds the entropy pool in Linux OS with true random bytes retrieved
* from a remote HTTP service. It requires 'sudo' access permissions to run.
*
* A command line argument is used to specify where the file with configuration properties is located.
*/
#include <iostream>
#include <stack>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/random.h>
#include "Configuration.h"
#include "HttpClient.h"
#include "HttpResponse.h"
#include "RSACryptor.h"
#include "XorCryptor.h"
using namespace entropyservice;
// Define property name for retrieving the entropy service host name from configuration file
#define ENTROPY_HOST_PROPERTY_NAME "entropy.host"
// Define property name for retrieving the entropy service port from configuration file
#define ENTROPY_PORT_PROPERTY_NAME "entropy.port"
// Define property name for retrieving the host entropy service resource name from configuration file
#define ENTROPY_RESOURCE_PROPERTY_NAME "entropy.resource"
// Define property name for retrieving the byte stream encryption flag (true/false) from configuration file
#define ENTROPY_RESOURCE_BYTESTREAM_ENCRYPT_PROPERTY_NAME "entropy.resource.bytestream.encrypt"
// Define property name for retrieving the path to the RSA public key from configuration file
#define ENTROPY_RESOURCE_BYTESTREAM_RSA_FILE_PROPERTY_NAME "entropy.resource.bytestream.encrypt.pubkey.rsa.file"
// Define property name for retrieving the entropy service request size from configuration file
#define ENTROPY_REQUEST_SIZE_PROPERTY_NAME "entropy.request.byte.count"
// Define property name for retrieving the SSL (true/false) flag from configuration file
#define ENTROPY_HOST_SSL_ENABLED_PROPERTY_NAME "entropy.host.ssl.enabled"
// Define property name for retrieving the entropy service authentication token from configuration file
#define ENTROPY_AUTH_TOKEN_PROPERTY_NAME "entropy.auth.token"
// Define property name for retrieving the download thread heart beat period (in microseconds) from configuration file
#define ENTROPY_DWNLD_THREAD_PERIOD_USECS_PROPERTY_NAME "entropy.download.thread.period.usecs"
// Define property name for retrieving the feeder thread heart beat period (in microseconds) from configuration file
#define ENTROPY_FEEDER_THREAD_PERIOD_USECS_PROPERTY_NAME "entropy.feeder.thread.period.usecs"
// Define property name for retrieving the maximum number of bytes in the double ended queues from configuration file
#define ENTROPY_MAX_DEQ_SIZE_BYTES_PROPERTY_NAME "entropy.feeder.max.deq.size.bytes"
// Define number of threads for feeding the entropy pool
#define NUM_THREADS 1
// Define maximum number of bytes per request when connecting to entropy service
#define MAX_REQUEST_BYTES 10000
// Location of the Linux entropy pool
#define KERNEL_ENTROPY_POOL_NAME "/dev/random"
// Location of the kernel entropy pool size
#define KERNEL_POOLSIZE_LOCATOIN "/proc/sys/kernel/random/poolsize"
// Maximum accepted size of the kernel entropy pool in bytes
#define MAX_POOL_SIZE_BYTES (1024 * 64) // 64 KB
// Maximum number of bytes in the double ended queue below
int maxDeqSizeBytes;
// Double ended queue as a dynamic storage for downloaded random bytes
std::deque<uint8_t> deq1;
// Double ended queue as a dynamic storage for random bytes for feeding the entropy pool
std::deque<uint8_t> deq2;
// Mutex for thread synchronization
pthread_mutex_t tMutex = PTHREAD_MUTEX_INITIALIZER;
// Reference to entropy client configuration
Configuration config;
// An array of threads, as a dynamic storage, for feeding the entropy pool
pthread_t entropyThreadArray[NUM_THREADS];
// A reference to the downloading thread
pthread_t downloadThread;
// Used to signal all threads that an error has been detected
volatile bool isError = false;
// Entropy pool size in bytes
int entropyPoolSizeBytes;
// Name of the property file for retrieving the configuration data
std::string propFileName;
// A pointer to the encryptor
RSACryptor *pubKeyCryptor;
// A flag to indicate if the byte stream should must be encrypted
bool isStreamEncrypted = false;
/**
* A thread for populating dynamic storage with random bytes
* downloaded from Entropy Service
*
* @param arg - pointer to arguments passed to the thread
* @return void*
*/
void *downloadBytes(void *arg) {
char *threadName = (char*) arg;
char rndBytes[MAX_REQUEST_BYTES];
int heartBeatUsecs = config.getProperty(ENTROPY_DWNLD_THREAD_PERIOD_USECS_PROPERTY_NAME).getIntValue();
std::string hostName = config.getProperty(ENTROPY_HOST_PROPERTY_NAME).getStringValue();
int port = config.getProperty(ENTROPY_PORT_PROPERTY_NAME).getIntValue();
std::string resource = config.getProperty(ENTROPY_RESOURCE_PROPERTY_NAME).getStringValue();
std::string authToken = config.getProperty(ENTROPY_AUTH_TOKEN_PROPERTY_NAME).getStringValue();
std::string requestSizeString = config.getProperty(ENTROPY_REQUEST_SIZE_PROPERTY_NAME).getStringValue();
bool isSSL = config.getProperty(ENTROPY_HOST_SSL_ENABLED_PROPERTY_NAME).getBoolValue();
int requestSize = config.getProperty(ENTROPY_REQUEST_SIZE_PROPERTY_NAME).getIntValue();
if (requestSize > MAX_REQUEST_BYTES) {
requestSize = MAX_REQUEST_BYTES;
}
resource.append(requestSizeString);
while (!isError) {
// Check to see if we need to download more bytes
if ((int)deq1.size() < maxDeqSizeBytes / 2) {
HttpClient httpCli(hostName, port, isSSL, authToken, isStreamEncrypted, pubKeyCryptor);
bool isConnectionError = false;
if (!httpCli.connectToHost()) {
std::cerr << "Connection to host failed" << std::endl;
isConnectionError = true;
} else {
CryptoToken cryptoToken = CryptoToken(pubKeyCryptor);
if (!httpCli.sendGetRequest(resource, &cryptoToken)) {
std::cerr << "Could not send request to host" << std::endl;
isConnectionError = true;
} else {
HttpResponse resp = httpCli.retrieveResponse(&cryptoToken);
if (!resp.isResponseAvailable()) {
std::cerr << "Could not retrieve HTTP response from host" << std::endl;
isConnectionError = true;
} else {
int httpCode = resp.retrieveResponseCode();
if (httpCode != 200) {
std::cerr << "Unexpected HTTP response code: " << httpCode << std::endl;
isConnectionError = true;
} else {
if (!resp.readContent(rndBytes, requestSize)) {
std::cerr << "Could not retrieve requested bytes" << std::endl;
isConnectionError = true;
} else {
for (int i = 0; i < requestSize; i++) {
deq1.push_back(rndBytes[i]);
}
}
}
}
}
}
httpCli.closeConnection();
if (isConnectionError) {
usleep(1000 * 1000 * 15 );
}
}
int rc = pthread_mutex_lock(&tMutex);
if (rc) {
std::cerr << "could not lock the mutex in thread: " << threadName << std::endl;
isError = true;
pthread_exit(NULL);
}
if ((int)deq2.size() < maxDeqSizeBytes / 2) {
// deq2 is below 'water mark', add more random bytes from deq1
for (int i = 0; i < (int) deq1.size(); i++) {
deq2.push_back(deq1.front());
deq1.pop_front();
}
}
rc = pthread_mutex_unlock(&tMutex);
if (rc) {
std::cerr << "could not unlock the mutex in thread: " << threadName
<< std::endl;
isError = true;
pthread_exit(NULL);
}
usleep(heartBeatUsecs);
}
pthread_exit(NULL);
}
/**
* A thread for feeding the Linux entropy pool with random data downloaded
* using Entropy Sector API
*
* @param arg - arguments passed to the thread
* @return void*
*/
void *feedEntropyPool(void *arg) {
int entropyAvailable; // A variable for checking the amount of the entropy available in the kernel pool
struct {
int entropy_count;
int buf_size;
unsigned char data[MAX_POOL_SIZE_BYTES];
} entropy;
char *threadName = (char*) arg;
int heartBeatUsecs = config.getProperty(ENTROPY_FEEDER_THREAD_PERIOD_USECS_PROPERTY_NAME).getIntValue();
int rndout = open(KERNEL_ENTROPY_POOL_NAME, O_WRONLY);
if (rndout < 0) {
std::cerr << "Cannot open " << KERNEL_ENTROPY_POOL_NAME << ":"
<< rndout << std::endl;
isError = true;
pthread_exit(NULL);
}
// Check to see if we have privileges for feeding the entropy pool
int result = ioctl(rndout, RNDGETENTCNT, &entropyAvailable);
if (result < 0) {
std::cerr
<< "Cannot verify available entropy in the pool, make sure you run this utility with CAP_SYS_ADMIN capability"
<< std::endl;
close(rndout);
isError = true;
pthread_exit(NULL);
}
std::cout << "Feeding the " << KERNEL_ENTROPY_POOL_NAME
<< " kernel entropy pool of size " << (entropyPoolSizeBytes * 8) << " bits. Initial amount of entropy bits in the pool: "
<< entropyAvailable << " ..." << std::endl;
while (!isError) {
// Lock the mutex
int rc = pthread_mutex_lock(&tMutex);
if (rc) {
std::cerr << "could not lock the mutex in thread: " << threadName
<< std::endl;
isError = true;
pthread_exit(NULL);
}
// Check to see if we need more entropy
ioctl(rndout, RNDGETENTCNT, &entropyAvailable);
if (entropyAvailable < (entropyPoolSizeBytes * 8) / 2
&& deq2.size() > 0) {
// entropy pool level is below 'water mark', add more random bytes from deq2
int addMoreBytes = entropyPoolSizeBytes - (entropyAvailable >> 3);
if (addMoreBytes > (int) deq2.size()) {
addMoreBytes = deq2.size();
}
// Fill the entropy pool structure
for (int i = 0; i < addMoreBytes; i++) {
entropy.data[i] = deq2.front();
deq2.pop_front();
}
entropy.buf_size = addMoreBytes;
// Estimate the amount of entropy
entropy.entropy_count = entropyAvailable + (addMoreBytes << 3);
// Push the entropy out to the pool
result = ioctl(rndout, RNDADDENTROPY, &entropy);
if (result < 0) {
std::cerr << "Cannot add more entropy to the pool, error: "
<< result << std::endl;
close(rndout);
isError = true;
pthread_exit(NULL);
}
}
// Unlock the mutex
rc = pthread_mutex_unlock(&tMutex);
if (rc) {
std::cerr << "could not unlock the mutex in thread: " << threadName
<< std::endl;
isError = true;
pthread_exit(NULL);
}
usleep(heartBeatUsecs);
}
close(rndout);
pthread_exit(NULL);
}
/**
* Display usage message
*
*/
void displayUsage() {
std::cout << "***************************************************************" << std::endl;
std::cout << " TectroLabs - Entropy Sector - Entropy Pool Feeder Ver 1.0 " << std::endl;
std::cout << "***************************************************************" << std::endl;
std::cout <<"Usage: epf <path to epf.properties configuration file>" << std::endl;
std::cout << std::endl;
}
/**
* Parse arguments for extracting command line parameters
*
* @param argc
* @param argv
* @return int - 0 when run successfully
*/
int processArguments(int argc, char **argv) {
if (argc < 2) {
std::cerr << std::endl << "Missing command line arguments" << std::endl << std::endl;
displayUsage();
return -1;
}
propFileName = argv[1];
return 0;
}
/**
* Retrieve the size in bytes of the kernel entropy pool
*
* @return true if entropy pool size retrieved successfully
*/
bool getEntropyPoolSize() {
bool status = false;
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(KERNEL_POOLSIZE_LOCATOIN, "r");
if (fp == NULL) {
return false;
}
if ((read = getline(&line, &len, fp)) != -1) {
entropyPoolSizeBytes = atoi(line) / 8;
if (entropyPoolSizeBytes > MAX_POOL_SIZE_BYTES) {
entropyPoolSizeBytes = MAX_POOL_SIZE_BYTES;
}
status = true;
}
if (line != NULL) {
free(line);
}
fclose(fp);
return status;
}
/**
* Validate configuration properties from the file
*
* @return true if configuration is valid
*/
bool isConfigurationValid() {
std::string errString = "Could not find property ";
if (config.getProperty(ENTROPY_HOST_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_HOST_PROPERTY_NAME << std::endl;
return false;
}
if (config.getProperty(ENTROPY_PORT_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_PORT_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_PORT_PROPERTY_NAME).isInteger()) {
std::cerr << ENTROPY_PORT_PROPERTY_NAME << " is not an integer number" << std::endl;
return false;
}
if (config.getProperty(ENTROPY_RESOURCE_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_RESOURCE_PROPERTY_NAME << std::endl;
return false;
}
if (config.getProperty(ENTROPY_RESOURCE_BYTESTREAM_ENCRYPT_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_RESOURCE_BYTESTREAM_ENCRYPT_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_RESOURCE_BYTESTREAM_ENCRYPT_PROPERTY_NAME).isBoolean()) {
std::cerr << ENTROPY_RESOURCE_BYTESTREAM_ENCRYPT_PROPERTY_NAME << " is not a boolean" << std::endl;
return false;
}
if (config.getProperty(ENTROPY_RESOURCE_BYTESTREAM_ENCRYPT_PROPERTY_NAME).getBoolValue() == true) {
if(config.getProperty(ENTROPY_RESOURCE_BYTESTREAM_RSA_FILE_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_RESOURCE_BYTESTREAM_RSA_FILE_PROPERTY_NAME << std::endl;
return false;
}
// Validate public key
std::string pubKeyFileName = config.getProperty(ENTROPY_RESOURCE_BYTESTREAM_RSA_FILE_PROPERTY_NAME).getStringValue();
pubKeyCryptor = new RSACryptor(pubKeyFileName.c_str(), true);
if (!pubKeyCryptor->isInitialized()) {
std::cerr << errString << "Could not use public key file: " << pubKeyFileName << std::endl;
return false;
}
isStreamEncrypted = true;
}
if (config.getProperty(ENTROPY_REQUEST_SIZE_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_REQUEST_SIZE_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_REQUEST_SIZE_PROPERTY_NAME).isInteger()) {
std::cerr << ENTROPY_REQUEST_SIZE_PROPERTY_NAME << " is not an integer number" << std::endl;
return false;
}
if (config.getProperty(ENTROPY_HOST_SSL_ENABLED_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_HOST_SSL_ENABLED_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_HOST_SSL_ENABLED_PROPERTY_NAME).isBoolean()) {
std::cerr << ENTROPY_HOST_SSL_ENABLED_PROPERTY_NAME << " is not a boolean" << std::endl;
return false;
}
if (config.getProperty(ENTROPY_DWNLD_THREAD_PERIOD_USECS_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_DWNLD_THREAD_PERIOD_USECS_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_DWNLD_THREAD_PERIOD_USECS_PROPERTY_NAME).isInteger()) {
std::cerr << ENTROPY_DWNLD_THREAD_PERIOD_USECS_PROPERTY_NAME << " is not an integer number" << std::endl;
return false;
}
if (config.getProperty(ENTROPY_FEEDER_THREAD_PERIOD_USECS_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_FEEDER_THREAD_PERIOD_USECS_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_FEEDER_THREAD_PERIOD_USECS_PROPERTY_NAME).isInteger()) {
std::cerr << ENTROPY_FEEDER_THREAD_PERIOD_USECS_PROPERTY_NAME << " is not an integer number" << std::endl;
return false;
}
if (config.getProperty(ENTROPY_MAX_DEQ_SIZE_BYTES_PROPERTY_NAME).getStringValue().size() == 0) {
std::cerr << errString << ENTROPY_MAX_DEQ_SIZE_BYTES_PROPERTY_NAME << std::endl;
return false;
}
if (!config.getProperty(ENTROPY_MAX_DEQ_SIZE_BYTES_PROPERTY_NAME).isInteger()) {
std::cerr << ENTROPY_MAX_DEQ_SIZE_BYTES_PROPERTY_NAME << " is not an integer number" << std::endl;
return false;
}
maxDeqSizeBytes = config.getProperty(ENTROPY_MAX_DEQ_SIZE_BYTES_PROPERTY_NAME).getIntValue();
return true;
}
/**
* Main entry
*
* @param argc number of arguments
* @rapam argv pointer to parameters
*
* @return return code
*/
int main(int argc, char **argv) {
int status = processArguments(argc, argv);
if (status) {
return status;
}
// Load configuration from properties file
if (!config.loadFromFile((char*)propFileName.c_str())) {
std::cerr << "Could not load configuration from propFileName" << std::endl;
return -1;
}
if (!isConfigurationValid()) {
return -1;
}
if (!getEntropyPoolSize()) {
std::cerr << "Cannot get the size of the kernel entropy pool " << KERNEL_POOLSIZE_LOCATOIN << status << std::endl;
return -1;
}
// Create the download thread
pthread_create(&downloadThread, NULL, downloadBytes,
(void*) "download thread");
// Create threads for feeding the entropy pool
for (int i = 0; i < NUM_THREADS; i++) {
pthread_create(&entropyThreadArray[i], NULL, feedEntropyPool,
(void*) "pool thread");
}
// Wait for feeding threads to finish
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(entropyThreadArray[i], NULL);
}
// If we got to this point then something went wrong
// Shutdown the downloadBytes thread
isError = true;
// Wait for downloadBytes thread to finish
pthread_join(downloadThread, NULL);
return -1;
}