2
* Copyright (c) 1991, 1993
3
* The Regents of the University of California. All rights reserved.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. All advertising materials mentioning features or use of this software
14
* must display the following acknowledgement:
15
* This product includes software developed by the University of
16
* California, Berkeley and its contributors.
17
* 4. Neither the name of the University nor the names of its contributors
18
* may be used to endorse or promote products derived from this software
19
* without specific prior written permission.
21
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
/* based on @(#)encrypt.c 8.1 (Berkeley) 6/4/93 */
37
* Copyright (C) 1990 by the Massachusetts Institute of Technology
39
* Export of this software from the United States of America may
40
* require a specific license from the United States Government.
41
* It is the responsibility of any person or organization contemplating
42
* export to obtain such a license before exporting.
44
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
45
* distribute this software and its documentation for any purpose and
46
* without fee is hereby granted, provided that the above copyright
47
* notice appear in all copies and that both that copyright notice and
48
* this permission notice appear in supporting documentation, and that
49
* the name of M.I.T. not be used in advertising or publicity pertaining
50
* to distribution of the software without specific, written prior
51
* permission. Furthermore if you modify this software you must label
52
* your software as modified software and not distribute it in such a
53
* fashion that it might be confused with the original M.I.T. software.
54
* M.I.T. makes no representations about the suitability of
55
* this software for any purpose. It is provided "as is" without express
56
* or implied warranty.
63
#define isprefix(a, b) (!strncmp((a), (b), strlen(b)))
83
#include "telnet_arpa.h"
86
* These function pointers point to the current routines
87
* for encrypting and decrypting data.
89
void (*encrypt_output) (unsigned char *, int);
90
int (*decrypt_input) (int);
93
int encrypt_debug_mode = 1;
94
int encrypt_verbose = 1;
96
int encrypt_verbose = 0;
99
static char dbgbuf [10240];
101
static int decrypt_mode = 0;
102
static int encrypt_mode = 0;
103
static int autoencrypt = 1;
104
static int autodecrypt = 1;
105
static int havesessionkey = 0;
107
kstream EncryptKSGlobalHack = NULL;
109
#define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
111
static long i_support_encrypt =
112
typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
113
static long i_support_decrypt =
114
typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
115
static long i_wont_support_encrypt = 0;
116
static long i_wont_support_decrypt = 0;
117
#define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
118
#define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
120
static long remote_supports_encrypt = 0;
121
static long remote_supports_decrypt = 0;
123
static Encryptions encryptions[] = {
149
static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
151
static unsigned char str_suplen = 0;
152
static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
153
static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
155
void encrypt_request_end(void);
156
void encrypt_request_start(unsigned char *, int);
157
void encrypt_enc_keyid(unsigned char *, int);
158
void encrypt_dec_keyid(unsigned char *, int);
159
void encrypt_support(unsigned char *, int);
160
void encrypt_start(unsigned char *, int);
161
void encrypt_end(void);
163
int encrypt_ks_stream(struct kstream_data_block *, /* output */
164
struct kstream_data_block *, /* input */
167
int decrypt_ks_stream(struct kstream_data_block *, /* output */
168
struct kstream_data_block *, /* input */
172
encrypt_ks_stream(struct kstream_data_block *i,
173
struct kstream_data_block *o,
178
* this is really quite bogus, since it does an in-place encryption...
180
if (encrypt_output) {
181
encrypt_output(i->ptr, i->length);
190
decrypt_ks_stream(struct kstream_data_block *i,
191
struct kstream_data_block *o,
196
* this is really quite bogus, since it does an in-place decryption...
199
for (len = 0 ; len < i->length ; len++)
200
((unsigned char *)i->ptr)[len]
201
= decrypt_input(((unsigned char *)i->ptr)[len]);
209
decrypt_ks_hack(unsigned char *buf, int cnt)
213
* this is really quite bogus, since it does an in-place decryption...
215
for (len = 0 ; len < cnt ; len++)
216
buf[len] = decrypt_input(buf[len]);
219
hexdump("hack:", buf, cnt);
226
printsub(char c, unsigned char *s, size_t len)
233
for (i = 0 ; (i < len) && (p - dbgbuf + 3 < sizeof(dbgbuf)) ; i++)
234
p += sprintf(p, "%02x ", s[i]);
235
dbgbuf[sizeof(dbgbuf) - 1] = '\0';
237
strncat(p, "\n", sizeof(dbgbuf) - 1 - (p - dbgbuf));
239
OutputDebugString(dbgbuf);
246
* parsedat[0] == the suboption we might be negoating,
249
encrypt_parse(kstream ks, unsigned char *parsedat, int end_sub)
254
printsub('<', parsedat, end_sub);
257
switch(parsedat[1]) {
259
encrypt_start(parsedat + 2, end_sub - 2);
264
case ENCRYPT_SUPPORT:
265
encrypt_support(parsedat + 2, end_sub - 2);
267
case ENCRYPT_REQSTART:
268
encrypt_request_start(parsedat + 2, end_sub - 2);
272
* We can always send an REQEND so that we cannot
273
* get stuck encrypting. We should only get this
274
* if we have been able to get in the correct mode
277
encrypt_request_end();
280
encrypt_is(parsedat + 2, end_sub - 2);
283
encrypt_reply(parsedat + 2, end_sub - 2);
285
case ENCRYPT_ENC_KEYID:
286
encrypt_enc_keyid(parsedat + 2, end_sub - 2);
288
case ENCRYPT_DEC_KEYID:
289
encrypt_dec_keyid(parsedat + 2, end_sub - 2);
301
Encryptions *ep = encryptions;
303
if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask(type)))
305
while (ep->type && ep->type != type)
307
return(ep->type ? ep : 0);
311
finddecryption(int type)
313
Encryptions *ep = encryptions;
315
if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask(type)))
317
while (ep->type && ep->type != type)
319
return(ep->type ? ep : 0);
324
static struct key_info {
325
unsigned char keyid[MAXKEYLEN];
329
Encryptions *(*getcrypt)();
331
{ { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
332
{ { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
336
encrypt_init(kstream iks, kstream_ptr data)
338
Encryptions *ep = encryptions;
340
i_support_encrypt = i_support_decrypt = 0;
341
remote_supports_encrypt = remote_supports_decrypt = 0;
344
encrypt_output = NULL;
345
decrypt_input = NULL;
349
EncryptKSGlobalHack = iks;
353
if (encrypt_debug_mode) {
354
sprintf(dbgbuf, ">>>I will support %s\n",
355
ENCTYPE_NAME(ep->type));
356
OutputDebugString(dbgbuf);
359
i_support_encrypt |= typemask(ep->type);
360
i_support_decrypt |= typemask(ep->type);
361
if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
362
if ((str_send[str_suplen++] = ep->type) == IAC)
363
str_send[str_suplen++] = IAC;
368
str_send[str_suplen++] = IAC;
369
str_send[str_suplen++] = SE;
373
encrypt_send_support()
377
* If the user has requested that decryption start
378
* immediatly, then send a "REQUEST START" before
379
* we negotiate the type.
382
encrypt_send_request_start();
383
TelnetSend(EncryptKSGlobalHack, str_send, str_suplen, 0);
386
printsub('>', &str_send[2], str_suplen - 2);
394
* Called when ENCRYPT SUPPORT is received.
397
encrypt_support(typelist, cnt)
398
unsigned char *typelist;
401
register int type, use_type = 0;
405
* Forget anything the other side has previously told us.
407
remote_supports_decrypt = 0;
412
if (encrypt_debug_mode) {
413
sprintf(dbgbuf, ">>>Remote supports %s (%d)\n",
414
ENCTYPE_NAME(type), type);
415
OutputDebugString(dbgbuf);
418
if ((type < ENCTYPE_CNT) &&
419
(I_SUPPORT_ENCRYPT & typemask(type))) {
420
remote_supports_decrypt |= typemask(type);
426
ep = findencryption(use_type);
429
type = ep->start ? (*ep->start)(DIR_ENCRYPT, 0) : 0;
431
if (encrypt_debug_mode) {
432
sprintf(dbgbuf, ">>>(*ep->start)() %s returned %d (%s)\n",
433
ENCTYPE_NAME(use_type), type, ENCRYPT_NAME(type));
434
OutputDebugString(dbgbuf);
439
encrypt_mode = use_type;
441
encrypt_start_output(use_type);
446
encrypt_is(data, cnt)
451
register int type, ret;
456
if (type < ENCTYPE_CNT)
457
remote_supports_encrypt |= typemask(type);
458
if (!(ep = finddecryption(type))) {
460
if (encrypt_debug_mode) {
461
sprintf(dbgbuf, ">>>encrypt_reply: "
462
"Can't find type %s (%d) for initial negotiation\n",
463
ENCTYPE_NAME_OK(type)
464
? ENCTYPE_NAME(type) : "(unknown)",
466
OutputDebugString(dbgbuf);
473
if (encrypt_debug_mode) {
474
sprintf(dbgbuf, ">>>encrypt_reply: "
475
"No initial negotiation needed for type %s (%d)\n",
476
ENCTYPE_NAME_OK(type)
477
? ENCTYPE_NAME(type) : "(unknown)",
479
OutputDebugString(dbgbuf);
484
ret = (*ep->is)(data, cnt);
486
if (encrypt_debug_mode) {
487
sprintf(dbgbuf, "encrypt_reply: "
488
"(*ep->is)(%x, %d) returned %s(%d)\n", data, cnt,
489
(ret < 0) ? "FAIL " :
490
(ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
491
OutputDebugString(dbgbuf);
499
if (ret == 0 && autodecrypt)
500
encrypt_send_request_start();
505
encrypt_reply(data, cnt)
510
register int ret, type;
515
if (!(ep = findencryption(type))) {
517
if (encrypt_debug_mode) {
518
sprintf(dbgbuf, ">>>Can't find type %s (%d) for initial negotiation\n",
519
ENCTYPE_NAME_OK(type)
520
? ENCTYPE_NAME(type) : "(unknown)",
522
OutputDebugString(dbgbuf);
529
if (encrypt_debug_mode) {
530
sprintf(dbgbuf, ">>>No initial negotiation needed for type %s (%d)\n",
531
ENCTYPE_NAME_OK(type)
532
? ENCTYPE_NAME(type) : "(unknown)",
534
OutputDebugString(dbgbuf);
539
ret = (*ep->reply)(data, cnt);
541
if (encrypt_debug_mode) {
542
sprintf(dbgbuf, "(*ep->reply)(%x, %d) returned %s(%d)\n",
544
(ret < 0) ? "FAIL " :
545
(ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
546
OutputDebugString(dbgbuf);
551
if (encrypt_debug_mode) {
552
sprintf(dbgbuf, ">>>encrypt_reply returned %d\n", ret);
553
OutputDebugString(dbgbuf);
560
if (ret == 0 && autoencrypt)
561
encrypt_start_output(type);
566
* Called when a ENCRYPT START command is received.
569
encrypt_start(data, cnt)
577
* Something is wrong. We should not get a START
578
* command without having already picked our
579
* decryption scheme. Send a REQUEST-END to
580
* attempt to clear the channel...
582
/* printf("Warning, Cannot decrypt input stream!!!\n"); */
583
encrypt_send_request_end();
584
MessageBox(NULL, "Warning, Cannot decrypt input stream!!!", NULL,
585
MB_OK | MB_ICONEXCLAMATION);
589
if (ep = finddecryption(decrypt_mode)) {
590
extern BOOL encrypt_flag;
592
decrypt_input = ep->input;
593
EncryptKSGlobalHack->decrypt = decrypt_ks_stream;
594
encrypt_flag = 2; /* XXX hack */
596
if (encrypt_verbose) {
597
sprintf(dbgbuf, "[ Input is now decrypted with type %s ]\n",
598
ENCTYPE_NAME(decrypt_mode));
599
OutputDebugString(dbgbuf);
602
if (encrypt_debug_mode) {
603
sprintf(dbgbuf, ">>>Start to decrypt input with type %s\n",
604
ENCTYPE_NAME(decrypt_mode));
605
OutputDebugString(dbgbuf);
610
wsprintf(buf, "Warning, Cannot decrypt type %s (%d)!!!",
611
ENCTYPE_NAME_OK(decrypt_mode)
612
? ENCTYPE_NAME(decrypt_mode) : "(unknown)",
614
MessageBox(NULL, buf, NULL, MB_OK | MB_ICONEXCLAMATION);
615
encrypt_send_request_end();
620
encrypt_session_key(key, server)
624
Encryptions *ep = encryptions;
630
(*ep->session)(key, server);
632
if (!encrypt_output && autoencrypt && !server)
633
encrypt_start_output(ep->type);
634
if (!decrypt_input && autodecrypt && !server)
635
encrypt_send_request_start();
642
* Called when ENCRYPT END is received.
647
decrypt_input = NULL;
648
EncryptKSGlobalHack->decrypt = NULL;
650
if (encrypt_debug_mode) {
651
sprintf(dbgbuf, ">>>Input is back to clear text\n");
652
OutputDebugString(dbgbuf);
655
if (encrypt_verbose) {
656
sprintf(dbgbuf, "[ Input is now clear text ]\n");
657
OutputDebugString(dbgbuf);
662
* Called when ENCRYPT REQUEST-END is received.
665
encrypt_request_end()
671
* Called when ENCRYPT REQUEST-START is received. If we receive
672
* this before a type is picked, then that indicates that the
673
* other side wants us to start encrypting data as soon as we
677
encrypt_request_start(data, cnt)
681
if (encrypt_mode == 0) {
684
encrypt_start_output(encrypt_mode);
687
static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
693
encrypt_enc_keyid(keyid, len)
694
unsigned char *keyid;
697
encrypt_keyid(&ki[1], keyid, len);
701
encrypt_dec_keyid(keyid, len)
702
unsigned char *keyid;
705
encrypt_keyid(&ki[0], keyid, len);
709
encrypt_keyid(kp, keyid, len)
711
unsigned char *keyid;
716
register int ret = 0;
718
if (!(ep = (*kp->getcrypt)(*kp->modep))) {
722
} else if (len == 0) {
724
* Empty option, indicates a failure.
730
(void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
732
} else if ((len != kp->keylen) || (memcmp(keyid, kp->keyid, len) != 0)) {
734
* Length or contents are different
737
memcpy(kp->keyid, keyid, len);
739
(void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
742
ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
743
if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
744
encrypt_start_output(*kp->modep);
748
encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
752
encrypt_send_keyid(dir, keyid, keylen, saveit)
754
unsigned char *keyid;
760
str_keyid[3] = (dir == DIR_ENCRYPT)
761
? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
763
struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
764
memcpy(kp->keyid, keyid, keylen);
768
for (strp = &str_keyid[4]; keylen > 0; --keylen) {
769
if ((*strp++ = *keyid++) == IAC)
774
TelnetSend(EncryptKSGlobalHack, str_keyid, strp - str_keyid, 0);
777
printsub('>', &str_keyid[2], strp - str_keyid - 2);
789
autoencrypt = on ? 1 : 0;
799
autodecrypt = on ? 1 : 0;
803
encrypt_start_output(type)
807
register unsigned char *p;
810
if (!(ep = findencryption(type))) {
812
if (encrypt_debug_mode) {
813
sprintf(dbgbuf, ">>>Can't encrypt with type %s (%d)\n",
814
ENCTYPE_NAME_OK(type)
815
? ENCTYPE_NAME(type) : "(unknown)",
817
OutputDebugString(dbgbuf);
823
i = (*ep->start)(DIR_ENCRYPT, 0);
825
if (encrypt_debug_mode) {
826
sprintf(dbgbuf, ">>>Encrypt start: %s (%d) %s\n",
828
"initial negotiation in progress",
829
i, ENCTYPE_NAME(type));
830
OutputDebugString(dbgbuf);
837
*p++ = ENCRYPT_START;
838
for (i = 0; i < ki[0].keylen; ++i) {
839
if ((*p++ = ki[0].keyid[i]) == IAC)
844
TelnetSend(EncryptKSGlobalHack, str_start, p - str_start, 0);
846
printsub('>', &str_start[2], p - &str_start[2]);
850
* If we are already encrypting in some mode, then
851
* encrypt the ring (which includes our request) in
852
* the old mode, mark it all as "clear text" and then
853
* switch to the new mode.
855
encrypt_output = ep->output;
856
EncryptKSGlobalHack->encrypt = encrypt_ks_stream;
859
if (encrypt_debug_mode) {
860
sprintf(dbgbuf, ">>>Started to encrypt output with type %s\n",
862
OutputDebugString(dbgbuf);
865
if (encrypt_verbose) {
866
sprintf(dbgbuf, "[ Output is now encrypted with type %s ]\n",
868
OutputDebugString(dbgbuf);
878
str_end[3] = ENCRYPT_END;
879
TelnetSend(EncryptKSGlobalHack, str_end, sizeof(str_end), 0);
881
printsub('>', &str_end[2], sizeof(str_end) - 2);
885
* Encrypt the output buffer now because it will not be done by
889
EncryptKSGlobalHack->encrypt = NULL;
891
if (encrypt_debug_mode) {
892
sprintf(dbgbuf, ">>>Output is back to clear text\n");
893
OutputDebugString(dbgbuf);
896
if (encrypt_verbose) {
897
sprintf(dbgbuf, "[ Output is now clear text ]\n");
898
OutputDebugString(dbgbuf);
903
encrypt_send_request_start()
905
register unsigned char *p;
909
*p++ = ENCRYPT_REQSTART;
910
for (i = 0; i < ki[1].keylen; ++i) {
911
if ((*p++ = ki[1].keyid[i]) == IAC)
916
TelnetSend(EncryptKSGlobalHack, str_start, p - str_start, 0);
918
printsub('>', &str_start[2], p - &str_start[2]);
920
if (encrypt_debug_mode) {
921
sprintf(dbgbuf, ">>>Request input to be encrypted\n");
922
OutputDebugString(dbgbuf);
928
encrypt_send_request_end()
930
str_end[3] = ENCRYPT_REQEND;
931
TelnetSend(EncryptKSGlobalHack, str_end, sizeof(str_end), 0);
933
printsub('>', &str_end[2], sizeof(str_end) - 2);
935
if (encrypt_debug_mode) {
936
sprintf(dbgbuf, ">>>Request input to be clear text\n");
937
OutputDebugString(dbgbuf);
942
int encrypt_is_encrypting()
944
if (encrypt_output && decrypt_input)
954
encrypt_debug_mode = mode;
960
encrypt_gen_printsub(data, cnt, buf, buflen)
961
unsigned char *data, *buf;
968
buf[buflen-1] = '\0';
971
for (; cnt > 0; cnt--, data++) {
972
sprintf(tbuf, " %d", *data);
973
for (cp = tbuf; *cp && buflen > 0; --buflen)
982
encrypt_printsub(data, cnt, buf, buflen)
983
unsigned char *data, *buf;
987
register int type = data[1];
989
for (ep = encryptions; ep->type && ep->type != type; ep++)
993
(*ep->printsub)(data, cnt, buf, buflen);
995
encrypt_gen_printsub(data, cnt, buf, buflen);
999
#endif /* ENCRYPTION */