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.
12 #include <arpa/inet.h>
18 #include <openssl/hmac.h>
19 #include <openssl/rand.h>
21 #define RADLEN(x) ntohs(((uint16_t *)(x))[1])
23 void radmsg_free(struct radmsg *msg) {
25 freetlvlist(msg->attrs);
30 struct radmsg *radmsg_init(uint8_t code, uint8_t id, uint8_t *auth) {
33 msg = malloc(sizeof(struct radmsg));
36 memset(msg, 0, sizeof(struct radmsg));
37 msg->attrs = list_create();
45 memcpy(msg->auth, auth, 16);
46 else if (!RAND_bytes(msg->auth, 16)) {
53 int radmsg_add(struct radmsg *msg, struct tlv *attr) {
54 if (!msg || !msg->attrs)
58 return list_push(msg->attrs, attr);
61 /* returns first tlv of the given type */
62 struct tlv *radmsg_gettype(struct radmsg *msg, uint8_t type) {
63 struct list_node *node;
68 for (node = list_first(msg->attrs); node; node = list_next(node)) {
69 tlv = (struct tlv *)node->data;
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;
81 uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
83 pthread_mutex_lock(&lock);
85 HMAC_CTX_init(&hmacctx);
89 memcpy(auth, authattr, 16);
90 memset(authattr, 0, 16);
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);
97 debug(DBG_WARN, "message auth computation failed");
98 pthread_mutex_unlock(&lock);
102 if (memcmp(auth, hash, 16)) {
103 debug(DBG_WARN, "message authenticator, wrong value");
104 pthread_mutex_unlock(&lock);
108 pthread_mutex_unlock(&lock);
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];
120 pthread_mutex_lock(&lock);
122 EVP_MD_CTX_init(&mdctx);
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) &&
135 !memcmp(hash, rad + 4, 16));
136 pthread_mutex_unlock(&lock);
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;
149 pthread_mutex_lock(&lock);
151 HMAC_CTX_init(&hmacctx);
155 memset(authattrval, 0, 16);
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);
161 debug(DBG_WARN, "message auth computation failed");
162 pthread_mutex_unlock(&lock);
165 pthread_mutex_unlock(&lock);
169 uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret) {
170 struct list_node *node;
173 uint8_t *buf, *p, *msgauth = NULL;
175 if (!msg || !msg->attrs)
178 for (node = list_first(msg->attrs); node; node = list_next(node))
179 size += 2 + ((struct tlv *)node->data)->l;
189 *(uint16_t *)p = htons(size);
191 memcpy(p, msg->auth, 16);
194 for (node = list_first(msg->attrs); node; node = list_next(node)) {
195 tlv = (struct tlv *)node->data;
198 if (tlv->t == RAD_Attr_Message_Authenticator && secret)
202 if (msgauth && !_createmessageauth(buf, msgauth, secret)) {
209 /* if secret set we also validate message authenticator if present */
210 struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, uint8_t *rqauth) {
212 uint8_t t, l, *v, *p, auth[16];
220 if (secret && buf[0] == RAD_Accounting_Request) {
222 if (!_validauth(buf, auth, secret)) {
223 debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
228 if (rqauth && !_validauth(buf, rqauth, secret)) {
229 debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
233 msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
238 while (p - buf + 2 <= len) {
242 debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
248 if (p - buf + l > len) {
249 debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
257 if (t == RAD_Attr_Message_Authenticator && secret) {
259 memcpy(buf + 4, rqauth, 16);
260 if (l != 16 || !_checkmsgauth(buf, v, secret)) {
261 debug(DBG_WARN, "buf2radmsg: message authentication failed");
263 memcpy(buf + 4, msg->auth, 16);
268 memcpy(buf + 4, msg->auth, 16);
269 debug(DBG_DBG, "buf2radmsg: message auth ok");
272 attr = maketlv(t, l, v);
273 if (!attr || !radmsg_add(msg, attr)) {