2 * p9cr, vnc - one-sided challenge/response authentication
11 * Note that this is the protocol between factotum and the local
12 * program, not between the two factotums. The information
13 * exchanged here is wrapped in other protocols by the local
23 char *chal, *pw, *res, *user;
24 int astype, nchal, npw, ntry, ret;
36 if(c->proto == &p9cr){
39 }else if(c->proto == &vnc){
47 c->state = "find key";
48 k = keyfetch(c, "%A %s", attr, c->proto->keyprompt);
55 c->attr = addattrs(copyattr(attr), k->attr);
56 if((pw = strfindattr(k->privattr, "!password")) == nil){
57 werrstr("key has no !password (cannot happen)");
62 if((user = strfindattr(k->attr, "user")) == nil){
63 werrstr("key has no user (cannot happen)");
67 if(convprint(c, "%s", user) < 0)
70 if(convreadm(c, &chal) < 0)
73 if((nresp = (*response)(chal, resp)) < 0)
76 if(convwrite(c, resp, nresp) < 0)
79 if(convreadm(c, &res) < 0)
82 if(strcmp(res, "ok") == 0)
85 if((k = keyreplace(c, k, "%s", res)) == nil){
86 c->state = "auth failed";
106 char chal[MAXCHAL], *user, *resp;
107 int astype, challen, asfd, fd, ret;
115 memset(&s, 0, sizeof s);
118 if(c->proto == &p9cr){
121 }else if(c->proto == &vnc){
125 werrstr("bad proto");
129 c->state = "find key";
130 if((k = plan9authkey(c->attr)) == nil)
134 a = copyattr(k->attr);
135 a = delattr(a, "proto");
136 c->attr = addattrs(c->attr, a);
140 c->state = "authdial";
141 hostid = strfindattr(s.k->attr, "user");
142 dom = strfindattr(s.k->attr, "dom");
143 if((asfd = xioauthdial(nil, s.dom)) < 0){
144 werrstr("authdial %s: %r", s.dom);
148 c->state = "authchal";
149 if(p9crchal(&s, astype, chal) < 0)
152 c->state = "write challenge";
153 if(convprint(c, "%s", chal) < 0)
157 c->state = "read user";
158 if(convreadm(c, &user) < 0)
161 c->state = "read response";
162 if(convreadm(c, &resp) < 0)
165 c->state = "authwrite";
166 switch(apopresp(&s, user, resp)){
170 c->state = "write status";
171 if(convprint(c, "bad authentication failed") < 0)
175 c->state = "write status";
176 if(convprint(c, "ok") < 0)
188 c->attr = addcap(c->attr, c->sysuser, &s.t);
203 typedef struct State State;
228 static char *phasenames[Maxphase] =
230 [CNeedChal] "CNeedChal",
231 [CHaveResp] "CHaveResp",
233 [SHaveChal] "SHaveChal",
234 [SNeedResp] "SNeedResp",
238 p9crclose(Fsstate *fss)
250 static int getchal(State*, Fsstate*);
253 p9crinit(Proto *p, Fsstate *fss)
260 if((iscli = isclient(_str_findattr(fss->attr, "role"))) < 0)
261 return failure(fss, nil);
263 s = emalloc(sizeof(*s));
266 s->astype = AuthChal;
267 s->challen = NETCHLEN;
270 s->challen = Maxchal;
275 fss->phase = CNeedChal;
277 attr = setattr(_copyattr(fss->attr), "proto=p9sk1");
280 ret = findkey(&s->key, fss, Kuser, 0, attr ? attr : fss->attr,
281 "role=client %s", p->keyprompt);
289 if((ret = findp9authkey(&s->key, fss)) != RpcOk){
293 if((user = _str_findattr(fss->attr, "user")) == nil){
295 return failure(fss, "no user name specified in start msg");
297 if(strlen(user) >= sizeof s->tr.uid){
299 return failure(fss, "user name too long");
302 strcpy(s->tr.uid, user);
303 ret = getchal(s, fss);
305 p9crclose(fss); /* frees s */
309 fss->phasename = phasenames;
310 fss->maxphase = Maxphase;
315 p9crread(Fsstate *fss, void *va, uint *n)
323 return phaseerror(fss, "read");
328 memmove(va, s->resp, *n);
329 fss->phase = Established;
333 if(s->astype == AuthChal)
334 m = strlen(s->chal); /* ascii string */
336 m = s->challen; /* fixed length binary */
338 return toosmall(fss, m);
340 memmove(va, s->chal, m);
341 fss->phase = SNeedResp;
347 p9response(Fsstate *fss, State *s)
354 pw = _str_findattr(s->key->privattr, "!password");
356 return failure(fss, "vncresponse cannot happen");
359 sprint((char*)buf, "%d", atoi(s->chal));
360 if(encrypt(key, buf, 8) < 0)
361 return failure(fss, "can't encrypt response");
362 chal = (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+buf[3];
363 s->resplen = snprint(s->resp, sizeof s->resp, "%.8lux", chal);
367 static uchar tab[256];
369 /* VNC reverses the bits of each byte before using as a des key */
380 for(i=0; i<256; i++) {
384 tab[i] = (tab[i]<<1) | (j&1);
396 k->priv = emalloc(8+1);
397 if(s = _str_findattr(k->privattr, "!password")){
399 memset(k->priv, 0, 8+1);
400 strncpy((char*)k->priv, s, 8);
401 for(p=k->priv; *p; p++)
404 werrstr("no key data");
407 return replacekey(k);
417 vncresponse(Fsstate*, State *s)
421 memmove(s->resp, s->chal, sizeof s->chal);
422 setupDESstate(&des, s->key->priv, nil);
423 desECBencrypt((uchar*)s->resp, s->challen, &des);
424 s->resplen = s->challen;
429 p9crwrite(Fsstate *fss, void *va, uint n)
431 char tbuf[TICKETLEN+AUTHENTLEN];
441 return phaseerror(fss, "write");
444 if(n >= sizeof(s->chal))
445 return failure(fss, Ebadarg);
446 memset(s->chal, 0, sizeof s->chal);
447 memmove(s->chal, data, n);
450 if(s->astype == AuthChal)
451 ret = p9response(fss, s);
453 ret = vncresponse(fss, s);
456 fss->phase = CHaveResp;
460 /* send response to auth server and get ticket */
462 return failure(fss, Ebadarg);
463 memset(resp, 0, sizeof resp);
464 memmove(resp, data, n);
465 if(write(s->asfd, resp, s->challen) != s->challen)
466 return failure(fss, Easproto);
468 /* get ticket plus authenticator from auth server */
469 if(_asrdresp(s->asfd, tbuf, TICKETLEN+AUTHENTLEN) < 0)
470 return failure(fss, nil);
473 convM2T(tbuf, &s->t, s->key->priv);
474 if(s->t.num != AuthTs
475 || memcmp(s->t.chal, s->tr.chal, sizeof(s->t.chal)) != 0)
476 return failure(fss, Easproto);
477 convM2A(tbuf+TICKETLEN, &a, s->t.key);
479 || memcmp(a.chal, s->tr.chal, sizeof(a.chal)) != 0
481 return failure(fss, Easproto);
484 fss->ai.cuid = s->t.cuid;
485 fss->ai.suid = s->t.suid;
487 fss->ai.secret = nil;
488 fss->phase = Established;
494 getchal(State *s, Fsstate *fss)
496 char trbuf[TICKREQLEN];
499 safecpy(s->tr.hostid, _str_findattr(s->key->attr, "user"), sizeof(s->tr.hostid));
500 safecpy(s->tr.authdom, _str_findattr(s->key->attr, "dom"), sizeof(s->tr.authdom));
501 s->tr.type = s->astype;
502 convTR2M(&s->tr, trbuf);
504 /* get challenge from auth server */
505 s->asfd = _authdial(nil, _str_findattr(s->key->attr, "dom"));
507 return failure(fss, Easproto);
508 if(write(s->asfd, trbuf, TICKREQLEN) != TICKREQLEN)
509 return failure(fss, Easproto);
510 n = _asrdresp(s->asfd, s->chal, s->challen);
513 werrstr("_asrdresp short read");
514 return failure(fss, nil);
517 fss->phase = SHaveChal;
528 .keyprompt= "user? !password?",
538 .keyprompt= "!password?",