1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
implement Authproto;
# currently includes p9sk1
include "sys.m";
sys: Sys;
Rread, Rwrite: import Sys;
include "draw.m";
include "keyring.m";
kr: Keyring;
include "auth9.m";
auth9: Auth9;
ANAMELEN, AERRLEN, DOMLEN, DESKEYLEN, CHALLEN, SECRETLEN: import Auth9;
TICKREQLEN, TICKETLEN, AUTHENTLEN: import Auth9;
Ticketreq, Ticket, Authenticator: import auth9;
include "../authio.m";
authio: Authio;
Aattr, Aval, Aquery: import Authio;
Attr, IO, Key, Authinfo: import authio;
netmkaddr, eqbytes, memrandom: import authio;
include "encoding.m";
base16: Encoding;
Debug: con 0;
# init, addkey, closekey, write, read, close, keyprompt
init(f: Authio): string
{
authio = f;
sys = load Sys Sys->PATH;
kr = load Keyring Keyring->PATH;
auth9 = load Auth9 Auth9->PATH;
auth9->init();
base16 = load Encoding Encoding->BASE16PATH;
return nil;
}
version := 1;
interaction(attrs: list of ref Attr, io: ref IO): string
{
return p9any(io);
}
p9any(io: ref IO): string
{
while((buf := io.read()) == nil || (n := len buf) == 0 || buf[n-1] != byte 0)
io.toosmall(2048);
s := string buf[0:n-1];
if(Debug)
sys->print("s: %q\n", s);
(nil, flds) := sys->tokenize(s, " \t");
if(flds != nil && len hd flds >= 2 && (hd flds)[0:2] == "v."){
if(hd flds == "v.2"){
version = 2;
flds = tl flds;
if(Debug)
sys->print("version 2\n");
}else
return "p9any: unknown version";
}
doms: list of string;
for(; flds != nil; flds = tl flds){
(nf, subf) := sys->tokenize(hd flds, "@");
if(nf == 2 && hd subf == "p9sk1")
doms = hd tl subf :: doms;
}
if(doms == nil)
return "p9any: unsupported protocol";
if(Debug){
for(l := doms; l != nil; l = tl l)
sys->print("dom: %q\n", hd l);
}
r := array of byte ("p9sk1 "+hd doms);
buf[0:] = r;
buf[len r] = byte 0;
io.write(buf, len r + 1);
if(version == 2){
b := io.readn(3);
if(b == nil || b[0] != byte 'O' || b[1] != byte 'K' || b[2] != byte 0)
return "p9any: AS protocol botch: not OK";
if(Debug)
sys->print("OK\n");
}
return p9sk1client(io, hd doms);
}
#p9sk1:
# C->S: nonce-C
# S->C: nonce-S, uid-S, domain-S
# C->A: nonce-S, uid-S, domain-S, uid-C, factotum-C
# A->C: Kc{nonce-S, uid-C, uid-S, Kn}, Ks{nonce-S, uid-C, uid-S, K-n}
# C->S: Ks{nonce-S, uid-C, uid-S, K-n}, Kn{nonce-S, counter}
# S->C: Kn{nonce-C, counter}
#asserts that uid-S and uid-C share new secret Kn
#increment the counter to reuse the ticket.
p9sk1client(io: ref IO, udom: string): string
{
# C->S: nonce-C
cchal := array[CHALLEN] of byte;
memrandom(cchal, CHALLEN);
if(io.write(cchal, len cchal) != len cchal)
return sys->sprint("p9sk1: can't write cchal: %r");
# S->C: nonce-S, uid-S, domain-S
trbuf := io.readn(TICKREQLEN);
if(trbuf == nil)
return sys->sprint("p9sk1: can't read ticketreq: %r");
(nil, tr) := Ticketreq.unpack(trbuf);
if(tr == nil)
return "p9sk1: can't unpack ticket request";
if(Debug)
sys->print("ticketreq: type=%d authid=%q authdom=%q chal= hostid=%q uid=%q\n",
tr.rtype, tr.authid, tr.authdom, tr.hostid, tr.uid);
(mykey, diag) := io.findkey(nil, sys->sprint("dom=%q proto=p9sk1 user? !password?", udom));
if(mykey == nil)
return "can't find key: "+diag;
ukey: array of byte;
if((a := authio->lookattrval(mykey.secrets, "!hex")) != nil){
ukey = base16->dec(a);
if(len ukey != DESKEYLEN)
return "p9sk1: invalid !hex key";
}else if((a = authio->lookattrval(mykey.secrets, "!password")) != nil)
ukey = auth9->passtokey(a);
else
return "no !password (or !hex) in key";
# A->C: Kc{nonce-S, uid-C, uid-S, Kn}, Ks{nonce-S, uid-C, uid-S, K-n}
user := authio->lookattrval(mykey.attrs, "user");
if(user == nil)
user = authio->user(); # shouldn't happen
tr.rtype = Auth9->AuthTreq;
tr.hostid = user;
tr.uid = tr.hostid; # not speaking for anyone else
(tick, serverbits) := getastickets(tr, ukey);
if(tick == nil)
return sys->sprint("p9sk1: getasticket failed: %r");
if(tick.num != Auth9->AuthTc)
return "p9sk1: getasticket: failed: wrong key?";
if(Debug)
sys->print("ticket: num=%d chal= cuid=%q suid=%q key=\n", tick.num, tick.cuid, tick.suid);
# C->S: Ks{nonce-S, uid-C, uid-S, K-n}, Kn{nonce-S, counter}
ar := ref Authenticator;
ar.num = Auth9->AuthAc;
ar.chal = tick.chal;
ar.id = 0;
obuf := array[TICKETLEN+AUTHENTLEN] of byte;
obuf[0:] = serverbits;
obuf[TICKETLEN:] = ar.pack(tick.key);
if(io.write(obuf, len obuf) != len obuf)
return "p9sk1: error writing authenticator: %r";
# S->C: Kn{nonce-C, counter}
sbuf := io.readn(AUTHENTLEN);
if(sbuf == nil)
return sys->sprint("p9sk1: can't read server's authenticator: %r");
(nil, ar) = Authenticator.unpack(sbuf, tick.key);
if(ar.num != Auth9->AuthAs || !eqbytes(ar.chal, cchal) || ar.id != 0)
return "invalid authenticator from server";
ai := ref Authinfo(tick.cuid, tick.suid, nil, auth9->des56to64(tick.key));
io.done(ai);
return nil;
}
getastickets(tr: ref Ticketreq, key: array of byte): (ref Ticket, array of byte)
{
afd := authdial(nil, tr.authdom);
if(afd == nil)
return (nil, nil);
return auth9->_asgetticket(afd, tr, key);
}
#
# where to put the following functions?
#
csgetvalue(netroot: string, keytag: string, keyval: string, needtag: string): string
{
cs := "/net/cs";
if(netroot != nil)
cs = netroot+"/cs";
fd := sys->open(cs, Sys->ORDWR); # TO DO: choice of root
if(fd == nil)
return nil;
if(sys->fprint(fd, "!%s=%s %s=*", keytag, keyval, needtag) < 0)
return nil;
sys->seek(fd, big 0, 0);
buf := array[1024] of byte;
while((n := sys->read(fd, buf, len buf)) > 0){
al := authio->parseline(string buf[0:n]); # assume the conventions match factotum's
for(; al != nil; al = tl al)
if((hd al).name == needtag)
return (hd al).val;
}
return nil;
}
authdial(netroot: string, dom: string): ref Sys->FD
{
p: string;
if(dom != nil){
# look up an auth server in an authentication domain
p = csgetvalue(netroot, "authdom", dom, "auth");
# if that didn't work, just try the IP domain
if(p == nil)
p = csgetvalue(netroot, "dom", dom, "auth");
if(p == nil)
p = "$auth"; # temporary ...
if(p == nil){
sys->werrstr("no auth server found for "+dom);
return nil;
}
}else
p = "$auth"; # look for one relative to my machine
(nil, conn) := sys->dial(netmkaddr(p, netroot, "ticket"), nil);
return conn.dfd;
}
keycheck(nil: ref Authio->Key): string
{
return nil;
}
|