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