forked from commoncriteria/tls-cc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.c
More file actions
2800 lines (2398 loc) · 65.7 KB
/
tests.c
File metadata and controls
2800 lines (2398 loc) · 65.7 KB
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
#include "tests.h"
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include "ssl.h"
#include "printer.h"
#include "tcp_sock.h"
#include "common.h"
#include "raw_ssl.h"
#include "cert_gen.h"
#define FAKE_SSL_VERSION 0x0304
#define BAD_CN_1 "invalidcn.com"
#define BAD_CN_2 "badcn.org"
#define BAD_ALT_1 "URI:invalidalt.com"
#define BAD_ALT_2 "URI:badalt.org"
#define FAKE_COUNTRY "FAKE COUNTRY"
#define FAKE_ORG "FAKE ORGANIZATION"
#define FAKE_UNIT "FAKE ORG UNIT"
#define FAKE_CA "FAKE CA"
#define FAKE_BITS 2048
#define CLIENT_COUNTRY "CLIENT COUNTRY"
#define CLIENT_ORG "CLIENT ORGANIZATION"
#define CLIENT_UNIT "CLIENT ORG UNIT"
#define CLIENT_NAME "CLIENT NAME"
#define CLIENT_CERT "client_cert.pem"
#define CLIENT_CERT_P12 "client_cert.pfx"
#define CLIENT_P12_NAME "Fake PKCS12 Certificate"
#define CLIENT_P12_PWD ""
#define WILD_START "foo"
#define INTER_CERT "ica_"
int
get_rsa_mod(X509 *cert, unsigned char *buf, unsigned int size)
{
int algid;
EVP_PKEY *pub_key = 0;
RSA *rsa_key = 0;
#if OPENSSL_VERSION_NUMBER >= 0x10100000L /* OpenSSL 1.1.x */
const BIGNUM *n, *e;
#endif
/* int key_len; */
int ret;
#if OPENSSL_VERSION_NUMBER >= 0x10002000L /* OpenSSL 1.0.2 */
ASN1_OBJECT *ppkalg;
X509_PUBKEY *pk;
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
pk = X509_get_X509_PUBKEY(cert);
X509_PUBKEY_get0_param(&ppkalg, NULL, NULL, NULL, pk);
algid = OBJ_obj2nid(ppkalg);
#else
algid = OBJ_obj2nid(cert->cert_info->key->algor->algorithm);
#endif
if (algid != NID_rsaEncryption) {
ret = 0;
goto error_die;
}
if (!(pub_key = X509_get_pubkey(cert))) {
ret = 0;
goto error_die;
}
if (!(rsa_key = EVP_PKEY_get1_RSA(pub_key))) {
ret = 0;
goto error_die;
}
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
if (size < RSA_size(rsa_key))
#else
if (size < BN_num_bytes(rsa_key->n))
#endif
{
ret = 0;
goto error_die;
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L /* OpenSSL 1.0.2 */
ret = BN_bn2bin(rsa_key->n, buf);
#else /* 1.1.x */
RSA_get0_key(rsa_key, &n, &e, NULL);
ret = BN_bn2bin(n, buf);
#endif
if (ret > size) {
write_out(PRINT_ERROR, "Buffer overflow when extracting RSA"
" modulus!");
ret = 0;
goto error_die;
}
error_die:
if (pub_key)
EVP_PKEY_free(pub_key);
if (rsa_key)
RSA_free(rsa_key);
return (ret);
}
int
is_cipher_in_list(unsigned short cipher, cipher_suite_info *cipher_list,
int cipher_count)
{
int i;
for (i = 0; i < cipher_count; ++i)
if (cipher == cipher_list[i].id)
return (1);
return (0);
}
int
is_ec_in_list(unsigned short ec, ec_info *ec_list, int ec_count)
{
int i;
for (i = 0; i < ec_count; ++i)
if (ec == ec_list[i].id)
return (1);
return (0);
}
int
test_connection(int rsock, SSL *ssl, BIO *rbio, BIO *wbio)
{
char msg[TEST_MSG_LEN + 1];
unsigned char buffer[BUFFER_SIZE];
int count;
int ret;
ret = recv_wait(rsock, rbio, 1, 0, 0, 0);
if (ret == RECV_WAIT_ERROR)
return (0);
else if (ret > 0) {
/* make sure we process alerts */
count = get_ssl_record(ssl, rsock, rbio, buffer, BUFFER_SIZE);
if (count > 0) /* we got an actual message */
return (1);
else if (count <= 0) /* disconnect or err */
return (0);
}
if (SSL_get_shutdown(ssl) & SSL_RECEIVED_SHUTDOWN)
return (0);
/* try sending it data to make sure the connection is still there */
strcpy(msg, TEST_MSG);
if (send_ssl_record(ssl, rsock, wbio, (unsigned char *)msg,
TEST_MSG_LEN))
return (1);
return (0);
}
int
do_tls_connection(int ssock, SSL *ssl, MUTATOR in_mut, void *in_state,
MUTATOR out_mut, void *out_state, int should_connect,
int bad_digest_expected)
{
BIO *bio_ssl_read;
BIO *bio_ssl_write;
int rsock;
int ret = 0;
write_out(PRINT_INFO, "Waiting for app to connect...");
if ((rsock = accept_connection(ssock, 0)) == -1) {
write_out(PRINT_INFO, "Unable to accept remote connection!");
return (0);
}
printf("doing tls connection\n");
bio_ssl_read = BIO_new(BIO_s_mem());
bio_ssl_write = BIO_new(BIO_s_mem());
SSL_set_bio(ssl, bio_ssl_read, bio_ssl_write);
switch (do_handshake(rsock, ssl, bio_ssl_read, bio_ssl_write, in_mut,
in_state, out_mut, out_state)) {
case HANDSHAKE_SUCCESSFUL:
if (test_connection(rsock, ssl, bio_ssl_read, bio_ssl_write)) {
write_out(PRINT_INFO, "SSL handshake successful!");
ret = should_connect ? 1 : 0;
} else {
write_out(PRINT_INFO, "Connection terminated after"
" successful SSL handshake.");
ret = should_connect ? 0 : 1;
}
break;
case HANDSHAKE_BAD_DIGEST:
if (bad_digest_expected) {
write_out(PRINT_INFO, "Bad digest expected..."
" ignoring.");
ret = should_connect ? 1 : 0;
} else {
write_out(PRINT_INFO, "SSL handshake failed!");
ret = should_connect ? 0 : 1;
}
break;
case HANDSHAKE_UNSUCCESSFUL:
write_out(PRINT_INFO, "SSL handshake failed!");
ret = should_connect ? 0 : 1;
break;
}
shutdown_ssl(ssl, rsock, bio_ssl_read, bio_ssl_write);
close(rsock);
printf("Finished doing tls connection\n");
return (ret);
}
unsigned char *
get_cipher_suites(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_1 *handshake;
TLS_extensions *tls_ext;
handshake_cipher_state *hstate = (handshake_cipher_state *) state;
data_buffer *db;
data_buffer ret;
unsigned short *cipher_suites;
unsigned short *supported_ec;
int cipher_suite_count;
int ec_count;
int i;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_1 *) (record + 1);
if (handshake->handshake_type ==
TLS_HANDSHAKE_CLIENT_HELLO) {
/* client hello */
hstate->is_restricted = 1;
cipher_suites =
ssl_get_cipher_suites(handshake);
/*
* this is the size, and each entry is 2 bytes
* long:
*/
cipher_suite_count = ntohs(*cipher_suites) / 2;
++cipher_suites;
for (i = 0; i < cipher_suite_count; ++i)
if (!is_cipher_in_list(
ntohs(cipher_suites[i]),
hstate->cipher_list,
hstate->cipher_count)) {
write_out(PRINT_INFO,
"Unapproved cipher suite"
" 0x%04hx is enabled.",
ntohs(cipher_suites[i]));
hstate->is_restricted = 0;
}
tls_ext = ssl_get_extension(ELLIPTIC_CURVE_EXT,
handshake);
/* elliptic curves extension */
if (tls_ext) {
supported_ec =
(unsigned short *)(tls_ext + 1);
ec_count = ntohs(*supported_ec) / 2;
++supported_ec;
for (i = 0; i < ec_count; ++i)
if (!is_ec_in_list(
ntohs(supported_ec[i]),
hstate->ec_list,
hstate->ec_count)) {
write_out(PRINT_INFO,
"Unapproved ellipti"
"c curve 0x%04hx is"
" enabled.", ntohs(
supported_ec[i]));
hstate->is_restricted =
0;
}
}
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_chosen_cipher(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_1 *handshake;
unsigned short *cipher_suite;
handshake_cipher_mod *hstate = (handshake_cipher_mod *) state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_1 *) (record + 1);
if (handshake->handshake_type ==
TLS_HANDSHAKE_SERVER_HELLO) {
/* server hello */
cipher_suite = ssl_get_cipher_suites(handshake);
write_out(PRINT_INFO, "Changing cipher suite "
"from 0x%04x to 0x%04x.",
ntohs(*cipher_suite), hstate->new_cipher);
*cipher_suite = htons(hstate->new_cipher);
hstate->success = 1;
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* we're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_chosen_cipher_term(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
handshake_cipher_mod *hstate = (handshake_cipher_mod *)state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE)
/* TLS handshake */
hstate->success = 0;
/*
* there should be no more handshakes after
* this is set
*/
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_version(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_1 *handshake;
tls_generic_success *hstate = (tls_generic_success *)state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_1 *) (record + 1);
if (handshake->handshake_type ==
TLS_HANDSHAKE_SERVER_HELLO) {
/* server hello */
write_out(PRINT_INFO, "Changing Server Hello"
" version from from 0x%04x to 0x%04x.",
ntohs(handshake->version),
FAKE_SSL_VERSION);
handshake->version = htons(FAKE_SSL_VERSION);
hstate->success = 1;
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_version_term(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
tls_generic_success *hstate = (tls_generic_success *)state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE)
/* TLS handshake */
hstate->success = 0;
/*
* There should be no more handshakes after
* this is set
*/
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* we're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_nonce(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_1 *handshake;
buffered_ssl_ctx *bssl = (buffered_ssl_ctx *)state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_1 *) (record + 1);
if (handshake->handshake_type ==
TLS_HANDSHAKE_SERVER_HELLO) {
/* server hello */
write_out(PRINT_INFO, "Changing last byte in "
"Server Hello nonce from from 0x%02x to"
" 0x%02x.",
(unsigned int)handshake->random[27],
(unsigned int)(handshake->random[27] + 1));
++handshake->random[27];
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_client_cert_request(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_generic *handshake;
buffered_ssl_ctx *bssl = (buffered_ssl_ctx *)state;
unsigned char *data_ptr;
data_buffer *db;
data_buffer ret;
unsigned short dname_length;
TLS_cert_req_disting_str *ptr_to_dstr;
TLS_rdn_sequence *ptr_to_rdn;
char *string;
unsigned int total;
unsigned int outer_total;
unsigned int length;
int good_name;
int good_org;
int good_org_unit;
if (!buffered_ssl_add_data(bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_generic *) (record + 1);
if (handshake->handshake_type ==
TLS_SERVER_CERT_REQUEST) {
/* cert request */
data_ptr =
ssl_cert_req_get_disting_names(
handshake);
dname_length = ntohs(*((unsigned short *)
data_ptr));
ptr_to_dstr =
(TLS_cert_req_disting_str *)
(data_ptr + 2);
for (outer_total = 0; outer_total
< dname_length; /* */) {
length = ntohs(ptr_to_dstr->length);
if (length >=
sizeof (TLS_cert_req_disting_str)) {
good_name = 0;
good_org = 0;
good_org_unit = 0;
ptr_to_rdn =
ptr_to_dstr->sequences;
for (total = 2; total
< length; /* */) {
/*
* find our fake
* cert
*/
string =
get_disting_string(
ptr_to_rdn);
if (string == 0) {
write_out(
PRINT_ERROR,
"Unable to"
" get dist"
"inguished"
" string");
break;
}
if (strcmp(FAKE_CA,
string) == 0)
good_name = 1;
else if (strcmp(
FAKE_ORG, string)
== 0)
good_org = 1;
else if (strcmp(
FAKE_UNIT, string)
== 0)
good_org_unit =
1;
free(string);
total += sizeof
(TLS_rdn_sequence) +
ptr_to_rdn->
str_size;
ptr_to_rdn =
(TLS_rdn_sequence *)
(((unsigned char *)
ptr_to_rdn) +
sizeof
(TLS_rdn_sequence)
+ ptr_to_rdn->
str_size);
}
if (good_name && good_org &&
good_org_unit) {
/*
* modify our fake
* cert
*/
ptr_to_rdn =
ptr_to_dstr->
sequences;
++ptr_to_rdn->string[0];
break;
}
outer_total += length + 2;
ptr_to_dstr =
(TLS_cert_req_disting_str *)
ptr_to_rdn;
} else
break;
}
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* we're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned short
get_weak_cipher(int cipher_count, unsigned short *cipher_list,
int weak_list_count, cipher_suite_info *weak_list)
{
int i;
int j;
int found;
for (i = 0; i < weak_list_count; ++i) {
found = 0;
for (j = 0; j < cipher_count; ++j)
if (weak_list[i].id == ntohs(cipher_list[j])) {
found = 1;
break;
}
if (!found)
return (weak_list[i].id);
}
return (0x0000);
}
unsigned char *
change_client_ciphers(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_1 *handshake;
handshake_enum_ciphers *hstate = (handshake_enum_ciphers *) state;
data_buffer *db;
data_buffer ret;
unsigned short *cipher_suites;
unsigned short cipher;
int cipher_suite_count;
int i;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
hstate->success = 0;
handshake = (TLS_handshake_1 *) (record + 1);
if (handshake->handshake_type ==
TLS_HANDSHAKE_CLIENT_HELLO) {
/* client hello */
cipher_suites = ssl_get_cipher_suites(
handshake);
cipher_suite_count = ntohs(*cipher_suites) / 2;
/*
* this is the size, and each entry is
* 2 bytes long
*/
++cipher_suites;
/*
* replace all entries with our weak cipher
* so that the server will be forced to
* select a weak cipher
*/
cipher = htons(get_weak_cipher(
cipher_suite_count, cipher_suites,
hstate->weak_list_count,
hstate->weak_list));
if (!cipher)
write_out(PRINT_WARNING, "Unable to "
"select find non-existant cipher."
" Defaulting to 0x0000.");
for (i = 0; i < cipher_suite_count; ++i)
cipher_suites[i] = cipher;
hstate->success = 1;
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* we're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_key_exch(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_1 *handshake;
handshake_key_exch *hstate = (handshake_key_exch *)state;
data_buffer *db;
data_buffer ret;
unsigned char *exch;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_1 *) (record + 1);
if (handshake->handshake_type == TLS_SERVER_KEY_EXCH) {
/* server key exchange */
/* find last byte */
exch = ((unsigned char *)handshake) +
ntohs(record->length) - 1;
/* tweak last byte */
write_out(PRINT_INFO, "Changing last byte in "
"server key exchange from from 0x%02x to "
"0x%02x.", (unsigned int)*exch,
(unsigned int)(*exch + 1));
*exch += 1;
hstate->success = 1;
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_key_exch_term(void *state, unsigned char *data, int len,
int *out_len)
{
TLS_record *record;
handshake_key_exch *hstate = (handshake_key_exch *)state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE)
/* TLS handshake */
hstate->success = 0;
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_fin(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
unsigned char *finished_data;
tls_change_cipher_spec *hstate = (tls_change_cipher_spec *)state;
data_buffer *db;
data_buffer ret;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_CHANGE_CIPHER_SPEC)
hstate->has_changed_cipher_spec = 1;
else if ((record->record_type == TLS_RECORD_HANDSHAKE) &&
(hstate->has_changed_cipher_spec)) {
/*
* this is the TLS Finished message according to the
* spec
* skip past the handshake type, length, and version
* fields
*/
finished_data = ((unsigned char *)(record + 1)) + 6;
/*
* skip past the handshake type, length, and version
* fields
*/
/* finished_data = ((unsigned char*)(record + 1)); */
write_out(PRINT_INFO, "Changing 6th byte in finished"
" message from 0x%02x to 0x%02x.",
(unsigned int)*finished_data,
(unsigned int)(*finished_data + 1));
*finished_data += 1;
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_scram_next(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
tls_change_cipher_spec *hstate = (tls_change_cipher_spec *)state;
data_buffer *db;
data_buffer ret;
unsigned short record_len;
int i;
if (!buffered_ssl_add_data(&hstate->bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(&hstate->bssl))) {
record = (TLS_record *) db->data;
if (hstate->has_changed_cipher_spec) {
write_out(PRINT_INFO, "Overriding record with random "
"data...");
record_len = ntohs(record->length) +
sizeof (TLS_record);
/* memset(db->data, 0, record_len); */
for (i = 0; i < record_len; ++i)
db->data[i] = rand();
hstate->has_changed_cipher_spec = 0;
} else if (record->record_type == TLS_CHANGE_CIPHER_SPEC)
hstate->has_changed_cipher_spec = 1;
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return
* the pieces
*/
*out_len = ret.length;
return (ret.data);
}
unsigned char *
mod_server_cert_start(void *state, unsigned char *data, int len, int *out_len)
{
TLS_record *record;
TLS_handshake_generic *handshake;
buffered_ssl_ctx *bssl = (buffered_ssl_ctx *)state;
data_buffer *db;
data_buffer ret;
unsigned char *pos;
unsigned int total_length;
if (!buffered_ssl_add_data(bssl, data, len)) {
write_out(PRINT_ERROR, "Unable to buffer TLS data. The "
"internal data stream is likely out of sync.");
return (0);
}
data_buffer_ctor(&ret);
ret.data = (unsigned char *)malloc(0);
while ((db = buffered_ssl_get_record(bssl))) {
record = (TLS_record *) db->data;
if (record->record_type == TLS_RECORD_HANDSHAKE) {
/* TLS handshake */
handshake = (TLS_handshake_generic *) (record + 1);
if (handshake->handshake_type ==
TLS_HANDSHAKE_CERTIFICATE) {
/* certificate */
/*
* the first certificate is the one that's
* suppose to validate the site
*/
total_length =
get_ssl_3_byte_number(handshake->length);
if (total_length > 6) {
pos = (unsigned char *)(handshake + 1);
/* skip the total cert length */
pos += 3;
write_out(PRINT_INFO, "Changing first"
" byte of the first certificate "
"from 0x%02x to 0x%02x.", pos[3],
pos[3] + 1);
++pos[3]; /* first byte of data */
}
}
}
data_buffer_merge(&ret, db);
data_buffer_dtor(db);
}
/*
* We're not going to destruct ret, but we're just going to return