Need some help. Have Java Springboot API and ma using TinyRadius to authenticate against Radius Server. Below works fine without passing Message-Authenticator
AccessRequest ar = new AccessRequest(userid, passcode);
ar.setAuthProtocol(AccessRequest.AUTH_PAP);
RadiusPacket response = rc.authenticate(ar);
....
....
I need to send Message-Authenticator attribute and need some help on how do I go about doing that. I tried below it’s not working always comes back with timeout it’s not liking message-authenticator I am passing, don’t know if I am even generating the Message-Authenticator correctly
import org.tinyradius.packet.RadiusPacket;
import org.tinyradius.packet.AccessRequest;
import org.tinyradius.util.RadiusClient;
import org.tinyradius.attribute.RadiusAttribute;
....
.........
byte[] secret = mySecretValue.getBytes();
RadiusPacket response = null;
try {
byte[] authenticator = MessageAuthenticator.generateAuthenticator(ar, secret);
ar.addAttribute(new
RadiusAttribute(RadiusAttributeType.MESSAGE_AUTHENTICATOR.getValue(), authenticator));
response = rc.authenticate(ar);
} catch (Exception e) {
e.printStackTrace();
}
Here is generateAuthenticator implementation
public static byte[] generateAuthenticator(RadiusPacket packet, byte[] secret) throws Exception {
String HMAC_MD5 = "HmacMD5";
int AUTHENTICATOR_LENGTH = 16;
Mac mac = Mac.getInstance(HMAC_MD5);
mac.init(new SecretKeySpec(secret, HMAC_MD5));
mac.update((byte) packet.getPacketType());
return mac.doFinal();
}
//also tried below with noluck
public static byte[] generateAuthenticator2(RadiusPacket packet, byte[] secret, int len) throws Exception {
MessageDigest md5=getMd5Digest();
md5.reset();
byte[] requestAuthenticator=new byte[16];
Random r=new Random();
for(int i=0;i<16;i++){
requestAuthenticator[i]=(byte)r.nextInt();
}
md5.update(secret,0,len);
md5.update(requestAuthenticator,0,requestAuthenticator.length);
return md5.digest();
}
Any help is appreciated.
Thanks.