1 /* $NetBSD: encrypt.c,v 1.13 2005/02/06 05:53:07 perry Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 static char sccsid
[] = "@(#)encrypt.c 8.2 (Berkeley) 5/30/95";
36 __RCSID("$NetBSD: encrypt.c,v 1.13 2005/02/06 05:53:07 perry Exp $");
40 * Copyright (C) 1990 by the Massachusetts Institute of Technology
42 * Export of this software from the United States of America is assumed
43 * to require a specific license from the United States Government.
44 * It is the responsibility of any person or organization contemplating
45 * export to obtain such a license before exporting.
47 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
48 * distribute this software and its documentation for any purpose and
49 * without fee is hereby granted, provided that the above copyright
50 * notice appear in all copies and that both that copyright notice and
51 * this permission notice appear in supporting documentation, and that
52 * the name of M.I.T. not be used in advertising or publicity pertaining
53 * to distribution of the software without specific, written prior
54 * permission. M.I.T. makes no representations about the suitability of
55 * this software for any purpose. It is provided "as is" without express
56 * or implied warranty.
63 #include <arpa/telnet.h>
76 * These functions pointers point to the current routines
77 * for encrypting and decrypting data.
79 void (*encrypt_output
)(unsigned char *, int);
80 int (*decrypt_input
)(int);
82 int encrypt_debug_mode
= 0;
83 static int decrypt_mode
= 0;
84 static int encrypt_mode
= 0;
85 static int encrypt_verbose
= 0;
86 static int autoencrypt
= 0;
87 static int autodecrypt
= 0;
88 static int havesessionkey
= 0;
89 static int Server
= 0;
90 static const char *Name
= "Noname";
92 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
94 static long i_support_encrypt
= typemask(ENCTYPE_DES_CFB64
)
95 | typemask(ENCTYPE_DES_OFB64
);
96 static long i_support_decrypt
= typemask(ENCTYPE_DES_CFB64
)
97 | typemask(ENCTYPE_DES_OFB64
);
98 static long i_wont_support_encrypt
= 0;
99 static long i_wont_support_decrypt
= 0;
100 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
101 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
103 static long remote_supports_encrypt
= 0;
104 static long remote_supports_decrypt
= 0;
106 static Encryptions encryptions
[] = {
107 #ifdef DES_ENCRYPTION
108 { "DES_CFB64", ENCTYPE_DES_CFB64
,
118 { "DES_OFB64", ENCTYPE_DES_OFB64
,
128 #endif /* DES_ENCRYPTION */
132 static unsigned char str_send
[64] = { IAC
, SB
, TELOPT_ENCRYPT
,
134 static unsigned char str_suplen
= 0;
135 static unsigned char str_start
[72] = { IAC
, SB
, TELOPT_ENCRYPT
};
136 static unsigned char str_end
[] = { IAC
, SB
, TELOPT_ENCRYPT
, 0, IAC
, SE
};
142 Encryptions
*ep
= encryptions
;
144 if (!(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
& typemask(type
)))
146 while (ep
->type
&& ep
->type
!= type
)
148 return(ep
->type
? ep
: 0);
155 Encryptions
*ep
= encryptions
;
157 if (!(I_SUPPORT_DECRYPT
& remote_supports_encrypt
& 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
},
178 encrypt_init(name
, server
)
182 Encryptions
*ep
= encryptions
;
186 i_support_encrypt
= i_support_decrypt
= 0;
187 remote_supports_encrypt
= remote_supports_decrypt
= 0;
193 encrypt_verbose
= !server
;
199 if (encrypt_debug_mode
)
200 printf(">>>%s: I will support %s\r\n",
201 Name
, ENCTYPE_NAME(ep
->type
));
202 i_support_encrypt
|= typemask(ep
->type
);
203 i_support_decrypt
|= typemask(ep
->type
);
204 if ((i_wont_support_decrypt
& typemask(ep
->type
)) == 0)
205 if ((str_send
[str_suplen
++] = ep
->type
) == IAC
)
206 str_send
[str_suplen
++] = IAC
;
211 str_send
[str_suplen
++] = IAC
;
212 str_send
[str_suplen
++] = SE
;
218 Encryptions
*ep
= encryptions
;
220 printf("Valid encryption types:\n");
222 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep
->type
), ep
->type
);
228 EncryptEnable(type
, 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(type
, mode
)
245 register Encryptions
*ep
;
248 if (isprefix(type
, "help") || isprefix(type
, "?")) {
249 printf("Usage: encrypt disable <type> [input|output]\n");
250 encrypt_list_types();
251 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
252 sizeof(Encryptions
))) == 0) {
253 printf("%s: invalid encryption type\n", type
);
254 } else if (Ambiguous(ep
)) {
255 printf("Ambiguous type '%s'\n", type
);
257 if ((mode
== 0) || (isprefix(mode
, "input") ? 1 : 0)) {
258 if (decrypt_mode
== ep
->type
)
260 i_wont_support_decrypt
|= typemask(ep
->type
);
263 if ((mode
== 0) || (isprefix(mode
, "output"))) {
264 if (encrypt_mode
== ep
->type
)
266 i_wont_support_encrypt
|= typemask(ep
->type
);
270 printf("%s: invalid encryption mode\n", mode
);
276 EncryptType(type
, mode
)
280 register Encryptions
*ep
;
283 if (isprefix(type
, "help") || isprefix(type
, "?")) {
284 printf("Usage: encrypt type <type> [input|output]\n");
285 encrypt_list_types();
286 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
287 sizeof(Encryptions
))) == 0) {
288 printf("%s: invalid encryption type\n", type
);
289 } else if (Ambiguous(ep
)) {
290 printf("Ambiguous type '%s'\n", type
);
292 if ((mode
== 0) || isprefix(mode
, "input")) {
293 decrypt_mode
= ep
->type
;
294 i_wont_support_decrypt
&= ~typemask(ep
->type
);
297 if ((mode
== 0) || isprefix(mode
, "output")) {
298 encrypt_mode
= ep
->type
;
299 i_wont_support_encrypt
&= ~typemask(ep
->type
);
303 printf("%s: invalid encryption mode\n", mode
);
312 register int ret
= 0;
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();
334 encrypt_send_request_start();
337 printf("No previous decryption mode, decryption not enabled\r\n");
345 encrypt_start_output(encrypt_mode
);
348 printf("No previous encryption mode, encryption not enabled\r\n");
358 if (isprefix(mode
, "input"))
359 return(EncryptStopInput());
360 if (isprefix(mode
, "output"))
361 return(EncryptStopOutput());
362 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
363 printf("Usage: encrypt stop [input|output]\n");
366 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode
);
369 ret
+= EncryptStopInput();
370 ret
+= EncryptStopOutput();
377 encrypt_send_request_end();
392 printf("Currently encrypting output with %s\r\n",
393 ENCTYPE_NAME(encrypt_mode
));
395 printf("Currently decrypting input with %s\r\n",
396 ENCTYPE_NAME(decrypt_mode
));
403 printf("Currently encrypting output with %s\r\n",
404 ENCTYPE_NAME(encrypt_mode
));
405 else if (encrypt_mode
) {
406 printf("Currently output is clear text.\r\n");
407 printf("Last encryption mode was %s\r\n",
408 ENCTYPE_NAME(encrypt_mode
));
411 printf("Currently decrypting input with %s\r\n",
412 ENCTYPE_NAME(decrypt_mode
));
413 } else if (decrypt_mode
) {
414 printf("Currently input is clear text.\r\n");
415 printf("Last decryption mode was %s\r\n",
416 ENCTYPE_NAME(decrypt_mode
));
422 encrypt_send_support()
426 * If the user has requested that decryption start
427 * immediatly, then send a "REQUEST START" before
428 * we negotiate the type.
430 if (!Server
&& autodecrypt
)
431 encrypt_send_request_start();
432 telnet_net_write(str_send
, str_suplen
);
433 printsub('>', &str_send
[2], str_suplen
- 2);
443 encrypt_debug_mode
^= 1;
445 encrypt_debug_mode
= on
;
446 printf("Encryption debugging %s\r\n",
447 encrypt_debug_mode
? "enabled" : "disabled");
456 encrypt_verbose
^= 1;
458 encrypt_verbose
= on
;
459 printf("Encryption %s verbose\r\n",
460 encrypt_verbose
? "is" : "is not");
469 printf("Automatic encryption of output is %s\r\n",
470 autoencrypt
? "enabled" : "disabled");
479 printf("Automatic decryption of input is %s\r\n",
480 autodecrypt
? "enabled" : "disabled");
485 * Called when ENCRYPT SUPPORT is received.
488 encrypt_support(typelist
, cnt
)
489 unsigned char *typelist
;
492 register int type
, use_type
= 0;
496 * Forget anything the other side has previously told us.
498 remote_supports_decrypt
= 0;
502 if (encrypt_debug_mode
)
503 printf(">>>%s: He is supporting %s (%d)\r\n",
505 ENCTYPE_NAME(type
), type
);
506 if ((type
< ENCTYPE_CNT
) &&
507 (I_SUPPORT_ENCRYPT
& typemask(type
))) {
508 remote_supports_decrypt
|= typemask(type
);
514 ep
= findencryption(use_type
);
517 type
= ep
->start
? (*ep
->start
)(DIR_ENCRYPT
, Server
) : 0;
518 if (encrypt_debug_mode
)
519 printf(">>>%s: (*ep->start)() returned %d\r\n",
523 encrypt_mode
= use_type
;
525 encrypt_start_output(use_type
);
530 encrypt_is(data
, cnt
)
535 register int type
, ret
;
540 if (type
< ENCTYPE_CNT
)
541 remote_supports_encrypt
|= typemask(type
);
542 if (!(ep
= finddecryption(type
))) {
543 if (encrypt_debug_mode
)
544 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
546 ENCTYPE_NAME_OK(type
)
547 ? ENCTYPE_NAME(type
) : "(unknown)",
552 if (encrypt_debug_mode
)
553 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
555 ENCTYPE_NAME_OK(type
)
556 ? ENCTYPE_NAME(type
) : "(unknown)",
560 ret
= (*ep
->is
)(data
, cnt
);
561 if (encrypt_debug_mode
)
562 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data
, cnt
,
563 (ret
< 0) ? "FAIL " :
564 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
570 if (ret
== 0 && autodecrypt
)
571 encrypt_send_request_start();
576 encrypt_reply(data
, cnt
)
581 register int ret
, type
;
586 if (!(ep
= findencryption(type
))) {
587 if (encrypt_debug_mode
)
588 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
590 ENCTYPE_NAME_OK(type
)
591 ? ENCTYPE_NAME(type
) : "(unknown)",
596 if (encrypt_debug_mode
)
597 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
599 ENCTYPE_NAME_OK(type
)
600 ? ENCTYPE_NAME(type
) : "(unknown)",
604 ret
= (*ep
->reply
)(data
, cnt
);
605 if (encrypt_debug_mode
)
606 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
608 (ret
< 0) ? "FAIL " :
609 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
611 if (encrypt_debug_mode
)
612 printf(">>>%s: encrypt_reply returned %d\n", Name
, ret
);
617 if (ret
== 0 && autoencrypt
)
618 encrypt_start_output(type
);
623 * Called when a ENCRYPT START command is received.
626 encrypt_start(data
, cnt
)
634 * Something is wrong. We should not get a START
635 * command without having already picked our
636 * decryption scheme. Send a REQUEST-END to
637 * attempt to clear the channel...
639 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name
);
640 encrypt_send_request_end();
644 if ((ep
= finddecryption(decrypt_mode
)) != NULL
) {
645 decrypt_input
= ep
->input
;
647 printf("[ Input is now decrypted with type %s ]\r\n",
648 ENCTYPE_NAME(decrypt_mode
));
649 if (encrypt_debug_mode
)
650 printf(">>>%s: Start to decrypt input with type %s\r\n",
651 Name
, ENCTYPE_NAME(decrypt_mode
));
653 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
655 ENCTYPE_NAME_OK(decrypt_mode
)
656 ? ENCTYPE_NAME(decrypt_mode
)
659 encrypt_send_request_end();
664 encrypt_session_key(key
, server
)
668 Encryptions
*ep
= encryptions
;
674 (*ep
->session
)(key
, server
);
676 if (!encrypt_output
&& autoencrypt
&& !server
)
677 encrypt_start_output(ep
->type
);
678 if (!decrypt_input
&& autodecrypt
&& !server
)
679 encrypt_send_request_start();
686 * Called when ENCRYPT END is received.
692 if (encrypt_debug_mode
)
693 printf(">>>%s: Input is back to clear text\r\n", Name
);
695 printf("[ Input is now clear text ]\r\n");
699 * Called when ENCRYPT REQUEST-END is received.
702 encrypt_request_end()
708 * Called when ENCRYPT REQUEST-START is received. If we receive
709 * this before a type is picked, then that indicates that the
710 * other side wants us to start encrypting data as soon as we
714 encrypt_request_start(data
, cnt
)
718 if (encrypt_mode
== 0) {
723 encrypt_start_output(encrypt_mode
);
726 static unsigned char str_keyid
[(MAXKEYLEN
*2)+5] = { IAC
, SB
, TELOPT_ENCRYPT
};
729 encrypt_enc_keyid(keyid
, len
)
730 unsigned char *keyid
;
733 encrypt_keyid(&ki
[1], keyid
, len
);
737 encrypt_dec_keyid(keyid
, len
)
738 unsigned char *keyid
;
741 encrypt_keyid(&ki
[0], keyid
, len
);
745 encrypt_keyid(kp
, keyid
, len
)
747 unsigned char *keyid
;
752 register int ret
= 0;
754 if (!(ep
= (*kp
->getcrypt
)(*kp
->modep
))) {
758 } else if (len
== 0) {
760 * Empty option, indicates a failure.
766 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
768 } else if ((len
!= kp
->keylen
) ||
769 (memcmp(keyid
, kp
->keyid
, len
) != 0)) {
771 * Length or contents are different
774 memmove(kp
->keyid
, keyid
, len
);
776 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
779 ret
= (*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
780 if ((ret
== 0) && (dir
== DIR_ENCRYPT
) && autoencrypt
)
781 encrypt_start_output(*kp
->modep
);
785 encrypt_send_keyid(dir
, kp
->keyid
, kp
->keylen
, 0);
789 encrypt_send_keyid(dir
, keyid
, keylen
, saveit
)
791 unsigned char *keyid
;
797 str_keyid
[3] = (dir
== DIR_ENCRYPT
)
798 ? ENCRYPT_ENC_KEYID
: ENCRYPT_DEC_KEYID
;
800 struct key_info
*kp
= &ki
[(dir
== DIR_ENCRYPT
) ? 0 : 1];
801 memmove(kp
->keyid
, keyid
, keylen
);
805 for (strp
= &str_keyid
[4]; keylen
> 0; --keylen
) {
806 if ((*strp
++ = *keyid
++) == IAC
)
811 telnet_net_write(str_keyid
, strp
- str_keyid
);
812 printsub('>', &str_keyid
[2], strp
- str_keyid
- 2);
822 autoencrypt
= on
? 1 : 0;
832 autodecrypt
= on
? 1 : 0;
836 encrypt_start_output(type
)
840 register unsigned char *p
;
843 if (!(ep
= findencryption(type
))) {
844 if (encrypt_debug_mode
) {
845 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
847 ENCTYPE_NAME_OK(type
)
848 ? ENCTYPE_NAME(type
) : "(unknown)",
854 i
= (*ep
->start
)(DIR_ENCRYPT
, Server
);
855 if (encrypt_debug_mode
) {
856 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
859 "initial negotiation in progress",
860 i
, ENCTYPE_NAME(type
));
866 *p
++ = ENCRYPT_START
;
867 for (i
= 0; i
< ki
[0].keylen
; ++i
) {
868 if ((*p
++ = ki
[0].keyid
[i
]) == IAC
)
873 telnet_net_write(str_start
, p
- str_start
);
875 printsub('>', &str_start
[2], p
- &str_start
[2]);
877 * If we are already encrypting in some mode, then
878 * encrypt the ring (which includes our request) in
879 * the old mode, mark it all as "clear text" and then
880 * switch to the new mode.
882 encrypt_output
= ep
->output
;
884 if (encrypt_debug_mode
)
885 printf(">>>%s: Started to encrypt output with type %s\r\n",
886 Name
, ENCTYPE_NAME(type
));
888 printf("[ Output is now encrypted with type %s ]\r\n",
898 str_end
[3] = ENCRYPT_END
;
899 telnet_net_write(str_end
, sizeof(str_end
));
901 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
903 * Encrypt the output buffer now because it will not be done by
907 if (encrypt_debug_mode
)
908 printf(">>>%s: Output is back to clear text\r\n", Name
);
910 printf("[ Output is now clear text ]\r\n");
914 encrypt_send_request_start()
916 register unsigned char *p
;
920 *p
++ = ENCRYPT_REQSTART
;
921 for (i
= 0; i
< ki
[1].keylen
; ++i
) {
922 if ((*p
++ = ki
[1].keyid
[i
]) == IAC
)
927 telnet_net_write(str_start
, p
- str_start
);
928 printsub('>', &str_start
[2], p
- &str_start
[2]);
929 if (encrypt_debug_mode
)
930 printf(">>>%s: Request input to be encrypted\r\n", Name
);
934 encrypt_send_request_end()
936 str_end
[3] = ENCRYPT_REQEND
;
937 telnet_net_write(str_end
, sizeof(str_end
));
938 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
940 if (encrypt_debug_mode
)
941 printf(">>>%s: Request input to be clear text\r\n", Name
);
947 if (encrypt_debug_mode
)
948 printf(">>>%s: in encrypt_wait\r\n", Name
);
949 if (!havesessionkey
|| !(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
))
951 while (autoencrypt
&& !encrypt_output
)
960 encrypt_debug_mode
= mode
;
964 encrypt_gen_printsub(data
, cnt
, buf
, buflen
)
965 unsigned char *data
, *buf
;
972 buf
[buflen
-1] = '\0';
975 for (; cnt
> 0; cnt
--, data
++) {
976 snprintf(tbuf
, sizeof(tbuf
), " %d", *data
);
977 for (cp
= tbuf
; *cp
&& buflen
> 0; --buflen
)
986 encrypt_printsub(data
, cnt
, buf
, buflen
)
987 unsigned char *data
, *buf
;
991 register int type
= data
[1];
993 for (ep
= encryptions
; ep
->type
&& ep
->type
!= type
; ep
++)
997 (*ep
->printsub
)(data
, cnt
, buf
, buflen
);
999 encrypt_gen_printsub(data
, cnt
, buf
, buflen
);
1001 #endif /* ENCRYPTION */