]> www.average.org Git - pam_pcsc_cr.git/blob - test_auth.c
177fb5f26abf6639034148e2e942d5a1f6c822c6
[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 int main(int argc, char *argv[])
8 {
9         const char *id = "testuser";
10         const char *pass = "testpassword";
11         const char *nonce = "1";
12         unsigned char secret[] = {0xb4, 0x62, 0xf2, 0x60, 0x87,
13                                         0x78, 0x16, 0x87, 0xde, 0xce,
14                                         0x80, 0x09, 0x24, 0x0b, 0x93,
15                                         0xfc, 0xa0, 0xfc, 0x56, 0x56};
16         const unsigned char *payload = (unsigned char *)
17                                         "To authorize or not to authorize?";
18         int i;
19         struct _auth_obj ao;
20         struct _auth_obj nao;
21
22         if (argc == 2 && strlen(argv[1]) == 40 &&
23                         strspn(argv[1], "0123456789abcdefABCDEF") == 40) {
24                 for (i = 0; i < sizeof(secret); i++)
25                         sscanf(&argv[1][i*2], "%2hhx", &secret[i]);
26         }
27         ao = new_authobj(id, pass, nonce, secret, sizeof(secret),
28                         payload, strlen((char *)payload));
29         printf("new_authobj err=%s\n", ao.err?ao.err:"<no error>");
30         printf("data(%d):", ao.datasize);
31         for (i = 0; i < ao.datasize; i++) printf(" %02x", ao.data[i]);
32         printf("\npayload(%d): \"%.*s\"\n", ao.paylsize, ao.paylsize,
33                 ao.payload?(char*)ao.payload:"");
34         if (ao.err) {
35                 if (ao.buffer) free(ao.buffer);
36                 return 1;
37         }
38
39         nao = verify_authobj(id, pass, nonce, nonce, ao.data, ao.datasize);
40         printf("verify_authobj err=%s\n", nao.err?nao.err:"<no error>");
41         printf("data(%d):", nao.datasize);
42         for (i = 0; i < nao.datasize; i++) printf(" %02x", nao.data[i]);
43         printf("\npayload(%d): \"%.*s\"\n", nao.paylsize, nao.paylsize,
44                 nao.payload?(char*)nao.payload:"");
45         if (nao.err) {
46                 if (nao.buffer) free(nao.buffer);
47                 return 1;
48         }
49         if (ao.paylsize != nao.paylsize ||
50                         memcmp(ao.payload, nao.payload, ao.paylsize)) {
51                 printf("payload does not match");
52                 return 1;
53         }
54
55         if (ao.buffer) free(ao.buffer);
56         if (nao.buffer) free(nao.buffer);
57         return 0;
58 }