--- freeradius-1.0.4/src/lib/radius.c	Sun Dec 19 21:11:54 2004
+++ freeradius-1.0.4-bmork/src/lib/radius.c	Fri Aug 26 11:15:03 2005
@@ -488,8 +488,6 @@
 
 			  case PW_TYPE_INTEGER:
 			  case PW_TYPE_IPADDR:
-				  *length_ptr += 4;
-				  if (vsa_length_ptr) *vsa_length_ptr += 4;
 
 				  if (reply->type == PW_TYPE_INTEGER ) {
 				          /*  If tagged, the tag becomes the MSB of the value */
@@ -510,9 +508,23 @@
 					   */
 					  lvalue = reply->lvalue;
 				  }
-				  memcpy(ptr, &lvalue, 4);
-				  ptr += 4;
-				  total_length += 4;
+
+				  len = 4;
+				  if (reply->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD) {
+					  if (!original) {
+						  librad_log("ERROR: No request packet, cannot encrypt Tunnel-Password attribute in the reply.");
+						  return -1;
+					  }
+					  memcpy(reply->strvalue, &lvalue, len); /* NOTE: destroys the original text attribute */
+					  rad_tunnel_pwencode(reply->strvalue, &len, secret, original->vector);
+					  memcpy(ptr, reply->strvalue, len);
+				  } else {
+					  memcpy(ptr, &lvalue, len);
+				  }
+				  *length_ptr += len;
+				  if (vsa_length_ptr) *vsa_length_ptr += len;
+				  ptr += len;
+				  total_length += len;
 				  break;
 
 				  /*
