2 * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
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.
10 #include <sys/inttypes.h>
16 #include <arpa/inet.h>
22 #include <openssl/hmac.h>
23 #include <openssl/rand.h>
25 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
27 void radmsg_free(struct radmsg *msg) {
29 freetlvlist(msg->attrs);
34 struct radmsg *radmsg_init(uint8_t code, uint8_t id, uint8_t *auth) {
37 msg = malloc(sizeof(struct radmsg));
40 memset(msg, 0, sizeof(struct radmsg));
41 msg->attrs = list_create();
49 memcpy(msg->auth, auth, 16);
50 else if (!RAND_bytes(msg->auth, 16)) {
57 int radmsg_add(struct radmsg *msg, struct tlv *attr) {
58 if (!msg || !msg->attrs)
62 return list_push(msg->attrs, attr);
65 /** Return a new list with all tlv's in \a msg of type \a type. The
66 * caller is responsible for freeing the list by calling \a
69 radmsg_getalltype(const struct radmsg *msg, uint8_t type)
71 struct list *list = NULL;
72 struct list_node *node = NULL;
74 if (!msg || !msg->attrs)
80 for (node = list_first(msg->attrs); node; node = list_next(node))
81 if (((struct tlv *) node->data)->t == type)
82 if (list_push(list, node->data) != 1) {
89 /* returns first tlv of the given type */
90 struct tlv *radmsg_gettype(struct radmsg *msg, uint8_t type) {
91 struct list_node *node;
96 for (node = list_first(msg->attrs); node; node = list_next(node)) {
97 tlv = (struct tlv *)node->data;
104 /** Copy all attributes of type \a type from \a src to \a dst.
106 * If all attributes were copied successfully, the number of
107 * attributes copied is returned.
109 * If copying failed, a negative number is returned. */
110 int radmsg_copy_attrs(struct radmsg *dst,
111 const struct radmsg *src,
114 struct list_node *node = NULL;
115 struct list *list = radmsg_getalltype(src, type);
118 for (node = list_first(list); node; node = list_next(node)) {
119 if (radmsg_add(dst, copytlv((struct tlv *) node->data)) != 1) {
129 int _checkmsgauth(unsigned char *rad, uint8_t *authattr, uint8_t *secret) {
130 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
131 static unsigned char first = 1;
132 static HMAC_CTX hmacctx;
134 uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
136 pthread_mutex_lock(&lock);
138 HMAC_CTX_init(&hmacctx);
142 memcpy(auth, authattr, 16);
143 memset(authattr, 0, 16);
145 HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
146 HMAC_Update(&hmacctx, rad, RADLEN(rad));
147 HMAC_Final(&hmacctx, hash, &md_len);
148 memcpy(authattr, auth, 16);
150 debug(DBG_WARN, "message auth computation failed");
151 pthread_mutex_unlock(&lock);
155 if (memcmp(auth, hash, 16)) {
156 debug(DBG_WARN, "message authenticator, wrong value");
157 pthread_mutex_unlock(&lock);
161 pthread_mutex_unlock(&lock);
165 int _validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec) {
166 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
167 static unsigned char first = 1;
168 static EVP_MD_CTX mdctx;
169 unsigned char hash[EVP_MAX_MD_SIZE];
173 pthread_mutex_lock(&lock);
175 EVP_MD_CTX_init(&mdctx);
181 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
182 EVP_DigestUpdate(&mdctx, rad, 4) &&
183 EVP_DigestUpdate(&mdctx, reqauth, 16) &&
184 (len <= 20 || EVP_DigestUpdate(&mdctx, rad + 20, len - 20)) &&
185 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
186 EVP_DigestFinal_ex(&mdctx, hash, &len) &&
188 !memcmp(hash, rad + 4, 16));
189 pthread_mutex_unlock(&lock);
193 int _createmessageauth(unsigned char *rad, unsigned char *authattrval, uint8_t *secret) {
194 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
195 static unsigned char first = 1;
196 static HMAC_CTX hmacctx;
202 pthread_mutex_lock(&lock);
204 HMAC_CTX_init(&hmacctx);
208 memset(authattrval, 0, 16);
210 HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
211 HMAC_Update(&hmacctx, rad, RADLEN(rad));
212 HMAC_Final(&hmacctx, authattrval, &md_len);
214 debug(DBG_WARN, "message auth computation failed");
215 pthread_mutex_unlock(&lock);
218 pthread_mutex_unlock(&lock);
222 int _radsign(unsigned char *rad, unsigned char *sec) {
223 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
224 static unsigned char first = 1;
225 static EVP_MD_CTX mdctx;
229 pthread_mutex_lock(&lock);
231 EVP_MD_CTX_init(&mdctx);
235 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
236 EVP_DigestUpdate(&mdctx, rad, RADLEN(rad)) &&
237 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
238 EVP_DigestFinal_ex(&mdctx, rad + 4, &md_len) &&
240 pthread_mutex_unlock(&lock);
244 uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret) {
245 struct list_node *node;
248 uint8_t *buf, *p, *msgauth = NULL;
250 if (!msg || !msg->attrs)
253 for (node = list_first(msg->attrs); node; node = list_next(node))
254 size += 2 + ((struct tlv *)node->data)->l;
264 *(uint16_t *)p = htons(size);
266 memcpy(p, msg->auth, 16);
269 for (node = list_first(msg->attrs); node; node = list_next(node)) {
270 tlv = (struct tlv *)node->data;
273 if (tlv->t == RAD_Attr_Message_Authenticator && secret)
277 if (msgauth && !_createmessageauth(buf, msgauth, secret)) {
282 if ((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)) {
286 if (msg->code == RAD_Accounting_Request)
287 memcpy(msg->auth, buf + 4, 16);
292 /* if secret set we also validate message authenticator if present */
293 struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, uint8_t *rqauth) {
295 uint8_t t, l, *v = NULL, *p, auth[16];
303 if (secret && buf[0] == RAD_Accounting_Request) {
305 if (!_validauth(buf, auth, secret)) {
306 debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
311 if (rqauth && !_validauth(buf, rqauth, secret)) {
312 debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
316 msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
321 while (p - buf + 2 <= len) {
325 debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
331 if (p - buf + l > len) {
332 debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
340 if (t == RAD_Attr_Message_Authenticator && secret) {
342 memcpy(buf + 4, rqauth, 16);
343 if (l != 16 || !_checkmsgauth(buf, v, secret)) {
344 debug(DBG_WARN, "buf2radmsg: message authentication failed");
346 memcpy(buf + 4, msg->auth, 16);
351 memcpy(buf + 4, msg->auth, 16);
352 debug(DBG_DBG, "buf2radmsg: message auth ok");
355 attr = maketlv(t, l, v);
356 if (!attr || !radmsg_add(msg, attr)) {
365 /* Local Variables: */
366 /* c-file-style: "stroustrup" */