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