19c97e
diff --git a/omapip/connection.c b/omapip/connection.c
19c97e
index 014ff21..6800514 100644
19c97e
--- a/omapip/connection.c
19c97e
+++ b/omapip/connection.c
19c97e
@@ -44,6 +44,9 @@ extern omapi_array_t *trace_listeners;
19c97e
 #endif
19c97e
 static isc_result_t omapi_connection_connect_internal (omapi_object_t *);
19c97e
 
19c97e
+static isc_result_t ctring_from_attribute(omapi_object_t *obj, char *attr_name,
19c97e
+                                          char **cstr);
19c97e
+
19c97e
 OMAPI_OBJECT_ALLOC (omapi_connection,
19c97e
 		    omapi_connection_object_t, omapi_type_connection)
19c97e
 
19c97e
@@ -763,64 +766,41 @@ isc_result_t omapi_connection_reaper (omapi_object_t *h)
19c97e
 }
19c97e
 
19c97e
 static isc_result_t make_dst_key (dst_key_t **dst_key, omapi_object_t *a) {
19c97e
-	omapi_value_t *name      = (omapi_value_t *)0;
19c97e
-	omapi_value_t *algorithm = (omapi_value_t *)0;
19c97e
-	omapi_value_t *key       = (omapi_value_t *)0;
19c97e
-	char *name_str = NULL;
19c97e
+	omapi_value_t *key = 0;
19c97e
+	char *name_str = 0;
19c97e
+	char *algorithm_str = 0;
19c97e
 	isc_result_t status = ISC_R_SUCCESS;
19c97e
 
19c97e
-	if (status == ISC_R_SUCCESS)
19c97e
-		status = omapi_get_value_str
19c97e
-			(a, (omapi_object_t *)0, "name", &name);
19c97e
-
19c97e
-	if (status == ISC_R_SUCCESS)
19c97e
-		status = omapi_get_value_str
19c97e
-			(a, (omapi_object_t *)0, "algorithm", &algorithm);
19c97e
-
19c97e
-	if (status == ISC_R_SUCCESS)
19c97e
-		status = omapi_get_value_str
19c97e
-			(a, (omapi_object_t *)0, "key", &key);
19c97e
-
19c97e
+	/* Get the key name as a C string. */
19c97e
+	status = ctring_from_attribute(a, "name", &name_str);
19c97e
 	if (status == ISC_R_SUCCESS) {
19c97e
-		if ((algorithm->value->type != omapi_datatype_data &&
19c97e
-		     algorithm->value->type != omapi_datatype_string) ||
19c97e
-		    strncasecmp((char *)algorithm->value->u.buffer.value,
19c97e
-				NS_TSIG_ALG_HMAC_MD5 ".",
19c97e
-				algorithm->value->u.buffer.len) != 0) {
19c97e
-			status = DHCP_R_INVALIDARG;
19c97e
+		/* Get the algorithm name as a C string. */
19c97e
+		status = ctring_from_attribute(a, "algorithm", &algorithm_str);
19c97e
+		if (status == ISC_R_SUCCESS) {
19c97e
+			/* Get the key secret value */
19c97e
+			status = omapi_get_value_str(a, 0, "key", &key);
19c97e
+			if (status == ISC_R_SUCCESS) {
19c97e
+				/* Now let's try and create the key */
19c97e
+				status = isclib_make_dst_key(
19c97e
+						name_str,
19c97e
+						algorithm_str,
19c97e
+						key->value->u.buffer.value,
19c97e
+						key->value->u.buffer.len,
19c97e
+						dst_key);
19c97e
+
19c97e
+				if (*dst_key == NULL) {
19c97e
+					status = ISC_R_NOMEMORY;
19c97e
+				}
19c97e
+			}
19c97e
 		}
19c97e
 	}
19c97e
 
19c97e
-	if (status == ISC_R_SUCCESS) {
19c97e
-		name_str = dmalloc (name -> value -> u.buffer.len + 1, MDL);
19c97e
-		if (!name_str)
19c97e
-			status = ISC_R_NOMEMORY;
19c97e
-	}
19c97e
-
19c97e
-	if (status == ISC_R_SUCCESS) {
19c97e
-		memcpy (name_str,
19c97e
-			name -> value -> u.buffer.value,
19c97e
-			name -> value -> u.buffer.len);
19c97e
-		name_str [name -> value -> u.buffer.len] = 0;
19c97e
-
19c97e
-		status = isclib_make_dst_key(name_str,
19c97e
-					     DHCP_HMAC_MD5_NAME,
19c97e
-					     key->value->u.buffer.value,
19c97e
-					     key->value->u.buffer.len,
19c97e
-					     dst_key);
19c97e
-
19c97e
-		if (*dst_key == NULL)
19c97e
-			status = ISC_R_NOMEMORY;
19c97e
-	}
19c97e
-
19c97e
 	if (name_str)
19c97e
 		dfree (name_str, MDL);
19c97e
+	if (algorithm_str)
19c97e
+		dfree (algorithm_str, MDL);
19c97e
 	if (key)
19c97e
 		omapi_value_dereference (&key, MDL);
19c97e
-	if (algorithm)
19c97e
-		omapi_value_dereference (&algorithm, MDL);
19c97e
-	if (name)
19c97e
-		omapi_value_dereference (&name, MDL);
19c97e
 
19c97e
 	return status;
19c97e
 }
19c97e
@@ -1103,3 +1083,50 @@ isc_result_t omapi_connection_stuff_values (omapi_object_t *c,
19c97e
 								m -> inner);
19c97e
 	return ISC_R_SUCCESS;
19c97e
 }
