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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 static const char sccsid[] = "@(#)encrypt.c 8.2 (Berkeley) 5/30/95";
41 * Copyright (C) 1990 by the Massachusetts Institute of Technology
43 * Export of this software from the United States of America is assumed
44 * to require a specific license from the United States Government.
45 * It is the responsibility of any person or organization contemplating
46 * export to obtain such a license before exporting.
48 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
49 * distribute this software and its documentation for any purpose and
50 * without fee is hereby granted, provided that the above copyright
51 * notice appear in all copies and that both that copyright notice and
52 * this permission notice appear in supporting documentation, and that
53 * the name of M.I.T. not be used in advertising or publicity pertaining
54 * to distribution of the software without specific, written prior
55 * permission. M.I.T. makes no representations about the suitability of
56 * this software for any purpose. It is provided "as is" without express
57 * or implied warranty.
62 #include <sys/types.h>
64 #include <arpa/telnet.h>
73 * These functions pointers point to the current routines
74 * for encrypting and decrypting data.
76 void (*encrypt_output)(unsigned char *, int);
77 int (*decrypt_input)(int);
79 int EncryptType(char *type, char *mode);
80 int EncryptStart(char *mode);
81 int EncryptStop(char *mode);
82 int EncryptStartInput(void);
83 int EncryptStartOutput(void);
84 int EncryptStopInput(void);
85 int EncryptStopOutput(void);
87 int encrypt_debug_mode = 0;
88 static int decrypt_mode = 0;
89 static int encrypt_mode = 0;
90 static int encrypt_verbose = 0;
91 static int autoencrypt = 0;
92 static int autodecrypt = 0;
93 static int havesessionkey = 0;
94 static int Server = 0;
95 static const char *Name = "Noname";
97 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
99 static u_long i_support_encrypt = 0
100 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
102 static u_long i_support_decrypt = 0
103 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
106 static u_long i_wont_support_encrypt = 0;
107 static u_long i_wont_support_decrypt = 0;
108 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
109 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
111 static u_long remote_supports_encrypt = 0;
112 static u_long remote_supports_decrypt = 0;
114 static Encryptions encryptions[] = {
115 { "DES_CFB64", ENCTYPE_DES_CFB64,
125 { "DES_OFB64", ENCTYPE_DES_OFB64,
135 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
138 static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
140 static unsigned char str_suplen = 0;
141 static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
142 static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
145 findencryption(int type)
147 Encryptions *ep = encryptions;
149 if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & (unsigned)typemask(type)))
151 while (ep->type && ep->type != type)
153 return(ep->type ? ep : 0);
157 finddecryption(int type)
159 Encryptions *ep = encryptions;
161 if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & (unsigned)typemask(type)))
163 while (ep->type && ep->type != type)
165 return(ep->type ? ep : 0);
170 static struct key_info {
171 unsigned char keyid[MAXKEYLEN];
175 Encryptions *(*getcrypt)(int);
177 { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
178 { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
181 static void encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len);
184 encrypt_init(const char *name, int server)
186 Encryptions *ep = encryptions;
190 i_support_encrypt = i_support_decrypt = 0;
191 remote_supports_encrypt = remote_supports_decrypt = 0;
200 if (encrypt_debug_mode)
201 printf(">>>%s: I will support %s\r\n",
202 Name, ENCTYPE_NAME(ep->type));
203 i_support_encrypt |= typemask(ep->type);
204 i_support_decrypt |= typemask(ep->type);
205 if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
206 if ((str_send[str_suplen++] = ep->type) == IAC)
207 str_send[str_suplen++] = IAC;
212 str_send[str_suplen++] = IAC;
213 str_send[str_suplen++] = SE;
217 encrypt_list_types(void)
219 Encryptions *ep = encryptions;
221 printf("Valid encryption types:\n");
223 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
229 EncryptEnable(char *type, char *mode)
231 if (isprefix(type, "help") || isprefix(type, "?")) {
232 printf("Usage: encrypt enable <type> [input|output]\n");
233 encrypt_list_types();
236 if (EncryptType(type, mode))
237 return(EncryptStart(mode));
242 EncryptDisable(char *type, char *mode)
247 if (isprefix(type, "help") || isprefix(type, "?")) {
248 printf("Usage: encrypt disable <type> [input|output]\n");
249 encrypt_list_types();
250 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
251 sizeof(Encryptions))) == 0) {
252 printf("%s: invalid encryption type\n", type);
253 } else if (Ambiguous((char **)ep)) {
254 printf("Ambiguous type '%s'\n", type);
256 if ((mode == 0) || (isprefix(mode, "input") ? 1 : 0)) {
257 if (decrypt_mode == ep->type)
259 i_wont_support_decrypt |= typemask(ep->type);
262 if ((mode == 0) || (isprefix(mode, "output"))) {
263 if (encrypt_mode == ep->type)
265 i_wont_support_encrypt |= typemask(ep->type);
269 printf("%s: invalid encryption mode\n", mode);
275 EncryptType(char *type, char *mode)
280 if (isprefix(type, "help") || isprefix(type, "?")) {
281 printf("Usage: encrypt type <type> [input|output]\n");
282 encrypt_list_types();
283 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
284 sizeof(Encryptions))) == 0) {
285 printf("%s: invalid encryption type\n", type);
286 } else if (Ambiguous((char **)ep)) {
287 printf("Ambiguous type '%s'\n", type);
289 if ((mode == 0) || isprefix(mode, "input")) {
290 decrypt_mode = ep->type;
291 i_wont_support_decrypt &= ~typemask(ep->type);
294 if ((mode == 0) || isprefix(mode, "output")) {
295 encrypt_mode = ep->type;
296 i_wont_support_encrypt &= ~typemask(ep->type);
300 printf("%s: invalid encryption mode\n", mode);
306 EncryptStart(char *mode)
310 if (isprefix(mode, "input"))
311 return(EncryptStartInput());
312 if (isprefix(mode, "output"))
313 return(EncryptStartOutput());
314 if (isprefix(mode, "help") || isprefix(mode, "?")) {
315 printf("Usage: encrypt start [input|output]\n");
318 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
321 ret += EncryptStartInput();
322 ret += EncryptStartOutput();
327 EncryptStartInput(void)
330 encrypt_send_request_start();
333 printf("No previous decryption mode, decryption not enabled\r\n");
338 EncryptStartOutput(void)
341 encrypt_start_output(encrypt_mode);
344 printf("No previous encryption mode, encryption not enabled\r\n");
349 EncryptStop(char *mode)
353 if (isprefix(mode, "input"))
354 return(EncryptStopInput());
355 if (isprefix(mode, "output"))
356 return(EncryptStopOutput());
357 if (isprefix(mode, "help") || isprefix(mode, "?")) {
358 printf("Usage: encrypt stop [input|output]\n");
361 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
364 ret += EncryptStopInput();
365 ret += EncryptStopOutput();
370 EncryptStopInput(void)
372 encrypt_send_request_end();
377 EncryptStopOutput(void)
384 encrypt_display(void)
387 printf("Currently encrypting output with %s\r\n",
388 ENCTYPE_NAME(encrypt_mode));
390 printf("Currently decrypting input with %s\r\n",
391 ENCTYPE_NAME(decrypt_mode));
398 printf("Currently encrypting output with %s\r\n",
399 ENCTYPE_NAME(encrypt_mode));
400 else if (encrypt_mode) {
401 printf("Currently output is clear text.\r\n");
402 printf("Last encryption mode was %s\r\n",
403 ENCTYPE_NAME(encrypt_mode));
406 printf("Currently decrypting input with %s\r\n",
407 ENCTYPE_NAME(decrypt_mode));
408 } else if (decrypt_mode) {
409 printf("Currently input is clear text.\r\n");
410 printf("Last decryption mode was %s\r\n",
411 ENCTYPE_NAME(decrypt_mode));
417 encrypt_send_support(void)
421 * If the user has requested that decryption start
422 * immediatly, then send a "REQUEST START" before
423 * we negotiate the type.
425 if (!Server && autodecrypt)
426 encrypt_send_request_start();
427 net_write(str_send, str_suplen);
428 printsub('>', &str_send[2], str_suplen - 2);
437 encrypt_debug_mode ^= 1;
439 encrypt_debug_mode = on;
440 printf("Encryption debugging %s\r\n",
441 encrypt_debug_mode ? "enabled" : "disabled");
446 EncryptVerbose(int on)
449 encrypt_verbose ^= 1;
451 encrypt_verbose = on;
452 printf("Encryption %s verbose\r\n",
453 encrypt_verbose ? "is" : "is not");
458 EncryptAutoEnc(int on)
461 printf("Automatic encryption of output is %s\r\n",
462 autoencrypt ? "enabled" : "disabled");
467 EncryptAutoDec(int on)
470 printf("Automatic decryption of input is %s\r\n",
471 autodecrypt ? "enabled" : "disabled");
476 * Called when ENCRYPT SUPPORT is received.
479 encrypt_support(unsigned char *typelist, int cnt)
481 int type, use_type = 0;
485 * Forget anything the other side has previously told us.
487 remote_supports_decrypt = 0;
491 if (encrypt_debug_mode)
492 printf(">>>%s: He is supporting %s (%d)\r\n",
494 ENCTYPE_NAME(type), type);
495 if ((type < ENCTYPE_CNT) &&
496 (I_SUPPORT_ENCRYPT & typemask(type))) {
497 remote_supports_decrypt |= typemask(type);
503 ep = findencryption(use_type);
506 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
507 if (encrypt_debug_mode)
508 printf(">>>%s: (*ep->start)() returned %d\r\n",
512 encrypt_mode = use_type;
514 encrypt_start_output(use_type);
519 encrypt_is(unsigned char *data, int cnt)
527 if (type < ENCTYPE_CNT)
528 remote_supports_encrypt |= typemask(type);
529 if (!(ep = finddecryption(type))) {
530 if (encrypt_debug_mode)
531 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
533 ENCTYPE_NAME_OK(type)
534 ? ENCTYPE_NAME(type) : "(unknown)",
539 if (encrypt_debug_mode)
540 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
542 ENCTYPE_NAME_OK(type)
543 ? ENCTYPE_NAME(type) : "(unknown)",
547 ret = (*ep->is)(data, cnt);
548 if (encrypt_debug_mode)
549 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data, cnt,
550 (ret < 0) ? "FAIL " :
551 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
557 if (ret == 0 && autodecrypt)
558 encrypt_send_request_start();
563 encrypt_reply(unsigned char *data, int cnt)
571 if (!(ep = findencryption(type))) {
572 if (encrypt_debug_mode)
573 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
575 ENCTYPE_NAME_OK(type)
576 ? ENCTYPE_NAME(type) : "(unknown)",
581 if (encrypt_debug_mode)
582 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
584 ENCTYPE_NAME_OK(type)
585 ? ENCTYPE_NAME(type) : "(unknown)",
589 ret = (*ep->reply)(data, cnt);
590 if (encrypt_debug_mode)
591 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
593 (ret < 0) ? "FAIL " :
594 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
596 if (encrypt_debug_mode)
597 printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
602 if (ret == 0 && autoencrypt)
603 encrypt_start_output(type);
608 * Called when a ENCRYPT START command is received.
611 encrypt_start(unsigned char *data __unused, int cnt __unused)
617 * Something is wrong. We should not get a START
618 * command without having already picked our
619 * decryption scheme. Send a REQUEST-END to
620 * attempt to clear the channel...
622 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
623 encrypt_send_request_end();
627 if ((ep = finddecryption(decrypt_mode))) {
628 decrypt_input = ep->input;
630 printf("[ Input is now decrypted with type %s ]\r\n",
631 ENCTYPE_NAME(decrypt_mode));
632 if (encrypt_debug_mode)
633 printf(">>>%s: Start to decrypt input with type %s\r\n",
634 Name, ENCTYPE_NAME(decrypt_mode));
636 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
638 ENCTYPE_NAME_OK(decrypt_mode)
639 ? ENCTYPE_NAME(decrypt_mode)
642 encrypt_send_request_end();
647 encrypt_session_key( Session_Key *key, int server)
649 Encryptions *ep = encryptions;
655 (*ep->session)(key, server);
661 * Called when ENCRYPT END is received.
667 if (encrypt_debug_mode)
668 printf(">>>%s: Input is back to clear text\r\n", Name);
670 printf("[ Input is now clear text ]\r\n");
674 * Called when ENCRYPT REQUEST-END is received.
677 encrypt_request_end(void)
683 * Called when ENCRYPT REQUEST-START is received. If we receive
684 * this before a type is picked, then that indicates that the
685 * other side wants us to start encrypting data as soon as we
689 encrypt_request_start(unsigned char *data __unused, int cnt __unused)
691 if (encrypt_mode == 0) {
696 encrypt_start_output(encrypt_mode);
699 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
702 encrypt_enc_keyid(unsigned char *keyid, int len)
704 encrypt_keyid(&ki[1], keyid, len);
708 encrypt_dec_keyid(unsigned char *keyid, int len)
710 encrypt_keyid(&ki[0], keyid, len);
714 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
723 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
727 } else if (len == 0) {
729 * Empty option, indicates a failure.
735 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
737 } else if ((len != kp->keylen) ||
738 (memcmp(keyid, kp->keyid, len) != 0)) {
740 * Length or contents are different
743 memmove(kp->keyid, keyid, len);
745 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
748 ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
749 if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
750 encrypt_start_output(*kp->modep);
754 encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
758 encrypt_send_keyid(int dir, const char *keyid, int keylen, int saveit)
762 str_keyid[3] = (dir == DIR_ENCRYPT)
763 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
765 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
766 memmove(kp->keyid, keyid, keylen);
770 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
771 if ((*strp++ = *keyid++) == IAC)
776 net_write(str_keyid, strp - str_keyid);
777 printsub('>', &str_keyid[2], strp - str_keyid - 2);
786 autoencrypt = on ? 1 : 0;
795 autodecrypt = on ? 1 : 0;
799 encrypt_start_output(int type)
805 if (!(ep = findencryption(type))) {
806 if (encrypt_debug_mode) {
807 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
809 ENCTYPE_NAME_OK(type)
810 ? ENCTYPE_NAME(type) : "(unknown)",
816 i = (*ep->start)(DIR_ENCRYPT, Server);
817 if (encrypt_debug_mode) {
818 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
821 "initial negotiation in progress",
822 i, ENCTYPE_NAME(type));
828 *p++ = ENCRYPT_START;
829 for (i = 0; i < ki[0].keylen; ++i) {
830 if ((*p++ = ki[0].keyid[i]) == IAC)
835 net_write(str_start, p - str_start);
837 printsub('>', &str_start[2], p - &str_start[2]);
839 * If we are already encrypting in some mode, then
840 * encrypt the ring (which includes our request) in
841 * the old mode, mark it all as "clear text" and then
842 * switch to the new mode.
844 encrypt_output = ep->output;
846 if (encrypt_debug_mode)
847 printf(">>>%s: Started to encrypt output with type %s\r\n",
848 Name, ENCTYPE_NAME(type));
850 printf("[ Output is now encrypted with type %s ]\r\n",
855 encrypt_send_end(void)
860 str_end[3] = ENCRYPT_END;
861 net_write(str_end, sizeof(str_end));
863 printsub('>', &str_end[2], sizeof(str_end) - 2);
865 * Encrypt the output buffer now because it will not be done by
869 if (encrypt_debug_mode)
870 printf(">>>%s: Output is back to clear text\r\n", Name);
872 printf("[ Output is now clear text ]\r\n");
876 encrypt_send_request_start(void)
882 *p++ = ENCRYPT_REQSTART;
883 for (i = 0; i < ki[1].keylen; ++i) {
884 if ((*p++ = ki[1].keyid[i]) == IAC)
889 net_write(str_start, p - str_start);
890 printsub('>', &str_start[2], p - &str_start[2]);
891 if (encrypt_debug_mode)
892 printf(">>>%s: Request input to be encrypted\r\n", Name);
896 encrypt_send_request_end(void)
898 str_end[3] = ENCRYPT_REQEND;
899 net_write(str_end, sizeof(str_end));
900 printsub('>', &str_end[2], sizeof(str_end) - 2);
902 if (encrypt_debug_mode)
903 printf(">>>%s: Request input to be clear text\r\n", Name);
909 if (encrypt_debug_mode)
910 printf(">>>%s: in encrypt_wait\r\n", Name);
911 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
913 while (autoencrypt && !encrypt_output)
919 encrypt_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
925 buf[buflen-1] = '\0';
928 for (; cnt > 0; cnt--, data++) {
929 sprintf(tbuf, " %d", *data);
930 for (cp = tbuf; *cp && buflen > 0; --buflen)
939 encrypt_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
944 for (ep = encryptions; ep->type && ep->type != type; ep++)
948 (*ep->printsub)(data, cnt, buf, buflen);
950 encrypt_gen_printsub(data, cnt, buf, buflen);
952 #endif /* ENCRYPTION */