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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)encrypt.c 8.2 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/libtelnet/encrypt.c,v 1.3.2.2 2002/04/13 10:59:07 markm Exp $
34 * Copyright (C) 1990 by the Massachusetts Institute of Technology
36 * Export of this software from the United States of America is assumed
37 * to require a specific license from the United States Government.
38 * It is the responsibility of any person or organization contemplating
39 * export to obtain such a license before exporting.
41 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42 * distribute this software and its documentation for any purpose and
43 * without fee is hereby granted, provided that the above copyright
44 * notice appear in all copies and that both that copyright notice and
45 * this permission notice appear in supporting documentation, and that
46 * the name of M.I.T. not be used in advertising or publicity pertaining
47 * to distribution of the software without specific, written prior
48 * permission. M.I.T. makes no representations about the suitability of
49 * this software for any purpose. It is provided "as is" without express
50 * or implied warranty.
56 #include <arpa/telnet.h>
64 int EncryptType(char *type, char *mode);
65 int EncryptStart(char *mode);
66 int EncryptStop(char *mode);
67 int EncryptStartInput(void);
68 int EncryptStartOutput(void);
69 int EncryptStopInput(void);
70 int EncryptStopOutput(void);
72 int encrypt_debug_mode = 0;
73 static int decrypt_mode = 0;
74 static int encrypt_mode = 0;
75 static int encrypt_verbose = 0;
76 static int autoencrypt = 0;
77 static int autodecrypt = 0;
78 static int havesessionkey = 0;
79 static int Server = 0;
80 static const char *Name = "Noname";
82 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
84 static long i_support_encrypt = 0
85 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
87 static long i_support_decrypt = 0
88 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
91 static long i_wont_support_encrypt = 0;
92 static long i_wont_support_decrypt = 0;
93 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
94 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
96 static long remote_supports_encrypt = 0;
97 static long remote_supports_decrypt = 0;
99 static Encryptions encryptions[] = {
100 { "DES_CFB64", ENCTYPE_DES_CFB64,
110 { "DES_OFB64", ENCTYPE_DES_OFB64,
120 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
123 static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
125 static unsigned char str_suplen = 0;
126 static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
127 static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
130 findencryption(int type)
132 Encryptions *ep = encryptions;
134 if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & (unsigned)typemask(type)))
136 while (ep->type && ep->type != type)
138 return(ep->type ? ep : 0);
142 finddecryption(int type)
144 Encryptions *ep = encryptions;
146 if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & (unsigned)typemask(type)))
148 while (ep->type && ep->type != type)
150 return(ep->type ? ep : 0);
155 static struct key_info {
156 unsigned char keyid[MAXKEYLEN];
160 Encryptions *(*getcrypt)(int);
162 { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
163 { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
166 static void encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len);
169 encrypt_init(const char *name, int server)
171 Encryptions *ep = encryptions;
175 i_support_encrypt = i_support_decrypt = 0;
176 remote_supports_encrypt = remote_supports_decrypt = 0;
179 encrypt_output = NULL;
180 decrypt_input = NULL;
185 if (encrypt_debug_mode)
186 printf(">>>%s: I will support %s\r\n",
187 Name, ENCTYPE_NAME(ep->type));
188 i_support_encrypt |= typemask(ep->type);
189 i_support_decrypt |= typemask(ep->type);
190 if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
191 if ((str_send[str_suplen++] = ep->type) == IAC)
192 str_send[str_suplen++] = IAC;
197 str_send[str_suplen++] = IAC;
198 str_send[str_suplen++] = SE;
202 encrypt_list_types(void)
204 Encryptions *ep = encryptions;
206 printf("Valid encryption types:\n");
208 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
214 EncryptEnable(char *type, char *mode)
216 if (isprefix(type, "help") || isprefix(type, "?")) {
217 printf("Usage: encrypt enable <type> [input|output]\n");
218 encrypt_list_types();
221 if (EncryptType(type, mode))
222 return(EncryptStart(mode));
227 EncryptDisable(char *type, char *mode)
232 if (isprefix(type, "help") || isprefix(type, "?")) {
233 printf("Usage: encrypt disable <type> [input|output]\n");
234 encrypt_list_types();
235 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
236 sizeof(Encryptions))) == NULL) {
237 printf("%s: invalid encryption type\n", type);
238 } else if (Ambiguous((char **)ep)) {
239 printf("Ambiguous type '%s'\n", type);
241 if ((mode == NULL) || (isprefix(mode, "input") ? 1 : 0)) {
242 if (decrypt_mode == ep->type)
244 i_wont_support_decrypt |= typemask(ep->type);
247 if ((mode == NULL) || (isprefix(mode, "output"))) {
248 if (encrypt_mode == ep->type)
250 i_wont_support_encrypt |= typemask(ep->type);
254 printf("%s: invalid encryption mode\n", mode);
260 EncryptType(char *type, char *mode)
265 if (isprefix(type, "help") || isprefix(type, "?")) {
266 printf("Usage: encrypt type <type> [input|output]\n");
267 encrypt_list_types();
268 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
269 sizeof(Encryptions))) == NULL) {
270 printf("%s: invalid encryption type\n", type);
271 } else if (Ambiguous((char **)ep)) {
272 printf("Ambiguous type '%s'\n", type);
274 if ((mode == NULL) || isprefix(mode, "input")) {
275 decrypt_mode = ep->type;
276 i_wont_support_decrypt &= ~typemask(ep->type);
279 if ((mode == NULL) || isprefix(mode, "output")) {
280 encrypt_mode = ep->type;
281 i_wont_support_encrypt &= ~typemask(ep->type);
285 printf("%s: invalid encryption mode\n", mode);
291 EncryptStart(char *mode)
295 if (isprefix(mode, "input"))
296 return(EncryptStartInput());
297 if (isprefix(mode, "output"))
298 return(EncryptStartOutput());
299 if (isprefix(mode, "help") || isprefix(mode, "?")) {
300 printf("Usage: encrypt start [input|output]\n");
303 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
306 ret += EncryptStartInput();
307 ret += EncryptStartOutput();
312 EncryptStartInput(void)
315 encrypt_send_request_start();
318 printf("No previous decryption mode, decryption not enabled\r\n");
323 EncryptStartOutput(void)
326 encrypt_start_output(encrypt_mode);
329 printf("No previous encryption mode, encryption not enabled\r\n");
334 EncryptStop(char *mode)
338 if (isprefix(mode, "input"))
339 return(EncryptStopInput());
340 if (isprefix(mode, "output"))
341 return(EncryptStopOutput());
342 if (isprefix(mode, "help") || isprefix(mode, "?")) {
343 printf("Usage: encrypt stop [input|output]\n");
346 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
349 ret += EncryptStopInput();
350 ret += EncryptStopOutput();
355 EncryptStopInput(void)
357 encrypt_send_request_end();
362 EncryptStopOutput(void)
369 encrypt_display(void)
372 printf("Currently encrypting output with %s\r\n",
373 ENCTYPE_NAME(encrypt_mode));
375 printf("Currently decrypting input with %s\r\n",
376 ENCTYPE_NAME(decrypt_mode));
383 printf("Currently encrypting output with %s\r\n",
384 ENCTYPE_NAME(encrypt_mode));
385 else if (encrypt_mode) {
386 printf("Currently output is clear text.\r\n");
387 printf("Last encryption mode was %s\r\n",
388 ENCTYPE_NAME(encrypt_mode));
391 printf("Currently decrypting input with %s\r\n",
392 ENCTYPE_NAME(decrypt_mode));
393 } else if (decrypt_mode) {
394 printf("Currently input is clear text.\r\n");
395 printf("Last decryption mode was %s\r\n",
396 ENCTYPE_NAME(decrypt_mode));
402 encrypt_send_support(void)
406 * If the user has requested that decryption start
407 * immediatly, then send a "REQUEST START" before
408 * we negotiate the type.
410 if (!Server && autodecrypt)
411 encrypt_send_request_start();
412 net_write(str_send, str_suplen);
413 printsub('>', &str_send[2], str_suplen - 2);
422 encrypt_debug_mode ^= 1;
424 encrypt_debug_mode = on;
425 printf("Encryption debugging %s\r\n",
426 encrypt_debug_mode ? "enabled" : "disabled");
431 EncryptVerbose(int on)
434 encrypt_verbose ^= 1;
436 encrypt_verbose = on;
437 printf("Encryption %s verbose\r\n",
438 encrypt_verbose ? "is" : "is not");
443 EncryptAutoEnc(int on)
446 printf("Automatic encryption of output is %s\r\n",
447 autoencrypt ? "enabled" : "disabled");
452 EncryptAutoDec(int on)
455 printf("Automatic decryption of input is %s\r\n",
456 autodecrypt ? "enabled" : "disabled");
461 * Called when ENCRYPT SUPPORT is received.
464 encrypt_support(unsigned char *typelist, int cnt)
466 int type, use_type = 0;
470 * Forget anything the other side has previously told us.
472 remote_supports_decrypt = 0;
476 if (encrypt_debug_mode)
477 printf(">>>%s: He is supporting %s (%d)\r\n",
479 ENCTYPE_NAME(type), type);
480 if ((type < ENCTYPE_CNT) &&
481 (I_SUPPORT_ENCRYPT & typemask(type))) {
482 remote_supports_decrypt |= typemask(type);
488 ep = findencryption(use_type);
491 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
492 if (encrypt_debug_mode)
493 printf(">>>%s: (*ep->start)() returned %d\r\n",
497 encrypt_mode = use_type;
499 encrypt_start_output(use_type);
504 encrypt_is(unsigned char *data, int cnt)
512 if (type < ENCTYPE_CNT)
513 remote_supports_encrypt |= typemask(type);
514 if (!(ep = finddecryption(type))) {
515 if (encrypt_debug_mode)
516 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
518 ENCTYPE_NAME_OK(type)
519 ? ENCTYPE_NAME(type) : "(unknown)",
524 if (encrypt_debug_mode)
525 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
527 ENCTYPE_NAME_OK(type)
528 ? ENCTYPE_NAME(type) : "(unknown)",
532 ret = (*ep->is)(data, cnt);
533 if (encrypt_debug_mode)
534 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data, cnt,
535 (ret < 0) ? "FAIL " :
536 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
542 if (ret == 0 && autodecrypt)
543 encrypt_send_request_start();
548 encrypt_reply(unsigned char *data, int cnt)
556 if (!(ep = findencryption(type))) {
557 if (encrypt_debug_mode)
558 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
560 ENCTYPE_NAME_OK(type)
561 ? ENCTYPE_NAME(type) : "(unknown)",
566 if (encrypt_debug_mode)
567 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
569 ENCTYPE_NAME_OK(type)
570 ? ENCTYPE_NAME(type) : "(unknown)",
574 ret = (*ep->reply)(data, cnt);
575 if (encrypt_debug_mode)
576 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
578 (ret < 0) ? "FAIL " :
579 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
581 if (encrypt_debug_mode)
582 printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
587 if (ret == 0 && autoencrypt)
588 encrypt_start_output(type);
593 * Called when a ENCRYPT START command is received.
596 encrypt_start(unsigned char *data __unused, int cnt __unused)
602 * Something is wrong. We should not get a START
603 * command without having already picked our
604 * decryption scheme. Send a REQUEST-END to
605 * attempt to clear the channel...
607 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
608 encrypt_send_request_end();
612 if ((ep = finddecryption(decrypt_mode))) {
613 decrypt_input = ep->input;
615 printf("[ Input is now decrypted with type %s ]\r\n",
616 ENCTYPE_NAME(decrypt_mode));
617 if (encrypt_debug_mode)
618 printf(">>>%s: Start to decrypt input with type %s\r\n",
619 Name, ENCTYPE_NAME(decrypt_mode));
621 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
623 ENCTYPE_NAME_OK(decrypt_mode)
624 ? ENCTYPE_NAME(decrypt_mode)
627 encrypt_send_request_end();
632 encrypt_session_key( Session_Key *key, int server)
634 Encryptions *ep = encryptions;
640 (*ep->session)(key, server);
646 * Called when ENCRYPT END is received.
651 decrypt_input = NULL;
652 if (encrypt_debug_mode)
653 printf(">>>%s: Input is back to clear text\r\n", Name);
655 printf("[ Input is now clear text ]\r\n");
659 * Called when ENCRYPT REQUEST-END is received.
662 encrypt_request_end(void)
668 * Called when ENCRYPT REQUEST-START is received. If we receive
669 * this before a type is picked, then that indicates that the
670 * other side wants us to start encrypting data as soon as we
674 encrypt_request_start(unsigned char *data __unused, int cnt __unused)
676 if (encrypt_mode == 0) {
681 encrypt_start_output(encrypt_mode);
684 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
687 encrypt_enc_keyid(unsigned char *keyid, int len)
689 encrypt_keyid(&ki[1], keyid, len);
693 encrypt_dec_keyid(unsigned char *keyid, int len)
695 encrypt_keyid(&ki[0], keyid, len);
699 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
708 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
712 } else if (len == 0) {
714 * Empty option, indicates a failure.
720 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
722 } else if ((len != kp->keylen) ||
723 (memcmp(keyid, kp->keyid, len) != 0)) {
725 * Length or contents are different
728 memmove(kp->keyid, keyid, len);
730 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
733 ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
734 if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
735 encrypt_start_output(*kp->modep);
739 encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
743 encrypt_send_keyid(int dir, const char *keyid, int keylen, int saveit)
747 str_keyid[3] = (dir == DIR_ENCRYPT)
748 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
750 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
751 memmove(kp->keyid, keyid, keylen);
755 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
756 if ((*strp++ = *keyid++) == IAC)
761 net_write(str_keyid, strp - str_keyid);
762 printsub('>', &str_keyid[2], strp - str_keyid - 2);
771 autoencrypt = on ? 1 : 0;
780 autodecrypt = on ? 1 : 0;
784 encrypt_start_output(int type)
790 if (!(ep = findencryption(type))) {
791 if (encrypt_debug_mode) {
792 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
794 ENCTYPE_NAME_OK(type)
795 ? ENCTYPE_NAME(type) : "(unknown)",
801 i = (*ep->start)(DIR_ENCRYPT, Server);
802 if (encrypt_debug_mode) {
803 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
806 "initial negotiation in progress",
807 i, ENCTYPE_NAME(type));
813 *p++ = ENCRYPT_START;
814 for (i = 0; i < ki[0].keylen; ++i) {
815 if ((*p++ = ki[0].keyid[i]) == IAC)
820 net_write(str_start, p - str_start);
822 printsub('>', &str_start[2], p - &str_start[2]);
824 * If we are already encrypting in some mode, then
825 * encrypt the ring (which includes our request) in
826 * the old mode, mark it all as "clear text" and then
827 * switch to the new mode.
829 encrypt_output = ep->output;
831 if (encrypt_debug_mode)
832 printf(">>>%s: Started to encrypt output with type %s\r\n",
833 Name, ENCTYPE_NAME(type));
835 printf("[ Output is now encrypted with type %s ]\r\n",
840 encrypt_send_end(void)
845 str_end[3] = ENCRYPT_END;
846 net_write(str_end, sizeof(str_end));
848 printsub('>', &str_end[2], sizeof(str_end) - 2);
850 * Encrypt the output buffer now because it will not be done by
853 encrypt_output = NULL;
854 if (encrypt_debug_mode)
855 printf(">>>%s: Output is back to clear text\r\n", Name);
857 printf("[ Output is now clear text ]\r\n");
861 encrypt_send_request_start(void)
867 *p++ = ENCRYPT_REQSTART;
868 for (i = 0; i < ki[1].keylen; ++i) {
869 if ((*p++ = ki[1].keyid[i]) == IAC)
874 net_write(str_start, p - str_start);
875 printsub('>', &str_start[2], p - &str_start[2]);
876 if (encrypt_debug_mode)
877 printf(">>>%s: Request input to be encrypted\r\n", Name);
881 encrypt_send_request_end(void)
883 str_end[3] = ENCRYPT_REQEND;
884 net_write(str_end, sizeof(str_end));
885 printsub('>', &str_end[2], sizeof(str_end) - 2);
887 if (encrypt_debug_mode)
888 printf(">>>%s: Request input to be clear text\r\n", Name);
894 if (encrypt_debug_mode)
895 printf(">>>%s: in encrypt_wait\r\n", Name);
896 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
898 while (autoencrypt && !encrypt_output)
904 encrypt_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
910 buf[buflen-1] = '\0';
913 for (; cnt > 0; cnt--, data++) {
914 sprintf(tbuf, " %d", *data);
915 for (cp = tbuf; *cp && buflen > 0; --buflen)
924 encrypt_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
929 for (ep = encryptions; ep->type && ep->type != type; ep++)
933 (*ep->printsub)(data, cnt, buf, buflen);
935 encrypt_gen_printsub(data, cnt, buf, buflen);
937 #endif /* ENCRYPTION */