-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrypt.cpp
1946 lines (1748 loc) · 57.5 KB
/
crypt.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
/*****************************************************************************\
** **
** PBX4Linux **
** **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg **
** **
** cryption stuff **
** **
\*****************************************************************************/
/* how authentication is performed:
Alice Bob
-----------------------------------------------------------------
unencrypted call
start *
| -----> sending random number -----> (gets lost)
|
| * start
comparing | <----- sending random number <----- |
master state | |
| -----> sending "slave" code -----> | slave state
calculating | |
rsa key pair | |
| |
done | -----> sending public key -----> |
| | crpyting
| | session key
| |
| <----- sending session key <----- | done
decrypting | | enable crypt
dession key | * stop
|
done |
enable crypt |
stop *
encrypted call
When Bob and Alice activate their authentication process, they will make
up a random number.
Lets assume Alice starts encryption first. She activates the authentication
process. Bob hat not activates the process yet. Alice sends a random number
to Bob, but he will ignore it, because he is not listening.
Bob also activates the authentication process, and sends his random number.
Now Alice will receive that number and compares the values. If the values
are equal, the process will fail and Alice will send the message "LOOPED".
If Alice's random number is greater, she will identify herself as "master".
Bob will get a "SLAVE" message. Bob might also send a "MASTER" message,
if he got Alice's random number, due to parallel activation of the
authentication.
After transmission or reception of a master message, more messages are
ignored. After reception of a "RANDOM" message, more messages are
ignored. A reception of a "RANDOM" message always causes to identify who
is slave.
Now Alice starts calculating his public/private key pair, because she is
"master". When Bob receives the "SLAVE" message, he will change the timeout
value. If no random number or "SLAVE", "MASTER" or "LOOPED" is received within
a timeout value, the "ABORT" message is sent. If the "ABORT" message is
received at any state of the process, the process is aborted.
After the key of Alices is calculated, she will send it to Bob. Bob will use
the key to start encryption of a random session key. Both will change their
timeout values.
After Bob has finished is crypted session key, he will send it to Alice and
enable encryption. Bob has finished his process now.
As soon as Alice received the encrypted key, she will encrypt it and also
enable encryption with the same key as Bob. Alis has finished her process now.
Both will get displayed some of the first digits of the public key. Both can
talk about the digits now. A man in the middle cannot change the keys without
changing the public key. The voice of Alice and/or Bob is used to "sign" and
"check" that the public key is not modified.
If Alice or Bob wants to stop encryption, one will send the "ABORT" message.
After transmission or reception.
The states of the process:
CM_ST_NULL
----------
no encryption
CM_ST_IDENT
-----------
Waiting for the remote random number or "MASTER", "SLAVE", "LOOPED" message.
CM_ST_KEYGEN
------------
The master generates the key and waits for the key engine to finish.
CM_ST_KEYWAIT
-------------
The slave waits for the master to send the key.
CM_ST_CSKEY
-----------
The slave generates the session key and waits for the encryption engine to
finish.
CM_ST_CSWAIT
------------
The master waits for the slave to send the crypted session key.
CM_ST_DSKEY
-----------
The master waits for the decryption engine to finish decryption of the session
key.
CM_ST_ACTIVE
------------
The encryption is established.
Timouts
-------
CM_TO_IDENT = waiting for the remote party to enable encryption
CM_TO_KEY = waiting for key generation
CM_TO_CSKEY = waiting for session key encryption
CM_TO_DSKEY = waiting for session key decryption
Structure of message:
---------------------
one octet message element
two octets element length (first octet = high-byte)
data as given in length
last element is 0
the message type is encoded as element
*/
#include "main.h"
#ifdef CRYPTO
#include <openssl/rsa.h>
#endif
/* convert key string to binary key vector
* returns 0 if an error ocurred
*/
unsigned char *crypt_key(unsigned char *key, int *binary_len)
{
static unsigned char binary_key[2048];
int i = 0;
binary_key[0] = '\0';
if (!key[0])
return(NULL);
/* check for 0xXXXX... type of key */
if (!strncmp((char *)key, "0x", 2)) {
key+=2;
while(*key) {
if (i == (int)sizeof(binary_key))
return(NULL);
if (*key>='0' && *key<='9')
binary_key[i] = (*key-'0') << 8;
else if (*key>='a' && *key<='f')
binary_key[i] = (*key-'a'+10) << 8;
else if (*key>='A' && *key<='F')
binary_key[i] = (*key-'A'+10) << 8;
else
return(NULL);
key++;
if (*key>='0' && *key<='9')
binary_key[i] += (*key - '0');
else if (*key>='a' && *key<='f')
binary_key[i] += (*key - 'a' + 10);
else if (*key>='A' && *key<='F')
binary_key[i] += (*key - 'A' + 10);
else
return(NULL);
key++;
i++;
}
*binary_len = i;
return(binary_key);
}
/* ascii key too long */
if (strlen((char *)key) >= sizeof((char *)binary_key))
return(NULL);
memcpy(binary_key, key, strlen((char *)key));
*binary_len = strlen((char *)key);
return(binary_key);
}
/*
* support routine to get cpu speed
*/
static unsigned int get_bogomips(void)
{
FILE *fp;
char buffer[64], *p;
fp = fopen("/proc/cpuinfo", "r");
if (!fp) {
PERROR("Cannot access /proc/cpuinfo. Will not use cpuinfo for identification of pear\n");
return(0);
}
fduse++;
buffer[sizeof(buffer-1)] = '\0';
while(GETLINE(buffer, fp)) {
if (!!strncmp(buffer, "bogomips", 8))
continue;
if (!strchr(buffer, ':'))
continue;
p = strchr(buffer, ':')+1;
while(*p == ' ')
p++;
if (strchr(p, '.'))
*strchr(p, '.') = '\0';
fclose(fp);
fduse--;
return(atoi(p));
}
fclose(fp);
fduse--;
PERROR("Cannot find 'bogomips' in /proc/cpuinfo. Will not use cpuinfo for identification of pear\n");
return(0);
}
/*
* crc 32 stuff
*/
static unsigned int crc_reflect(unsigned int ref, char ch)
{
unsigned int value = 0;
int i;
i = 1;
while(i < ch+1) {
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
i++;
}
return(value);
}
static unsigned int crc32_table[256];
static int crc_initialized = 0;
void crc_init(void)
{
unsigned int ulPolynomial = 0x04c11db7;
int i, j;
i = 0;
while(i < 256) {
crc32_table[i] = crc_reflect(i, 8) << 24;
j = 0;
while(j < 8) {
crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
j++;
}
crc32_table[i] = crc_reflect(crc32_table[i], 32);
i++;
}
crc_initialized = 1;
}
unsigned int crc32(unsigned char *data, int len)
{
unsigned int crc = 0xffffffff;
if (!crc_initialized)
FATAL("crc not initialized, exitting...");
while (len--)
crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ *data++];
return(crc^0xffffffff);
}
CM_ST_NAMES
/* give name of state */
static const char *statename(int state)
{
if (state>=0 && state<cm_st_num)
return(cm_st_name[state]);
return("<<STATE UNKNOWN>>");
}
/*
* authentication key generation, encryption, decryption
*/
struct auth_args {
class EndpointAppPBX *apppbx;
int job;
};
static void *keyengine_child(void *arg)
{
struct auth_args *args = (struct auth_args *)arg;
class EndpointAppPBX *apppbx = args->apppbx;
int job = args->job;
#ifdef CRYPTO
RSA *rsa;
int exponent;
int i;
#endif
struct sched_param schedp;
int ret;
PDEBUG((DEBUG_EPOINT | DEBUG_CRYPT), "EPOINT(%d) child process started for using libcrypto\n", apppbx->ea_endpoint->ep_serial);
/* lower priority to keep pbx running fluently */
if (options.schedule > 0) {
memset(&schedp, 0, sizeof(schedp));
schedp.sched_priority = 0;
ret = sched_setscheduler(0, SCHED_OTHER, &schedp);
if (ret < 0) {
PERROR("Scheduling to normal priority failed (errno = %d).\nExitting child process...\n", errno);
goto done;
}
}
switch(job) {
/* generate rsa key pair */
case CK_GENRSA_REQ:
#ifndef CRYPTO
PERROR("Not compliled wiht crypto.\n");
apppbx->e_crypt_keyengine_return = -1;
#else
srandom(*((unsigned int *)mISDN_rand) ^ random());
// exponent = (((random()<<1)|1) & 0x7f) + 0x80; /* odd */
exponent = 65537;
// if (exponent < 3) exponent = 3; /* >= 3 */
rsa = RSA_generate_key(RSA_BITS, exponent, NULL, NULL);
if (!rsa) {
PERROR("Failed to generate rsa key pair.\n");
apppbx->e_crypt_keyengine_return = -1;
break;
}
ememuse++;
apppbx->e_crypt_rsa_n_len = BN_num_bytes(rsa->n);
if (apppbx->e_crypt_rsa_n_len > (int)sizeof(apppbx->e_crypt_rsa_n)) {
kerror_buffer:
PERROR("e_crypt_rsa_* too small for bignum.\n");
apppbx->e_crypt_keyengine_return = -1;
RSA_free(rsa);
ememuse--;
break;
}
BN_bn2bin(rsa->n, apppbx->e_crypt_rsa_n);
apppbx->e_crypt_rsa_n_len = BN_num_bytes(rsa->n);
if (apppbx->e_crypt_rsa_e_len > (int)sizeof(apppbx->e_crypt_rsa_e))
goto kerror_buffer;
BN_bn2bin(rsa->e, apppbx->e_crypt_rsa_e);
apppbx->e_crypt_rsa_e_len = BN_num_bytes(rsa->e);
if (apppbx->e_crypt_rsa_d_len > (int)sizeof(apppbx->e_crypt_rsa_d))
goto kerror_buffer;
BN_bn2bin(rsa->d, apppbx->e_crypt_rsa_d);
apppbx->e_crypt_rsa_p_len = BN_num_bytes(rsa->p);
if (apppbx->e_crypt_rsa_p_len > (int)sizeof(apppbx->e_crypt_rsa_p))
goto kerror_buffer;
BN_bn2bin(rsa->p, apppbx->e_crypt_rsa_p);
apppbx->e_crypt_rsa_q_len = BN_num_bytes(rsa->q);
if (apppbx->e_crypt_rsa_q_len > (int)sizeof(apppbx->e_crypt_rsa_q))
goto kerror_buffer;
BN_bn2bin(rsa->q, apppbx->e_crypt_rsa_q);
apppbx->e_crypt_rsa_dmp1_len = BN_num_bytes(rsa->dmp1);
if (apppbx->e_crypt_rsa_dmp1_len > (int)sizeof(apppbx->e_crypt_rsa_dmp1))
goto kerror_buffer;
BN_bn2bin(rsa->dmp1, apppbx->e_crypt_rsa_dmp1);
apppbx->e_crypt_rsa_dmq1_len = BN_num_bytes(rsa->dmq1);
if (apppbx->e_crypt_rsa_dmq1_len > (int)sizeof(apppbx->e_crypt_rsa_dmq1))
goto kerror_buffer;
BN_bn2bin(rsa->dmq1, apppbx->e_crypt_rsa_dmq1);
apppbx->e_crypt_rsa_iqmp_len = BN_num_bytes(rsa->iqmp);
if (apppbx->e_crypt_rsa_iqmp_len > (int)sizeof(apppbx->e_crypt_rsa_iqmp))
goto kerror_buffer;
BN_bn2bin(rsa->iqmp, apppbx->e_crypt_rsa_iqmp);
PDEBUG(DEBUG_CRYPT, "gen: rsa n=%02x...\n", *apppbx->e_crypt_rsa_n);
PDEBUG(DEBUG_CRYPT, "gen: rsa e=%02x...\n", *apppbx->e_crypt_rsa_e);
PDEBUG(DEBUG_CRYPT, "gen: rsa d=%02x...\n", *apppbx->e_crypt_rsa_d);
PDEBUG(DEBUG_CRYPT, "gen: rsa p=%02x...\n", *apppbx->e_crypt_rsa_p);
PDEBUG(DEBUG_CRYPT, "gen: rsa q=%02x...\n", *apppbx->e_crypt_rsa_q);
PDEBUG(DEBUG_CRYPT, "gen: rsa dmp1=%02x...\n", *apppbx->e_crypt_rsa_dmp1);
PDEBUG(DEBUG_CRYPT, "gen: rsa dmq1=%02x...\n", *apppbx->e_crypt_rsa_dmq1);
PDEBUG(DEBUG_CRYPT, "gen: rsa iqmp=%02x...\n", *apppbx->e_crypt_rsa_iqmp);
apppbx->e_crypt_keyengine_return = 1;
RSA_free(rsa);
ememuse--;
#endif
break;
/* encrypt session key */
case CK_CPTRSA_REQ:
#ifndef CRYPTO
PERROR("No crypto lib.\n");
apppbx->e_crypt_keyengine_return = -1;
#else
/* generating session key */
srandom(*((unsigned int *)mISDN_rand) ^ random());
i = 0;
while(i < 56) {
apppbx->e_crypt_key[i] = random();
apppbx->e_crypt_key[i] ^= mISDN_rand[random() & 0xff];
i++;
}
apppbx->e_crypt_key_len = i;
/* encrypt via rsa */
rsa = RSA_new();
if (!rsa) {
PERROR("Failed to allocate rsa structure.\n");
apppbx->e_crypt_keyengine_return = 1;
break;
}
ememuse++;
rsa->n = BN_new();
rsa->e = BN_new();
if (!rsa->n || !rsa->e) {
PERROR("Failed to generate rsa structure.\n");
apppbx->e_crypt_keyengine_return = -1;
RSA_free(rsa);
ememuse--;
break;
}
if (!BN_bin2bn(apppbx->e_crypt_rsa_n, apppbx->e_crypt_rsa_n_len, rsa->n)) {
eerror_bin2bn:
PERROR("Failed to convert binary to bignum.\n");
apppbx->e_crypt_keyengine_return = -1;
RSA_free(rsa);
ememuse--;
break;
}
if ((apppbx->e_crypt_rsa_n_len*8) != BN_num_bits(rsa->n)) {
PERROR("SOFTWARE API ERROR: length not equal stored data. (%d != %d)\n", apppbx->e_crypt_rsa_n_len*8, BN_num_bits(rsa->n));
apppbx->e_crypt_keyengine_return = -1;
RSA_free(rsa);
ememuse--;
break;
}
if (!BN_bin2bn(apppbx->e_crypt_rsa_e, apppbx->e_crypt_rsa_e_len, rsa->e))
goto eerror_bin2bn;
PDEBUG(DEBUG_CRYPT, "crypt: rsa n=%02x...\n", *apppbx->e_crypt_rsa_n);
PDEBUG(DEBUG_CRYPT, "crypt: rsa e=%02x...\n", *apppbx->e_crypt_rsa_e);
PDEBUG(DEBUG_CRYPT, "crypt: key =%02x%02x%02x%02x... (len=%d)\n", apppbx->e_crypt_key[0], apppbx->e_crypt_key[1], apppbx->e_crypt_key[2], apppbx->e_crypt_key[3], apppbx->e_crypt_key_len);
apppbx->e_crypt_ckey_len = RSA_public_encrypt(
apppbx->e_crypt_key_len,
apppbx->e_crypt_key,
apppbx->e_crypt_ckey,
rsa,
RSA_PKCS1_PADDING);
PDEBUG(DEBUG_CRYPT, "crypt: ckey =%02x%02x%02x%02x... (len=%d)\n", apppbx->e_crypt_ckey[0], apppbx->e_crypt_ckey[1], apppbx->e_crypt_ckey[2], apppbx->e_crypt_ckey[3], apppbx->e_crypt_ckey_len);
RSA_free(rsa);
ememuse--;
if (apppbx->e_crypt_ckey_len > 0)
apppbx->e_crypt_keyengine_return = 1;
else
apppbx->e_crypt_keyengine_return = -1;
#endif
break;
/* decrypt session key */
case CK_DECRSA_REQ:
#ifndef CRYPTO
PERROR("No crypto lib.\n");
apppbx->e_crypt_keyengine_return = -1;
#else
rsa = RSA_new();
if (!rsa) {
PERROR("Failed to allocate rsa structure.\n");
apppbx->e_crypt_keyengine_return = 1;
break;
}
ememuse++;
rsa->n = BN_new();
rsa->e = BN_new();
rsa->d = BN_new();
rsa->p = BN_new();
rsa->q = BN_new();
rsa->dmp1 = BN_new();
rsa->dmq1 = BN_new();
rsa->iqmp = BN_new();
if (!rsa->n || !rsa->e
|| !rsa->d || !rsa->p
|| !rsa->q || !rsa->dmp1
|| !rsa->dmq1 || !rsa->iqmp) {
PERROR("Failed to generate rsa structure.\n");
apppbx->e_crypt_keyengine_return = 1;
RSA_free(rsa);
ememuse--;
break;
}
if (!BN_bin2bn(apppbx->e_crypt_rsa_n, apppbx->e_crypt_rsa_n_len, rsa->n)) {
derror_bin2bn:
PERROR("Failed to convert binary to bignum.\n");
apppbx->e_crypt_keyengine_return = -1;
RSA_free(rsa);
ememuse--;
break;
}
if (!BN_bin2bn(apppbx->e_crypt_rsa_e, apppbx->e_crypt_rsa_e_len, rsa->e))
goto derror_bin2bn;
if (!BN_bin2bn(apppbx->e_crypt_rsa_d, apppbx->e_crypt_rsa_d_len, rsa->d))
goto derror_bin2bn;
if (!BN_bin2bn(apppbx->e_crypt_rsa_p, apppbx->e_crypt_rsa_p_len, rsa->p))
goto derror_bin2bn;
if (!BN_bin2bn(apppbx->e_crypt_rsa_q, apppbx->e_crypt_rsa_q_len, rsa->q))
goto derror_bin2bn;
if (!BN_bin2bn(apppbx->e_crypt_rsa_dmp1, apppbx->e_crypt_rsa_dmp1_len, rsa->dmp1))
goto derror_bin2bn;
if (!BN_bin2bn(apppbx->e_crypt_rsa_dmq1, apppbx->e_crypt_rsa_dmq1_len, rsa->dmq1))
goto derror_bin2bn;
if (!BN_bin2bn(apppbx->e_crypt_rsa_iqmp, apppbx->e_crypt_rsa_iqmp_len, rsa->iqmp))
goto derror_bin2bn;
PDEBUG(DEBUG_CRYPT, "decrypt: ckey =%02x%02x%02x%02x... (len=%d)\n", apppbx->e_crypt_ckey[0], apppbx->e_crypt_ckey[1], apppbx->e_crypt_ckey[2], apppbx->e_crypt_ckey[3], apppbx->e_crypt_ckey_len);
apppbx->e_crypt_key_len = RSA_private_decrypt(
apppbx->e_crypt_ckey_len,
apppbx->e_crypt_ckey,
apppbx->e_crypt_key,
rsa,
RSA_PKCS1_PADDING);
PDEBUG(DEBUG_CRYPT, "decrypt: key =%02x%02x%02x%02x... (len=%d)\n", apppbx->e_crypt_key[0], apppbx->e_crypt_key[1], apppbx->e_crypt_key[2], apppbx->e_crypt_key[3], apppbx->e_crypt_key_len);
RSA_free(rsa);
ememuse--;
apppbx->e_crypt_keyengine_return = 1;
#endif
break;
default:
PERROR("Unknown job %d\n", job);
apppbx->e_crypt_keyengine_return = -1;
}
done:
PDEBUG((DEBUG_EPOINT | DEBUG_CRYPT), "child process done after using libcrypto with return value %d\n", apppbx->e_crypt_keyengine_return);
/* exit process */
if (--apppbx->ea_endpoint->ep_use <= 0)
trigger_work(&apppbx->ea_endpoint->ep_delete);
FREE(args, sizeof(struct auth_args));
amemuse--;
return(NULL);
}
void EndpointAppPBX::cryptman_keyengine(int job)
{
struct auth_args *arg;
pthread_t tid;
if (e_crypt_keyengine_busy) {
e_crypt_keyengine_return = -1;
PERROR("engine currently busy.\n");
return;
}
arg = (struct auth_args *)MALLOC(sizeof(struct auth_args));
arg->apppbx = this;
arg->job = job;
e_crypt_keyengine_return = 0;
e_crypt_keyengine_busy = job;
ea_endpoint->ep_use++;
if ((pthread_create(&tid, NULL, keyengine_child, arg)<0)) {
ea_endpoint->ep_use--;
PERROR("failed to create keyengine-thread.\n");
e_crypt_keyengine_return = -1;
return;
}
amemuse++;
PDEBUG((DEBUG_EPOINT | DEBUG_CRYPT), "send_mail(%d): child process created for doing crypto stuff\n", ea_endpoint->ep_serial);
}
/* handler for authentication (called by apppbx's handler)
*/
int crypt_handler(struct lcr_timer *timer, void *instance, int index)
{
class EndpointAppPBX *ea = (class EndpointAppPBX *)instance;
struct timeval current_time;
if (ea->e_crypt_keyengine_busy) {
if (ea->e_crypt_keyengine_return < 0) {
ea->e_crypt_keyengine_busy = 0;
ea->cryptman_message(CK_ERROR_IND, NULL, 0);
} else
if (ea->e_crypt_keyengine_return > 0) {
switch(ea->e_crypt_keyengine_busy) {
case CK_GENRSA_REQ:
ea->e_crypt_keyengine_busy = 0;
ea->cryptman_message(CK_GENRSA_CONF, NULL, 0);
break;
case CK_CPTRSA_REQ:
ea->e_crypt_keyengine_busy = 0;
ea->cryptman_message(CK_CPTRSA_CONF, NULL, 0);
break;
case CK_DECRSA_REQ:
ea->e_crypt_keyengine_busy = 0;
ea->cryptman_message(CK_DECRSA_CONF, NULL, 0);
break;
}
}
}
/* check for event, make next event */
gettimeofday(¤t_time, NULL);
if (ea->e_crypt_timeout_sec) if (ea->e_crypt_timeout_sec<current_time.tv_sec || (ea->e_crypt_timeout_sec==current_time.tv_sec && ea->e_crypt_timeout_usec<current_time.tv_usec)) {
ea->e_crypt_timeout_sec = 0;
ea->e_crypt_timeout_usec = 0;
ea->cryptman_message(CT_TIMEOUT, NULL, 0);
}
/* trigger until state is 0 */
if (ea->e_crypt_state != CM_ST_NULL)
schedule_timer(&ea->e_crypt_handler, 0, 100000);
return 0;
}
/*
* process message to the crypt manager
*/
/* remote peer sends ident request */
void EndpointAppPBX::cr_ident(int message, unsigned char *param, int len)
{
unsigned char buf[4], *p;
unsigned int bogomips = 0, ran;
int l;
l = CM_SIZEOFINF(CM_INFO_RANDOM);
if (l != 4) {
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) missing (or corrupt) random number, ignoring (len = %d)\n", ea_endpoint->ep_serial, l);
return;
}
p = CM_GETINF(CM_INFO_RANDOM, buf);
ran = (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
l = CM_SIZEOFINF(CM_INFO_BOGOMIPS);
if (l != 4) {
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) missing (or corrupt) random bogomips, just comparing random (len = %d)\n", ea_endpoint->ep_serial, l);
goto compare_random;
}
p = CM_GETINF(CM_INFO_BOGOMIPS, buf);
bogomips = (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
if (e_crypt_bogomips > bogomips) {
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) our cpu is faster, so we are master (%d > %d)\n", ea_endpoint->ep_serial, e_crypt_bogomips, bogomips);
cr_master(message, NULL, 0);
return;
}
if (e_crypt_bogomips < bogomips) {
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) our cpu is slower, so we are slave (%d < %d)\n", ea_endpoint->ep_serial, e_crypt_bogomips, bogomips);
cr_slave(message, NULL, 0);
return;
}
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) our cpu is equal speed, so we check for random value (%d == %d)\n", ea_endpoint->ep_serial, e_crypt_bogomips, bogomips);
compare_random:
/* bogomips are equal, so we compare */
if (e_crypt_random > ran) {
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) our random value is greater, so we are master (%d > %d)\n", ea_endpoint->ep_serial, e_crypt_random, ran);
cr_master(message, NULL, 0);
return;
}
if (e_crypt_random < ran) {
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) our random value is smaller, so we are slave (%d < %d)\n", ea_endpoint->ep_serial, e_crypt_random, ran);
cr_slave(message, NULL, 0);
return;
}
PDEBUG(DEBUG_CRYPT, "EPOINT(%d) random values are equal, so we are looped (%d == %d)\n", ea_endpoint->ep_serial, e_crypt_random, ran);
cr_looped(message, NULL, 0);
}
/* key-exchange activation by the user */
void EndpointAppPBX::cr_activate(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg;
unsigned char bogomips[4], ran[4];
struct timeval current_time;
/* activate listener */
cryptman_msg2crengine(CR_LISTEN_REQ, NULL, 0);
/* send ident message */
msg = CMSG_IDENT;
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
/* random number element */
gettimeofday(¤t_time, NULL);
srandom(current_time.tv_sec ^ current_time.tv_usec ^ random());
e_crypt_random = random();
ran[0] = e_crypt_random >> 24;
ran[1] = e_crypt_random >> 16;
ran[2] = e_crypt_random >> 8;
ran[3] = e_crypt_random;
CM_ADDINF(CM_INFO_RANDOM, 4, ran);
/* cpu speed element */
e_crypt_bogomips = get_bogomips();
if (e_crypt_bogomips > 0) {
bogomips[0] = e_crypt_bogomips >> 24;
bogomips[1] = e_crypt_bogomips >> 16;
bogomips[2] = e_crypt_bogomips >> 8;
bogomips[3] = e_crypt_bogomips;
CM_ADDINF(CM_INFO_BOGOMIPS, 4, bogomips);
}
/* send ident message */
cryptman_msg2peer(buf);
/* change state */
cryptman_state(CM_ST_IDENT);
/* set timeout */
cryptman_timeout(CM_TO_IDENT);
}
/* deactivation by the user */
void EndpointAppPBX::cr_deactivate(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg;
/* deactivate listener (if not already) */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* message */
msg = CMSG_ABORT;
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
/* deactivate encryption */
cryptman_msg2crengine(CC_DACT_REQ, NULL, 0);
/* change state */
cryptman_state(CM_ST_NULL);
/* send message to user */
cryptman_msg2user(CU_DACT_CONF, "Deactivated");
}
/* remote peer tells us to be master */
void EndpointAppPBX::cr_master(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg;
/* change to master state */
cryptman_state(CM_ST_KEYGEN);
if (message == CP_IDENT) {
/* send you-are-slave-message */
msg = CMSG_SLAVE;
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
}
/* start generation of key */
cryptman_keyengine(CK_GENRSA_REQ);
/* disable timeout */
cryptman_timeout(0);
/* send message to user */
cryptman_msg2user(CU_INFO_IND, "Master");
}
/* remote peer tells us to be slave */
void EndpointAppPBX::cr_slave(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg;
/* change to slave state */
cryptman_state(CM_ST_KEYWAIT);
if (message == CP_IDENT) {
/* send you-are-slave-message */
msg = CMSG_MASTER;
/* message */
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
}
/* set timeout */
cryptman_timeout(CM_TO_PUBKEY);
/* send message to user */
cryptman_msg2user(CU_INFO_IND, "Slave");
}
/* remote peer tells us about loop */
void EndpointAppPBX::cr_looped(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg;
/* change to idle state */
cryptman_state(CM_ST_NULL);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
if (message == CP_IDENT) {
/* send looped */
msg = CMSG_LOOPED;
/* message */
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
}
/* disable timeout */
cryptman_timeout(0);
/* send message to user */
cryptman_msg2user(CU_ERROR_IND, "Loop Detected");
}
/* abort */
void EndpointAppPBX::cr_abort(int message, unsigned char *param, int len)
{
/* if already encrypting */
if (e_crypt_state==CM_ST_WAIT_CRYPT
|| e_crypt_state==CM_ST_SWAIT
|| e_crypt_state==CM_ST_ACTIVE) {
/* deactivate blowfish */
cryptman_msg2crengine(CC_DACT_REQ, NULL, 0);
}
/* change to idle state */
cryptman_state(CM_ST_NULL);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* disable timeout */
cryptman_timeout(0);
/* send message to user */
if (message == CT_TIMEOUT)
cryptman_msg2user(CU_ERROR_IND, "Timeout");
else if (message == CP_ABORT)
cryptman_msg2user(CU_ERROR_IND, "Remote Abort");
else
cryptman_msg2user(CU_DACT_IND, NULL);
}
/* abort but wait for engine to release*/
void EndpointAppPBX::cr_abort_engine(int message, unsigned char *param, int len)
{
/* change to release state */
cryptman_state(CM_ST_RELEASE);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* disable timeout */
cryptman_timeout(0);
/* send message to user */
if (message == CT_TIMEOUT)
cryptman_msg2user(CU_ERROR_IND, "Timeout");
else if (message == CP_ABORT)
cryptman_msg2user(CU_ERROR_IND, "Remote Abort");
else
cryptman_msg2user(CU_DACT_IND, NULL);
}
/* abort and disable crypt engine */
void EndpointAppPBX::cr_abort_wait(int message, unsigned char *param, int len)
{
/* change to idle state */
cryptman_state(CM_ST_NULL);
/* deactivate listener (if not already) */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* deactivate blowfish */
cryptman_msg2crengine(CC_DACT_REQ, NULL, 0);
/* disable timeout */
cryptman_timeout(0);
/* send message to user */
if (message == CT_TIMEOUT)
cryptman_msg2user(CU_ERROR_IND, "Timeout");
else if (message == CP_ABORT)
cryptman_msg2user(CU_ERROR_IND, "Remote Abort");
else
cryptman_msg2user(CU_DACT_IND, NULL);
}
/* key engine tells us that the rsa is ready */
void EndpointAppPBX::cr_genrsa(int message, unsigned char *param, int len)
{
unsigned char buf[1024] = "";
unsigned char msg;
/* change to wait for crypted session key state */
cryptman_state(CM_ST_CSWAIT);
/* message */
msg = CMSG_PUBKEY;
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
CM_ADDINF(CM_INFO_PUBKEY, e_crypt_rsa_n_len, &e_crypt_rsa_n);
CM_ADDINF(CM_INFO_PUBEXPONENT, e_crypt_rsa_e_len, &e_crypt_rsa_e);
cryptman_msg2peer(buf);
/* set timeout */
cryptman_timeout(CM_TO_CSKEY);
}
/* our engine has a key error */
void EndpointAppPBX::cr_keyerror(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg;
/* change to idle state */
cryptman_state(CM_ST_NULL);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* message */
msg = CMSG_ABORT;
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
/* send message to user */
cryptman_msg2user(CU_ERROR_IND, "Local Key Error");
}
/* remote sends us the rsa public key */
void EndpointAppPBX::cr_pubkey(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg = CMSG_ABORT;
int l;
l = CM_SIZEOFINF(CM_INFO_PUBKEY);
if (l<1 || l>(int)sizeof(e_crypt_rsa_n)) {
size_error:
/* change to idle state */
cryptman_state(CM_ST_NULL);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* message */
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
/* send message to user */
cryptman_msg2user(CU_ERROR_IND, "Remote Key Error");
return;
}
CM_GETINF(CM_INFO_PUBKEY, e_crypt_rsa_n);
e_crypt_rsa_n_len = l;
l = CM_SIZEOFINF(CM_INFO_PUBEXPONENT);
if (l<1 || l>(int)sizeof(e_crypt_rsa_e))
goto size_error;
CM_GETINF(CM_INFO_PUBEXPONENT, e_crypt_rsa_e);
e_crypt_rsa_e_len = l;
/* change to generating encrypted sessnion key state */
cryptman_state(CM_ST_CSKEY);
/* start generation of crypted session key */
cryptman_keyengine(CK_CPTRSA_REQ);
/* disable timeout */
cryptman_timeout(0);
}
/* key engine tells us that the crypted session key is ready */
void EndpointAppPBX::cr_cptrsa(int message, unsigned char *param, int len)
{
unsigned char buf[1024] = "";
unsigned char msg = CMSG_CSKEY;
/* change to wait for crypt engine state */
cryptman_state(CM_ST_WAIT_DELAY);
/* message */
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
CM_ADDINF(CM_INFO_CSKEY, e_crypt_ckey_len, &e_crypt_ckey);
cryptman_msg2peer(buf);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* timeout 1 sec */
cryptman_timeout(1);
}
/* now we waited for the remote to receive and decrypt the session key */
void EndpointAppPBX::cr_waitdelay(int message, unsigned char *param, int len)
{
/* change to wait for crypt engine state */
cryptman_state(CM_ST_WAIT_CRYPT);
/* disable timeout */
cryptman_timeout(0);
/* send message to crypt engine */
cryptman_msg2crengine(CC_ACTBF_REQ, e_crypt_key, e_crypt_key_len);
}
/* remote sends us the crypted session key */
void EndpointAppPBX::cr_cskey(int message, unsigned char *param, int len)
{
unsigned char buf[128] = "";
unsigned char msg = CMSG_ABORT;
int l;
/* disable timeout */
cryptman_timeout(0);
l = CM_SIZEOFINF(CM_INFO_CSKEY);
if (l<1 || l>(int)sizeof(e_crypt_ckey)) {
/* change to idle state */
cryptman_state(CM_ST_NULL);
/* deactivate listener */
cryptman_msg2crengine(CR_UNLISTEN_REQ, NULL, 0);
/* message */
CM_ADDINF(CM_INFO_MESSAGE, 1, &msg);
cryptman_msg2peer(buf);
/* send message to user */
cryptman_msg2user(CU_ERROR_IND, "Remote Key Error");
return;
}
CM_GETINF(CM_INFO_CSKEY, e_crypt_ckey);
e_crypt_ckey_len = l;
/* change to generating decrypted session key state */