Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / tests / etinfo.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/etinfo.c - Test harness for KDC etype-info behavior */
3 /*
4  * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
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
17  *   distribution.
18  *
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.
31  */
32
33 /*
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:
38  *
39  *     error/asrep etype-info2/etype-info/pw-salt enctype salt [s2kparams]
40  *
41  * enctype is omitted for PA-PW-SALT entries.  salt is displayed directly;
42  * s2kparams is displayed in uppercase hex.
43  */
44
45 #include "k5-int.h"
46
47 static krb5_context ctx;
48
49 static void
50 check(krb5_error_code code)
51 {
52     const char *errmsg;
53
54     if (code) {
55         errmsg = krb5_get_error_message(ctx, code);
56         fprintf(stderr, "%s\n", errmsg);
57         krb5_free_error_message(ctx, errmsg);
58         exit(1);
59     }
60 }
61
62 static void
63 display_etinfo(krb5_etype_info_entry **list, const char *l1, const char *l2)
64 {
65     krb5_etype_info_entry *info;
66     char etname[256];
67     unsigned int i;
68
69     for (; *list != NULL; list++) {
70         info = *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);
75         else
76             printf("(default)");
77         if (info->s2kparams.length > 0) {
78             printf(" ");
79             for (i = 0; i < info->s2kparams.length; i++)
80                 printf("%02X", (unsigned char)info->s2kparams.data[i]);
81         }
82         printf("\n");
83     }
84 }
85
86 static void
87 display_padata(krb5_pa_data **pa_list, const char *label)
88 {
89     krb5_pa_data *pa;
90     krb5_data d;
91     krb5_etype_info_entry **etinfo_list;
92
93     for (; pa_list != NULL && *pa_list != NULL; pa_list++) {
94         pa = *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);
108         }
109     }
110 }
111
112 int
113 main(int argc, char **argv)
114 {
115     krb5_principal client;
116     krb5_get_init_creds_opt *opt;
117     krb5_init_creds_context icc;
118     krb5_data reply, request, realm;
119     krb5_error *error;
120     krb5_kdc_rep *asrep;
121     krb5_pa_data **padata;
122     krb5_enctype *enctypes, def[] = { ENCTYPE_NULL };
123     krb5_preauthtype pa_type = KRB5_PADATA_NONE;
124     unsigned int flags;
125     int master = 0;
126
127     if (argc < 2 && argc > 4) {
128         fprintf(stderr, "Usage: %s princname [enctypes] [patype]\n", argv[0]);
129         exit(1);
130     }
131     check(krb5_init_context(&ctx));
132     check(krb5_parse_name(ctx, argv[1], &client));
133     if (argc >= 3) {
134         check(krb5int_parse_enctype_list(ctx, "", argv[2], def, &enctypes));
135         krb5_set_default_in_tkt_ktypes(ctx, enctypes);
136         free(enctypes);
137     }
138     if (argc >= 4)
139         pa_type = atoi(argv[3]);
140
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);
144
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));
150
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");
157         } else {
158             fprintf(stderr, "Unexpected error %d\n", (int)error->error);
159             return 1;
160         }
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);
166     } else {
167         abort();
168     }
169
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);
177     return 0;
178 }