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
33 * @(#)encrypt.c 8.2 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/libtelnet/encrypt.c,v 1.3.2.2 2002/04/13 10:59:07 markm Exp $
35 * $DragonFly: src/crypto/telnet/libtelnet/encrypt.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
39 * Copyright (C) 1990 by the Massachusetts Institute of Technology
41 * Export of this software from the United States of America is assumed
42 * to require a specific license from the United States Government.
43 * It is the responsibility of any person or organization contemplating
44 * export to obtain such a license before exporting.
46 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
47 * distribute this software and its documentation for any purpose and
48 * without fee is hereby granted, provided that the above copyright
49 * notice appear in all copies and that both that copyright notice and
50 * this permission notice appear in supporting documentation, and that
51 * the name of M.I.T. not be used in advertising or publicity pertaining
52 * to distribution of the software without specific, written prior
53 * permission. M.I.T. makes no representations about the suitability of
54 * this software for any purpose. It is provided "as is" without express
55 * or implied warranty.
61 #include <arpa/telnet.h>
70 * These functions pointers point to the current routines
71 * for encrypting and decrypting data.
73 void (*encrypt_output)(unsigned char *, int);
74 int (*decrypt_input)(int);
76 int EncryptType(char *type, char *mode);
77 int EncryptStart(char *mode);
78 int EncryptStop(char *mode);
79 int EncryptStartInput(void);
80 int EncryptStartOutput(void);
81 int EncryptStopInput(void);
82 int EncryptStopOutput(void);
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 = 0
97 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
99 static long i_support_decrypt = 0
100 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
103 static long i_wont_support_encrypt = 0;
104 static long i_wont_support_decrypt = 0;
105 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
106 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
108 static long remote_supports_encrypt = 0;
109 static long remote_supports_decrypt = 0;
111 static Encryptions encryptions[] = {
112 { "DES_CFB64", ENCTYPE_DES_CFB64,
122 { "DES_OFB64", ENCTYPE_DES_OFB64,
132 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
135 static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
137 static unsigned char str_suplen = 0;
138 static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
139 static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
142 findencryption(int type)
144 Encryptions *ep = encryptions;
146 if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & (unsigned)typemask(type)))
148 while (ep->type && ep->type != type)
150 return(ep->type ? ep : 0);
154 finddecryption(int type)
156 Encryptions *ep = encryptions;
158 if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & (unsigned)typemask(type)))
160 while (ep->type && ep->type != type)
162 return(ep->type ? ep : 0);
167 static struct key_info {
168 unsigned char keyid[MAXKEYLEN];
172 Encryptions *(*getcrypt)(int);
174 { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
175 { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
178 static void encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len);
181 encrypt_init(const char *name, int server)
183 Encryptions *ep = encryptions;
187 i_support_encrypt = i_support_decrypt = 0;
188 remote_supports_encrypt = remote_supports_decrypt = 0;
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((char **)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((char **)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)
384 printf("Currently encrypting output with %s\r\n",
385 ENCTYPE_NAME(encrypt_mode));
387 printf("Currently decrypting input with %s\r\n",
388 ENCTYPE_NAME(decrypt_mode));
395 printf("Currently encrypting output with %s\r\n",
396 ENCTYPE_NAME(encrypt_mode));
397 else if (encrypt_mode) {
398 printf("Currently output is clear text.\r\n");
399 printf("Last encryption mode was %s\r\n",
400 ENCTYPE_NAME(encrypt_mode));
403 printf("Currently decrypting input with %s\r\n",
404 ENCTYPE_NAME(decrypt_mode));
405 } else if (decrypt_mode) {
406 printf("Currently input is clear text.\r\n");
407 printf("Last decryption mode was %s\r\n",
408 ENCTYPE_NAME(decrypt_mode));
414 encrypt_send_support(void)
418 * If the user has requested that decryption start
419 * immediatly, then send a "REQUEST START" before
420 * we negotiate the type.
422 if (!Server && autodecrypt)
423 encrypt_send_request_start();
424 net_write(str_send, str_suplen);
425 printsub('>', &str_send[2], str_suplen - 2);
434 encrypt_debug_mode ^= 1;
436 encrypt_debug_mode = on;
437 printf("Encryption debugging %s\r\n",
438 encrypt_debug_mode ? "enabled" : "disabled");
443 EncryptVerbose(int on)
446 encrypt_verbose ^= 1;
448 encrypt_verbose = on;
449 printf("Encryption %s verbose\r\n",
450 encrypt_verbose ? "is" : "is not");
455 EncryptAutoEnc(int on)
458 printf("Automatic encryption of output is %s\r\n",
459 autoencrypt ? "enabled" : "disabled");
464 EncryptAutoDec(int on)
467 printf("Automatic decryption of input is %s\r\n",
468 autodecrypt ? "enabled" : "disabled");
473 * Called when ENCRYPT SUPPORT is received.
476 encrypt_support(unsigned char *typelist, int cnt)
478 int type, use_type = 0;
482 * Forget anything the other side has previously told us.
484 remote_supports_decrypt = 0;
488 if (encrypt_debug_mode)
489 printf(">>>%s: He is supporting %s (%d)\r\n",
491 ENCTYPE_NAME(type), type);
492 if ((type < ENCTYPE_CNT) &&
493 (I_SUPPORT_ENCRYPT & typemask(type))) {
494 remote_supports_decrypt |= typemask(type);
500 ep = findencryption(use_type);
503 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
504 if (encrypt_debug_mode)
505 printf(">>>%s: (*ep->start)() returned %d\r\n",
509 encrypt_mode = use_type;
511 encrypt_start_output(use_type);
516 encrypt_is(unsigned char *data, int cnt)
524 if (type < ENCTYPE_CNT)
525 remote_supports_encrypt |= typemask(type);
526 if (!(ep = finddecryption(type))) {
527 if (encrypt_debug_mode)
528 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
530 ENCTYPE_NAME_OK(type)
531 ? ENCTYPE_NAME(type) : "(unknown)",
536 if (encrypt_debug_mode)
537 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
539 ENCTYPE_NAME_OK(type)
540 ? ENCTYPE_NAME(type) : "(unknown)",
544 ret = (*ep->is)(data, cnt);
545 if (encrypt_debug_mode)
546 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data, cnt,
547 (ret < 0) ? "FAIL " :
548 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
554 if (ret == 0 && autodecrypt)
555 encrypt_send_request_start();
560 encrypt_reply(unsigned char *data, int cnt)
568 if (!(ep = findencryption(type))) {
569 if (encrypt_debug_mode)
570 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
572 ENCTYPE_NAME_OK(type)
573 ? ENCTYPE_NAME(type) : "(unknown)",
578 if (encrypt_debug_mode)
579 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
581 ENCTYPE_NAME_OK(type)
582 ? ENCTYPE_NAME(type) : "(unknown)",
586 ret = (*ep->reply)(data, cnt);
587 if (encrypt_debug_mode)
588 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
590 (ret < 0) ? "FAIL " :
591 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
593 if (encrypt_debug_mode)
594 printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
599 if (ret == 0 && autoencrypt)
600 encrypt_start_output(type);
605 * Called when a ENCRYPT START command is received.
608 encrypt_start(unsigned char *data __unused, int cnt __unused)
614 * Something is wrong. We should not get a START
615 * command without having already picked our
616 * decryption scheme. Send a REQUEST-END to
617 * attempt to clear the channel...
619 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
620 encrypt_send_request_end();
624 if ((ep = finddecryption(decrypt_mode))) {
625 decrypt_input = ep->input;
627 printf("[ Input is now decrypted with type %s ]\r\n",
628 ENCTYPE_NAME(decrypt_mode));
629 if (encrypt_debug_mode)
630 printf(">>>%s: Start to decrypt input with type %s\r\n",
631 Name, ENCTYPE_NAME(decrypt_mode));
633 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
635 ENCTYPE_NAME_OK(decrypt_mode)
636 ? ENCTYPE_NAME(decrypt_mode)
639 encrypt_send_request_end();
644 encrypt_session_key( Session_Key *key, int server)
646 Encryptions *ep = encryptions;
652 (*ep->session)(key, server);
658 * Called when ENCRYPT END is received.
664 if (encrypt_debug_mode)
665 printf(">>>%s: Input is back to clear text\r\n", Name);
667 printf("[ Input is now clear text ]\r\n");
671 * Called when ENCRYPT REQUEST-END is received.
674 encrypt_request_end(void)
680 * Called when ENCRYPT REQUEST-START is received. If we receive
681 * this before a type is picked, then that indicates that the
682 * other side wants us to start encrypting data as soon as we
686 encrypt_request_start(unsigned char *data __unused, int cnt __unused)
688 if (encrypt_mode == 0) {
693 encrypt_start_output(encrypt_mode);
696 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
699 encrypt_enc_keyid(unsigned char *keyid, int len)
701 encrypt_keyid(&ki[1], keyid, len);
705 encrypt_dec_keyid(unsigned char *keyid, int len)
707 encrypt_keyid(&ki[0], keyid, len);
711 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
717 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
721 } else if (len == 0) {
723 * Empty option, indicates a failure.
729 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
731 } else if ((len != kp->keylen) ||
732 (memcmp(keyid, kp->keyid, len) != 0)) {
734 * Length or contents are different
737 memmove(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(int dir, const char *keyid, int keylen, int saveit)
756 str_keyid[3] = (dir == DIR_ENCRYPT)
757 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
759 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
760 memmove(kp->keyid, keyid, keylen);
764 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
765 if ((*strp++ = *keyid++) == IAC)
770 net_write(str_keyid, strp - str_keyid);
771 printsub('>', &str_keyid[2], strp - str_keyid - 2);
780 autoencrypt = on ? 1 : 0;
789 autodecrypt = on ? 1 : 0;
793 encrypt_start_output(int type)
799 if (!(ep = findencryption(type))) {
800 if (encrypt_debug_mode) {
801 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
803 ENCTYPE_NAME_OK(type)
804 ? ENCTYPE_NAME(type) : "(unknown)",
810 i = (*ep->start)(DIR_ENCRYPT, Server);
811 if (encrypt_debug_mode) {
812 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
815 "initial negotiation in progress",
816 i, ENCTYPE_NAME(type));
822 *p++ = ENCRYPT_START;
823 for (i = 0; i < ki[0].keylen; ++i) {
824 if ((*p++ = ki[0].keyid[i]) == IAC)
829 net_write(str_start, p - str_start);
831 printsub('>', &str_start[2], p - &str_start[2]);
833 * If we are already encrypting in some mode, then
834 * encrypt the ring (which includes our request) in
835 * the old mode, mark it all as "clear text" and then
836 * switch to the new mode.
838 encrypt_output = ep->output;
840 if (encrypt_debug_mode)
841 printf(">>>%s: Started to encrypt output with type %s\r\n",
842 Name, ENCTYPE_NAME(type));
844 printf("[ Output is now encrypted with type %s ]\r\n",
849 encrypt_send_end(void)
854 str_end[3] = ENCRYPT_END;
855 net_write(str_end, sizeof(str_end));
857 printsub('>', &str_end[2], sizeof(str_end) - 2);
859 * Encrypt the output buffer now because it will not be done by
863 if (encrypt_debug_mode)
864 printf(">>>%s: Output is back to clear text\r\n", Name);
866 printf("[ Output is now clear text ]\r\n");
870 encrypt_send_request_start(void)
876 *p++ = ENCRYPT_REQSTART;
877 for (i = 0; i < ki[1].keylen; ++i) {
878 if ((*p++ = ki[1].keyid[i]) == IAC)
883 net_write(str_start, p - str_start);
884 printsub('>', &str_start[2], p - &str_start[2]);
885 if (encrypt_debug_mode)
886 printf(">>>%s: Request input to be encrypted\r\n", Name);
890 encrypt_send_request_end(void)
892 str_end[3] = ENCRYPT_REQEND;
893 net_write(str_end, sizeof(str_end));
894 printsub('>', &str_end[2], sizeof(str_end) - 2);
896 if (encrypt_debug_mode)
897 printf(">>>%s: Request input to be clear text\r\n", Name);
903 if (encrypt_debug_mode)
904 printf(">>>%s: in encrypt_wait\r\n", Name);
905 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
907 while (autoencrypt && !encrypt_output)
913 encrypt_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
919 buf[buflen-1] = '\0';
922 for (; cnt > 0; cnt--, data++) {
923 sprintf(tbuf, " %d", *data);
924 for (cp = tbuf; *cp && buflen > 0; --buflen)
933 encrypt_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
938 for (ep = encryptions; ep->type && ep->type != type; ep++)
942 (*ep->printsub)(data, cnt, buf, buflen);
944 encrypt_gen_printsub(data, cnt, buf, buflen);
946 #endif /* ENCRYPTION */