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_close(context
, id
);
59 krb5_err(context
, 1, ret
, "krb5_kt_close");
63 * Test that memory keytab are refcounted.
67 test_memory_keytab(krb5_context context
, const char *keytab
, const char *keytab2
)
70 krb5_keytab id
, id2
, id3
;
71 krb5_keytab_entry entry
, entry2
, entry3
;
73 ret
= krb5_kt_resolve(context
, keytab
, &id
);
75 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
77 memset(&entry
, 0, sizeof(entry
));
78 ret
= krb5_parse_name(context
, "lha@SU.SE", &entry
.principal
);
80 krb5_err(context
, 1, ret
, "krb5_parse_name");
82 ret
= krb5_generate_random_keyblock(context
,
83 ETYPE_AES256_CTS_HMAC_SHA1_96
,
86 krb5_err(context
, 1, ret
, "krb5_generate_random_keyblock");
88 krb5_kt_add_entry(context
, id
, &entry
);
90 ret
= krb5_kt_resolve(context
, keytab
, &id2
);
92 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
94 ret
= krb5_kt_get_entry(context
, id
,
97 ETYPE_AES256_CTS_HMAC_SHA1_96
,
100 krb5_err(context
, 1, ret
, "krb5_kt_get_entry");
101 krb5_kt_free_entry(context
, &entry2
);
103 ret
= krb5_kt_close(context
, id
);
105 krb5_err(context
, 1, ret
, "krb5_kt_close");
107 ret
= krb5_kt_get_entry(context
, id2
,
110 ETYPE_AES256_CTS_HMAC_SHA1_96
,
113 krb5_err(context
, 1, ret
, "krb5_kt_get_entry");
114 krb5_kt_free_entry(context
, &entry2
);
116 ret
= krb5_kt_close(context
, id2
);
118 krb5_err(context
, 1, ret
, "krb5_kt_close");
121 ret
= krb5_kt_resolve(context
, keytab2
, &id3
);
123 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
125 memset(&entry3
, 0, sizeof(entry3
));
126 ret
= krb5_parse_name(context
, "lha3@SU.SE", &entry3
.principal
);
128 krb5_err(context
, 1, ret
, "krb5_parse_name");
130 ret
= krb5_generate_random_keyblock(context
,
131 ETYPE_AES256_CTS_HMAC_SHA1_96
,
134 krb5_err(context
, 1, ret
, "krb5_generate_random_keyblock");
136 krb5_kt_add_entry(context
, id3
, &entry3
);
139 ret
= krb5_kt_resolve(context
, keytab
, &id
);
141 krb5_err(context
, 1, ret
, "krb5_kt_resolve");
143 ret
= krb5_kt_get_entry(context
, id
,
146 ETYPE_AES256_CTS_HMAC_SHA1_96
,
149 krb5_errx(context
, 1, "krb5_kt_get_entry when if should fail");
151 krb5_kt_remove_entry(context
, id
, &entry
);
153 ret
= krb5_kt_close(context
, id
);
155 krb5_err(context
, 1, ret
, "krb5_kt_close");
157 krb5_kt_free_entry(context
, &entry
);
159 krb5_kt_remove_entry(context
, id3
, &entry3
);
161 ret
= krb5_kt_close(context
, id3
);
163 krb5_err(context
, 1, ret
, "krb5_kt_close");
165 krb5_free_principal(context
, entry3
.principal
);
166 krb5_free_keyblock_contents(context
, &entry3
.keyblock
);
170 perf_add(krb5_context context
, krb5_keytab id
, int times
)
175 perf_find(krb5_context context
, krb5_keytab id
, int times
)
180 perf_delete(krb5_context context
, krb5_keytab id
, int forward
, int times
)
185 static int version_flag
= 0;
186 static int help_flag
= 0;
187 static char *perf_str
= NULL
;
188 static int times
= 1000;
190 static struct getargs args
[] = {
191 {"performance", 0, arg_string
, &perf_str
,
192 "test performance for named keytab", "keytab" },
193 {"times", 0, arg_integer
, ×
,
194 "number of times to run the perforamce test", "number" },
195 {"version", 0, arg_flag
, &version_flag
,
196 "print version", NULL
},
197 {"help", 0, arg_flag
, &help_flag
,
204 arg_printusage (args
,
205 sizeof(args
)/sizeof(*args
),
212 main(int argc
, char **argv
)
214 krb5_context context
;
218 setprogname(argv
[0]);
220 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
235 errx(1, "argc != 0");
237 ret
= krb5_init_context(&context
);
239 errx (1, "krb5_init_context failed: %d", ret
);
244 ret
= krb5_kt_resolve(context
, perf_str
, &id
);
246 krb5_err(context
, 1, ret
, "krb5_kt_resolve: %s", perf_str
);
248 /* add, find, delete on keytab */
249 perf_add(context
, id
, times
);
250 perf_find(context
, id
, times
);
251 perf_delete(context
, id
, 0, times
);
253 /* add and find again on used keytab */
254 perf_add(context
, id
, times
);
255 perf_find(context
, id
, times
);
257 ret
= krb5_kt_destroy(context
, id
);
259 krb5_err(context
, 1, ret
, "krb5_kt_destroy: %s", perf_str
);
261 ret
= krb5_kt_resolve(context
, perf_str
, &id
);
263 krb5_err(context
, 1, ret
, "krb5_kt_resolve: %s", perf_str
);
265 /* try delete backwards */
267 perf_add(context
, id
, times
);
268 perf_delete(context
, id
, 1, times
);
271 ret
= krb5_kt_destroy(context
, id
);
273 krb5_err(context
, 1, ret
, "krb5_kt_destroy");
277 test_empty_keytab(context
, "MEMORY:foo");
278 test_empty_keytab(context
, "FILE:foo");
280 test_memory_keytab(context
, "MEMORY:foo", "MEMORY:foo2");
284 krb5_free_context(context
);