3 Subroutines having to do with authentication. */
6 * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1998-2003 by Internet Software Consortium
9 * This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was not distributed with this
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
25 * https://www.isc.org/
31 #include <omapip/omapip_p.h>
33 OMAPI_OBJECT_ALLOC (omapi_auth_key
, omapi_auth_key_t
, omapi_type_auth_key
)
34 typedef struct hash omapi_auth_hash_t
;
35 HASH_FUNCTIONS_DECL (omapi_auth_key
, const char *,
36 omapi_auth_key_t
, omapi_auth_hash_t
)
37 omapi_auth_hash_t
*auth_key_hash
;
38 HASH_FUNCTIONS (omapi_auth_key
, const char *, omapi_auth_key_t
,
40 omapi_auth_key_reference
, omapi_auth_key_dereference
,
43 isc_result_t
omapi_auth_key_new (omapi_auth_key_t
**o
, const char *file
,
46 return omapi_auth_key_allocate (o
, file
, line
);
49 isc_result_t
omapi_auth_key_destroy (omapi_object_t
*h
,
50 const char *file
, int line
)
54 if (h
->type
!= omapi_type_auth_key
)
55 return DHCP_R_INVALIDARG
;
56 a
= (omapi_auth_key_t
*)h
;
58 if (auth_key_hash
!= NULL
)
59 omapi_auth_key_hash_delete(auth_key_hash
, a
->name
, 0, MDL
);
63 if (a
->algorithm
!= NULL
)
64 dfree(a
->algorithm
, MDL
);
66 omapi_data_string_dereference(&a
->key
, MDL
);
67 if (a
->tsec_key
!= NULL
)
68 dns_tsec_destroy(&a
->tsec_key
);
73 isc_result_t
omapi_auth_key_enter (omapi_auth_key_t
*a
)
79 if (a
-> type
!= omapi_type_auth_key
)
80 return DHCP_R_INVALIDARG
;
82 tk
= (omapi_auth_key_t
*)0;
84 omapi_auth_key_hash_lookup (&tk
, auth_key_hash
,
87 omapi_auth_key_dereference (&tk
, MDL
);
91 omapi_auth_key_hash_delete (auth_key_hash
,
93 omapi_auth_key_dereference (&tk
, MDL
);
96 if (!omapi_auth_key_new_hash(&auth_key_hash
,
98 return ISC_R_NOMEMORY
;
102 * If possible create a tsec structure for this key,
103 * if we can't create the structure we put out a warning
106 status
= isclib_make_dst_key(a
->name
, a
->algorithm
,
107 a
->key
->value
, a
->key
->len
,
109 if (status
== ISC_R_SUCCESS
) {
110 status
= dns_tsec_create(dhcp_gbl_ctx
.mctx
, dns_tsectype_tsig
,
111 dstkey
, &a
->tsec_key
);
112 dst_key_free(&dstkey
);
114 if (status
!= ISC_R_SUCCESS
)
115 log_error("Unable to create tsec structure for %s", a
->name
);
117 omapi_auth_key_hash_add (auth_key_hash
, a
-> name
, 0, a
, MDL
);
118 return ISC_R_SUCCESS
;
121 isc_result_t
omapi_auth_key_lookup_name (omapi_auth_key_t
**a
,
125 return ISC_R_NOTFOUND
;
126 if (!omapi_auth_key_hash_lookup (a
, auth_key_hash
, name
, 0, MDL
))
127 return ISC_R_NOTFOUND
;
128 return ISC_R_SUCCESS
;
131 isc_result_t
omapi_auth_key_lookup (omapi_object_t
**h
,
136 omapi_value_t
*name
= (omapi_value_t
*)0;
137 omapi_value_t
*algorithm
= (omapi_value_t
*)0;
140 return ISC_R_NOTFOUND
;
143 return DHCP_R_NOKEYS
;
145 status
= omapi_get_value_str (ref
, id
, "name", &name
);
146 if (status
!= ISC_R_SUCCESS
)
149 if ((name
-> value
-> type
!= omapi_datatype_string
) &&
150 (name
-> value
-> type
!= omapi_datatype_data
)) {
151 omapi_value_dereference (&name
, MDL
);
152 return ISC_R_NOTFOUND
;
155 status
= omapi_get_value_str (ref
, id
, "algorithm", &algorithm
);
156 if (status
!= ISC_R_SUCCESS
) {
157 omapi_value_dereference (&name
, MDL
);
161 if ((algorithm
-> value
-> type
!= omapi_datatype_string
) &&
162 (algorithm
-> value
-> type
!= omapi_datatype_data
)) {
163 omapi_value_dereference (&name
, MDL
);
164 omapi_value_dereference (&algorithm
, MDL
);
165 return ISC_R_NOTFOUND
;
169 if (!omapi_auth_key_hash_lookup ((omapi_auth_key_t
**)h
, auth_key_hash
,
171 name
-> value
-> u
.buffer
.value
,
172 name
-> value
-> u
.buffer
.len
, MDL
)) {
173 omapi_value_dereference (&name
, MDL
);
174 omapi_value_dereference (&algorithm
, MDL
);
175 return ISC_R_NOTFOUND
;
178 if (omapi_td_strcasecmp (algorithm
-> value
,
179 ((omapi_auth_key_t
*)*h
) -> algorithm
) != 0) {
180 omapi_value_dereference (&name
, MDL
);
181 omapi_value_dereference (&algorithm
, MDL
);
182 omapi_object_dereference (h
, MDL
);
183 return ISC_R_NOTFOUND
;
186 omapi_value_dereference (&name
, MDL
);
187 omapi_value_dereference (&algorithm
, MDL
);
189 return ISC_R_SUCCESS
;
192 isc_result_t
omapi_auth_key_stuff_values (omapi_object_t
*c
,
199 if (h
-> type
!= omapi_type_auth_key
)
200 return DHCP_R_INVALIDARG
;
201 a
= (omapi_auth_key_t
*)h
;
203 /* Write only the name and algorithm -- not the secret! */
205 status
= omapi_connection_put_name (c
, "name");
206 if (status
!= ISC_R_SUCCESS
)
208 status
= omapi_connection_put_string (c
, a
-> name
);
209 if (status
!= ISC_R_SUCCESS
)
212 if (a
-> algorithm
) {
213 status
= omapi_connection_put_name (c
, "algorithm");
214 if (status
!= ISC_R_SUCCESS
)
216 status
= omapi_connection_put_string (c
, a
-> algorithm
);
217 if (status
!= ISC_R_SUCCESS
)
221 return ISC_R_SUCCESS
;
224 isc_result_t
omapi_auth_key_get_value (omapi_object_t
*h
,
226 omapi_data_string_t
*name
,
227 omapi_value_t
**value
)
232 if (h
-> type
!= omapi_type_auth_key
)
233 return ISC_R_UNEXPECTED
;
234 a
= (omapi_auth_key_t
*)h
;
236 if (omapi_ds_strcmp (name
, "name") == 0) {
238 return omapi_make_string_value
239 (value
, name
, a
-> name
, MDL
);
241 return ISC_R_NOTFOUND
;
242 } else if (omapi_ds_strcmp (name
, "key") == 0) {
244 status
= omapi_value_new (value
, MDL
);
245 if (status
!= ISC_R_SUCCESS
)
248 status
= omapi_data_string_reference
249 (&(*value
) -> name
, name
, MDL
);
250 if (status
!= ISC_R_SUCCESS
) {
251 omapi_value_dereference (value
, MDL
);
255 status
= omapi_typed_data_new (MDL
, &(*value
) -> value
,
258 if (status
!= ISC_R_SUCCESS
) {
259 omapi_value_dereference (value
, MDL
);
263 memcpy ((*value
) -> value
-> u
.buffer
.value
,
264 a
-> key
-> value
, a
-> key
-> len
);
265 return ISC_R_SUCCESS
;
267 return ISC_R_NOTFOUND
;
268 } else if (omapi_ds_strcmp (name
, "algorithm") == 0) {
270 return omapi_make_string_value
271 (value
, name
, a
-> algorithm
, MDL
);
273 return ISC_R_NOTFOUND
;
276 return ISC_R_SUCCESS
;