s4:torture: Adapt KDC canon test to Heimdal upstream changes
[Samba.git] / source4 / heimdal / tests / plugin / windc.c
blob357148019ae84cdcb46e642a0446968a9376a97f
1 #include <string.h>
2 #include <krb5_locl.h>
3 #include <hdb.h>
4 #include <hx509.h>
5 #include <kdc.h>
6 #include <windc_plugin.h>
8 static krb5_error_code KRB5_CALLCONV
9 windc_init(krb5_context context, void **ctx)
11 krb5_warnx(context, "windc init");
12 *ctx = NULL;
13 return 0;
16 static void KRB5_CALLCONV
17 windc_fini(void *ctx)
21 static krb5_error_code KRB5_CALLCONV
22 pac_generate(void *ctx, krb5_context context,
23 struct hdb_entry_ex *client,
24 struct hdb_entry_ex *server,
25 const krb5_keyblock *pk_replykey,
26 uint64_t pac_attributes,
27 krb5_pac *pac)
29 krb5_error_code ret;
30 krb5_data data;
32 if ((pac_attributes & (KRB5_PAC_WAS_REQUESTED |
33 KRB5_PAC_WAS_GIVEN_IMPLICITLY)) == 0) {
34 *pac = NULL;
35 return 0;
38 krb5_warnx(context, "pac generate");
40 data.data = "\x00\x01";
41 data.length = 2;
43 ret = krb5_pac_init(context, pac);
44 if (ret)
45 return ret;
47 ret = krb5_pac_add_buffer(context, *pac, 1, &data);
48 if (ret)
49 return ret;
51 return 0;
54 static krb5_error_code KRB5_CALLCONV
55 pac_verify(void *ctx, krb5_context context,
56 const krb5_principal new_ticket_client,
57 const krb5_principal delegation_proxy,
58 struct hdb_entry_ex * client,
59 struct hdb_entry_ex * server,
60 struct hdb_entry_ex * krbtgt,
61 krb5_pac *pac)
63 krb5_error_code ret;
64 krb5_data data;
65 krb5_cksumtype cstype;
66 uint16_t rodc_id;
67 krb5_enctype etype;
68 Key *key;
70 krb5_warnx(context, "pac_verify");
72 ret = krb5_pac_get_buffer(context, *pac, 1, &data);
73 if (ret)
74 return ret;
75 krb5_data_free(&data);
77 ret = krb5_pac_get_kdc_checksum_info(context, *pac, &cstype, &rodc_id);
78 if (ret)
79 return ret;
81 if (rodc_id == 0 || rodc_id != krbtgt->entry.kvno >> 16) {
82 krb5_warnx(context, "Wrong RODCIdentifier");
83 return EINVAL;
86 ret = krb5_cksumtype_to_enctype(context, cstype, &etype);
87 if (ret)
88 return ret;
90 ret = hdb_enctype2key(context, &krbtgt->entry, NULL, etype, &key);
91 if (ret)
92 return ret;
94 return krb5_pac_verify(context, *pac, 0, NULL, NULL, &key->key);
97 static void logit(const char *what, astgs_request_t r)
99 krb5_warnx(r->context, "%s: client %s server %s",
100 what,
101 r->cname ? r->cname : "<unknown>",
102 r->sname ? r->sname : "<unknown>");
105 static krb5_error_code KRB5_CALLCONV
106 client_access(void *ctx, astgs_request_t r)
108 logit("client_access", r);
109 return 0;
112 static krb5_error_code KRB5_CALLCONV
113 finalize_reply(void *ctx, astgs_request_t r)
115 logit("finalize_reply", r);
116 return 0;
119 static krb5plugin_windc_ftable windc = {
120 KRB5_WINDC_PLUGING_MINOR,
121 windc_init,
122 windc_fini,
123 pac_generate,
124 pac_verify,
125 client_access,
126 finalize_reply
129 static const krb5plugin_windc_ftable *const windc_plugins[] = {
130 &windc
133 krb5_error_code KRB5_CALLCONV
134 windc_plugin_load(krb5_context context,
135 krb5_get_instance_func_t *get_instance,
136 size_t *num_plugins,
137 const krb5plugin_windc_ftable *const **plugins);
139 static uintptr_t KRB5_CALLCONV
140 windc_get_instance(const char *libname)
142 if (strcmp(libname, "hdb") == 0)
143 return hdb_get_instance(libname);
144 else if (strcmp(libname, "krb5") == 0)
145 return krb5_get_instance(libname);
147 return 0;
150 krb5_error_code KRB5_CALLCONV
151 windc_plugin_load(krb5_context context,
152 krb5_get_instance_func_t *get_instance,
153 size_t *num_plugins,
154 const krb5plugin_windc_ftable *const **plugins)
156 *get_instance = windc_get_instance;
157 *num_plugins = sizeof(windc_plugins) / sizeof(windc_plugins[0]);
158 *plugins = windc_plugins;
160 return 0;