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 $
38 * Copyright (C) 1990 by the Massachusetts Institute of Technology
40 * Export of this software from the United States of America is assumed
41 * to require a specific license from the United States Government.
42 * It is the responsibility of any person or organization contemplating
43 * export to obtain such a license before exporting.
45 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
46 * distribute this software and its documentation for any purpose and
47 * without fee is hereby granted, provided that the above copyright
48 * notice appear in all copies and that both that copyright notice and
49 * this permission notice appear in supporting documentation, and that
50 * the name of M.I.T. not be used in advertising or publicity pertaining
51 * to distribution of the software without specific, written prior
52 * permission. M.I.T. makes no representations about the suitability of
53 * this software for any purpose. It is provided "as is" without express
54 * or implied warranty.
60 #include <arpa/telnet.h>
69 * These functions pointers point to the current routines
70 * for encrypting and decrypting data.
72 void (*encrypt_output)(unsigned char *, int);
73 int (*decrypt_input)(int);
75 int EncryptType(char *type, char *mode);
76 int EncryptStart(char *mode);
77 int EncryptStop(char *mode);
78 int EncryptStartInput(void);
79 int EncryptStartOutput(void);
80 int EncryptStopInput(void);
81 int EncryptStopOutput(void);
83 int encrypt_debug_mode = 0;
84 static int decrypt_mode = 0;
85 static int encrypt_mode = 0;
86 static int encrypt_verbose = 0;
87 static int autoencrypt = 0;
88 static int autodecrypt = 0;
89 static int havesessionkey = 0;
90 static int Server = 0;
91 static const char *Name = "Noname";
93 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
95 static long i_support_encrypt = 0
96 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
98 static long i_support_decrypt = 0
99 | typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64)
102 static long i_wont_support_encrypt = 0;
103 static long i_wont_support_decrypt = 0;
104 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
105 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
107 static long remote_supports_encrypt = 0;
108 static long remote_supports_decrypt = 0;
110 static Encryptions encryptions[] = {
111 { "DES_CFB64", ENCTYPE_DES_CFB64,
121 { "DES_OFB64", ENCTYPE_DES_OFB64,
131 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
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 & (unsigned)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 & (unsigned)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)(int);
173 { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
174 { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
177 static void encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len);
180 encrypt_init(const char *name, int server)
182 Encryptions *ep = encryptions;
186 i_support_encrypt = i_support_decrypt = 0;
187 remote_supports_encrypt = remote_supports_decrypt = 0;
190 encrypt_output = NULL;
191 decrypt_input = NULL;
196 if (encrypt_debug_mode)
197 printf(">>>%s: I will support %s\r\n",
198 Name, ENCTYPE_NAME(ep->type));
199 i_support_encrypt |= typemask(ep->type);
200 i_support_decrypt |= typemask(ep->type);
201 if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
202 if ((str_send[str_suplen++] = ep->type) == IAC)
203 str_send[str_suplen++] = IAC;
208 str_send[str_suplen++] = IAC;
209 str_send[str_suplen++] = SE;
213 encrypt_list_types(void)
215 Encryptions *ep = encryptions;
217 printf("Valid encryption types:\n");
219 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
225 EncryptEnable(char *type, char *mode)
227 if (isprefix(type, "help") || isprefix(type, "?")) {
228 printf("Usage: encrypt enable <type> [input|output]\n");
229 encrypt_list_types();
232 if (EncryptType(type, mode))
233 return(EncryptStart(mode));
238 EncryptDisable(char *type, char *mode)
243 if (isprefix(type, "help") || isprefix(type, "?")) {
244 printf("Usage: encrypt disable <type> [input|output]\n");
245 encrypt_list_types();
246 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
247 sizeof(Encryptions))) == NULL) {
248 printf("%s: invalid encryption type\n", type);
249 } else if (Ambiguous((char **)ep)) {
250 printf("Ambiguous type '%s'\n", type);
252 if ((mode == NULL) || (isprefix(mode, "input") ? 1 : 0)) {
253 if (decrypt_mode == ep->type)
255 i_wont_support_decrypt |= typemask(ep->type);
258 if ((mode == NULL) || (isprefix(mode, "output"))) {
259 if (encrypt_mode == ep->type)
261 i_wont_support_encrypt |= typemask(ep->type);
265 printf("%s: invalid encryption mode\n", mode);
271 EncryptType(char *type, char *mode)
276 if (isprefix(type, "help") || isprefix(type, "?")) {
277 printf("Usage: encrypt type <type> [input|output]\n");
278 encrypt_list_types();
279 } else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
280 sizeof(Encryptions))) == NULL) {
281 printf("%s: invalid encryption type\n", type);
282 } else if (Ambiguous((char **)ep)) {
283 printf("Ambiguous type '%s'\n", type);
285 if ((mode == NULL) || isprefix(mode, "input")) {
286 decrypt_mode = ep->type;
287 i_wont_support_decrypt &= ~typemask(ep->type);
290 if ((mode == NULL) || isprefix(mode, "output")) {
291 encrypt_mode = ep->type;
292 i_wont_support_encrypt &= ~typemask(ep->type);
296 printf("%s: invalid encryption mode\n", mode);
302 EncryptStart(char *mode)
306 if (isprefix(mode, "input"))
307 return(EncryptStartInput());
308 if (isprefix(mode, "output"))
309 return(EncryptStartOutput());
310 if (isprefix(mode, "help") || isprefix(mode, "?")) {
311 printf("Usage: encrypt start [input|output]\n");
314 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
317 ret += EncryptStartInput();
318 ret += EncryptStartOutput();
323 EncryptStartInput(void)
326 encrypt_send_request_start();
329 printf("No previous decryption mode, decryption not enabled\r\n");
334 EncryptStartOutput(void)
337 encrypt_start_output(encrypt_mode);
340 printf("No previous encryption mode, encryption not enabled\r\n");
345 EncryptStop(char *mode)
349 if (isprefix(mode, "input"))
350 return(EncryptStopInput());
351 if (isprefix(mode, "output"))
352 return(EncryptStopOutput());
353 if (isprefix(mode, "help") || isprefix(mode, "?")) {
354 printf("Usage: encrypt stop [input|output]\n");
357 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
360 ret += EncryptStopInput();
361 ret += EncryptStopOutput();
366 EncryptStopInput(void)
368 encrypt_send_request_end();
373 EncryptStopOutput(void)
380 encrypt_display(void)
383 printf("Currently encrypting output with %s\r\n",
384 ENCTYPE_NAME(encrypt_mode));
386 printf("Currently decrypting input with %s\r\n",
387 ENCTYPE_NAME(decrypt_mode));
394 printf("Currently encrypting output with %s\r\n",
395 ENCTYPE_NAME(encrypt_mode));
396 else if (encrypt_mode) {
397 printf("Currently output is clear text.\r\n");
398 printf("Last encryption mode was %s\r\n",
399 ENCTYPE_NAME(encrypt_mode));
402 printf("Currently decrypting input with %s\r\n",
403 ENCTYPE_NAME(decrypt_mode));
404 } else if (decrypt_mode) {
405 printf("Currently input is clear text.\r\n");
406 printf("Last decryption mode was %s\r\n",
407 ENCTYPE_NAME(decrypt_mode));
413 encrypt_send_support(void)
417 * If the user has requested that decryption start
418 * immediatly, then send a "REQUEST START" before
419 * we negotiate the type.
421 if (!Server && autodecrypt)
422 encrypt_send_request_start();
423 net_write(str_send, str_suplen);
424 printsub('>', &str_send[2], str_suplen - 2);
433 encrypt_debug_mode ^= 1;
435 encrypt_debug_mode = on;
436 printf("Encryption debugging %s\r\n",
437 encrypt_debug_mode ? "enabled" : "disabled");
442 EncryptVerbose(int on)
445 encrypt_verbose ^= 1;
447 encrypt_verbose = on;
448 printf("Encryption %s verbose\r\n",
449 encrypt_verbose ? "is" : "is not");
454 EncryptAutoEnc(int on)
457 printf("Automatic encryption of output is %s\r\n",
458 autoencrypt ? "enabled" : "disabled");
463 EncryptAutoDec(int on)
466 printf("Automatic decryption of input is %s\r\n",
467 autodecrypt ? "enabled" : "disabled");
472 * Called when ENCRYPT SUPPORT is received.
475 encrypt_support(unsigned char *typelist, int cnt)
477 int type, use_type = 0;
481 * Forget anything the other side has previously told us.
483 remote_supports_decrypt = 0;
487 if (encrypt_debug_mode)
488 printf(">>>%s: He is supporting %s (%d)\r\n",
490 ENCTYPE_NAME(type), type);
491 if ((type < ENCTYPE_CNT) &&
492 (I_SUPPORT_ENCRYPT & typemask(type))) {
493 remote_supports_decrypt |= typemask(type);
499 ep = findencryption(use_type);
502 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
503 if (encrypt_debug_mode)
504 printf(">>>%s: (*ep->start)() returned %d\r\n",
508 encrypt_mode = use_type;
510 encrypt_start_output(use_type);
515 encrypt_is(unsigned char *data, int cnt)
523 if (type < ENCTYPE_CNT)
524 remote_supports_encrypt |= typemask(type);
525 if (!(ep = finddecryption(type))) {
526 if (encrypt_debug_mode)
527 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
529 ENCTYPE_NAME_OK(type)
530 ? ENCTYPE_NAME(type) : "(unknown)",
535 if (encrypt_debug_mode)
536 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
538 ENCTYPE_NAME_OK(type)
539 ? ENCTYPE_NAME(type) : "(unknown)",
543 ret = (*ep->is)(data, cnt);
544 if (encrypt_debug_mode)
545 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data, cnt,
546 (ret < 0) ? "FAIL " :
547 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
553 if (ret == 0 && autodecrypt)
554 encrypt_send_request_start();
559 encrypt_reply(unsigned char *data, int cnt)
567 if (!(ep = findencryption(type))) {
568 if (encrypt_debug_mode)
569 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
571 ENCTYPE_NAME_OK(type)
572 ? ENCTYPE_NAME(type) : "(unknown)",
577 if (encrypt_debug_mode)
578 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
580 ENCTYPE_NAME_OK(type)
581 ? ENCTYPE_NAME(type) : "(unknown)",
585 ret = (*ep->reply)(data, cnt);
586 if (encrypt_debug_mode)
587 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
589 (ret < 0) ? "FAIL " :
590 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
592 if (encrypt_debug_mode)
593 printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
598 if (ret == 0 && autoencrypt)
599 encrypt_start_output(type);
604 * Called when a ENCRYPT START command is received.
607 encrypt_start(unsigned char *data __unused, int cnt __unused)
613 * Something is wrong. We should not get a START
614 * command without having already picked our
615 * decryption scheme. Send a REQUEST-END to
616 * attempt to clear the channel...
618 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
619 encrypt_send_request_end();
623 if ((ep = finddecryption(decrypt_mode))) {
624 decrypt_input = ep->input;
626 printf("[ Input is now decrypted with type %s ]\r\n",
627 ENCTYPE_NAME(decrypt_mode));
628 if (encrypt_debug_mode)
629 printf(">>>%s: Start to decrypt input with type %s\r\n",
630 Name, ENCTYPE_NAME(decrypt_mode));
632 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
634 ENCTYPE_NAME_OK(decrypt_mode)
635 ? ENCTYPE_NAME(decrypt_mode)
638 encrypt_send_request_end();
643 encrypt_session_key( Session_Key *key, int server)
645 Encryptions *ep = encryptions;
651 (*ep->session)(key, server);
657 * Called when ENCRYPT END is received.
662 decrypt_input = NULL;
663 if (encrypt_debug_mode)
664 printf(">>>%s: Input is back to clear text\r\n", Name);
666 printf("[ Input is now clear text ]\r\n");
670 * Called when ENCRYPT REQUEST-END is received.
673 encrypt_request_end(void)
679 * Called when ENCRYPT REQUEST-START is received. If we receive
680 * this before a type is picked, then that indicates that the
681 * other side wants us to start encrypting data as soon as we
685 encrypt_request_start(unsigned char *data __unused, int cnt __unused)
687 if (encrypt_mode == 0) {
692 encrypt_start_output(encrypt_mode);
695 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
698 encrypt_enc_keyid(unsigned char *keyid, int len)
700 encrypt_keyid(&ki[1], keyid, len);
704 encrypt_dec_keyid(unsigned char *keyid, int len)
706 encrypt_keyid(&ki[0], keyid, len);
710 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
719 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
723 } else if (len == 0) {
725 * Empty option, indicates a failure.
731 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
733 } else if ((len != kp->keylen) ||
734 (memcmp(keyid, kp->keyid, len) != 0)) {
736 * Length or contents are different
739 memmove(kp->keyid, keyid, len);
741 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
744 ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
745 if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
746 encrypt_start_output(*kp->modep);
750 encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
754 encrypt_send_keyid(int dir, const char *keyid, int keylen, int saveit)
758 str_keyid[3] = (dir == DIR_ENCRYPT)
759 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
761 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
762 memmove(kp->keyid, keyid, keylen);
766 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
767 if ((*strp++ = *keyid++) == IAC)
772 net_write(str_keyid, strp - str_keyid);
773 printsub('>', &str_keyid[2], strp - str_keyid - 2);
782 autoencrypt = on ? 1 : 0;
791 autodecrypt = on ? 1 : 0;
795 encrypt_start_output(int type)
801 if (!(ep = findencryption(type))) {
802 if (encrypt_debug_mode) {
803 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
805 ENCTYPE_NAME_OK(type)
806 ? ENCTYPE_NAME(type) : "(unknown)",
812 i = (*ep->start)(DIR_ENCRYPT, Server);
813 if (encrypt_debug_mode) {
814 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
817 "initial negotiation in progress",
818 i, ENCTYPE_NAME(type));
824 *p++ = ENCRYPT_START;
825 for (i = 0; i < ki[0].keylen; ++i) {
826 if ((*p++ = ki[0].keyid[i]) == IAC)
831 net_write(str_start, p - str_start);
833 printsub('>', &str_start[2], p - &str_start[2]);
835 * If we are already encrypting in some mode, then
836 * encrypt the ring (which includes our request) in
837 * the old mode, mark it all as "clear text" and then
838 * switch to the new mode.
840 encrypt_output = ep->output;
842 if (encrypt_debug_mode)
843 printf(">>>%s: Started to encrypt output with type %s\r\n",
844 Name, ENCTYPE_NAME(type));
846 printf("[ Output is now encrypted with type %s ]\r\n",
851 encrypt_send_end(void)
856 str_end[3] = ENCRYPT_END;
857 net_write(str_end, sizeof(str_end));
859 printsub('>', &str_end[2], sizeof(str_end) - 2);
861 * Encrypt the output buffer now because it will not be done by
864 encrypt_output = NULL;
865 if (encrypt_debug_mode)
866 printf(">>>%s: Output is back to clear text\r\n", Name);
868 printf("[ Output is now clear text ]\r\n");
872 encrypt_send_request_start(void)
878 *p++ = ENCRYPT_REQSTART;
879 for (i = 0; i < ki[1].keylen; ++i) {
880 if ((*p++ = ki[1].keyid[i]) == IAC)
885 net_write(str_start, p - str_start);
886 printsub('>', &str_start[2], p - &str_start[2]);
887 if (encrypt_debug_mode)
888 printf(">>>%s: Request input to be encrypted\r\n", Name);
892 encrypt_send_request_end(void)
894 str_end[3] = ENCRYPT_REQEND;
895 net_write(str_end, sizeof(str_end));
896 printsub('>', &str_end[2], sizeof(str_end) - 2);
898 if (encrypt_debug_mode)
899 printf(">>>%s: Request input to be clear text\r\n", Name);
905 if (encrypt_debug_mode)
906 printf(">>>%s: in encrypt_wait\r\n", Name);
907 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
909 while (autoencrypt && !encrypt_output)
915 encrypt_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
921 buf[buflen-1] = '\0';
924 for (; cnt > 0; cnt--, data++) {
925 sprintf(tbuf, " %d", *data);
926 for (cp = tbuf; *cp && buflen > 0; --buflen)
935 encrypt_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
940 for (ep = encryptions; ep->type && ep->type != type; ep++)
944 (*ep->printsub)(data, cnt, buf, buflen);
946 encrypt_gen_printsub(data, cnt, buf, buflen);
948 #endif /* ENCRYPTION */