]>
git.cameronkatri.com Git - apple_cmds.git/blob - lib/libtelnet/encrypt.c
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
34 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/crypto/telnet/libtelnet/encrypt.c,v 1.3.2.2 2002/04/13 10:59:07 markm Exp $");
42 static const char sccsid
[] = "@(#)encrypt.c 8.2 (Berkeley) 5/30/95";
47 * Copyright (C) 1990 by the Massachusetts Institute of Technology
49 * Export of this software from the United States of America is assumed
50 * to require a specific license from the United States Government.
51 * It is the responsibility of any person or organization contemplating
52 * export to obtain such a license before exporting.
54 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
55 * distribute this software and its documentation for any purpose and
56 * without fee is hereby granted, provided that the above copyright
57 * notice appear in all copies and that both that copyright notice and
58 * this permission notice appear in supporting documentation, and that
59 * the name of M.I.T. not be used in advertising or publicity pertaining
60 * to distribution of the software without specific, written prior
61 * permission. M.I.T. makes no representations about the suitability of
62 * this software for any purpose. It is provided "as is" without express
63 * or implied warranty.
69 #include <arpa/telnet.h>
78 #define __unused __attribute__((__unused__))
82 * These functions pointers point to the current routines
83 * for encrypting and decrypting data.
85 void (*encrypt_output
)(unsigned char *, int);
86 int (*decrypt_input
)(int);
88 int EncryptType(char *type
, char *mode
);
89 int EncryptStart(char *mode
);
90 int EncryptStop(char *mode
);
91 int EncryptStartInput(void);
92 int EncryptStartOutput(void);
93 int EncryptStopInput(void);
94 int EncryptStopOutput(void);
96 int encrypt_debug_mode
= 0;
97 static int decrypt_mode
= 0;
98 static int encrypt_mode
= 0;
99 static int encrypt_verbose
= 0;
100 static int autoencrypt
= 0;
101 static int autodecrypt
= 0;
102 static int havesessionkey
= 0;
103 static int Server
= 0;
104 static const char *Name
= "Noname";
106 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
108 static long i_support_encrypt
= 0
109 | typemask(ENCTYPE_DES_CFB64
) | typemask(ENCTYPE_DES_OFB64
)
111 static long i_support_decrypt
= 0
112 | typemask(ENCTYPE_DES_CFB64
) | typemask(ENCTYPE_DES_OFB64
)
115 static long i_wont_support_encrypt
= 0;
116 static long i_wont_support_decrypt
= 0;
117 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
118 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
120 static long remote_supports_encrypt
= 0;
121 static long remote_supports_decrypt
= 0;
123 static Encryptions encryptions
[] = {
124 { "DES_CFB64", ENCTYPE_DES_CFB64
,
134 { "DES_OFB64", ENCTYPE_DES_OFB64
,
144 { NULL
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
147 static unsigned char str_send
[64] = { IAC
, SB
, TELOPT_ENCRYPT
,
149 static unsigned char str_suplen
= 0;
150 static unsigned char str_start
[72] = { IAC
, SB
, TELOPT_ENCRYPT
};
151 static unsigned char str_end
[] = { IAC
, SB
, TELOPT_ENCRYPT
, 0, IAC
, SE
};
154 findencryption(int type
)
156 Encryptions
*ep
= encryptions
;
158 if (!(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
& (unsigned)typemask(type
)))
160 while (ep
->type
&& ep
->type
!= type
)
162 return(ep
->type
? ep
: 0);
166 finddecryption(int type
)
168 Encryptions
*ep
= encryptions
;
170 if (!(I_SUPPORT_DECRYPT
& remote_supports_encrypt
& (unsigned)typemask(type
)))
172 while (ep
->type
&& ep
->type
!= type
)
174 return(ep
->type
? ep
: 0);
179 static struct key_info
{
180 unsigned char keyid
[MAXKEYLEN
];
184 Encryptions
*(*getcrypt
)(int);
186 { { 0 }, 0, DIR_ENCRYPT
, &encrypt_mode
, findencryption
},
187 { { 0 }, 0, DIR_DECRYPT
, &decrypt_mode
, finddecryption
},
190 static void encrypt_keyid(struct key_info
*kp
, unsigned char *keyid
, int len
);
193 encrypt_init(const char *name
, int server
)
195 Encryptions
*ep
= encryptions
;
199 i_support_encrypt
= i_support_decrypt
= 0;
200 remote_supports_encrypt
= remote_supports_decrypt
= 0;
209 if (encrypt_debug_mode
)
210 printf(">>>%s: I will support %s\r\n",
211 Name
, ENCTYPE_NAME(ep
->type
));
212 i_support_encrypt
|= typemask(ep
->type
);
213 i_support_decrypt
|= typemask(ep
->type
);
214 if ((i_wont_support_decrypt
& typemask(ep
->type
)) == 0)
215 if ((str_send
[str_suplen
++] = ep
->type
) == IAC
)
216 str_send
[str_suplen
++] = IAC
;
221 str_send
[str_suplen
++] = IAC
;
222 str_send
[str_suplen
++] = SE
;
226 encrypt_list_types(void)
228 Encryptions
*ep
= encryptions
;
230 printf("Valid encryption types:\n");
232 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep
->type
), ep
->type
);
238 EncryptEnable(char *type
, char *mode
)
240 if (isprefix(type
, "help") || isprefix(type
, "?")) {
241 printf("Usage: encrypt enable <type> [input|output]\n");
242 encrypt_list_types();
245 if (EncryptType(type
, mode
))
246 return(EncryptStart(mode
));
251 EncryptDisable(char *type
, char *mode
)
256 if (isprefix(type
, "help") || isprefix(type
, "?")) {
257 printf("Usage: encrypt disable <type> [input|output]\n");
258 encrypt_list_types();
259 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
260 sizeof(Encryptions
))) == 0) {
261 printf("%s: invalid encryption type\n", type
);
262 } else if (Ambiguous((char **)ep
)) {
263 printf("Ambiguous type '%s'\n", type
);
265 if ((mode
== 0) || (isprefix(mode
, "input") ? 1 : 0)) {
266 if (decrypt_mode
== ep
->type
)
268 i_wont_support_decrypt
|= typemask(ep
->type
);
271 if ((mode
== 0) || (isprefix(mode
, "output"))) {
272 if (encrypt_mode
== ep
->type
)
274 i_wont_support_encrypt
|= typemask(ep
->type
);
278 printf("%s: invalid encryption mode\n", mode
);
284 EncryptType(char *type
, char *mode
)
289 if (isprefix(type
, "help") || isprefix(type
, "?")) {
290 printf("Usage: encrypt type <type> [input|output]\n");
291 encrypt_list_types();
292 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
293 sizeof(Encryptions
))) == 0) {
294 printf("%s: invalid encryption type\n", type
);
295 } else if (Ambiguous((char **)ep
)) {
296 printf("Ambiguous type '%s'\n", type
);
298 if ((mode
== 0) || isprefix(mode
, "input")) {
299 decrypt_mode
= ep
->type
;
300 i_wont_support_decrypt
&= ~typemask(ep
->type
);
303 if ((mode
== 0) || isprefix(mode
, "output")) {
304 encrypt_mode
= ep
->type
;
305 i_wont_support_encrypt
&= ~typemask(ep
->type
);
309 printf("%s: invalid encryption mode\n", mode
);
315 EncryptStart(char *mode
)
319 if (isprefix(mode
, "input"))
320 return(EncryptStartInput());
321 if (isprefix(mode
, "output"))
322 return(EncryptStartOutput());
323 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
324 printf("Usage: encrypt start [input|output]\n");
327 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode
);
330 ret
+= EncryptStartInput();
331 ret
+= EncryptStartOutput();
336 EncryptStartInput(void)
339 encrypt_send_request_start();
342 printf("No previous decryption mode, decryption not enabled\r\n");
347 EncryptStartOutput(void)
350 encrypt_start_output(encrypt_mode
);
353 printf("No previous encryption mode, encryption not enabled\r\n");
358 EncryptStop(char *mode
)
362 if (isprefix(mode
, "input"))
363 return(EncryptStopInput());
364 if (isprefix(mode
, "output"))
365 return(EncryptStopOutput());
366 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
367 printf("Usage: encrypt stop [input|output]\n");
370 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode
);
373 ret
+= EncryptStopInput();
374 ret
+= EncryptStopOutput();
379 EncryptStopInput(void)
381 encrypt_send_request_end();
386 EncryptStopOutput(void)
393 encrypt_display(void)
396 printf("Currently encrypting output with %s\r\n",
397 ENCTYPE_NAME(encrypt_mode
));
399 printf("Currently decrypting input with %s\r\n",
400 ENCTYPE_NAME(decrypt_mode
));
407 printf("Currently encrypting output with %s\r\n",
408 ENCTYPE_NAME(encrypt_mode
));
409 else if (encrypt_mode
) {
410 printf("Currently output is clear text.\r\n");
411 printf("Last encryption mode was %s\r\n",
412 ENCTYPE_NAME(encrypt_mode
));
415 printf("Currently decrypting input with %s\r\n",
416 ENCTYPE_NAME(decrypt_mode
));
417 } else if (decrypt_mode
) {
418 printf("Currently input is clear text.\r\n");
419 printf("Last decryption mode was %s\r\n",
420 ENCTYPE_NAME(decrypt_mode
));
426 encrypt_send_support(void)
430 * If the user has requested that decryption start
431 * immediatly, then send a "REQUEST START" before
432 * we negotiate the type.
434 if (!Server
&& autodecrypt
)
435 encrypt_send_request_start();
436 net_write(str_send
, str_suplen
);
437 printsub('>', &str_send
[2], str_suplen
- 2);
446 encrypt_debug_mode
^= 1;
448 encrypt_debug_mode
= on
;
449 printf("Encryption debugging %s\r\n",
450 encrypt_debug_mode
? "enabled" : "disabled");
455 EncryptVerbose(int on
)
458 encrypt_verbose
^= 1;
460 encrypt_verbose
= on
;
461 printf("Encryption %s verbose\r\n",
462 encrypt_verbose
? "is" : "is not");
467 EncryptAutoEnc(int on
)
470 printf("Automatic encryption of output is %s\r\n",
471 autoencrypt
? "enabled" : "disabled");
476 EncryptAutoDec(int on
)
479 printf("Automatic decryption of input is %s\r\n",
480 autodecrypt
? "enabled" : "disabled");
485 * Called when ENCRYPT SUPPORT is received.
488 encrypt_support(unsigned char *typelist
, int cnt
)
490 int type
, use_type
= 0;
494 * Forget anything the other side has previously told us.
496 remote_supports_decrypt
= 0;
500 if (encrypt_debug_mode
)
501 printf(">>>%s: He is supporting %s (%d)\r\n",
503 ENCTYPE_NAME(type
), type
);
504 if ((type
< ENCTYPE_CNT
) &&
505 (I_SUPPORT_ENCRYPT
& typemask(type
))) {
506 remote_supports_decrypt
|= typemask(type
);
512 ep
= findencryption(use_type
);
515 type
= ep
->start
? (*ep
->start
)(DIR_ENCRYPT
, Server
) : 0;
516 if (encrypt_debug_mode
)
517 printf(">>>%s: (*ep->start)() returned %d\r\n",
521 encrypt_mode
= use_type
;
523 encrypt_start_output(use_type
);
528 encrypt_is(unsigned char *data
, int cnt
)
536 if (type
< ENCTYPE_CNT
)
537 remote_supports_encrypt
|= typemask(type
);
538 if (!(ep
= finddecryption(type
))) {
539 if (encrypt_debug_mode
)
540 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
542 ENCTYPE_NAME_OK(type
)
543 ? ENCTYPE_NAME(type
) : "(unknown)",
548 if (encrypt_debug_mode
)
549 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
551 ENCTYPE_NAME_OK(type
)
552 ? ENCTYPE_NAME(type
) : "(unknown)",
556 ret
= (*ep
->is
)(data
, cnt
);
557 if (encrypt_debug_mode
)
558 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data
, cnt
,
559 (ret
< 0) ? "FAIL " :
560 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
566 if (ret
== 0 && autodecrypt
)
567 encrypt_send_request_start();
572 encrypt_reply(unsigned char *data
, int cnt
)
580 if (!(ep
= findencryption(type
))) {
581 if (encrypt_debug_mode
)
582 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
584 ENCTYPE_NAME_OK(type
)
585 ? ENCTYPE_NAME(type
) : "(unknown)",
590 if (encrypt_debug_mode
)
591 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
593 ENCTYPE_NAME_OK(type
)
594 ? ENCTYPE_NAME(type
) : "(unknown)",
598 ret
= (*ep
->reply
)(data
, cnt
);
599 if (encrypt_debug_mode
)
600 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
602 (ret
< 0) ? "FAIL " :
603 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
605 if (encrypt_debug_mode
)
606 printf(">>>%s: encrypt_reply returned %d\n", Name
, ret
);
611 if (ret
== 0 && autoencrypt
)
612 encrypt_start_output(type
);
617 * Called when a ENCRYPT START command is received.
620 encrypt_start(unsigned char *data __unused
, int cnt __unused
)
626 * Something is wrong. We should not get a START
627 * command without having already picked our
628 * decryption scheme. Send a REQUEST-END to
629 * attempt to clear the channel...
631 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name
);
632 encrypt_send_request_end();
636 if ((ep
= finddecryption(decrypt_mode
))) {
637 decrypt_input
= ep
->input
;
639 printf("[ Input is now decrypted with type %s ]\r\n",
640 ENCTYPE_NAME(decrypt_mode
));
641 if (encrypt_debug_mode
)
642 printf(">>>%s: Start to decrypt input with type %s\r\n",
643 Name
, ENCTYPE_NAME(decrypt_mode
));
645 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
647 ENCTYPE_NAME_OK(decrypt_mode
)
648 ? ENCTYPE_NAME(decrypt_mode
)
651 encrypt_send_request_end();
656 encrypt_session_key( Session_Key
*key
, int server
)
658 Encryptions
*ep
= encryptions
;
664 (*ep
->session
)(key
, server
);
670 * Called when ENCRYPT END is received.
676 if (encrypt_debug_mode
)
677 printf(">>>%s: Input is back to clear text\r\n", Name
);
679 printf("[ Input is now clear text ]\r\n");
683 * Called when ENCRYPT REQUEST-END is received.
686 encrypt_request_end(void)
692 * Called when ENCRYPT REQUEST-START is received. If we receive
693 * this before a type is picked, then that indicates that the
694 * other side wants us to start encrypting data as soon as we
698 encrypt_request_start(unsigned char *data __unused
, int cnt __unused
)
700 if (encrypt_mode
== 0) {
705 encrypt_start_output(encrypt_mode
);
708 static unsigned char str_keyid
[(MAXKEYLEN
*2)+5] = { IAC
, SB
, TELOPT_ENCRYPT
};
711 encrypt_enc_keyid(unsigned char *keyid
, int len
)
713 encrypt_keyid(&ki
[1], keyid
, len
);
717 encrypt_dec_keyid(unsigned char *keyid
, int len
)
719 encrypt_keyid(&ki
[0], keyid
, len
);
723 encrypt_keyid(struct key_info
*kp
, unsigned char *keyid
, int len
)
732 if (!(ep
= (*kp
->getcrypt
)(*kp
->modep
))) {
736 } else if (len
== 0) {
738 * Empty option, indicates a failure.
744 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
746 } else if ((len
!= kp
->keylen
) ||
747 (memcmp(keyid
, kp
->keyid
, len
) != 0)) {
749 * Length or contents are different
752 memmove(kp
->keyid
, keyid
, len
);
754 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
757 ret
= (*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
758 if ((ret
== 0) && (dir
== DIR_ENCRYPT
) && autoencrypt
)
759 encrypt_start_output(*kp
->modep
);
763 encrypt_send_keyid(dir
, kp
->keyid
, kp
->keylen
, 0);
767 encrypt_send_keyid(int dir
, const char *keyid
, int keylen
, int saveit
)
771 str_keyid
[3] = (dir
== DIR_ENCRYPT
)
772 ? ENCRYPT_ENC_KEYID
: ENCRYPT_DEC_KEYID
;
774 struct key_info
*kp
= &ki
[(dir
== DIR_ENCRYPT
) ? 0 : 1];
775 memmove(kp
->keyid
, keyid
, keylen
);
779 for (strp
= &str_keyid
[4]; keylen
> 0; --keylen
) {
780 if ((*strp
++ = *keyid
++) == IAC
)
785 net_write(str_keyid
, strp
- str_keyid
);
786 printsub('>', &str_keyid
[2], strp
- str_keyid
- 2);
795 autoencrypt
= on
? 1 : 0;
804 autodecrypt
= on
? 1 : 0;
808 encrypt_start_output(int type
)
814 if (!(ep
= findencryption(type
))) {
815 if (encrypt_debug_mode
) {
816 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
818 ENCTYPE_NAME_OK(type
)
819 ? ENCTYPE_NAME(type
) : "(unknown)",
825 i
= (*ep
->start
)(DIR_ENCRYPT
, Server
);
826 if (encrypt_debug_mode
) {
827 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
830 "initial negotiation in progress",
831 i
, ENCTYPE_NAME(type
));
837 *p
++ = ENCRYPT_START
;
838 for (i
= 0; i
< ki
[0].keylen
; ++i
) {
839 if ((*p
++ = ki
[0].keyid
[i
]) == IAC
)
844 net_write(str_start
, p
- str_start
);
846 printsub('>', &str_start
[2], p
- &str_start
[2]);
848 * If we are already encrypting in some mode, then
849 * encrypt the ring (which includes our request) in
850 * the old mode, mark it all as "clear text" and then
851 * switch to the new mode.
853 encrypt_output
= ep
->output
;
855 if (encrypt_debug_mode
)
856 printf(">>>%s: Started to encrypt output with type %s\r\n",
857 Name
, ENCTYPE_NAME(type
));
859 printf("[ Output is now encrypted with type %s ]\r\n",
864 encrypt_send_end(void)
869 str_end
[3] = ENCRYPT_END
;
870 net_write(str_end
, sizeof(str_end
));
872 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
874 * Encrypt the output buffer now because it will not be done by
878 if (encrypt_debug_mode
)
879 printf(">>>%s: Output is back to clear text\r\n", Name
);
881 printf("[ Output is now clear text ]\r\n");
885 encrypt_send_request_start(void)
891 *p
++ = ENCRYPT_REQSTART
;
892 for (i
= 0; i
< ki
[1].keylen
; ++i
) {
893 if ((*p
++ = ki
[1].keyid
[i
]) == IAC
)
898 net_write(str_start
, p
- str_start
);
899 printsub('>', &str_start
[2], p
- &str_start
[2]);
900 if (encrypt_debug_mode
)
901 printf(">>>%s: Request input to be encrypted\r\n", Name
);
905 encrypt_send_request_end(void)
907 str_end
[3] = ENCRYPT_REQEND
;
908 net_write(str_end
, sizeof(str_end
));
909 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
911 if (encrypt_debug_mode
)
912 printf(">>>%s: Request input to be clear text\r\n", Name
);
918 if (encrypt_debug_mode
)
919 printf(">>>%s: in encrypt_wait\r\n", Name
);
920 if (!havesessionkey
|| !(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
))
922 while (autoencrypt
&& !encrypt_output
)
928 encrypt_gen_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
934 buf
[buflen
-1] = '\0';
937 for (; cnt
> 0; cnt
--, data
++) {
938 sprintf(tbuf
, " %d", *data
);
939 for (cp
= tbuf
; *cp
&& buflen
> 0; --buflen
)
948 encrypt_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
953 for (ep
= encryptions
; ep
->type
&& ep
->type
!= type
; ep
++)
957 (*ep
->printsub
)(data
, cnt
, buf
, buflen
);
959 encrypt_gen_printsub(data
, cnt
, buf
, buflen
);
961 #endif /* ENCRYPTION */