]> www.average.org Git - pam_pcsc_cr.git/blob - authobj.c
introduce make_challenge()
[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
62         if (*bufsize < datasize) return -1;
63         *bufsize = datasize;
64         if (encrypt(key, keysize, data, buffer, datasize)) return -1;
65
66         return 0;
67 }
68
69 int parse_authobj(const unsigned char *key, const int keysize,
70                 const unsigned char *buffer, const int bufsize,
71                 unsigned char *secret, int *secsize,
72                 unsigned char *payload, int *paysize)
73 {
74         int datasize = bufsize;
75         unsigned char *data = alloca(datasize);
76         serializer_t srl;
77         int tsize;
78         unsigned char myhash[HASHSIZE];
79         int myhashsize = HASHSIZE;
80         unsigned char theirhash[HASHSIZE];
81         int theirhashsize = HASHSIZE;
82
83         if (decrypt(key, keysize, buffer, data, datasize))
84                 return -1;
85         if (serial_init(&srl, data, datasize)) return -1;
86         tsize = *secsize;
87         if ((*secsize = serial_get(&srl, secret, tsize)) > tsize) return -1;
88         tsize = *paysize;
89         if ((*paysize = serial_get(&srl, payload, tsize)) > tsize) return -1;
90         if (hash(data, serial_size(&srl), myhash, &myhashsize))
91                 return -1;
92         if ((theirhashsize = serial_get(&srl, theirhash, theirhashsize)) != HASHSIZE)
93                 return -1;
94         if ((myhashsize != theirhashsize) ||
95                                 memcmp(myhash, theirhash, myhashsize))
96                 return -1;
97         return 0;
98 }