xref: /netbsd/external/mpl/dhcp/dist/omapip/auth.c (revision 13df4856)
1*13df4856Schristos /*	$NetBSD: auth.c,v 1.3 2022/04/03 01:10:59 christos Exp $	*/
23965be93Schristos 
33965be93Schristos /* auth.c
43965be93Schristos 
53965be93Schristos    Subroutines having to do with authentication. */
63965be93Schristos 
73965be93Schristos /*
8*13df4856Schristos  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
93965be93Schristos  * Copyright (c) 1998-2003 by Internet Software Consortium
103965be93Schristos  *
113965be93Schristos  * This Source Code Form is subject to the terms of the Mozilla Public
123965be93Schristos  * License, v. 2.0. If a copy of the MPL was not distributed with this
133965be93Schristos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
143965be93Schristos  *
153965be93Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
163965be93Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
173965be93Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
183965be93Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
193965be93Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
203965be93Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
213965be93Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
223965be93Schristos  *
233965be93Schristos  *   Internet Systems Consortium, Inc.
24*13df4856Schristos  *   PO Box 360
25*13df4856Schristos  *   Newmarket, NH 03857 USA
263965be93Schristos  *   <info@isc.org>
273965be93Schristos  *   https://www.isc.org/
283965be93Schristos  *
293965be93Schristos  */
303965be93Schristos 
313965be93Schristos #include <sys/cdefs.h>
32*13df4856Schristos __RCSID("$NetBSD: auth.c,v 1.3 2022/04/03 01:10:59 christos Exp $");
333965be93Schristos 
343965be93Schristos #include "dhcpd.h"
353965be93Schristos 
363965be93Schristos #include <omapip/omapip_p.h>
373965be93Schristos 
383965be93Schristos OMAPI_OBJECT_ALLOC (omapi_auth_key, omapi_auth_key_t, omapi_type_auth_key)
393965be93Schristos typedef struct hash omapi_auth_hash_t;
403965be93Schristos HASH_FUNCTIONS_DECL (omapi_auth_key, const char *,
413965be93Schristos 		     omapi_auth_key_t, omapi_auth_hash_t)
423965be93Schristos omapi_auth_hash_t *auth_key_hash;
HASH_FUNCTIONS(omapi_auth_key,const char *,omapi_auth_key_t,omapi_auth_hash_t,omapi_auth_key_reference,omapi_auth_key_dereference,do_case_hash)433965be93Schristos HASH_FUNCTIONS (omapi_auth_key, const char *, omapi_auth_key_t,
443965be93Schristos 		omapi_auth_hash_t,
453965be93Schristos 		omapi_auth_key_reference, omapi_auth_key_dereference,
463965be93Schristos 		do_case_hash)
473965be93Schristos 
483965be93Schristos isc_result_t omapi_auth_key_new (omapi_auth_key_t **o, const char *file,
493965be93Schristos 				 int line)
503965be93Schristos {
513965be93Schristos 	return omapi_auth_key_allocate (o, file, line);
523965be93Schristos }
533965be93Schristos 
omapi_auth_key_destroy(omapi_object_t * h,const char * file,int line)543965be93Schristos isc_result_t omapi_auth_key_destroy (omapi_object_t *h,
553965be93Schristos 				     const char *file, int line)
563965be93Schristos {
573965be93Schristos 	omapi_auth_key_t *a;
583965be93Schristos 
593965be93Schristos 	if (h->type != omapi_type_auth_key)
603965be93Schristos 		return DHCP_R_INVALIDARG;
613965be93Schristos 	a = (omapi_auth_key_t *)h;
623965be93Schristos 
633965be93Schristos 	if (auth_key_hash != NULL)
643965be93Schristos 		omapi_auth_key_hash_delete(auth_key_hash, a->name, 0, MDL);
653965be93Schristos 
663965be93Schristos 	if (a->name != NULL)
673965be93Schristos 		dfree(a->name, MDL);
683965be93Schristos 	if (a->algorithm != NULL)
693965be93Schristos 		dfree(a->algorithm, MDL);
703965be93Schristos 	if (a->key != NULL)
713965be93Schristos 		omapi_data_string_dereference(&a->key, MDL);
723965be93Schristos 	if (a->tsec_key != NULL)
733965be93Schristos 		dns_tsec_destroy(&a->tsec_key);
743965be93Schristos 
753965be93Schristos 	return ISC_R_SUCCESS;
763965be93Schristos }
773965be93Schristos 
omapi_auth_key_enter(omapi_auth_key_t * a)783965be93Schristos isc_result_t omapi_auth_key_enter (omapi_auth_key_t *a)
793965be93Schristos {
803965be93Schristos 	omapi_auth_key_t *tk;
813965be93Schristos 	isc_result_t      status;
823965be93Schristos 	dst_key_t        *dstkey;
833965be93Schristos 
843965be93Schristos 	if (a -> type != omapi_type_auth_key)
853965be93Schristos 		return DHCP_R_INVALIDARG;
863965be93Schristos 
873965be93Schristos 	tk = (omapi_auth_key_t *)0;
883965be93Schristos 	if (auth_key_hash) {
893965be93Schristos 		omapi_auth_key_hash_lookup (&tk, auth_key_hash,
903965be93Schristos 					    a -> name, 0, MDL);
913965be93Schristos 		if (tk == a) {
923965be93Schristos 			omapi_auth_key_dereference (&tk, MDL);
933965be93Schristos 			return ISC_R_SUCCESS;
943965be93Schristos 		}
953965be93Schristos 		if (tk) {
963965be93Schristos 			omapi_auth_key_hash_delete (auth_key_hash,
973965be93Schristos 						    tk -> name, 0, MDL);
983965be93Schristos 			omapi_auth_key_dereference (&tk, MDL);
993965be93Schristos 		}
1003965be93Schristos 	} else {
1013965be93Schristos 		if (!omapi_auth_key_new_hash(&auth_key_hash,
1023965be93Schristos 					     KEY_HASH_SIZE, MDL))
1033965be93Schristos 			return ISC_R_NOMEMORY;
1043965be93Schristos 	}
1053965be93Schristos 
1063965be93Schristos 	/*
1073965be93Schristos 	 * If possible create a tsec structure for this key,
1083965be93Schristos 	 * if we can't create the structure we put out a warning
1093965be93Schristos 	 * and continue.
1103965be93Schristos 	 */
1113965be93Schristos 	status = isclib_make_dst_key(a->name, a->algorithm,
1123965be93Schristos 				     a->key->value, a->key->len,
1133965be93Schristos 				     &dstkey);
1143965be93Schristos 	if (status == ISC_R_SUCCESS) {
1153965be93Schristos 		status = dns_tsec_create(dhcp_gbl_ctx.mctx, dns_tsectype_tsig,
1163965be93Schristos 					 dstkey, &a->tsec_key);
1173965be93Schristos 		dst_key_free(&dstkey);
1183965be93Schristos 	}
1193965be93Schristos 	if (status != ISC_R_SUCCESS)
1203965be93Schristos 		log_error("Unable to create tsec structure for %s", a->name);
1213965be93Schristos 
1223965be93Schristos 	omapi_auth_key_hash_add (auth_key_hash, a -> name, 0, a, MDL);
1233965be93Schristos 	return ISC_R_SUCCESS;
1243965be93Schristos }
1253965be93Schristos 
omapi_auth_key_lookup_name(omapi_auth_key_t ** a,const char * name)1263965be93Schristos isc_result_t omapi_auth_key_lookup_name (omapi_auth_key_t **a,
1273965be93Schristos 					 const char *name)
1283965be93Schristos {
1293965be93Schristos 	if (!auth_key_hash)
1303965be93Schristos 		return ISC_R_NOTFOUND;
1313965be93Schristos 	if (!omapi_auth_key_hash_lookup (a, auth_key_hash, name, 0, MDL))
1323965be93Schristos 		return ISC_R_NOTFOUND;
1333965be93Schristos 	return ISC_R_SUCCESS;
1343965be93Schristos }
1353965be93Schristos 
omapi_auth_key_lookup(omapi_object_t ** h,omapi_object_t * id,omapi_object_t * ref)1363965be93Schristos isc_result_t omapi_auth_key_lookup (omapi_object_t **h,
1373965be93Schristos 				    omapi_object_t *id,
1383965be93Schristos 				    omapi_object_t *ref)
1393965be93Schristos {
1403965be93Schristos 	isc_result_t status;
1413965be93Schristos 	omapi_value_t *name = (omapi_value_t *)0;
1423965be93Schristos 	omapi_value_t *algorithm = (omapi_value_t *)0;
1433965be93Schristos 
1443965be93Schristos 	if (!auth_key_hash)
1453965be93Schristos 		return ISC_R_NOTFOUND;
1463965be93Schristos 
1473965be93Schristos 	if (!ref)
1483965be93Schristos 		return DHCP_R_NOKEYS;
1493965be93Schristos 
1503965be93Schristos 	status = omapi_get_value_str (ref, id, "name", &name);
1513965be93Schristos 	if (status != ISC_R_SUCCESS)
1523965be93Schristos 		return status;
1533965be93Schristos 
1543965be93Schristos 	if ((name -> value -> type != omapi_datatype_string) &&
1553965be93Schristos 	    (name -> value -> type != omapi_datatype_data)) {
1563965be93Schristos 		omapi_value_dereference (&name, MDL);
1573965be93Schristos 		return ISC_R_NOTFOUND;
1583965be93Schristos 	}
1593965be93Schristos 
1603965be93Schristos 	status = omapi_get_value_str (ref, id, "algorithm", &algorithm);
1613965be93Schristos 	if (status != ISC_R_SUCCESS) {
1623965be93Schristos 		omapi_value_dereference (&name, MDL);
1633965be93Schristos 		return status;
1643965be93Schristos 	}
1653965be93Schristos 
1663965be93Schristos 	if ((algorithm -> value -> type != omapi_datatype_string) &&
1673965be93Schristos 	    (algorithm -> value -> type != omapi_datatype_data)) {
1683965be93Schristos 		omapi_value_dereference (&name, MDL);
1693965be93Schristos 		omapi_value_dereference (&algorithm, MDL);
1703965be93Schristos 		return ISC_R_NOTFOUND;
1713965be93Schristos 	}
1723965be93Schristos 
1733965be93Schristos 
1743965be93Schristos 	if (!omapi_auth_key_hash_lookup ((omapi_auth_key_t **)h, auth_key_hash,
1753965be93Schristos 					 (const char *)
1763965be93Schristos 					 name -> value -> u.buffer.value,
1773965be93Schristos 					 name -> value -> u.buffer.len, MDL)) {
1783965be93Schristos 		omapi_value_dereference (&name, MDL);
1793965be93Schristos 		omapi_value_dereference (&algorithm, MDL);
1803965be93Schristos 		return ISC_R_NOTFOUND;
1813965be93Schristos 	}
1823965be93Schristos 
1833965be93Schristos 	if (omapi_td_strcasecmp (algorithm -> value,
1843965be93Schristos 				 ((omapi_auth_key_t *)*h) -> algorithm) != 0) {
1853965be93Schristos 		omapi_value_dereference (&name, MDL);
1863965be93Schristos 		omapi_value_dereference (&algorithm, MDL);
1873965be93Schristos 		omapi_object_dereference (h, MDL);
1883965be93Schristos 		return ISC_R_NOTFOUND;
1893965be93Schristos 	}
1903965be93Schristos 
1913965be93Schristos 	omapi_value_dereference (&name, MDL);
1923965be93Schristos 	omapi_value_dereference (&algorithm, MDL);
1933965be93Schristos 
1943965be93Schristos 	return ISC_R_SUCCESS;
1953965be93Schristos }
1963965be93Schristos 
omapi_auth_key_stuff_values(omapi_object_t * c,omapi_object_t * id,omapi_object_t * h)1973965be93Schristos isc_result_t omapi_auth_key_stuff_values (omapi_object_t *c,
1983965be93Schristos 					  omapi_object_t *id,
1993965be93Schristos 					  omapi_object_t *h)
2003965be93Schristos {
2013965be93Schristos 	omapi_auth_key_t *a;
2023965be93Schristos 	isc_result_t status;
2033965be93Schristos 
2043965be93Schristos 	if (h -> type != omapi_type_auth_key)
2053965be93Schristos 		return DHCP_R_INVALIDARG;
2063965be93Schristos 	a = (omapi_auth_key_t *)h;
2073965be93Schristos 
2083965be93Schristos 	/* Write only the name and algorithm -- not the secret! */
2093965be93Schristos 	if (a -> name) {
2103965be93Schristos 		status = omapi_connection_put_name (c, "name");
2113965be93Schristos 		if (status != ISC_R_SUCCESS)
2123965be93Schristos 			return status;
2133965be93Schristos 		status = omapi_connection_put_string (c, a -> name);
2143965be93Schristos 		if (status != ISC_R_SUCCESS)
2153965be93Schristos 			return status;
2163965be93Schristos 	}
2173965be93Schristos 	if (a -> algorithm) {
2183965be93Schristos 		status = omapi_connection_put_name (c, "algorithm");
2193965be93Schristos 		if (status != ISC_R_SUCCESS)
2203965be93Schristos 			return status;
2213965be93Schristos 		status = omapi_connection_put_string (c, a -> algorithm);
2223965be93Schristos 		if (status != ISC_R_SUCCESS)
2233965be93Schristos 			return status;
2243965be93Schristos 	}
2253965be93Schristos 
2263965be93Schristos 	return ISC_R_SUCCESS;
2273965be93Schristos }
2283965be93Schristos 
omapi_auth_key_get_value(omapi_object_t * h,omapi_object_t * id,omapi_data_string_t * name,omapi_value_t ** value)2293965be93Schristos isc_result_t omapi_auth_key_get_value (omapi_object_t *h,
2303965be93Schristos 				       omapi_object_t *id,
2313965be93Schristos 				       omapi_data_string_t *name,
2323965be93Schristos 				       omapi_value_t **value)
2333965be93Schristos {
2343965be93Schristos 	omapi_auth_key_t *a;
2353965be93Schristos 	isc_result_t status;
2363965be93Schristos 
2373965be93Schristos 	if (h -> type != omapi_type_auth_key)
2383965be93Schristos 		return ISC_R_UNEXPECTED;
2393965be93Schristos 	a = (omapi_auth_key_t *)h;
2403965be93Schristos 
2413965be93Schristos 	if (omapi_ds_strcmp (name, "name") == 0) {
2423965be93Schristos 		if (a -> name)
2433965be93Schristos 			return omapi_make_string_value
2443965be93Schristos 				(value, name, a -> name, MDL);
2453965be93Schristos 		else
2463965be93Schristos 			return ISC_R_NOTFOUND;
2473965be93Schristos 	} else if (omapi_ds_strcmp (name, "key") == 0) {
2483965be93Schristos 		if (a -> key) {
2493965be93Schristos 			status = omapi_value_new (value, MDL);
2503965be93Schristos 			if (status != ISC_R_SUCCESS)
2513965be93Schristos 				return status;
2523965be93Schristos 
2533965be93Schristos 			status = omapi_data_string_reference
2543965be93Schristos 				(&(*value) -> name, name, MDL);
2553965be93Schristos 			if (status != ISC_R_SUCCESS) {
2563965be93Schristos 				omapi_value_dereference (value, MDL);
2573965be93Schristos 				return status;
2583965be93Schristos 			}
2593965be93Schristos 
2603965be93Schristos 			status = omapi_typed_data_new (MDL, &(*value) -> value,
2613965be93Schristos 						       omapi_datatype_data,
2623965be93Schristos 						       a -> key -> len);
2633965be93Schristos 			if (status != ISC_R_SUCCESS) {
2643965be93Schristos 				omapi_value_dereference (value, MDL);
2653965be93Schristos 				return status;
2663965be93Schristos 			}
2673965be93Schristos 
2683965be93Schristos 			memcpy ((*value) -> value -> u.buffer.value,
2693965be93Schristos 				a -> key -> value, a -> key -> len);
2703965be93Schristos 			return ISC_R_SUCCESS;
2713965be93Schristos 		} else
2723965be93Schristos 			return ISC_R_NOTFOUND;
2733965be93Schristos 	} else if (omapi_ds_strcmp (name, "algorithm") == 0) {
2743965be93Schristos 		if (a -> algorithm)
2753965be93Schristos 			return omapi_make_string_value
2763965be93Schristos 				(value, name, a -> algorithm, MDL);
2773965be93Schristos 		else
2783965be93Schristos 			return ISC_R_NOTFOUND;
2793965be93Schristos 	}
2803965be93Schristos 
2813965be93Schristos 	return ISC_R_SUCCESS;
2823965be93Schristos }
283