]> www.average.org Git - pam_pcsc_cr.git/commitdiff
systematize authobj errors
authorEugene Crosser <crosser@average.org>
Sat, 9 Nov 2013 19:48:33 +0000 (23:48 +0400)
committerEugene Crosser <crosser@average.org>
Sat, 9 Nov 2013 19:48:33 +0000 (23:48 +0400)
authobj.c
authobj.h
serial.c
serial.h
test_serial.c

index 3bd9da5f33e4de709fbf52924e680a11fb015fec..2b774abccae724298d35666d94d9fd83ebbf1f7b 100644 (file)
--- a/authobj.c
+++ b/authobj.c
@@ -13,11 +13,15 @@ int make_challenge(const char *id, const char *pass, const char *nonce,
 {
        serializer_t srl;
 
-       if (serial_init(&srl, challenge, *challengesize)) return -1;
-       if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -2;
-       if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -3;
-       if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -4;
-       if (serial_put(&srl, NULL, 0) != 0) return -5;
+       serial_init(&srl, challenge, *challengesize);
+       if (serial_put(&srl, id, strlen(id)) != strlen(id))
+               return aoe_serial;
+       if (serial_put(&srl, pass, strlen(pass)) != strlen(pass))
+               return aoe_serial;
+       if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce))
+               return aoe_serial;
+       if (serial_put(&srl, NULL, 0) != 0)
+               return aoe_serial;
        *challengesize = serial_size(&srl);
        return 0;
 }
@@ -40,27 +44,27 @@ int make_authobj(const char *id, const char *pass, const char *nonce,
        datasize = ((secsize + paysize + HASHSIZE * 4 * sizeof(short) - 1) /
                        CBLKSIZE + 1) * CBLKSIZE;
        data = alloca(datasize);
-       if (serial_init(&srl, data, datasize)) return -1;
-       if (serial_put(&srl, secret, secsize) != secsize) return -2;
-       if (serial_put(&srl, payload, paysize) != paysize) return -3;
+       serial_init(&srl, data, datasize);
+       if (serial_put(&srl, secret, secsize) != secsize) return aoe_serial;
+       if (serial_put(&srl, payload, paysize) != paysize) return aoe_serial;
        if (hash(data, serial_size(&srl), datahash, &datahashsize))
-               return -4;
+               return aoe_size;
        if (serial_put(&srl, datahash, datahashsize) != datahashsize)
-               return -5;
-       if (serial_put(&srl, NULL, 0) != 0) return -6;
+               return aoe_serial;
+       if (serial_put(&srl, NULL, 0) != 0) return aoe_serial;
        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 -7;
+               return aoe_serial;
 
        if (hmac(secret, secsize, challenge, challengesize,
-               key, &keysize)) return -8;
+               key, &keysize)) return aoe_crypt;
 
-       if (*bufsize < datasize) return -9;
-       if (encrypt(key, CBLKSIZE, data, buffer, datasize)) return -10;
+       if (*bufsize < datasize) return aoe_size;
+       if (encrypt(key, CBLKSIZE, data, buffer, datasize)) return aoe_crypt;
        *bufsize = datasize;
 
        return 0;
@@ -81,19 +85,20 @@ int parse_authobj(const unsigned char *key, const int keysize,
        int theirhashsize = HASHSIZE;
 
        if (decrypt(key, CBLKSIZE, buffer, data, datasize))
-               return -1;
-       if (serial_init(&srl, data, datasize)) return -2;
+               return aoe_crypt;
+       serial_init(&srl, data, datasize);
        tsize = *secsize;
        *secsize = serial_get(&srl, secret, tsize);
-       if (*secsize > tsize || *secsize <= 0) return -3;
+       if (*secsize > tsize || *secsize <= 0) return aoe_serial;
        tsize = *paysize;
        *paysize = serial_get(&srl, payload, tsize);
-       if (*paysize > tsize || *paysize <= 0) return -4;
-       if (hash(data, serial_size(&srl), myhash, &myhashsize)) return -5;
+       if (*paysize > tsize || *paysize <= 0) return aoe_serial;
+       if (hash(data, serial_size(&srl), myhash, &myhashsize))
+               return aoe_crypt;
        theirhashsize = serial_get(&srl, theirhash, theirhashsize);
-       if (theirhashsize != HASHSIZE) return -6;
+       if (theirhashsize != HASHSIZE) return aoe_data;
        if ((myhashsize != theirhashsize) ||
-                               memcmp(myhash, theirhash, myhashsize))
-               return -7;
+           memcmp(myhash, theirhash, myhashsize))
+               return aoe_data;
        return 0;
 }
