This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
testclient.c
2044 lines (1804 loc) · 56.6 KB
/
testclient.c
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
// kssl_testclient.c: test program to communicate with a keyserver
//
// Copyright (c) 2013-2014 CloudFlare, Inc.
//
// Command-line options:
//
// --port
//
// TCP port to contact the kssl_server on (matches the --port parameter of
// kssl_server)
//
// --server
//
// Hostname or IP of the kssl_server.
//
// --client-cert
// --client-key
//
// The filenames of a client certificate to present to the server to verify
// that this client is a valid user of kssl_server. These must be signed by a
// CA that kssl_server can check the certificate against (i.e one in the
// --ca-file parameter of kssl_server).
//
// --ca-file
//
// Path to a PEM-encoded file containing the CA certificate used to verify
// server certificates presented on connection.
//
// --rsa-pubkey
//
// The filename of an RSA rsa_pubkey key file (PEM encoded) that is used for
// testing. This must be one of the rsa_pubkey keys specified in the
// kssl_server's --rsa_pubkey-key-directory.
//
// --debug
//
// Turns in debug logging
//
// --short
//
// Just do a connectivity test. Exit code 0 if successful.
//
// --alive
//
// Instead of performing all the tests this simply checks connectivity with
// the kssl_server by running a limit number of tests.
#include "kssl.h"
#include "kssl_helpers.h"
#include "kssl_private_key.h"
#if PLATFORM_WINDOWS
#include <winsock2.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
#endif
#include <ctype.h>
#include <uv.h>
#include <openssl/evp.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <sys/types.h>
#include <stdarg.h>
#include "kssl_getopt.h"
unsigned char ipv6[16] = {0x0, 0xf2, 0x13, 0x48, 0x43, 0x01};
unsigned char ipv4[4] = {127, 0, 0, 1};
#if PLATFORM_WINDOWS
#define SOCKET_CLOSE closesocket
#else
#define SOCKET_CLOSE close
#endif
// libuv locking primitives
#define MUTEX_TYPE uv_mutex_t
#define MUTEX_SETUP(x) uv_mutex_init(&(x))
#define MUTEX_CLEANUP(x) uv_mutex_destroy(&(x))
#define MUTEX_LOCK(x) uv_mutex_lock(&(x))
#define MUTEX_UNLOCK(x) uv_mutex_unlock(&(x))
#define THREAD_ID uv_thread_self()
struct hostent *localhost;
char *server = 0;
int tests = 0;
int debug = 0;
int health = 0;
int alive = 0;
// This array will store all of the mutexes available to OpenSSL.
static MUTEX_TYPE *mutex_buf=NULL;
static void locking_function(int mode, int n, const char * file, int line)
{
if (mode & CRYPTO_LOCK)
MUTEX_LOCK(mutex_buf[n]);
else
MUTEX_UNLOCK(mutex_buf[n]);
}
static unsigned long id_function(void)
{
return ((unsigned long)THREAD_ID);
}
int thread_setup(void)
{
int i;
mutex_buf = malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
if (!mutex_buf)
return 0;
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_SETUP(mutex_buf[i]);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
return 1;
}
int thread_cleanup(void)
{
int i;
if (!mutex_buf)
return 0;
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_CLEANUP(mutex_buf[i]);
free(mutex_buf);
mutex_buf = NULL;
return 1;
}
// ssl_error: call when a fatal SSL error occurs. Exits the program
// with return code 1.
void ssl_error(void)
{
ERR_print_errors_fp(stderr);
exit(1);
}
// fatal_error: call to print an error message to STDERR. Exits the
// program with return code 1.
void fatal_error(const char *fmt, ...)
{
va_list l;
va_start(l, fmt);
vfprintf(stderr, fmt, l);
va_end(l);
fprintf(stderr, "\n");
exit(1);
}
// digest_public_rsa: calculates the SHA256 digest of the
// hexadecimal representation of the public modulus of an RSA
// key. digest must be initialized with at least 32 bytes of
// space.
void digest_public_rsa(RSA *key, BYTE *digest)
{
// QUESTION: can we use a single EVP_MD_CTX for multiple
// digests?
char *hex;
EVP_MD_CTX *ctx;
ctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(ctx, EVP_sha256(), 0);
hex = BN_bn2hex(key->n);
EVP_DigestUpdate(ctx, hex, strlen(hex));
EVP_DigestFinal_ex(ctx, digest, 0);
EVP_MD_CTX_destroy(ctx);
OPENSSL_free(hex);
}
// digest_public_ec: calculates the SHA256 digest of the
// hexadecimal representation of the EC public key group and
// point. digest must be initialized with at least 32 bytes of
// space.
void digest_public_ec(EC_KEY *ec_key, BYTE *digest) {
const EC_POINT *ec_pub_key = EC_KEY_get0_public_key(ec_key);
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
char *hex = EC_POINT_point2hex(group, ec_pub_key, POINT_CONVERSION_COMPRESSED, NULL);
EVP_MD_CTX *ctx;
ctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(ctx, EVP_sha256(), 0);
EVP_DigestUpdate(ctx, hex, strlen(hex));
EVP_DigestFinal_ex(ctx, digest, 0);
EVP_MD_CTX_destroy(ctx);
OPENSSL_free(hex);
}
// ok: indicate that some tests passed and free memory
void ok(kssl_header *h)
{
if (!alive) {
printf(" ok\n");
}
if (h != 0) {
free(h->data);
free(h);
}
}
// test: start a set of test
void test(const char *fmt, ...)
{
if (!alive) {
va_list l;
va_start(l, fmt);
vfprintf(stderr, fmt, l);
va_end(l);
}
}
// test_assert: assert that some condition is true, fatal
// error if not
void test_assert(int a)
{
if (!a) {
fatal_error(" test failure");
}
tests += 1;
}
static void dump_request(kssl_operation *request) {
BYTE op;
BYTE *p = request->payload;
int l = request->payload_len;
if (!debug) {
return;
}
op = request->opcode;
printf("OPCODE: %s ", opstring(op));
if (op == KSSL_OP_RSA_DECRYPT) {
int i;
printf(" Digest: ");
for (i = 0; i < KSSL_DIGEST_SIZE; ++i) {
printf("%02x", request->digest[i]);
}
printf("\n");
}
if (op == KSSL_OP_ERROR && request->payload_len > 0) {
printf(" Error: ");
switch(request->payload[0]) {
case KSSL_ERROR_CRYPTO_FAILED:
printf("KSSL_ERROR_CRYPTO_FAILED\n");
break;
case KSSL_ERROR_KEY_NOT_FOUND:
printf("KSSL_ERROR_KEY_NOT_FOUND\n");
break;
case KSSL_ERROR_BAD_OPCODE:
printf("KSSL_ERROR_BAD_OPCODE\n");
break;
case KSSL_ERROR_READ:
printf("KSSL_ERROR_READ\n");
break;
case KSSL_ERROR_VERSION_MISMATCH:
printf("KSSL_ERROR_VERSION_MISMATCH\n");
break;
case KSSL_ERROR_UNEXPECTED_OPCODE:
printf("KSSL_ERROR_UNEXPECTED_OPCODE\n");
break;
default:
printf("unknown KSSL error: %02x\n", request->payload[0]);
break;
}
return;
}
if (l > 0) {
int printable = 1;
int i;
for (i = 0; i < l; i++) {
if (iscntrl(p[i])) {
printable = 0;
break;
}
}
printf(" Payload: ");
for (i = 0; i < l; ++i) {
if (!printable && (i != 0) && (i%16 == 0)) {
printf("\n ");
}
printf(printable?"%c":"%02x ", p[i]);
}
printf("\n");
}
}
// dump_payload: print out the payload from a KSSL operation in hex
void dump_payload(int l, BYTE *p)
{
kssl_operation request;
if (!debug) return;
if (l > 0) {
int i;
printf(" Payload Raw: ");
for (i = 0; i < l; ++i) {
if ((i != 0) && (i%16 == 0)) {
printf("\n ");
}
printf("%02x ", p[i]);
}
printf("\n");
}
parse_message_payload(p, l, &request);
dump_request(&request);
}
// dump_header: print out a KSSL header
void dump_header(kssl_header *k, const char *msg)
{
if (debug) {
printf(" KSSL %s: %02x %02x %08x %d\n", msg, k->version_maj, k->version_min,
k->id, k->length);
}
}
// kssl: send a KSSL message to the server and read the response
kssl_header *kssl(SSL *ssl, kssl_header *k, kssl_operation *r)
{
BYTE buf[KSSL_HEADER_SIZE];
BYTE *req;
int req_len;
int n;
kssl_header h;
kssl_header *to_return;
flatten_operation(k, r, &req, &req_len);
dump_header(k, "send");
dump_request(r);
n = SSL_write(ssl, req, req_len);
if (n != req_len) {
fatal_error("Failed to send KSSL header");
}
free(req);
while (1) {
n = SSL_read(ssl, buf, KSSL_HEADER_SIZE);
if (n <= 0) {
int x = SSL_get_error(ssl, n);
if (x == SSL_ERROR_WANT_READ || x == SSL_ERROR_WANT_WRITE) {
continue;
} else if (x == SSL_ERROR_ZERO_RETURN) {
fatal_error("Connection closed while reading header\n");
} else {
fatal_error("Error performing SSL_read: %x\n", x);
}
} else {
if (n != KSSL_HEADER_SIZE) {
fatal_error("Error receiving KSSL header, size: %d", n);
}
}
break;
}
parse_header(buf, &h);
if (h.version_maj != KSSL_VERSION_MAJ) {
fatal_error("Version mismatch %d != %d", h.version_maj, KSSL_VERSION_MAJ);
}
if (k->id != h.id) {
fatal_error("ID mismatch %08x != %08x", k->id, h.id);
}
dump_header(&h, "recv");
to_return = (kssl_header *)malloc(sizeof(kssl_header));
memcpy(to_return, &h, sizeof(kssl_header));
if (h.length > 0) {
BYTE *payload = (BYTE *)malloc(h.length);
while (1) {
n = SSL_read(ssl, payload, h.length);
if (n <= 0) {
int x = SSL_get_error(ssl, n);
if (x == SSL_ERROR_WANT_READ || x == SSL_ERROR_WANT_WRITE) {
continue;
} else if (x == SSL_ERROR_ZERO_RETURN) {
fatal_error("Connection closed while reading payload\n");
} else {
fatal_error("Error performing SSL_read: %x\n", x);
}
} else {
if (n != h.length) {
fatal_error("Error receiving KSSL payload, size: %d", n);
}
}
break;
}
if (n != h.length) {
fatal_error("Failed to read payload got length %d wanted %d", n, h.length);
}
dump_payload(h.length, payload);
to_return->data = payload;
}
return to_return;
}
void kssl_write(SSL *ssl, kssl_header *k, kssl_operation *r)
{
BYTE *req;
int req_len, n;
flatten_operation(k, r, &req, &req_len);
dump_header(k, "send");
dump_request(r);
n = SSL_write(ssl, req, req_len);
if (n != req_len) {
fatal_error("Failed to send KSSL header");
}
free(req);
}
kssl_header* kssl_read(SSL *ssl, kssl_header *k, kssl_operation *r)
{
kssl_header h, *to_return;
BYTE buf[KSSL_HEADER_SIZE];
int n;
while (1) {
n = SSL_read(ssl, buf, KSSL_HEADER_SIZE);
if (n <= 0) {
int x = SSL_get_error(ssl, n);
if (x == SSL_ERROR_WANT_READ || x == SSL_ERROR_WANT_WRITE) {
continue;
} else if (x == SSL_ERROR_ZERO_RETURN) {
fatal_error("Connection closed while reading header\n");
} else {
fatal_error("Error performing SSL_read: %x\n", x);
}
} else {
if (n != KSSL_HEADER_SIZE) {
fatal_error("Error receiving KSSL header, size: %d", n);
}
}
break;
}
parse_header(buf, &h);
if (h.version_maj != KSSL_VERSION_MAJ) {
fatal_error("Version mismatch %d != %d", h.version_maj, KSSL_VERSION_MAJ);
}
if (k->id != h.id) {
fatal_error("ID mismatch %08x != %08x", k->id, h.id);
}
dump_header(&h, "recv");
to_return = (kssl_header *)malloc(sizeof(kssl_header));
memcpy(to_return, &h, sizeof(kssl_header));
to_return->data = 0;
if (h.length > 0) {
BYTE *payload = (BYTE *)malloc(h.length);
while (1) {
n = SSL_read(ssl, payload, h.length);
if (n <= 0) {
int x = SSL_get_error(ssl, n);
if (x == SSL_ERROR_WANT_READ || x == SSL_ERROR_WANT_WRITE) {
continue;
} else if (x == SSL_ERROR_ZERO_RETURN) {
fatal_error("Connection closed while reading payload\n");
} else {
fatal_error("Error performing SSL_read: %x\n", x);
}
} else {
if (n != h.length) {
fatal_error("Error receiving KSSL payload, size: %d", n);
}
}
break;
}
if (n != h.length) {
fatal_error("Failed to read payload got length %d wanted %d", n, h.length);
}
dump_payload(h.length, payload);
to_return->data = payload;
}
return to_return;
}
// verify response, only verify kssl header currently
// TODO: verify different kssl payload
int verify_response(kssl_header* k, kssl_operation* r, kssl_header* resp_k)
{
test_assert(resp_k->id == k->id);
test_assert(resp_k->version_maj == KSSL_VERSION_MAJ);
// TODO: add other verification logic here
return 1;
}
// send and read pipeline requests and responses
void kssl_pipeline(SSL *ssl, kssl_header *k, kssl_operation *r, int repeat)
{
int i, cur_gap, max_gap = 300;
int w_count = 0, r_count = 0;
long int increment;
kssl_header *to_return;
srand((unsigned int) time(NULL));
while (w_count < repeat || r_count < repeat) {
cur_gap = w_count - r_count;
// pipeline write
increment = (random() % max_gap);
if ((increment + cur_gap) > max_gap) {
increment = max_gap - cur_gap;
}
if ((increment + w_count) > repeat) {
increment = repeat - w_count;
}
for (i = 0; i < increment; i++) {
kssl_write(ssl, k, r);
w_count += 1;
}
// pipeline read
increment = (random() % max_gap);
if ((r_count + increment) > w_count) {
increment = w_count - r_count;
}
for (i = 0; i < increment; i++) {
to_return = kssl_read(ssl, k, r);
verify_response(k, r, to_return);
free(to_return->data);
free(to_return);
r_count += 1;
}
}
}
typedef struct {
SSL *ssl;
int fd;
} connection;
void kssl_bad_opcode(connection *c)
{
kssl_header bad;
kssl_operation req, resp;
kssl_header *h;
test("Bad KSSL opcode (%p)", c);
bad.version_maj = KSSL_VERSION_MAJ;
bad.version_min = KSSL_VERSION_MIN;
bad.id = 0x12345678;
bad.length = 0; // to be overridden by serialization
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = 0xBB;
req.payload_len = 0;
h = kssl(c->ssl, &bad, &req);
test_assert(h->id == bad.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_ERROR);
test_assert(resp.payload_len == 1);
test_assert(resp.payload[0] == KSSL_ERROR_BAD_OPCODE);
ok(h);
}
void kssl_op_pong(connection *c)
{
kssl_header echo0;
kssl_operation req, resp;
kssl_header *h;
test("KSSL_OP_PONG (%p)", c);
echo0.version_maj = KSSL_VERSION_MAJ;
echo0.version_min = KSSL_VERSION_MIN;
echo0.id = 0x12345678;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_PONG;
req.payload_len = 0;
h = kssl(c->ssl, &echo0, &req);
test_assert(h->id == echo0.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_ERROR);
test_assert(resp.payload_len == 1);
test_assert(resp.payload[0] == KSSL_ERROR_UNEXPECTED_OPCODE);
ok(h);
}
void kssl_op_error(connection *c)
{
kssl_header echo0;
kssl_operation req, resp;
kssl_header *h;
test("KSSL_OP_ERROR (%p)", c);
echo0.version_maj = KSSL_VERSION_MAJ;
echo0.id = 0x12345678;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_ERROR;
req.payload_len = 0;
h = kssl(c->ssl, &echo0, &req);
test_assert(h->id == echo0.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_ERROR);
test_assert(resp.payload_len == 1);
test_assert(resp.payload[0] == KSSL_ERROR_UNEXPECTED_OPCODE);
ok(h);
}
void kssl_op_ping_no_payload(connection *c)
{
kssl_header echo0;
kssl_operation req, resp;
kssl_header *h;
test("KSSL_OP_PING with no payload (%p)", c);
echo0.version_maj = KSSL_VERSION_MAJ;
echo0.id = 0x12345678;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_PING;
req.payload_len = 0;
h = kssl(c->ssl, &echo0, &req);
test_assert(h->id == echo0.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_PONG);
test_assert(resp.payload_len == 0);
ok(h);
}
void kssl_op_ping_payload(connection *c)
{
const char *hello = "Hello, World!";
kssl_operation req, resp;
kssl_header echo1;
kssl_header *h;
BYTE *payload;
test("KSSL_OP_PING with payload (%p)", c);
payload = malloc(strlen(hello) + 1);
echo1.version_maj = KSSL_VERSION_MAJ;
echo1.id = 0x12345679;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_PING;
req.payload_len = strlen(hello);
req.payload = payload;
memcpy((char *)payload, hello, strlen(hello));
h = kssl(c->ssl, &echo1, &req);
test_assert(h->id == echo1.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_PONG);
test_assert(resp.payload_len == req.payload_len);
test_assert(strncmp((char *)resp.payload, (char *)req.payload, strlen(hello)) == 0);
ok(h);
free(payload);
}
void kssl_repeat_op_ping(connection *c, int repeat)
{
char hello[255];
kssl_header echo1;
kssl_operation req, resp;
kssl_header *h;
int i;
BYTE *payload = malloc(255 + 1);
test("Repeat KSSL_OP_PING %d times (%p)", repeat, c);
echo1.version_maj = KSSL_VERSION_MAJ;
echo1.id = 0x12345679;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_PING;
req.payload_len = 255 + 1;
req.payload = payload;
for (i = 0; i < repeat; i++) {
sprintf(hello, "Hello, World! %d", i);
memcpy((char *)payload, hello, strlen(hello)+1);
req.payload_len = strlen(hello)+1;
h = kssl(c->ssl, &echo1, &req);
test_assert(h->id == echo1.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_PONG);
test_assert(resp.payload_len == req.payload_len);
test_assert(strncmp((char *)resp.payload, (char *)req.payload, strlen(hello)) == 0);
free(h->data);
free(h);
}
ok(0);
free(payload);
}
void kssl_pipeline_op_ping(connection *c, int repeat)
{
char hello[255];
kssl_header echo1;
kssl_operation req;
BYTE *payload = malloc(255 + 1);
test("Pipeline KSSL_OP_PING %d times (%p)", repeat, c);
echo1.version_maj = KSSL_VERSION_MAJ;
echo1.id = 0x12345679;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_PING;
req.payload_len = 255 + 1;
req.payload = payload;
sprintf(hello, "Hello, World! Pipeline");
memcpy((char *)payload, hello, strlen(hello)+1);
req.payload_len = strlen(hello)+1;
kssl_pipeline(c->ssl, &echo1, &req, repeat);
ok(0);
free(payload);
}
void kssl_op_ping_bad_version(connection *c)
{
kssl_header echo0;
kssl_operation req, resp;
kssl_header *h;
test("KSSL_OP_PING with bad version (%p)", c);
echo0.id = 0x12345678;
echo0.version_maj = KSSL_VERSION_MAJ+1;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.opcode = KSSL_OP_PING;
req.payload_len = 0;
h = kssl(c->ssl, &echo0, &req);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
test_assert(h->id == echo0.id);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_ERROR);
test_assert(resp.payload_len == 1);
test_assert(resp.payload[0] == KSSL_ERROR_VERSION_MISMATCH);
ok(h);
}
void kssl_op_rsa_decrypt(connection *c, RSA *rsa_pubkey)
{
static int count = 0;
char kryptos2[255];
kssl_header decrypt;
kssl_operation req, resp;
kssl_header *h;
int size;
test("KSSL_OP_RSA_DECRYPT (%p)", c);
decrypt.version_maj = KSSL_VERSION_MAJ;
decrypt.id = 0x1234567a;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.is_digest_set = 1;
req.is_ip_set = 1;
req.ip = ipv6;
req.ip_len = 16;
req.payload = malloc(RSA_size(rsa_pubkey));
req.payload_len = RSA_size(rsa_pubkey);
req.digest = malloc(KSSL_DIGEST_SIZE);
digest_public_rsa(rsa_pubkey, req.digest);
req.opcode = KSSL_OP_RSA_DECRYPT;
sprintf(kryptos2, "%02x It was totally invisible, how's that possible?", count);
count += 1;
size = RSA_public_encrypt(strlen(kryptos2), (unsigned char *)kryptos2,
(unsigned char *)req.payload,
rsa_pubkey, RSA_PKCS1_PADDING);
if (size == -1) {
fatal_error("Failed to RSA encrypt");
}
h = kssl(c->ssl, &decrypt, &req);
test_assert(h->id == decrypt.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_RESPONSE);
test_assert(resp.payload_len == strlen(kryptos2));
test_assert(strncmp((char *)resp.payload, kryptos2, strlen(kryptos2)) == 0);
ok(h);
free(req.payload);
free(req.digest);
}
void kssl_op_rsa_decrypt_raw(connection *c, RSA *rsa_pubkey)
{
static int count = 0;
char kryptos2[255];
char unpadded_resp[255];
int unpadded_len;
kssl_header decrypt;
kssl_operation req, resp;
kssl_header *h;
int size;
test("KSSL_OP_RSA_DECRYPT_RAW (%p)", c);
decrypt.version_maj = KSSL_VERSION_MAJ;
decrypt.id = 0x1234567a;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.is_digest_set = 1;
req.is_ip_set = 1;
req.ip = ipv6;
req.ip_len = 16;
req.payload = malloc(RSA_size(rsa_pubkey));
req.payload_len = RSA_size(rsa_pubkey);
req.digest = malloc(KSSL_DIGEST_SIZE);
digest_public_rsa(rsa_pubkey, req.digest);
req.opcode = KSSL_OP_RSA_DECRYPT_RAW;
sprintf(kryptos2, "%02x It was totally invisible, how's that possible?", count);
count += 1;
size = RSA_public_encrypt(strlen(kryptos2), (unsigned char *)kryptos2,
(unsigned char *)req.payload,
rsa_pubkey, RSA_PKCS1_PADDING);
if (size == -1) {
fatal_error("Failed to RSA encrypt");
}
h = kssl(c->ssl, &decrypt, &req);
test_assert(h->id == decrypt.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_RESPONSE);
test_assert(resp.payload_len == RSA_size(rsa_pubkey));
test_assert(*(resp.payload++) == 0);
unpadded_len = RSA_padding_check_PKCS1_type_2((unsigned char *)unpadded_resp,
sizeof(unpadded_resp),
resp.payload,
resp.payload_len-1,
RSA_size(rsa_pubkey));
test_assert(unpadded_len == (int)strlen(kryptos2));
test_assert(strncmp(unpadded_resp, kryptos2, strlen(kryptos2)) == 0);
ok(h);
free(req.payload);
free(req.digest);
}
#define ALGS_COUNT 6
// RSA signing algorithm opcodes
static int rsa_algs[ALGS_COUNT] = {
KSSL_OP_RSA_SIGN_MD5SHA1,
KSSL_OP_RSA_SIGN_SHA1,
KSSL_OP_RSA_SIGN_SHA224,
KSSL_OP_RSA_SIGN_SHA256,
KSSL_OP_RSA_SIGN_SHA384,
KSSL_OP_RSA_SIGN_SHA512,
};
// ECDSA signing algorithm opcodes
static int ecdsa_algs[ALGS_COUNT] = {
KSSL_OP_ECDSA_SIGN_MD5SHA1,
KSSL_OP_ECDSA_SIGN_SHA1,
KSSL_OP_ECDSA_SIGN_SHA224,
KSSL_OP_ECDSA_SIGN_SHA256,
KSSL_OP_ECDSA_SIGN_SHA384,
KSSL_OP_ECDSA_SIGN_SHA512,
};
// OpenSSL digest NIDs
static int nid[ALGS_COUNT] = {
NID_md5_sha1,
NID_sha1,
NID_sha224,
NID_sha256,
NID_sha384,
NID_sha512,
};
// These are totally bogus but they have the right lengths (and, anyway, who's to say these aren't real
// message digests?)
static char* digests[ALGS_COUNT] = {
"123456789012345678901234567890123456", // MD5SH1 is 36 bytes
"12345678901234567890", // SHA1 is 20 bytes
"1234567890123456789012345678", // SHA224 is 28 bytes
"12345678901234567890123456789012", // SHA256 is 32 bytes
"123456789012345678901234567890123456789012345678", // SHA384 is 48 bytes
"1234567890123456789012345678901234567890123456789012345678901234", // SHA512 is 64 bytes
};
void kssl_op_rsa_sign(connection *c, RSA *rsa_pubkey, int opcode)
{
int i, rc;
kssl_header *h;
test("KSSL_OP_RSA_SIGN_* (%p)", c);
for (i = 0; i < ALGS_COUNT; i++) {
kssl_header sign;
kssl_operation req, resp;
if (opcode != rsa_algs[i] && opcode != 0) continue;
sign.version_maj = KSSL_VERSION_MAJ;
sign.id = 0x1234567a;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.is_digest_set = 1;
req.is_ip_set = 1;
req.ip = ipv4;
req.ip_len = 4;
req.digest = malloc(KSSL_DIGEST_SIZE);
digest_public_rsa(rsa_pubkey, req.digest);
req.payload = (BYTE *)digests[i];
req.payload_len = strlen(digests[i]);
req.opcode = rsa_algs[i];
h = kssl(c->ssl, &sign, &req);
test_assert(h->id == sign.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_RESPONSE);
rc = RSA_verify(nid[i], (unsigned char *)digests[i], strlen(digests[i]), resp.payload, resp.payload_len, rsa_pubkey);
test_assert(rc == 1);
free(h);
free(req.digest);
}
ok(0);
}
void kssl_op_ecdsa_sign(connection *c, EC_KEY *ecdsa_pubkey, int opcode)
{
int i, rc;
kssl_header *h;
test("KSSL_OP_ECDSA_SIGN_* (%p)", c);
for (i = 0; i < ALGS_COUNT; i++) {
kssl_header sign;
kssl_operation req, resp;
if (opcode != ecdsa_algs[i] && opcode != 0) continue;
sign.version_maj = KSSL_VERSION_MAJ;
sign.id = 0x1234567a;
zero_operation(&req);
req.is_opcode_set = 1;
req.is_payload_set = 1;
req.is_digest_set = 1;
req.is_ip_set = 1;
req.ip = ipv4;
req.ip_len = 4;
req.digest = malloc(KSSL_DIGEST_SIZE);
digest_public_ec(ecdsa_pubkey, req.digest);
req.payload = (BYTE *)digests[i];
req.payload_len = strlen(digests[i]);
req.opcode = ecdsa_algs[i];
h = kssl(c->ssl, &sign, &req);
test_assert(h->id == sign.id);
test_assert(h->version_maj == KSSL_VERSION_MAJ);
parse_message_payload(h->data, h->length, &resp);
test_assert(resp.opcode == KSSL_OP_RESPONSE);
rc = ECDSA_verify(nid[i], (unsigned char *)digests[i], strlen(digests[i]), resp.payload, resp.payload_len, ecdsa_pubkey);
test_assert(rc == 1);
free(h);
free(req.digest);