-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecure.h
439 lines (316 loc) · 10.8 KB
/
Secure.h
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
/******************************************************************************/
/* */
/* Copyright (c) 2010, 2014 Sylwester Wysocki <[email protected]> */
/* */
/* 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. */
/* */
/******************************************************************************/
#ifndef Tegenaria_Core_Secure_H
#define Tegenaria_Core_Secure_H
//
// Includes.
//
#include <cstdio>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>
#include <unistd.h>
#include <string>
#include <list>
#include <map>
#ifdef WIN32
# include <io.h>
#else
# include <sys/socket.h>
# include <termios.h>
#endif
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/blowfish.h>
#include <Tegenaria/Debug.h>
#include <Tegenaria/Mutex.h>
namespace Tegenaria
{
using std::string;
using std::list;
using std::map;
//
// Defines.
//
#define SECURE_INTENT_SERVER 0
#define SECURE_INTENT_CLIENT 1
#define SECURE_STATE_HANDSHAKE_WRITE 0
#define SECURE_STATE_HANDSHAKE_READ 1
#define SECURE_STATE_ESTABLISHED 2
#define SECURE_IOMODE_NONE 0
#define SECURE_IOMODE_CALLBACKS 1
#define SECURE_IOMODE_FDS 2
#define SECURE_IOMODE_SOCKET 3
#define SECURE_TLS_HANDSHAKE_TIMEOUT 30000 // Timeout while performing SSL handshake
#define SECURE_BLOWFISH_KEY_SIZE 16
#define SECURE_CIPHER_BLOWFISH 0
#define SECURE_CIPHER_MODE_ECB 0
#define SECURE_CIPHER_MODE_CTR 1
#define SECURE_MAX_KEYPASS_LEN 64
//
// Rights for ACL.
//
#define SECURE_ACL_DENY (1 << 0)
#define SECURE_ACL_FULL (1 << 1)
#define SECURE_ACL_READ (1 << 2)
#define SECURE_ACL_WRITE (1 << 3)
#define SECURE_ACL_ERASE (1 << 4)
#define SECURE_ACL_SYMBOL_DENY 'D'
#define SECURE_ACL_SYMBOL_FULL 'F'
#define SECURE_ACL_SYMBOL_READ 'R'
#define SECURE_ACL_SYMBOL_WRITE 'W'
#define SECURE_ACL_SYMBOL_ERASE 'E'
//
// Typedef.
//
typedef int (*SecureReadProto)(void *buf, int count, int timeout, void *ctx);
typedef int (*SecureWriteProto)(const void *buf, int count, int timeout, void *ctx);
//
// Class to wrap FD/SOCKET/Callbacks into secure one.
//
class SecureConnection
{
//
// Friends.
//
friend SecureConnection
*SecureConnectionCreate(int, SecureReadProto, SecureWriteProto,
void *, const char *, const char *,
const char *);
friend SecureConnection
*SecureConnectionCreate(int, int, int, const char *,
const char *, const char *);
friend SecureConnection
*SecureConnectionCreate(int, int, const char *,
const char *, const char *);
friend SecureConnection
*SecureConnectionCreate(int, const char *,
const char *, const char *);
//
// Private fields.
//
private:
//
// Underlying SSL data.
//
BIO *readBio_;
BIO *writeBio_;
SSL *ssl_;
SSL_CTX *sslCtx_;
//
// SECURE_INTENT_CLIENT or SECURE_INTENT_SERVER.
//
int intent_;
//
// Current connection state.
// See SECURE_STATE_XXX defines.
//
int state_;
//
// Specify type of underlying unecrypted IO.
// See SECURE_IOMODE_XXX defines.
//
int ioMode_;
//
// Used when underlying IO is defined by {read, write} callbacks.
//
SecureReadProto readCallback_;
SecureWriteProto writeCallback_;
void *ioCtx_;
//
// Used when underlying IO is defined by FD pair.
//
int fdIn_;
int fdOut_;
//
// Used when underlying IO is a socket.
//
int sock_;
//
// Reffrence counter to track number of third objects
// using current secure connection object.
//
int refCount_;
Mutex refCountMutex_;
//
// Private init functions.
//
SecureConnection();
~SecureConnection();
int initSSL(int intent, const char *cert,
const char *privKey, const char *privKeyPass);
//
// Callbacks called by OpenSSL.
//
static int readPassCallback(char *buf, int size,
int rwflag, void *userdata);
//
// Public interface.
//
public:
//
// Refference counter.
//
void addRef();
void release();
//
// Functions for read/write/request over secure connection.
//
int write(const void *buf, int len, int timeout);
int read(void *buf, int len, int timeout);
int request(int *serverCode, char *serverMsg,
int serverMsgSize, int timeout, const char *fmt, ...);
//
// Direct encrypt/decrypt using underlying SSL object.
//
int encrypt(void *encrypted, int encryptedSize,
const void *buffer, int bufferSize);
int decrypt(void *decrypted,int decryptedSize,
const void *buffer, int bufferSize);
//
// Functions for read/write bypassing encryption system.
// These function passes data to underlying IO directly.
//
int writeRaw(const void *buf, int len, int timeout);
int readRaw(void *buf, int len, int timeout);
//
// Handshake.
//
int handshakeStep(void *customBuffer = NULL, int *customSize = NULL);
int handshakeStep(void *outputBuffer, int *outputSize,
void *inputBuffer, int inputSize);
//
// Getters.
//
int getState();
};
//
// Structure to store cipher context while encrypting/decrypting
// streams.
//
struct SecureCipher;
//
// Class to implement generic access list.
//
class SecureAcl
{
private:
map<string, int> rights_;
public:
//
// Init methods.
//
SecureAcl();
int initFromString(const char *acl);
//
// Get rights from ACL.
//
int getRights(const char *user);
string getRightsString(const char *user);
//
// Grant/revoke rights to ACL.
//
int setRights(const char *user, int rights);
int setRights(const char *user, const char *rights);
int setOthersRights(int rights);
int setOthersRights(const char *rights);
int revokeAll(const char *user);
void clear();
//
// Conversion methods.
//
int encodeRights(const char *str);
string decodeRights(int rights);
string toString();
};
//
// Exported functions.
//
//
// Random numbers.
//
int SecureRandom(void *buf, int len);
int SecureRandomText(char *buf, int len);
int SecureRandomHexText(char *buf, int len);
uint32_t SecureRandomInt32();
uint64_t SecureRandomInt64();
unsigned char SecureRandomByte();
//
// Generic encrypt/decrypt for raw buffers.
//
void SecureEncrypt(SecureCipher *sc, void *buffer, int size);
void SecureDecrypt(SecureCipher *sc, void *buffer, int size);
SecureCipher *SecureCipherCreate(int cipher, int cipherMode,
const char *key, int keySize,
const char *iv, int ivSize);
void SecureCipherDestroy(SecureCipher *ctx);
//
// Wrap {read, write} callback into secure connection.
//
SecureConnection *SecureConnectionCreate(int intent,
SecureReadProto readCallback,
SecureWriteProto writeCallback,
void *ioCtx,
const char *cert,
const char *privKey,
const char *privKeyPass);
//
// Wrap fdin/fdout pair into secure connection.
//
SecureConnection *SecureConnectionCreate(int intent, int fdin, int fdout,
const char *cert,
const char *privKey,
const char *privKeyPass);
//
// Wrap socket into secure connection.
//
SecureConnection *SecureConnectionCreate(int intent, int sock,
const char *cert,
const char *privKey,
const char *privKeyPass);
//
// Custom secure connection.
//
SecureConnection *SecureConnectionCreate(int intent,
const char *cert,
const char *privKey,
const char *privKeyPass);
//
// One-direction hash functions.
//
int SecureHashSha256(char *hash, int hashSize,
const char *data, int dataSize,
const char *salt, int saltSize);
//
// Password related functions.
//
int SecureDisableEcho();
int SecureEnableEcho();
int SecureReadPassword(char *pass, int passSize, const char *prompt);
int SecurePassAuthorize(const char *expectedHash,
const char *password, const char *salt);
} /* namespace Tegenaria */
#endif /* Tegenaria_Core_Secure_H */