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 /* returns first tlv of the given type */
66 struct tlv *radmsg_gettype(struct radmsg *msg, uint8_t type) {
67 struct list_node *node;
72 for (node = list_first(msg->attrs); node; node = list_next(node)) {
73 tlv = (struct tlv *)node->data;
80 int _checkmsgauth(unsigned char *rad, uint8_t *authattr, uint8_t *secret) {
81 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
82 static unsigned char first = 1;
83 static HMAC_CTX hmacctx;
85 uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
87 pthread_mutex_lock(&lock);
89 HMAC_CTX_init(&hmacctx);
93 memcpy(auth, authattr, 16);
94 memset(authattr, 0, 16);
96 HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
97 HMAC_Update(&hmacctx, rad, RADLEN(rad));
98 HMAC_Final(&hmacctx, hash, &md_len);
99 memcpy(authattr, auth, 16);
101 debug(DBG_WARN, "message auth computation failed");
102 pthread_mutex_unlock(&lock);
106 if (memcmp(auth, hash, 16)) {
107 debug(DBG_WARN, "message authenticator, wrong value");
108 pthread_mutex_unlock(&lock);
112 pthread_mutex_unlock(&lock);
116 int _validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec) {
117 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
118 static unsigned char first = 1;
119 static EVP_MD_CTX mdctx;
120 unsigned char hash[EVP_MAX_MD_SIZE];
124 pthread_mutex_lock(&lock);
126 EVP_MD_CTX_init(&mdctx);
132 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
133 EVP_DigestUpdate(&mdctx, rad, 4) &&
134 EVP_DigestUpdate(&mdctx, reqauth, 16) &&
135 (len <= 20 || EVP_DigestUpdate(&mdctx, rad + 20, len - 20)) &&
136 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
137 EVP_DigestFinal_ex(&mdctx, hash, &len) &&
139 !memcmp(hash, rad + 4, 16));
140 pthread_mutex_unlock(&lock);
144 int _createmessageauth(unsigned char *rad, unsigned char *authattrval, uint8_t *secret) {
145 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
146 static unsigned char first = 1;
147 static HMAC_CTX hmacctx;
153 pthread_mutex_lock(&lock);
155 HMAC_CTX_init(&hmacctx);
159 memset(authattrval, 0, 16);
161 HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
162 HMAC_Update(&hmacctx, rad, RADLEN(rad));
163 HMAC_Final(&hmacctx, authattrval, &md_len);
165 debug(DBG_WARN, "message auth computation failed");
166 pthread_mutex_unlock(&lock);
169 pthread_mutex_unlock(&lock);
173 int _radsign(unsigned char *rad, unsigned char *sec) {
174 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
175 static unsigned char first = 1;
176 static EVP_MD_CTX mdctx;
180 pthread_mutex_lock(&lock);
182 EVP_MD_CTX_init(&mdctx);
186 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
187 EVP_DigestUpdate(&mdctx, rad, RADLEN(rad)) &&
188 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
189 EVP_DigestFinal_ex(&mdctx, rad + 4, &md_len) &&
191 pthread_mutex_unlock(&lock);
195 uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret) {
196 struct list_node *node;
199 uint8_t *buf, *p, *msgauth = NULL;
201 if (!msg || !msg->attrs)
204 for (node = list_first(msg->attrs); node; node = list_next(node))
205 size += 2 + ((struct tlv *)node->data)->l;
215 *(uint16_t *)p = htons(size);
217 memcpy(p, msg->auth, 16);
220 for (node = list_first(msg->attrs); node; node = list_next(node)) {
221 tlv = (struct tlv *)node->data;
224 if (tlv->t == RAD_Attr_Message_Authenticator && secret)
228 if (msgauth && !_createmessageauth(buf, msgauth, secret)) {
233 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)) {
237 if (msg->code == RAD_Accounting_Request)
238 memcpy(msg->auth, buf + 4, 16);
243 /* if secret set we also validate message authenticator if present */
244 struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, uint8_t *rqauth) {
246 uint8_t t, l, *v = NULL, *p, auth[16];
254 if (secret && buf[0] == RAD_Accounting_Request) {
256 if (!_validauth(buf, auth, secret)) {
257 debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
262 if (rqauth && !_validauth(buf, rqauth, secret)) {
263 debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
267 msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
272 while (p - buf + 2 <= len) {
276 debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
282 if (p - buf + l > len) {
283 debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
291 if (t == RAD_Attr_Message_Authenticator && secret) {
293 memcpy(buf + 4, rqauth, 16);
294 if (l != 16 || !_checkmsgauth(buf, v, secret)) {
295 debug(DBG_WARN, "buf2radmsg: message authentication failed");
297 memcpy(buf + 4, msg->auth, 16);
302 memcpy(buf + 4, msg->auth, 16);
303 debug(DBG_DBG, "buf2radmsg: message auth ok");
306 attr = maketlv(t, l, v);
307 if (!attr || !radmsg_add(msg, attr)) {