source: trunk/src/testing/app/vnc/vncauth.cpp @ 4

Revision 4, 3.7 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/*
2 *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
3 *
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.
8 *
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.
13 *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17 *  USA.
18 */
19
20/*
21 * vncauth.c - Functions for VNC password management and authentication.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#if defined(WIN32)
29#include <io.h>
30#endif
31
32#include <time.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35
36#include "vncauth.h"
37#include "d3des.h"
38
39
40/*
41 * We use a fixed key to store passwords, since we assume that our local
42 * file system is secure but nonetheless don't want to store passwords
43 * as plaintext.
44 */
45
46unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
47
48
49/*
50 * Encrypt a password and store it in a file.  Returns 0 if successful,
51 * 1 if the file could not be written.
52 */
53
54int
55vncEncryptAndStorePasswd(char *passwd, char *fname)
56{
57    FILE *fp;
58    int i;
59    unsigned char encryptedPasswd[8];
60
61    if ((fp = fopen(fname,"w")) == NULL) return 1;
62
63#if defined(WIN32)
64    _chmod(fname, _S_IREAD | _S_IWRITE);
65#else
66    chmod(fname, S_IRUSR|S_IWUSR);
67#endif
68
69    /* pad password with nulls */
70
71    for (i = 0; i < 8; i++) {
72                if (i < (int)strlen(passwd)) {
73                        encryptedPasswd[i] = passwd[i];
74                } else {
75                        encryptedPasswd[i] = 0;
76                }
77    }
78
79    /* Do encryption in-place - this way we overwrite our copy of the plaintext
80       password */
81
82    deskey(fixedkey, EN0);
83    des(encryptedPasswd, encryptedPasswd);
84
85    for (i = 0; i < 8; i++) {
86        putc(encryptedPasswd[i], fp);
87    }
88 
89    fclose(fp);
90    return 0;
91}
92
93
94/*
95 * Decrypt a password from a file.  Returns a pointer to a newly allocated
96 * string containing the password or a null pointer if the password could
97 * not be retrieved for some reason.
98 */
99
100char *
101vncDecryptPasswdFromFile(char *fname)
102{
103    FILE *fp;
104    int i, ch;
105    unsigned char *passwd = (unsigned char *)malloc(9);
106
107    if ((fp = fopen(fname,"r")) == NULL) return NULL;
108
109    for (i = 0; i < 8; i++) {
110        ch = getc(fp);
111        if (ch == EOF) {
112            fclose(fp);
113            return NULL;
114        }
115        passwd[i] = ch;
116    }
117
118    fclose(fp);
119
120    deskey(fixedkey, DE1);
121    des(passwd, passwd);
122
123    passwd[8] = 0;
124
125    return (char *)passwd;
126}
127
128
129/*
130 * Generate CHALLENGESIZE random bytes for use in challenge-response
131 * authentication.
132 */
133
134void
135vncRandomBytes(unsigned char *bytes)
136{
137    int i;
138    unsigned int seed = (unsigned int) time(0);
139
140#if defined(WIN32)
141    srand(seed);
142#else
143    srandom(seed);
144#endif
145    for (i = 0; i < CHALLENGESIZE; i++) {
146#if defined(WIN32)
147                bytes[i] = (unsigned char)(rand() & 255);   
148#else
149                bytes[i] = (unsigned char)(random() & 255);   
150#endif
151    }
152}
153
154
155/*
156 * Encrypt CHALLENGESIZE bytes in memory using a password.
157 */
158
159void
160vncEncryptBytes(unsigned char *bytes, char *passwd)
161{
162    unsigned char key[8];
163    int i;
164
165    /* key is simply password padded with nulls */
166
167    for (i = 0; i < 8; i++) {
168                if (i < (int)strlen(passwd)) {
169                        key[i] = passwd[i];
170                } else {
171                        key[i] = 0;
172                }
173    }
174
175    deskey(key, EN0);
176
177    for (i = 0; i < CHALLENGESIZE; i += 8) {
178        des(bytes+i, bytes+i);
179    }
180}
Note: See TracBrowser for help on using the repository browser.