libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / kdc / kdc-plugin.c
blob925c250597a272fbb22b3b5aa729c7711843685f
1 /*
2 * Copyright (c) 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions (c) 2021, 2022 PADL Software Pty Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "kdc_locl.h"
38 static int have_plugin = 0;
41 * Pick the first KDC plugin module that we find.
44 static const char *kdc_plugin_deps[] = {
45 "kdc",
46 "krb5",
47 "hdb",
48 NULL
51 static struct heim_plugin_data kdc_plugin_data = {
52 "krb5",
53 "kdc",
54 KRB5_PLUGIN_KDC_VERSION_10,
55 kdc_plugin_deps,
56 kdc_get_instance
59 static krb5_error_code KRB5_LIB_CALL
60 load(krb5_context context, const void *plug, void *plugctx, void *userctx)
62 have_plugin = 1;
63 return KRB5_PLUGIN_NO_HANDLE;
66 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
67 krb5_kdc_plugin_init(krb5_context context)
69 (void)_krb5_plugin_run_f(context, &kdc_plugin_data, 0, NULL, load);
71 return 0;
74 struct generate_uc {
75 astgs_request_t r;
76 hdb_entry *client;
77 hdb_entry *server;
78 const krb5_keyblock *reply_key;
79 uint64_t pac_attributes;
80 krb5_pac *pac;
83 static krb5_error_code KRB5_LIB_CALL
84 generate(krb5_context context, const void *plug, void *plugctx, void *userctx)
86 const krb5plugin_kdc_ftable *ft = (const krb5plugin_kdc_ftable *)plug;
87 struct generate_uc *uc = (struct generate_uc *)userctx;
89 if (ft->pac_generate == NULL)
90 return KRB5_PLUGIN_NO_HANDLE;
92 return ft->pac_generate((void *)plug,
93 uc->r,
94 uc->client,
95 uc->server,
96 uc->reply_key,
97 uc->pac_attributes,
98 uc->pac);
102 krb5_error_code
103 _kdc_pac_generate(astgs_request_t r,
104 hdb_entry *client,
105 hdb_entry *server,
106 const krb5_keyblock *reply_key,
107 uint64_t pac_attributes,
108 krb5_pac *pac)
110 krb5_error_code ret = 0;
111 struct generate_uc uc;
113 *pac = NULL;
115 if (krb5_config_get_bool_default(r->context, NULL, FALSE, "realms",
116 client->principal->realm,
117 "disable_pac", NULL))
118 return 0;
120 if (have_plugin) {
121 uc.r = r;
122 uc.client = client;
123 uc.server = server;
124 uc.reply_key = reply_key;
125 uc.pac = pac;
126 uc.pac_attributes = pac_attributes;
128 ret = _krb5_plugin_run_f(r->context, &kdc_plugin_data,
129 0, &uc, generate);
130 if (ret != KRB5_PLUGIN_NO_HANDLE)
131 return ret;
132 ret = 0;
135 if (*pac == NULL)
136 ret = krb5_pac_init(r->context, pac);
138 return ret;
141 struct verify_uc {
142 astgs_request_t r;
143 krb5_principal client_principal;
144 krb5_principal delegated_proxy_principal;
145 hdb_entry *client;
146 hdb_entry *server;
147 hdb_entry *krbtgt;
148 krb5_pac *pac;
151 static krb5_error_code KRB5_LIB_CALL
152 verify(krb5_context context, const void *plug, void *plugctx, void *userctx)
154 const krb5plugin_kdc_ftable *ft = (const krb5plugin_kdc_ftable *)plug;
155 struct verify_uc *uc = (struct verify_uc *)userctx;
156 krb5_error_code ret;
158 if (ft->pac_verify == NULL)
159 return KRB5_PLUGIN_NO_HANDLE;
161 ret = ft->pac_verify((void *)plug,
162 uc->r,
163 uc->client_principal,
164 uc->delegated_proxy_principal,
165 uc->client, uc->server, uc->krbtgt, uc->pac);
166 return ret;
169 krb5_error_code
170 _kdc_pac_verify(astgs_request_t r,
171 const krb5_principal client_principal,
172 const krb5_principal delegated_proxy_principal,
173 hdb_entry *client,
174 hdb_entry *server,
175 hdb_entry *krbtgt,
176 krb5_pac *pac)
178 struct verify_uc uc;
180 if (!have_plugin)
181 return KRB5_PLUGIN_NO_HANDLE;
183 uc.r = r;
184 uc.client_principal = client_principal;
185 uc.delegated_proxy_principal = delegated_proxy_principal;
186 uc.client = client;
187 uc.server = server;
188 uc.krbtgt = krbtgt;
189 uc.pac = pac;
191 return _krb5_plugin_run_f(r->context, &kdc_plugin_data,
192 0, &uc, verify);
195 static krb5_error_code KRB5_LIB_CALL
196 check(krb5_context context, const void *plug, void *plugctx, void *userctx)
198 const krb5plugin_kdc_ftable *ft = (const krb5plugin_kdc_ftable *)plug;
200 if (ft->client_access == NULL)
201 return KRB5_PLUGIN_NO_HANDLE;
202 return ft->client_access((void *)plug, userctx);
205 krb5_error_code
206 _kdc_check_access(astgs_request_t r)
208 krb5_error_code ret = KRB5_PLUGIN_NO_HANDLE;
210 if (have_plugin) {
211 ret = _krb5_plugin_run_f(r->context, &kdc_plugin_data,
212 0, r, check);
215 if (ret == KRB5_PLUGIN_NO_HANDLE)
216 return kdc_check_flags(r, r->req.msg_type == krb_as_req,
217 r->client, r->server);
218 return ret;
221 static krb5_error_code KRB5_LIB_CALL
222 referral_policy(krb5_context context, const void *plug, void *plugctx, void *userctx)
224 const krb5plugin_kdc_ftable *ft = (const krb5plugin_kdc_ftable *)plug;
226 if (ft->referral_policy == NULL)
227 return KRB5_PLUGIN_NO_HANDLE;
228 return ft->referral_policy((void *)plug, userctx);
231 krb5_error_code
232 _kdc_referral_policy(astgs_request_t r)
234 krb5_error_code ret = KRB5_PLUGIN_NO_HANDLE;
236 if (have_plugin)
237 ret = _krb5_plugin_run_f(r->context, &kdc_plugin_data, 0, r, referral_policy);
239 return ret;
242 static krb5_error_code KRB5_LIB_CALL
243 finalize_reply(krb5_context context, const void *plug, void *plugctx, void *userctx)
245 const krb5plugin_kdc_ftable *ft = (const krb5plugin_kdc_ftable *)plug;
247 if (ft->finalize_reply == NULL)
248 return KRB5_PLUGIN_NO_HANDLE;
249 return ft->finalize_reply((void *)plug, userctx);
252 krb5_error_code
253 _kdc_finalize_reply(astgs_request_t r)
255 krb5_error_code ret = KRB5_PLUGIN_NO_HANDLE;
257 if (have_plugin)
258 ret = _krb5_plugin_run_f(r->context, &kdc_plugin_data, 0, r, finalize_reply);
260 if (ret == KRB5_PLUGIN_NO_HANDLE)
261 ret = 0;
263 return ret;
266 static krb5_error_code KRB5_LIB_CALL
267 audit(krb5_context context, const void *plug, void *plugctx, void *userctx)
269 const krb5plugin_kdc_ftable *ft = (const krb5plugin_kdc_ftable *)plug;
271 if (ft->audit == NULL)
272 return KRB5_PLUGIN_NO_HANDLE;
273 return ft->audit((void *)plug, userctx);
276 krb5_error_code
277 _kdc_plugin_audit(astgs_request_t r)
279 krb5_error_code ret = KRB5_PLUGIN_NO_HANDLE;
281 if (have_plugin)
282 ret = _krb5_plugin_run_f(r->context, &kdc_plugin_data, 0, r, audit);
284 if (ret == KRB5_PLUGIN_NO_HANDLE)
285 ret = 0;
287 return ret;
290 KDC_LIB_FUNCTION uintptr_t KDC_LIB_CALL
291 kdc_get_instance(const char *libname)
293 static const char *instance = "libkdc";
295 if (strcmp(libname, "kdc") == 0)
296 return (uintptr_t)instance;
297 else if (strcmp(libname, "hdb") == 0)
298 return hdb_get_instance(libname);
299 else if (strcmp(libname, "krb5") == 0)
300 return krb5_get_instance(libname);
302 return 0;
306 * Minimum API surface wrapper for libheimbase object types so it
307 * may remain a private interface, yet plugins can interact with
308 * objects.
311 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
312 kdc_object_alloc(size_t size, const char *name, kdc_type_dealloc dealloc)
314 return heim_alloc(size, name, dealloc);
317 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
318 kdc_object_retain(kdc_object_t o)
320 return heim_retain(o);
323 KDC_LIB_FUNCTION void KDC_LIB_CALL
324 kdc_object_release(kdc_object_t o)
326 heim_release(o);
329 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
330 kdc_bool_create(krb5_boolean v)
332 return heim_bool_create(v);
335 KDC_LIB_FUNCTION krb5_boolean KDC_LIB_CALL
336 kdc_bool_get_value(kdc_object_t o)
338 return heim_bool_val(o);
341 struct kdc_array_iterator_trampoline_data {
342 kdc_array_iterator_t iter;
343 void *data;
347 * Calling convention shim to avoid needing to update all internal
348 * consumers of heim_array_iterate_f()
350 static void
351 _kdc_array_iterator_trampoline(kdc_object_t o, void *data, int *stop)
353 struct kdc_array_iterator_trampoline_data *t = data;
355 t->iter(o, t->data, stop);
358 KDC_LIB_FUNCTION void KDC_LIB_CALL
359 kdc_array_iterate(kdc_array_t a, void *d, kdc_array_iterator_t iter)
361 struct kdc_array_iterator_trampoline_data t;
363 t.iter = iter;
364 t.data = d;
366 heim_array_iterate_f((heim_array_t)a, &t, _kdc_array_iterator_trampoline);
369 KDC_LIB_FUNCTION size_t KDC_LIB_CALL
370 kdc_array_get_length(kdc_array_t a)
372 return heim_array_get_length((heim_array_t)a);
375 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
376 kdc_array_get_value(heim_array_t a, size_t i)
378 return heim_array_get_value((heim_array_t)a, i);
381 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
382 kdc_array_copy_value(heim_array_t a, size_t i)
384 return heim_array_copy_value((heim_array_t)a, i);
387 KDC_LIB_FUNCTION kdc_string_t KDC_LIB_CALL
388 kdc_string_create(const char *s)
390 return (kdc_string_t)heim_string_create(s);
393 KDC_LIB_FUNCTION const char * KDC_LIB_CALL
394 kdc_string_get_utf8(kdc_string_t s)
396 return heim_string_get_utf8((heim_string_t)s);
399 KDC_LIB_FUNCTION kdc_data_t
400 kdc_data_create(const void *d, size_t len)
402 return (kdc_data_t)heim_data_create(d, len);
405 KDC_LIB_FUNCTION const krb5_data * KDC_LIB_CALL
406 kdc_data_get_data(kdc_data_t d)
408 return heim_data_get_data((heim_data_t)d);
411 KDC_LIB_FUNCTION kdc_number_t KDC_LIB_CALL
412 kdc_number_create(int64_t v)
414 return (kdc_number_t)heim_number_create(v);
417 KDC_LIB_FUNCTION int64_t KDC_LIB_CALL
418 kdc_number_get_value(kdc_number_t n)
420 return heim_number_get_long((heim_number_t)n);
424 * Plugin accessors
427 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
428 kdc_request_add_reply_padata(astgs_request_t r, PA_DATA *md)
430 heim_assert(r->rep.padata != NULL, "reply padata not allocated");
431 return add_METHOD_DATA(r->rep.padata, md);
434 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
435 kdc_request_add_encrypted_padata(astgs_request_t r, PA_DATA *md)
437 if (r->ek.encrypted_pa_data == NULL) {
438 r->ek.encrypted_pa_data = calloc(1, sizeof *(r->ek.encrypted_pa_data));
439 if (r->ek.encrypted_pa_data == NULL) {
440 return ENOMEM;
444 return add_METHOD_DATA(r->ek.encrypted_pa_data, md);
447 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
448 kdc_request_add_pac_buffer(astgs_request_t r,
449 uint32_t pactype,
450 const krb5_data *d)
452 krb5_error_code ret;
453 krb5_pac pac;
455 if (r->pac == NULL) {
456 ret = krb5_pac_init(r->context, &pac);
457 if (ret)
458 return ret;
459 } else
460 pac = heim_retain(r->pac);
462 ret = krb5_pac_add_buffer(r->context, pac, pactype, d);
463 if (ret == 0 && r->pac == NULL)
464 r->pac = pac;
465 else
466 heim_release(pac);
468 return ret;
471 #undef _KDC_REQUEST_GET_ACCESSOR
472 #define _KDC_REQUEST_GET_ACCESSOR(R, T, f) \
473 KDC_LIB_FUNCTION T KDC_LIB_CALL \
474 kdc_request_get_ ## f(R r) \
476 return r->f; \
479 #undef _KDC_REQUEST_SET_ACCESSOR
480 #define _KDC_REQUEST_SET_ACCESSOR(R, T, f) \
481 KDC_LIB_FUNCTION void KDC_LIB_CALL \
482 kdc_request_set_ ## f(R r, T v) \
484 r->f = v; \
487 #undef _KDC_REQUEST_GET_ACCESSOR_PTR
488 #define _KDC_REQUEST_GET_ACCESSOR_PTR(R, T, f) \
489 KDC_LIB_FUNCTION const T KDC_LIB_CALL \
490 kdc_request_get_ ## f(R r) \
492 return r->f; \
495 #undef _KDC_REQUEST_SET_ACCESSOR_PTR
496 #define _KDC_REQUEST_SET_ACCESSOR_PTR(R, T, t, f) \
497 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL \
498 kdc_request_set_ ## f(R r, const T v) \
500 krb5_error_code ret; \
501 T tmp; \
503 if (v == r->f) \
504 return 0; \
505 else if (v) { \
506 ret = copy_##t(v, &tmp); \
507 if (ret) \
508 return ret; \
509 } else \
510 tmp = NULL; \
512 free_##t(r->f); \
513 r->f = tmp; \
515 return 0; \
518 #undef _KDC_REQUEST_GET_ACCESSOR_STRUCT
519 #define _KDC_REQUEST_GET_ACCESSOR_STRUCT(R, T, f) \
520 KDC_LIB_FUNCTION const T * KDC_LIB_CALL \
521 kdc_request_get_ ## f(R r) \
523 return &r->f; \
526 #undef _KDC_REQUEST_SET_ACCESSOR_STRUCT
527 #define _KDC_REQUEST_SET_ACCESSOR_STRUCT(R, T, t, f) \
528 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL \
529 kdc_request_set_ ## f(R r, const T *v) \
531 krb5_error_code ret; \
532 T tmp; \
534 if (v == NULL) \
535 return EINVAL; \
536 else if (v == &r->f) \
537 return 0; \
539 ret = copy_##t(v, &tmp); \
540 if (ret) \
541 return ret; \
543 free_##t(&r->f); \
544 r->f = tmp; \
546 return 0; \
549 static krb5_error_code
550 copy_string_ptr(const char *src, char **dst)
552 *dst = strdup(src);
553 if (*dst == NULL)
554 return ENOMEM;
556 return 0;
559 static void
560 free_string_ptr(char *s)
562 free(s);
565 static krb5_error_code
566 copy_Principal_ptr(krb5_const_principal src, krb5_principal *dst)
568 krb5_error_code ret;
569 krb5_principal p;
571 *dst = NULL;
573 p = calloc(1, sizeof(*p));
574 if (p == NULL)
575 return ENOMEM;
577 ret = copy_Principal(src, p);
578 if (ret == 0)
579 *dst = p;
580 else
581 free(p);
583 return ret;
586 static void
587 free_Principal_ptr(krb5_principal p)
589 if (p) {
590 free_Principal(p);
591 free(p);
595 static krb5_error_code
596 copy_pac(const struct krb5_pac_data *src, struct krb5_pac_data **dst)
598 /* FIXME use heim_copy() when it exists */
599 *dst = (krb5_pac)heim_retain((heim_object_t)src);
600 return 0;
603 static void
604 free_pac(struct krb5_pac_data *o)
606 heim_release(o);
609 static krb5_error_code
610 copy_keyblock(const EncryptionKey *src, EncryptionKey *dst)
612 return copy_EncryptionKey(src, dst);
615 static void
616 free_keyblock(EncryptionKey *key)
618 krb5_free_keyblock_contents(NULL, key);
621 #undef HEIMDAL_KDC_KDC_ACCESSORS_H
622 #include "kdc-accessors.h"
624 #undef _KDC_REQUEST_GET_ACCESSOR
625 #undef _KDC_REQUEST_SET_ACCESSOR
627 #undef _KDC_REQUEST_GET_ACCESSOR_PTR
628 #undef _KDC_REQUEST_SET_ACCESSOR_PTR
629 #define _KDC_REQUEST_SET_ACCESSOR_PTR(R, T, t, f) \
630 void \
631 _kdc_request_set_ ## f ## _nocopy(R r, T *v) \
633 if (*v != r->f) { \
634 free_##t(r->f); \
635 r->f = *v; \
637 *v = NULL; \
640 #undef _KDC_REQUEST_GET_ACCESSOR_STRUCT
641 #undef _KDC_REQUEST_SET_ACCESSOR_STRUCT
642 #define _KDC_REQUEST_SET_ACCESSOR_STRUCT(R, T, t, f) \
643 void \
644 _kdc_request_set_ ## f ## _nocopy(R r, T *v) \
646 if (v != &r->f) { \
647 free_##t(&r->f); \
648 r->f = *v; \
650 memset(v, 0, sizeof(*v)); \
653 #undef HEIMDAL_KDC_KDC_ACCESSORS_H
654 #include "kdc-accessors.h"