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 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 static const char sccsid[] = "@(#)encrypt.c 8.2 (Berkeley) 5/30/95";
45 * Copyright (C) 1990 by the Massachusetts Institute of Technology
47 * Export of this software from the United States of America is assumed
48 * to require a specific license from the United States Government.
49 * It is the responsibility of any person or organization contemplating
50 * export to obtain such a license before exporting.
52 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
53 * distribute this software and its documentation for any purpose and
54 * without fee is hereby granted, provided that the above copyright
55 * notice appear in all copies and that both that copyright notice and
56 * this permission notice appear in supporting documentation, and that
57 * the name of M.I.T. not be used in advertising or publicity pertaining
58 * to distribution of the software without specific, written prior
59 * permission. M.I.T. makes no representations about the suitability of
60 * this software for any purpose. It is provided "as is" without express
61 * or implied warranty.
66 #include <sys/types.h>
68 #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);
83 int EncryptType(char *type, char *mode);
84 int EncryptStart(char *mode);
85 int EncryptStop(char *mode);
86 int EncryptStartInput(void);
87 int EncryptStartOutput(void);
88 int EncryptStopInput(void);
89 int EncryptStopOutput(void);
91 int encrypt_debug_mode = 0;
92 static int decrypt_mode = 0;
93 static int encrypt_mode = 0;
94 static int encrypt_verbose = 0;
95 static int autoencrypt = 0;
96 static int autodecrypt = 0;
97 static int havesessionkey = 0;
98 static int Server = 0;
99 static const char *Name = "Noname";
101 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
103 static u_long i_support_encrypt = 0
104 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
106 static u_long i_support_decrypt = 0
107 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
110 static u_long i_wont_support_encrypt = 0;
111 static u_long i_wont_support_decrypt = 0;
112 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
113 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
115 static u_long remote_supports_encrypt = 0;
116 static u_long remote_supports_decrypt = 0;
118 static Encryptions encryptions[] = {
119 { "DES_CFB64", ENCTYPE_DES_CFB64,
129 { "DES_OFB64", ENCTYPE_DES_OFB64,
139 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
142 static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
144 static unsigned char str_suplen = 0;
145 static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
146 static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
149 findencryption(int type)
151 Encryptions *ep = encryptions;
153 if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & (unsigned)typemask(type)))
155 while (ep->type && ep->type != type)
157 return(ep->type ? ep : 0);
161 finddecryption(int type)
163 Encryptions *ep = encryptions;
165 if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & (unsigned)typemask(type)))
167 while (ep->type && ep->type != type)
169 return(ep->type ? ep : 0);
174 static struct key_info {
175 unsigned char keyid[MAXKEYLEN];
179 Encryptions *(*getcrypt)(int);
181 { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
182 { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
185 static void encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len);
188 encrypt_init(const char *name, int server)
190 Encryptions *ep = encryptions;
194 i_support_encrypt = i_support_decrypt = 0;
195 remote_supports_encrypt = remote_supports_decrypt = 0;
204 if (encrypt_debug_mode)
205 printf(">>>%s: I will support %s\r\n",
206 Name, ENCTYPE_NAME(ep->type));
207 i_support_encrypt |= typemask(ep->type);
208 i_support_decrypt |= typemask(ep->type);
209 if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
210 if ((str_send[str_suplen++] = ep->type) == IAC)
211 str_send[str_suplen++] = IAC;
216 str_send[str_suplen++] = IAC;
217 str_send[str_suplen++] = SE;
221 encrypt_list_types(void)
223 Encryptions *ep = encryptions;
225 printf("Valid encryption types:\n");
227 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
233 EncryptEnable(char *type, char *mode)
235 if (isprefix(type, "help") || isprefix(type, "?")) {
236 printf("Usage: encrypt enable <type> [input|output]\n");
237 encrypt_list_types();
240 if (EncryptType(type, mode))
241 return(EncryptStart(mode));
246 EncryptDisable(char *type, char *mode)
251 if (isprefix(type, "help") || isprefix(type, "?")) {
252 printf("Usage: encrypt disable <type> [input|output]\n");
253 encrypt_list_types();
254 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
255 sizeof(Encryptions))) == 0) {
256 printf("%s: invalid encryption type\n", type);
257 } else if (Ambiguous((char **)ep)) {
258 printf("Ambiguous type '%s'\n", type);
260 if ((mode == 0) || (isprefix(mode, "input") ? 1 : 0)) {
261 if (decrypt_mode == ep->type)
263 i_wont_support_decrypt |= typemask(ep->type);
266 if ((mode == 0) || (isprefix(mode, "output"))) {
267 if (encrypt_mode == ep->type)
269 i_wont_support_encrypt |= typemask(ep->type);
273 printf("%s: invalid encryption mode\n", mode);
279 EncryptType(char *type, char *mode)
284 if (isprefix(type, "help") || isprefix(type, "?")) {
285 printf("Usage: encrypt type <type> [input|output]\n");
286 encrypt_list_types();
287 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
288 sizeof(Encryptions))) == 0) {
289 printf("%s: invalid encryption type\n", type);
290 } else if (Ambiguous((char **)ep)) {
291 printf("Ambiguous type '%s'\n", type);
293 if ((mode == 0) || isprefix(mode, "input")) {
294 decrypt_mode = ep->type;
295 i_wont_support_decrypt &= ~typemask(ep->type);
298 if ((mode == 0) || isprefix(mode, "output")) {
299 encrypt_mode = ep->type;
300 i_wont_support_encrypt &= ~typemask(ep->type);
304 printf("%s: invalid encryption mode\n", mode);
310 EncryptStart(char *mode)
314 if (isprefix(mode, "input"))
315 return(EncryptStartInput());
316 if (isprefix(mode, "output"))
317 return(EncryptStartOutput());
318 if (isprefix(mode, "help") || isprefix(mode, "?")) {
319 printf("Usage: encrypt start [input|output]\n");
322 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
325 ret += EncryptStartInput();
326 ret += EncryptStartOutput();
331 EncryptStartInput(void)
334 encrypt_send_request_start();
337 printf("No previous decryption mode, decryption not enabled\r\n");
342 EncryptStartOutput(void)
345 encrypt_start_output(encrypt_mode);
348 printf("No previous encryption mode, encryption not enabled\r\n");
353 EncryptStop(char *mode)
357 if (isprefix(mode, "input"))
358 return(EncryptStopInput());
359 if (isprefix(mode, "output"))
360 return(EncryptStopOutput());
361 if (isprefix(mode, "help") || isprefix(mode, "?")) {
362 printf("Usage: encrypt stop [input|output]\n");
365 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
368 ret += EncryptStopInput();
369 ret += EncryptStopOutput();
374 EncryptStopInput(void)
376 encrypt_send_request_end();
381 EncryptStopOutput(void)
388 encrypt_display(void)
391 printf("Currently encrypting output with %s\r\n",
392 ENCTYPE_NAME(encrypt_mode));
394 printf("Currently decrypting input with %s\r\n",
395 ENCTYPE_NAME(decrypt_mode));
402 printf("Currently encrypting output with %s\r\n",
403 ENCTYPE_NAME(encrypt_mode));
404 else if (encrypt_mode) {
405 printf("Currently output is clear text.\r\n");
406 printf("Last encryption mode was %s\r\n",
407 ENCTYPE_NAME(encrypt_mode));
410 printf("Currently decrypting input with %s\r\n",
411 ENCTYPE_NAME(decrypt_mode));
412 } else if (decrypt_mode) {
413 printf("Currently input is clear text.\r\n");
414 printf("Last decryption mode was %s\r\n",
415 ENCTYPE_NAME(decrypt_mode));
421 encrypt_send_support(void)
425 * If the user has requested that decryption start
426 * immediatly, then send a "REQUEST START" before
427 * we negotiate the type.
429 if (!Server && autodecrypt)
430 encrypt_send_request_start();
431 net_write(str_send, str_suplen);
432 printsub('>', &str_send[2], str_suplen - 2);
441 encrypt_debug_mode ^= 1;
443 encrypt_debug_mode = on;
444 printf("Encryption debugging %s\r\n",
445 encrypt_debug_mode ? "enabled" : "disabled");
450 EncryptVerbose(int on)
453 encrypt_verbose ^= 1;
455 encrypt_verbose = on;
456 printf("Encryption %s verbose\r\n",
457 encrypt_verbose ? "is" : "is not");
462 EncryptAutoEnc(int on)
465 printf("Automatic encryption of output is %s\r\n",
466 autoencrypt ? "enabled" : "disabled");
471 EncryptAutoDec(int on)
474 printf("Automatic decryption of input is %s\r\n",
475 autodecrypt ? "enabled" : "disabled");
480 * Called when ENCRYPT SUPPORT is received.
483 encrypt_support(unsigned char *typelist, int cnt)
485 int type, use_type = 0;
489 * Forget anything the other side has previously told us.
491 remote_supports_decrypt = 0;
495 if (encrypt_debug_mode)
496 printf(">>>%s: He is supporting %s (%d)\r\n",
498 ENCTYPE_NAME(type), type);
499 if ((type < ENCTYPE_CNT) &&
500 (I_SUPPORT_ENCRYPT & typemask(type))) {
501 remote_supports_decrypt |= typemask(type);
507 ep = findencryption(use_type);
510 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
511 if (encrypt_debug_mode)
512 printf(">>>%s: (*ep->start)() returned %d\r\n",
516 encrypt_mode = use_type;
518 encrypt_start_output(use_type);
523 encrypt_is(unsigned char *data, int cnt)
531 if (type < ENCTYPE_CNT)
532 remote_supports_encrypt |= typemask(type);
533 if (!(ep = finddecryption(type))) {
534 if (encrypt_debug_mode)
535 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
537 ENCTYPE_NAME_OK(type)
538 ? ENCTYPE_NAME(type) : "(unknown)",
543 if (encrypt_debug_mode)
544 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
546 ENCTYPE_NAME_OK(type)
547 ? ENCTYPE_NAME(type) : "(unknown)",
551 ret = (*ep->is)(data, cnt);
552 if (encrypt_debug_mode)
553 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data, cnt,
554 (ret < 0) ? "FAIL " :
555 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
561 if (ret == 0 && autodecrypt)
562 encrypt_send_request_start();
567 encrypt_reply(unsigned char *data, int cnt)
575 if (!(ep = findencryption(type))) {
576 if (encrypt_debug_mode)
577 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
579 ENCTYPE_NAME_OK(type)
580 ? ENCTYPE_NAME(type) : "(unknown)",
585 if (encrypt_debug_mode)
586 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
588 ENCTYPE_NAME_OK(type)
589 ? ENCTYPE_NAME(type) : "(unknown)",
593 ret = (*ep->reply)(data, cnt);
594 if (encrypt_debug_mode)
595 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
597 (ret < 0) ? "FAIL " :
598 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
600 if (encrypt_debug_mode)
601 printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
606 if (ret == 0 && autoencrypt)
607 encrypt_start_output(type);
612 * Called when a ENCRYPT START command is received.
615 encrypt_start(unsigned char *data __unused, int cnt __unused)
621 * Something is wrong. We should not get a START
622 * command without having already picked our
623 * decryption scheme. Send a REQUEST-END to
624 * attempt to clear the channel...
626 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
627 encrypt_send_request_end();
631 if ((ep = finddecryption(decrypt_mode))) {
632 decrypt_input = ep->input;
634 printf("[ Input is now decrypted with type %s ]\r\n",
635 ENCTYPE_NAME(decrypt_mode));
636 if (encrypt_debug_mode)
637 printf(">>>%s: Start to decrypt input with type %s\r\n",
638 Name, ENCTYPE_NAME(decrypt_mode));
640 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
642 ENCTYPE_NAME_OK(decrypt_mode)
643 ? ENCTYPE_NAME(decrypt_mode)
646 encrypt_send_request_end();
651 encrypt_session_key( Session_Key *key, int server)
653 Encryptions *ep = encryptions;
659 (*ep->session)(key, server);
665 * Called when ENCRYPT END is received.
671 if (encrypt_debug_mode)
672 printf(">>>%s: Input is back to clear text\r\n", Name);
674 printf("[ Input is now clear text ]\r\n");
678 * Called when ENCRYPT REQUEST-END is received.
681 encrypt_request_end(void)
687 * Called when ENCRYPT REQUEST-START is received. If we receive
688 * this before a type is picked, then that indicates that the
689 * other side wants us to start encrypting data as soon as we
693 encrypt_request_start(unsigned char *data __unused, int cnt __unused)
695 if (encrypt_mode == 0) {
700 encrypt_start_output(encrypt_mode);
703 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
706 encrypt_enc_keyid(unsigned char *keyid, int len)
708 encrypt_keyid(&ki[1], keyid, len);
712 encrypt_dec_keyid(unsigned char *keyid, int len)
714 encrypt_keyid(&ki[0], keyid, len);
718 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
727 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
731 } else if (len == 0) {
733 * Empty option, indicates a failure.
739 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
741 } else if ((len != kp->keylen) ||
742 (memcmp(keyid, kp->keyid, len) != 0)) {
744 * Length or contents are different
747 memmove(kp->keyid, keyid, len);
749 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
752 ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
753 if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
754 encrypt_start_output(*kp->modep);
758 encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
762 encrypt_send_keyid(int dir, const char *keyid, int keylen, int saveit)
766 str_keyid[3] = (dir == DIR_ENCRYPT)
767 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
769 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
770 memmove(kp->keyid, keyid, keylen);
774 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
775 if ((*strp++ = *keyid++) == IAC)
780 net_write(str_keyid, strp - str_keyid);
781 printsub('>', &str_keyid[2], strp - str_keyid - 2);
790 autoencrypt = on ? 1 : 0;
799 autodecrypt = on ? 1 : 0;
803 encrypt_start_output(int type)
809 if (!(ep = findencryption(type))) {
810 if (encrypt_debug_mode) {
811 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
813 ENCTYPE_NAME_OK(type)
814 ? ENCTYPE_NAME(type) : "(unknown)",
820 i = (*ep->start)(DIR_ENCRYPT, Server);
821 if (encrypt_debug_mode) {
822 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
825 "initial negotiation in progress",
826 i, ENCTYPE_NAME(type));
832 *p++ = ENCRYPT_START;
833 for (i = 0; i < ki[0].keylen; ++i) {
834 if ((*p++ = ki[0].keyid[i]) == IAC)
839 net_write(str_start, p - str_start);
841 printsub('>', &str_start[2], p - &str_start[2]);
843 * If we are already encrypting in some mode, then
844 * encrypt the ring (which includes our request) in
845 * the old mode, mark it all as "clear text" and then
846 * switch to the new mode.
848 encrypt_output = ep->output;
850 if (encrypt_debug_mode)
851 printf(">>>%s: Started to encrypt output with type %s\r\n",
852 Name, ENCTYPE_NAME(type));
854 printf("[ Output is now encrypted with type %s ]\r\n",
859 encrypt_send_end(void)
864 str_end[3] = ENCRYPT_END;
865 net_write(str_end, sizeof(str_end));
867 printsub('>', &str_end[2], sizeof(str_end) - 2);
869 * Encrypt the output buffer now because it will not be done by
873 if (encrypt_debug_mode)
874 printf(">>>%s: Output is back to clear text\r\n", Name);
876 printf("[ Output is now clear text ]\r\n");
880 encrypt_send_request_start(void)
886 *p++ = ENCRYPT_REQSTART;
887 for (i = 0; i < ki[1].keylen; ++i) {
888 if ((*p++ = ki[1].keyid[i]) == IAC)
893 net_write(str_start, p - str_start);
894 printsub('>', &str_start[2], p - &str_start[2]);
895 if (encrypt_debug_mode)
896 printf(">>>%s: Request input to be encrypted\r\n", Name);
900 encrypt_send_request_end(void)
902 str_end[3] = ENCRYPT_REQEND;
903 net_write(str_end, sizeof(str_end));
904 printsub('>', &str_end[2], sizeof(str_end) - 2);
906 if (encrypt_debug_mode)
907 printf(">>>%s: Request input to be clear text\r\n", Name);
913 if (encrypt_debug_mode)
914 printf(">>>%s: in encrypt_wait\r\n", Name);
915 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
917 while (autoencrypt && !encrypt_output)
923 encrypt_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
929 buf[buflen-1] = '\0';
932 for (; cnt > 0; cnt--, data++) {
933 sprintf(tbuf, " %d", *data);
934 for (cp = tbuf; *cp && buflen > 0; --buflen)
943 encrypt_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
948 for (ep = encryptions; ep->type && ep->type != type; ep++)
952 (*ep->printsub)(data, cnt, buf, buflen);
954 encrypt_gen_printsub(data, cnt, buf, buflen);
956 #endif /* ENCRYPTION */