-
Notifications
You must be signed in to change notification settings - Fork 0
/
ck_crp.c
5622 lines (5094 loc) · 164 KB
/
ck_crp.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
char *ckcrpv = "Encryption Engine, 9.0.117, 19 Mar 2010";
/*
C K _ C R P . C - Cryptography for C-Kermit"
Copyright (C) 1998, 2010,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
Author:
Jeffrey E Altman (jaltman@secure-endpoints.com)
Secure Endpoints Inc., New York City
*/
#define CK_CRP_C
#ifdef CK_DES
#ifdef CK_SSL
#ifndef LIBDES
#define LIBDES
#endif /* LIBDES */
#endif /* CK_SSL */
#endif /* CK_DES */
#ifdef CRYPT_DLL
#define CK_AUTHENTICATION
#define CK_ENCRYPTION
#define CK_DES
#define CK_CAST
#ifndef LIBDES
#define LIBDES
#endif /* LIBDES */
#define TELCMDS /* to define name array */
#define TELOPTS /* to define name array */
#define ENCRYPT_NAMES
#endif /* CRYPT_DLL */
#include "ckcsym.h"
#include "ckcdeb.h"
#include "ckcnet.h"
#ifdef DEBUG
#undef DEBUG
#endif /* DEBUG */
#ifdef CK_AUTHENTICATION
#ifdef CK_ENCRYPTION
#define ENCRYPTION
#ifdef CK_DES
#define DES_ENCRYPTION
#endif /* CK_DES */
#ifdef CK_CAST
#define CAST_ENCRYPTION
#endif /* CK_CAST */
#ifdef COMMENT
#define CAST_EXPORT_ENCRYPTION
#endif /* COMMENT */
#endif /* CK_ENCRYPTION */
#endif /* CK_AUTHENTICATION */
#ifdef CK_ENCRYPTION
#include "ckucmd.h" /* For struct keytab definition */
#include "ckuath.h"
#include "ckuat2.h"
#ifdef MIT_CURRENT
#include <krb5.h>
#endif /* MIT_CURRENT */
#include <stdlib.h>
#include <string.h>
#ifdef OS2
#include <stdarg.h>
#ifdef OS2ONLY
#include <os2.h>
#endif /* OS2ONLY */
#include "ckosyn.h"
#else /* OS2 */
static char * tmpstring = NULL;
#endif /* OS2 */
#ifndef CAST_OR_EXPORT
#ifdef CAST_ENCRYPTION
#define CAST_OR_EXPORT
#endif /* CAST_ENCRYPTION */
#ifdef CAST_EXPORT_ENCRYPTION
#define CAST_OR_EXPORT
#endif /* CAST_EXPORT_ENCRYPTION */
#endif /* CAST_OR_EXPORT */
#ifdef MACOSX
#undef LIBDES
#endif /* MACOSX */
#ifdef CRYPT_DLL
int cmd_quoting = 0;
#ifndef TELOPT_MACRO
int
telopt_index(opt) int opt; {
if ( opt >= 0 && opt <= TELOPT_SEND_URL )
return(opt);
else if ( opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT )
return(opt-89);
else
return(NTELOPTS);
}
int
telopt_ok(opt) int opt; {
return((opt >= TELOPT_BINARY && opt <= TELOPT_SEND_URL) ||
(opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT));
}
CHAR *
telopt(opt) int opt; {
if ( telopt_ok(opt) )
return(telopts[telopt_index(opt)]);
else
return("UNKNOWN");
}
#endif /* TELOPT_MACRO */
static int (*p_ttol)(char *,int)=NULL;
static int (*p_dodebug)(int,char *,char *,CK_OFF_T)=NULL;
static int (*p_dohexdump)(char *,char *,int)=NULL;
static void (*p_tn_debug)(char *)=NULL;
static int (*p_vscrnprintf)(char *, ...)=NULL;
static void * p_k5_context=NULL;
static unsigned long (*p_reqtelmutex)(unsigned long)=NULL;
static unsigned long (*p_reltelmutex)(void)=NULL;
unsigned long
RequestTelnetMutex(unsigned long x)
{
if ( p_reqtelmutex )
return p_reqtelmutex(x);
return 0;
}
unsigned long
ReleaseTelnetMutex(void)
{
if ( p_reltelmutex )
return p_reltelmutex();
return 0;
}
int
ttol(char * s, int n)
{
if ( p_ttol )
return(p_ttol(s,n));
else
return(-1);
}
int
dodebug(int flag, char * s1, char * s2, CK_OFF_T n)
{
if ( p_dodebug )
return(p_dodebug(flag,s1,s2,n));
else
return(-1);
}
int
dohexdump( char * s1, char * s2, int n )
{
if ( p_dohexdump )
p_dohexdump(s1,s2,n);
return(0);
}
void
tn_debug( char * s )
{
if ( p_tn_debug )
p_tn_debug(s);
}
static char myprtfstr[4096];
int
Vscrnprintf(const char * format, ...) {
int i, len, rc=0;
char *cp;
va_list ap;
va_start(ap, format);
#ifdef NT
rc = _vsnprintf(myprtfstr, sizeof(myprtfstr)-1, format, ap);
#else /* NT */
rc = vsprintf(myprtfstr, format, ap);
#endif /* NT */
va_end(ap);
if ( p_vscrnprintf )
return(p_vscrnprintf(myprtfstr));
else
return(-1);
}
int
#ifdef CK_ANSIC
tn_hex(CHAR * buf, int buflen, CHAR * data, int datalen)
#else /* CK_ANSIC */
tn_hex(buf, buflen, data, datalen)
CHAR * buf;
int buflen;
CHAR * data;
int datalen;
#endif /* CK_ANSIC */
{
int i = 0, j = 0, k = 0;
CHAR tmp[8];
#ifdef COMMENT
int was_hex = 1;
for (k=0; k < datalen; k++) {
if (data[k] < 32 || data[k] >= 127) {
sprintf(tmp,"%s%02X ",was_hex?"":"\" ",data[k]);
was_hex = 1;
} else {
sprintf(tmp,"%s%c",was_hex?"\"":"",data[k]);
was_hex = 0;
}
ckstrncat(buf,tmp,buflen);
}
if (!was_hex)
ckstrncat(buf,"\" ",buflen);
#else /* COMMENT */
if (datalen <= 0 || data == NULL)
return(0);
for (i = 0; i < datalen; i++) {
ckstrncat(buf,"\r\n ",buflen);
for (j = 0 ; (j < 16); j++) {
if ((i + j) < datalen)
sprintf(tmp,
"%s%02x ",
(j == 8 ? "| " : ""),
(CHAR) data[i + j]
);
else
sprintf(tmp,
"%s ",
(j == 8 ? "| " : "")
);
ckstrncat(buf,tmp,buflen);
}
ckstrncat(buf," ",buflen);
for (k = 0; (k < 16) && ((i + k) < datalen); k++) {
sprintf(tmp,
"%s%c",
(k == 8 ? " " : ""),
isprint(data[i + k]) ? data[i + k] : '.'
);
ckstrncat(buf,tmp,buflen);
}
i += j - 1;
} /* end for */
ckstrncat(buf,"\r\n ",buflen);
#endif /* COMMENT */
return(strlen(buf));
}
#ifdef COMMENT
#define ttol dll_ttol
#define dodebug dll_dodebug
#define dohexdump dll_dohexdump
#define tn_debug dll_tn_debug
#define Vscrnprintf dll_vscrnprintf
#endif /* COMMENT */
char tn_msg[TN_MSG_LEN], hexbuf[TN_MSG_LEN]; /* from ckcnet.c */
int deblog=1, debses=1, tn_deb=1;
#else /* CRYPT_DLL */
extern char tn_msg[], hexbuf[]; /* from ckcnet.c */
extern int deblog, debses, tn_deb;
#ifdef MIT_CURRENT
extern krb5_context k5_context;
#endif /* MIT_CURRENT */
#endif /* CRYPT_DLL */
#ifdef LIBDES
#ifdef MACOSX
#define des_new_random_key ck_des_new_random_key
#define des_set_random_generator_seed ck_des_set_random_generator_seed
#define des_key_sched ck_des_key_sched
#define des_ecb_encrypt ck_des_ecb_encrypt
#define des_string_to_key ck_des_string_to_key
#define des_fixup_key_parity ck_des_fixup_key_parity
#endif /* MACOSX */
#ifndef UNIX
#define des_new_random_key des_random_key
#define des_set_random_generator_seed des_random_seed
#endif /* UNIX */
#define des_fixup_key_parity des_set_odd_parity
#ifdef OPENSSL_097
#define OPENSSL_ENABLE_OLD_DES_SUPPORT
#include <openssl/des.h>
#endif /* OPENSSL_097 */
#else /* LIBDES */
#ifdef UNIX
#define des_set_random_generator_seed(x) des_init_random_number_generator(x)
#endif /* UNIX */
#ifdef OS2
#define des_new_random_key ck_des_new_random_key
#define des_set_random_generator_seed ck_des_set_random_generator_seed
#define des_key_sched ck_des_key_sched
#define des_ecb_encrypt ck_des_ecb_encrypt
#define des_string_to_key ck_des_string_to_key
#define des_fixup_key_parity ck_des_fixup_key_parity
#endif /* OS2 */
#endif /* LIBDES */
#ifdef CK_DES
/* This code comes from Eric Young's libdes package and is not part */
/* of the standard MIT DES library that is part of Kerberos. However, */
/* it is extremely useful. So we add it here. */
/* Weak and semi week keys as take from
* %A D.W. Davies
* %A W.L. Price
* %T Security for Computer Networks
* %I John Wiley & Sons
* %D 1984
* Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
* (and actual cblock values).
*/
#define NUM_WEAK_KEY 16
static Block weak_keys[NUM_WEAK_KEY]={
/* weak keys */
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
{0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
{0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
/* semi-weak keys */
{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
int
ck_des_is_weak_key(key)
Block key;
{
int i;
for (i=0; i<NUM_WEAK_KEY; i++) {
/* Added == 0 to comparision, I obviously don't run
* this section very often :-(, thanks to
* engineering@MorningStar.Com for the fix
* eay 93/06/29
* Another problem, I was comparing only the first 4
* bytes, 97/03/18 */
if (memcmp(weak_keys[i],key,sizeof(Block)) == 0)
return(1);
}
return(0);
}
#ifdef UNIX
#ifdef LIBDES
#ifndef MACOSX
/* These functions are not part of Eric Young's DES library */
/* _unix_time_gmt_unixsec */
/* _des_set_random_generator_seed */
/* _des_fixup_key_parity (added in 0.9.5) */
/* _des_new_random_key */
#include <sys/time.h>
unsigned long
unix_time_gmt_unixsec (usecptr)
unsigned long *usecptr;
{
struct timeval now;
(void) gettimeofday (&now, (struct timezone *)0);
if (usecptr)
*usecptr = now.tv_usec;
return now.tv_sec;
}
void
des_set_random_generator_seed(Block B)
{
des_random_seed(B);
return;
}
#ifdef COMMENT
/* added to openssl in 0.9.5 */
void
des_fixup_key_parity(Block B)
{
des_set_odd_parity(B);
return;
}
#endif /* COMMENT */
int
des_new_random_key(Block B)
{
int rc=0;
/* WARNING:
This might need to have the "rc = " removed because this
is VOID in later, and maybe even all, versions.
*/
rc = des_random_key(B);
return(rc);
}
#endif /* MACOSX */
#endif /* LIBDES */
#endif /* UNIX */
#endif /* CK_DES */
/*
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* based on @(#)encrypt.c 8.1 (Berkeley) 6/4/93 */
/*
* Copyright (C) 1990 by the Massachusetts Institute of Technology
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*/
#include <stdio.h>
/*
* These function pointers point to the current routines
* for encrypting and decrypting data.
*/
/* NOTE: These next two might need to have the "static " removed */
static VOID (*encrypt_output) P((unsigned char *, int));
static int (*decrypt_input) P((int));
#ifdef DEBUG
static int encrypt_debug_mode = 1;
static int encrypt_verbose = 1;
#else
static int encrypt_verbose = 1;
static int encrypt_debug_mode = 0;
#endif
static char dbgbuf [16384];
static int decrypt_mode = 0;
static int encrypt_mode = 0;
static int autoencrypt = 1;
static int autodecrypt = 1;
static int havesessionkey = 0;
static kstream EncryptKSGlobalHack = NULL;
static int EncryptType = ENCTYPE_ANY;
#define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
static long i_support_encrypt =
typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
static long i_support_decrypt =
typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
static long i_wont_support_encrypt = 0;
static long i_wont_support_decrypt = 0;
#define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
#define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
static long remote_supports_encrypt = 0;
static long remote_supports_decrypt = 0;
/* Make sure that this list is in order of algorithm strength */
/* as it determines the search order for selecting specific */
/* encryption choices. All CFB modes must come before OFB modes. */
static Encryptions encryptions[] = {
#ifdef DES_ENCRYPTION
{ "DES3_CFB64",
ENCTYPE_DES3_CFB64,
des3_cfb64_encrypt,
des3_cfb64_decrypt,
des3_cfb64_init,
des3_cfb64_start,
des3_cfb64_is,
des3_cfb64_reply,
des3_cfb64_session,
des3_cfb64_keyid,
NULL },
#endif /* DES_ENCRYPTION */
#ifdef CAST_ENCRYPTION
#ifndef CAST_EXPORT_ENCRYPTION
{ "CAST128_CFB64", ENCTYPE_CAST128_CFB64,
cast_cfb64_encrypt,
cast_cfb64_decrypt,
cast_cfb64_init,
cast_cfb64_start,
cast_cfb64_is,
cast_cfb64_reply,
cast_cfb64_session,
cast_cfb64_keyid,
NULL },
#endif
#endif
#ifdef DES_ENCRYPTION
{ "DES_CFB64",
ENCTYPE_DES_CFB64,
cfb64_encrypt,
cfb64_decrypt,
cfb64_init,
cfb64_start,
cfb64_is,
cfb64_reply,
cfb64_session,
cfb64_keyid,
NULL },
#endif /* DES_ENCRYPTION */
#if defined (CAST_EXPORT_ENCRYPTION) || defined(CAST_ENCRYPTION)
{ "CAST5_40_CFB64", ENCTYPE_CAST5_40_CFB64,
castexp_cfb64_encrypt,
castexp_cfb64_decrypt,
castexp_cfb64_init,
castexp_cfb64_start,
castexp_cfb64_is,
castexp_cfb64_reply,
castexp_cfb64_session,
castexp_cfb64_keyid,
NULL },
#endif /* CAST_ENCRYPTION */
#ifdef DES_ENCRYPTION
{ "DES3_OFB64",
ENCTYPE_DES3_OFB64,
des3_ofb64_encrypt,
des3_ofb64_decrypt,
des3_ofb64_init,
des3_ofb64_start,
des3_ofb64_is,
des3_ofb64_reply,
des3_ofb64_session,
des3_ofb64_keyid,
NULL },
#endif /* DES_ENCRYPTION */
#ifdef CAST_ENCRYPTION
#ifndef CAST_EXPORT_ENCRYPTION
{ "CAST128_OFB64", ENCTYPE_CAST128_OFB64,
cast_ofb64_encrypt,
cast_ofb64_decrypt,
cast_ofb64_init,
cast_ofb64_start,
cast_ofb64_is,
cast_ofb64_reply,
cast_ofb64_session,
cast_ofb64_keyid,
NULL },
#endif
#endif
#ifdef DES_ENCRYPTION
{ "DES_OFB64",
ENCTYPE_DES_OFB64,
ofb64_encrypt,
ofb64_decrypt,
ofb64_init,
ofb64_start,
ofb64_is,
ofb64_reply,
ofb64_session,
ofb64_keyid,
NULL },
#endif /* DES_ENCRYPTION */
#if defined (CAST_EXPORT_ENCRYPTION) || defined(CAST_ENCRYPTION)
{ "CAST5_40_OFB64", ENCTYPE_CAST5_40_OFB64,
castexp_ofb64_encrypt,
castexp_ofb64_decrypt,
castexp_ofb64_init,
castexp_ofb64_start,
castexp_ofb64_is,
castexp_ofb64_reply,
castexp_ofb64_session,
castexp_ofb64_keyid,
NULL },
#endif /* CAST_ENCRYPTION */
{ 0,0,0,0,0,0,0,0,0,0,0 }
};
int
get_crypt_table( struct keytab ** pTable, int * pN )
{
int i=0,n=0;
if ( *pTable )
{
for ( i=0 ; i < *pN ; i++ )
free( (*pTable)[i].kwd ) ;
free ( *pTable ) ;
}
*pTable = NULL;
*pN = 0;
/* How many encryption types do we have? */
while ( encryptions[n].name )
n++;
if ( n )
{
*pTable = malloc( sizeof(struct keytab) * (n+2) ) ;
if ( !(*pTable) )
return(0);
#ifdef OS2
(*pTable)[0].kwd =strdup("automatic");
#else /* OS2 */
makestr(&tmpstring,"automatic");
(*pTable)[0].kwd = tmpstring;
tmpstring = NULL;
#endif /* OS2 */
(*pTable)[0].kwval = ENCTYPE_ANY;
(*pTable)[0].flgs = 0;
#ifdef OS2
(*pTable)[1].kwd =strdup("none");
#else /* OS2 */
makestr(&tmpstring,"none");
(*pTable)[1].kwd = tmpstring;
tmpstring = NULL;
#endif /* OS2 */
(*pTable)[1].kwval = 999;
(*pTable)[1].flgs = 0;
(*pN) = 2;
for ( i=0 ; i < n ; i++ ) {
char * newstr = NULL, * p;
int newval = encryptions[i].type;
int j = 0, len = 0;
#ifdef OS2
newstr = strdup(encryptions[i].name);
strlwr(newstr);
#else /* OS2 */
makestr(&tmpstring,encryptions[i].name);
newstr = tmpstring;
tmpstring = NULL;
for (p = newstr; *p; p++) if (isupper(*p)) *p = tolower(*p);
#endif /* OS2 */
for (j = 0; j < (*pN); j++) {
int tempval = 0;
char * tempstr = NULL;
if ( strcmp( (*pTable)[j].kwd, newstr ) > 0 )
{
tempval = (*pTable)[j].kwval;
tempstr = (*pTable)[j].kwd;
(*pTable)[j].kwd = newstr ;
(*pTable)[j].kwval = newval;
newval = tempval;
newstr = tempstr;
(*pTable)[j].flgs = 0;
}
}
(*pTable)[*pN].kwd = newstr ;
(*pTable)[*pN].kwval = newval;
(*pTable)[*pN].flgs = 0 ;
(*pN)++ ;
}
} else {
*pTable = malloc( sizeof(struct keytab) * 2 ) ;
if ( !(*pTable) )
return(0);
#ifdef OS2
(*pTable)[0].kwd =strdup("automatic");
#else /* OS2 */
makestr(&tmpstring,"automatic");
(*pTable)[0].kwd = tmpstring;
tmpstring = NULL;
#endif /* OS2 */
(*pTable)[0].kwval = ENCTYPE_ANY;
(*pTable)[0].flgs = 0;
#ifdef OS2
(*pTable)[1].kwd =strdup("none");
#else /* OS2 */
makestr(&tmpstring,"none");
(*pTable)[1].kwd = tmpstring;
tmpstring = NULL;
#endif /* OS2 */
(*pTable)[1].kwval = 999;
(*pTable)[1].flgs = 0;
(*pN) = 2;
}
return(*pN);
}
static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPTION,
ENCRYPT_SUPPORT };
static unsigned char str_suplen = 0;
static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPTION };
static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPTION, 0, IAC, SE };
_PROTOTYP(int encrypt_request_end, (VOID));
_PROTOTYP(int encrypt_request_start, (VOID));
_PROTOTYP(int encrypt_enc_keyid, (unsigned char *, int));
_PROTOTYP(int encrypt_dec_keyid, (unsigned char *, int));
_PROTOTYP(int encrypt_support, (unsigned char *, int));
_PROTOTYP(int encrypt_start, (unsigned char *, int));
_PROTOTYP(int encrypt_end, (VOID));
_PROTOTYP(int encrypt_ks_stream,(struct kstream_data_block *, /* output */
struct kstream_data_block *)); /* input */
_PROTOTYP(int decrypt_ks_stream,(struct kstream_data_block *, /* output */
struct kstream_data_block *)); /* input */
int
#ifdef CK_ANSIC
encrypt_ks_stream(struct kstream_data_block *i,
struct kstream_data_block *o)
#else
encrypt_ks_stream(i,o)
struct kstream_data_block *i; struct kstream_data_block *o;
#endif
{
/*
* this is really quite bogus, since it does an in-place encryption...
*/
if (encrypt_output) {
encrypt_output(i->ptr, i->length);
return 1;
}
return 0;
}
int
#ifdef CK_ANSIC
decrypt_ks_stream(struct kstream_data_block *i,
struct kstream_data_block *o)
#else
decrypt_ks_stream(i,o)
struct kstream_data_block *i; struct kstream_data_block *o;
#endif
{
unsigned int len;
/*
* this is really quite bogus, since it does an in-place decryption...
*/
if (decrypt_input) {
for (len = 0 ; len < i->length ; len++)
((unsigned char *)i->ptr)[len]
= decrypt_input(((unsigned char *)i->ptr)[len]);
return 1;
}
return 0;
}
int
#ifdef CK_ANSIC
decrypt_ks_hack(unsigned char *buf, int cnt)
#else
decrypt_ks_hack(buf,cnt) unsigned char *buf; int cnt;
#endif
{
int len;
/*
* this is really quite bogus, since it does an in-place decryption...
*/
for (len = 0 ; len < cnt ; len++)
buf[len] = decrypt_input(buf[len]);
#ifdef DEBUG
ckhexdump("decrypt ks hack", buf, cnt);
#endif
return 1;
}
/*
* parsedat[0] == the suboption we might be negotiating,
*/
int
#ifdef CK_ANSIC
encrypt_parse(unsigned char *parsedat, int end_sub)
#else
encrypt_parse(parsedat,end_sub) unsigned char *parsedat; int end_sub;
#endif
{
int rc = 0;
switch(parsedat[1]) {
case ENCRYPT_START:
rc = encrypt_start(parsedat + 2, end_sub - 2);
break;
case ENCRYPT_END:
rc = encrypt_end();
break;
case ENCRYPT_SUPPORT:
rc = encrypt_support(parsedat + 2, end_sub - 2);
break;
case ENCRYPT_REQSTART:
rc = encrypt_request_start();
break;
case ENCRYPT_REQEND:
/*
* We can always send an REQEND so that we cannot
* get stuck encrypting. We should only get this
* if we have been able to get in the correct mode
* anyhow.
*/
rc = encrypt_request_end();
break;
case ENCRYPT_IS:
rc = encrypt_is(parsedat + 2, end_sub - 2);
break;
case ENCRYPT_REPLY:
rc = encrypt_reply(parsedat + 2, end_sub - 2);
break;
case ENCRYPT_ENC_KEYID:
rc = encrypt_enc_keyid(parsedat + 2, end_sub - 2);
break;
case ENCRYPT_DEC_KEYID:
rc = encrypt_dec_keyid(parsedat + 2, end_sub - 2);
break;
default:
rc = -1;
break;
}
return(rc);
}
/* XXX */
Encryptions *
#ifdef CK_ANSIC
findencryption(int type)
#else
findencryption(type) int type;
#endif
{
Encryptions *ep = encryptions;
if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask(type)))
return(0);
while (ep->type && ep->type != type)
++ep;
return(ep->type ? ep : 0);
}
Encryptions *
#ifdef CK_ANSIC
finddecryption(int type)
#else
finddecryption(type) int type;
#endif
{
Encryptions *ep = encryptions;
if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask(type)))
return(0);
while (ep->type && ep->type != type)
++ep;
return(ep->type ? ep : 0);
}
#define MAXKEYLEN 64
static struct key_info {
unsigned char keyid[MAXKEYLEN];
int keylen;
int dir;
int *modep;
Encryptions *(*getcrypt)();
} ki[2] = {
{ { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
{ { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
};
VOID
#ifdef CK_ANSIC
encrypt_init(kstream iks, int type)
#else
encrypt_init(iks, type) kstream iks; int type;
#endif
{
Encryptions *ep = encryptions;
i_support_encrypt = i_support_decrypt = 0;
remote_supports_encrypt = remote_supports_decrypt = 0;
i_wont_support_encrypt = i_wont_support_decrypt = 0;
encrypt_mode = 0;
decrypt_mode = 0;
encrypt_output = NULL;
decrypt_input = NULL;
ki[0].keylen = 0;
memset(ki[0].keyid,0,MAXKEYLEN);
ki[1].keylen = 0;
memset(ki[1].keyid,0,MAXKEYLEN);
havesessionkey = 0;
autoencrypt = 1;
autodecrypt = 1;
EncryptKSGlobalHack = iks;
EncryptType = type;
str_send[0] = IAC;
str_send[1] = SB;
str_send[2] = TELOPT_ENCRYPTION;
str_send[3] = ENCRYPT_SUPPORT;
str_suplen = 4;
while (ep->type) {
if ( EncryptType == ENCTYPE_ANY ||
EncryptType == ep->type ) {
#ifdef DEBUG
if (encrypt_debug_mode) {
sprintf(dbgbuf, ">>>I will support %s\n",
ENCTYPE_NAME(ep->type)); /* safe */
debug(F110,"encrypt_init",dbgbuf,0);
}
#endif
i_support_encrypt |= typemask(ep->type);
i_support_decrypt |= typemask(ep->type);
if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
if ((str_send[str_suplen++] = ep->type) == IAC)
str_send[str_suplen++] = IAC;
}
if (ep->init)
(*ep->init)(0);
++ep;
}
str_send[str_suplen++] = IAC;
str_send[str_suplen++] = SE;
}
VOID
#ifdef CK_ANSIC
encrypt_send_support(VOID)
#else
encrypt_send_support()
#endif
{
Encryptions *ep = encryptions;
#ifdef CK_SSL
if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
return;
#endif /* CK_SSL */
str_send[0] = IAC;
str_send[1] = SB;
str_send[2] = TELOPT_ENCRYPTION;
str_send[3] = ENCRYPT_SUPPORT;