16 int update_authfile(const char *fn, const char *tokenid, const char *id,
17 const char *password, const char *nonce,
18 const unsigned char *secret, const int secsize,
19 const unsigned char *payload, const int paysize)
24 unsigned char key[20];
25 int keysize = sizeof(key);
26 unsigned char mysecret[20];
27 int mysecsize = sizeof(mysecret);
28 unsigned char myload[256];
29 int myloadsize = sizeof(myload);
30 unsigned char *authobj = alloca(OBJSIZE);
31 int authsize = OBJSIZE;
33 char *oldtokenid = NULL, *oldid = NULL, *oldnonce = NULL,
35 unsigned char *oldauthobj = NULL;
38 if ((fp = fopen(fn, "r"))) {
39 if (fgets(buf, sizeof(buf), fp)) {
40 oldtokenid = strtok(buf, ":\r\n");
41 oldid = strtok(NULL, ":\r\n");
42 oldnonce = strtok(NULL, ":\r\n");
43 hauthobj = strtok(NULL, ":\r\n");
45 eprint("error reading from %s: %s\n",
53 hlen = strlen(hauthobj);
55 eprint("error: auth string has wrong length\n");
57 strspn(hauthobj, "0123456789abcdefABCDEF")) {
58 eprint("error: auth string not hexadecimal\n");
61 oldauthobj = alloca(oldauthsize);
62 for (i = 0; i < oldauthsize; i++)
63 sscanf(&hauthobj[i*2], "%2hhx", &oldauthobj[i]);
67 if (oldauthobj && password && !secret) {
68 unsigned char chal[64];
69 int csize = sizeof(chal);
72 rc = make_challenge(id, password, nonce, chal, &csize);
74 eprint("cannot make challenge\n");
77 rc = pcsc_cr(chal, csize, key, &keysize);
79 eprint("error querying token: %s\n", pcsc_errstr(rc));
82 rc = parse_authobj(key, keysize, oldauthobj, oldauthsize,
83 mysecret, &mysecsize, myload, &myloadsize);
85 eprint("cannot parse old authobj: %d\n", rc);
90 rc = make_authobj(id, password, nonce, mysecret, mysecsize,
91 payload, paysize, authobj, &authsize);
93 eprint("make_authobj error %d\n", rc);
98 eprint("cannot open \"%s\": %s\n",
102 if (fprintf(fp, "%s:%s:%s:", tokenid, id, nonce) < 0) {
103 eprint("cannot write to \"%s\": %s\n",
104 fn, strerror(errno));
107 for (i = 0; i < authsize; i++)
108 if (fprintf(fp, "%02x", authobj[i]) < 0) {
109 eprint("cannot write to \"%s\": %s\n",
110 fn, strerror(errno));
114 if (fclose(fp) < 0) {
115 eprint("cannot close \"%s\": %s\n",
116 fn, strerror(errno));