- if (serial_init(&srl, data, datasize)) return -1;
- if (serial_put(&srl, secret, secsize) != secsize) return -1;
- if (serial_put(&srl, payload, paysize) != paysize) return -1;
- if (hash(data, serial_size(&srl), datahash, &datahashsize))
- return -1;
- if (serial_put(&srl, datahash, datahashsize) != datahashsize)
- return -1;
- if (serial_put(&srl, NULL, 0) != 0) return -1;
- datasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
-
- challengesize = ((strlen(id) + strlen(pass) + strlen(nonce) +
- 4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE;
- challenge = alloca(challengesize);
- if (make_challenge(id, pass, nonce, challenge, &challengesize))
- return -1;
-
- if (hmac(secret, secsize, challenge, challengesize,
- key, &keysize)) return -1;
-#if 0
- int i;
- for (i = 0; i < keysize; i++) printf(", 0x%02x", key[i]);
- printf("\n");
-#endif
+ serial_init(&srl, data, datasize);
+ if (serial_put(&srl, secret, secsize) != secsize) {
+ ao.err = "authobj: serialization of secret failed";
+ } else if (serial_put(&srl, payload, paylsize) != paylsize) {
+ ao.err = "authobj: serialization of payload failed";
+ } else if ((rc = hash(data, serial_size(&srl),
+ datahash, &datahashsize))) {
+ ao.err = crypto_errstr(rc);
+ } else if (serial_put(&srl, datahash, datahashsize) != datahashsize) {
+ ao.err = "authobj: serialization of hash failed";
+ } else if (serial_put(&srl, NULL, 0) != 0) {
+ ao.err = "authobj: serialization of terminator failed";
+ } else {
+ unsigned long lrc;
+ int osize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
+ struct _auth_chunk ho_key;
+
+ ho_key = make_key(userid, password, nonce,
+ secret, secsize, NULL);
+ if (ho_key.err) {
+ ao.err = ho_key.err;
+ } else if ((ao.buffer = malloc(osize + paylsize)) == NULL) {
+ ao.err = "make authobj: malloc failed";
+ } else if ((lrc = encrypt(ho_key.data, CBLKSIZE, data,
+ ao.buffer, osize))) {
+ ao.err = crypto_errstr(lrc);
+ } else {
+ ao.data = ao.buffer;
+ ao.datasize = osize;
+ if (payload && paylsize) {
+ /* payload passthrough */
+ ao.payload = ao.data + osize;
+ memcpy(ao.payload, payload, paylsize);
+ ao.paylsize = paylsize;
+ }
+ }
+ memset(&ho_key, 0, sizeof(ho_key));
+ }
+ memset(data, 0, datasize);
+ return ao;
+}
+
+static struct _auth_obj
+parse_authobj(const char *userid, const char *password, const char *nonce,
+ const unsigned char *secret, const int secsize,
+ const unsigned char *ablob, const int blobsize,
+ struct _auth_chunk (*fetch_key)(const unsigned char *chal,
+ const int csize))
+{
+ unsigned long rc;
+ struct _auth_obj ao = {0};
+ struct _auth_chunk ho_key;