2 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * vncauth.c - Functions for VNC password management and authentication.
27 #include <sys/types.h>
33 #warning If we keep this, it needs to be cleaned up
39 * We use a fixed key to store passwords, since we assume that our local
40 * file system is secure but nonetheless don't want to store passwords
44 unsigned char fixedkey
[8] = {23,82,107,6,35,78,88,7};
48 * Encrypt a password and store it in a file. Returns 0 if successful,
49 * 1 if the file could not be written.
53 vncEncryptAndStorePasswd(char *passwd
, char *fname
)
57 unsigned char encryptedPasswd
[8];
59 if ((fp
= fopen(fname
,"w")) == NULL
) return 1;
61 chmod(fname
, S_IRUSR
|S_IWUSR
);
63 /* pad password with nulls */
65 for (i
= 0; i
< 8; i
++) {
66 if (i
< strlen(passwd
)) {
67 encryptedPasswd
[i
] = passwd
[i
];
69 encryptedPasswd
[i
] = 0;
73 /* Do encryption in-place - this way we overwrite our copy of the plaintext
76 deskey(fixedkey
, EN0
);
77 des(encryptedPasswd
, encryptedPasswd
);
79 for (i
= 0; i
< 8; i
++) {
80 putc(encryptedPasswd
[i
], fp
);
89 * Decrypt a password from a file. Returns a pointer to a newly allocated
90 * string containing the password or a null pointer if the password could
91 * not be retrieved for some reason.
95 vncDecryptPasswdFromFile(char *fname
)
99 unsigned char *passwd
;
101 if ((fp
= fopen(fname
,"r")) == NULL
) return NULL
;
103 passwd
= (unsigned char *)malloc(9);
105 for (i
= 0; i
< 8; i
++) {
116 deskey(fixedkey
, DE1
);
121 return (char *)passwd
;
126 * Generate CHALLENGESIZE random bytes for use in challenge-response
131 vncRandomBytes(unsigned char *bytes
)
134 unsigned int seed
= (unsigned int) time(0);
137 for (i
= 0; i
< CHALLENGESIZE
; i
++) {
138 bytes
[i
] = (unsigned char)(random() & 255);
144 * Encrypt CHALLENGESIZE bytes in memory using a password.
148 vncEncryptBytes(unsigned char *bytes
, char *passwd
)
150 unsigned char key
[8];
153 /* key is simply password padded with nulls */
155 for (i
= 0; i
< 8; i
++) {
156 if (i
< strlen(passwd
)) {
165 for (i
= 0; i
< CHALLENGESIZE
; i
+= 8) {
166 des(bytes
+i
, bytes
+i
);