various code improvements
[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 uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret) {
170     struct list_node *node;
171     struct tlv *tlv;
172     int size;
173     uint8_t *buf, *p, *msgauth = NULL;
174
175     if (!msg || !msg->attrs)
176         return NULL;
177     size = 20;
178     for (node = list_first(msg->attrs); node; node = list_next(node))
179         size += 2 + ((struct tlv *)node->data)->l;
180     if (size > 65535)
181         return NULL;
182     buf = malloc(size);
183     if (!buf)
184         return NULL;
185     
186     p = buf;
187     *p++ = msg->code;
188     *p++ = msg->id;
189     *(uint16_t *)p = htons(size);
190     p += 2;
191     memcpy(p, msg->auth, 16);
192     p += 16;
193
194     for (node = list_first(msg->attrs); node; node = list_next(node)) {
195         tlv = (struct tlv *)node->data;
196         p = tlv2buf(p, tlv);
197         p[-1] += 2;
198         if (tlv->t == RAD_Attr_Message_Authenticator && secret)
199             msgauth = p;
200         p += tlv->l;
201     }
202     if (msgauth && !_createmessageauth(buf, msgauth, secret)) {
203         free(buf);
204         return NULL;
205     }
206     return buf;
207 }
208
209 /* if secret set we also validate message authenticator if present */
210 struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, uint8_t *rqauth) {
211     struct radmsg *msg;
212     uint8_t t, l, *v, *p, auth[16];
213     uint16_t len;
214     struct tlv *attr;
215     
216     len = RADLEN(buf);
217     if (len < 20)
218         return NULL;
219
220     if (secret && buf[0] == RAD_Accounting_Request) {
221         memset(auth, 0, 16);
222         if (!_validauth(buf, auth, secret)) {
223             debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
224             return NULL;
225         }
226     }
227
228     if (rqauth && !_validauth(buf, rqauth, secret)) {
229         debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
230         return NULL;
231     }
232         
233     msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
234     if (!msg)
235         return NULL;
236
237     p = buf + 20;
238     while (p - buf + 2 <= len) {
239         t = *p++;
240         l = *p++;
241         if (l < 2) {
242             debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
243             radmsg_free(msg);
244             return NULL;
245         }
246         l -= 2;
247         if (l) {
248             if (p - buf + l > len) {
249                 debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
250                 radmsg_free(msg);
251                 return NULL;
252             }
253             v = p;
254             p += l;
255         }
256         
257         if (t == RAD_Attr_Message_Authenticator && secret) {
258             if (rqauth)
259                 memcpy(buf + 4, rqauth, 16);
260             if (l != 16 || !_checkmsgauth(buf, v, secret)) {
261                 debug(DBG_WARN, "buf2radmsg: message authentication failed");
262                 if (rqauth)
263                     memcpy(buf + 4, msg->auth, 16);
264                 radmsg_free(msg);
265                 return NULL;
266             }
267             if (rqauth)
268                 memcpy(buf + 4, msg->auth, 16);
269             debug(DBG_DBG, "buf2radmsg: message auth ok");
270         }
271
272         attr = maketlv(t, l, v);
273         if (!attr || !radmsg_add(msg, attr)) {
274             freetlv(attr);
275             radmsg_free(msg);
276             return NULL;
277         }
278     }
279     return msg;
280 }