1 6e527fbc 2005-02-13 devnull #include <u.h>
2 6e527fbc 2005-02-13 devnull #include <libc.h>
3 6e527fbc 2005-02-13 devnull #include <mp.h>
4 6e527fbc 2005-02-13 devnull #include <libsec.h>
5 6e527fbc 2005-02-13 devnull #include "SConn.h"
7 6e527fbc 2005-02-13 devnull extern int verbose;
9 6e527fbc 2005-02-13 devnull typedef struct ConnState {
10 6e527fbc 2005-02-13 devnull uchar secret[SHA1dlen];
11 6e527fbc 2005-02-13 devnull ulong seqno;
12 6e527fbc 2005-02-13 devnull RC4state rc4;
13 6e527fbc 2005-02-13 devnull } ConnState;
16 112a2c58 2008-12-02 trisk typedef struct SS {
17 cbeb0b26 2006-04-01 devnull int fd; /* file descriptor for read/write of encrypted data */
18 cbeb0b26 2006-04-01 devnull int alg; /* if nonzero, "alg sha rc4_128" */
19 6e527fbc 2005-02-13 devnull ConnState in, out;
22 6e527fbc 2005-02-13 devnull static int
23 6e527fbc 2005-02-13 devnull SC_secret(SConn *conn, uchar *sigma, int direction)
25 6e527fbc 2005-02-13 devnull SS *ss = (SS*)(conn->chan);
26 6e527fbc 2005-02-13 devnull int nsigma = conn->secretlen;
28 6e527fbc 2005-02-13 devnull if(direction != 0){
29 6e527fbc 2005-02-13 devnull hmac_sha1(sigma, nsigma, (uchar*)"one", 3, ss->out.secret, nil);
30 6e527fbc 2005-02-13 devnull hmac_sha1(sigma, nsigma, (uchar*)"two", 3, ss->in.secret, nil);
32 6e527fbc 2005-02-13 devnull hmac_sha1(sigma, nsigma, (uchar*)"two", 3, ss->out.secret, nil);
33 6e527fbc 2005-02-13 devnull hmac_sha1(sigma, nsigma, (uchar*)"one", 3, ss->in.secret, nil);
35 cbeb0b26 2006-04-01 devnull setupRC4state(&ss->in.rc4, ss->in.secret, 16); /* restrict to 128 bits */
36 6e527fbc 2005-02-13 devnull setupRC4state(&ss->out.rc4, ss->out.secret, 16);
37 6e527fbc 2005-02-13 devnull ss->alg = 1;
38 6e527fbc 2005-02-13 devnull return 0;
41 6e527fbc 2005-02-13 devnull static void
42 6e527fbc 2005-02-13 devnull hash(uchar secret[SHA1dlen], uchar *data, int len, int seqno, uchar d[SHA1dlen])
44 6e527fbc 2005-02-13 devnull DigestState sha;
45 6e527fbc 2005-02-13 devnull uchar seq[4];
47 6e527fbc 2005-02-13 devnull seq[0] = seqno>>24;
48 6e527fbc 2005-02-13 devnull seq[1] = seqno>>16;
49 6e527fbc 2005-02-13 devnull seq[2] = seqno>>8;
50 6e527fbc 2005-02-13 devnull seq[3] = seqno;
51 6e527fbc 2005-02-13 devnull memset(&sha, 0, sizeof sha);
52 6e527fbc 2005-02-13 devnull sha1(secret, SHA1dlen, nil, &sha);
53 6e527fbc 2005-02-13 devnull sha1(data, len, nil, &sha);
54 6e527fbc 2005-02-13 devnull sha1(seq, 4, d, &sha);
57 6e527fbc 2005-02-13 devnull static int
58 6e527fbc 2005-02-13 devnull verify(uchar secret[SHA1dlen], uchar *data, int len, int seqno, uchar d[SHA1dlen])
60 6e527fbc 2005-02-13 devnull DigestState sha;
61 6e527fbc 2005-02-13 devnull uchar seq[4];
62 6e527fbc 2005-02-13 devnull uchar digest[SHA1dlen];
64 6e527fbc 2005-02-13 devnull seq[0] = seqno>>24;
65 6e527fbc 2005-02-13 devnull seq[1] = seqno>>16;
66 6e527fbc 2005-02-13 devnull seq[2] = seqno>>8;
67 6e527fbc 2005-02-13 devnull seq[3] = seqno;
68 6e527fbc 2005-02-13 devnull memset(&sha, 0, sizeof sha);
69 6e527fbc 2005-02-13 devnull sha1(secret, SHA1dlen, nil, &sha);
70 6e527fbc 2005-02-13 devnull sha1(data, len, nil, &sha);
71 6e527fbc 2005-02-13 devnull sha1(seq, 4, digest, &sha);
72 6e527fbc 2005-02-13 devnull return memcmp(d, digest, SHA1dlen);
75 6e527fbc 2005-02-13 devnull static int
76 6e527fbc 2005-02-13 devnull SC_read(SConn *conn, uchar *buf, int n)
78 6e527fbc 2005-02-13 devnull SS *ss = (SS*)(conn->chan);
79 6e527fbc 2005-02-13 devnull uchar count[2], digest[SHA1dlen];
80 6e527fbc 2005-02-13 devnull int len, nr;
82 6e527fbc 2005-02-13 devnull if(read(ss->fd, count, 2) != 2 || (count[0]&0x80) == 0){
83 6e527fbc 2005-02-13 devnull snprint((char*)buf,n,"!SC_read invalid count");
84 6e527fbc 2005-02-13 devnull return -1;
86 cbeb0b26 2006-04-01 devnull len = (count[0]&0x7f)<<8 | count[1]; /* SSL-style count; no pad */
87 6e527fbc 2005-02-13 devnull if(ss->alg){
88 6e527fbc 2005-02-13 devnull len -= SHA1dlen;
89 6e527fbc 2005-02-13 devnull if(len <= 0 || readn(ss->fd, digest, SHA1dlen) != SHA1dlen){
90 6e527fbc 2005-02-13 devnull snprint((char*)buf,n,"!SC_read missing sha1");
91 6e527fbc 2005-02-13 devnull return -1;
93 6e527fbc 2005-02-13 devnull if(len > n || readn(ss->fd, buf, len) != len){
94 6e527fbc 2005-02-13 devnull snprint((char*)buf,n,"!SC_read missing data");
95 6e527fbc 2005-02-13 devnull return -1;
97 6e527fbc 2005-02-13 devnull rc4(&ss->in.rc4, digest, SHA1dlen);
98 6e527fbc 2005-02-13 devnull rc4(&ss->in.rc4, buf, len);
99 6e527fbc 2005-02-13 devnull if(verify(ss->in.secret, buf, len, ss->in.seqno, digest) != 0){
100 6e527fbc 2005-02-13 devnull snprint((char*)buf,n,"!SC_read integrity check failed");
101 6e527fbc 2005-02-13 devnull return -1;
104 6e527fbc 2005-02-13 devnull if(len <= 0 || len > n){
105 6e527fbc 2005-02-13 devnull snprint((char*)buf,n,"!SC_read implausible record length");
106 6e527fbc 2005-02-13 devnull return -1;
108 6e527fbc 2005-02-13 devnull if( (nr = readn(ss->fd, buf, len)) != len){
109 6e527fbc 2005-02-13 devnull snprint((char*)buf,n,"!SC_read expected %d bytes, but got %d", len, nr);
110 6e527fbc 2005-02-13 devnull return -1;
113 6e527fbc 2005-02-13 devnull ss->in.seqno++;
114 6e527fbc 2005-02-13 devnull return len;
117 6e527fbc 2005-02-13 devnull static int
118 6e527fbc 2005-02-13 devnull SC_write(SConn *conn, uchar *buf, int n)
120 6e527fbc 2005-02-13 devnull SS *ss = (SS*)(conn->chan);
121 6e527fbc 2005-02-13 devnull uchar count[2], digest[SHA1dlen], enc[Maxmsg+1];
122 6e527fbc 2005-02-13 devnull int len;
124 6e527fbc 2005-02-13 devnull if(n <= 0 || n > Maxmsg+1){
125 6e527fbc 2005-02-13 devnull werrstr("!SC_write invalid n %d", n);
126 6e527fbc 2005-02-13 devnull return -1;
128 6e527fbc 2005-02-13 devnull len = n;
129 6e527fbc 2005-02-13 devnull if(ss->alg)
130 6e527fbc 2005-02-13 devnull len += SHA1dlen;
131 6e527fbc 2005-02-13 devnull count[0] = 0x80 | len>>8;
132 6e527fbc 2005-02-13 devnull count[1] = len;
133 6e527fbc 2005-02-13 devnull if(write(ss->fd, count, 2) != 2){
134 6e527fbc 2005-02-13 devnull werrstr("!SC_write invalid count");
135 6e527fbc 2005-02-13 devnull return -1;
137 6e527fbc 2005-02-13 devnull if(ss->alg){
138 6e527fbc 2005-02-13 devnull hash(ss->out.secret, buf, n, ss->out.seqno, digest);
139 6e527fbc 2005-02-13 devnull rc4(&ss->out.rc4, digest, SHA1dlen);
140 6e527fbc 2005-02-13 devnull memcpy(enc, buf, n);
141 6e527fbc 2005-02-13 devnull rc4(&ss->out.rc4, enc, n);
142 6e527fbc 2005-02-13 devnull if(write(ss->fd, digest, SHA1dlen) != SHA1dlen ||
143 6e527fbc 2005-02-13 devnull write(ss->fd, enc, n) != n){
144 6e527fbc 2005-02-13 devnull werrstr("!SC_write error on send");
145 6e527fbc 2005-02-13 devnull return -1;
148 6e527fbc 2005-02-13 devnull if(write(ss->fd, buf, n) != n){
149 6e527fbc 2005-02-13 devnull werrstr("!SC_write error on send");
150 6e527fbc 2005-02-13 devnull return -1;
153 6e527fbc 2005-02-13 devnull ss->out.seqno++;
154 6e527fbc 2005-02-13 devnull return n;
157 6e527fbc 2005-02-13 devnull static void
158 6e527fbc 2005-02-13 devnull SC_free(SConn *conn)
160 6e527fbc 2005-02-13 devnull SS *ss = (SS*)(conn->chan);
162 6e527fbc 2005-02-13 devnull close(ss->fd);
163 6e527fbc 2005-02-13 devnull free(ss);
164 6e527fbc 2005-02-13 devnull free(conn);
168 6e527fbc 2005-02-13 devnull newSConn(int fd)
171 6e527fbc 2005-02-13 devnull SConn *conn;
173 6e527fbc 2005-02-13 devnull if(fd < 0)
174 6e527fbc 2005-02-13 devnull return nil;
175 6e527fbc 2005-02-13 devnull ss = (SS*)emalloc(sizeof(*ss));
176 6e527fbc 2005-02-13 devnull conn = (SConn*)emalloc(sizeof(*conn));
177 6e527fbc 2005-02-13 devnull ss->fd = fd;
178 6e527fbc 2005-02-13 devnull ss->alg = 0;
179 6e527fbc 2005-02-13 devnull conn->chan = (void*)ss;
180 6e527fbc 2005-02-13 devnull conn->secretlen = SHA1dlen;
181 6e527fbc 2005-02-13 devnull conn->free = SC_free;
182 6e527fbc 2005-02-13 devnull conn->secret = SC_secret;
183 6e527fbc 2005-02-13 devnull conn->read = SC_read;
184 6e527fbc 2005-02-13 devnull conn->write = SC_write;
185 6e527fbc 2005-02-13 devnull return conn;
189 6e527fbc 2005-02-13 devnull writerr(SConn *conn, char *s)
191 6e527fbc 2005-02-13 devnull char buf[Maxmsg];
193 6e527fbc 2005-02-13 devnull snprint(buf, Maxmsg, "!%s", s);
194 6e527fbc 2005-02-13 devnull conn->write(conn, (uchar*)buf, strlen(buf));
198 6e527fbc 2005-02-13 devnull readstr(SConn *conn, char *s)
202 6e527fbc 2005-02-13 devnull n = conn->read(conn, (uchar*)s, Maxmsg);
203 6e527fbc 2005-02-13 devnull if(n >= 0){
204 6e527fbc 2005-02-13 devnull s[n] = 0;
205 6e527fbc 2005-02-13 devnull if(s[0] == '!'){
206 6e527fbc 2005-02-13 devnull memmove(s, s+1, n);
210 6e527fbc 2005-02-13 devnull strcpy(s, "read error");
212 6e527fbc 2005-02-13 devnull return n;