1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/etinfo.c - Test harness for KDC etype-info behavior */
4 * Copyright (C) 2015 by the Massachusetts Institute of Technology.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Send an AS-REQ to the KDC for a specified principal, with an optionally
35 * specified request enctype list. Decode the output as either an AS-REP or a
36 * KRB-ERROR and display the PA-ETYPE-INFO2, PA-ETYPE-INFO, and PA-PW-SALT
37 * padata in the following format:
39 * error/asrep etype-info2/etype-info/pw-salt enctype salt [s2kparams]
41 * enctype is omitted for PA-PW-SALT entries. salt is displayed directly;
42 * s2kparams is displayed in uppercase hex.
47 static krb5_context ctx;
50 check(krb5_error_code code)
55 errmsg = krb5_get_error_message(ctx, code);
56 fprintf(stderr, "%s\n", errmsg);
57 krb5_free_error_message(ctx, errmsg);
63 display_etinfo(krb5_etype_info_entry **list, const char *l1, const char *l2)
65 krb5_etype_info_entry *info;
69 for (; *list != NULL; list++) {
71 check(krb5_enctype_to_name(info->etype, TRUE, etname, sizeof(etname)));
72 printf("%s %s %s ", l1, l2, etname);
73 if (info->length != KRB5_ETYPE_NO_SALT)
74 printf("%.*s", info->length, info->salt);
77 if (info->s2kparams.length > 0) {
79 for (i = 0; i < info->s2kparams.length; i++)
80 printf("%02X", (unsigned char)info->s2kparams.data[i]);
87 display_padata(krb5_pa_data **pa_list, const char *label)
91 krb5_etype_info_entry **etinfo_list;
93 for (; pa_list != NULL && *pa_list != NULL; pa_list++) {
95 d = make_data(pa->contents, pa->length);
96 if (pa->pa_type == KRB5_PADATA_ETYPE_INFO2) {
97 check(decode_krb5_etype_info2(&d, &etinfo_list));
98 display_etinfo(etinfo_list, label, "etype_info2");
99 krb5_free_etype_info(ctx, etinfo_list);
100 } else if (pa->pa_type == KRB5_PADATA_ETYPE_INFO) {
101 check(decode_krb5_etype_info(&d, &etinfo_list));
102 display_etinfo(etinfo_list, label, "etype_info");
103 krb5_free_etype_info(ctx, etinfo_list);
104 } else if (pa->pa_type == KRB5_PADATA_PW_SALT) {
105 printf("%s pw_salt %.*s\n", label, (int)d.length, d.data);
106 } else if (pa->pa_type == KRB5_PADATA_AFS3_SALT) {
107 printf("%s afs3_salt %.*s\n", label, (int)d.length, d.data);
113 main(int argc, char **argv)
115 krb5_principal client;
116 krb5_get_init_creds_opt *opt;
117 krb5_init_creds_context icc;
118 krb5_data reply, request, realm;
121 krb5_pa_data **padata;
122 krb5_enctype *enctypes, def[] = { ENCTYPE_NULL };
123 krb5_preauthtype pa_type = KRB5_PADATA_NONE;
127 if (argc < 2 && argc > 4) {
128 fprintf(stderr, "Usage: %s princname [enctypes] [patype]\n", argv[0]);
131 check(krb5_init_context(&ctx));
132 check(krb5_parse_name(ctx, argv[1], &client));
134 check(krb5int_parse_enctype_list(ctx, "", argv[2], def, &enctypes));
135 krb5_set_default_in_tkt_ktypes(ctx, enctypes);
139 pa_type = atoi(argv[3]);
141 check(krb5_get_init_creds_opt_alloc(ctx, &opt));
142 if (pa_type != KRB5_PADATA_NONE)
143 krb5_get_init_creds_opt_set_preauth_list(opt, &pa_type, 1);
145 check(krb5_init_creds_init(ctx, client, NULL, NULL, 0, opt, &icc));
146 reply = empty_data();
147 check(krb5_init_creds_step(ctx, icc, &reply, &request, &realm, &flags));
148 assert(flags == KRB5_INIT_CREDS_STEP_FLAG_CONTINUE);
149 check(krb5_sendto_kdc(ctx, &request, &realm, &reply, &master, 0));
151 if (decode_krb5_error(&reply, &error) == 0) {
152 decode_krb5_padata_sequence(&error->e_data, &padata);
153 if (error->error == KDC_ERR_PREAUTH_REQUIRED) {
154 display_padata(padata, "error");
155 } else if (error->error == KDC_ERR_MORE_PREAUTH_DATA_REQUIRED) {
156 display_padata(padata, "more");
158 fprintf(stderr, "Unexpected error %d\n", (int)error->error);
161 krb5_free_pa_data(ctx, padata);
162 krb5_free_error(ctx, error);
163 } else if (decode_krb5_as_rep(&reply, &asrep) == 0) {
164 display_padata(asrep->padata, "asrep");
165 krb5_free_kdc_rep(ctx, asrep);
170 krb5_free_data_contents(ctx, &request);
171 krb5_free_data_contents(ctx, &reply);
172 krb5_free_data_contents(ctx, &realm);
173 krb5_get_init_creds_opt_free(ctx, opt);
174 krb5_init_creds_free(ctx, icc);
175 krb5_free_principal(ctx, client);
176 krb5_free_context(ctx);