1 /* Copyright (c) 2007-2009, UNINETT AS */
2 /* See LICENSE for licensing information. */
5 #include <sys/inttypes.h>
11 #include <arpa/inet.h>
17 #include <openssl/hmac.h>
18 #include <openssl/rand.h>
20 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
22 void radmsg_free(struct radmsg *msg) {
24 freetlvlist(msg->attrs);
29 struct radmsg *radmsg_init(uint8_t code, uint8_t id, uint8_t *auth) {
32 msg = malloc(sizeof(struct radmsg));
35 memset(msg, 0, sizeof(struct radmsg));
36 msg->attrs = list_create();
44 memcpy(msg->auth, auth, 16);
45 else if (!RAND_bytes(msg->auth, 16)) {
52 int radmsg_add(struct radmsg *msg, struct tlv *attr) {
53 if (!msg || !msg->attrs)
57 return list_push(msg->attrs, attr);
60 /* returns first tlv of the given type */
61 struct tlv *radmsg_gettype(struct radmsg *msg, uint8_t type) {
62 struct list_node *node;
67 for (node = list_first(msg->attrs); node; node = list_next(node)) {
68 tlv = (struct tlv *)node->data;
75 int _checkmsgauth(unsigned char *rad, uint8_t *authattr, uint8_t *secret) {
76 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
77 static unsigned char first = 1;
78 static HMAC_CTX hmacctx;
80 uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
82 pthread_mutex_lock(&lock);
84 HMAC_CTX_init(&hmacctx);
88 memcpy(auth, authattr, 16);
89 memset(authattr, 0, 16);
91 HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
92 HMAC_Update(&hmacctx, rad, RADLEN(rad));
93 HMAC_Final(&hmacctx, hash, &md_len);
94 memcpy(authattr, auth, 16);
96 debug(DBG_WARN, "message auth computation failed");
97 pthread_mutex_unlock(&lock);
101 if (memcmp(auth, hash, 16)) {
102 debug(DBG_WARN, "message authenticator, wrong value");
103 pthread_mutex_unlock(&lock);
107 pthread_mutex_unlock(&lock);
111 int _validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec) {
112 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
113 static unsigned char first = 1;
114 static EVP_MD_CTX mdctx;
115 unsigned char hash[EVP_MAX_MD_SIZE];
119 pthread_mutex_lock(&lock);
121 EVP_MD_CTX_init(&mdctx);
127 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
128 EVP_DigestUpdate(&mdctx, rad, 4) &&
129 EVP_DigestUpdate(&mdctx, reqauth, 16) &&
130 (len <= 20 || EVP_DigestUpdate(&mdctx, rad + 20, len - 20)) &&
131 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
132 EVP_DigestFinal_ex(&mdctx, hash, &len) &&
134 !memcmp(hash, rad + 4, 16));
135 pthread_mutex_unlock(&lock);
139 int _createmessageauth(unsigned char *rad, unsigned char *authattrval, uint8_t *secret) {
140 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
141 static unsigned char first = 1;
142 static HMAC_CTX hmacctx;
148 pthread_mutex_lock(&lock);
150 HMAC_CTX_init(&hmacctx);
154 memset(authattrval, 0, 16);
156 HMAC_Init_ex(&hmacctx, secret, strlen((char *)secret), EVP_md5(), NULL);
157 HMAC_Update(&hmacctx, rad, RADLEN(rad));
158 HMAC_Final(&hmacctx, authattrval, &md_len);
160 debug(DBG_WARN, "message auth computation failed");
161 pthread_mutex_unlock(&lock);
164 pthread_mutex_unlock(&lock);
168 int _radsign(unsigned char *rad, unsigned char *sec) {
169 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
170 static unsigned char first = 1;
171 static EVP_MD_CTX mdctx;
175 pthread_mutex_lock(&lock);
177 EVP_MD_CTX_init(&mdctx);
181 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
182 EVP_DigestUpdate(&mdctx, rad, RADLEN(rad)) &&
183 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
184 EVP_DigestFinal_ex(&mdctx, rad + 4, &md_len) &&
186 pthread_mutex_unlock(&lock);
190 uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret) {
191 struct list_node *node;
194 uint8_t *buf, *p, *msgauth = NULL;
196 if (!msg || !msg->attrs)
199 for (node = list_first(msg->attrs); node; node = list_next(node))
200 size += 2 + ((struct tlv *)node->data)->l;
210 *(uint16_t *)p = htons(size);
212 memcpy(p, msg->auth, 16);
215 for (node = list_first(msg->attrs); node; node = list_next(node)) {
216 tlv = (struct tlv *)node->data;
219 if (tlv->t == RAD_Attr_Message_Authenticator && secret)
223 if (msgauth && !_createmessageauth(buf, msgauth, secret)) {
228 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)) {
232 if (msg->code == RAD_Accounting_Request)
233 memcpy(msg->auth, buf + 4, 16);
238 /* if secret set we also validate message authenticator if present */
239 struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, uint8_t *rqauth) {
241 uint8_t t, l, *v = NULL, *p, auth[16];
249 if (secret && buf[0] == RAD_Accounting_Request) {
251 if (!_validauth(buf, auth, secret)) {
252 debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
257 if (rqauth && !_validauth(buf, rqauth, secret)) {
258 debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
262 msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
267 while (p - buf + 2 <= len) {
271 debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
277 if (p - buf + l > len) {
278 debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
286 if (t == RAD_Attr_Message_Authenticator && secret) {
288 memcpy(buf + 4, rqauth, 16);
289 if (l != 16 || !_checkmsgauth(buf, v, secret)) {
290 debug(DBG_WARN, "buf2radmsg: message authentication failed");
292 memcpy(buf + 4, msg->auth, 16);
297 memcpy(buf + 4, msg->auth, 16);
298 debug(DBG_DBG, "buf2radmsg: message auth ok");
301 attr = maketlv(t, l, v);
302 if (!attr || !radmsg_add(msg, attr)) {
311 /* Local Variables: */
312 /* c-file-style: "stroustrup" */