-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.cpp
1449 lines (1120 loc) · 35.3 KB
/
Connection.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************/
/* */
/* 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. */
/* */
/******************************************************************************/
//
// WARNING!
// To create fully secure connection we need two steps:
//
// 1. Create secure TLS session - after that transfered data are encrypted
// and *NOT READABLE* by third parties,
//
// 2. validate *WHO is ON THE OTHER SIDE* basing on data from certificate,
// e.g. by check domain name.
//
// Below code performs (1) but does *NOT* perform (2).
// Caller *SHOULD* deliver own mechanism to check does data delivered
// within certificate are match to what he's expecting.
//
//
// Purpose: Wrap existing unsecure connection into secure one.
//
// Source unsecure connection can be defined in follow ways:
//
// - {read, write} callbacks
// - {FDin, FDout} pair
// - socket
//
// - custom, caller must use SecureEncrypt before every write operation and
// SecureDecrypt afrer every read operation manually
//
#include "Secure.h"
#ifdef WIN64
static int Win64NotImportedError()
{
fprintf(stderr, "OpenSSL is not imported on Win64");
exit(-1);
}
#define SSL_write(x, y, z) Win64NotImportedError()
#define SSL_read(x, y, z) Win64NotImportedError()
#define SSL_free(x) Win64NotImportedError()
#define SSL_CTX_free(x) Win64NotImportedError()
#define SSL_is_init_finished(x) Win64NotImportedError()
#define SSL_do_handshake(x) Win64NotImportedError()
#define BIO_read(x, y, z) Win64NotImportedError()
#define BIO_write(x, y, z) Win64NotImportedError()
#define SSL_library_init() Win64NotImportedError()
#define SSL_CTX_new(x) (SSL_CTX *) Win64NotImportedError()
#define SSL_CTX_use_certificate_chain_file(x, y) Win64NotImportedError()
#define SSL_CTX_set_default_passwd_cb(x, y) Win64NotImportedError()
#define SSL_CTX_set_default_passwd_cb_userdata(x, y) Win64NotImportedError()
#define SSL_CTX_use_PrivateKey_file(x, y, z) Win64NotImportedError()
#define SSL_CTX_use_certificate_file(x, y, z) Win64NotImportedError()
#define SSL_CTX_set_options(x, y) Win64NotImportedError()
#define SSL_CTX_set_session_id_context(x, y, z) Win64NotImportedError()
#define SSL_new(x) (SSL *) Win64NotImportedError()
#define SSL_set_accept_state(x) Win64NotImportedError()
#define SSL_set_verify(x, y, z) Win64NotImportedError()
#define SSL_set_session_id_context(x, y, z) Win64NotImportedError()
#define BIO_s_mem() Win64NotImportedError()
#define SSL_set_connect_state(x) Win64NotImportedError()
#define BIO_new(x) (BIO *) Win64NotImportedError()
#define BIO_set_nbio(x, y) Win64NotImportedError()
#define SSL_set_bio(x, y, z) Win64NotImportedError()
#endif
namespace Tegenaria
{
//
// Write <len> bytes directly to underlying IO skipping SSL object beetwen.
//
// Used internally only.
//
// buf - source buffer with data, which we want to write (IN).
// len - number of bytes to write (IN).
// timeout - timeout in ms, set to -1 for infinite (IN).
//
// RETURNS: Number of bytes written or
// -1 if error.
//
int SecureConnection::writeRaw(const void *buf, int len, int timeout)
{
DBG_ENTER3("SecureConnection::writeRaw");
int written = -1;
//
// Write data to underlying IO.
//
switch(ioMode_)
{
//
// Underlying IO is defined by {read, write, cancel} callbacks.
//
case SECURE_IOMODE_CALLBACKS:
{
written = writeCallback_(buf, len, -1, ioCtx_);
break;
}
//
// Underlying IO is defined as FD pair.
//
case SECURE_IOMODE_FDS:
{
if (timeout > 0)
{
Error("ERROR: Timeout not implemented for FDs yet in SecureConnection::writeRaw().\n");
}
written = ::write(fdOut_, buf, len);
break;
}
//
// Underlying IO is a socket.
//
case SECURE_IOMODE_SOCKET:
{
if (timeout > 0)
{
Error("ERROR: Timeout not implemented for SOCKET yet in SecureConnection::writeRaw().\n");
}
written = send(sock_, (char *) buf, len, 0);
break;
}
}
DBG_LEAVE3("SecureConnection::writeRaw");
return written;
}
//
// Read data directly from underlying IO (without parsing it via SSL
// object).
//
// Used internally only.
//
// buf - destination buffer, where to write readed data (OUT).
// len - number of bytes to read (IN).
// timeout - timeout in ms, set -1 to infinity (IN).
//
// RETURNS: Number of bytes readed or
// -1 if error.
//
int SecureConnection::readRaw(void *buf, int len, int timeout)
{
DBG_ENTER3("SecureConnection::readRaw");
int written = -1;
int readed = -1;
//
// Read data from underlying IO.
//
switch(ioMode_)
{
//
// Underlying IO is defined as {read, write, cancel} callbacks.
//
case SECURE_IOMODE_CALLBACKS:
{
readed = readCallback_(buf, len, timeout, ioCtx_);
break;
}
//
// Underlying IO is defined as FD pair.
//
case SECURE_IOMODE_FDS:
{
if (timeout > 0)
{
Error("WARNING: Timeout is not available for FDs yet in SecureConnection::readRaw().\n");
}
readed = ::read(fdIn_, buf, len);
break;
}
//
// Underlying IO is a socket.
//
case SECURE_IOMODE_SOCKET:
{
if (timeout > 0)
{
Error("WARNING: Timeout is not available for SOCKETs yet in SecureConnection::readRaw().\n");
}
readed = recv(sock_, (char *) buf, len, 0);
break;
}
}
DBG_ENTER3("SecureConnection::readRaw");
return readed;
}
//
// Encrypt message.
//
// encrypted - buffer, where to store encrypted message (OUT).
// encryptedSize - size of encrypted[] buffer in bytes (IN).
// buffer - source buffer with data to encrypt (IN).
// bufferSize - number of bytes to be encrypted (IN).
//
// RETURNS: Length of encrypted data written to encrypted[] in bytes or
// -1 if error.
//
int SecureConnection::encrypt(void *encrypted, int encryptedSize,
const void *buffer, int bufferSize)
{
DBG_ENTER3("SecureConnection::encrypt");
int readed = 0;
//
// Pass unecrypted message to SSL BIO.
//
SSL_write(ssl_, buffer, bufferSize);
//
// Read back encrypted message from SSL.
//
readed = BIO_read(writeBio_, encrypted, encryptedSize);
DBG_LEAVE3("SecureConnection::encrypt");
return readed;
}
//
// Decrypt message.
//
// decrypted - buffer, where to store decrypted message (OUT).
// decryptedSize - size of decrypted[] buffer in bytes (IN).
// buffer - source buffer with data to be decrypt (IN).
// bufferSize - number of bytes to be decrypted (IN).
//
// RETURNS: Length of decrypted data written to decrypted[] in bytes or
// -1 if error.
//
int SecureConnection::decrypt(void *decrypted, int decryptedSize,
const void *buffer, int bufferSize)
{
DBG_ENTER3("SecureConnection::decrypt");
int readed = 0;
//
// Pass readed enrypted data to SSL BIO.
//
BIO_write(readBio_, buffer, bufferSize);
//
// Read back decrypted data from SSL.
//
readed = SSL_read(ssl_, decrypted, decryptedSize);
DBG_LEAVE3("SecureConnection::ecrypt");
return readed;
}
//
// Write <len> bytes throught secure connection.
//
// buf - source buffer with data, which we want to write (IN).
// len - number of bytes to write (IN).
// timeout - timeout in ms, set to -1 for infinite (IN).
//
// RETURNS: Number of bytes written or
// -1 if error.
//
int SecureConnection::write(const void *buf, int len, int timeout)
{
DBG_ENTER3("SecureConnection::write");
int written = -1;
int readed = -1;
char encrypted[1024];
int encryptedSize = 0;
//
// Encrypt message.
//
encryptedSize = this -> encrypt(encrypted, sizeof(encrypted), buf, len);
//
// Write encrypted data to underlying IO.
//
if (encryptedSize > 0)
{
written = this -> writeRaw(encrypted, encryptedSize, timeout);
}
DBG_LEAVE3("SecureConnection::write");
return written;
}
//
// Read data from secure connection.
//
// buf - destination buffer, where to write readed data (OUT).
// len - number of bytes to read (IN).
// timeout - timeout in ms, set -1 to infinite (IN)
//
// RETURNS: Number of bytes readed or
// -1 if error.
//
int SecureConnection::read(void *buf, int len, int timeout)
{
DBG_ENTER3("SecureConnection::read");
int written = -1;
int readed = -1;
char encrypted[1024];
int encryptedSize = 0;
//
// Read encrypted data from underlying IO.
//
encryptedSize = this -> readRaw(encrypted, sizeof(encrypted), timeout);
//
// Decrypt message into caller buffer.
//
if (encryptedSize > 0)
{
readed = this -> decrypt(buf, len, encrypted, encryptedSize);
}
DBG_LEAVE3("SecureConnection::read");
return readed;
}
//
// - Send single, printf like formatted request to server
// - read answer in format 'XYZ > message'
// - split answer to <XYZ> code and <message> parts.
//
// Example usage:
//
// request(&serverCode, serverMsg, sizeof(serverMsg),
// "share --alias %s --path %s", alias, path);
//
// TIP: If only exit code is needed <answer> can be set to NULL.
//
// sc - pointer to SecureConnection object connected to server (IN).
// serverCode - exit code returned by server (OUT).
// serverMsg - ASCIZ message returned by server (OUT/OPT).
// serverMsgSize - size of answer buffer in bytes (IN).
// timeout - timeout in ms, defaulted to infinite if -1 (IN/OPT).
// fmt - printf like parameters to format command to send (IN).
//
// RETURNS: 0 if request sucessfuly sent and asnwer from server received.
// -1 otherwise.
//
// WARNING!: Request could still failed on server side.
// To get server's side exit code use 'answerCode' parameter.
//
int SecureConnection::request(int *serverCode, char *serverMsg,
int serverMsgSize, int timeout,
const char *fmt, ...)
{
DBG_ENTER("SecureConnection::request");
int exitCode = -1;
char buf[1024];
int cmdLen = 0;
char *dst = NULL;
int readed = 0;
int total = 0;
int eofReceived = 0;
int len = 0;
va_list ap;
//
// Check args.
//
FAILEX(serverCode == NULL, "ERROR: 'serverCode' cannot be NULL in SecureRequest.");
FAILEX(fmt == NULL, "ERROR: 'fmt' cannot be NULL in SecureRequest.");
//
// Format printf like message.
//
va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
va_end(ap);
//
// Send command to server INCLUDING zero terminator byte.
//
FAILEX(this -> write(buf, len + 1, timeout) < 0,
"ERROR: Cannot send request.\n");
//
// Read answer from server in below format:
// 'XYZ> <message>'
//
// Where <XYZ> is 3 decimal server side code e.g. "871".
//
//
// Read 'XYZ> ' prefix first.
// where XYZ is 3 decimal exit code returned by server.
//
FAILEX(this -> read(buf, 5, timeout) != 5,
"ERROR: Cannot read 'XYZ> ' prefix.");
buf[4] = 0;
*serverCode = atoi(buf);
//
// Read ASCIZ message part if needed.
//
if (serverMsg && serverMsgSize > 0)
{
dst = serverMsg;
total = 0;
//
// FIXME: Avoid reading byte by byte.
//
while(this -> read(dst, 1, timeout) == 1)
{
//
// Caller buffer too short.
//
if (total == serverMsgSize)
{
break;
}
//
// End of message, it's ordinal end.
//
if (dst[0] == 0)
{
eofReceived = 1;
break;
}
total ++;
dst ++;
}
serverMsg[total] = 0;
}
//
// Flush remaining message from server if any.
// This is scenario when caller message[] buffer is shorter
// than message sent by server.
//
while(eofReceived == 0)
{
if (this -> read(buf, 1, timeout) <= 0 || buf[0] == 0)
{
eofReceived = 1;
}
}
exitCode = 0;
//
// Clean up.
//
fail:
if (exitCode)
{
Error("Cannot execute secure request.\n"
"Error code is : %d.\n", GetLastError());
}
DBG_LEAVE("SecureConnection::request");
return exitCode;
}
//
// Desctroy secure connection created by SecureConnectionCreate() before.
//
SecureConnection::~SecureConnection()
{
DBG_ENTER("SecureConnection::~SecureConnection");
if (ssl_)
{
SSL_free(ssl_);
}
if (sslCtx_)
{
SSL_CTX_free(sslCtx_);
}
DBG_LEAVE("SecureConnection::~SecureConnection");
}
//
// Perform underlying SSL handshake to init encrypted secure connection.
//
// Internal use only.
//
// TIP#1: For custom connection use 5-parameter SecureHandshakeStep.
//
// TIP#2: For non-custom connections (underlying IO is set to callbacks,
// socket or FDs pair} handshake is performed automatically in
// SecureConnectionCreate. No manually work needed.
//
//
// WARNING! Handshake must be performed before any data would be send
// via SecureWrite() or read via SecureRead() functions.
//
// Parameters:
//
// customBuffer - on input data treated as readed from underlying IO if IOMODE
// set to NONE. On output data needed to be written to
// underlying IO. (IN/OUT/OPT).
//
// customSize - on input size of customBuffer[] in bytes if IOMODE set to
// NONE. On output number of bytes returned in customBuffer[]
// and needed to be written back to underlying IO (IN/OUT/OPT).
//
// RETURNS: 0 if OK.
//
int SecureConnection::handshakeStep(void *customBuffer, int *customSize)
{
DBG_ENTER("SecureConnection::handshakeStep");
int exitCode = -1;
char buffer[1024];
int readed = 0;
int written = 0;
//
// Handshake finished.
//
if (SSL_is_init_finished(ssl_))
{
state_ = SECURE_STATE_ESTABLISHED;
//
// Server should sent "OK" message at the end of handshake.
//
if (intent_ == SECURE_INTENT_CLIENT)
{
readed = SSL_read(ssl_, buffer, 2);
FAIL(readed != 2);
FAIL(buffer[0] != 'O');
FAIL(buffer[1] != 'K');
}
}
//
// Handshake still pending.
//
else
{
switch(state_)
{
//
// Write turn.
// Client begins here.
//
// -> SSL -> IO -> ...
//
case SECURE_STATE_HANDSHAKE_WRITE:
{
//
// Read handshake data from SSL BIO.
//
SSL_do_handshake(ssl_);
readed = BIO_read(writeBio_, buffer, 1024);
DEBUG3("Readed [%d] bytes from SSL BIO.\n", readed);
FAIL(readed <= 0);
//
// Underlying IO is set to NONE.
// Pass data needed to be written back to caller.
//
if (ioMode_ == SECURE_IOMODE_NONE)
{
FAILEX(*customSize < readed,
"ERROR: 'customSize' too small in SecureHandshakeStep.\n");
memcpy(customBuffer, buffer, readed);
*customSize = readed;
written = readed;
}
// Underlying IO is set.
// Write back handshake data to underlying IO.
//
else
{
written = this -> writeRaw(buffer, readed, SECURE_TLS_HANDSHAKE_TIMEOUT);
}
DEBUG3("Written [%d] bytes to underlying IO.\n", written);
FAIL(written <= 0);
//
// Handshake finished.
//
if (SSL_is_init_finished(ssl_))
{
state_ = SECURE_STATE_ESTABLISHED;
}
//
// Handshake pending.
// Swap turn into read.
//
else
{
state_ = SECURE_STATE_HANDSHAKE_READ;
}
break;
}
//
// Read turn.
// Server begins here.
//
// ... -> IO -> SSL
//
case SECURE_STATE_HANDSHAKE_READ:
{
//
// Underlying IO is not set. Use input customBuffer as data
// readed from underlying IO.
//
if (ioMode_ == SECURE_IOMODE_NONE)
{
memcpy(buffer, customBuffer, *customSize);
readed = *customSize;
}
//
// Underlying IO is set.
// Read handshake data from underlying IO.
//
else
{
readed = this -> readRaw(buffer, 1024, SECURE_TLS_HANDSHAKE_TIMEOUT);
}
DEBUG3("Readed [%d] bytes from underlying IO.\n", readed);
FAIL(readed <= 0);
//
// Redirect readed data to SSL BIO.
//
written = BIO_write(readBio_, buffer, readed);
DEBUG3("Written [%d] bytes to SSL BIO.\n", written);
FAIL(written <= 0);
//
// Handshake finished.
//
if (SSL_is_init_finished(ssl_))
{
state_ = SECURE_STATE_ESTABLISHED;
if (intent_ == SECURE_INTENT_CLIENT)
{
readed = SSL_read(ssl_, buffer, 2);
FAIL(readed != 2);
FAIL(buffer[0] != 'O');
FAIL(buffer[1] != 'K');
}
}
//
// Handshake pending.
// Swap turn into write.
//
else
{
SSL_do_handshake(ssl_);
state_ = SECURE_STATE_HANDSHAKE_WRITE;
}
break;
}
}
}
//
// Server send ecrypted "OK" message if handshake finished.
//
if (state_ == SECURE_STATE_ESTABLISHED && intent_ == SECURE_INTENT_SERVER)
{
if (ioMode_ == SECURE_IOMODE_NONE)
{
*customSize = this -> encrypt(customBuffer, *customSize, "OK", 2);
DEBUG1("SSL Handshake finished.\n");
}
else
{
this -> write("OK", 2, SECURE_TLS_HANDSHAKE_TIMEOUT);
}
}
exitCode = 0;
fail:
if (exitCode)
{
Error("SSL handshake failed.\n");
}
DBG_LEAVE("SecureConnection::handshakeStep");
return exitCode;
}
//
// Handshake step for custom connection (iomode set to none).
//
// TIP#1: This function should be used to perform manually handshake when
// underlying IO is set to NONE (i.e. custom secure connection).
//
// TIP#2: Before call, caller should read data from underlying IO manually
// and pass it in {inputBuffer, inputSize} parameters.
//
// TIP#3: After call, caller should write data returned in
// {outputBuffer, outputSize} to underlying IO manually.
//
// Caller algorithm to do handshake manually:
//
// while(sc -> state != SECURE_STATE_ESTABLISHED)
// {
// Read data from underlying IO to inputBuffer[].
// Call SecureHandshakeStep(inputBuffer, ..., outputBuffer, ...)
// Write data from outputBuffer[] to underlying IO.
// }
//
//
// sc - secure connection object returned from SecureConnectionCreate() (IN).
// outputBuffer - data needed to be written to underlying IO by caller (OUT).
//
// outputSize - on input size of outputBuffer[] in bytes, on output number
// of bytes written to outputBuffer[] (IN/OUT).
//
// inputBuffer - data readed from underlying IO (IN).
// inputSize - size of inputBuffer[] in bytes (IN).
//
// RETURNS: 0 if OK.
//
int SecureConnection::handshakeStep(void *outputBuffer, int *outputSize,
void *inputBuffer, int inputSize)
{
DBG_ENTER("SecureConnection::handshakeStep");
int exitCode = -1;
FAIL(this -> handshakeStep(inputBuffer, &inputSize));
if (this -> state_ != SECURE_STATE_ESTABLISHED)
{
FAIL(this -> handshakeStep(outputBuffer, outputSize));
}
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE("SecureConnection::handshakeStep");
return exitCode;
}
//
// Create empty secure connection object.
// Used internally only.
//
// TIP#1: Don't create SecureConnection object directly.
// Use one of SecureConnectionCreate() function instead.
//
SecureConnection::SecureConnection()
{
readBio_ = NULL;
writeBio_ = NULL;
ssl_ = NULL;
sslCtx_ = NULL;
int intent_ = -1;
int state_ = -1;
int ioMode_ = -1;
readCallback_ = NULL;
writeCallback_ = NULL;
ioCtx_ = NULL;
int fdIn_ = -1;
int fdOut_ = -1;
int sock_ = -1;
refCount_ = 1;
refCountMutex_.setName("SecureConnection::refCountMutex_");
}
//
// Initialize SSL DTLS connection inside secure connection object.
//
// Internal use only by SecureConnectionCreate().
//
// RETURNS: 0 if OK.
//
int SecureConnection::initSSL(int intent, const char *cert,
const char *privKey, const char *privKeyPass)
{
DBG_ENTER("SecureConnection::initSSL");
int exitCode = -1;
unsigned char sid[SSL_MAX_SSL_SESSION_ID_LENGTH] ;
//
// Init SSL library.
//
SSL_library_init();
//
// Create SSL context.
//
DEBUG3("Creating SSL context...\n");
sslCtx_ = SSL_CTX_new(DTLSv1_method());
FAILEX(sslCtx_ == NULL, "ERROR: Cannot create SSL context.\n");
//
// Server.
//
if (intent == SECURE_INTENT_SERVER)
{
//
// Assign certificate and private key to context.
//
DEBUG3("Assigning certificate to SSL context...\n");
SSL_CTX_use_certificate_chain_file(sslCtx_, cert);
//
// Set pass to decode private key if needed.
//
if (privKeyPass)
{
FAILEX(true, "ERROR: 'privKeyPass' param is obsolete.")
}
//
// Set input files with prviate key and server certificate.
//
SSL_CTX_use_PrivateKey_file(sslCtx_, privKey, SSL_FILETYPE_PEM);
SSL_CTX_use_certificate_file(sslCtx_, cert, SSL_FILETYPE_PEM);
//
// Set SINGLE_DH_USE option.