Blame SOURCES/dhcp-omshell-hmac-sha512-support.patch

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