index 8478d95ef4680a5228d0e72dc8b8fbbba3c33d61..cc5baedde5b827e5f12bc774f8dc6991c3dc2c22 100644 (file)
--- a/authobj.h
+++ b/authobj.h
@@ -1,6 +1,8 @@
 #ifndef _AUTHOBJ_H
 #define _AUTHOBJ_H
 
+enum aobj_err {aoe_serial, aoe_size, aoe_crypt, aoe_data};
+
 int make_challenge(const char *id, const char *pass, const char *nonce,
                unsigned char *challenge, int *challengesize);
 int make_authobj(const char *id, const char *pass, const char *nonce,
index 8a24abd3db1d11f19bdce85a905420936e4a155f..809ea7ce108a41e0d0e942ce58980291870ea6b8 100644 (file)
--- a/serial.c
+++ b/serial.c
@@ -4,14 +4,13 @@
 #include <string.h>
 #include "serial.h"
 
-int serial_init(serializer_t *srl, void *buffer, int size)
+void serial_init(serializer_t *srl, void *buffer, int size)
 {
        srl->buffer = srl->cursor = buffer;
        srl->bufsize = size;
-       return 0;
 }
 
-int serial_switch(serializer_t *srl, void *buffer, int size)
+void serial_switch(serializer_t *srl, void *buffer, int size)
 {
        int used = srl->cursor - srl->buffer;
 
@@ -19,7 +18,6 @@ int serial_switch(serializer_t *srl, void *buffer, int size)
        srl->buffer = buffer;
        srl->bufsize = size;
        srl->cursor = buffer + used;
-       return 0;
 }
 
 int serial_put(serializer_t *srl, const void *item, int size)
index a501fa81eed1807ab792a3d0ae7529965326f06f..9cda40c66b6ec03d17a364d6d5a91b022c9cb4c2 100644 (file)
--- a/serial.h
+++ b/serial.h
@@ -7,8 +7,8 @@ typedef struct _serializer {
        char *cursor;
 } serializer_t;
 
-int serial_init(serializer_t *srl, void *buffer, int size);
-int serial_switch(serializer_t *srl, void *buffer, int size);
+void serial_init(serializer_t *srl, void *buffer, int size);
+void serial_switch(serializer_t *srl, void *buffer, int size);
 int serial_put(serializer_t *srl, const void *item, int size);
 int serial_get(serializer_t *srl, void *item, int bufsize);
 int serial_size(serializer_t *srl);
index b9d5e4bbd7f70d11d3367625d4008a36ccf960b1..c2a59e60171fe77cc3a9cdc3d8b7557e277dce13 100644 (file)
@@ -14,10 +14,7 @@ int main(int argc, char *argv[])
        int i, rc;
        serializer_t srl;
 
-       if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) {
-               printf("serial_init rc=%d\n", rc);
-               return 1;
-       }
+       serial_init(&srl, buffer, sizeof(buffer));
        for (i = 0; in[i]; i++) {
                int size = strlen(in[i]);
                if ((rc = serial_put(&srl, in[i], size)) != size) {
@@ -31,10 +28,7 @@ int main(int argc, char *argv[])
                return 1;
        }
        printf("serialized size=%d\n", serial_size(&srl));
-       if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) {
-               printf("second serial_init rc=%d\n", rc);
-               return 1;
-       }
+       serial_init(&srl, buffer, sizeof(buffer));
        for (i = 0; i < 4; i++) {
                char item[32];
                memset(item, 0, sizeof(item));