19c97e
+
19c97e
+/* @brief Fetches the value of an attribute in an object as an allocated
19c97e
+ * C string
19c97e
+ *
19c97e
+ * @param obj ompapi object containing the desire attribute
19c97e
+ * @param attr_name  name of the desired attribute
19c97e
+ * @param[out] cstr pointer in which to place the allocated C string's address
19c97e
+ *
19c97e
+ * Caller is responsible for freeing (via dfree) the allocated string.
19c97e
+ *
19c97e
+ * @return ISC_R_SUCCESS if successful, otherwise indicates the type of failure
19c97e
+*/
19c97e
+static isc_result_t ctring_from_attribute(omapi_object_t *obj, char *attr_name,
19c97e
+                                          char **cstr) {
19c97e
+	isc_result_t status = ISC_R_SUCCESS;
19c97e
+	omapi_value_t *attr = 0;
19c97e
+
19c97e
+	/* Find the attribute in the object. */
19c97e
+	status = omapi_get_value_str(obj, (omapi_object_t *)0, attr_name,
19c97e
+                                 &attr);
19c97e
+	if (status != ISC_R_SUCCESS) {
19c97e
+		return (status);
19c97e
+	}
19c97e
+
19c97e
+	/* Got it, let's make sure it's either data or string type. */
19c97e
+	if (attr->value->type != omapi_datatype_data &&
19c97e
+            attr->value->type != omapi_datatype_string) {
19c97e
+		return (DHCP_R_INVALIDARG);
19c97e
+        }
19c97e
+
19c97e
+	/* Make a C string from the attribute value. */
19c97e
+	*cstr = dmalloc (attr->value->u.buffer.len + 1, MDL);
19c97e
+	if (!(*cstr)) {
19c97e
+		status = ISC_R_NOMEMORY;
19c97e
+        } else {
19c97e
+	        memcpy (*cstr, attr->value->u.buffer.value,
19c97e
+		            attr->value->u.buffer.len);
19c97e
+	        (*cstr)[attr->value->u.buffer.len] = 0;
19c97e
+	}
19c97e
+
19c97e
+	/* Get rid of the attribute reference */
19c97e
+	if (attr) {
19c97e
+		omapi_value_dereference (&attr, MDL);
19c97e
+	}
19c97e
+
19c97e
+	return (status);
19c97e
+}