merge in my changes from soc-krdc branch
[kdenetwork.git] / krdc / vnc / vncauth.c
blob58683f3e62ef197b50b63f86eb8519e801c389ce
1 /*
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,
17 * USA.
21 * vncauth.c - Functions for VNC password management and authentication.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <vncauth.h>
30 #include <d3des.h>
32 #ifdef __GNUC__
33 #warning If we keep this, it needs to be cleaned up
34 #endif
35 #include <time.h>
36 #include <sys/time.h>
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
41 * as plaintext.
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.
52 int
53 vncEncryptAndStorePasswd(char *passwd, char *fname)
55 FILE *fp;
56 int i;
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];
68 } else {
69 encryptedPasswd[i] = 0;
73 /* Do encryption in-place - this way we overwrite our copy of the plaintext
74 password */
76 deskey(fixedkey, EN0);
77 des(encryptedPasswd, encryptedPasswd);
79 for (i = 0; i < 8; i++) {
80 putc(encryptedPasswd[i], fp);
83 fclose(fp);
84 return 0;
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.
94 char *
95 vncDecryptPasswdFromFile(char *fname)
97 FILE *fp;
98 int i, ch;
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++) {
106 ch = getc(fp);
107 if (ch == EOF) {
108 fclose(fp);
109 return NULL;
111 passwd[i] = ch;
114 fclose(fp);
116 deskey(fixedkey, DE1);
117 des(passwd, passwd);
119 passwd[8] = 0;
121 return (char *)passwd;
126 * Generate CHALLENGESIZE random bytes for use in challenge-response
127 * authentication.
130 void
131 vncRandomBytes(unsigned char *bytes)
133 int i;
134 unsigned int seed = (unsigned int) time(0);
136 srandom(seed);
137 for (i = 0; i < CHALLENGESIZE; i++) {
138 bytes[i] = (unsigned char)(random() & 255);
144 * Encrypt CHALLENGESIZE bytes in memory using a password.
147 void
148 vncEncryptBytes(unsigned char *bytes, char *passwd)
150 unsigned char key[8];
151 int i;
153 /* key is simply password padded with nulls */
155 for (i = 0; i < 8; i++) {
156 if (i < strlen(passwd)) {
157 key[i] = passwd[i];
158 } else {
159 key[i] = 0;
163 deskey(key, EN0);
165 for (i = 0; i < CHALLENGESIZE; i += 8) {
166 des(bytes+i, bytes+i);