X-Git-Url: http://www.average.org/gitweb/?p=pam_pcsc_cr.git;a=blobdiff_plain;f=test_auth.c;h=8a68810e07b93854655ee3f13650a31d15dd60d5;hp=55911c7a8cf97458c21aea6e117df9860f5897b0;hb=dc8959a3cb5615448ea9307a3277e608ffd8a6dd;hpb=af4a47cbe298e76f76cfa6dbff8190d0158bd080 diff --git a/test_auth.c b/test_auth.c index 55911c7..8a68810 100644 --- a/test_auth.c +++ b/test_auth.c @@ -1,49 +1,81 @@ #include #include +#include #include "authobj.h" +#include "crypto.h" + +unsigned char secret[] = { + 0xb4, 0x62, 0xf2, 0x60, 0x87, 0x78, 0x16, 0x87, 0xde, 0xce, + 0x80, 0x09, 0x24, 0x0b, 0x93, 0xfc, 0xa0, 0xfc, 0x56, 0x56 +}; + +static struct _auth_chunk +fetch_key(const unsigned char *challenge, const int challengesize) +{ + struct _auth_chunk ho = {0}; + long rc; + int keysize = sizeof(ho.data); + +#if 0 /* The "real" fetch_key shall do this: */ + if ((rc = pcsc_cr(challenge, challengesize, ho.data, &keysize))) { + ho.err = pcsc_errstr(rc); + } +#else /* Instead, duplicate make_key so it works without the token */ + if ((rc = hmac(secret, sizeof(secret), challenge, challengesize, + &ho.data, &keysize))) { + ho.err = crypto_errstr(rc); + } else if (keysize != sizeof(ho.data)) { + ho.err = "make_key: hash size is wrong"; + } +#endif + return ho; +} int main(int argc, char *argv[]) { const char *id = "testuser"; const char *pass = "testpassword"; const char *nonce = "1"; - const unsigned char secret[] = {0x52, 0xf3, 0xbe, 0x1f, 0x3e, - 0x22, 0xa8, 0xee, 0xdf, 0x10, - 0x86, 0xf2, 0x17, 0xd7, 0x21, - 0x9d, 0x08, 0x14, 0x48, 0x38}; const unsigned char *payload = (unsigned char *) "To authorize or not to authorize?"; - unsigned char authobj[512]; - int authsize = sizeof(authobj); - unsigned char challenge[128]; - int challengesize = sizeof(challenge); - int rc; - const unsigned char key[] = {0xcc, 0x21, 0xaa, 0xb7, 0xf5, - 0x76, 0xd6, 0xe7, 0xed, 0x90, - 0x69, 0x51, 0x3d, 0x9b, 0x3a, - 0x9d, 0xa8, 0xcf, 0xf9, 0x2f}; - unsigned char newsecret[20]; - int newsecsize = sizeof(newsecret); - unsigned char newload[128]; - int newloadsize=sizeof(newload); - - rc = make_authobj(id, pass, nonce, secret, sizeof(secret), - payload, strlen((char *)payload), - authobj, &authsize); - printf("make_authobj() rc=%d size=%d\n", rc, authsize); - if (rc) return rc; + int i; + struct _auth_obj ao; + struct _auth_obj nao; - rc = make_challenge(id, pass, nonce, challenge, &challengesize); - printf("make_challenge() rc=%d size=%d\n", rc, challengesize); - if (rc) return rc; + if (argc == 2 && strlen(argv[1]) == 40 && + strspn(argv[1], "0123456789abcdefABCDEF") == 40) { + for (i = 0; i < sizeof(secret); i++) + sscanf(&argv[1][i*2], "%2hhx", &secret[i]); + } + ao = new_authobj(id, pass, nonce, secret, sizeof(secret), + payload, strlen((char *)payload)); + printf("new_authobj err=%s\n", ao.err?ao.err:""); + printf("data(%d):", ao.datasize); + for (i = 0; i < ao.datasize; i++) printf(" %02x", ao.data[i]); + printf("\npayload(%d): \"%.*s\"\n", ao.paylsize, ao.paylsize, + ao.payload?(char*)ao.payload:""); + if (ao.err) { + if (ao.buffer) free(ao.buffer); + return 1; + } - rc = parse_authobj(key, sizeof(key), authobj, authsize, - newsecret, &newsecsize, newload, &newloadsize); - printf("parse_authobj() rc=%d secretsize=%d payload=\"%.*s\" (%d)\n", - rc, newsecsize, newloadsize, newload, newloadsize); - if (memcmp(secret, newsecret, newsecsize)) { - printf("extracted secret does not match\n"); - return -1; + nao = verify_authobj(id, pass, nonce, nonce, ao.data, ao.datasize); + printf("verify_authobj err=%s\n", nao.err?nao.err:""); + printf("data(%d):", nao.datasize); + for (i = 0; i < nao.datasize; i++) printf(" %02x", nao.data[i]); + printf("\npayload(%d): \"%.*s\"\n", nao.paylsize, nao.paylsize, + nao.payload?(char*)nao.payload:""); + if (nao.err) { + if (nao.buffer) free(nao.buffer); + return 1; + } + if (ao.paylsize != nao.paylsize || + memcmp(ao.payload, nao.payload, ao.paylsize)) { + printf("payload does not match"); + return 1; } + + if (ao.buffer) free(ao.buffer); + if (nao.buffer) free(nao.buffer); return 0; }