trying to avoid some silly compiler warnings
[libradsec.git] / radmsg.c
1 /*
2  * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <arpa/inet.h>
13 #include "list.h"
14 #include "tlv11.h"
15 #include "radmsg.h"
16 #include "debug.h"
17 #include <pthread.h>
18 #include <openssl/hmac.h>
19 #include <openssl/rand.h>
20
21 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
22
23 void radmsg_free(struct radmsg *msg) {
24     if (msg) {
25         freetlvlist(msg->attrs);
26         free(msg);
27     }
28 }
29
30 struct radmsg *radmsg_init(uint8_t code, uint8_t id, uint8_t *auth) {
31     struct radmsg *msg;
32     
33     msg = malloc(sizeof(struct radmsg));
34     if (!msg)
35         return NULL;
36     memset(msg, 0, sizeof(struct radmsg));
37     msg->attrs = list_create();
38     if (!msg->attrs) {
39         free(msg);
40         return NULL;
41     }   
42     msg->code = code;
43     msg->id = id;
44     if (auth)
45         memcpy(msg->auth, auth, 16);
46     else if (!RAND_bytes(msg->auth, 16)) {
47         free(msg);
48         return NULL;
49     }   
50     return msg;
51 }
52
53 int radmsg_add(struct radmsg *msg, struct tlv *attr) {
54     if (!msg || !msg->attrs)
55         return 1;
56     if (!attr)
57         return 0;
58     return list_push(msg->attrs, attr);
59 }
60
61 /* returns first tlv of the given type */
62 struct tlv *radmsg_gettype(struct radmsg *msg, uint8_t type) {
63     struct list_node *node;
64     struct tlv *tlv;
65
66     if (!msg)
67         return NULL;
68     for (node = list_first(msg->attrs); node; node = list_next(node)) {
69         tlv = (struct tlv *)node->data;
70         if (tlv->t == type)
71             return tlv;
72     }
73     return NULL;
74 }
75
76 int _checkmsgauth(unsigned char *rad, uint8_t *authattr, uint8_t *secret) {
77     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
78     static unsigned char first = 1;
79     static HMAC_CTX hmacctx;
80     unsigned int md_len;
81     uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
82     
83     pthread_mutex_lock(&lock);
84     if (first) {
85         HMAC_CTX_init(&hmacctx);
86         first = 0;
87     }
88
89     memcpy(auth, authattr, 16);
90     memset(authattr, 0, 16);
91     md_len = 0;
92     HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
93     HMAC_Update(&hmacctx, rad, RADLEN(rad));
94     HMAC_Final(&hmacctx, hash, &md_len);
95     memcpy(authattr, auth, 16);
96     if (md_len != 16) {
97         debug(DBG_WARN, "message auth computation failed");
98         pthread_mutex_unlock(&lock);
99         return 0;
100     }
101
102     if (memcmp(auth, hash, 16)) {
103         debug(DBG_WARN, "message authenticator, wrong value");
104         pthread_mutex_unlock(&lock);
105         return 0;
106     }   
107         
108     pthread_mutex_unlock(&lock);
109     return 1;
110 }
111
112 int _validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec) {
113     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
114     static unsigned char first = 1;
115     static EVP_MD_CTX mdctx;
116     unsigned char hash[EVP_MAX_MD_SIZE];
117     unsigned int len;
118     int result;
119     
120     pthread_mutex_lock(&lock);
121     if (first) {
122         EVP_MD_CTX_init(&mdctx);
123         first = 0;
124     }
125
126     len = RADLEN(rad);
127     
128     result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
129               EVP_DigestUpdate(&mdctx, rad, 4) &&
130               EVP_DigestUpdate(&mdctx, reqauth, 16) &&
131               (len <= 20 || EVP_DigestUpdate(&mdctx, rad + 20, len - 20)) &&
132               EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
133               EVP_DigestFinal_ex(&mdctx, hash, &len) &&
134               len == 16 &&
135               !memcmp(hash, rad + 4, 16));
136     pthread_mutex_unlock(&lock);
137     return result;
138 }
139
140 int _createmessageauth(unsigned char *rad, unsigned char *authattrval, uint8_t *secret) {
141     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
142     static unsigned char first = 1;
143     static HMAC_CTX hmacctx;
144     unsigned int md_len;
145
146     if (!authattrval)
147         return 1;
148     
149     pthread_mutex_lock(&lock);
150     if (first) {
151         HMAC_CTX_init(&hmacctx);
152         first = 0;
153     }
154
155     memset(authattrval, 0, 16);
156     md_len = 0;
157     HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
158     HMAC_Update(&hmacctx, rad, RADLEN(rad));
159     HMAC_Final(&hmacctx, authattrval, &md_len);
160     if (md_len != 16) {
161         debug(DBG_WARN, "message auth computation failed");
162         pthread_mutex_unlock(&lock);
163         return 0;
164     }
165     pthread_mutex_unlock(&lock);
166     return 1;
167 }
168
169 int _radsign(unsigned char *rad, unsigned char *sec) {
170     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
171     static unsigned char first = 1;
172     static EVP_MD_CTX mdctx;
173     unsigned int md_len;
174     int result;
175
176     pthread_mutex_lock(&lock);
177     if (first) {
178         EVP_MD_CTX_init(&mdctx);
179         first = 0;
180     }
181
182     result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
183               EVP_DigestUpdate(&mdctx, rad, RADLEN(rad)) &&
184               EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
185               EVP_DigestFinal_ex(&mdctx, rad + 4, &md_len) &&
186               md_len == 16);
187     pthread_mutex_unlock(&lock);
188     return result;
189 }
190
191 uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret) {
192     struct list_node *node;
193     struct tlv *tlv;
194     int size;
195     uint8_t *buf, *p, *msgauth = NULL;
196
197     if (!msg || !msg->attrs)
198         return NULL;
199     size = 20;
200     for (node = list_first(msg->attrs); node; node = list_next(node))
201         size += 2 + ((struct tlv *)node->data)->l;
202     if (size > 65535)
203         return NULL;
204     buf = malloc(size);
205     if (!buf)
206         return NULL;
207     
208     p = buf;
209     *p++ = msg->code;
210     *p++ = msg->id;
211     *(uint16_t *)p = htons(size);
212     p += 2;
213     memcpy(p, msg->auth, 16);
214     p += 16;
215
216     for (node = list_first(msg->attrs); node; node = list_next(node)) {
217         tlv = (struct tlv *)node->data;
218         p = tlv2buf(p, tlv);
219         p[-1] += 2;
220         if (tlv->t == RAD_Attr_Message_Authenticator && secret)
221             msgauth = p;
222         p += tlv->l;
223     }
224     if (msgauth && !_createmessageauth(buf, msgauth, secret)) {
225         free(buf);
226         return NULL;
227     }
228     if (secret && (msg->code == RAD_Access_Accept || msg->code == RAD_Access_Reject || msg->code == RAD_Access_Challenge || msg->code == RAD_Accounting_Response || msg->code == RAD_Accounting_Request) && !_radsign(buf, secret)) {
229         free(buf);
230         return NULL;
231     }
232     return buf;
233 }
234
235 /* if secret set we also validate message authenticator if present */
236 struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, uint8_t *rqauth) {
237     struct radmsg *msg;
238     uint8_t t, l, *v = NULL, *p, auth[16];
239     uint16_t len;
240     struct tlv *attr;
241     
242     len = RADLEN(buf);
243     if (len < 20)
244         return NULL;
245
246     if (secret && buf[0] == RAD_Accounting_Request) {
247         memset(auth, 0, 16);
248         if (!_validauth(buf, auth, secret)) {
249             debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
250             return NULL;
251         }
252     }
253
254     if (rqauth && !_validauth(buf, rqauth, secret)) {
255         debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
256         return NULL;
257     }
258         
259     msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
260     if (!msg)
261         return NULL;
262
263     p = buf + 20;
264     while (p - buf + 2 <= len) {
265         t = *p++;
266         l = *p++;
267         if (l < 2) {
268             debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
269             radmsg_free(msg);
270             return NULL;
271         }
272         l -= 2;
273         if (l) {
274             if (p - buf + l > len) {
275                 debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
276                 radmsg_free(msg);
277                 return NULL;
278             }
279             v = p;
280             p += l;
281         }
282         
283         if (t == RAD_Attr_Message_Authenticator && secret) {
284             if (rqauth)
285                 memcpy(buf + 4, rqauth, 16);
286             if (l != 16 || !_checkmsgauth(buf, v, secret)) {
287                 debug(DBG_WARN, "buf2radmsg: message authentication failed");
288                 if (rqauth)
289                     memcpy(buf + 4, msg->auth, 16);
290                 radmsg_free(msg);
291                 return NULL;
292             }
293             if (rqauth)
294                 memcpy(buf + 4, msg->auth, 16);
295             debug(DBG_DBG, "buf2radmsg: message auth ok");
296         }
297
298         attr = maketlv(t, l, v);
299         if (!attr || !radmsg_add(msg, attr)) {
300             freetlv(attr);
301             radmsg_free(msg);
302             return NULL;
303         }
304     }
305     return msg;
306 }