2 * p9sk1, p9sk2 - Plan 9 secret (private) key authentication.
3 * p9sk2 is an incomplete flawed variant of p9sk1.
6 * write challenge[challen] (p9sk1 only)
7 * read tickreq[tickreqlen]
8 * write ticket[ticketlen]
9 * read authenticator[authentlen]
12 * read challenge[challen] (p9sk1 only)
13 * write tickreq[tickreqlen]
14 * read ticket[ticketlen]
15 * write authenticator[authentlen]
21 extern Proto p9sk1, p9sk2;
22 static int gettickets(Ticketreq*, char*, Key*);
24 #define max(a, b) ((a) > (b) ? (a) : (b))
27 MAXAUTH = max(TICKREQLEN, TICKETLEN+max(TICKETLEN, AUTHENTLEN))
48 /* p9sk1: send client challenge */
49 if(c->proto == &p9sk1){
50 c->state = "write challenge";
51 memrandom(cchal, CHALLEN);
52 if(convwrite(c, cchal, CHALLEN) < 0)
56 /* read ticket request */
57 c->state = "read tickreq";
58 if(convread(c, buf, TICKREQLEN) < 0)
62 /* p9sk2: use server challenge as client challenge */
63 if(c->proto == &p9sk2)
64 memmove(cchal, tr.chal, CHALLEN);
69 * if the user is the factotum owner, any key will do.
70 * if not, then if we have a speakfor key,
71 * we will only vouch for the user's local identity.
73 * this logic is duplicated in p9any.c
75 user = strfindattr(c->attr, "user");
76 a = delattr(copyattr(c->attr), "role");
77 a = addattr(a, "proto=p9sk1");
79 if(strcmp(c->sysuser, owner) == 0){
81 a = addattr(a, "proto=p9sk1 user? dom=%q", tr.authdom);
82 }else if(user==nil || strcmp(c->sysuser, user)==0){
84 a = delattr(a, "user");
85 a = addattr(a, "proto=p9sk1 user? dom=%q role=speakfor", tr.authdom);
87 werrstr("will not authenticate for %q as %q", c->sysuser, user);
92 c->state = "find key";
93 k = keyfetch(c, "%A", a);
97 /* relay ticket request to auth server, get tickets */
98 strcpy(tr.hostid, strfindattr(k->attr, "user"));
100 strcpy(tr.uid, c->sysuser);
102 strcpy(tr.uid, tr.hostid);
104 c->state = "get tickets";
105 if(gettickets(&tr, buf, k) < 0)
108 convM2T(buf, &t, k->priv);
112 /* we don't agree with the auth server about the key; try again */
113 c->state = "replace key";
114 if((k = keyreplace(c, k, "key mismatch with auth server")) == nil){
115 werrstr("key mismatch with auth server");
120 /* send second ticket and authenticator to server */
121 c->state = "write ticket+auth";
122 memmove(buf, buf+TICKETLEN, TICKETLEN);
124 memmove(au.chal, tr.chal, CHALLEN);
126 convA2M(&au, buf+TICKETLEN, t.key);
127 if(convwrite(c, buf, TICKETLEN+AUTHENTLEN) < 0)
130 /* read authenticator from server */
131 c->state = "read auth";
132 if(convread(c, buf, AUTHENTLEN) < 0)
134 convM2A(buf, &au, t.key);
135 if(au.num != AuthAs || memcmp(au.chal, cchal, CHALLEN) != 0 || au.id != 0){
136 werrstr("server lies through his teeth");
141 c->attr = addcap(c->attr, c->sysuser, &t);
142 flog("p9skclient success %A", c->attr); /* before adding secret! */
143 des56to64((uchar*)t.key, secret);
144 c->attr = addattr(c->attr, "secret=%.8H", secret);
149 flog("p9skclient: %r");
158 char cchal[CHALLEN], buf[MAXAUTH];
169 a = addattr(copyattr(c->attr), "user? dom?");
170 a = addattr(a, "user? dom? proto=p9sk1");
171 if((k = keyfetch(c, "%A", a)) == nil)
174 /* p9sk1: read client challenge */
175 if(c->proto == &p9sk1){
176 if(convread(c, cchal, CHALLEN) < 0)
180 /* send ticket request */
181 memset(&tr, 0, sizeof tr);
183 strcpy(tr.authid, strfindattr(k->attr, "user"));
184 strcpy(tr.authdom, strfindattr(k->attr, "dom"));
185 memrandom(tr.chal, sizeof tr.chal);
187 if(convwrite(c, buf, TICKREQLEN) < 0)
190 /* p9sk2: use server challenge as client challenge */
191 if(c->proto == &p9sk2)
192 memmove(cchal, tr.chal, sizeof tr.chal);
194 /* read ticket+authenticator */
195 if(convread(c, buf, TICKETLEN+AUTHENTLEN) < 0)
198 convM2T(buf, &t, k->priv);
199 if(t.num != AuthTs || memcmp(t.chal, tr.chal, CHALLEN) != 0){
201 werrstr("key mismatch with auth server");
205 convM2A(buf+TICKETLEN, &au, t.key);
206 if(au.num != AuthAc || memcmp(au.chal, tr.chal, CHALLEN) != 0 || au.id != 0){
207 werrstr("client lies through his teeth");
211 /* send authenticator */
213 memmove(au.chal, cchal, CHALLEN);
214 convA2M(&au, buf, t.key);
215 if(convwrite(c, buf, AUTHENTLEN) < 0)
219 c->attr = addcap(c->attr, c->sysuser, &t);
220 flog("p9skserver success %A", c->attr); /* before adding secret! */
221 des56to64((uchar*)t.key, secret);
222 c->attr = addattr(c->attr, "secret=%.8H", secret);
227 flog("p9skserver: %r");
234 _asgetticket(int fd, char *trbuf, char *tbuf)
236 if(write(fd, trbuf, TICKREQLEN) < 0){
240 return _asrdresp(fd, tbuf, 2*TICKETLEN);
243 getastickets(Ticketreq *tr, char *buf)
248 if((asfd = xioauthdial(nil, tr->authdom)) < 0)
251 ret = xioasgetticket(asfd, buf, buf);
257 mktickets(Ticketreq *tr, char *buf, Key *k)
261 if(strcmp(tr->authid, tr->hostid) != 0)
264 memset(&t, 0, sizeof t);
265 memmove(t.chal, tr->chal, CHALLEN);
266 strcpy(t.cuid, tr->uid);
267 strcpy(t.suid, tr->uid);
268 memrandom(t.key, DESKEYLEN);
270 convT2M(&t, buf, k->priv);
272 convT2M(&t, buf+TICKETLEN, k->priv);
277 gettickets(Ticketreq *tr, char *buf, Key *k)
279 if(getastickets(tr, buf) == 0)
281 if(mktickets(tr, buf, k) == 0)
283 werrstr("gettickets: %r");
290 char *user, *dom, *pass;
293 user = strfindattr(k->attr, "user");
294 dom = strfindattr(k->attr, "dom");
295 if(user==nil || dom==nil){
296 werrstr("need user and dom attributes");
299 if(strlen(user) >= sizeof tr.authid){
300 werrstr("user name too long");
303 if(strlen(dom) >= sizeof tr.authdom){
304 werrstr("auth dom name too long");
308 k->priv = emalloc(DESKEYLEN);
309 if(pass = strfindattr(k->privattr, "!password"))
310 passtokey(k->priv, pass);
311 else if(pass = strfindattr(k->privattr, "!hex")){
312 if(hexparse(pass, k->priv, 7) < 0){
313 werrstr("malformed !hex key data");
317 werrstr("need !password or !hex attribute");
334 "client", p9skclient,
335 "server", p9skserver,
342 "client", p9skclient,
343 "server", p9skserver,
350 "user? dom? !password?",