Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
#include <ctype.h>
4
#include <authsrv.h>
5
#include <mp.h>
6
#include <libsec.h>
7
#include <bio.h>
8
#include "authcmdlib.h"
9
 
10
char	authkey[DESKEYLEN];
11
int	verb;
12
int	usepass;
13
 
14
int	convert(char*, char*, int);
15
int	dofcrypt(int, char*, char*, int);
16
void	usage(void);
17
 
18
void
19
main(int argc, char *argv[])
20
{
21
	Dir *d;
22
	char *p, *file, key[DESKEYLEN];
23
	int fd, len;
24
 
25
	ARGBEGIN{
26
	case 'p':
27
		usepass = 1;
28
		break;
29
	case 'v':
30
		verb = 1;
31
		break;
32
	default:
33
		usage();
34
	}ARGEND
35
 
36
	if(argc != 1)
37
		usage();
38
	file = argv[0];
39
 
40
	/* get original key */
41
	if(usepass){
42
		print("enter password file is encoded with\n");
43
		getpass(authkey, nil, 0, 1);
44
	} else
45
		getauthkey(authkey);
46
	if(!verb){
47
		print("enter password to reencode with\n");
48
		getpass(key, nil, 0, 1);
49
	}
50
 
51
	fd = open(file, ORDWR);
52
	if(fd < 0)
53
		error("can't open %s: %r\n", file);
54
	d = dirfstat(fd);
55
	if(d == nil)
56
		error("can't stat %s: %r\n", file);
57
	len = d->length;
58
	p = malloc(len);
59
	if(!p)
60
		error("out of memory");
61
	if(read(fd, p, len) != len)
62
		error("can't read key file: %r\n");
63
	len = convert(p, key, len);
64
	if(verb)
65
		exits(0);
66
	if(pwrite(fd, p, len, 0) != len)
67
		error("can't write key file: %r\n");
68
	close(fd);
69
	exits(0);
70
}
71
 
72
void
73
randombytes(uchar *p, int len)
74
{
75
	int i, fd;
76
 
77
	fd = open("/dev/random", OREAD);
78
	if(fd < 0){
79
		fprint(2, "convkeys: can't open /dev/random, using rand()\n");
80
		srand(time(0));
81
		for(i = 0; i < len; i++)
82
			p[i] = rand();
83
		return;
84
	}
85
	read(fd, p, len);
86
	close(fd);
87
}
88
 
89
void
90
oldCBCencrypt(char *key7, char *p, int len)
91
{
92
	uchar ivec[8];
93
	uchar key[8];
94
	DESstate s;
95
 
96
	memset(ivec, 0, 8);
97
	des56to64((uchar*)key7, key);
98
	setupDESstate(&s, key, ivec);
99
	desCBCencrypt((uchar*)p, len, &s);
100
}
101
 
102
void
103
oldCBCdecrypt(char *key7, char *p, int len)
104
{
105
	uchar ivec[8];
106
	uchar key[8];
107
	DESstate s;
108
 
109
	memset(ivec, 0, 8);
110
	des56to64((uchar*)key7, key);
111
	setupDESstate(&s, key, ivec);
112
	desCBCdecrypt((uchar*)p, len, &s);
113
 
114
}
115
 
116
static int
117
badname(char *s)
118
{
119
	int n;
120
	Rune r;
121
 
122
	for (; *s != '\0'; s += n) {
123
		n = chartorune(&r, s);
124
		if (n == 1 && r == Runeerror)
125
			return 1;
126
	}
127
	return 0;
128
}
129
 
130
int
131
convert(char *p, char *key, int len)
132
{
133
	int i;
134
 
135
	len -= KEYDBOFF;
136
	if(len % KEYDBLEN){
137
		fprint(2, "convkeys: file odd length; not converting %d bytes\n",
138
			len % KEYDBLEN);
139
		len -= len % KEYDBLEN;
140
	}
141
	len += KEYDBOFF;
142
	oldCBCdecrypt(authkey, p, len);
143
	for(i = KEYDBOFF; i < len; i += KEYDBLEN)
144
		if (badname(&p[i])) {
145
			print("bad name %.30s... - aborting\n", &p[i]);
146
			return 0;
147
		}
148
	if(verb)
149
		for(i = KEYDBOFF; i < len; i += KEYDBLEN)
150
			print("%s\n", &p[i]);
151
 
152
	randombytes((uchar*)p, 8);
153
	oldCBCencrypt(key, p, len);
154
	return len;
155
}
156
 
157
void
158
usage(void)
159
{
160
	fprint(2, "usage: convkeys keyfile\n");
161
	exits("usage");
162
}