BTRFS: Reimplement TreeIterator, add some error checks and remove redundancies.
[haiku.git] / src / libs / libtelnet / encrypt.c
blob7227006ceb52589670f6c560273b7d256ef9d464
1 /*-
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
7 * are met:
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
31 * SUCH DAMAGE.
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/encrypt.c,v 1.9 2002/06/26 17:06:14 markm Exp $");
38 #ifndef lint
39 #if 0
40 static const char sccsid[] = "@(#)encrypt.c 8.2 (Berkeley) 5/30/95";
41 #endif
42 #endif /* not lint */
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.
64 #ifdef ENCRYPTION
66 #include <sys/types.h>
67 #define ENCRYPT_NAMES
68 #include <arpa/telnet.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
73 #include "encrypt.h"
74 #include "misc.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,
120 cfb64_encrypt,
121 cfb64_decrypt,
122 cfb64_init,
123 cfb64_start,
124 cfb64_is,
125 cfb64_reply,
126 cfb64_session,
127 cfb64_keyid,
128 cfb64_printsub },
129 { "DES_OFB64", ENCTYPE_DES_OFB64,
130 ofb64_encrypt,
131 ofb64_decrypt,
132 ofb64_init,
133 ofb64_start,
134 ofb64_is,
135 ofb64_reply,
136 ofb64_session,
137 ofb64_keyid,
138 ofb64_printsub },
139 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
142 static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
143 ENCRYPT_SUPPORT };
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 };
148 Encryptions *
149 findencryption(int type)
151 Encryptions *ep = encryptions;
153 if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & (unsigned)typemask(type)))
154 return(0);
155 while (ep->type && ep->type != type)
156 ++ep;
157 return(ep->type ? ep : 0);
160 static Encryptions *
161 finddecryption(int type)
163 Encryptions *ep = encryptions;
165 if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & (unsigned)typemask(type)))
166 return(0);
167 while (ep->type && ep->type != type)
168 ++ep;
169 return(ep->type ? ep : 0);
172 #define MAXKEYLEN 64
174 static struct key_info {
175 unsigned char keyid[MAXKEYLEN];
176 int keylen;
177 int dir;
178 int *modep;
179 Encryptions *(*getcrypt)(int);
180 } ki[2] = {
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);
187 void
188 encrypt_init(const char *name, int server)
190 Encryptions *ep = encryptions;
192 Name = name;
193 Server = server;
194 i_support_encrypt = i_support_decrypt = 0;
195 remote_supports_encrypt = remote_supports_decrypt = 0;
196 encrypt_mode = 0;
197 decrypt_mode = 0;
198 encrypt_output = 0;
199 decrypt_input = 0;
201 str_suplen = 4;
203 while (ep->type) {
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;
212 if (ep->init)
213 (*ep->init)(Server);
214 ++ep;
216 str_send[str_suplen++] = IAC;
217 str_send[str_suplen++] = SE;
220 static void
221 encrypt_list_types(void)
223 Encryptions *ep = encryptions;
225 printf("Valid encryption types:\n");
226 while (ep->type) {
227 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
228 ++ep;
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();
238 return(0);
240 if (EncryptType(type, mode))
241 return(EncryptStart(mode));
242 return(0);
246 EncryptDisable(char *type, char *mode)
248 Encryptions *ep;
249 int ret = 0;
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);
259 } else {
260 if ((mode == 0) || (isprefix(mode, "input") ? 1 : 0)) {
261 if (decrypt_mode == ep->type)
262 EncryptStopInput();
263 i_wont_support_decrypt |= typemask(ep->type);
264 ret = 1;
266 if ((mode == 0) || (isprefix(mode, "output"))) {
267 if (encrypt_mode == ep->type)
268 EncryptStopOutput();
269 i_wont_support_encrypt |= typemask(ep->type);
270 ret = 1;
272 if (ret == 0)
273 printf("%s: invalid encryption mode\n", mode);
275 return(ret);
279 EncryptType(char *type, char *mode)
281 Encryptions *ep;
282 int ret = 0;
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);
292 } else {
293 if ((mode == 0) || isprefix(mode, "input")) {
294 decrypt_mode = ep->type;
295 i_wont_support_decrypt &= ~typemask(ep->type);
296 ret = 1;
298 if ((mode == 0) || isprefix(mode, "output")) {
299 encrypt_mode = ep->type;
300 i_wont_support_encrypt &= ~typemask(ep->type);
301 ret = 1;
303 if (ret == 0)
304 printf("%s: invalid encryption mode\n", mode);
306 return(ret);
310 EncryptStart(char *mode)
312 int ret = 0;
313 if (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");
320 return(0);
322 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
323 return(0);
325 ret += EncryptStartInput();
326 ret += EncryptStartOutput();
327 return(ret);
331 EncryptStartInput(void)
333 if (decrypt_mode) {
334 encrypt_send_request_start();
335 return(1);
337 printf("No previous decryption mode, decryption not enabled\r\n");
338 return(0);
342 EncryptStartOutput(void)
344 if (encrypt_mode) {
345 encrypt_start_output(encrypt_mode);
346 return(1);
348 printf("No previous encryption mode, encryption not enabled\r\n");
349 return(0);
353 EncryptStop(char *mode)
355 int ret = 0;
356 if (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");
363 return(0);
365 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
366 return(0);
368 ret += EncryptStopInput();
369 ret += EncryptStopOutput();
370 return(ret);
374 EncryptStopInput(void)
376 encrypt_send_request_end();
377 return(1);
381 EncryptStopOutput(void)
383 encrypt_send_end();
384 return(1);
387 void
388 encrypt_display(void)
390 if (encrypt_output)
391 printf("Currently encrypting output with %s\r\n",
392 ENCTYPE_NAME(encrypt_mode));
393 if (decrypt_input)
394 printf("Currently decrypting input with %s\r\n",
395 ENCTYPE_NAME(decrypt_mode));
399 EncryptStatus(void)
401 if (encrypt_output)
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));
409 if (decrypt_input) {
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));
417 return 1;
420 void
421 encrypt_send_support(void)
423 if (str_suplen) {
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);
433 str_suplen = 0;
438 EncryptDebug(int on)
440 if (on < 0)
441 encrypt_debug_mode ^= 1;
442 else
443 encrypt_debug_mode = on;
444 printf("Encryption debugging %s\r\n",
445 encrypt_debug_mode ? "enabled" : "disabled");
446 return(1);
450 EncryptVerbose(int on)
452 if (on < 0)
453 encrypt_verbose ^= 1;
454 else
455 encrypt_verbose = on;
456 printf("Encryption %s verbose\r\n",
457 encrypt_verbose ? "is" : "is not");
458 return(1);
462 EncryptAutoEnc(int on)
464 encrypt_auto(on);
465 printf("Automatic encryption of output is %s\r\n",
466 autoencrypt ? "enabled" : "disabled");
467 return(1);
471 EncryptAutoDec(int on)
473 decrypt_auto(on);
474 printf("Automatic decryption of input is %s\r\n",
475 autodecrypt ? "enabled" : "disabled");
476 return(1);
480 * Called when ENCRYPT SUPPORT is received.
482 void
483 encrypt_support(unsigned char *typelist, int cnt)
485 int type, use_type = 0;
486 Encryptions *ep;
489 * Forget anything the other side has previously told us.
491 remote_supports_decrypt = 0;
493 while (cnt-- > 0) {
494 type = *typelist++;
495 if (encrypt_debug_mode)
496 printf(">>>%s: He is supporting %s (%d)\r\n",
497 Name,
498 ENCTYPE_NAME(type), type);
499 if ((type < ENCTYPE_CNT) &&
500 (I_SUPPORT_ENCRYPT & typemask(type))) {
501 remote_supports_decrypt |= typemask(type);
502 if (use_type == 0)
503 use_type = type;
506 if (use_type) {
507 ep = findencryption(use_type);
508 if (!ep)
509 return;
510 type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
511 if (encrypt_debug_mode)
512 printf(">>>%s: (*ep->start)() returned %d\r\n",
513 Name, type);
514 if (type < 0)
515 return;
516 encrypt_mode = use_type;
517 if (type == 0)
518 encrypt_start_output(use_type);
522 void
523 encrypt_is(unsigned char *data, int cnt)
525 Encryptions *ep;
526 int type, ret;
528 if (--cnt < 0)
529 return;
530 type = *data++;
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",
536 Name,
537 ENCTYPE_NAME_OK(type)
538 ? ENCTYPE_NAME(type) : "(unknown)",
539 type);
540 return;
542 if (!ep->is) {
543 if (encrypt_debug_mode)
544 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
545 Name,
546 ENCTYPE_NAME_OK(type)
547 ? ENCTYPE_NAME(type) : "(unknown)",
548 type);
549 ret = 0;
550 } else {
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);
557 if (ret < 0) {
558 autodecrypt = 0;
559 } else {
560 decrypt_mode = type;
561 if (ret == 0 && autodecrypt)
562 encrypt_send_request_start();
566 void
567 encrypt_reply(unsigned char *data, int cnt)
569 Encryptions *ep;
570 int ret, type;
572 if (--cnt < 0)
573 return;
574 type = *data++;
575 if (!(ep = findencryption(type))) {
576 if (encrypt_debug_mode)
577 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
578 Name,
579 ENCTYPE_NAME_OK(type)
580 ? ENCTYPE_NAME(type) : "(unknown)",
581 type);
582 return;
584 if (!ep->reply) {
585 if (encrypt_debug_mode)
586 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
587 Name,
588 ENCTYPE_NAME_OK(type)
589 ? ENCTYPE_NAME(type) : "(unknown)",
590 type);
591 ret = 0;
592 } else {
593 ret = (*ep->reply)(data, cnt);
594 if (encrypt_debug_mode)
595 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
596 data, cnt,
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);
602 if (ret < 0) {
603 autoencrypt = 0;
604 } else {
605 encrypt_mode = type;
606 if (ret == 0 && autoencrypt)
607 encrypt_start_output(type);
612 * Called when a ENCRYPT START command is received.
614 void
615 encrypt_start(unsigned char *data __unused, int cnt __unused)
617 Encryptions *ep;
619 if (!decrypt_mode) {
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();
628 return;
631 if ((ep = finddecryption(decrypt_mode))) {
632 decrypt_input = ep->input;
633 if (encrypt_verbose)
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));
639 } else {
640 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
641 Name,
642 ENCTYPE_NAME_OK(decrypt_mode)
643 ? ENCTYPE_NAME(decrypt_mode)
644 : "(unknown)",
645 decrypt_mode);
646 encrypt_send_request_end();
650 void
651 encrypt_session_key( Session_Key *key, int server)
653 Encryptions *ep = encryptions;
655 havesessionkey = 1;
657 while (ep->type) {
658 if (ep->session)
659 (*ep->session)(key, server);
660 ++ep;
665 * Called when ENCRYPT END is received.
667 void
668 encrypt_end(void)
670 decrypt_input = 0;
671 if (encrypt_debug_mode)
672 printf(">>>%s: Input is back to clear text\r\n", Name);
673 if (encrypt_verbose)
674 printf("[ Input is now clear text ]\r\n");
678 * Called when ENCRYPT REQUEST-END is received.
680 void
681 encrypt_request_end(void)
683 encrypt_send_end();
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
690 * can.
692 void
693 encrypt_request_start(unsigned char *data __unused, int cnt __unused)
695 if (encrypt_mode == 0) {
696 if (Server)
697 autoencrypt = 1;
698 return;
700 encrypt_start_output(encrypt_mode);
703 static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
705 void
706 encrypt_enc_keyid(unsigned char *keyid, int len)
708 encrypt_keyid(&ki[1], keyid, len);
711 void
712 encrypt_dec_keyid(unsigned char *keyid, int len)
714 encrypt_keyid(&ki[0], keyid, len);
717 void
718 encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
720 Encryptions *ep;
721 int dir = kp->dir;
722 int ret = 0;
724 if (!(ep = (*kp->getcrypt)(*kp->modep))) {
725 if (len == 0)
726 return;
727 kp->keylen = 0;
728 } else if (len == 0) {
730 * Empty option, indicates a failure.
732 if (kp->keylen == 0)
733 return;
734 kp->keylen = 0;
735 if (ep->keyid)
736 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
738 } else if ((len != kp->keylen) ||
739 (memcmp(keyid, kp->keyid, len) != 0)) {
741 * Length or contents are different
743 kp->keylen = len;
744 memmove(kp->keyid, keyid, len);
745 if (ep->keyid)
746 (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
747 } else {
748 if (ep->keyid)
749 ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
750 if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
751 encrypt_start_output(*kp->modep);
752 return;
755 encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
758 void
759 encrypt_send_keyid(int dir, const char *keyid, int keylen, int saveit)
761 unsigned char *strp;
763 str_keyid[3] = (dir == DIR_ENCRYPT)
764 ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
765 if (saveit) {
766 struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
767 memmove(kp->keyid, keyid, keylen);
768 kp->keylen = keylen;
771 for (strp = &str_keyid[4]; keylen > 0; --keylen) {
772 if ((*strp++ = *keyid++) == IAC)
773 *strp++ = IAC;
775 *strp++ = IAC;
776 *strp++ = SE;
777 net_write(str_keyid, strp - str_keyid);
778 printsub('>', &str_keyid[2], strp - str_keyid - 2);
781 void
782 encrypt_auto(int on)
784 if (on < 0)
785 autoencrypt ^= 1;
786 else
787 autoencrypt = on ? 1 : 0;
790 void
791 decrypt_auto(int on)
793 if (on < 0)
794 autodecrypt ^= 1;
795 else
796 autodecrypt = on ? 1 : 0;
799 void
800 encrypt_start_output(int type)
802 Encryptions *ep;
803 unsigned char *p;
804 int i;
806 if (!(ep = findencryption(type))) {
807 if (encrypt_debug_mode) {
808 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
809 Name,
810 ENCTYPE_NAME_OK(type)
811 ? ENCTYPE_NAME(type) : "(unknown)",
812 type);
814 return;
816 if (ep->start) {
817 i = (*ep->start)(DIR_ENCRYPT, Server);
818 if (encrypt_debug_mode) {
819 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
820 Name,
821 (i < 0) ? "failed" :
822 "initial negotiation in progress",
823 i, ENCTYPE_NAME(type));
825 if (i)
826 return;
828 p = str_start + 3;
829 *p++ = ENCRYPT_START;
830 for (i = 0; i < ki[0].keylen; ++i) {
831 if ((*p++ = ki[0].keyid[i]) == IAC)
832 *p++ = IAC;
834 *p++ = IAC;
835 *p++ = SE;
836 net_write(str_start, p - str_start);
837 net_encrypt();
838 printsub('>', &str_start[2], p - &str_start[2]);
840 * If we are already encrypting in some mode, then
841 * encrypt the ring (which includes our request) in
842 * the old mode, mark it all as "clear text" and then
843 * switch to the new mode.
845 encrypt_output = ep->output;
846 encrypt_mode = type;
847 if (encrypt_debug_mode)
848 printf(">>>%s: Started to encrypt output with type %s\r\n",
849 Name, ENCTYPE_NAME(type));
850 if (encrypt_verbose)
851 printf("[ Output is now encrypted with type %s ]\r\n",
852 ENCTYPE_NAME(type));
855 void
856 encrypt_send_end(void)
858 if (!encrypt_output)
859 return;
861 str_end[3] = ENCRYPT_END;
862 net_write(str_end, sizeof(str_end));
863 net_encrypt();
864 printsub('>', &str_end[2], sizeof(str_end) - 2);
866 * Encrypt the output buffer now because it will not be done by
867 * netflush...
869 encrypt_output = 0;
870 if (encrypt_debug_mode)
871 printf(">>>%s: Output is back to clear text\r\n", Name);
872 if (encrypt_verbose)
873 printf("[ Output is now clear text ]\r\n");
876 void
877 encrypt_send_request_start(void)
879 unsigned char *p;
880 int i;
882 p = &str_start[3];
883 *p++ = ENCRYPT_REQSTART;
884 for (i = 0; i < ki[1].keylen; ++i) {
885 if ((*p++ = ki[1].keyid[i]) == IAC)
886 *p++ = IAC;
888 *p++ = IAC;
889 *p++ = SE;
890 net_write(str_start, p - str_start);
891 printsub('>', &str_start[2], p - &str_start[2]);
892 if (encrypt_debug_mode)
893 printf(">>>%s: Request input to be encrypted\r\n", Name);
896 void
897 encrypt_send_request_end(void)
899 str_end[3] = ENCRYPT_REQEND;
900 net_write(str_end, sizeof(str_end));
901 printsub('>', &str_end[2], sizeof(str_end) - 2);
903 if (encrypt_debug_mode)
904 printf(">>>%s: Request input to be clear text\r\n", Name);
907 void
908 encrypt_wait(void)
910 if (encrypt_debug_mode)
911 printf(">>>%s: in encrypt_wait\r\n", Name);
912 if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
913 return;
914 while (autoencrypt && !encrypt_output)
915 if (telnet_spin())
916 return;
919 void
920 encrypt_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
922 char tbuf[16], *cp;
924 cnt -= 2;
925 data += 2;
926 buf[buflen-1] = '\0';
927 buf[buflen-2] = '*';
928 buflen -= 2;;
929 for (; cnt > 0; cnt--, data++) {
930 sprintf(tbuf, " %d", *data);
931 for (cp = tbuf; *cp && buflen > 0; --buflen)
932 *buf++ = *cp++;
933 if (buflen <= 0)
934 return;
936 *buf = '\0';
939 void
940 encrypt_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
942 Encryptions *ep;
943 int type = data[1];
945 for (ep = encryptions; ep->type && ep->type != type; ep++)
948 if (ep->printsub)
949 (*ep->printsub)(data, cnt, buf, buflen);
950 else
951 encrypt_gen_printsub(data, cnt, buf, buflen);
953 #endif /* ENCRYPTION */