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
35 * Copyright (C) 1990 by the Massachusetts Institute of Technology
37 * Export of this software from the United States of America is assumed
38 * to require a specific license from the United States Government.
39 * It is the responsibility of any person or organization contemplating
40 * export to obtain such a license before exporting.
42 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
43 * distribute this software and its documentation for any purpose and
44 * without fee is hereby granted, provided that the above copyright
45 * notice appear in all copies and that both that copyright notice and
46 * this permission notice appear in supporting documentation, and that
47 * the name of M.I.T. not be used in advertising or publicity pertaining
48 * to distribution of the software without specific, written prior
49 * permission. M.I.T. makes no representations about the suitability of
50 * this software for any purpose. It is provided "as is" without express
51 * or implied warranty.
59 #if defined(ENCRYPTION)
62 #include <arpa/telnet.h>
77 * These functions pointers point to the current routines
78 * for encrypting and decrypting data.
80 void (*encrypt_output) (unsigned char *, int);
81 int (*decrypt_input) (int);
84 int encrypt_debug_mode = 0;
85 static int decrypt_mode = 0;
86 static int encrypt_mode = 0;
87 static int encrypt_verbose = 0;
88 static int autoencrypt = 0;
89 static int autodecrypt = 0;
90 static int havesessionkey = 0;
91 static int Server = 0;
92 static const char *Name = "Noname";
94 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
96 static long i_support_encrypt = typemask(ENCTYPE_DES_CFB64)
97 | typemask(ENCTYPE_DES_OFB64);
98 static long i_support_decrypt = typemask(ENCTYPE_DES_CFB64)
99 | typemask(ENCTYPE_DES_OFB64);
100 static long i_wont_support_encrypt = 0;
101 static long i_wont_support_decrypt = 0;
102 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
103 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
105 static long remote_supports_encrypt = 0;
106 static long remote_supports_decrypt = 0;
108 static Encryptions encryptions[] = {
109 #if defined(DES_ENCRYPTION)
110 { "DES_CFB64", ENCTYPE_DES_CFB64,
120 { "DES_OFB64", ENCTYPE_DES_OFB64,
134 static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
136 static unsigned char str_suplen = 0;
137 static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
138 static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
141 findencryption(int type)
143 Encryptions *ep = encryptions;
145 if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask(type)))
147 while (ep->type && ep->type != type)
149 return(ep->type ? ep : 0);
153 finddecryption(int type)
155 Encryptions *ep = encryptions;
157 if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask(type)))
159 while (ep->type && ep->type != type)
161 return(ep->type ? ep : 0);
166 static struct key_info {
167 unsigned char keyid[MAXKEYLEN];
171 Encryptions *(*getcrypt)();
173 { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
174 { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
178 encrypt_init(const char *name, int server)
180 Encryptions *ep = encryptions;
184 i_support_encrypt = i_support_decrypt = 0;
185 remote_supports_encrypt = remote_supports_decrypt = 0;
191 encrypt_verbose = !server;
197 if (encrypt_debug_mode)
198 printf(">>>%s: I will support %s\r\n",
199 Name, ENCTYPE_NAME(ep->type));
200 i_support_encrypt |= typemask(ep->type);
201 i_support_decrypt |= typemask(ep->type);
202 if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
203 if ((str_send[str_suplen++] = ep->type) == IAC)
204 str_send[str_suplen++] = IAC;
209 str_send[str_suplen++] = IAC;
210 str_send[str_suplen++] = SE;
214 encrypt_list_types(void)
216 Encryptions *ep = encryptions;
218 printf("Valid encryption types:\n");
220 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
226 EncryptEnable(char *type, char *mode)
228 if (isprefix(type, "help") || isprefix(type, "?")) {
229 printf("Usage: encrypt enable <type> [input|output]\n");
230 encrypt_list_types();
233 if (EncryptType(type, mode))
234 return(EncryptStart(mode));
239 EncryptDisable(char *type, char *mode)
244 if (isprefix(type, "help") || isprefix(type, "?")) {
245 printf("Usage: encrypt disable <type> [input|output]\n");
246 encrypt_list_types();
247 } else if ((ep = (Encryptions *)genget(type, (char**)encryptions,
248 sizeof(Encryptions))) == 0) {
249 printf("%s: invalid encryption type\n", type);
250 } else if (Ambiguous(ep)) {
251 printf("Ambiguous type '%s'\n", type);
253 if ((mode == 0) || (isprefix(mode, "input") ? 1 : 0)) {
254 if (decrypt_mode == ep->type)
256 i_wont_support_decrypt |= typemask(ep->type);
259 if ((mode == 0) || (isprefix(mode, "output"))) {
260 if (encrypt_mode == ep->type)
262 i_wont_support_encrypt |= typemask(ep->type);
266 printf("%s: invalid encryption mode\n", mode);
272 EncryptType(char *type, char *mode)
277 if (isprefix(type, "help") || isprefix(type, "?")) {
278 printf("Usage: encrypt type <type> [input|output]\n");
279 encrypt_list_types();
280 } else if ((ep = (Encryptions *)genget(type, (char**)encryptions,
281 sizeof(Encryptions))) == 0) {
282 printf("%s: invalid encryption type\n", type);
283 } else if (Ambiguous(ep)) {
284 printf("Ambiguous type '%s'\n", type);
286 if ((mode == 0) || isprefix(mode, "input")) {
287 decrypt_mode = ep->type;
288 i_wont_support_decrypt &= ~typemask(ep->type);
291 if ((mode == 0) || isprefix(mode, "output")) {
292 encrypt_mode = ep->type;
293 i_wont_support_encrypt &= ~typemask(ep->type);
297 printf("%s: invalid encryption mode\n", mode);
303 EncryptStart(char *mode)
307 if (isprefix(mode, "input"))
308 return(EncryptStartInput());
309 if (isprefix(mode, "output"))
310 return(EncryptStartOutput());
311 if (isprefix(mode, "help") || isprefix(mode, "?")) {
312 printf("Usage: encrypt start [input|output]\n");
315 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
318 ret += EncryptStartInput();
319 ret += EncryptStartOutput();
324 EncryptStartInput(void)
327 encrypt_send_request_start();
330 printf("No previous decryption mode, decryption not enabled\r\n");
335 EncryptStartOutput(void)
338 encrypt_start_output(encrypt_mode);
341 printf("No previous encryption mode, encryption not enabled\r\n");
346 EncryptStop(char *mode)
350 if (isprefix(mode, "input"))
351 return(EncryptStopInput());
352 if (isprefix(mode, "output"))
353 return(EncryptStopOutput());
354 if (isprefix(mode, "help") || isprefix(mode, "?")) {
355 printf("Usage: encrypt stop [input|output]\n");
358 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
361 ret += EncryptStopInput();
362 ret += EncryptStopOutput();
367 EncryptStopInput(void)
369 encrypt_send_request_end();
374 EncryptStopOutput(void)
381 encrypt_display(void)
383 printf("Autoencrypt for output is %s. Autodecrypt for input is %s.\r\n",
384 autoencrypt?"on":"off", autodecrypt?"on":"off");
387 printf("Currently encrypting output with %s\r\n",
388 ENCTYPE_NAME(encrypt_mode));
390 printf("Currently not encrypting output\r\n");
393 printf("Currently decrypting input with %s\r\n",
394 ENCTYPE_NAME(decrypt_mode));
396 printf("Currently not decrypting input\r\n");
402 printf("Autoencrypt for output is %s. Autodecrypt for input is %s.\r\n",
403 autoencrypt?"on":"off", autodecrypt?"on":"off");
406 printf("Currently encrypting output with %s\r\n",
407 ENCTYPE_NAME(encrypt_mode));
408 else if (encrypt_mode) {
409 printf("Currently output is clear text.\r\n");
410 printf("Last encryption mode was %s\r\n",
411 ENCTYPE_NAME(encrypt_mode));
413 printf("Currently not encrypting output\r\n");
416 printf("Currently decrypting input with %s\r\n",
417 ENCTYPE_NAME(decrypt_mode));
418 } else if (decrypt_mode) {
419 printf("Currently input is clear text.\r\n");
420 printf("Last decryption mode was %s\r\n",
421 ENCTYPE_NAME(decrypt_mode));
423 printf("Currently not decrypting input\r\n");
429 encrypt_send_support(void)
433 * If the user has requested that decryption start
434 * immediatly, then send a "REQUEST START" before
435 * we negotiate the type.
437 if (!Server && autodecrypt)
438 encrypt_send_request_start();
439 telnet_net_write(str_send, str_suplen);
440 printsub('>', &str_send[2], str_suplen - 2);
449 encrypt_debug_mode ^= 1;
451 encrypt_debug_mode = on;
452 printf("Encryption debugging %s\r\n",
453 encrypt_debug_mode ? "enabled" : "disabled");
457 /* turn on verbose encryption, but dont keep telling the whole world
459 void encrypt_verbose_quiet(int on)
462 encrypt_verbose ^= 1;
464 encrypt_verbose = on ? 1 : 0;
468 EncryptVerbose(int on)
470 encrypt_verbose_quiet(on);
471 printf("Encryption %s verbose\r\n",
472 encrypt_verbose ? "is" : "is not");
477 EncryptAutoEnc(int on)
480 printf("Automatic encryption of output is %s\r\n",
481 autoencrypt ? "enabled" : "disabled");
486 EncryptAutoDec(int on)
489 printf("Automatic decryption of input is %s\r\n",
490 autodecrypt ? "enabled" : "disabled");
494 /* Called when we receive a WONT or a DONT ENCRYPT after we sent a DO
500 printf("[ Connection is NOT encrypted ]\r\n");
502 printf("\r\n*** Connection not encrypted! "
503 "Communication may be eavesdropped. ***\r\n");
507 * Called when ENCRYPT SUPPORT is received.
510 encrypt_support(unsigned char *typelist, int cnt)
512 int type, use_type = 0;
516 * Forget anything the other side has previously told us.
518 remote_supports_decrypt = 0;
522 if (encrypt_debug_mode)
523 printf(">>>%s: He is supporting %s (%d)\r\n",
525 ENCTYPE_NAME(type), type);
526 if ((type < ENCTYPE_CNT) &&
527 (I_SUPPORT_ENCRYPT & typemask(type))) {
528 remote_supports_decrypt |= typemask(type);
534 ep = findencryption(use_type);
537 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
538 if (encrypt_debug_mode)
539 printf(">>>%s: (*ep->start)() returned %d\r\n",
543 encrypt_mode = use_type;
545 encrypt_start_output(use_type);
550 encrypt_is(unsigned char *data, int cnt)
558 if (type < ENCTYPE_CNT)
559 remote_supports_encrypt |= typemask(type);
560 if (!(ep = finddecryption(type))) {
561 if (encrypt_debug_mode)
562 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
564 ENCTYPE_NAME_OK(type)
565 ? ENCTYPE_NAME(type) : "(unknown)",
570 if (encrypt_debug_mode)
571 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
573 ENCTYPE_NAME_OK(type)
574 ? ENCTYPE_NAME(type) : "(unknown)",
578 ret = (*ep->is)(data, cnt);
579 if (encrypt_debug_mode)
580 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data, cnt,
581 (ret < 0) ? "FAIL " :
582 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
588 if (ret == 0 && autodecrypt)
589 encrypt_send_request_start();
594 encrypt_reply(unsigned char *data, int cnt)
602 if (!(ep = findencryption(type))) {
603 if (encrypt_debug_mode)
604 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
606 ENCTYPE_NAME_OK(type)
607 ? ENCTYPE_NAME(type) : "(unknown)",
612 if (encrypt_debug_mode)
613 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
615 ENCTYPE_NAME_OK(type)
616 ? ENCTYPE_NAME(type) : "(unknown)",
620 ret = (*ep->reply)(data, cnt);
621 if (encrypt_debug_mode)
622 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
624 (ret < 0) ? "FAIL " :
625 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
627 if (encrypt_debug_mode)
628 printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
633 if (ret == 0 && autoencrypt)
634 encrypt_start_output(type);
639 * Called when ENCRYPT START is received.
642 encrypt_start(unsigned char *data, int cnt)
648 * Something is wrong. We should not get a START
649 * command without having already picked our
650 * decryption scheme. Send a REQUEST-END to
651 * attempt to clear the channel...
653 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
654 encrypt_send_request_end();
658 if ((ep = finddecryption(decrypt_mode))) {
659 decrypt_input = ep->input;
661 printf("[ Input is now decrypted with type %s ]\r\n",
662 ENCTYPE_NAME(decrypt_mode));
663 if (encrypt_debug_mode)
664 printf(">>>%s: Start to decrypt input with type %s\r\n",
665 Name, ENCTYPE_NAME(decrypt_mode));
667 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
669 ENCTYPE_NAME_OK(decrypt_mode)
670 ? ENCTYPE_NAME(decrypt_mode)
673 encrypt_send_request_end();
678 encrypt_session_key(Session_Key *key, int server)
680 Encryptions *ep = encryptions;
686 (*ep->session)(key, server);
692 * Called when ENCRYPT END is received.
698 if (encrypt_debug_mode)
699 printf(">>>%s: Input is back to clear text\r\n", Name);
701 printf("[ Input is now clear text ]\r\n");
705 * Called when ENCRYPT REQUEST-END is received.
708 encrypt_request_end(void)
714 * Called when ENCRYPT REQUEST-START is received. If we receive
715 * this before a type is picked, then that indicates that the
716 * other side wants us to start encrypting data as soon as we
720 encrypt_request_start(unsigned char *data, int cnt)
722 if (encrypt_mode == 0) {
727 encrypt_start_output(encrypt_mode);
730 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
733 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
742 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
746 } else if (len == 0) {
748 * Empty option, indicates a failure.
754 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
756 } else if ((len != kp->keylen) || (memcmp(keyid,kp->keyid,len) != 0)) {
758 * Length or contents are different
761 memcpy(kp->keyid,keyid, len);
763 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
766 ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
767 if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
768 encrypt_start_output(*kp->modep);
772 encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
775 void encrypt_enc_keyid(unsigned char *keyid, int len)
777 encrypt_keyid(&ki[1], keyid, len);
780 void encrypt_dec_keyid(unsigned char *keyid, int len)
782 encrypt_keyid(&ki[0], keyid, len);
786 void encrypt_send_keyid(int dir, unsigned char *keyid, int keylen, int saveit)
790 str_keyid[3] = (dir == DIR_ENCRYPT)
791 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
793 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
794 memcpy(kp->keyid,keyid, keylen);
798 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
799 if ((*strp++ = *keyid++) == IAC)
804 telnet_net_write(str_keyid, strp - str_keyid);
805 printsub('>', &str_keyid[2], strp - str_keyid - 2);
814 autoencrypt = on ? 1 : 0;
823 autodecrypt = on ? 1 : 0;
827 encrypt_start_output(int type)
833 if (!(ep = findencryption(type))) {
834 if (encrypt_debug_mode) {
835 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
837 ENCTYPE_NAME_OK(type)
838 ? ENCTYPE_NAME(type) : "(unknown)",
844 i = (*ep->start)(DIR_ENCRYPT, Server);
845 if (encrypt_debug_mode) {
846 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
849 "initial negotiation in progress",
850 i, ENCTYPE_NAME(type));
856 *p++ = ENCRYPT_START;
857 for (i = 0; i < ki[0].keylen; ++i) {
858 if ((*p++ = ki[0].keyid[i]) == IAC)
863 telnet_net_write(str_start, p - str_start);
865 printsub('>', &str_start[2], p - &str_start[2]);
867 * If we are already encrypting in some mode, then
868 * encrypt the ring (which includes our request) in
869 * the old mode, mark it all as "clear text" and then
870 * switch to the new mode.
872 encrypt_output = ep->output;
874 if (encrypt_debug_mode)
875 printf(">>>%s: Started to encrypt output with type %s\r\n",
876 Name, ENCTYPE_NAME(type));
878 printf("[ Output is now encrypted with type %s ]\r\n",
883 encrypt_send_end(void)
888 str_end[3] = ENCRYPT_END;
889 telnet_net_write(str_end, sizeof(str_end));
891 printsub('>', &str_end[2], sizeof(str_end) - 2);
893 * Encrypt the output buffer now because it will not be done by
897 if (encrypt_debug_mode)
898 printf(">>>%s: Output is back to clear text\r\n", Name);
900 printf("[ Output is now clear text ]\r\n");
904 encrypt_send_request_start(void)
910 *p++ = ENCRYPT_REQSTART;
911 for (i = 0; i < ki[1].keylen; ++i) {
912 if ((*p++ = ki[1].keyid[i]) == IAC)
917 telnet_net_write(str_start, p - str_start);
918 printsub('>', &str_start[2], p - &str_start[2]);
919 if (encrypt_debug_mode)
920 printf(">>>%s: Request input to be encrypted\r\n", Name);
924 encrypt_send_request_end(void)
926 str_end[3] = ENCRYPT_REQEND;
927 telnet_net_write(str_end, sizeof(str_end));
928 printsub('>', &str_end[2], sizeof(str_end) - 2);
930 if (encrypt_debug_mode)
931 printf(">>>%s: Request input to be clear text\r\n", Name);
935 void encrypt_wait(void)
937 if (encrypt_debug_mode)
938 printf(">>>%s: in encrypt_wait\r\n", Name);
939 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
941 while (autoencrypt && !encrypt_output)
949 if(!havesessionkey ||
950 (I_SUPPORT_ENCRYPT & remote_supports_decrypt) == 0 ||
951 (I_SUPPORT_DECRYPT & remote_supports_encrypt) == 0)
953 if(!(encrypt_output && decrypt_input))
958 int encrypt_is_encrypting()
960 if (encrypt_output && decrypt_input)
966 encrypt_debug(int mode)
968 encrypt_debug_mode = mode;
971 void encrypt_gen_printsub(unsigned char *data, size_t cnt,
972 unsigned char *buf, size_t buflen)
978 buf[buflen-1] = '\0';
981 for (; cnt > 0; cnt--, data++) {
982 snprintf(tbuf, sizeof(tbuf), " %d", *data);
983 for (cp = tbuf; *cp && buflen > 0; --buflen)
992 encrypt_printsub(unsigned char *data, size_t cnt,
993 unsigned char *buf, size_t buflen)
998 for (ep = encryptions; ep->type && ep->type != type; ep++)
1002 (*ep->printsub)(data, cnt, buf, buflen);
1004 encrypt_gen_printsub(data, cnt, buf, buflen);