]> www.average.org Git - pam_pcsc_cr.git/blob - test_auth.c
wip single-function authobj
[pam_pcsc_cr.git] / test_auth.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "authobj.h"
5 #include "crypto.h"
6
7 unsigned char secret[] = {
8         0xb4, 0x62, 0xf2, 0x60, 0x87, 0x78, 0x16, 0x87, 0xde, 0xce,
9         0x80, 0x09, 0x24, 0x0b, 0x93, 0xfc, 0xa0, 0xfc, 0x56, 0x56
10 };
11
12 static struct _auth_chunk
13 fetch_key(const unsigned char *challenge, const int challengesize)
14 {
15         struct _auth_chunk ho = {0};
16         long rc;
17         int keysize = sizeof(ho.data);
18
19 #if 0   /* The "real" fetch_key shall do this: */
20         if ((rc = pcsc_cr(challenge, challengesize, ho.data, &keysize))) {
21                 ho.err = pcsc_errstr(rc);
22         }
23 #else   /* Instead, duplicate make_key so it works without the token */
24         if ((rc = hmac(secret, sizeof(secret), challenge, challengesize,
25                                                 &ho.data, &keysize))) {
26                 ho.err = crypto_errstr(rc);
27         } else if (keysize != sizeof(ho.data)) {
28                 ho.err = "make_key: hash size is wrong";
29         }
30 #endif
31         return ho;
32 }
33
34 int main(int argc, char *argv[])
35 {
36         const char *id = "testuser";
37         const char *pass = "testpassword";
38         const char *nonce = "1";
39         const unsigned char *payload = (unsigned char *)
40                                         "To authorize or not to authorize?";
41         int i;
42         struct _auth_obj ao;
43         struct _auth_obj nao;
44
45         if (argc == 2 && strlen(argv[1]) == 40 &&
46                         strspn(argv[1], "0123456789abcdefABCDEF") == 40) {
47                 for (i = 0; i < sizeof(secret); i++)
48                         sscanf(&argv[1][i*2], "%2hhx", &secret[i]);
49         }
50         ao = new_authobj(id, pass, nonce, secret, sizeof(secret),
51                         payload, strlen((char *)payload));
52         printf("new_authobj err=%s\n", ao.err?ao.err:"<no error>");
53         printf("data(%d):", ao.datasize);
54         for (i = 0; i < ao.datasize; i++) printf(" %02x", ao.data[i]);
55         printf("\npayload(%d): \"%.*s\"\n", ao.paylsize, ao.paylsize,
56                 ao.payload?(char*)ao.payload:"");
57         if (ao.err) {
58                 if (ao.buffer) free(ao.buffer);
59                 return 1;
60         }
61
62         nao = verify_authobj(id, pass, nonce, nonce, ao.data, ao.datasize);
63         printf("verify_authobj err=%s\n", nao.err?nao.err:"<no error>");
64         printf("data(%d):", nao.datasize);
65         for (i = 0; i < nao.datasize; i++) printf(" %02x", nao.data[i]);
66         printf("\npayload(%d): \"%.*s\"\n", nao.paylsize, nao.paylsize,
67                 nao.payload?(char*)nao.payload:"");
68         if (nao.err) {
69                 if (nao.buffer) free(nao.buffer);
70                 return 1;
71         }
72         if (ao.paylsize != nao.paylsize ||
73                         memcmp(ao.payload, nao.payload, ao.paylsize)) {
74                 printf("payload does not match");
75                 return 1;
76         }
77
78         if (ao.buffer) free(ao.buffer);
79         if (nao.buffer) free(nao.buffer);
80         return 0;
81 }