summaryrefslogtreecommitdiff
path: root/noncore/comm/keypebble/vncauth.c
Unidiff
Diffstat (limited to 'noncore/comm/keypebble/vncauth.c') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/comm/keypebble/vncauth.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/noncore/comm/keypebble/vncauth.c b/noncore/comm/keypebble/vncauth.c
new file mode 100644
index 0000000..dc276bf
--- a/dev/null
+++ b/noncore/comm/keypebble/vncauth.c
@@ -0,0 +1,160 @@
1/*
2 * Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
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#include <sys/types.h>
28#include <sys/stat.h>
29#include <time.h>
30#include "vncauth.h"
31#include "d3des.h"
32
33
34/*
35 * We use a fixed key to store passwords, since we assume that our local
36 * file system is secure but nonetheless don't want to store passwords
37 * as plaintext.
38 */
39
40unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
41
42
43/*
44 * Encrypt a password and store it in a file. Returns 0 if successful,
45 * 1 if the file could not be written.
46 */
47
48int
49vncEncryptAndStorePasswd(char *passwd, char *fname)
50{
51 FILE *fp;
52 int i;
53 unsigned char encryptedPasswd[8];
54
55 if ((fp = fopen(fname,"w")) == NULL) return 1;
56
57 chmod(fname, S_IRUSR|S_IWUSR);
58
59 /* pad password with nulls */
60
61 for (i = 0; i < 8; i++) {
62 if (i < strlen(passwd)) {
63 encryptedPasswd[i] = passwd[i];
64 } else {
65 encryptedPasswd[i] = 0;
66 }
67 }
68
69 /* Do encryption in-place - this way we overwrite our copy of the plaintext
70 password */
71
72 deskey(fixedkey, EN0);
73 des(encryptedPasswd, encryptedPasswd);
74
75 for (i = 0; i < 8; i++) {
76 putc(encryptedPasswd[i], fp);
77 }
78
79 fclose(fp);
80 return 0;
81}
82
83
84/*
85 * Decrypt a password from a file. Returns a pointer to a newly allocated
86 * string containing the password or a null pointer if the password could
87 * not be retrieved for some reason.
88 */
89
90char *
91vncDecryptPasswdFromFile(char *fname)
92{
93 FILE *fp;
94 int i, ch;
95 unsigned char *passwd = (unsigned char *)malloc(9);
96
97 if ((fp = fopen(fname,"r")) == NULL) return NULL;
98
99 for (i = 0; i < 8; i++) {
100 ch = getc(fp);
101 if (ch == EOF) {
102 fclose(fp);
103 return NULL;
104 }
105 passwd[i] = ch;
106 }
107
108 deskey(fixedkey, DE1);
109 des(passwd, passwd);
110
111 passwd[8] = 0;
112
113 return (char *)passwd;
114}
115
116
117/*
118 * Generate CHALLENGESIZE random bytes for use in challenge-response
119 * authentication.
120 */
121
122void
123vncRandomBytes(unsigned char *bytes)
124{
125 int i;
126 unsigned int seed = (unsigned int) time(0);
127
128 srandom(seed);
129 for (i = 0; i < CHALLENGESIZE; i++) {
130 bytes[i] = (unsigned char)(random() & 255);
131 }
132}
133
134
135/*
136 * Encrypt CHALLENGESIZE bytes in memory using a password.
137 */
138
139void
140vncEncryptBytes(unsigned char *bytes, char *passwd)
141{
142 unsigned char key[8];
143 int i;
144
145 /* key is simply password padded with nulls */
146
147 for (i = 0; i < 8; i++) {
148 if (i < strlen(passwd)) {
149 key[i] = passwd[i];
150 } else {
151 key[i] = 0;
152 }
153 }
154
155 deskey(key, EN0);
156
157 for (i = 0; i < CHALLENGESIZE; i += 8) {
158 des(bytes+i, bytes+i);
159 }
160}