2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
38 * Test that removal entry from of empty keytab doesn't corrupts
43 test_empty_keytab(krb5_context context
, const char *keytab
)
47 krb5_keytab_entry entry
;
49 ret
= krb5_kt_resolve(context
, keytab
, &id
);
51 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
53 memset(&entry
, 0, sizeof(entry
));
55 krb5_kt_remove_entry(context
, id
, &entry
);
57 ret
= krb5_kt_have_content(context
, id
);
59 krb5_errx(context
, 1, "supposed to be empty keytab isn't");
61 ret
= krb5_kt_close(context
, id
);
63 krb5_err(context
, 1, ret
, "krb5_kt_close");
67 * Test that memory keytab are refcounted.
71 test_memory_keytab(krb5_context context
, const char *keytab
, const char *keytab2
)
74 krb5_keytab id
, id2
, id3
;
75 krb5_keytab_entry entry
, entry2
, entry3
;
77 ret
= krb5_kt_resolve(context
, keytab
, &id
);
79 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
81 memset(&entry
, 0, sizeof(entry
));
82 ret
= krb5_parse_name(context
, "lha@SU.SE", &entry
.principal
);
84 krb5_err(context
, 1, ret
, "krb5_parse_name");
86 ret
= krb5_generate_random_keyblock(context
,
87 ETYPE_AES256_CTS_HMAC_SHA1_96
,
90 krb5_err(context
, 1, ret
, "krb5_generate_random_keyblock");
92 krb5_kt_add_entry(context
, id
, &entry
);
94 ret
= krb5_kt_resolve(context
, keytab
, &id2
);
96 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
98 ret
= krb5_kt_get_entry(context
, id
,
101 ETYPE_AES256_CTS_HMAC_SHA1_96
,
104 krb5_err(context
, 1, ret
, "krb5_kt_get_entry");
105 krb5_kt_free_entry(context
, &entry2
);
107 ret
= krb5_kt_close(context
, id
);
109 krb5_err(context
, 1, ret
, "krb5_kt_close");
111 ret
= krb5_kt_get_entry(context
, id2
,
114 ETYPE_AES256_CTS_HMAC_SHA1_96
,
117 krb5_err(context
, 1, ret
, "krb5_kt_get_entry");
118 krb5_kt_free_entry(context
, &entry2
);
120 ret
= krb5_kt_close(context
, id2
);
122 krb5_err(context
, 1, ret
, "krb5_kt_close");
125 ret
= krb5_kt_resolve(context
, keytab2
, &id3
);
127 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
129 memset(&entry3
, 0, sizeof(entry3
));
130 ret
= krb5_parse_name(context
, "lha3@SU.SE", &entry3
.principal
);
132 krb5_err(context
, 1, ret
, "krb5_parse_name");
134 ret
= krb5_generate_random_keyblock(context
,
135 ETYPE_AES256_CTS_HMAC_SHA1_96
,
138 krb5_err(context
, 1, ret
, "krb5_generate_random_keyblock");
140 krb5_kt_add_entry(context
, id3
, &entry3
);
143 ret
= krb5_kt_resolve(context
, keytab
, &id
);
145 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
147 ret
= krb5_kt_get_entry(context
, id
,
150 ETYPE_AES256_CTS_HMAC_SHA1_96
,
153 krb5_errx(context
, 1, "krb5_kt_get_entry when if should fail");
155 krb5_kt_remove_entry(context
, id
, &entry
);
157 ret
= krb5_kt_close(context
, id
);
159 krb5_err(context
, 1, ret
, "krb5_kt_close");
161 krb5_kt_free_entry(context
, &entry
);
163 krb5_kt_remove_entry(context
, id3
, &entry3
);
165 ret
= krb5_kt_close(context
, id3
);
167 krb5_err(context
, 1, ret
, "krb5_kt_close");
169 krb5_free_principal(context
, entry3
.principal
);
170 krb5_free_keyblock_contents(context
, &entry3
.keyblock
);
174 perf_add(krb5_context context
, krb5_keytab id
, int times
)
179 perf_find(krb5_context context
, krb5_keytab id
, int times
)
184 perf_delete(krb5_context context
, krb5_keytab id
, int forward
, int times
)
189 static int version_flag
= 0;
190 static int help_flag
= 0;
191 static char *perf_str
= NULL
;
192 static int times
= 1000;
194 static struct getargs args
[] = {
195 {"performance", 0, arg_string
, &perf_str
,
196 "test performance for named keytab", "keytab" },
197 {"times", 0, arg_integer
, ×
,
198 "number of times to run the perforamce test", "number" },
199 {"version", 0, arg_flag
, &version_flag
,
200 "print version", NULL
},
201 {"help", 0, arg_flag
, &help_flag
,
208 arg_printusage (args
,
209 sizeof(args
)/sizeof(*args
),
216 main(int argc
, char **argv
)
218 krb5_context context
;
222 setprogname(argv
[0]);
224 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
239 errx(1, "argc != 0");
241 ret
= krb5_init_context(&context
);
243 errx (1, "krb5_init_context failed: %d", ret
);
248 ret
= krb5_kt_resolve(context
, perf_str
, &id
);
250 krb5_err(context
, 1, ret
, "krb5_kt_resolve: %s", perf_str
);
252 /* add, find, delete on keytab */
253 perf_add(context
, id
, times
);
254 perf_find(context
, id
, times
);
255 perf_delete(context
, id
, 0, times
);
257 /* add and find again on used keytab */
258 perf_add(context
, id
, times
);
259 perf_find(context
, id
, times
);
261 ret
= krb5_kt_destroy(context
, id
);
263 krb5_err(context
, 1, ret
, "krb5_kt_destroy: %s", perf_str
);
265 ret
= krb5_kt_resolve(context
, perf_str
, &id
);
267 krb5_err(context
, 1, ret
, "krb5_kt_resolve: %s", perf_str
);
269 /* try delete backwards */
271 perf_add(context
, id
, times
);
272 perf_delete(context
, id
, 1, times
);
275 ret
= krb5_kt_destroy(context
, id
);
277 krb5_err(context
, 1, ret
, "krb5_kt_destroy");
281 test_empty_keytab(context
, "MEMORY:foo");
282 test_empty_keytab(context
, "FILE:foo");
284 test_memory_keytab(context
, "MEMORY:foo", "MEMORY:foo2");
288 krb5_free_context(context
);