2 * Various files from /sys/src/cmd/auth/secstore, just enough
3 * to download a file at boot time.
11 enum{ MAXFILESIZE = 10*1024*1024 };
13 enum{/* PW status bits */
15 STA = (1<<1) /* extra SecurID step */
18 static char testmess[] = "__secstore\tPAK\nC=%s\nm=0\n";
27 if(p == nil) /* else use the authserver */
28 p = getenv("secstore");
34 return dial(netmkaddr(p, "net", "secstore"), 0, 0, 0);
44 n = snprint((char*)buf, sizeof buf, testmess, owner);
45 hnputs(buf, 0x8000+n-2);
50 fprint(2, "secdial: %r\n");
54 if(write(fd, buf, n) != n || readn(fd, buf, 2) != 2){
55 flog("secstore: no count");
59 n = ((buf[0]&0x7f)<<8) + buf[1];
61 flog("secstore: bad count");
62 werrstr("implausibly large count %d", n);
66 m = readn(fd, buf, n);
69 flog("secstore: unexpected eof");
71 werrstr("short read from secstore");
75 if(strcmp((char*)buf, "!account expired") == 0){
76 flog("secstore: account expired");
77 werrstr("account expired");
80 if(strcmp((char*)buf, "!account exists") == 0){
81 flog("secstore: account exists");
84 flog("secstore: %s", buf);
88 /* delimited, authenticated, encrypted connection */
89 enum{ Maxmsg=4096 }; /* messages > Maxmsg bytes are truncated */
90 typedef struct SConn SConn;
92 extern SConn* newSConn(int); /* arg is open file descriptor */
96 int (*secret)(SConn*, uchar*, int);/* */
97 int (*read)(SConn*, uchar*, int); /* <0 if error; errmess in buffer */
98 int (*write)(SConn*, uchar*, int);
99 void (*free)(SConn*); /* also closes file descriptor */
101 /* secret(s,b,dir) sets secret for digest, encrypt, using the secretlen */
102 /* bytes in b to form keys for the two directions; */
103 /* set dir=0 in client, dir=1 in server */
105 /* error convention: write !message in-band */
106 #define readstr secstore_readstr
107 static void writerr(SConn*, char*);
108 static int readstr(SConn*, char*); /* call with buf of size Maxmsg+1 */
109 /* returns -1 upon error, with error message in buf */
111 typedef struct ConnState {
112 uchar secret[SHA1dlen];
119 int fd; /* file descriptor for read/write of encrypted data */
120 int alg; /* if nonzero, "alg sha rc4_128" */
125 SC_secret(SConn *conn, uchar *sigma, int direction)
127 SS *ss = (SS*)(conn->chan);
128 int nsigma = conn->secretlen;
131 hmac_sha1(sigma, nsigma, (uchar*)"one", 3, ss->out.secret, nil);
132 hmac_sha1(sigma, nsigma, (uchar*)"two", 3, ss->in.secret, nil);
134 hmac_sha1(sigma, nsigma, (uchar*)"two", 3, ss->out.secret, nil);
135 hmac_sha1(sigma, nsigma, (uchar*)"one", 3, ss->in.secret, nil);
137 setupRC4state(&ss->in.rc4, ss->in.secret, 16); /* restrict to 128 bits */
138 setupRC4state(&ss->out.rc4, ss->out.secret, 16);
144 hash(uchar secret[SHA1dlen], uchar *data, int len, int seqno, uchar d[SHA1dlen])
153 memset(&sha, 0, sizeof sha);
154 sha1(secret, SHA1dlen, nil, &sha);
155 sha1(data, len, nil, &sha);
156 sha1(seq, 4, d, &sha);
160 verify(uchar secret[SHA1dlen], uchar *data, int len, int seqno, uchar d[SHA1dlen])
164 uchar digest[SHA1dlen];
170 memset(&sha, 0, sizeof sha);
171 sha1(secret, SHA1dlen, nil, &sha);
172 sha1(data, len, nil, &sha);
173 sha1(seq, 4, digest, &sha);
174 return memcmp(d, digest, SHA1dlen);
178 SC_read(SConn *conn, uchar *buf, int n)
180 SS *ss = (SS*)(conn->chan);
181 uchar count[2], digest[SHA1dlen];
184 if(read(ss->fd, count, 2) != 2 || count[0]&0x80 == 0){
185 werrstr("!SC_read invalid count");
188 len = (count[0]&0x7f)<<8 | count[1]; /* SSL-style count; no pad */
191 if(len <= 0 || readn(ss->fd, digest, SHA1dlen) != SHA1dlen){
192 werrstr("!SC_read missing sha1");
195 if(len > n || readn(ss->fd, buf, len) != len){
196 werrstr("!SC_read missing data");
199 rc4(&ss->in.rc4, digest, SHA1dlen);
200 rc4(&ss->in.rc4, buf, len);
201 if(verify(ss->in.secret, buf, len, ss->in.seqno, digest) != 0){
202 werrstr("!SC_read integrity check failed");
206 if(len <= 0 || len > n){
207 werrstr("!SC_read implausible record length");
210 if( (nr = readn(ss->fd, buf, len)) != len){
211 werrstr("!SC_read expected %d bytes, but got %d", len, nr);
220 SC_write(SConn *conn, uchar *buf, int n)
222 SS *ss = (SS*)(conn->chan);
223 uchar count[2], digest[SHA1dlen], enc[Maxmsg+1];
226 if(n <= 0 || n > Maxmsg+1){
227 werrstr("!SC_write invalid n %d", n);
233 count[0] = 0x80 | len>>8;
235 if(write(ss->fd, count, 2) != 2){
236 werrstr("!SC_write invalid count");
240 hash(ss->out.secret, buf, n, ss->out.seqno, digest);
241 rc4(&ss->out.rc4, digest, SHA1dlen);
243 rc4(&ss->out.rc4, enc, n);
244 if(write(ss->fd, digest, SHA1dlen) != SHA1dlen ||
245 write(ss->fd, enc, n) != n){
246 werrstr("!SC_write error on send");
250 if(write(ss->fd, buf, n) != n){
251 werrstr("!SC_write error on send");
262 SS *ss = (SS*)(conn->chan);
277 ss = (SS*)emalloc(sizeof(*ss));
278 conn = (SConn*)emalloc(sizeof(*conn));
281 conn->chan = (void*)ss;
282 conn->secretlen = SHA1dlen;
283 conn->free = SC_free;
284 conn->secret = SC_secret;
285 conn->read = SC_read;
286 conn->write = SC_write;
291 writerr(SConn *conn, char *s)
295 snprint(buf, Maxmsg, "!%s", s);
296 conn->write(conn, (uchar*)buf, strlen(buf));
300 readstr(SConn *conn, char *s)
304 n = conn->read(conn, (uchar*)s, Maxmsg);
312 strcpy(s, "read error");
318 getfile(SConn *conn, uchar *key, int nkey)
321 int nbuf, n, nr, len;
322 char s[Maxmsg+1], *gf, *p, *q;
323 uchar skey[SHA1dlen], ib[Maxmsg+CHK], *ibr, *ibw;
328 memset(&aes, 0, sizeof aes);
330 snprint(s, Maxmsg, "GET %s\n", gf);
331 conn->write(conn, (uchar*)s, strlen(s));
335 if(readstr(conn, s) < 0){
336 werrstr("secstore: %r");
339 if((len = atoi(s)) < 0){
340 werrstr("secstore: remote file %s does not exist", gf);
342 }else if(len > MAXFILESIZE){/*assert */
343 werrstr("secstore: implausible file size %d for %s", len, gf);
350 for(nr=0; nr < len;){
351 if((n = conn->read(conn, ibw, Maxmsg)) <= 0){
352 werrstr("secstore: empty file chunk n=%d nr=%d len=%d: %r", n, nr, len);
357 if(!aes.setup){ /* first time, read 16 byte IV */
359 werrstr("secstore: no IV in file");
362 sha = sha1((uchar*)"aescbc file", 11, nil, nil);
363 sha1(key, nkey, skey, sha);
364 setupAESstate(&aes, skey, AESbsize, ibr);
365 memset(skey, 0, sizeof skey);
369 aesCBCdecrypt(ibw-n, n, &aes);
372 buf = erealloc(buf, nbuf+n+1);
373 memmove(buf+nbuf, ibr, n);
377 memmove(ib, ibr, ibw-ibr);
378 ibw = ib + (ibw-ibr);
382 if((n != CHK) || (memcmp(ib, "XXXXXXXXXXXXXXXX", CHK) != 0)){
383 werrstr("secstore: decrypted file failed to authenticate!");
388 werrstr("secstore got empty file");
395 if(q = strchr(p, '\n'))
399 flog("secstore %s:%d: %r", gf, n);
400 fprint(2, "secstore(%s) line %d: %r\n", gf, n);
408 static char VERSION[] = "secstore";
410 typedef struct PAKparams{
411 mpint *q, *p, *r, *g;
414 static PAKparams *pak;
416 /* This group was generated by the seed EB7B6E35F7CD37B511D96C67D6688CC4DD440E1E. */
422 pak = (PAKparams*)emalloc(sizeof(*pak));
423 pak->q = strtomp("E0F0EF284E10796C5A2A511E94748BA03C795C13", nil, 16, nil);
424 pak->p = strtomp("C41CFBE4D4846F67A3DF7DE9921A49D3B42DC33728427AB159CEC8CBBD"
425 "B12B5F0C244F1A734AEB9840804EA3C25036AD1B61AFF3ABBC247CD4B384224567A86"
426 "3A6F020E7EE9795554BCD08ABAD7321AF27E1E92E3DB1C6E7E94FAAE590AE9C48F96D9"
427 "3D178E809401ABE8A534A1EC44359733475A36A70C7B425125062B1142D", nil, 16, nil);
428 pak->r = strtomp("DF310F4E54A5FEC5D86D3E14863921E834113E060F90052AD332B3241CEF"
429 "2497EFA0303D6344F7C819691A0F9C4A773815AF8EAECFB7EC1D98F039F17A32A7E887"
430 "D97251A927D093F44A55577F4D70444AEBD06B9B45695EC23962B175F266895C67D21"
431 "C4656848614D888A4", nil, 16, nil);
432 pak->g = strtomp("2F1C308DC46B9A44B52DF7DACCE1208CCEF72F69C743ADD4D2327173444"
433 "ED6E65E074694246E07F9FD4AE26E0FDDD9F54F813C40CB9BCD4338EA6F242AB94CD41"
434 "0E676C290368A16B1A3594877437E516C53A6EEE5493A038A017E955E218E7819734E3E"
435 "2A6E0BAE08B14258F8C03CC1B30E0DDADFCF7CEDF0727684D3D255F1", nil, 16, nil);
438 /* H = (sha(ver,C,sha(passphrase)))^r mod p, */
439 /* a hash function expensive to attack by brute force. */
441 longhash(char *ver, char *C, uchar *passwd, mpint *H)
445 uchar buf[140], key[1];
449 n = nver + nC + SHA1dlen;
450 Cp = (uchar*)emalloc(n);
451 memmove(Cp, ver, nver);
452 memmove(Cp+nver, C, nC);
453 memmove(Cp+nver+nC, passwd, SHA1dlen);
454 for(i = 0; i < 7; i++){
456 hmac_sha1(Cp, n, key, sizeof key, buf+i*SHA1dlen, nil);
460 betomp(buf, sizeof buf, H);
462 mpexp(H, pak->r, pak->p, H);
465 /* Hi = H^-1 mod p */
467 PAK_Hi(char *C, char *passphrase, mpint *H, mpint *Hi)
469 uchar passhash[SHA1dlen];
471 sha1((uchar *)passphrase, strlen(passphrase), passhash, nil);
473 longhash(VERSION, C, passhash, H);
474 mpinvert(H, pak->p, Hi);
475 return mptoa(Hi, 64, nil, 0);
478 /* another, faster, hash function for each party to */
479 /* confirm that the other has the right secrets. */
481 shorthash(char *mess, char *C, char *S, char *m, char *mu, char *sigma, char *Hi, uchar *digest)
485 state = sha1((uchar*)mess, strlen(mess), 0, 0);
486 state = sha1((uchar*)C, strlen(C), 0, state);
487 state = sha1((uchar*)S, strlen(S), 0, state);
488 state = sha1((uchar*)m, strlen(m), 0, state);
489 state = sha1((uchar*)mu, strlen(mu), 0, state);
490 state = sha1((uchar*)sigma, strlen(sigma), 0, state);
491 state = sha1((uchar*)Hi, strlen(Hi), 0, state);
492 state = sha1((uchar*)mess, strlen(mess), 0, state);
493 state = sha1((uchar*)C, strlen(C), 0, state);
494 state = sha1((uchar*)S, strlen(S), 0, state);
495 state = sha1((uchar*)m, strlen(m), 0, state);
496 state = sha1((uchar*)mu, strlen(mu), 0, state);
497 state = sha1((uchar*)sigma, strlen(sigma), 0, state);
498 sha1((uchar*)Hi, strlen(Hi), digest, state);
501 /* On input, conn provides an open channel to the server; */
502 /* C is the name this client calls itself; */
503 /* pass is the user's passphrase */
504 /* On output, session secret has been set in conn */
505 /* (unless return code is negative, which means failure). */
506 /* If pS is not nil, it is set to the (alloc'd) name the server calls itself. */
508 PAKclient(SConn *conn, char *C, char *pass, char **pS)
510 char *mess, *mess2, *eol, *S, *hexmu, *ks, *hexm, *hexsigma = nil, *hexHi;
511 char kc[2*SHA1dlen+1];
512 uchar digest[SHA1dlen];
514 mpint *x, *m = mpnew(0), *mu = mpnew(0), *sigma = mpnew(0);
515 mpint *H = mpnew(0), *Hi = mpnew(0);
517 hexHi = PAK_Hi(C, pass, H, Hi);
519 /* random 1<=x<=q-1; send C, m=g**x H */
520 x = mprand(164, genrandom, nil);
522 if(mpcmp(x, mpzero) == 0)
524 mpexp(pak->g, x, pak->p, m);
527 hexm = mptoa(m, 64, nil, 0);
528 mess = (char*)emalloc(2*Maxmsg+2);
529 mess2 = mess+Maxmsg+1;
530 snprint(mess, Maxmsg, "%s\tPAK\nC=%s\nm=%s\n", VERSION, C, hexm);
531 conn->write(conn, (uchar*)mess, strlen(mess));
533 /* recv g**y, S, check hash1(g**xy) */
534 if(readstr(conn, mess) < 0){
535 fprint(2, "error: %s\n", mess);
536 writerr(conn, "couldn't read g**y");
539 eol = strchr(mess, '\n');
540 if(strncmp("mu=", mess, 3) != 0 || !eol || strncmp("\nk=", eol, 3) != 0){
541 writerr(conn, "verifier syntax error");
547 eol = strchr(ks, '\n');
548 if(!eol || strncmp("\nS=", eol, 3) != 0){
549 writerr(conn, "verifier syntax error for secstore 1.0");
554 eol = strchr(S, '\n');
556 writerr(conn, "verifier syntax error for secstore 1.0");
562 strtomp(hexmu, nil, 64, mu);
563 mpexp(mu, x, pak->p, sigma);
564 hexsigma = mptoa(sigma, 64, nil, 0);
565 shorthash("server", C, S, hexm, hexmu, hexsigma, hexHi, digest);
566 enc64(kc, sizeof kc, digest, SHA1dlen);
567 if(strcmp(ks, kc) != 0){
568 writerr(conn, "verifier didn't match");
572 /* send hash2(g**xy) */
573 shorthash("client", C, S, hexm, hexmu, hexsigma, hexHi, digest);
574 enc64(kc, sizeof kc, digest, SHA1dlen);
575 snprint(mess2, Maxmsg, "k'=%s\n", kc);
576 conn->write(conn, (uchar*)mess2, strlen(mess2));
578 /* set session key */
579 shorthash("session", C, S, hexm, hexmu, hexsigma, hexHi, digest);
580 memset(hexsigma, 0, strlen(hexsigma));
581 n = conn->secret(conn, digest, 0);
582 memset(digest, 0, SHA1dlen);
583 if(n < 0){/*assert */
584 writerr(conn, "can't set secret");
613 pass = readcons("secstore password", nil, 1);
614 if(pass==nil || strlen(pass)==0){
618 if((fd = secdial()) < 0)
620 if((conn = newSConn(fd)) == nil)
622 if(PAKclient(conn, owner, pass, nil) < 0){
623 werrstr("password mistyped?");
626 if(readstr(conn, s) < 0)
628 if(strcmp(s, "STA") == 0){
629 sta = readcons("STA PIN+SecureID", nil, 1);
630 if(sta==nil || strlen(sta)==0){
634 if(strlen(sta) >= sizeof s - 3){
635 werrstr("STA response too long");
639 conn->write(conn, (uchar*)s, strlen(s));
642 if(strcmp(s, "OK") !=0){
646 if(getfile(conn, (uchar*)pass, strlen(pass)) < 0)
648 conn->write(conn, (uchar*)"BYE", 3);
653 flog("secstorefetch: %r");