]> www.average.org Git - pam_pcsc_cr.git/blob - authobj.c
add test_auth
[pam_pcsc_cr.git] / authobj.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <stdio.h>
5 #include <string.h>
6 #include <alloca.h>
7 #include "serial.h"
8 #include "crypto.h"
9 #include "authobj.h"
10
11 int make_challenge(const char *id, const char *pass, const char *nonce,
12                 unsigned char *challenge, int *challengesize)
13 {
14         serializer_t srl;
15
16         if (serial_init(&srl, challenge, *challengesize)) return -1;
17         if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -1;
18         if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -1;
19         if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -1;
20         if (serial_put(&srl, NULL, 0) != 0) return -1;
21         *challengesize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
22         return 0;
23 }
24
25 int make_authobj(const char *id, const char *pass, const char *nonce,
26                 const unsigned char *secret, const int secsize,
27                 const unsigned char *payload, const int paysize,
28                 unsigned char *buffer, int *bufsize)
29 {
30         unsigned char *data;
31         int datasize;
32         unsigned char datahash[HASHSIZE];
33         int datahashsize = HASHSIZE;
34         unsigned char *challenge;
35         int challengesize;
36         unsigned char key[HASHSIZE];
37         int keysize = HASHSIZE;
38         serializer_t srl;
39
40         datasize = ((secsize + paysize + HASHSIZE * 4 * sizeof(short) - 1) /
41                         CBLKSIZE + 1) * CBLKSIZE;
42         data = alloca(datasize);
43         if (serial_init(&srl, data, datasize)) return -1;
44         if (serial_put(&srl, secret, secsize) != secsize) return -1;
45         if (serial_put(&srl, payload, paysize) != paysize) return -1;
46         if (hash(data, serial_size(&srl), datahash, &datahashsize))
47                 return -1;
48         if (serial_put(&srl, datahash, datahashsize) != datahashsize)
49                 return -1;
50         if (serial_put(&srl, NULL, 0) != 0) return -1;
51         datasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
52
53         challengesize = ((strlen(id) + strlen(pass) + strlen(nonce) +
54                         4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE;
55         challenge = alloca(challengesize);
56         if (make_challenge(id, pass, nonce, challenge, &challengesize))
57                 return -1;
58
59         if (hmac(secret, secsize, challenge, challengesize,
60                 key, &keysize)) return -1;
61 #if 0
62         int i;
63         for (i = 0; i < keysize; i++) printf(", 0x%02x", key[i]);
64         printf("\n");
65 #endif
66
67         if (*bufsize < datasize) return -1;
68         *bufsize = datasize;
69         if (encrypt(key, CBLKSIZE, data, buffer, datasize)) return -1;
70
71         return 0;
72 }
73
74 int parse_authobj(const unsigned char *key, const int keysize,
75                 const unsigned char *buffer, const int bufsize,
76                 unsigned char *secret, int *secsize,
77                 unsigned char *payload, int *paysize)
78 {
79         int datasize = bufsize;
80         unsigned char *data = alloca(datasize);
81         serializer_t srl;
82         int tsize;
83         unsigned char myhash[HASHSIZE];
84         int myhashsize = HASHSIZE;
85         unsigned char theirhash[HASHSIZE];
86         int theirhashsize = HASHSIZE;
87
88         if (decrypt(key, CBLKSIZE, buffer, data, datasize))
89                 return -1;
90         if (serial_init(&srl, data, datasize)) return -1;
91         tsize = *secsize;
92         if ((*secsize = serial_get(&srl, secret, tsize)) > tsize) return -1;
93         tsize = *paysize;
94         if ((*paysize = serial_get(&srl, payload, tsize)) > tsize) return -1;
95         if (hash(data, serial_size(&srl), myhash, &myhashsize))
96                 return -1;
97         if ((theirhashsize = serial_get(&srl, theirhash, theirhashsize)) != HASHSIZE)
98                 return -1;
99         if ((myhashsize != theirhashsize) ||
100                                 memcmp(myhash, theirhash, myhashsize))
101                 return -1;
102         return 0;
103 }